Merging trunk up to revision 41245.
[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         switch(type) {
39         case GHOST_kEventCursorMove:            return "CursorMove";
40         case GHOST_kEventButtonDown:            return "ButtonDown";
41         case GHOST_kEventButtonUp:                      return "ButtonUp";
42
43         case GHOST_kEventKeyDown:                       return "KeyDown";
44         case GHOST_kEventKeyUp:                         return "KeyUp";
45
46         case GHOST_kEventQuit:                          return "Quit";
47
48         case GHOST_kEventWindowClose:           return "WindowClose";
49         case GHOST_kEventWindowActivate:        return "WindowActivate";        
50         case GHOST_kEventWindowDeactivate:      return "WindowDeactivate";
51         case GHOST_kEventWindowUpdate:          return "WindowUpdate";
52         case GHOST_kEventWindowSize:            return "WindowSize";
53         default:
54                 return "<invalid>";
55         }
56 }
57
58 static char *keytype_to_string(GHOST_TKey key) {
59 #define K(key)  case GHOST_k##key:      return #key;
60         switch (key) {
61         K(KeyBackSpace);
62         K(KeyTab);
63         K(KeyLinefeed);
64         K(KeyClear);
65         K(KeyEnter);
66
67         K(KeyEsc);
68         K(KeySpace);
69         K(KeyQuote);
70         K(KeyComma);
71         K(KeyMinus);
72         K(KeyPeriod);
73         K(KeySlash);
74
75         K(Key0);
76         K(Key1);
77         K(Key2);
78         K(Key3);
79         K(Key4);
80         K(Key5);
81         K(Key6);
82         K(Key7);
83         K(Key8);
84         K(Key9);
85
86         K(KeySemicolon);
87         K(KeyEqual);
88
89         K(KeyA);
90         K(KeyB);
91         K(KeyC);
92         K(KeyD);
93         K(KeyE);
94         K(KeyF);
95         K(KeyG);
96         K(KeyH);
97         K(KeyI);
98         K(KeyJ);
99         K(KeyK);
100         K(KeyL);
101         K(KeyM);
102         K(KeyN);
103         K(KeyO);
104         K(KeyP);
105         K(KeyQ);
106         K(KeyR);
107         K(KeyS);
108         K(KeyT);
109         K(KeyU);
110         K(KeyV);
111         K(KeyW);
112         K(KeyX);
113         K(KeyY);
114         K(KeyZ);
115
116         K(KeyLeftBracket);
117         K(KeyRightBracket);
118         K(KeyBackslash);
119         K(KeyAccentGrave);
120
121         K(KeyLeftShift);
122         K(KeyRightShift);
123         K(KeyLeftControl);
124         K(KeyRightControl);
125         K(KeyLeftAlt);
126         K(KeyRightAlt);
127         K(KeyOS);
128
129         K(KeyCapsLock);
130         K(KeyNumLock);
131         K(KeyScrollLock);
132
133         K(KeyLeftArrow);
134         K(KeyRightArrow);
135         K(KeyUpArrow);
136         K(KeyDownArrow);
137
138         K(KeyPrintScreen);
139         K(KeyPause);
140
141         K(KeyInsert);
142         K(KeyDelete);
143         K(KeyHome);
144         K(KeyEnd);
145         K(KeyUpPage);
146         K(KeyDownPage);
147
148         K(KeyNumpad0);
149         K(KeyNumpad1);
150         K(KeyNumpad2);
151         K(KeyNumpad3);
152         K(KeyNumpad4);
153         K(KeyNumpad5);
154         K(KeyNumpad6);
155         K(KeyNumpad7);
156         K(KeyNumpad8);
157         K(KeyNumpad9);
158         K(KeyNumpadPeriod);
159         K(KeyNumpadEnter);
160         K(KeyNumpadPlus);
161         K(KeyNumpadMinus);
162         K(KeyNumpadAsterisk);
163         K(KeyNumpadSlash);
164
165         K(KeyF1);
166         K(KeyF2);
167         K(KeyF3);
168         K(KeyF4);
169         K(KeyF5);
170         K(KeyF6);
171         K(KeyF7);
172         K(KeyF8);
173         K(KeyF9);
174         K(KeyF10);
175         K(KeyF11);
176         K(KeyF12);
177         K(KeyF13);
178         K(KeyF14);
179         K(KeyF15);
180         K(KeyF16);
181         K(KeyF17);
182         K(KeyF18);
183         K(KeyF19);
184         K(KeyF20);
185         K(KeyF21);
186         K(KeyF22);
187         K(KeyF23);
188         K(KeyF24);
189         
190         default:
191                 return "KeyUnknown";
192         }
193 #undef K
194 }
195
196 void event_to_buf(GHOST_EventHandle evt, char buf[128]) {
197         GHOST_TEventType type= GHOST_GetEventType(evt);
198         double time= (double) ((GHOST_TInt64) GHOST_GetEventTime(evt))/1000;
199         GHOST_WindowHandle win= GHOST_GetEventWindow(evt);
200         void *data= GHOST_GetEventData(evt);
201         char *pos= buf;
202
203         pos+= sprintf(pos, "event: %6.2f, %16s", time, eventtype_to_string(type));
204         if (win) {
205                 char *s= GHOST_GetTitle(win);
206                 pos+= sprintf(pos, " - win: %s", s);
207                 free(s);
208         } else {
209                 pos+= sprintf(pos, " - sys evt");
210         }
211         switch (type) {
212         case GHOST_kEventCursorMove: {
213                 GHOST_TEventCursorData *cd= data;
214                 pos+= sprintf(pos, " - pos: (%d, %d)", cd->x, cd->y);
215                 break;
216         }
217         case GHOST_kEventButtonDown:
218         case GHOST_kEventButtonUp: {
219                 GHOST_TEventButtonData *bd= data;
220                 pos+= sprintf(pos, " - but: %d", bd->button);
221                 break;
222         }
223         
224         case GHOST_kEventKeyDown:
225         case GHOST_kEventKeyUp: {
226                 GHOST_TEventKeyData *kd= data;
227                 pos+= sprintf(pos, " - key: %s (%d)", keytype_to_string(kd->key), kd->key);
228                 if (kd->ascii) pos+= sprintf(pos, " ascii: '%c' (%d)", kd->ascii, kd->ascii);
229                 break;
230         }
231         }
232 }