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