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