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