Filling in branch from trunk
[blender.git] / intern / ghost / test / multitest / EventToBuf.c
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #include <stdlib.h>
33
34 #include <stdio.h>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "MEM_guardedalloc.h"
41
42 #include "GHOST_C-api.h"
43 #include "EventToBuf.h"
44
45 char *eventtype_to_string(GHOST_TEventType type) {
46         switch(type) {
47         case GHOST_kEventCursorMove:            return "CursorMove";
48         case GHOST_kEventButtonDown:            return "ButtonDown";
49         case GHOST_kEventButtonUp:                      return "ButtonUp";
50
51         case GHOST_kEventKeyDown:                       return "KeyDown";
52         case GHOST_kEventKeyUp:                         return "KeyUp";
53
54         case GHOST_kEventQuit:                          return "Quit";
55
56         case GHOST_kEventWindowClose:           return "WindowClose";
57         case GHOST_kEventWindowActivate:        return "WindowActivate";        
58         case GHOST_kEventWindowDeactivate:      return "WindowDeactivate";
59         case GHOST_kEventWindowUpdate:          return "WindowUpdate";
60         case GHOST_kEventWindowSize:            return "WindowSize";
61         default:
62                 return "<invalid>";
63         }
64 }
65
66 static char *keytype_to_string(GHOST_TKey key) {
67 #define K(key)  case GHOST_k##key:      return #key;
68         switch (key) {
69         K(KeyBackSpace);
70         K(KeyTab);
71         K(KeyLinefeed);
72         K(KeyClear);
73         K(KeyEnter);
74
75         K(KeyEsc);
76         K(KeySpace);
77         K(KeyQuote);
78         K(KeyComma);
79         K(KeyMinus);
80         K(KeyPeriod);
81         K(KeySlash);
82
83         K(Key0);
84         K(Key1);
85         K(Key2);
86         K(Key3);
87         K(Key4);
88         K(Key5);
89         K(Key6);
90         K(Key7);
91         K(Key8);
92         K(Key9);
93
94         K(KeySemicolon);
95         K(KeyEqual);
96
97         K(KeyA);
98         K(KeyB);
99         K(KeyC);
100         K(KeyD);
101         K(KeyE);
102         K(KeyF);
103         K(KeyG);
104         K(KeyH);
105         K(KeyI);
106         K(KeyJ);
107         K(KeyK);
108         K(KeyL);
109         K(KeyM);
110         K(KeyN);
111         K(KeyO);
112         K(KeyP);
113         K(KeyQ);
114         K(KeyR);
115         K(KeyS);
116         K(KeyT);
117         K(KeyU);
118         K(KeyV);
119         K(KeyW);
120         K(KeyX);
121         K(KeyY);
122         K(KeyZ);
123
124         K(KeyLeftBracket);
125         K(KeyRightBracket);
126         K(KeyBackslash);
127         K(KeyAccentGrave);
128
129         K(KeyLeftShift);
130         K(KeyRightShift);
131         K(KeyLeftControl);
132         K(KeyRightControl);
133         K(KeyLeftAlt);
134         K(KeyRightAlt);
135         K(KeyCommand);
136
137         K(KeyCapsLock);
138         K(KeyNumLock);
139         K(KeyScrollLock);
140
141         K(KeyLeftArrow);
142         K(KeyRightArrow);
143         K(KeyUpArrow);
144         K(KeyDownArrow);
145
146         K(KeyPrintScreen);
147         K(KeyPause);
148
149         K(KeyInsert);
150         K(KeyDelete);
151         K(KeyHome);
152         K(KeyEnd);
153         K(KeyUpPage);
154         K(KeyDownPage);
155
156         K(KeyNumpad0);
157         K(KeyNumpad1);
158         K(KeyNumpad2);
159         K(KeyNumpad3);
160         K(KeyNumpad4);
161         K(KeyNumpad5);
162         K(KeyNumpad6);
163         K(KeyNumpad7);
164         K(KeyNumpad8);
165         K(KeyNumpad9);
166         K(KeyNumpadPeriod);
167         K(KeyNumpadEnter);
168         K(KeyNumpadPlus);
169         K(KeyNumpadMinus);
170         K(KeyNumpadAsterisk);
171         K(KeyNumpadSlash);
172
173         K(KeyF1);
174         K(KeyF2);
175         K(KeyF3);
176         K(KeyF4);
177         K(KeyF5);
178         K(KeyF6);
179         K(KeyF7);
180         K(KeyF8);
181         K(KeyF9);
182         K(KeyF10);
183         K(KeyF11);
184         K(KeyF12);
185         K(KeyF13);
186         K(KeyF14);
187         K(KeyF15);
188         K(KeyF16);
189         K(KeyF17);
190         K(KeyF18);
191         K(KeyF19);
192         K(KeyF20);
193         K(KeyF21);
194         K(KeyF22);
195         K(KeyF23);
196         K(KeyF24);
197         
198         default:
199                 return "KeyUnknown";
200         }
201 #undef K
202 }
203
204 void event_to_buf(GHOST_EventHandle evt, char buf[128]) {
205         GHOST_TEventType type= GHOST_GetEventType(evt);
206         double time= (double) ((GHOST_TInt64) GHOST_GetEventTime(evt))/1000;
207         GHOST_WindowHandle win= GHOST_GetEventWindow(evt);
208         void *data= GHOST_GetEventData(evt);
209         char *pos= buf;
210
211         pos+= sprintf(pos, "event: %6.2f, %16s", time, eventtype_to_string(type));
212         if (win) {
213                 char *s= GHOST_GetTitle(win);
214                 pos+= sprintf(pos, " - win: %s", s);
215                 free(s);
216         } else {
217                 pos+= sprintf(pos, " - sys evt");
218         }
219         switch (type) {
220         case GHOST_kEventCursorMove: {
221                 GHOST_TEventCursorData *cd= data;
222                 pos+= sprintf(pos, " - pos: (%d, %d)", cd->x, cd->y);
223                 break;
224         }
225         case GHOST_kEventButtonDown:
226         case GHOST_kEventButtonUp: {
227                 GHOST_TEventButtonData *bd= data;
228                 pos+= sprintf(pos, " - but: %d", bd->button);
229                 break;
230         }
231         
232         case GHOST_kEventKeyDown:
233         case GHOST_kEventKeyUp: {
234                 GHOST_TEventKeyData *kd= data;
235                 pos+= sprintf(pos, " - key: %s (%d)", keytype_to_string(kd->key), kd->key);
236                 if (kd->ascii) pos+= sprintf(pos, " ascii: '%c' (%d)", kd->ascii, kd->ascii);
237                 break;
238         }
239         }
240 }