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