fixed closing window crash, thanks Philipp!
[freeglut] / src / x11 / fg_main_x11.c
1 /*
2  * freeglut_main_x11.c
3  *
4  * The X11-specific windows message processing methods.
5  *
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8  * Copied for Platform code by Evan Felix <karcaw at gmail.com>
9  * Creation date: Thur Feb 2 2012
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
25  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 #include <GL/freeglut.h>
30 #include "../fg_internal.h"
31 #ifdef HAVE_ERRNO_H
32 #    include <errno.h>
33 #endif
34 #include <stdarg.h>
35 #ifdef  HAVE_VFPRINTF
36 #    define VFPRINTF(s,f,a) vfprintf((s),(f),(a))
37 #elif defined(HAVE__DOPRNT)
38 #    define VFPRINTF(s,f,a) _doprnt((f),(a),(s))
39 #else
40 #    define VFPRINTF(s,f,a)
41 #endif
42
43 #include "fg_main.h"
44
45 /*
46  * Try to get the maximum value allowed for ints, falling back to the minimum
47  * guaranteed by ISO C99 if there is no suitable header.
48  */
49 #ifdef HAVE_LIMITS_H
50 #    include <limits.h>
51 #endif
52 #ifndef INT_MAX
53 #    define INT_MAX 32767
54 #endif
55
56 #ifndef MIN
57 #    define MIN(a,b) (((a)<(b)) ? (a) : (b))
58 #endif
59
60 /* used in the event handling code to match and discard stale mouse motion events */
61 static Bool match_motion(Display *dpy, XEvent *xev, XPointer arg);
62
63 /*
64  * TODO BEFORE THE STABLE RELEASE:
65  *
66  * There are some issues concerning window redrawing under X11, and maybe
67  * some events are not handled. The Win32 version lacks some more features,
68  * but seems acceptable for not demanding purposes.
69  *
70  * Need to investigate why the X11 version breaks out with an error when
71  * closing a window (using the window manager, not glutDestroyWindow)...
72  */
73  
74  
75 /*
76  * Request a window resize
77  */
78 void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height )
79 {
80     XResizeWindow( fgDisplay.pDisplay.Display, window->Window.Handle,
81                    width, height );
82     XFlush( fgDisplay.pDisplay.Display ); /* XXX Shouldn't need this */
83 }
84
85
86 /*
87  * A static helper function to execute display callback for a window
88  */
89 void fgPlatformDisplayWindow ( SFG_Window *window )
90 {
91         fghRedrawWindow ( window ) ;
92 }
93
94
95 fg_time_t fgPlatformSystemTime ( void )
96 {
97 #ifdef CLOCK_MONOTONIC
98     struct timespec now;
99     clock_gettime(CLOCK_MONOTONIC, &now);
100     return now.tv_nsec/1000000 + now.tv_sec*1000;
101 #elif defined(HAVE_GETTIMEOFDAY)
102     struct timeval now;
103     gettimeofday( &now, NULL );
104     return now.tv_usec/1000 + now.tv_sec*1000;
105 #endif
106 }
107
108 /*
109  * Does the magic required to relinquish the CPU until something interesting
110  * happens.
111  */
112
113 void fgPlatformSleepForEvents( fg_time_t msec )
114 {
115     /*
116      * Possibly due to aggressive use of XFlush() and friends,
117      * it is possible to have our socket drained but still have
118      * unprocessed events.  (Or, this may just be normal with
119      * X, anyway?)  We do non-trivial processing of X events
120      * after the event-reading loop, in any case, so we
121      * need to allow that we may have an empty socket but non-
122      * empty event queue.
123      */
124     if( ! XPending( fgDisplay.pDisplay.Display ) )
125     {
126         fd_set fdset;
127         int err;
128         int socket;
129         struct timeval wait;
130
131         socket = ConnectionNumber( fgDisplay.pDisplay.Display );
132         FD_ZERO( &fdset );
133         FD_SET( socket, &fdset );
134         wait.tv_sec = msec / 1000;
135         wait.tv_usec = (msec % 1000) * 1000;
136         err = select( socket+1, &fdset, NULL, NULL, &wait );
137
138 #ifdef HAVE_ERRNO_H
139         if( ( -1 == err ) && ( errno != EINTR ) )
140             fgWarning ( "freeglut select() error: %d", errno );
141 #endif
142     }
143 }
144
145
146 /*
147  * Returns GLUT modifier mask for the state field of an X11 event.
148  */
149 int fgPlatformGetModifiers( int state )
150 {
151     int ret = 0;
152
153     if( state & ( ShiftMask | LockMask ) )
154         ret |= GLUT_ACTIVE_SHIFT;
155     if( state & ControlMask )
156         ret |= GLUT_ACTIVE_CTRL;
157     if( state & Mod1Mask )
158         ret |= GLUT_ACTIVE_ALT;
159
160     return ret;
161 }
162
163 static const char* fghTypeToString( int type )
164 {
165     switch( type ) {
166     case KeyPress: return "KeyPress";
167     case KeyRelease: return "KeyRelease";
168     case ButtonPress: return "ButtonPress";
169     case ButtonRelease: return "ButtonRelease";
170     case MotionNotify: return "MotionNotify";
171     case EnterNotify: return "EnterNotify";
172     case LeaveNotify: return "LeaveNotify";
173     case FocusIn: return "FocusIn";
174     case FocusOut: return "FocusOut";
175     case KeymapNotify: return "KeymapNotify";
176     case Expose: return "Expose";
177     case GraphicsExpose: return "GraphicsExpose";
178     case NoExpose: return "NoExpose";
179     case VisibilityNotify: return "VisibilityNotify";
180     case CreateNotify: return "CreateNotify";
181     case DestroyNotify: return "DestroyNotify";
182     case UnmapNotify: return "UnmapNotify";
183     case MapNotify: return "MapNotify";
184     case MapRequest: return "MapRequest";
185     case ReparentNotify: return "ReparentNotify";
186     case ConfigureNotify: return "ConfigureNotify";
187     case ConfigureRequest: return "ConfigureRequest";
188     case GravityNotify: return "GravityNotify";
189     case ResizeRequest: return "ResizeRequest";
190     case CirculateNotify: return "CirculateNotify";
191     case CirculateRequest: return "CirculateRequest";
192     case PropertyNotify: return "PropertyNotify";
193     case SelectionClear: return "SelectionClear";
194     case SelectionRequest: return "SelectionRequest";
195     case SelectionNotify: return "SelectionNotify";
196     case ColormapNotify: return "ColormapNotify";
197     case ClientMessage: return "ClientMessage";
198     case MappingNotify: return "MappingNotify";
199     default: return "UNKNOWN";
200     }
201 }
202
203 static const char* fghBoolToString( Bool b )
204 {
205     return b == False ? "False" : "True";
206 }
207
208 static const char* fghNotifyHintToString( char is_hint )
209 {
210     switch( is_hint ) {
211     case NotifyNormal: return "NotifyNormal";
212     case NotifyHint: return "NotifyHint";
213     default: return "UNKNOWN";
214     }
215 }
216
217 static const char* fghNotifyModeToString( int mode )
218 {
219     switch( mode ) {
220     case NotifyNormal: return "NotifyNormal";
221     case NotifyGrab: return "NotifyGrab";
222     case NotifyUngrab: return "NotifyUngrab";
223     case NotifyWhileGrabbed: return "NotifyWhileGrabbed";
224     default: return "UNKNOWN";
225     }
226 }
227
228 static const char* fghNotifyDetailToString( int detail )
229 {
230     switch( detail ) {
231     case NotifyAncestor: return "NotifyAncestor";
232     case NotifyVirtual: return "NotifyVirtual";
233     case NotifyInferior: return "NotifyInferior";
234     case NotifyNonlinear: return "NotifyNonlinear";
235     case NotifyNonlinearVirtual: return "NotifyNonlinearVirtual";
236     case NotifyPointer: return "NotifyPointer";
237     case NotifyPointerRoot: return "NotifyPointerRoot";
238     case NotifyDetailNone: return "NotifyDetailNone";
239     default: return "UNKNOWN";
240     }
241 }
242
243 static const char* fghVisibilityToString( int state ) {
244     switch( state ) {
245     case VisibilityUnobscured: return "VisibilityUnobscured";
246     case VisibilityPartiallyObscured: return "VisibilityPartiallyObscured";
247     case VisibilityFullyObscured: return "VisibilityFullyObscured";
248     default: return "UNKNOWN";
249     }
250 }
251
252 static const char* fghConfigureDetailToString( int detail )
253 {
254     switch( detail ) {
255     case Above: return "Above";
256     case Below: return "Below";
257     case TopIf: return "TopIf";
258     case BottomIf: return "BottomIf";
259     case Opposite: return "Opposite";
260     default: return "UNKNOWN";
261     }
262 }
263
264 static const char* fghPlaceToString( int place )
265 {
266     switch( place ) {
267     case PlaceOnTop: return "PlaceOnTop";
268     case PlaceOnBottom: return "PlaceOnBottom";
269     default: return "UNKNOWN";
270     }
271 }
272
273 static const char* fghMappingRequestToString( int request )
274 {
275     switch( request ) {
276     case MappingModifier: return "MappingModifier";
277     case MappingKeyboard: return "MappingKeyboard";
278     case MappingPointer: return "MappingPointer";
279     default: return "UNKNOWN";
280     }
281 }
282
283 static const char* fghPropertyStateToString( int state )
284 {
285     switch( state ) {
286     case PropertyNewValue: return "PropertyNewValue";
287     case PropertyDelete: return "PropertyDelete";
288     default: return "UNKNOWN";
289     }
290 }
291
292 static const char* fghColormapStateToString( int state )
293 {
294     switch( state ) {
295     case ColormapUninstalled: return "ColormapUninstalled";
296     case ColormapInstalled: return "ColormapInstalled";
297     default: return "UNKNOWN";
298     }
299 }
300
301 __fg_unused static void fghPrintEvent( XEvent *event )
302 {
303     switch( event->type ) {
304
305     case KeyPress:
306     case KeyRelease: {
307         XKeyEvent *e = &event->xkey;
308         fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
309                    "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
310                    "keycode=%u, same_screen=%s", fghTypeToString( e->type ),
311                    e->window, e->root, e->subwindow, (unsigned long)e->time,
312                    e->x, e->y, e->x_root, e->y_root, e->state, e->keycode,
313                    fghBoolToString( e->same_screen ) );
314         break;
315     }
316
317     case ButtonPress:
318     case ButtonRelease: {
319         XButtonEvent *e = &event->xbutton;
320         fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
321                    "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
322                    "button=%u, same_screen=%d", fghTypeToString( e->type ),
323                    e->window, e->root, e->subwindow, (unsigned long)e->time,
324                    e->x, e->y, e->x_root, e->y_root, e->state, e->button,
325                    fghBoolToString( e->same_screen ) );
326         break;
327     }
328
329     case MotionNotify: {
330         XMotionEvent *e = &event->xmotion;
331         fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
332                    "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
333                    "is_hint=%s, same_screen=%d", fghTypeToString( e->type ),
334                    e->window, e->root, e->subwindow, (unsigned long)e->time,
335                    e->x, e->y, e->x_root, e->y_root, e->state,
336                    fghNotifyHintToString( e->is_hint ),
337                    fghBoolToString( e->same_screen ) );
338         break;
339     }
340
341     case EnterNotify:
342     case LeaveNotify: {
343         XCrossingEvent *e = &event->xcrossing;
344         fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
345                    "(x,y)=(%d,%d), mode=%s, detail=%s, same_screen=%d, "
346                    "focus=%d, state=0x%x", fghTypeToString( e->type ),
347                    e->window, e->root, e->subwindow, (unsigned long)e->time,
348                    e->x, e->y, fghNotifyModeToString( e->mode ),
349                    fghNotifyDetailToString( e->detail ), (int)e->same_screen,
350                    (int)e->focus, e->state );
351         break;
352     }
353
354     case FocusIn:
355     case FocusOut: {
356         XFocusChangeEvent *e = &event->xfocus;
357         fgWarning( "%s: window=0x%x, mode=%s, detail=%s",
358                    fghTypeToString( e->type ), e->window,
359                    fghNotifyModeToString( e->mode ),
360                    fghNotifyDetailToString( e->detail ) );
361         break;
362     }
363
364     case KeymapNotify: {
365         XKeymapEvent *e = &event->xkeymap;
366         char buf[32 * 2 + 1];
367         int i;
368         for ( i = 0; i < 32; i++ ) {
369             snprintf( &buf[ i * 2 ], sizeof( buf ) - i * 2,
370                       "%02x", e->key_vector[ i ] );
371         }
372         buf[ i ] = '\0';
373         fgWarning( "%s: window=0x%x, %s", fghTypeToString( e->type ), e->window,
374                    buf );
375         break;
376     }
377
378     case Expose: {
379         XExposeEvent *e = &event->xexpose;
380         fgWarning( "%s: window=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "
381                    "count=%d", fghTypeToString( e->type ), e->window, e->x,
382                    e->y, e->width, e->height, e->count );
383         break;
384     }
385
386     case GraphicsExpose: {
387         XGraphicsExposeEvent *e = &event->xgraphicsexpose;
388         fgWarning( "%s: drawable=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "
389                    "count=%d, (major_code,minor_code)=(%d,%d)",
390                    fghTypeToString( e->type ), e->drawable, e->x, e->y,
391                    e->width, e->height, e->count, e->major_code,
392                    e->minor_code );
393         break;
394     }
395
396     case NoExpose: {
397         XNoExposeEvent *e = &event->xnoexpose;
398         fgWarning( "%s: drawable=0x%x, (major_code,minor_code)=(%d,%d)",
399                    fghTypeToString( e->type ), e->drawable, e->major_code,
400                    e->minor_code );
401         break;
402     }
403
404     case VisibilityNotify: {
405         XVisibilityEvent *e = &event->xvisibility;
406         fgWarning( "%s: window=0x%x, state=%s", fghTypeToString( e->type ),
407                    e->window, fghVisibilityToString( e->state) );
408         break;
409     }
410
411     case CreateNotify: {
412         XCreateWindowEvent *e = &event->xcreatewindow;
413         fgWarning( "%s: (x,y)=(%d,%d), (width,height)=(%d,%d), border_width=%d, "
414                    "window=0x%x, override_redirect=%s",
415                    fghTypeToString( e->type ), e->x, e->y, e->width, e->height,
416                    e->border_width, e->window,
417                    fghBoolToString( e->override_redirect ) );
418         break;
419     }
420
421     case DestroyNotify: {
422         XDestroyWindowEvent *e = &event->xdestroywindow;
423         fgWarning( "%s: event=0x%x, window=0x%x",
424                    fghTypeToString( e->type ), e->event, e->window );
425         break;
426     }
427
428     case UnmapNotify: {
429         XUnmapEvent *e = &event->xunmap;
430         fgWarning( "%s: event=0x%x, window=0x%x, from_configure=%s",
431                    fghTypeToString( e->type ), e->event, e->window,
432                    fghBoolToString( e->from_configure ) );
433         break;
434     }
435
436     case MapNotify: {
437         XMapEvent *e = &event->xmap;
438         fgWarning( "%s: event=0x%x, window=0x%x, override_redirect=%s",
439                    fghTypeToString( e->type ), e->event, e->window,
440                    fghBoolToString( e->override_redirect ) );
441         break;
442     }
443
444     case MapRequest: {
445         XMapRequestEvent *e = &event->xmaprequest;
446         fgWarning( "%s: parent=0x%x, window=0x%x",
447                    fghTypeToString( event->type ), e->parent, e->window );
448         break;
449     }
450
451     case ReparentNotify: {
452         XReparentEvent *e = &event->xreparent;
453         fgWarning( "%s: event=0x%x, window=0x%x, parent=0x%x, (x,y)=(%d,%d), "
454                    "override_redirect=%s", fghTypeToString( e->type ),
455                    e->event, e->window, e->parent, e->x, e->y,
456                    fghBoolToString( e->override_redirect ) );
457         break;
458     }
459
460     case ConfigureNotify: {
461         XConfigureEvent *e = &event->xconfigure;
462         fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d), "
463                    "(width,height)=(%d,%d), border_width=%d, above=0x%x, "
464                    "override_redirect=%s", fghTypeToString( e->type ), e->event,
465                    e->window, e->x, e->y, e->width, e->height, e->border_width,
466                    e->above, fghBoolToString( e->override_redirect ) );
467         break;
468     }
469
470     case ConfigureRequest: {
471         XConfigureRequestEvent *e = &event->xconfigurerequest;
472         fgWarning( "%s: parent=0x%x, window=0x%x, (x,y)=(%d,%d), "
473                    "(width,height)=(%d,%d), border_width=%d, above=0x%x, "
474                    "detail=%s, value_mask=%lx", fghTypeToString( e->type ),
475                    e->parent, e->window, e->x, e->y, e->width, e->height,
476                    e->border_width, e->above,
477                    fghConfigureDetailToString( e->detail ), e->value_mask );
478         break;
479     }
480
481     case GravityNotify: {
482         XGravityEvent *e = &event->xgravity;
483         fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d)",
484                    fghTypeToString( e->type ), e->event, e->window, e->x, e->y );
485         break;
486     }
487
488     case ResizeRequest: {
489         XResizeRequestEvent *e = &event->xresizerequest;
490         fgWarning( "%s: window=0x%x, (width,height)=(%d,%d)",
491                    fghTypeToString( e->type ), e->window, e->width, e->height );
492         break;
493     }
494
495     case CirculateNotify: {
496         XCirculateEvent *e = &event->xcirculate;
497         fgWarning( "%s: event=0x%x, window=0x%x, place=%s",
498                    fghTypeToString( e->type ), e->event, e->window,
499                    fghPlaceToString( e->place ) );
500         break;
501     }
502
503     case CirculateRequest: {
504         XCirculateRequestEvent *e = &event->xcirculaterequest;
505         fgWarning( "%s: parent=0x%x, window=0x%x, place=%s",
506                    fghTypeToString( e->type ), e->parent, e->window,
507                    fghPlaceToString( e->place ) );
508         break;
509     }
510
511     case PropertyNotify: {
512         XPropertyEvent *e = &event->xproperty;
513         fgWarning( "%s: window=0x%x, atom=%lu, time=%lu, state=%s",
514                    fghTypeToString( e->type ), e->window,
515                    (unsigned long)e->atom, (unsigned long)e->time,
516                    fghPropertyStateToString( e->state ) );
517         break;
518     }
519
520     case SelectionClear: {
521         XSelectionClearEvent *e = &event->xselectionclear;
522         fgWarning( "%s: window=0x%x, selection=%lu, time=%lu",
523                    fghTypeToString( e->type ), e->window,
524                    (unsigned long)e->selection, (unsigned long)e->time );
525         break;
526     }
527
528     case SelectionRequest: {
529         XSelectionRequestEvent *e = &event->xselectionrequest;
530         fgWarning( "%s: owner=0x%x, requestor=0x%x, selection=0x%x, "
531                    "target=0x%x, property=%lu, time=%lu",
532                    fghTypeToString( e->type ), e->owner, e->requestor,
533                    (unsigned long)e->selection, (unsigned long)e->target,
534                    (unsigned long)e->property, (unsigned long)e->time );
535         break;
536     }
537
538     case SelectionNotify: {
539         XSelectionEvent *e = &event->xselection;
540         fgWarning( "%s: requestor=0x%x, selection=0x%x, target=0x%x, "
541                    "property=%lu, time=%lu", fghTypeToString( e->type ),
542                    e->requestor, (unsigned long)e->selection,
543                    (unsigned long)e->target, (unsigned long)e->property,
544                    (unsigned long)e->time );
545         break;
546     }
547
548     case ColormapNotify: {
549         XColormapEvent *e = &event->xcolormap;
550         fgWarning( "%s: window=0x%x, colormap=%lu, new=%s, state=%s",
551                    fghTypeToString( e->type ), e->window,
552                    (unsigned long)e->colormap, fghBoolToString( e->new ),
553                    fghColormapStateToString( e->state ) );
554         break;
555     }
556
557     case ClientMessage: {
558         XClientMessageEvent *e = &event->xclient;
559         char buf[ 61 ];
560         char* p = buf;
561         char* end = buf + sizeof( buf );
562         int i;
563         switch( e->format ) {
564         case 8:
565           for ( i = 0; i < 20; i++, p += 3 ) {
566                 snprintf( p, end - p, " %02x", e->data.b[ i ] );
567             }
568             break;
569         case 16:
570             for ( i = 0; i < 10; i++, p += 5 ) {
571                 snprintf( p, end - p, " %04x", e->data.s[ i ] );
572             }
573             break;
574         case 32:
575             for ( i = 0; i < 5; i++, p += 9 ) {
576                 snprintf( p, end - p, " %08lx", e->data.l[ i ] );
577             }
578             break;
579         }
580         *p = '\0';
581         fgWarning( "%s: window=0x%x, message_type=%lu, format=%d, data=(%s )",
582                    fghTypeToString( e->type ), e->window,
583                    (unsigned long)e->message_type, e->format, buf );
584         break;
585     }
586
587     case MappingNotify: {
588         XMappingEvent *e = &event->xmapping;
589         fgWarning( "%s: window=0x%x, request=%s, first_keycode=%d, count=%d",
590                    fghTypeToString( e->type ), e->window,
591                    fghMappingRequestToString( e->request ), e->first_keycode,
592                    e->count );
593         break;
594     }
595
596     default: {
597         fgWarning( "%s", fghTypeToString( event->type ) );
598         break;
599     }
600     }
601 }
602
603
604 void fgPlatformProcessSingleEvent ( void )
605 {
606     SFG_Window* window;
607     XEvent event;
608
609     /* This code was repeated constantly, so here it goes into a definition: */
610 #define GETWINDOW(a)                             \
611     window = fgWindowByHandle( event.a.window ); \
612     if( window == NULL )                         \
613         break;
614
615 #define GETMOUSE(a)                              \
616     window->State.MouseX = event.a.x;            \
617     window->State.MouseY = event.a.y;
618
619     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
620
621     while( XPending( fgDisplay.pDisplay.Display ) )
622     {
623         XNextEvent( fgDisplay.pDisplay.Display, &event );
624 #if _DEBUG
625         fghPrintEvent( &event );
626 #endif
627
628         switch( event.type )
629         {
630         case ClientMessage:
631             if (fgStructure.CurrentWindow)
632                 if(fgIsSpaceballXEvent(&event)) {
633                     fgSpaceballHandleXEvent(&event);
634                     break;
635                 }
636             /* Destroy the window when the WM_DELETE_WINDOW message arrives */
637             if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.pDisplay.DeleteWindow )
638             {
639                 GETWINDOW( xclient );
640
641                 fgDestroyWindow ( window );
642
643                 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
644                 {
645                     fgDeinitialize( );
646                     exit( 0 );
647                 }
648                 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
649                     fgState.ExecState = GLUT_EXEC_STATE_STOP;
650
651                 return;
652             }
653             break;
654
655             /*
656              * CreateNotify causes a configure-event so that sub-windows are
657              * handled compatibly with GLUT.  Otherwise, your sub-windows
658              * (in freeglut only) will not get an initial reshape event,
659              * which can break things.
660              *
661              * GLUT presumably does this because it generally tries to treat
662              * sub-windows the same as windows.
663              */
664         case CreateNotify:
665         case ConfigureNotify:
666             {
667                 int width, height;
668                 if( event.type == CreateNotify ) {
669                     GETWINDOW( xcreatewindow );
670                     width = event.xcreatewindow.width;
671                     height = event.xcreatewindow.height;
672                 } else {
673                     GETWINDOW( xconfigure );
674                     width = event.xconfigure.width;
675                     height = event.xconfigure.height;
676                 }
677
678                 if( ( width != window->State.pWState.OldWidth ) ||
679                     ( height != window->State.pWState.OldHeight ) )
680                 {
681                     SFG_Window *current_window = fgStructure.CurrentWindow;
682
683                     window->State.pWState.OldWidth = width;
684                     window->State.pWState.OldHeight = height;
685                     if( FETCH_WCB( *window, Reshape ) )
686                         INVOKE_WCB( *window, Reshape, ( width, height ) );
687                     else
688                     {
689                         fgSetWindow( window );
690                         glViewport( 0, 0, width, height );
691                     }
692                     glutPostRedisplay( );
693                     if( window->IsMenu )
694                         fgSetWindow( current_window );
695                 }
696             }
697             break;
698
699         case DestroyNotify:
700             /*
701              * This is sent to confirm the XDestroyWindow call.
702              *
703              * XXX WHY is this commented out?  Should we re-enable it?
704              */
705             /* fgAddToWindowDestroyList ( window ); */
706             break;
707
708         case Expose:
709             /*
710              * We are too dumb to process partial exposes...
711              *
712              * XXX Well, we could do it.  However, it seems to only
713              * XXX be potentially useful for single-buffered (since
714              * XXX double-buffered does not respect viewport when we
715              * XXX do a buffer-swap).
716              *
717              */
718             if( event.xexpose.count == 0 )
719             {
720                 GETWINDOW( xexpose );
721                 window->State.Redisplay = GL_TRUE;
722             }
723             break;
724
725         case MapNotify:
726             break;
727
728         case UnmapNotify:
729             /* We get this when iconifying a window. */ 
730             GETWINDOW( xunmap );
731             INVOKE_WCB( *window, WindowStatus, ( GLUT_HIDDEN ) );
732             window->State.Visible = GL_FALSE;
733             break;
734
735         case MappingNotify:
736             /*
737              * Have the client's keyboard knowledge updated (xlib.ps,
738              * page 206, says that's a good thing to do)
739              */
740             XRefreshKeyboardMapping( (XMappingEvent *) &event );
741             break;
742
743         case VisibilityNotify:
744         {
745             /*
746              * Sending this event, the X server can notify us that the window
747              * has just acquired one of the three possible visibility states:
748              * VisibilityUnobscured, VisibilityPartiallyObscured or
749              * VisibilityFullyObscured. Note that we DO NOT receive a
750              * VisibilityNotify event when iconifying a window, we only get an
751              * UnmapNotify then.
752              */
753             GETWINDOW( xvisibility );
754             switch( event.xvisibility.state )
755             {
756             case VisibilityUnobscured:
757                 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
758                 window->State.Visible = GL_TRUE;
759                 break;
760
761             case VisibilityPartiallyObscured:
762                 INVOKE_WCB( *window, WindowStatus,
763                             ( GLUT_PARTIALLY_RETAINED ) );
764                 window->State.Visible = GL_TRUE;
765                 break;
766
767             case VisibilityFullyObscured:
768                 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
769                 window->State.Visible = GL_FALSE;
770                 break;
771
772             default:
773                 fgWarning( "Unknown X visibility state: %d",
774                            event.xvisibility.state );
775                 break;
776             }
777         }
778         break;
779
780         case EnterNotify:
781         case LeaveNotify:
782             GETWINDOW( xcrossing );
783             GETMOUSE( xcrossing );
784             if( ( event.type == LeaveNotify ) && window->IsMenu &&
785                 window->ActiveMenu && window->ActiveMenu->IsActive )
786                 fgUpdateMenuHighlight( window->ActiveMenu );
787
788             INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
789                                           GLUT_ENTERED :
790                                           GLUT_LEFT ) );
791             break;
792
793         case MotionNotify:
794         {
795             /* if GLUT_SKIP_STALE_MOTION_EVENTS is true, then discard all but
796              * the last motion event from the queue
797              */
798             if(fgState.SkipStaleMotion) {
799                 while(XCheckIfEvent(fgDisplay.pDisplay.Display, &event, match_motion, 0));
800             }
801
802             GETWINDOW( xmotion );
803             GETMOUSE( xmotion );
804
805             if( window->ActiveMenu )
806             {
807                 if( window == window->ActiveMenu->ParentWindow )
808                 {
809                     window->ActiveMenu->Window->State.MouseX =
810                         event.xmotion.x_root - window->ActiveMenu->X;
811                     window->ActiveMenu->Window->State.MouseY =
812                         event.xmotion.y_root - window->ActiveMenu->Y;
813                 }
814
815                 fgUpdateMenuHighlight( window->ActiveMenu );
816
817                 break;
818             }
819
820             /*
821              * XXX For more than 5 buttons, just check {event.xmotion.state},
822              * XXX rather than a host of bit-masks?  Or maybe we need to
823              * XXX track ButtonPress/ButtonRelease events in our own
824              * XXX bit-mask?
825              */
826             fgState.Modifiers = fgPlatformGetModifiers( event.xmotion.state );
827             if ( event.xmotion.state & ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask ) ) {
828                 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
829                                                event.xmotion.y ) );
830             } else {
831                 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
832                                                 event.xmotion.y ) );
833             }
834             fgState.Modifiers = INVALID_MODIFIERS;
835         }
836         break;
837
838         case ButtonRelease:
839         case ButtonPress:
840         {
841             GLboolean pressed = GL_TRUE;
842             int button;
843
844             if( event.type == ButtonRelease )
845                 pressed = GL_FALSE ;
846
847             /*
848              * A mouse button has been pressed or released. Traditionally,
849              * break if the window was found within the freeglut structures.
850              */
851             GETWINDOW( xbutton );
852             GETMOUSE( xbutton );
853
854             /*
855              * An X button (at least in XFree86) is numbered from 1.
856              * A GLUT button is numbered from 0.
857              * Old GLUT passed through buttons other than just the first
858              * three, though it only gave symbolic names and official
859              * support to the first three.
860              */
861             button = event.xbutton.button - 1;
862
863             /*
864              * Do not execute the application's mouse callback if a menu
865              * is hooked to this button.  In that case an appropriate
866              * private call should be generated.
867              */
868             if( fgCheckActiveMenu( window, button, pressed,
869                                    event.xbutton.x_root, event.xbutton.y_root ) )
870                 break;
871
872             /*
873              * Check if there is a mouse or mouse wheel callback hooked to the
874              * window
875              */
876             if( ! FETCH_WCB( *window, Mouse ) &&
877                 ! FETCH_WCB( *window, MouseWheel ) )
878                 break;
879
880             fgState.Modifiers = fgPlatformGetModifiers( event.xbutton.state );
881
882             /* Finally execute the mouse or mouse wheel callback */
883             if( ( button < glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS ) ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
884                 INVOKE_WCB( *window, Mouse, ( button,
885                                               pressed ? GLUT_DOWN : GLUT_UP,
886                                               event.xbutton.x,
887                                               event.xbutton.y )
888                 );
889             else
890             {
891                 /*
892                  * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
893                  *  "  6 and 7 "    "   one; ...
894                  *
895                  * XXX This *should* be behind some variables/macros,
896                  * XXX since the order and numbering isn't certain
897                  * XXX See XFree86 configuration docs (even back in the
898                  * XXX 3.x days, and especially with 4.x).
899                  *
900                  * XXX Note that {button} has already been decremented
901                  * XXX in mapping from X button numbering to GLUT.
902                                  *
903                                  * XXX Should add support for partial wheel turns as Windows does -- 5/27/11
904                  */
905                 int wheel_number = (button - glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS )) / 2;
906                 int direction = -1;
907                 if( button % 2 )
908                     direction = 1;
909
910                 if( pressed )
911                     INVOKE_WCB( *window, MouseWheel, ( wheel_number,
912                                                        direction,
913                                                        event.xbutton.x,
914                                                        event.xbutton.y )
915                     );
916             }
917             fgState.Modifiers = INVALID_MODIFIERS;
918         }
919         break;
920
921         case KeyRelease:
922         case KeyPress:
923         {
924             FGCBKeyboard keyboard_cb;
925             FGCBSpecial special_cb;
926
927             GETWINDOW( xkey );
928             GETMOUSE( xkey );
929
930             /* Detect auto repeated keys, if configured globally or per-window */
931
932             if ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE )
933             {
934                 if (event.type==KeyRelease)
935                 {
936                     /*
937                      * Look at X11 keystate to detect repeat mode.
938                      * While X11 says the key is actually held down, we'll ignore KeyRelease/KeyPress pairs.
939                      */
940
941                     char keys[32];
942                     XQueryKeymap( fgDisplay.pDisplay.Display, keys ); /* Look at X11 keystate to detect repeat mode */
943
944                     if ( event.xkey.keycode<256 )            /* XQueryKeymap is limited to 256 keycodes    */
945                     {
946                         if ( keys[event.xkey.keycode>>3] & (1<<(event.xkey.keycode%8)) )
947                             window->State.KeyRepeating = GL_TRUE;
948                         else
949                             window->State.KeyRepeating = GL_FALSE;
950                     }
951                 }
952             }
953             else
954                 window->State.KeyRepeating = GL_FALSE;
955
956             /* Cease processing this event if it is auto repeated */
957
958             if (window->State.KeyRepeating)
959             {
960                 if (event.type == KeyPress) window->State.KeyRepeating = GL_FALSE;
961                 break;
962             }
963
964             if( event.type == KeyPress )
965             {
966                 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, Keyboard ));
967                 special_cb  = (FGCBSpecial) ( FETCH_WCB( *window, Special  ));
968             }
969             else
970             {
971                 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, KeyboardUp ));
972                 special_cb  = (FGCBSpecial) ( FETCH_WCB( *window, SpecialUp  ));
973             }
974
975             /* Is there a keyboard/special callback hooked for this window? */
976             if( keyboard_cb || special_cb )
977             {
978                 XComposeStatus composeStatus;
979                 char asciiCode[ 32 ];
980                 KeySym keySym;
981                 int len;
982
983                 /* Check for the ASCII/KeySym codes associated with the event: */
984                 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
985                                      &keySym, &composeStatus
986                 );
987
988                 /* GLUT API tells us to have two separate callbacks... */
989                 if( len > 0 )
990                 {
991                     /* ...one for the ASCII translateable keypresses... */
992                     if( keyboard_cb )
993                     {
994                         fgSetWindow( window );
995                         fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state );
996                         keyboard_cb( asciiCode[ 0 ],
997                                      event.xkey.x, event.xkey.y
998                         );
999                         fgState.Modifiers = INVALID_MODIFIERS;
1000                     }
1001                 }
1002                 else
1003                 {
1004                     int special = -1;
1005
1006                     /*
1007                      * ...and one for all the others, which need to be
1008                      * translated to GLUT_KEY_Xs...
1009                      */
1010                     switch( keySym )
1011                     {
1012                     case XK_F1:     special = GLUT_KEY_F1;     break;
1013                     case XK_F2:     special = GLUT_KEY_F2;     break;
1014                     case XK_F3:     special = GLUT_KEY_F3;     break;
1015                     case XK_F4:     special = GLUT_KEY_F4;     break;
1016                     case XK_F5:     special = GLUT_KEY_F5;     break;
1017                     case XK_F6:     special = GLUT_KEY_F6;     break;
1018                     case XK_F7:     special = GLUT_KEY_F7;     break;
1019                     case XK_F8:     special = GLUT_KEY_F8;     break;
1020                     case XK_F9:     special = GLUT_KEY_F9;     break;
1021                     case XK_F10:    special = GLUT_KEY_F10;    break;
1022                     case XK_F11:    special = GLUT_KEY_F11;    break;
1023                     case XK_F12:    special = GLUT_KEY_F12;    break;
1024
1025                     case XK_KP_Left:
1026                     case XK_Left:   special = GLUT_KEY_LEFT;   break;
1027                     case XK_KP_Right:
1028                     case XK_Right:  special = GLUT_KEY_RIGHT;  break;
1029                     case XK_KP_Up:
1030                     case XK_Up:     special = GLUT_KEY_UP;     break;
1031                     case XK_KP_Down:
1032                     case XK_Down:   special = GLUT_KEY_DOWN;   break;
1033
1034                     case XK_KP_Prior:
1035                     case XK_Prior:  special = GLUT_KEY_PAGE_UP; break;
1036                     case XK_KP_Next:
1037                     case XK_Next:   special = GLUT_KEY_PAGE_DOWN; break;
1038                     case XK_KP_Home:
1039                     case XK_Home:   special = GLUT_KEY_HOME;   break;
1040                     case XK_KP_End:
1041                     case XK_End:    special = GLUT_KEY_END;    break;
1042                     case XK_KP_Insert:
1043                     case XK_Insert: special = GLUT_KEY_INSERT; break;
1044
1045                     case XK_Num_Lock :  special = GLUT_KEY_NUM_LOCK;  break;
1046                     case XK_KP_Begin :  special = GLUT_KEY_BEGIN;     break;
1047                     case XK_KP_Delete:  special = GLUT_KEY_DELETE;    break;
1048
1049                     case XK_Shift_L:   special = GLUT_KEY_SHIFT_L;    break;
1050                     case XK_Shift_R:   special = GLUT_KEY_SHIFT_R;    break;
1051                     case XK_Control_L: special = GLUT_KEY_CTRL_L;     break;
1052                     case XK_Control_R: special = GLUT_KEY_CTRL_R;     break;
1053                     case XK_Alt_L:     special = GLUT_KEY_ALT_L;      break;
1054                     case XK_Alt_R:     special = GLUT_KEY_ALT_R;      break;
1055                     }
1056
1057                     /*
1058                      * Execute the callback (if one has been specified),
1059                      * given that the special code seems to be valid...
1060                      */
1061                     if( special_cb && (special != -1) )
1062                     {
1063                         fgSetWindow( window );
1064                         fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state );
1065                         special_cb( special, event.xkey.x, event.xkey.y );
1066                         fgState.Modifiers = INVALID_MODIFIERS;
1067                     }
1068                 }
1069             }
1070         }
1071         break;
1072
1073         case ReparentNotify:
1074             break; /* XXX Should disable this event */
1075
1076         /* Not handled */
1077         case GravityNotify:
1078             break;
1079
1080         default:
1081             /* enter handling of Extension Events here */
1082             #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1083                 fgHandleExtensionEvents( &event );
1084             #endif
1085             break;
1086         }
1087     }
1088 }
1089
1090
1091 static Bool match_motion(Display *dpy, XEvent *xev, XPointer arg)
1092 {
1093     return xev->type == MotionNotify;
1094 }
1095
1096 void fgPlatformMainLoopPreliminaryWork ( void )
1097 {
1098 }
1099