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 "../Common/freeglut_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)
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.
51 # define INT_MAX 32767
55 # define MIN(a,b) (((a)<(b)) ? (a) : (b))
59 * TODO BEFORE THE STABLE RELEASE:
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.
65 * Need to investigate why the X11 version breaks out with an error when
66 * closing a window (using the window manager, not glutDestroyWindow)...
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.
75 void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height )
77 XResizeWindow( fgDisplay.pDisplay.Display, window->Window.Handle,
79 XFlush( fgDisplay.pDisplay.Display ); /* XXX Shouldn't need this */
84 * A static helper function to execute display callback for a window
86 void fgPlatformDisplayWindow ( SFG_Window *window )
88 fghRedrawWindow ( window ) ;
92 unsigned long fgPlatformSystemTime ( void )
94 #ifdef HAVE_GETTIMEOFDAY
96 gettimeofday( &now, NULL );
97 return now.tv_usec/1000 + now.tv_sec*1000;
102 * Does the magic required to relinquish the CPU until something interesting
106 void fgPlatformSleepForEvents( long msec )
109 * Possibly due to aggressive use of XFlush() and friends,
110 * it is possible to have our socket drained but still have
111 * unprocessed events. (Or, this may just be normal with
112 * X, anyway?) We do non-trivial processing of X events
113 * after the event-reading loop, in any case, so we
114 * need to allow that we may have an empty socket but non-
117 if( ! XPending( fgDisplay.pDisplay.Display ) )
124 socket = ConnectionNumber( fgDisplay.pDisplay.Display );
126 FD_SET( socket, &fdset );
127 wait.tv_sec = msec / 1000;
128 wait.tv_usec = (msec % 1000) * 1000;
129 err = select( socket+1, &fdset, NULL, NULL, &wait );
132 if( ( -1 == err ) && ( errno != EINTR ) )
133 fgWarning ( "freeglut select() error: %d", errno );
140 * Returns GLUT modifier mask for the state field of an X11 event.
142 int fgPlatformGetModifiers( int state )
146 if( state & ( ShiftMask | LockMask ) )
147 ret |= GLUT_ACTIVE_SHIFT;
148 if( state & ControlMask )
149 ret |= GLUT_ACTIVE_CTRL;
150 if( state & Mod1Mask )
151 ret |= GLUT_ACTIVE_ALT;
156 static const char* fghTypeToString( int type )
159 case KeyPress: return "KeyPress";
160 case KeyRelease: return "KeyRelease";
161 case ButtonPress: return "ButtonPress";
162 case ButtonRelease: return "ButtonRelease";
163 case MotionNotify: return "MotionNotify";
164 case EnterNotify: return "EnterNotify";
165 case LeaveNotify: return "LeaveNotify";
166 case FocusIn: return "FocusIn";
167 case FocusOut: return "FocusOut";
168 case KeymapNotify: return "KeymapNotify";
169 case Expose: return "Expose";
170 case GraphicsExpose: return "GraphicsExpose";
171 case NoExpose: return "NoExpose";
172 case VisibilityNotify: return "VisibilityNotify";
173 case CreateNotify: return "CreateNotify";
174 case DestroyNotify: return "DestroyNotify";
175 case UnmapNotify: return "UnmapNotify";
176 case MapNotify: return "MapNotify";
177 case MapRequest: return "MapRequest";
178 case ReparentNotify: return "ReparentNotify";
179 case ConfigureNotify: return "ConfigureNotify";
180 case ConfigureRequest: return "ConfigureRequest";
181 case GravityNotify: return "GravityNotify";
182 case ResizeRequest: return "ResizeRequest";
183 case CirculateNotify: return "CirculateNotify";
184 case CirculateRequest: return "CirculateRequest";
185 case PropertyNotify: return "PropertyNotify";
186 case SelectionClear: return "SelectionClear";
187 case SelectionRequest: return "SelectionRequest";
188 case SelectionNotify: return "SelectionNotify";
189 case ColormapNotify: return "ColormapNotify";
190 case ClientMessage: return "ClientMessage";
191 case MappingNotify: return "MappingNotify";
192 default: return "UNKNOWN";
196 static const char* fghBoolToString( Bool b )
198 return b == False ? "False" : "True";
201 static const char* fghNotifyHintToString( char is_hint )
204 case NotifyNormal: return "NotifyNormal";
205 case NotifyHint: return "NotifyHint";
206 default: return "UNKNOWN";
210 static const char* fghNotifyModeToString( int mode )
213 case NotifyNormal: return "NotifyNormal";
214 case NotifyGrab: return "NotifyGrab";
215 case NotifyUngrab: return "NotifyUngrab";
216 case NotifyWhileGrabbed: return "NotifyWhileGrabbed";
217 default: return "UNKNOWN";
221 static const char* fghNotifyDetailToString( int detail )
224 case NotifyAncestor: return "NotifyAncestor";
225 case NotifyVirtual: return "NotifyVirtual";
226 case NotifyInferior: return "NotifyInferior";
227 case NotifyNonlinear: return "NotifyNonlinear";
228 case NotifyNonlinearVirtual: return "NotifyNonlinearVirtual";
229 case NotifyPointer: return "NotifyPointer";
230 case NotifyPointerRoot: return "NotifyPointerRoot";
231 case NotifyDetailNone: return "NotifyDetailNone";
232 default: return "UNKNOWN";
236 static const char* fghVisibilityToString( int state ) {
238 case VisibilityUnobscured: return "VisibilityUnobscured";
239 case VisibilityPartiallyObscured: return "VisibilityPartiallyObscured";
240 case VisibilityFullyObscured: return "VisibilityFullyObscured";
241 default: return "UNKNOWN";
245 static const char* fghConfigureDetailToString( int detail )
248 case Above: return "Above";
249 case Below: return "Below";
250 case TopIf: return "TopIf";
251 case BottomIf: return "BottomIf";
252 case Opposite: return "Opposite";
253 default: return "UNKNOWN";
257 static const char* fghPlaceToString( int place )
260 case PlaceOnTop: return "PlaceOnTop";
261 case PlaceOnBottom: return "PlaceOnBottom";
262 default: return "UNKNOWN";
266 static const char* fghMappingRequestToString( int request )
269 case MappingModifier: return "MappingModifier";
270 case MappingKeyboard: return "MappingKeyboard";
271 case MappingPointer: return "MappingPointer";
272 default: return "UNKNOWN";
276 static const char* fghPropertyStateToString( int state )
279 case PropertyNewValue: return "PropertyNewValue";
280 case PropertyDelete: return "PropertyDelete";
281 default: return "UNKNOWN";
285 static const char* fghColormapStateToString( int state )
288 case ColormapUninstalled: return "ColormapUninstalled";
289 case ColormapInstalled: return "ColormapInstalled";
290 default: return "UNKNOWN";
294 static void fghPrintEvent( XEvent *event )
296 switch( event->type ) {
300 XKeyEvent *e = &event->xkey;
301 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
302 "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
303 "keycode=%u, same_screen=%s", fghTypeToString( e->type ),
304 e->window, e->root, e->subwindow, (unsigned long)e->time,
305 e->x, e->y, e->x_root, e->y_root, e->state, e->keycode,
306 fghBoolToString( e->same_screen ) );
311 case ButtonRelease: {
312 XButtonEvent *e = &event->xbutton;
313 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
314 "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
315 "button=%u, same_screen=%d", fghTypeToString( e->type ),
316 e->window, e->root, e->subwindow, (unsigned long)e->time,
317 e->x, e->y, e->x_root, e->y_root, e->state, e->button,
318 fghBoolToString( e->same_screen ) );
323 XMotionEvent *e = &event->xmotion;
324 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
325 "(x,y)=(%d,%d), (x_root,y_root)=(%d,%d), state=0x%x, "
326 "is_hint=%s, same_screen=%d", fghTypeToString( e->type ),
327 e->window, e->root, e->subwindow, (unsigned long)e->time,
328 e->x, e->y, e->x_root, e->y_root, e->state,
329 fghNotifyHintToString( e->is_hint ),
330 fghBoolToString( e->same_screen ) );
336 XCrossingEvent *e = &event->xcrossing;
337 fgWarning( "%s: window=0x%x, root=0x%x, subwindow=0x%x, time=%lu, "
338 "(x,y)=(%d,%d), mode=%s, detail=%s, same_screen=%d, "
339 "focus=%d, state=0x%x", fghTypeToString( e->type ),
340 e->window, e->root, e->subwindow, (unsigned long)e->time,
341 e->x, e->y, fghNotifyModeToString( e->mode ),
342 fghNotifyDetailToString( e->detail ), (int)e->same_screen,
343 (int)e->focus, e->state );
349 XFocusChangeEvent *e = &event->xfocus;
350 fgWarning( "%s: window=0x%x, mode=%s, detail=%s",
351 fghTypeToString( e->type ), e->window,
352 fghNotifyModeToString( e->mode ),
353 fghNotifyDetailToString( e->detail ) );
358 XKeymapEvent *e = &event->xkeymap;
359 char buf[32 * 2 + 1];
361 for ( i = 0; i < 32; i++ ) {
362 snprintf( &buf[ i * 2 ], sizeof( buf ) - i * 2,
363 "%02x", e->key_vector[ i ] );
366 fgWarning( "%s: window=0x%x, %s", fghTypeToString( e->type ), e->window,
372 XExposeEvent *e = &event->xexpose;
373 fgWarning( "%s: window=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "
374 "count=%d", fghTypeToString( e->type ), e->window, e->x,
375 e->y, e->width, e->height, e->count );
379 case GraphicsExpose: {
380 XGraphicsExposeEvent *e = &event->xgraphicsexpose;
381 fgWarning( "%s: drawable=0x%x, (x,y)=(%d,%d), (width,height)=(%d,%d), "
382 "count=%d, (major_code,minor_code)=(%d,%d)",
383 fghTypeToString( e->type ), e->drawable, e->x, e->y,
384 e->width, e->height, e->count, e->major_code,
390 XNoExposeEvent *e = &event->xnoexpose;
391 fgWarning( "%s: drawable=0x%x, (major_code,minor_code)=(%d,%d)",
392 fghTypeToString( e->type ), e->drawable, e->major_code,
397 case VisibilityNotify: {
398 XVisibilityEvent *e = &event->xvisibility;
399 fgWarning( "%s: window=0x%x, state=%s", fghTypeToString( e->type ),
400 e->window, fghVisibilityToString( e->state) );
405 XCreateWindowEvent *e = &event->xcreatewindow;
406 fgWarning( "%s: (x,y)=(%d,%d), (width,height)=(%d,%d), border_width=%d, "
407 "window=0x%x, override_redirect=%s",
408 fghTypeToString( e->type ), e->x, e->y, e->width, e->height,
409 e->border_width, e->window,
410 fghBoolToString( e->override_redirect ) );
414 case DestroyNotify: {
415 XDestroyWindowEvent *e = &event->xdestroywindow;
416 fgWarning( "%s: event=0x%x, window=0x%x",
417 fghTypeToString( e->type ), e->event, e->window );
422 XUnmapEvent *e = &event->xunmap;
423 fgWarning( "%s: event=0x%x, window=0x%x, from_configure=%s",
424 fghTypeToString( e->type ), e->event, e->window,
425 fghBoolToString( e->from_configure ) );
430 XMapEvent *e = &event->xmap;
431 fgWarning( "%s: event=0x%x, window=0x%x, override_redirect=%s",
432 fghTypeToString( e->type ), e->event, e->window,
433 fghBoolToString( e->override_redirect ) );
438 XMapRequestEvent *e = &event->xmaprequest;
439 fgWarning( "%s: parent=0x%x, window=0x%x",
440 fghTypeToString( event->type ), e->parent, e->window );
444 case ReparentNotify: {
445 XReparentEvent *e = &event->xreparent;
446 fgWarning( "%s: event=0x%x, window=0x%x, parent=0x%x, (x,y)=(%d,%d), "
447 "override_redirect=%s", fghTypeToString( e->type ),
448 e->event, e->window, e->parent, e->x, e->y,
449 fghBoolToString( e->override_redirect ) );
453 case ConfigureNotify: {
454 XConfigureEvent *e = &event->xconfigure;
455 fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d), "
456 "(width,height)=(%d,%d), border_width=%d, above=0x%x, "
457 "override_redirect=%s", fghTypeToString( e->type ), e->event,
458 e->window, e->x, e->y, e->width, e->height, e->border_width,
459 e->above, fghBoolToString( e->override_redirect ) );
463 case ConfigureRequest: {
464 XConfigureRequestEvent *e = &event->xconfigurerequest;
465 fgWarning( "%s: parent=0x%x, window=0x%x, (x,y)=(%d,%d), "
466 "(width,height)=(%d,%d), border_width=%d, above=0x%x, "
467 "detail=%s, value_mask=%lx", fghTypeToString( e->type ),
468 e->parent, e->window, e->x, e->y, e->width, e->height,
469 e->border_width, e->above,
470 fghConfigureDetailToString( e->detail ), e->value_mask );
474 case GravityNotify: {
475 XGravityEvent *e = &event->xgravity;
476 fgWarning( "%s: event=0x%x, window=0x%x, (x,y)=(%d,%d)",
477 fghTypeToString( e->type ), e->event, e->window, e->x, e->y );
481 case ResizeRequest: {
482 XResizeRequestEvent *e = &event->xresizerequest;
483 fgWarning( "%s: window=0x%x, (width,height)=(%d,%d)",
484 fghTypeToString( e->type ), e->window, e->width, e->height );
488 case CirculateNotify: {
489 XCirculateEvent *e = &event->xcirculate;
490 fgWarning( "%s: event=0x%x, window=0x%x, place=%s",
491 fghTypeToString( e->type ), e->event, e->window,
492 fghPlaceToString( e->place ) );
496 case CirculateRequest: {
497 XCirculateRequestEvent *e = &event->xcirculaterequest;
498 fgWarning( "%s: parent=0x%x, window=0x%x, place=%s",
499 fghTypeToString( e->type ), e->parent, e->window,
500 fghPlaceToString( e->place ) );
504 case PropertyNotify: {
505 XPropertyEvent *e = &event->xproperty;
506 fgWarning( "%s: window=0x%x, atom=%lu, time=%lu, state=%s",
507 fghTypeToString( e->type ), e->window,
508 (unsigned long)e->atom, (unsigned long)e->time,
509 fghPropertyStateToString( e->state ) );
513 case SelectionClear: {
514 XSelectionClearEvent *e = &event->xselectionclear;
515 fgWarning( "%s: window=0x%x, selection=%lu, time=%lu",
516 fghTypeToString( e->type ), e->window,
517 (unsigned long)e->selection, (unsigned long)e->time );
521 case SelectionRequest: {
522 XSelectionRequestEvent *e = &event->xselectionrequest;
523 fgWarning( "%s: owner=0x%x, requestor=0x%x, selection=0x%x, "
524 "target=0x%x, property=%lu, time=%lu",
525 fghTypeToString( e->type ), e->owner, e->requestor,
526 (unsigned long)e->selection, (unsigned long)e->target,
527 (unsigned long)e->property, (unsigned long)e->time );
531 case SelectionNotify: {
532 XSelectionEvent *e = &event->xselection;
533 fgWarning( "%s: requestor=0x%x, selection=0x%x, target=0x%x, "
534 "property=%lu, time=%lu", fghTypeToString( e->type ),
535 e->requestor, (unsigned long)e->selection,
536 (unsigned long)e->target, (unsigned long)e->property,
537 (unsigned long)e->time );
541 case ColormapNotify: {
542 XColormapEvent *e = &event->xcolormap;
543 fgWarning( "%s: window=0x%x, colormap=%lu, new=%s, state=%s",
544 fghTypeToString( e->type ), e->window,
545 (unsigned long)e->colormap, fghBoolToString( e->new ),
546 fghColormapStateToString( e->state ) );
550 case ClientMessage: {
551 XClientMessageEvent *e = &event->xclient;
554 char* end = buf + sizeof( buf );
556 switch( e->format ) {
558 for ( i = 0; i < 20; i++, p += 3 ) {
559 snprintf( p, end - p, " %02x", e->data.b[ i ] );
563 for ( i = 0; i < 10; i++, p += 5 ) {
564 snprintf( p, end - p, " %04x", e->data.s[ i ] );
568 for ( i = 0; i < 5; i++, p += 9 ) {
569 snprintf( p, end - p, " %08lx", e->data.l[ i ] );
574 fgWarning( "%s: window=0x%x, message_type=%lu, format=%d, data=(%s )",
575 fghTypeToString( e->type ), e->window,
576 (unsigned long)e->message_type, e->format, buf );
580 case MappingNotify: {
581 XMappingEvent *e = &event->xmapping;
582 fgWarning( "%s: window=0x%x, request=%s, first_keycode=%d, count=%d",
583 fghTypeToString( e->type ), e->window,
584 fghMappingRequestToString( e->request ), e->first_keycode,
590 fgWarning( "%s", fghTypeToString( event->type ) );
597 void fgPlatformProcessSingleEvent ( void )
602 /* This code was repeated constantly, so here it goes into a definition: */
603 #define GETWINDOW(a) \
604 window = fgWindowByHandle( event.a.window ); \
605 if( window == NULL ) \
608 #define GETMOUSE(a) \
609 window->State.MouseX = event.a.x; \
610 window->State.MouseY = event.a.y;
612 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
614 while( XPending( fgDisplay.pDisplay.Display ) )
616 XNextEvent( fgDisplay.pDisplay.Display, &event );
618 fghPrintEvent( &event );
624 if(fgIsSpaceballXEvent(&event)) {
625 fgSpaceballHandleXEvent(&event);
628 /* Destroy the window when the WM_DELETE_WINDOW message arrives */
629 if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.pDisplay.DeleteWindow )
631 GETWINDOW( xclient );
633 fgDestroyWindow ( window );
635 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
640 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
641 fgState.ExecState = GLUT_EXEC_STATE_STOP;
648 * CreateNotify causes a configure-event so that sub-windows are
649 * handled compatibly with GLUT. Otherwise, your sub-windows
650 * (in freeglut only) will not get an initial reshape event,
651 * which can break things.
653 * GLUT presumably does this because it generally tries to treat
654 * sub-windows the same as windows.
657 case ConfigureNotify:
660 if( event.type == CreateNotify ) {
661 GETWINDOW( xcreatewindow );
662 width = event.xcreatewindow.width;
663 height = event.xcreatewindow.height;
665 GETWINDOW( xconfigure );
666 width = event.xconfigure.width;
667 height = event.xconfigure.height;
670 if( ( width != window->State.pWState.OldWidth ) ||
671 ( height != window->State.pWState.OldHeight ) )
673 SFG_Window *current_window = fgStructure.CurrentWindow;
675 window->State.pWState.OldWidth = width;
676 window->State.pWState.OldHeight = height;
677 if( FETCH_WCB( *window, Reshape ) )
678 INVOKE_WCB( *window, Reshape, ( width, height ) );
681 fgSetWindow( window );
682 glViewport( 0, 0, width, height );
684 glutPostRedisplay( );
686 fgSetWindow( current_window );
693 * This is sent to confirm the XDestroyWindow call.
695 * XXX WHY is this commented out? Should we re-enable it?
697 /* fgAddToWindowDestroyList ( window ); */
702 * We are too dumb to process partial exposes...
704 * XXX Well, we could do it. However, it seems to only
705 * XXX be potentially useful for single-buffered (since
706 * XXX double-buffered does not respect viewport when we
707 * XXX do a buffer-swap).
710 if( event.xexpose.count == 0 )
712 GETWINDOW( xexpose );
713 window->State.Redisplay = GL_TRUE;
721 /* We get this when iconifying a window. */
723 INVOKE_WCB( *window, WindowStatus, ( GLUT_HIDDEN ) );
724 window->State.Visible = GL_FALSE;
729 * Have the client's keyboard knowledge updated (xlib.ps,
730 * page 206, says that's a good thing to do)
732 XRefreshKeyboardMapping( (XMappingEvent *) &event );
735 case VisibilityNotify:
738 * Sending this event, the X server can notify us that the window
739 * has just acquired one of the three possible visibility states:
740 * VisibilityUnobscured, VisibilityPartiallyObscured or
741 * VisibilityFullyObscured. Note that we DO NOT receive a
742 * VisibilityNotify event when iconifying a window, we only get an
745 GETWINDOW( xvisibility );
746 switch( event.xvisibility.state )
748 case VisibilityUnobscured:
749 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
750 window->State.Visible = GL_TRUE;
753 case VisibilityPartiallyObscured:
754 INVOKE_WCB( *window, WindowStatus,
755 ( GLUT_PARTIALLY_RETAINED ) );
756 window->State.Visible = GL_TRUE;
759 case VisibilityFullyObscured:
760 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
761 window->State.Visible = GL_FALSE;
765 fgWarning( "Unknown X visibility state: %d",
766 event.xvisibility.state );
774 GETWINDOW( xcrossing );
775 GETMOUSE( xcrossing );
776 if( ( event.type == LeaveNotify ) && window->IsMenu &&
777 window->ActiveMenu && window->ActiveMenu->IsActive )
778 fgUpdateMenuHighlight( window->ActiveMenu );
780 INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
787 GETWINDOW( xmotion );
790 if( window->ActiveMenu )
792 if( window == window->ActiveMenu->ParentWindow )
794 window->ActiveMenu->Window->State.MouseX =
795 event.xmotion.x_root - window->ActiveMenu->X;
796 window->ActiveMenu->Window->State.MouseY =
797 event.xmotion.y_root - window->ActiveMenu->Y;
800 fgUpdateMenuHighlight( window->ActiveMenu );
806 * XXX For more than 5 buttons, just check {event.xmotion.state},
807 * XXX rather than a host of bit-masks? Or maybe we need to
808 * XXX track ButtonPress/ButtonRelease events in our own
811 fgState.Modifiers = fgPlatformGetModifiers( event.xmotion.state );
812 if ( event.xmotion.state & ( Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask ) ) {
813 INVOKE_WCB( *window, Motion, ( event.xmotion.x,
816 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
819 fgState.Modifiers = INVALID_MODIFIERS;
826 GLboolean pressed = GL_TRUE;
829 if( event.type == ButtonRelease )
833 * A mouse button has been pressed or released. Traditionally,
834 * break if the window was found within the freeglut structures.
836 GETWINDOW( xbutton );
840 * An X button (at least in XFree86) is numbered from 1.
841 * A GLUT button is numbered from 0.
842 * Old GLUT passed through buttons other than just the first
843 * three, though it only gave symbolic names and official
844 * support to the first three.
846 button = event.xbutton.button - 1;
849 * Do not execute the application's mouse callback if a menu
850 * is hooked to this button. In that case an appropriate
851 * private call should be generated.
853 if( fgCheckActiveMenu( window, button, pressed,
854 event.xbutton.x_root, event.xbutton.y_root ) )
858 * Check if there is a mouse or mouse wheel callback hooked to the
861 if( ! FETCH_WCB( *window, Mouse ) &&
862 ! FETCH_WCB( *window, MouseWheel ) )
865 fgState.Modifiers = fgPlatformGetModifiers( event.xbutton.state );
867 /* Finally execute the mouse or mouse wheel callback */
868 if( ( button < glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS ) ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
869 INVOKE_WCB( *window, Mouse, ( button,
870 pressed ? GLUT_DOWN : GLUT_UP,
877 * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
878 * " 6 and 7 " " one; ...
880 * XXX This *should* be behind some variables/macros,
881 * XXX since the order and numbering isn't certain
882 * XXX See XFree86 configuration docs (even back in the
883 * XXX 3.x days, and especially with 4.x).
885 * XXX Note that {button} has already been decremented
886 * XXX in mapping from X button numbering to GLUT.
888 * XXX Should add support for partial wheel turns as Windows does -- 5/27/11
890 int wheel_number = (button - glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS )) / 2;
896 INVOKE_WCB( *window, MouseWheel, ( wheel_number,
902 fgState.Modifiers = INVALID_MODIFIERS;
909 FGCBKeyboard keyboard_cb;
910 FGCBSpecial special_cb;
915 /* Detect auto repeated keys, if configured globally or per-window */
917 if ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE )
919 if (event.type==KeyRelease)
922 * Look at X11 keystate to detect repeat mode.
923 * While X11 says the key is actually held down, we'll ignore KeyRelease/KeyPress pairs.
927 XQueryKeymap( fgDisplay.pDisplay.Display, keys ); /* Look at X11 keystate to detect repeat mode */
929 if ( event.xkey.keycode<256 ) /* XQueryKeymap is limited to 256 keycodes */
931 if ( keys[event.xkey.keycode>>3] & (1<<(event.xkey.keycode%8)) )
932 window->State.KeyRepeating = GL_TRUE;
934 window->State.KeyRepeating = GL_FALSE;
939 window->State.KeyRepeating = GL_FALSE;
941 /* Cease processing this event if it is auto repeated */
943 if (window->State.KeyRepeating)
945 if (event.type == KeyPress) window->State.KeyRepeating = GL_FALSE;
949 if( event.type == KeyPress )
951 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, Keyboard ));
952 special_cb = (FGCBSpecial) ( FETCH_WCB( *window, Special ));
956 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, KeyboardUp ));
957 special_cb = (FGCBSpecial) ( FETCH_WCB( *window, SpecialUp ));
960 /* Is there a keyboard/special callback hooked for this window? */
961 if( keyboard_cb || special_cb )
963 XComposeStatus composeStatus;
964 char asciiCode[ 32 ];
968 /* Check for the ASCII/KeySym codes associated with the event: */
969 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
970 &keySym, &composeStatus
973 /* GLUT API tells us to have two separate callbacks... */
976 /* ...one for the ASCII translateable keypresses... */
979 fgSetWindow( window );
980 fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state );
981 keyboard_cb( asciiCode[ 0 ],
982 event.xkey.x, event.xkey.y
984 fgState.Modifiers = INVALID_MODIFIERS;
992 * ...and one for all the others, which need to be
993 * translated to GLUT_KEY_Xs...
997 case XK_F1: special = GLUT_KEY_F1; break;
998 case XK_F2: special = GLUT_KEY_F2; break;
999 case XK_F3: special = GLUT_KEY_F3; break;
1000 case XK_F4: special = GLUT_KEY_F4; break;
1001 case XK_F5: special = GLUT_KEY_F5; break;
1002 case XK_F6: special = GLUT_KEY_F6; break;
1003 case XK_F7: special = GLUT_KEY_F7; break;
1004 case XK_F8: special = GLUT_KEY_F8; break;
1005 case XK_F9: special = GLUT_KEY_F9; break;
1006 case XK_F10: special = GLUT_KEY_F10; break;
1007 case XK_F11: special = GLUT_KEY_F11; break;
1008 case XK_F12: special = GLUT_KEY_F12; break;
1011 case XK_Left: special = GLUT_KEY_LEFT; break;
1013 case XK_Right: special = GLUT_KEY_RIGHT; break;
1015 case XK_Up: special = GLUT_KEY_UP; break;
1017 case XK_Down: special = GLUT_KEY_DOWN; break;
1020 case XK_Prior: special = GLUT_KEY_PAGE_UP; break;
1022 case XK_Next: special = GLUT_KEY_PAGE_DOWN; break;
1024 case XK_Home: special = GLUT_KEY_HOME; break;
1026 case XK_End: special = GLUT_KEY_END; break;
1028 case XK_Insert: special = GLUT_KEY_INSERT; break;
1030 case XK_Num_Lock : special = GLUT_KEY_NUM_LOCK; break;
1031 case XK_KP_Begin : special = GLUT_KEY_BEGIN; break;
1032 case XK_KP_Delete: special = GLUT_KEY_DELETE; break;
1034 case XK_Shift_L: special = GLUT_KEY_SHIFT_L; break;
1035 case XK_Shift_R: special = GLUT_KEY_SHIFT_R; break;
1036 case XK_Control_L: special = GLUT_KEY_CTRL_L; break;
1037 case XK_Control_R: special = GLUT_KEY_CTRL_R; break;
1038 case XK_Alt_L: special = GLUT_KEY_ALT_L; break;
1039 case XK_Alt_R: special = GLUT_KEY_ALT_R; break;
1043 * Execute the callback (if one has been specified),
1044 * given that the special code seems to be valid...
1046 if( special_cb && (special != -1) )
1048 fgSetWindow( window );
1049 fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state );
1050 special_cb( special, event.xkey.x, event.xkey.y );
1051 fgState.Modifiers = INVALID_MODIFIERS;
1058 case ReparentNotify:
1059 break; /* XXX Should disable this event */
1066 /* enter handling of Extension Events here */
1067 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1068 fgHandleExtensionEvents( &event );
1076 void fgPlatformMainLoopPreliminaryWork ( void )