66281ea71812108a55b42aa5fa0f0a7f2648e8c6
[freeglut] / src / Common / freeglut_main.c
1 /*\r
2  * freeglut_main.c\r
3  *\r
4  * The windows message processing methods.\r
5  *\r
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.\r
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>\r
8  * Creation date: Fri Dec 3 1999\r
9  *\r
10  * Permission is hereby granted, free of charge, to any person obtaining a\r
11  * copy of this software and associated documentation files (the "Software"),\r
12  * to deal in the Software without restriction, including without limitation\r
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
14  * and/or sell copies of the Software, and to permit persons to whom the\r
15  * Software is furnished to do so, subject to the following conditions:\r
16  *\r
17  * The above copyright notice and this permission notice shall be included\r
18  * in all copies or substantial portions of the Software.\r
19  *\r
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL\r
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
26  */\r
27 \r
28 #include <GL/freeglut.h>\r
29 #include "freeglut_internal.h"\r
30 #ifdef HAVE_ERRNO_H\r
31 #    include <errno.h>\r
32 #endif\r
33 #include <stdarg.h>\r
34 #ifdef  HAVE_VFPRINTF\r
35 #    define VFPRINTF(s,f,a) vfprintf((s),(f),(a))\r
36 #elif defined(HAVE__DOPRNT)\r
37 #    define VFPRINTF(s,f,a) _doprnt((f),(a),(s))\r
38 #else\r
39 #    define VFPRINTF(s,f,a)\r
40 #endif\r
41 \r
42 #ifdef _WIN32_WCE\r
43 \r
44 typedef struct GXDisplayProperties GXDisplayProperties;\r
45 typedef struct GXKeyList GXKeyList;\r
46 #include <gx.h>\r
47 \r
48 typedef struct GXKeyList (*GXGETDEFAULTKEYS)(int);\r
49 typedef int (*GXOPENINPUT)();\r
50 \r
51 GXGETDEFAULTKEYS GXGetDefaultKeys_ = NULL;\r
52 GXOPENINPUT GXOpenInput_ = NULL;\r
53 \r
54 struct GXKeyList gxKeyList;\r
55 \r
56 #endif /* _WIN32_WCE */\r
57 \r
58 /*\r
59  * Try to get the maximum value allowed for ints, falling back to the minimum\r
60  * guaranteed by ISO C99 if there is no suitable header.\r
61  */\r
62 #ifdef HAVE_LIMITS_H\r
63 #    include <limits.h>\r
64 #endif\r
65 #ifndef INT_MAX\r
66 #    define INT_MAX 32767\r
67 #endif\r
68 \r
69 #ifndef MIN\r
70 #    define MIN(a,b) (((a)<(b)) ? (a) : (b))\r
71 #endif\r
72 \r
73 extern void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height );\r
74 extern void fgPlatformDisplayWindow ( SFG_Window *window );\r
75 extern unsigned long fgPlatformSystemTime ( void );\r
76 extern void fgPlatformSleepForEvents( long msec );\r
77 extern void fgPlatformProcessSingleEvent ( void );\r
78 extern void fgPlatformMainLoopPreliminaryWork ( void );\r
79 \r
80 \r
81 /*\r
82  * TODO BEFORE THE STABLE RELEASE:\r
83  *\r
84  * There are some issues concerning window redrawing under X11, and maybe\r
85  * some events are not handled. The Win32 version lacks some more features,\r
86  * but seems acceptable for not demanding purposes.\r
87  *\r
88  * Need to investigate why the X11 version breaks out with an error when\r
89  * closing a window (using the window manager, not glutDestroyWindow)...\r
90  */\r
91 \r
92 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */\r
93 \r
94 /*\r
95  * Handle a window configuration change. When no reshape\r
96  * callback is hooked, the viewport size is updated to\r
97  * match the new window size.\r
98  */\r
99 #if TARGET_HOST_POSIX_X11\r
100 void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height )\r
101 {\r
102     XResizeWindow( fgDisplay.pDisplay.Display, window->Window.Handle,\r
103                    width, height );\r
104     XFlush( fgDisplay.pDisplay.Display ); /* XXX Shouldn't need this */\r
105 }\r
106 #endif\r
107 \r
108 static void fghReshapeWindow ( SFG_Window *window, int width, int height )\r
109 {\r
110     SFG_Window *current_window = fgStructure.CurrentWindow;\r
111 \r
112     freeglut_return_if_fail( window != NULL );\r
113 \r
114         fgPlatformReshapeWindow ( window, width, height );\r
115 \r
116     if( FETCH_WCB( *window, Reshape ) )\r
117         INVOKE_WCB( *window, Reshape, ( width, height ) );\r
118     else\r
119     {\r
120         fgSetWindow( window );\r
121         glViewport( 0, 0, width, height );\r
122     }\r
123 \r
124     /*\r
125      * Force a window redraw.  In Windows at least this is only a partial\r
126      * solution:  if the window is increasing in size in either dimension,\r
127      * the already-drawn part does not get drawn again and things look funny.\r
128      * But without this we get this bad behaviour whenever we resize the\r
129      * window.\r
130      */\r
131     window->State.Redisplay = GL_TRUE;\r
132 \r
133     if( window->IsMenu )\r
134         fgSetWindow( current_window );\r
135 }\r
136 \r
137 /*\r
138  * Calls a window's redraw method. This is used when\r
139  * a redraw is forced by the incoming window messages.\r
140  */\r
141 void fghRedrawWindow ( SFG_Window *window )\r
142 {\r
143     SFG_Window *current_window = fgStructure.CurrentWindow;\r
144 \r
145     freeglut_return_if_fail( window );\r
146     freeglut_return_if_fail( FETCH_WCB ( *window, Display ) );\r
147 \r
148     window->State.Redisplay = GL_FALSE;\r
149 \r
150     freeglut_return_if_fail( window->State.Visible );\r
151 \r
152     fgSetWindow( window );\r
153 \r
154     if( window->State.NeedToResize )\r
155     {\r
156         fghReshapeWindow(\r
157             window,\r
158             window->State.Width,\r
159             window->State.Height\r
160         );\r
161 \r
162         window->State.NeedToResize = GL_FALSE;\r
163     }\r
164 \r
165     INVOKE_WCB( *window, Display, ( ) );\r
166 \r
167     fgSetWindow( current_window );\r
168 }\r
169 \r
170 /*\r
171  * A static helper function to execute display callback for a window\r
172  */\r
173 #if TARGET_HOST_POSIX_X11\r
174 void fgPlatformDisplayWindow ( SFG_Window *window )\r
175 {\r
176         fghRedrawWindow ( window ) ;\r
177 }\r
178 \r
179 \r
180 unsigned long fgPlatformSystemTime ( void )\r
181 {\r
182 #if TARGET_HOST_SOLARIS || HAVE_GETTIMEOFDAY\r
183     struct timeval now;\r
184     gettimeofday( &now, NULL );\r
185     return now.tv_usec/1000 + now.tv_sec*1000;\r
186 #endif\r
187 }\r
188 #endif\r
189 \r
190 static void fghcbDisplayWindow( SFG_Window *window,\r
191                                 SFG_Enumerator *enumerator )\r
192 {\r
193     if( window->State.Redisplay &&\r
194         window->State.Visible )\r
195     {\r
196         window->State.Redisplay = GL_FALSE;\r
197                 fgPlatformDisplayWindow ( window );\r
198     }\r
199 \r
200     fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );\r
201 }\r
202 \r
203 /*\r
204  * Make all windows perform a display call\r
205  */\r
206 static void fghDisplayAll( void )\r
207 {\r
208     SFG_Enumerator enumerator;\r
209 \r
210     enumerator.found = GL_FALSE;\r
211     enumerator.data  =  NULL;\r
212 \r
213     fgEnumWindows( fghcbDisplayWindow, &enumerator );\r
214 }\r
215 \r
216 /*\r
217  * Window enumerator callback to check for the joystick polling code\r
218  */\r
219 static void fghcbCheckJoystickPolls( SFG_Window *window,\r
220                                      SFG_Enumerator *enumerator )\r
221 {\r
222     long int checkTime = fgElapsedTime( );\r
223 \r
224     if( window->State.JoystickLastPoll + window->State.JoystickPollRate <=\r
225         checkTime )\r
226     {\r
227 #if !defined(_WIN32_WCE)\r
228         fgJoystickPollWindow( window );\r
229 #endif /* !defined(_WIN32_WCE) */\r
230         window->State.JoystickLastPoll = checkTime;\r
231     }\r
232 \r
233     fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );\r
234 }\r
235 \r
236 /*\r
237  * Check all windows for joystick polling\r
238  */\r
239 static void fghCheckJoystickPolls( void )\r
240 {\r
241     SFG_Enumerator enumerator;\r
242 \r
243     enumerator.found = GL_FALSE;\r
244     enumerator.data  =  NULL;\r
245 \r
246     fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );\r
247 }\r
248 \r
249 /*\r
250  * Check the global timers\r
251  */\r
252 static void fghCheckTimers( void )\r
253 {\r
254     long checkTime = fgElapsedTime( );\r
255 \r
256     while( fgState.Timers.First )\r
257     {\r
258         SFG_Timer *timer = fgState.Timers.First;\r
259 \r
260         if( timer->TriggerTime > checkTime )\r
261             break;\r
262 \r
263         fgListRemove( &fgState.Timers, &timer->Node );\r
264         fgListAppend( &fgState.FreeTimers, &timer->Node );\r
265 \r
266         timer->Callback( timer->ID );\r
267     }\r
268 }\r
269 \r
270  \r
271 /* Platform-dependent time in milliseconds, as an unsigned 32-bit integer.\r
272  * This value wraps every 49.7 days, but integer overflows cancel\r
273  * when subtracting an initial start time, unless the total time exceeds\r
274  * 32-bit, where the GLUT API return value is also overflowed.\r
275  */  \r
276 unsigned long fgSystemTime(void)\r
277 {\r
278         return fgPlatformSystemTime ();\r
279 }\r
280   \r
281 /*\r
282  * Elapsed Time\r
283  */\r
284 long fgElapsedTime( void )\r
285 {\r
286     return (long) (fgSystemTime() - fgState.Time);\r
287 }\r
288 \r
289 /*\r
290  * Error Messages.\r
291  */\r
292 void fgError( const char *fmt, ... )\r
293 {\r
294     va_list ap;\r
295 \r
296     if (fgState.ErrorFunc) {\r
297 \r
298         va_start( ap, fmt );\r
299 \r
300         /* call user set error handler here */\r
301         fgState.ErrorFunc(fmt, ap);\r
302 \r
303         va_end( ap );\r
304 \r
305     } else {\r
306 \r
307         va_start( ap, fmt );\r
308 \r
309         fprintf( stderr, "freeglut ");\r
310         if( fgState.ProgramName )\r
311             fprintf( stderr, "(%s): ", fgState.ProgramName );\r
312         VFPRINTF( stderr, fmt, ap );\r
313         fprintf( stderr, "\n" );\r
314 \r
315         va_end( ap );\r
316 \r
317         if ( fgState.Initialised )\r
318             fgDeinitialize ();\r
319 \r
320         exit( 1 );\r
321     }\r
322 }\r
323 \r
324 void fgWarning( const char *fmt, ... )\r
325 {\r
326     va_list ap;\r
327 \r
328     if (fgState.WarningFunc) {\r
329 \r
330         va_start( ap, fmt );\r
331 \r
332         /* call user set warning handler here */\r
333         fgState.WarningFunc(fmt, ap);\r
334 \r
335         va_end( ap );\r
336 \r
337     } else {\r
338 \r
339         va_start( ap, fmt );\r
340 \r
341         fprintf( stderr, "freeglut ");\r
342         if( fgState.ProgramName )\r
343             fprintf( stderr, "(%s): ", fgState.ProgramName );\r
344         VFPRINTF( stderr, fmt, ap );\r
345         fprintf( stderr, "\n" );\r
346 \r
347         va_end( ap );\r
348     }\r
349 }\r
350 \r
351 \r
352 /*\r
353  * Indicates whether Joystick events are being used by ANY window.\r
354  *\r
355  * The current mechanism is to walk all of the windows and ask if\r
356  * there is a joystick callback.  We have a short-circuit early\r
357  * return if we find any joystick handler registered.\r
358  *\r
359  * The real way to do this is to make use of the glutTimer() API\r
360  * to more cleanly re-implement the joystick API.  Then, this code\r
361  * and all other "joystick timer" code can be yanked.\r
362  *\r
363  */\r
364 static void fghCheckJoystickCallback( SFG_Window* w, SFG_Enumerator* e)\r
365 {\r
366     if( FETCH_WCB( *w, Joystick ) )\r
367     {\r
368         e->found = GL_TRUE;\r
369         e->data = w;\r
370     }\r
371     fgEnumSubWindows( w, fghCheckJoystickCallback, e );\r
372 }\r
373 static int fghHaveJoystick( void )\r
374 {\r
375     SFG_Enumerator enumerator;\r
376 \r
377     enumerator.found = GL_FALSE;\r
378     enumerator.data = NULL;\r
379     fgEnumWindows( fghCheckJoystickCallback, &enumerator );\r
380     return !!enumerator.data;\r
381 }\r
382 static void fghHavePendingRedisplaysCallback( SFG_Window* w, SFG_Enumerator* e)\r
383 {\r
384     if( w->State.Redisplay && w->State.Visible )\r
385     {\r
386         e->found = GL_TRUE;\r
387         e->data = w;\r
388     }\r
389     fgEnumSubWindows( w, fghHavePendingRedisplaysCallback, e );\r
390 }\r
391 static int fghHavePendingRedisplays (void)\r
392 {\r
393     SFG_Enumerator enumerator;\r
394 \r
395     enumerator.found = GL_FALSE;\r
396     enumerator.data = NULL;\r
397     fgEnumWindows( fghHavePendingRedisplaysCallback, &enumerator );\r
398     return !!enumerator.data;\r
399 }\r
400 /*\r
401  * Returns the number of GLUT ticks (milliseconds) till the next timer event.\r
402  */\r
403 static long fghNextTimer( void )\r
404 {\r
405     long ret = INT_MAX;\r
406     SFG_Timer *timer = fgState.Timers.First;\r
407 \r
408     if( timer )\r
409         ret = timer->TriggerTime - fgElapsedTime();\r
410     if( ret < 0 )\r
411         ret = 0;\r
412 \r
413     return ret;\r
414 }\r
415 /*\r
416  * Does the magic required to relinquish the CPU until something interesting\r
417  * happens.\r
418  */\r
419 \r
420 #if TARGET_HOST_POSIX_X11\r
421 void fgPlatformSleepForEvents( long msec )\r
422 {\r
423     /*\r
424      * Possibly due to aggressive use of XFlush() and friends,\r
425      * it is possible to have our socket drained but still have\r
426      * unprocessed events.  (Or, this may just be normal with\r
427      * X, anyway?)  We do non-trivial processing of X events\r
428      * after the event-reading loop, in any case, so we\r
429      * need to allow that we may have an empty socket but non-\r
430      * empty event queue.\r
431      */\r
432     if( ! XPending( fgDisplay.pDisplay.Display ) )\r
433     {\r
434         fd_set fdset;\r
435         int err;\r
436         int socket;\r
437         struct timeval wait;\r
438 \r
439         socket = ConnectionNumber( fgDisplay.pDisplay.Display );\r
440         FD_ZERO( &fdset );\r
441         FD_SET( socket, &fdset );\r
442         wait.tv_sec = msec / 1000;\r
443         wait.tv_usec = (msec % 1000) * 1000;\r
444         err = select( socket+1, &fdset, NULL, NULL, &wait );\r
445 \r
446 #ifdef HAVE_ERRNO_H\r
447         if( ( -1 == err ) && ( errno != EINTR ) )\r
448             fgWarning ( "freeglut select() error: %d", errno );\r
449 #endif\r
450     }\r
451 }\r
452 #endif\r
453 \r
454 static void fghSleepForEvents( void )\r
455 {\r
456     long msec;\r
457 \r
458     if( fgState.IdleCallback || fghHavePendingRedisplays( ) )\r
459         return;\r
460 \r
461     msec = fghNextTimer( );\r
462     /* XXX Use GLUT timers for joysticks... */\r
463     /* XXX Dumb; forces granularity to .01sec */\r
464     if( fghHaveJoystick( ) && ( msec > 10 ) )     \r
465         msec = 10;\r
466 \r
467         fgPlatformSleepForEvents ( msec );\r
468 }\r
469 \r
470 #if TARGET_HOST_POSIX_X11\r
471 /*\r
472  * Returns GLUT modifier mask for the state field of an X11 event.\r
473  */\r
474 int fgPlatformGetModifiers( int state )\r
475 {\r
476     int ret = 0;\r
477 \r
478     if( state & ( ShiftMask | LockMask ) )\r
479         ret |= GLUT_ACTIVE_SHIFT;\r
480     if( state & ControlMask )\r
481         ret |= GLUT_ACTIVE_CTRL;\r
482     if( state & Mod1Mask )\r
483         ret |= GLUT_ACTIVE_ALT;\r
484 \r
485     return ret;\r
486 }\r
487 \r
488 \r
489 \r
490 static const char* fghTypeToString( int type )\r
491 {\r
492     switch( type ) {\r
493     case KeyPress: return "KeyPress";\r
494     case KeyRelease: return "KeyRelease";\r
495     case ButtonPress: return "ButtonPress";\r
496     case ButtonRelease: return "ButtonRelease";\r
497     case MotionNotify: return "MotionNotify";\r
498     case EnterNotify: return "EnterNotify";\r
499     case LeaveNotify: return "LeaveNotify";\r
500     case FocusIn: return "FocusIn";\r
501     case FocusOut: return "FocusOut";\r
502     case KeymapNotify: return "KeymapNotify";\r
503     case Expose: return "Expose";\r
504     case GraphicsExpose: return "GraphicsExpose";\r
505     case NoExpose: return "NoExpose";\r
506     case VisibilityNotify: return "VisibilityNotify";\r
507     case CreateNotify: return "CreateNotify";\r
508     case DestroyNotify: return "DestroyNotify";\r
509     case UnmapNotify: return "UnmapNotify";\r
510     case MapNotify: return "MapNotify";\r
511     case MapRequest: return "MapRequest";\r
512     case ReparentNotify: return "ReparentNotify";\r
513     case ConfigureNotify: return "ConfigureNotify";\r
514     case ConfigureRequest: return "ConfigureRequest";\r
515     case GravityNotify: return "GravityNotify";\r
516     case ResizeRequest: return "ResizeRequest";\r
517     case CirculateNotify: return "CirculateNotify";\r
518     case CirculateRequest: return "CirculateRequest";\r
519     case PropertyNotify: return "PropertyNotify";\r
520     case SelectionClear: return "SelectionClear";\r
521     case SelectionRequest: return "SelectionRequest";\r
522     case SelectionNotify: return "SelectionNotify";\r
523     case ColormapNotify: return "ColormapNotify";\r
524     case ClientMessage: return "ClientMessage";\r
525     case MappingNotify: return "MappingNotify";\r
526     default: return "UNKNOWN";\r
527     }\r
528 }\r
529 \r
530 static const char* fghBoolToString( Bool b )\r
531 {\r
532     return b == False ? "False" : "True";\r
533 }\r
534 \r
535 static const char* fghNotifyHintToString( char is_hint )\r
536 {\r
537     switch( is_hint ) {\r
538     case NotifyNormal: return "NotifyNormal";\r
539     case NotifyHint: return "NotifyHint";\r
540     default: return "UNKNOWN";\r
541     }\r
542 }\r
543 \r
544 static const char* fghNotifyModeToString( int mode )\r
545 {\r
546     switch( mode ) {\r
547     case NotifyNormal: return "NotifyNormal";\r
548     case NotifyGrab: return "NotifyGrab";\r
549     case NotifyUngrab: return "NotifyUngrab";\r
550     case NotifyWhileGrabbed: return "NotifyWhileGrabbed";\r
551     default: return "UNKNOWN";\r
552     }\r
553 }\r
554 \r
555 static const char* fghNotifyDetailToString( int detail )\r
556 {\r
557     switch( detail ) {\r
558     case NotifyAncestor: return "NotifyAncestor";\r
559     case NotifyVirtual: return "NotifyVirtual";\r
560     case NotifyInferior: return "NotifyInferior";\r
561     case NotifyNonlinear: return "NotifyNonlinear";\r
562     case NotifyNonlinearVirtual: return "NotifyNonlinearVirtual";\r
563     case NotifyPointer: return "NotifyPointer";\r
564     case NotifyPointerRoot: return "NotifyPointerRoot";\r
565     case NotifyDetailNone: return "NotifyDetailNone";\r
566     default: return "UNKNOWN";\r
567     }\r
568 }\r
569 \r
570 static const char* fghVisibilityToString( int state ) {\r
571     switch( state ) {\r
572     case VisibilityUnobscured: return "VisibilityUnobscured";\r
573     case VisibilityPartiallyObscured: return "VisibilityPartiallyObscured";\r
574     case VisibilityFullyObscured: return "VisibilityFullyObscured";\r
575     default: return "UNKNOWN";\r
576     }\r
577 }\r
578 \r
579 static const char* fghConfigureDetailToString( int detail )\r
580 {\r
581     switch( detail ) {\r
582     case Above: return "Above";\r
583     case Below: return "Below";\r
584     case TopIf: return "TopIf";\r
585     case BottomIf: return "BottomIf";\r
586     case Opposite: return "Opposite";\r
587     default: return "UNKNOWN";\r
588     }\r
589 }\r
590 \r
591 static const char* fghPlaceToString( int place )\r
592 {\r
593     switch( place ) {\r
594     case PlaceOnTop: return "PlaceOnTop";\r
595     case PlaceOnBottom: return "PlaceOnBottom";\r
596     default: return "UNKNOWN";\r
597     }\r
598 }\r
599 \r
600 static const char* fghMappingRequestToString( int request )\r
601 {\r
602     switch( request ) {\r
603     case MappingModifier: return "MappingModifier";\r
604     case MappingKeyboard: return "MappingKeyboard";\r
605     case MappingPointer: return "MappingPointer";\r
606     default: return "UNKNOWN";\r
607     }\r
608 }\r
609 \r
610 static const char* fghPropertyStateToString( int state )\r
611 {\r
612     switch( state ) {\r
613     case PropertyNewValue: return "PropertyNewValue";\r
614     case PropertyDelete: return "PropertyDelete";\r
615     default: return "UNKNOWN";\r
616     }\r
617 }\r
618 \r
619 static const char* fghColormapStateToString( int state )\r
620 {\r
621     switch( state ) {\r
622     case ColormapUninstalled: return "ColormapUninstalled";\r
623     case ColormapInstalled: return "ColormapInstalled";\r
624     default: return "UNKNOWN";\r
625     }\r
626 }\r
627 \r
628 static void fghPrintEvent( XEvent *event )\r
629 {\r
630     switch( event->type ) {\r
631 \r
632     case KeyPress:\r
633     case KeyRelease: {\r
634         XKeyEvent *e = &event->xkey;\r
635         fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "\r
636                    "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "\r
637                    "keycode=%u, same_screen=%s", fghTypeToString( e->type ),\r
638                    e->window, e->root, e->subwindow, (unsigned long)e->time,\r
639                    e->x, e->y, e->x_root, e->y_root, e->state, e->keycode,\r
640                    fghBoolToString( e->same_screen ) );\r
641         break;\r
642     }\r
643 \r
644     case ButtonPress:\r
645     case ButtonRelease: {\r
646         XButtonEvent *e = &event->xbutton;\r
647         fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "\r
648                    "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "\r
649                    "button=%u, same_screen=%d", fghTypeToString( e->type ),\r
650                    e->window, e->root, e->subwindow, (unsigned long)e->time,\r
651                    e->x, e->y, e->x_root, e->y_root, e->state, e->button,\r
652                    fghBoolToString( e->same_screen ) );\r
653         break;\r
654     }\r
655 \r
656     case MotionNotify: {\r
657         XMotionEvent *e = &event->xmotion;\r
658         fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "\r
659                    "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "\r
660                    "is_hint=%s, same_screen=%d", fghTypeToString( e->type ),\r
661                    e->window, e->root, e->subwindow, (unsigned long)e->time,\r
662                    e->x, e->y, e->x_root, e->y_root, e->state,\r
663                    fghNotifyHintToString( e->is_hint ),\r
664                    fghBoolToString( e->same_screen ) );\r
665         break;\r
666     }\r
667 \r
668     case EnterNotify:\r
669     case LeaveNotify: {\r
670         XCrossingEvent *e = &event->xcrossing;\r
671         fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "\r
672                    "(x,y)=(%d,%d), mode=%s, detail=%s, same_screen=%d, "\r
673                    "focus=%d, state=0x%x", fghTypeToString( e->type ),\r
674                    e->window, e->root, e->subwindow, (unsigned long)e->time,\r
675                    e->x, e->y, fghNotifyModeToString( e->mode ),\r
676                    fghNotifyDetailToString( e->detail ), (int)e->same_screen,\r
677                    (int)e->focus, e->state );\r
678         break;\r
679     }\r
680 \r
681     case FocusIn:\r
682     case FocusOut: {\r
683         XFocusChangeEvent *e = &event->xfocus;\r
684         fgWarning( "%s: window=0x%x, mode=%s, detail=%s",\r
685                    fghTypeToString( e->type ), e->window,\r
686                    fghNotifyModeToString( e->mode ),\r
687                    fghNotifyDetailToString( e->detail ) );\r
688         break;\r
689     }\r
690 \r
691     case KeymapNotify: {\r
692         XKeymapEvent *e = &event->xkeymap;\r
693         char buf[32 * 2 + 1];\r
694         int i;\r
695         for ( i = 0; i < 32; i++ ) {\r
696             snprintf( &buf[ i * 2 ], sizeof( buf ) - i * 2,\r
697                       "%02x", e->key_vector[ i ] );\r
698         }\r
699         buf[ i ] = '\0';\r
700         fgWarning( "%s: window=0x%x, %s", fghTypeToString( e->type ), e->window,\r
701                    buf );\r
702         break;\r
703     }\r
704 \r
705     case Expose: {\r
706         XExposeEvent *e = &event->xexpose;\r
707         fgWarning( "%s: window=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "\r
708                    "count=%d", fghTypeToString( e->type ), e->window, e->x,\r
709                    e->y, e->width, e->height, e->count );\r
710         break;\r
711     }\r
712 \r
713     case GraphicsExpose: {\r
714         XGraphicsExposeEvent *e = &event->xgraphicsexpose;\r
715         fgWarning( "%s: drawable=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "\r
716                    "count=%d, (major_code,minor_code)=(%d,%d)",\r
717                    fghTypeToString( e->type ), e->drawable, e->x, e->y,\r
718                    e->width, e->height, e->count, e->major_code,\r
719                    e->minor_code );\r
720         break;\r
721     }\r
722 \r
723     case NoExpose: {\r
724         XNoExposeEvent *e = &event->xnoexpose;\r
725         fgWarning( "%s: drawable=0x%x, (major_code,minor_code)=(%d,%d)",\r
726                    fghTypeToString( e->type ), e->drawable, e->major_code,\r
727                    e->minor_code );\r
728         break;\r
729     }\r
730 \r
731     case VisibilityNotify: {\r
732         XVisibilityEvent *e = &event->xvisibility;\r
733         fgWarning( "%s: window=0x%x, state=%s", fghTypeToString( e->type ),\r
734                    e->window, fghVisibilityToString( e->state) );\r
735         break;\r
736     }\r
737 \r
738     case CreateNotify: {\r
739         XCreateWindowEvent *e = &event->xcreatewindow;\r
740         fgWarning( "%s: (x,y)=(%d,%d), (width,height)=(%d,%d), border_width=%d, "\r
741                    "window=0x%x, override_redirect=%s",\r
742                    fghTypeToString( e->type ), e->x, e->y, e->width, e->height,\r
743                    e->border_width, e->window,\r
744                    fghBoolToString( e->override_redirect ) );\r
745         break;\r
746     }\r
747 \r
748     case DestroyNotify: {\r
749         XDestroyWindowEvent *e = &event->xdestroywindow;\r
750         fgWarning( "%s: event=0x%x, window=0x%x",\r
751                    fghTypeToString( e->type ), e->event, e->window );\r
752         break;\r
753     }\r
754 \r
755     case UnmapNotify: {\r
756         XUnmapEvent *e = &event->xunmap;\r
757         fgWarning( "%s: event=0x%x, window=0x%x, from_configure=%s",\r
758                    fghTypeToString( e->type ), e->event, e->window,\r
759                    fghBoolToString( e->from_configure ) );\r
760         break;\r
761     }\r
762 \r
763     case MapNotify: {\r
764         XMapEvent *e = &event->xmap;\r
765         fgWarning( "%s: event=0x%x, window=0x%x, override_redirect=%s",\r
766                    fghTypeToString( e->type ), e->event, e->window,\r
767                    fghBoolToString( e->override_redirect ) );\r
768         break;\r
769     }\r
770 \r
771     case MapRequest: {\r
772         XMapRequestEvent *e = &event->xmaprequest;\r
773         fgWarning( "%s: parent=0x%x, window=0x%x",\r
774                    fghTypeToString( event->type ), e->parent, e->window );\r
775         break;\r
776     }\r
777 \r
778     case ReparentNotify: {\r
779         XReparentEvent *e = &event->xreparent;\r
780         fgWarning( "%s: event=0x%x, window=0x%x, parent=0x%x, (x,y)=(%d,%d), "\r
781                    "override_redirect=%s", fghTypeToString( e->type ),\r
782                    e->event, e->window, e->parent, e->x, e->y,\r
783                    fghBoolToString( e->override_redirect ) );\r
784         break;\r
785     }\r
786 \r
787     case ConfigureNotify: {\r
788         XConfigureEvent *e = &event->xconfigure;\r
789         fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d), "\r
790                    "(width,height)=(%d,%d), border_width=%d, above=0x%x, "\r
791                    "override_redirect=%s", fghTypeToString( e->type ), e->event,\r
792                    e->window, e->x, e->y, e->width, e->height, e->border_width,\r
793                    e->above, fghBoolToString( e->override_redirect ) );\r
794         break;\r
795     }\r
796 \r
797     case ConfigureRequest: {\r
798         XConfigureRequestEvent *e = &event->xconfigurerequest;\r
799         fgWarning( "%s: parent=0x%x, window=0x%x, (x,y)=(%d,%d), "\r
800                    "(width,height)=(%d,%d), border_width=%d, above=0x%x, "\r
801                    "detail=%s, value_mask=%lx", fghTypeToString( e->type ),\r
802                    e->parent, e->window, e->x, e->y, e->width, e->height,\r
803                    e->border_width, e->above,\r
804                    fghConfigureDetailToString( e->detail ), e->value_mask );\r
805         break;\r
806     }\r
807 \r
808     case GravityNotify: {\r
809         XGravityEvent *e = &event->xgravity;\r
810         fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d)",\r
811                    fghTypeToString( e->type ), e->event, e->window, e->x, e->y );\r
812         break;\r
813     }\r
814 \r
815     case ResizeRequest: {\r
816         XResizeRequestEvent *e = &event->xresizerequest;\r
817         fgWarning( "%s: window=0x%x, (width,height)=(%d,%d)",\r
818                    fghTypeToString( e->type ), e->window, e->width, e->height );\r
819         break;\r
820     }\r
821 \r
822     case CirculateNotify: {\r
823         XCirculateEvent *e = &event->xcirculate;\r
824         fgWarning( "%s: event=0x%x, window=0x%x, place=%s",\r
825                    fghTypeToString( e->type ), e->event, e->window,\r
826                    fghPlaceToString( e->place ) );\r
827         break;\r
828     }\r
829 \r
830     case CirculateRequest: {\r
831         XCirculateRequestEvent *e = &event->xcirculaterequest;\r
832         fgWarning( "%s: parent=0x%x, window=0x%x, place=%s",\r
833                    fghTypeToString( e->type ), e->parent, e->window,\r
834                    fghPlaceToString( e->place ) );\r
835         break;\r
836     }\r
837 \r
838     case PropertyNotify: {\r
839         XPropertyEvent *e = &event->xproperty;\r
840         fgWarning( "%s: window=0x%x, atom=%lu, time=%lu, state=%s",\r
841                    fghTypeToString( e->type ), e->window,\r
842                    (unsigned long)e->atom, (unsigned long)e->time,\r
843                    fghPropertyStateToString( e->state ) );\r
844         break;\r
845     }\r
846 \r
847     case SelectionClear: {\r
848         XSelectionClearEvent *e = &event->xselectionclear;\r
849         fgWarning( "%s: window=0x%x, selection=%lu, time=%lu",\r
850                    fghTypeToString( e->type ), e->window,\r
851                    (unsigned long)e->selection, (unsigned long)e->time );\r
852         break;\r
853     }\r
854 \r
855     case SelectionRequest: {\r
856         XSelectionRequestEvent *e = &event->xselectionrequest;\r
857         fgWarning( "%s: owner=0x%x, requestor=0x%x, selection=0x%x, "\r
858                    "target=0x%x, property=%lu, time=%lu",\r
859                    fghTypeToString( e->type ), e->owner, e->requestor,\r
860                    (unsigned long)e->selection, (unsigned long)e->target,\r
861                    (unsigned long)e->property, (unsigned long)e->time );\r
862         break;\r
863     }\r
864 \r
865     case SelectionNotify: {\r
866         XSelectionEvent *e = &event->xselection;\r
867         fgWarning( "%s: requestor=0x%x, selection=0x%x, target=0x%x, "\r
868                    "property=%lu, time=%lu", fghTypeToString( e->type ),\r
869                    e->requestor, (unsigned long)e->selection,\r
870                    (unsigned long)e->target, (unsigned long)e->property,\r
871                    (unsigned long)e->time );\r
872         break;\r
873     }\r
874 \r
875     case ColormapNotify: {\r
876         XColormapEvent *e = &event->xcolormap;\r
877         fgWarning( "%s: window=0x%x, colormap=%lu, new=%s, state=%s",\r
878                    fghTypeToString( e->type ), e->window,\r
879                    (unsigned long)e->colormap, fghBoolToString( e->new ),\r
880                    fghColormapStateToString( e->state ) );\r
881         break;\r
882     }\r
883 \r
884     case ClientMessage: {\r
885         XClientMessageEvent *e = &event->xclient;\r
886         char buf[ 61 ];\r
887         char* p = buf;\r
888         char* end = buf + sizeof( buf );\r
889         int i;\r
890         switch( e->format ) {\r
891         case 8:\r
892           for ( i = 0; i < 20; i++, p += 3 ) {\r
893                 snprintf( p, end - p, " %02x", e->data.b[ i ] );\r
894             }\r
895             break;\r
896         case 16:\r
897             for ( i = 0; i < 10; i++, p += 5 ) {\r
898                 snprintf( p, end - p, " %04x", e->data.s[ i ] );\r
899             }\r
900             break;\r
901         case 32:\r
902             for ( i = 0; i < 5; i++, p += 9 ) {\r
903                 snprintf( p, end - p, " %08lx", e->data.l[ i ] );\r
904             }\r
905             break;\r
906         }\r
907         *p = '\0';\r
908         fgWarning( "%s: window=0x%x, message_type=%lu, format=%d, data=(%s )",\r
909                    fghTypeToString( e->type ), e->window,\r
910                    (unsigned long)e->message_type, e->format, buf );\r
911         break;\r
912     }\r
913 \r
914     case MappingNotify: {\r
915         XMappingEvent *e = &event->xmapping;\r
916         fgWarning( "%s: window=0x%x, request=%s, first_keycode=%d, count=%d",\r
917                    fghTypeToString( e->type ), e->window,\r
918                    fghMappingRequestToString( e->request ), e->first_keycode,\r
919                    e->count );\r
920         break;\r
921     }\r
922 \r
923     default: {\r
924         fgWarning( "%s", fghTypeToString( event->type ) );\r
925         break;\r
926     }\r
927     }\r
928 }\r
929 \r
930 \r
931 void fgPlatformProcessSingleEvent ( void )\r
932 {\r
933     SFG_Window* window;\r
934     XEvent event;\r
935 \r
936     /* This code was repeated constantly, so here it goes into a definition: */\r
937 #define GETWINDOW(a)                             \\r
938     window = fgWindowByHandle( event.a.window ); \\r
939     if( window == NULL )                         \\r
940         break;\r
941 \r
942 #define GETMOUSE(a)                              \\r
943     window->State.MouseX = event.a.x;            \\r
944     window->State.MouseY = event.a.y;\r
945 \r
946     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );\r
947 \r
948     while( XPending( fgDisplay.pDisplay.Display ) )\r
949     {\r
950         XNextEvent( fgDisplay.pDisplay.Display, &event );\r
951 #if _DEBUG\r
952         fghPrintEvent( &event );\r
953 #endif\r
954 \r
955         switch( event.type )\r
956         {\r
957         case ClientMessage:\r
958             if(fgIsSpaceballXEvent(&event)) {\r
959                 fgSpaceballHandleXEvent(&event);\r
960                 break;\r
961             }\r
962             /* Destroy the window when the WM_DELETE_WINDOW message arrives */\r
963             if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.pDisplay.DeleteWindow )\r
964             {\r
965                 GETWINDOW( xclient );\r
966 \r
967                 fgDestroyWindow ( window );\r
968 \r
969                 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )\r
970                 {\r
971                     fgDeinitialize( );\r
972                     exit( 0 );\r
973                 }\r
974                 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )\r
975                     fgState.ExecState = GLUT_EXEC_STATE_STOP;\r
976 \r
977                 return;\r
978             }\r
979             break;\r
980 \r
981             /*\r
982              * CreateNotify causes a configure-event so that sub-windows are\r
983              * handled compatibly with GLUT.  Otherwise, your sub-windows\r
984              * (in freeglut only) will not get an initial reshape event,\r
985              * which can break things.\r
986              *\r
987              * GLUT presumably does this because it generally tries to treat\r
988              * sub-windows the same as windows.\r
989              */\r
990         case CreateNotify:\r
991         case ConfigureNotify:\r
992             {\r
993                 int width, height;\r
994                 if( event.type == CreateNotify ) {\r
995                     GETWINDOW( xcreatewindow );\r
996                     width = event.xcreatewindow.width;\r
997                     height = event.xcreatewindow.height;\r
998                 } else {\r
999                     GETWINDOW( xconfigure );\r
1000                     width = event.xconfigure.width;\r
1001                     height = event.xconfigure.height;\r
1002                 }\r
1003 \r
1004                 if( ( width != window->State.pWState.OldWidth ) ||\r
1005                     ( height != window->State.pWState.OldHeight ) )\r
1006                 {\r
1007                     SFG_Window *current_window = fgStructure.CurrentWindow;\r
1008 \r
1009                     window->State.pWState.OldWidth = width;\r
1010                     window->State.pWState.OldHeight = height;\r
1011                     if( FETCH_WCB( *window, Reshape ) )\r
1012                         INVOKE_WCB( *window, Reshape, ( width, height ) );\r
1013                     else\r
1014                     {\r
1015                         fgSetWindow( window );\r
1016                         glViewport( 0, 0, width, height );\r
1017                     }\r
1018                     glutPostRedisplay( );\r
1019                     if( window->IsMenu )\r
1020                         fgSetWindow( current_window );\r
1021                 }\r
1022             }\r
1023             break;\r
1024 \r
1025         case DestroyNotify:\r
1026             /*\r
1027              * This is sent to confirm the XDestroyWindow call.\r
1028              *\r
1029              * XXX WHY is this commented out?  Should we re-enable it?\r
1030              */\r
1031             /* fgAddToWindowDestroyList ( window ); */\r
1032             break;\r
1033 \r
1034         case Expose:\r
1035             /*\r
1036              * We are too dumb to process partial exposes...\r
1037              *\r
1038              * XXX Well, we could do it.  However, it seems to only\r
1039              * XXX be potentially useful for single-buffered (since\r
1040              * XXX double-buffered does not respect viewport when we\r
1041              * XXX do a buffer-swap).\r
1042              *\r
1043              */\r
1044             if( event.xexpose.count == 0 )\r
1045             {\r
1046                 GETWINDOW( xexpose );\r
1047                 window->State.Redisplay = GL_TRUE;\r
1048             }\r
1049             break;\r
1050 \r
1051         case MapNotify:\r
1052             break;\r
1053 \r
1054         case UnmapNotify:\r
1055             /* We get this when iconifying a window. */ \r
1056             GETWINDOW( xunmap );\r
1057             INVOKE_WCB( *window, WindowStatus, ( GLUT_HIDDEN ) );\r
1058             window->State.Visible = GL_FALSE;\r
1059             break;\r
1060 \r
1061         case MappingNotify:\r
1062             /*\r
1063              * Have the client's keyboard knowledge updated (xlib.ps,\r
1064              * page 206, says that's a good thing to do)\r
1065              */\r
1066             XRefreshKeyboardMapping( (XMappingEvent *) &event );\r
1067             break;\r
1068 \r
1069         case VisibilityNotify:\r
1070         {\r
1071             /*\r
1072              * Sending this event, the X server can notify us that the window\r
1073              * has just acquired one of the three possible visibility states:\r
1074              * VisibilityUnobscured, VisibilityPartiallyObscured or\r
1075              * VisibilityFullyObscured. Note that we DO NOT receive a\r
1076              * VisibilityNotify event when iconifying a window, we only get an\r
1077              * UnmapNotify then.\r
1078              */\r
1079             GETWINDOW( xvisibility );\r
1080             switch( event.xvisibility.state )\r
1081             {\r
1082             case VisibilityUnobscured:\r
1083                 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );\r
1084                 window->State.Visible = GL_TRUE;\r
1085                 break;\r
1086 \r
1087             case VisibilityPartiallyObscured:\r
1088                 INVOKE_WCB( *window, WindowStatus,\r
1089                             ( GLUT_PARTIALLY_RETAINED ) );\r
1090                 window->State.Visible = GL_TRUE;\r
1091                 break;\r
1092 \r
1093             case VisibilityFullyObscured:\r
1094                 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );\r
1095                 window->State.Visible = GL_FALSE;\r
1096                 break;\r
1097 \r
1098             default:\r
1099                 fgWarning( "Unknown X visibility state: %d",\r
1100                            event.xvisibility.state );\r
1101                 break;\r
1102             }\r
1103         }\r
1104         break;\r
1105 \r
1106         case EnterNotify:\r
1107         case LeaveNotify:\r
1108             GETWINDOW( xcrossing );\r
1109             GETMOUSE( xcrossing );\r
1110             if( ( event.type == LeaveNotify ) && window->IsMenu &&\r
1111                 window->ActiveMenu && window->ActiveMenu->IsActive )\r
1112                 fgUpdateMenuHighlight( window->ActiveMenu );\r
1113 \r
1114             INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?\r
1115                                           GLUT_ENTERED :\r
1116                                           GLUT_LEFT ) );\r
1117             break;\r
1118 \r
1119         case MotionNotify:\r
1120         {\r
1121             GETWINDOW( xmotion );\r
1122             GETMOUSE( xmotion );\r
1123 \r
1124             if( window->ActiveMenu )\r
1125             {\r
1126                 if( window == window->ActiveMenu->ParentWindow )\r
1127                 {\r
1128                     window->ActiveMenu->Window->State.MouseX =\r
1129                         event.xmotion.x_root - window->ActiveMenu->X;\r
1130                     window->ActiveMenu->Window->State.MouseY =\r
1131                         event.xmotion.y_root - window->ActiveMenu->Y;\r
1132                 }\r
1133 \r
1134                 fgUpdateMenuHighlight( window->ActiveMenu );\r
1135 \r
1136                 break;\r
1137             }\r
1138 \r
1139             /*\r
1140              * XXX For more than 5 buttons, just check {event.xmotion.state},\r
1141              * XXX rather than a host of bit-masks?  Or maybe we need to\r
1142              * XXX track ButtonPress/ButtonRelease events in our own\r
1143              * XXX bit-mask?\r
1144              */\r
1145             fgState.Modifiers = fgPlatformGetModifiers( event.xmotion.state );\r
1146             if ( event.xmotion.state & ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask ) ) {\r
1147                 INVOKE_WCB( *window, Motion, ( event.xmotion.x,\r
1148                                                event.xmotion.y ) );\r
1149             } else {\r
1150                 INVOKE_WCB( *window, Passive, ( event.xmotion.x,\r
1151                                                 event.xmotion.y ) );\r
1152             }\r
1153             fgState.Modifiers = INVALID_MODIFIERS;\r
1154         }\r
1155         break;\r
1156 \r
1157         case ButtonRelease:\r
1158         case ButtonPress:\r
1159         {\r
1160             GLboolean pressed = GL_TRUE;\r
1161             int button;\r
1162 \r
1163             if( event.type == ButtonRelease )\r
1164                 pressed = GL_FALSE ;\r
1165 \r
1166             /*\r
1167              * A mouse button has been pressed or released. Traditionally,\r
1168              * break if the window was found within the freeglut structures.\r
1169              */\r
1170             GETWINDOW( xbutton );\r
1171             GETMOUSE( xbutton );\r
1172 \r
1173             /*\r
1174              * An X button (at least in XFree86) is numbered from 1.\r
1175              * A GLUT button is numbered from 0.\r
1176              * Old GLUT passed through buttons other than just the first\r
1177              * three, though it only gave symbolic names and official\r
1178              * support to the first three.\r
1179              */\r
1180             button = event.xbutton.button - 1;\r
1181 \r
1182             /*\r
1183              * Do not execute the application's mouse callback if a menu\r
1184              * is hooked to this button.  In that case an appropriate\r
1185              * private call should be generated.\r
1186              */\r
1187             if( fgCheckActiveMenu( window, button, pressed,\r
1188                                    event.xbutton.x_root, event.xbutton.y_root ) )\r
1189                 break;\r
1190 \r
1191             /*\r
1192              * Check if there is a mouse or mouse wheel callback hooked to the\r
1193              * window\r
1194              */\r
1195             if( ! FETCH_WCB( *window, Mouse ) &&\r
1196                 ! FETCH_WCB( *window, MouseWheel ) )\r
1197                 break;\r
1198 \r
1199             fgState.Modifiers = fgPlatformGetModifiers( event.xbutton.state );\r
1200 \r
1201             /* Finally execute the mouse or mouse wheel callback */\r
1202             if( ( button < glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS ) ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )\r
1203                 INVOKE_WCB( *window, Mouse, ( button,\r
1204                                               pressed ? GLUT_DOWN : GLUT_UP,\r
1205                                               event.xbutton.x,\r
1206                                               event.xbutton.y )\r
1207                 );\r
1208             else\r
1209             {\r
1210                 /*\r
1211                  * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1\r
1212                  *  "  6 and 7 "    "   one; ...\r
1213                  *\r
1214                  * XXX This *should* be behind some variables/macros,\r
1215                  * XXX since the order and numbering isn't certain\r
1216                  * XXX See XFree86 configuration docs (even back in the\r
1217                  * XXX 3.x days, and especially with 4.x).\r
1218                  *\r
1219                  * XXX Note that {button} has already been decremented\r
1220                  * XXX in mapping from X button numbering to GLUT.\r
1221                                  *\r
1222                                  * XXX Should add support for partial wheel turns as Windows does -- 5/27/11\r
1223                  */\r
1224                 int wheel_number = (button - glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS )) / 2;\r
1225                 int direction = -1;\r
1226                 if( button % 2 )\r
1227                     direction = 1;\r
1228 \r
1229                 if( pressed )\r
1230                     INVOKE_WCB( *window, MouseWheel, ( wheel_number,\r
1231                                                        direction,\r
1232                                                        event.xbutton.x,\r
1233                                                        event.xbutton.y )\r
1234                     );\r
1235             }\r
1236             fgState.Modifiers = INVALID_MODIFIERS;\r
1237         }\r
1238         break;\r
1239 \r
1240         case KeyRelease:\r
1241         case KeyPress:\r
1242         {\r
1243             FGCBKeyboard keyboard_cb;\r
1244             FGCBSpecial special_cb;\r
1245 \r
1246             GETWINDOW( xkey );\r
1247             GETMOUSE( xkey );\r
1248 \r
1249             /* Detect auto repeated keys, if configured globally or per-window */\r
1250 \r
1251             if ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE )\r
1252             {\r
1253                 if (event.type==KeyRelease)\r
1254                 {\r
1255                     /*\r
1256                      * Look at X11 keystate to detect repeat mode.\r
1257                      * While X11 says the key is actually held down, we'll ignore KeyRelease/KeyPress pairs.\r
1258                      */\r
1259 \r
1260                     char keys[32];\r
1261                     XQueryKeymap( fgDisplay.pDisplay.Display, keys ); /* Look at X11 keystate to detect repeat mode */\r
1262 \r
1263                     if ( event.xkey.keycode<256 )            /* XQueryKeymap is limited to 256 keycodes    */\r
1264                     {\r
1265                         if ( keys[event.xkey.keycode>>3] & (1<<(event.xkey.keycode%8)) )\r
1266                             window->State.KeyRepeating = GL_TRUE;\r
1267                         else\r
1268                             window->State.KeyRepeating = GL_FALSE;\r
1269                     }\r
1270                 }\r
1271             }\r
1272             else\r
1273                 window->State.KeyRepeating = GL_FALSE;\r
1274 \r
1275             /* Cease processing this event if it is auto repeated */\r
1276 \r
1277             if (window->State.KeyRepeating)\r
1278             {\r
1279                 if (event.type == KeyPress) window->State.KeyRepeating = GL_FALSE;\r
1280                 break;\r
1281             }\r
1282 \r
1283             if( event.type == KeyPress )\r
1284             {\r
1285                 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, Keyboard ));\r
1286                 special_cb  = (FGCBSpecial) ( FETCH_WCB( *window, Special  ));\r
1287             }\r
1288             else\r
1289             {\r
1290                 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, KeyboardUp ));\r
1291                 special_cb  = (FGCBSpecial) ( FETCH_WCB( *window, SpecialUp  ));\r
1292             }\r
1293 \r
1294             /* Is there a keyboard/special callback hooked for this window? */\r
1295             if( keyboard_cb || special_cb )\r
1296             {\r
1297                 XComposeStatus composeStatus;\r
1298                 char asciiCode[ 32 ];\r
1299                 KeySym keySym;\r
1300                 int len;\r
1301 \r
1302                 /* Check for the ASCII/KeySym codes associated with the event: */\r
1303                 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),\r
1304                                      &keySym, &composeStatus\r
1305                 );\r
1306 \r
1307                 /* GLUT API tells us to have two separate callbacks... */\r
1308                 if( len > 0 )\r
1309                 {\r
1310                     /* ...one for the ASCII translateable keypresses... */\r
1311                     if( keyboard_cb )\r
1312                     {\r
1313                         fgSetWindow( window );\r
1314                         fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state );\r
1315                         keyboard_cb( asciiCode[ 0 ],\r
1316                                      event.xkey.x, event.xkey.y\r
1317                         );\r
1318                         fgState.Modifiers = INVALID_MODIFIERS;\r
1319                     }\r
1320                 }\r
1321                 else\r
1322                 {\r
1323                     int special = -1;\r
1324 \r
1325                     /*\r
1326                      * ...and one for all the others, which need to be\r
1327                      * translated to GLUT_KEY_Xs...\r
1328                      */\r
1329                     switch( keySym )\r
1330                     {\r
1331                     case XK_F1:     special = GLUT_KEY_F1;     break;\r
1332                     case XK_F2:     special = GLUT_KEY_F2;     break;\r
1333                     case XK_F3:     special = GLUT_KEY_F3;     break;\r
1334                     case XK_F4:     special = GLUT_KEY_F4;     break;\r
1335                     case XK_F5:     special = GLUT_KEY_F5;     break;\r
1336                     case XK_F6:     special = GLUT_KEY_F6;     break;\r
1337                     case XK_F7:     special = GLUT_KEY_F7;     break;\r
1338                     case XK_F8:     special = GLUT_KEY_F8;     break;\r
1339                     case XK_F9:     special = GLUT_KEY_F9;     break;\r
1340                     case XK_F10:    special = GLUT_KEY_F10;    break;\r
1341                     case XK_F11:    special = GLUT_KEY_F11;    break;\r
1342                     case XK_F12:    special = GLUT_KEY_F12;    break;\r
1343 \r
1344                     case XK_KP_Left:\r
1345                     case XK_Left:   special = GLUT_KEY_LEFT;   break;\r
1346                     case XK_KP_Right:\r
1347                     case XK_Right:  special = GLUT_KEY_RIGHT;  break;\r
1348                     case XK_KP_Up:\r
1349                     case XK_Up:     special = GLUT_KEY_UP;     break;\r
1350                     case XK_KP_Down:\r
1351                     case XK_Down:   special = GLUT_KEY_DOWN;   break;\r
1352 \r
1353                     case XK_KP_Prior:\r
1354                     case XK_Prior:  special = GLUT_KEY_PAGE_UP; break;\r
1355                     case XK_KP_Next:\r
1356                     case XK_Next:   special = GLUT_KEY_PAGE_DOWN; break;\r
1357                     case XK_KP_Home:\r
1358                     case XK_Home:   special = GLUT_KEY_HOME;   break;\r
1359                     case XK_KP_End:\r
1360                     case XK_End:    special = GLUT_KEY_END;    break;\r
1361                     case XK_KP_Insert:\r
1362                     case XK_Insert: special = GLUT_KEY_INSERT; break;\r
1363 \r
1364                     case XK_Num_Lock :  special = GLUT_KEY_NUM_LOCK;  break;\r
1365                     case XK_KP_Begin :  special = GLUT_KEY_BEGIN;     break;\r
1366                     case XK_KP_Delete:  special = GLUT_KEY_DELETE;    break;\r
1367 \r
1368                     case XK_Shift_L:   special = GLUT_KEY_SHIFT_L;    break;\r
1369                     case XK_Shift_R:   special = GLUT_KEY_SHIFT_R;    break;\r
1370                     case XK_Control_L: special = GLUT_KEY_CTRL_L;     break;\r
1371                     case XK_Control_R: special = GLUT_KEY_CTRL_R;     break;\r
1372                     case XK_Alt_L:     special = GLUT_KEY_ALT_L;      break;\r
1373                     case XK_Alt_R:     special = GLUT_KEY_ALT_R;      break;\r
1374                     }\r
1375 \r
1376                     /*\r
1377                      * Execute the callback (if one has been specified),\r
1378                      * given that the special code seems to be valid...\r
1379                      */\r
1380                     if( special_cb && (special != -1) )\r
1381                     {\r
1382                         fgSetWindow( window );\r
1383                         fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state );\r
1384                         special_cb( special, event.xkey.x, event.xkey.y );\r
1385                         fgState.Modifiers = INVALID_MODIFIERS;\r
1386                     }\r
1387                 }\r
1388             }\r
1389         }\r
1390         break;\r
1391 \r
1392         case ReparentNotify:\r
1393             break; /* XXX Should disable this event */\r
1394 \r
1395         /* Not handled */\r
1396         case GravityNotify:\r
1397             break;\r
1398 \r
1399         default:\r
1400             /* enter handling of Extension Events here */\r
1401             #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H\r
1402                 fgHandleExtensionEvents( &event );\r
1403             #endif\r
1404             break;\r
1405         }\r
1406     }\r
1407 }\r
1408 \r
1409 \r
1410 void fgPlatformMainLoopPreliminaryWork ( void )\r
1411 {\r
1412 }\r
1413 #endif\r
1414 \r
1415 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */\r
1416 \r
1417 /*\r
1418  * Executes a single iteration in the freeglut processing loop.\r
1419  */\r
1420 void FGAPIENTRY glutMainLoopEvent( void )\r
1421 {\r
1422         fgPlatformProcessSingleEvent ();\r
1423 \r
1424     if( fgState.Timers.First )\r
1425         fghCheckTimers( );\r
1426     fghCheckJoystickPolls( );\r
1427     fghDisplayAll( );\r
1428 \r
1429     fgCloseWindows( );\r
1430 }\r
1431 \r
1432 /*\r
1433  * Enters the freeglut processing loop.\r
1434  * Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".\r
1435  */\r
1436 void FGAPIENTRY glutMainLoop( void )\r
1437 {\r
1438     int action;\r
1439 \r
1440     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoop" );\r
1441 \r
1442         fgPlatformMainLoopPreliminaryWork ();\r
1443 \r
1444     fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;\r
1445     while( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )\r
1446     {\r
1447         SFG_Window *window;\r
1448 \r
1449         glutMainLoopEvent( );\r
1450         /*\r
1451          * Step through the list of windows, seeing if there are any\r
1452          * that are not menus\r
1453          */\r
1454         for( window = ( SFG_Window * )fgStructure.Windows.First;\r
1455              window;\r
1456              window = ( SFG_Window * )window->Node.Next )\r
1457             if ( ! ( window->IsMenu ) )\r
1458                 break;\r
1459 \r
1460         if( ! window )\r
1461             fgState.ExecState = GLUT_EXEC_STATE_STOP;\r
1462         else\r
1463         {\r
1464             if( fgState.IdleCallback )\r
1465             {\r
1466                 if( fgStructure.CurrentWindow &&\r
1467                     fgStructure.CurrentWindow->IsMenu )\r
1468                     /* fail safe */\r
1469                     fgSetWindow( window );\r
1470                 fgState.IdleCallback( );\r
1471             }\r
1472 \r
1473             fghSleepForEvents( );\r
1474         }\r
1475     }\r
1476 \r
1477     /*\r
1478      * When this loop terminates, destroy the display, state and structure\r
1479      * of a freeglut session, so that another glutInit() call can happen\r
1480      *\r
1481      * Save the "ActionOnWindowClose" because "fgDeinitialize" resets it.\r
1482      */\r
1483     action = fgState.ActionOnWindowClose;\r
1484     fgDeinitialize( );\r
1485     if( action == GLUT_ACTION_EXIT )\r
1486         exit( 0 );\r
1487 }\r
1488 \r
1489 /*\r
1490  * Leaves the freeglut processing loop.\r
1491  */\r
1492 void FGAPIENTRY glutLeaveMainLoop( void )\r
1493 {\r
1494     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutLeaveMainLoop" );\r
1495     fgState.ExecState = GLUT_EXEC_STATE_STOP ;\r
1496 }\r
1497 \r
1498 \r
1499 \r
1500 /*** END OF FILE ***/\r