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