implement arb_vertex_program in hw for r200. Code contains still some hacks, generic...
[mesa.git] / src / glut / directfb / events.c
1 /*
2 * Copyright (C) 2006 Claudio Ciccani <klan@users.sf.net>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 #include "internal.h"
25
26
27 int GLUTAPIENTRY
28 glutGetModifiers( void )
29 {
30 if (g_current)
31 return g_current->modifiers;
32 return 0;
33 }
34
35
36 void GLUTAPIENTRY
37 glutIgnoreKeyRepeat( int ignore )
38 {
39 }
40
41
42 void GLUTAPIENTRY
43 glutSetKeyRepeat( int mode )
44 {
45 }
46
47
48 void GLUTAPIENTRY
49 glutForceJoystickFunc( void )
50 {
51 if (g_game && joystick && joystick_func) {
52 joystick_func( g_game->buttons,
53 g_game->cx, g_game->cy, g_game->cz );
54 }
55 }
56
57
58 static int
59 __glutSpecialKey( DFBInputDeviceKeySymbol key )
60 {
61 switch (key) {
62 case DIKS_F1:
63 return GLUT_KEY_F1;
64 case DIKS_F2:
65 return GLUT_KEY_F2;
66 case DIKS_F3:
67 return GLUT_KEY_F3;
68 case DIKS_F4:
69 return GLUT_KEY_F4;
70 case DIKS_F5:
71 return GLUT_KEY_F5;
72 case DIKS_F6:
73 return GLUT_KEY_F6;
74 case DIKS_F7:
75 return GLUT_KEY_F7;
76 case DIKS_F8:
77 return GLUT_KEY_F8;
78 case DIKS_F9:
79 return GLUT_KEY_F9;
80 case DIKS_F10:
81 return GLUT_KEY_F10;
82 case DIKS_F11:
83 return GLUT_KEY_F11;
84 case DIKS_F12:
85 return GLUT_KEY_F12;
86 case DIKS_CURSOR_LEFT:
87 return GLUT_KEY_LEFT;
88 case DIKS_CURSOR_UP:
89 return GLUT_KEY_UP;
90 case DIKS_CURSOR_RIGHT:
91 return GLUT_KEY_RIGHT;
92 case DIKS_CURSOR_DOWN:
93 return GLUT_KEY_DOWN;
94 case DIKS_PAGE_UP:
95 return GLUT_KEY_PAGE_UP;
96 case DIKS_PAGE_DOWN:
97 return GLUT_KEY_PAGE_DOWN;
98 case DIKS_HOME:
99 return GLUT_KEY_HOME;
100 case DIKS_END:
101 return GLUT_KEY_END;
102 case DIKS_INSERT:
103 return GLUT_KEY_INSERT;
104 default:
105 break;
106 }
107
108 return 0;
109 }
110
111
112 static int
113 __glutButton( DFBInputDeviceButtonIdentifier button )
114 {
115 switch (button) {
116 case DIBI_LEFT:
117 return GLUT_LEFT_BUTTON;
118 case DIBI_MIDDLE:
119 return GLUT_MIDDLE_BUTTON;
120 case DIBI_RIGHT:
121 return GLUT_RIGHT_BUTTON;
122 default:
123 break;
124 }
125
126 return 0;
127 }
128
129
130 static int
131 __glutModifiers( DFBInputDeviceModifierMask mask )
132 {
133 return ((mask & DIMM_SHIFT) ? GLUT_ACTIVE_SHIFT : 0) |
134 ((mask & DIMM_CONTROL) ? GLUT_ACTIVE_CTRL : 0) |
135 ((mask & DIMM_ALT) ? GLUT_ACTIVE_ALT : 0);
136 }
137
138
139 static void
140 __glutWindowEvent( DFBWindowEvent *e )
141 {
142 __GlutWindow *window;
143
144 window = __glutFindWindow( e->window_id );
145 if (!window) /* window was destroyed */
146 return;
147
148 switch (e->type) {
149 case DWET_KEYDOWN:
150 window->modifiers = __glutModifiers( e->modifiers );
151 if (DFB_KEY_IS_ASCII( e->key_symbol )) {
152 if (keyboard_func) {
153 __glutSetWindow( window );
154 keyboard_func( e->key_symbol, e->x, e->y );
155 }
156 }
157 else {
158 int key = __glutSpecialKey( e->key_symbol );
159 if (key && special_func) {
160 __glutSetWindow( window );
161 special_func( key, e->x, e->y );
162 }
163 }
164 break;
165 case DWET_KEYUP:
166 window->modifiers = __glutModifiers( e->modifiers );
167 if (DFB_KEY_IS_ASCII( e->key_symbol )) {
168 if (keyboard_up_func) {
169 __glutSetWindow( window );
170 keyboard_up_func( e->key_symbol, e->x, e->y );
171 }
172 }
173 else {
174 int key = __glutSpecialKey( e->key_symbol );
175 if (key && special_up_func) {
176 __glutSetWindow( window );
177 special_up_func( key, e->x, e->y );
178 }
179 }
180 break;
181 case DWET_BUTTONDOWN:
182 if (mouse_func) {
183 __glutSetWindow( window );
184 mouse_func( __glutButton( e->button ), GLUT_DOWN, e->x, e->y );
185 }
186 break;
187 case DWET_BUTTONUP:
188 if (mouse_func) {
189 __glutSetWindow( window );
190 mouse_func( __glutButton( e->button ), GLUT_UP, e->x, e->y );
191 }
192 break;
193 case DWET_MOTION:
194 if (e->buttons) {
195 if (motion_func) {
196 __glutSetWindow( window );
197 motion_func( e->cx, e->cy );
198 }
199 }
200 else {
201 if (passive_motion_func) {
202 __glutSetWindow( window );
203 passive_motion_func( e->cx, e->cy );
204 }
205 }
206 break;
207 case DWET_ENTER:
208 if (entry_func) {
209 __glutSetWindow( window );
210 entry_func( GLUT_ENTERED );
211 }
212 break;
213 case DWET_LEAVE:
214 if (entry_func) {
215 __glutSetWindow( window );
216 entry_func( GLUT_LEFT );
217 }
218 break;
219 case DWET_SIZE:
220 window->reshape = GL_TRUE;
221 window->redisplay = GL_TRUE;
222 break;
223 default:
224 break;
225 }
226 }
227
228
229 static void
230 __glutInputEvent( DFBInputEvent *e )
231 {
232 __glutAssert( g_game != NULL );
233
234 switch (e->type) {
235 case DIET_KEYPRESS:
236 g_game->modifiers = __glutModifiers( e->modifiers );
237 if (DFB_KEY_IS_ASCII( e->key_symbol )) {
238 if (keyboard_func) {
239 __glutSetWindow( g_game );
240 keyboard_func( e->key_symbol, g_game->cx, g_game->cy );
241 }
242 }
243 else {
244 int key = __glutSpecialKey( e->key_symbol );
245 if (key && special_func) {
246 __glutSetWindow( g_game );
247 special_func( key, g_game->cx, g_game->cy );
248 }
249 }
250 break;
251 case DIET_KEYRELEASE:
252 g_game->modifiers = __glutModifiers( e->modifiers );
253 if (DFB_KEY_IS_ASCII( e->key_symbol )) {
254 if (keyboard_up_func) {
255 __glutSetWindow( g_game );
256 keyboard_up_func( e->key_symbol, g_game->cx, g_game->cy );
257 }
258 }
259 else {
260 int key = __glutSpecialKey( e->key_symbol );
261 if (key && special_up_func) {
262 __glutSetWindow( g_game );
263 special_up_func( key, g_game->cx, g_game->cy );
264 }
265 }
266 break;
267 case DIET_BUTTONPRESS:
268 if (e->device_id == DIDID_JOYSTICK) {
269 g_game->buttons = e->buttons;
270 if (joystick_func) {
271 __glutSetWindow( g_game );
272 joystick_func( g_game->buttons,
273 g_game->cx, g_game->cy, g_game->cz );
274 }
275 }
276 else {
277 if (mouse_func) {
278 __glutSetWindow( g_game );
279 mouse_func( __glutButton( e->button ),
280 GLUT_DOWN, g_game->cx, g_game->cy );
281 }
282 }
283 break;
284 case DIET_BUTTONRELEASE:
285 if (e->device_id == DIDID_JOYSTICK) {
286 g_game->buttons = e->buttons;
287 if (joystick_func) {
288 __glutSetWindow( g_game );
289 joystick_func( g_game->buttons,
290 g_game->cx, g_game->cy, g_game->cz );
291 }
292 }
293 else {
294 if (mouse_func) {
295 __glutSetWindow( g_game );
296 mouse_func( __glutButton( e->button ),
297 GLUT_UP, g_game->cx, g_game->cy );
298 }
299 }
300 break;
301 case DIET_AXISMOTION:
302 switch (e->axis) {
303 case DIAI_X:
304 if (e->flags & DIEF_AXISABS)
305 g_game->cx = e->axisabs;
306 else if (e->flags & DIEF_AXISREL)
307 g_game->cx += e->axisrel;
308 break;
309 case DIAI_Y:
310 if (e->flags & DIEF_AXISABS)
311 g_game->cy = e->axisabs;
312 else if (e->flags & DIEF_AXISREL)
313 g_game->cy += e->axisrel;
314 break;
315 case DIAI_Z:
316 if (e->flags & DIEF_AXISABS)
317 g_game->cz = e->axisabs;
318 else if (e->flags & DIEF_AXISREL)
319 g_game->cz += e->axisrel;
320 break;
321 default:
322 return;
323 }
324 if (e->device_id == DIDID_JOYSTICK) {
325 if (joystick_func) {
326 __glutSetWindow( g_game );
327 joystick_func( g_game->buttons,
328 g_game->cx, g_game->cy, g_game->cz );
329 }
330 }
331 else if (e->axis != DIAI_Z) {
332 if (e->buttons && motion_func) {
333 __glutSetWindow( g_game );
334 motion_func( g_game->cx, g_game->cy );
335 }
336 else if (!e->buttons && passive_motion_func) {
337 __glutSetWindow( g_game );
338 passive_motion_func( g_game->cx, g_game->cy );
339 }
340 }
341 break;
342 default:
343 break;
344 }
345 }
346
347
348 void GLUTAPIENTRY
349 glutMainLoop( void )
350 {
351 __glutAssert( events != NULL );
352
353 while (GL_TRUE) {
354 DFBEvent evt;
355
356 g_idle = GL_TRUE;
357
358 __glutHandleTimers();
359 __glutHandleWindows();
360
361 while (events->GetEvent( events, &evt ) == DFB_OK) {
362 g_idle = GL_FALSE;
363
364 if (evt.clazz == DFEC_WINDOW)
365 __glutWindowEvent( &evt.window );
366 else
367 __glutInputEvent( &evt.input );
368
369 __glutHandleTimers();
370 }
371
372 if (g_idle) {
373 if (idle_func) {
374 idle_func();
375 }
376 else {
377 __glutSetWindow( NULL );
378 usleep( 500 );
379 }
380 }
381 }
382 }
383