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