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