4 * The X11-specific windows message processing methods.
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
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:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
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.
29 #include <GL/freeglut.h>
30 #include "../fg_internal.h"
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))
40 # define VFPRINTF(s,f,a)
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.
53 # define INT_MAX 32767
57 # define MIN(a,b) (((a)<(b)) ? (a) : (b))
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);
64 * TODO BEFORE THE STABLE RELEASE:
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.
70 * Need to investigate why the X11 version breaks out with an error when
71 * closing a window (using the window manager, not glutDestroyWindow)...
76 * Request a window resize
78 void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height )
80 XResizeWindow( fgDisplay.pDisplay.Display, window->Window.Handle,
82 XFlush( fgDisplay.pDisplay.Display ); /* XXX Shouldn't need this */
87 * A static helper function to execute display callback for a window
89 void fgPlatformDisplayWindow ( SFG_Window *window )
91 fghRedrawWindow ( window ) ;
95 fg_time_t fgPlatformSystemTime ( void )
97 #ifdef CLOCK_MONOTONIC
99 clock_gettime(CLOCK_MONOTONIC, &now);
100 return now.tv_nsec/1000000 + now.tv_sec*1000;
101 #elif defined(HAVE_GETTIMEOFDAY)
103 gettimeofday( &now, NULL );
104 return now.tv_usec/1000 + now.tv_sec*1000;
109 * Does the magic required to relinquish the CPU until something interesting
113 void fgPlatformSleepForEvents( fg_time_t msec )
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-
124 if( ! XPending( fgDisplay.pDisplay.Display ) )
131 socket = ConnectionNumber( fgDisplay.pDisplay.Display );
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 );
139 if( ( -1 == err ) && ( errno != EINTR ) )
140 fgWarning ( "freeglut select() error: %d", errno );
147 * Returns GLUT modifier mask for the state field of an X11 event.
149 int fgPlatformGetModifiers( int state )
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;
163 static const char* fghTypeToString( int 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";
203 static const char* fghBoolToString( Bool b )
205 return b == False ? "False" : "True";
208 static const char* fghNotifyHintToString( char is_hint )
211 case NotifyNormal: return "NotifyNormal";
212 case NotifyHint: return "NotifyHint";
213 default: return "UNKNOWN";
217 static const char* fghNotifyModeToString( int 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";
228 static const char* fghNotifyDetailToString( int 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";
243 static const char* fghVisibilityToString( int state ) {
245 case VisibilityUnobscured: return "VisibilityUnobscured";
246 case VisibilityPartiallyObscured: return "VisibilityPartiallyObscured";
247 case VisibilityFullyObscured: return "VisibilityFullyObscured";
248 default: return "UNKNOWN";
252 static const char* fghConfigureDetailToString( int 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";
264 static const char* fghPlaceToString( int place )
267 case PlaceOnTop: return "PlaceOnTop";
268 case PlaceOnBottom: return "PlaceOnBottom";
269 default: return "UNKNOWN";
273 static const char* fghMappingRequestToString( int request )
276 case MappingModifier: return "MappingModifier";
277 case MappingKeyboard: return "MappingKeyboard";
278 case MappingPointer: return "MappingPointer";
279 default: return "UNKNOWN";
283 static const char* fghPropertyStateToString( int state )
286 case PropertyNewValue: return "PropertyNewValue";
287 case PropertyDelete: return "PropertyDelete";
288 default: return "UNKNOWN";
292 static const char* fghColormapStateToString( int state )
295 case ColormapUninstalled: return "ColormapUninstalled";
296 case ColormapInstalled: return "ColormapInstalled";
297 default: return "UNKNOWN";
301 __fg_unused static void fghPrintEvent( XEvent *event )
303 switch( event->type ) {
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 ) );
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 ) );
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 ) );
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 );
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 ) );
365 XKeymapEvent *e = &event->xkeymap;
366 char buf[32 * 2 + 1];
368 for ( i = 0; i < 32; i++ ) {
369 snprintf( &buf[ i * 2 ], sizeof( buf ) - i * 2,
370 "%02x", e->key_vector[ i ] );
373 fgWarning( "%s: window=0x%x, %s", fghTypeToString( e->type ), e->window,
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 );
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,
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,
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) );
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 ) );
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 );
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 ) );
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 ) );
445 XMapRequestEvent *e = &event->xmaprequest;
446 fgWarning( "%s: parent=0x%x, window=0x%x",
447 fghTypeToString( event->type ), e->parent, e->window );
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 ) );
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 ) );
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 );
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 );
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 );
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 ) );
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 ) );
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 ) );
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 );
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 );
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 );
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 ) );
557 case ClientMessage: {
558 XClientMessageEvent *e = &event->xclient;
561 char* end = buf + sizeof( buf );
563 switch( e->format ) {
565 for ( i = 0; i < 20; i++, p += 3 ) {
566 snprintf( p, end - p, " %02x", e->data.b[ i ] );
570 for ( i = 0; i < 10; i++, p += 5 ) {
571 snprintf( p, end - p, " %04x", e->data.s[ i ] );
575 for ( i = 0; i < 5; i++, p += 9 ) {
576 snprintf( p, end - p, " %08lx", e->data.l[ i ] );
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 );
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,
597 fgWarning( "%s", fghTypeToString( event->type ) );
604 void fgPlatformProcessSingleEvent ( void )
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 ) \
615 #define GETMOUSE(a) \
616 window->State.MouseX = event.a.x; \
617 window->State.MouseY = event.a.y;
619 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
621 while( XPending( fgDisplay.pDisplay.Display ) )
623 XNextEvent( fgDisplay.pDisplay.Display, &event );
625 fghPrintEvent( &event );
631 if(fgIsSpaceballXEvent(&event)) {
632 fgSpaceballHandleXEvent(&event);
635 /* Destroy the window when the WM_DELETE_WINDOW message arrives */
636 if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.pDisplay.DeleteWindow )
638 GETWINDOW( xclient );
640 fgDestroyWindow ( window );
642 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
647 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
648 fgState.ExecState = GLUT_EXEC_STATE_STOP;
655 * CreateNotify causes a configure-event so that sub-windows are
656 * handled compatibly with GLUT. Otherwise, your sub-windows
657 * (in freeglut only) will not get an initial reshape event,
658 * which can break things.
660 * GLUT presumably does this because it generally tries to treat
661 * sub-windows the same as windows.
664 case ConfigureNotify:
667 if( event.type == CreateNotify ) {
668 GETWINDOW( xcreatewindow );
669 width = event.xcreatewindow.width;
670 height = event.xcreatewindow.height;
672 GETWINDOW( xconfigure );
673 width = event.xconfigure.width;
674 height = event.xconfigure.height;
677 if( ( width != window->State.pWState.OldWidth ) ||
678 ( height != window->State.pWState.OldHeight ) )
680 SFG_Window *current_window = fgStructure.CurrentWindow;
682 window->State.pWState.OldWidth = width;
683 window->State.pWState.OldHeight = height;
684 if( FETCH_WCB( *window, Reshape ) )
685 INVOKE_WCB( *window, Reshape, ( width, height ) );
688 fgSetWindow( window );
689 glViewport( 0, 0, width, height );
691 glutPostRedisplay( );
693 fgSetWindow( current_window );
700 * This is sent to confirm the XDestroyWindow call.
702 * XXX WHY is this commented out? Should we re-enable it?
704 /* fgAddToWindowDestroyList ( window ); */
709 * We are too dumb to process partial exposes...
711 * XXX Well, we could do it. However, it seems to only
712 * XXX be potentially useful for single-buffered (since
713 * XXX double-buffered does not respect viewport when we
714 * XXX do a buffer-swap).
717 if( event.xexpose.count == 0 )
719 GETWINDOW( xexpose );
720 window->State.Redisplay = GL_TRUE;
728 /* We get this when iconifying a window. */
730 INVOKE_WCB( *window, WindowStatus, ( GLUT_HIDDEN ) );
731 window->State.Visible = GL_FALSE;
736 * Have the client's keyboard knowledge updated (xlib.ps,
737 * page 206, says that's a good thing to do)
739 XRefreshKeyboardMapping( (XMappingEvent *) &event );
742 case VisibilityNotify:
745 * Sending this event, the X server can notify us that the window
746 * has just acquired one of the three possible visibility states:
747 * VisibilityUnobscured, VisibilityPartiallyObscured or
748 * VisibilityFullyObscured. Note that we DO NOT receive a
749 * VisibilityNotify event when iconifying a window, we only get an
752 GETWINDOW( xvisibility );
753 switch( event.xvisibility.state )
755 case VisibilityUnobscured:
756 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
757 window->State.Visible = GL_TRUE;
760 case VisibilityPartiallyObscured:
761 INVOKE_WCB( *window, WindowStatus,
762 ( GLUT_PARTIALLY_RETAINED ) );
763 window->State.Visible = GL_TRUE;
766 case VisibilityFullyObscured:
767 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
768 window->State.Visible = GL_FALSE;
772 fgWarning( "Unknown X visibility state: %d",
773 event.xvisibility.state );
781 GETWINDOW( xcrossing );
782 GETMOUSE( xcrossing );
783 if( ( event.type == LeaveNotify ) && window->IsMenu &&
784 window->ActiveMenu && window->ActiveMenu->IsActive )
785 fgUpdateMenuHighlight( window->ActiveMenu );
787 INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
794 /* if GLUT_SKIP_STALE_MOTION_EVENTS is true, then discard all but
795 * the last motion event from the queue
797 if(fgState.SkipStaleMotion) {
798 while(XCheckIfEvent(fgDisplay.pDisplay.Display, &event, match_motion, 0));
801 GETWINDOW( xmotion );
804 if( window->ActiveMenu )
806 if( window == window->ActiveMenu->ParentWindow )
808 window->ActiveMenu->Window->State.MouseX =
809 event.xmotion.x_root - window->ActiveMenu->X;
810 window->ActiveMenu->Window->State.MouseY =
811 event.xmotion.y_root - window->ActiveMenu->Y;
814 fgUpdateMenuHighlight( window->ActiveMenu );
820 * XXX For more than 5 buttons, just check {event.xmotion.state},
821 * XXX rather than a host of bit-masks? Or maybe we need to
822 * XXX track ButtonPress/ButtonRelease events in our own
825 fgState.Modifiers = fgPlatformGetModifiers( event.xmotion.state );
826 if ( event.xmotion.state & ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask ) ) {
827 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
830 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
833 fgState.Modifiers = INVALID_MODIFIERS;
840 GLboolean pressed = GL_TRUE;
843 if( event.type == ButtonRelease )
847 * A mouse button has been pressed or released. Traditionally,
848 * break if the window was found within the freeglut structures.
850 GETWINDOW( xbutton );
854 * An X button (at least in XFree86) is numbered from 1.
855 * A GLUT button is numbered from 0.
856 * Old GLUT passed through buttons other than just the first
857 * three, though it only gave symbolic names and official
858 * support to the first three.
860 button = event.xbutton.button - 1;
863 * Do not execute the application's mouse callback if a menu
864 * is hooked to this button. In that case an appropriate
865 * private call should be generated.
867 if( fgCheckActiveMenu( window, button, pressed,
868 event.xbutton.x_root, event.xbutton.y_root ) )
872 * Check if there is a mouse or mouse wheel callback hooked to the
875 if( ! FETCH_WCB( *window, Mouse ) &&
876 ! FETCH_WCB( *window, MouseWheel ) )
879 fgState.Modifiers = fgPlatformGetModifiers( event.xbutton.state );
881 /* Finally execute the mouse or mouse wheel callback */
882 if( ( button < glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS ) ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
883 INVOKE_WCB( *window, Mouse, ( button,
884 pressed ? GLUT_DOWN : GLUT_UP,
891 * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
892 * " 6 and 7 " " one; ...
894 * XXX This *should* be behind some variables/macros,
895 * XXX since the order and numbering isn't certain
896 * XXX See XFree86 configuration docs (even back in the
897 * XXX 3.x days, and especially with 4.x).
899 * XXX Note that {button} has already been decremented
900 * XXX in mapping from X button numbering to GLUT.
902 * XXX Should add support for partial wheel turns as Windows does -- 5/27/11
904 int wheel_number = (button - glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS )) / 2;
910 INVOKE_WCB( *window, MouseWheel, ( wheel_number,
916 fgState.Modifiers = INVALID_MODIFIERS;
923 FGCBKeyboard keyboard_cb;
924 FGCBSpecial special_cb;
929 /* Detect auto repeated keys, if configured globally or per-window */
931 if ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE )
933 if (event.type==KeyRelease)
936 * Look at X11 keystate to detect repeat mode.
937 * While X11 says the key is actually held down, we'll ignore KeyRelease/KeyPress pairs.
941 XQueryKeymap( fgDisplay.pDisplay.Display, keys ); /* Look at X11 keystate to detect repeat mode */
943 if ( event.xkey.keycode<256 ) /* XQueryKeymap is limited to 256 keycodes */
945 if ( keys[event.xkey.keycode>>3] & (1<<(event.xkey.keycode%8)) )
946 window->State.KeyRepeating = GL_TRUE;
948 window->State.KeyRepeating = GL_FALSE;
953 window->State.KeyRepeating = GL_FALSE;
955 /* Cease processing this event if it is auto repeated */
957 if (window->State.KeyRepeating)
959 if (event.type == KeyPress) window->State.KeyRepeating = GL_FALSE;
963 if( event.type == KeyPress )
965 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, Keyboard ));
966 special_cb = (FGCBSpecial) ( FETCH_WCB( *window, Special ));
970 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, KeyboardUp ));
971 special_cb = (FGCBSpecial) ( FETCH_WCB( *window, SpecialUp ));
974 /* Is there a keyboard/special callback hooked for this window? */
975 if( keyboard_cb || special_cb )
977 XComposeStatus composeStatus;
978 char asciiCode[ 32 ];
982 /* Check for the ASCII/KeySym codes associated with the event: */
983 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
984 &keySym, &composeStatus
987 /* GLUT API tells us to have two separate callbacks... */
990 /* ...one for the ASCII translateable keypresses... */
993 fgSetWindow( window );
994 fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state );
995 keyboard_cb( asciiCode[ 0 ],
996 event.xkey.x, event.xkey.y
998 fgState.Modifiers = INVALID_MODIFIERS;
1006 * ...and one for all the others, which need to be
1007 * translated to GLUT_KEY_Xs...
1011 case XK_F1: special = GLUT_KEY_F1; break;
1012 case XK_F2: special = GLUT_KEY_F2; break;
1013 case XK_F3: special = GLUT_KEY_F3; break;
1014 case XK_F4: special = GLUT_KEY_F4; break;
1015 case XK_F5: special = GLUT_KEY_F5; break;
1016 case XK_F6: special = GLUT_KEY_F6; break;
1017 case XK_F7: special = GLUT_KEY_F7; break;
1018 case XK_F8: special = GLUT_KEY_F8; break;
1019 case XK_F9: special = GLUT_KEY_F9; break;
1020 case XK_F10: special = GLUT_KEY_F10; break;
1021 case XK_F11: special = GLUT_KEY_F11; break;
1022 case XK_F12: special = GLUT_KEY_F12; break;
1025 case XK_Left: special = GLUT_KEY_LEFT; break;
1027 case XK_Right: special = GLUT_KEY_RIGHT; break;
1029 case XK_Up: special = GLUT_KEY_UP; break;
1031 case XK_Down: special = GLUT_KEY_DOWN; break;
1034 case XK_Prior: special = GLUT_KEY_PAGE_UP; break;
1036 case XK_Next: special = GLUT_KEY_PAGE_DOWN; break;
1038 case XK_Home: special = GLUT_KEY_HOME; break;
1040 case XK_End: special = GLUT_KEY_END; break;
1042 case XK_Insert: special = GLUT_KEY_INSERT; break;
1044 case XK_Num_Lock : special = GLUT_KEY_NUM_LOCK; break;
1045 case XK_KP_Begin : special = GLUT_KEY_BEGIN; break;
1046 case XK_KP_Delete: special = GLUT_KEY_DELETE; break;
1048 case XK_Shift_L: special = GLUT_KEY_SHIFT_L; break;
1049 case XK_Shift_R: special = GLUT_KEY_SHIFT_R; break;
1050 case XK_Control_L: special = GLUT_KEY_CTRL_L; break;
1051 case XK_Control_R: special = GLUT_KEY_CTRL_R; break;
1052 case XK_Alt_L: special = GLUT_KEY_ALT_L; break;
1053 case XK_Alt_R: special = GLUT_KEY_ALT_R; break;
1057 * Execute the callback (if one has been specified),
1058 * given that the special code seems to be valid...
1060 if( special_cb && (special != -1) )
1062 fgSetWindow( window );
1063 fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state );
1064 special_cb( special, event.xkey.x, event.xkey.y );
1065 fgState.Modifiers = INVALID_MODIFIERS;
1072 case ReparentNotify:
1073 break; /* XXX Should disable this event */
1080 /* enter handling of Extension Events here */
1081 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1082 fgHandleExtensionEvents( &event );
1090 static Bool match_motion(Display *dpy, XEvent *xev, XPointer arg)
1092 return xev->type == MotionNotify;
1095 void fgPlatformMainLoopPreliminaryWork ( void )