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)
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 fg_time_t fgPlatformSystemTime ( void )
94 #ifdef CLOCK_MONOTONIC
96 clock_gettime(CLOCK_MONOTONIC, &now);
97 return now.tv_nsec/1000000 + now.tv_sec*1000;
98 #elif defined(HAVE_GETTIMEOFDAY)
100 gettimeofday( &now, NULL );
101 return now.tv_usec/1000 + now.tv_sec*1000;
106 * Does the magic required to relinquish the CPU until something interesting
110 void fgPlatformSleepForEvents( fg_time_t msec )
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-
121 if( ! XPending( fgDisplay.pDisplay.Display ) )
128 socket = ConnectionNumber( fgDisplay.pDisplay.Display );
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 );
136 if( ( -1 == err ) && ( errno != EINTR ) )
137 fgWarning ( "freeglut select() error: %d", errno );
144 * Returns GLUT modifier mask for the state field of an X11 event.
146 int fgPlatformGetModifiers( int state )
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;
160 static const char* fghTypeToString( int 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";
200 static const char* fghBoolToString( Bool b )
202 return b == False ? "False" : "True";
205 static const char* fghNotifyHintToString( char is_hint )
208 case NotifyNormal: return "NotifyNormal";
209 case NotifyHint: return "NotifyHint";
210 default: return "UNKNOWN";
214 static const char* fghNotifyModeToString( int 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";
225 static const char* fghNotifyDetailToString( int 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";
240 static const char* fghVisibilityToString( int state ) {
242 case VisibilityUnobscured: return "VisibilityUnobscured";
243 case VisibilityPartiallyObscured: return "VisibilityPartiallyObscured";
244 case VisibilityFullyObscured: return "VisibilityFullyObscured";
245 default: return "UNKNOWN";
249 static const char* fghConfigureDetailToString( int 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";
261 static const char* fghPlaceToString( int place )
264 case PlaceOnTop: return "PlaceOnTop";
265 case PlaceOnBottom: return "PlaceOnBottom";
266 default: return "UNKNOWN";
270 static const char* fghMappingRequestToString( int request )
273 case MappingModifier: return "MappingModifier";
274 case MappingKeyboard: return "MappingKeyboard";
275 case MappingPointer: return "MappingPointer";
276 default: return "UNKNOWN";
280 static const char* fghPropertyStateToString( int state )
283 case PropertyNewValue: return "PropertyNewValue";
284 case PropertyDelete: return "PropertyDelete";
285 default: return "UNKNOWN";
289 static const char* fghColormapStateToString( int state )
292 case ColormapUninstalled: return "ColormapUninstalled";
293 case ColormapInstalled: return "ColormapInstalled";
294 default: return "UNKNOWN";
298 static void fghPrintEvent( XEvent *event )
300 switch( event->type ) {
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 ) );
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 ) );
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 ) );
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 );
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 ) );
362 XKeymapEvent *e = &event->xkeymap;
363 char buf[32 * 2 + 1];
365 for ( i = 0; i < 32; i++ ) {
366 snprintf( &buf[ i * 2 ], sizeof( buf ) - i * 2,
367 "%02x", e->key_vector[ i ] );
370 fgWarning( "%s: window=0x%x, %s", fghTypeToString( e->type ), e->window,
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 );
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,
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,
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) );
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 ) );
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 );
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 ) );
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 ) );
442 XMapRequestEvent *e = &event->xmaprequest;
443 fgWarning( "%s: parent=0x%x, window=0x%x",
444 fghTypeToString( event->type ), e->parent, e->window );
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 ) );
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 ) );
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 );
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 );
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 );
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 ) );
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 ) );
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 ) );
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 );
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 );
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 );
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 ) );
554 case ClientMessage: {
555 XClientMessageEvent *e = &event->xclient;
558 char* end = buf + sizeof( buf );
560 switch( e->format ) {
562 for ( i = 0; i < 20; i++, p += 3 ) {
563 snprintf( p, end - p, " %02x", e->data.b[ i ] );
567 for ( i = 0; i < 10; i++, p += 5 ) {
568 snprintf( p, end - p, " %04x", e->data.s[ i ] );
572 for ( i = 0; i < 5; i++, p += 9 ) {
573 snprintf( p, end - p, " %08lx", e->data.l[ i ] );
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 );
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,
594 fgWarning( "%s", fghTypeToString( event->type ) );
601 void fgPlatformProcessSingleEvent ( void )
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 ) \
612 #define GETMOUSE(a) \
613 window->State.MouseX = event.a.x; \
614 window->State.MouseY = event.a.y;
616 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
618 while( XPending( fgDisplay.pDisplay.Display ) )
620 XNextEvent( fgDisplay.pDisplay.Display, &event );
622 fghPrintEvent( &event );
628 if(fgIsSpaceballXEvent(&event)) {
629 fgSpaceballHandleXEvent(&event);
632 /* Destroy the window when the WM_DELETE_WINDOW message arrives */
633 if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.pDisplay.DeleteWindow )
635 GETWINDOW( xclient );
637 fgDestroyWindow ( window );
639 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
644 else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
645 fgState.ExecState = GLUT_EXEC_STATE_STOP;
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.
657 * GLUT presumably does this because it generally tries to treat
658 * sub-windows the same as windows.
661 case ConfigureNotify:
664 if( event.type == CreateNotify ) {
665 GETWINDOW( xcreatewindow );
666 width = event.xcreatewindow.width;
667 height = event.xcreatewindow.height;
669 GETWINDOW( xconfigure );
670 width = event.xconfigure.width;
671 height = event.xconfigure.height;
674 if( ( width != window->State.pWState.OldWidth ) ||
675 ( height != window->State.pWState.OldHeight ) )
677 SFG_Window *current_window = fgStructure.CurrentWindow;
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 ) );
685 fgSetWindow( window );
686 glViewport( 0, 0, width, height );
688 glutPostRedisplay( );
690 fgSetWindow( current_window );
697 * This is sent to confirm the XDestroyWindow call.
699 * XXX WHY is this commented out? Should we re-enable it?
701 /* fgAddToWindowDestroyList ( window ); */
706 * We are too dumb to process partial exposes...
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).
714 if( event.xexpose.count == 0 )
716 GETWINDOW( xexpose );
717 window->State.Redisplay = GL_TRUE;
725 /* We get this when iconifying a window. */
727 INVOKE_WCB( *window, WindowStatus, ( GLUT_HIDDEN ) );
728 window->State.Visible = GL_FALSE;
733 * Have the client's keyboard knowledge updated (xlib.ps,
734 * page 206, says that's a good thing to do)
736 XRefreshKeyboardMapping( (XMappingEvent *) &event );
739 case VisibilityNotify:
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
749 GETWINDOW( xvisibility );
750 switch( event.xvisibility.state )
752 case VisibilityUnobscured:
753 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_RETAINED ) );
754 window->State.Visible = GL_TRUE;
757 case VisibilityPartiallyObscured:
758 INVOKE_WCB( *window, WindowStatus,
759 ( GLUT_PARTIALLY_RETAINED ) );
760 window->State.Visible = GL_TRUE;
763 case VisibilityFullyObscured:
764 INVOKE_WCB( *window, WindowStatus, ( GLUT_FULLY_COVERED ) );
765 window->State.Visible = GL_FALSE;
769 fgWarning( "Unknown X visibility state: %d",
770 event.xvisibility.state );
778 GETWINDOW( xcrossing );
779 GETMOUSE( xcrossing );
780 if( ( event.type == LeaveNotify ) && window->IsMenu &&
781 window->ActiveMenu && window->ActiveMenu->IsActive )
782 fgUpdateMenuHighlight( window->ActiveMenu );
784 INVOKE_WCB( *window, Entry, ( ( EnterNotify == event.type ) ?
791 GETWINDOW( xmotion );
794 if( window->ActiveMenu )
796 if( window == window->ActiveMenu->ParentWindow )
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;
804 fgUpdateMenuHighlight( window->ActiveMenu );
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
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,
820 INVOKE_WCB( *window, Passive, ( event.xmotion.x,
823 fgState.Modifiers = INVALID_MODIFIERS;
830 GLboolean pressed = GL_TRUE;
833 if( event.type == ButtonRelease )
837 * A mouse button has been pressed or released. Traditionally,
838 * break if the window was found within the freeglut structures.
840 GETWINDOW( xbutton );
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.
850 button = event.xbutton.button - 1;
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.
857 if( fgCheckActiveMenu( window, button, pressed,
858 event.xbutton.x_root, event.xbutton.y_root ) )
862 * Check if there is a mouse or mouse wheel callback hooked to the
865 if( ! FETCH_WCB( *window, Mouse ) &&
866 ! FETCH_WCB( *window, MouseWheel ) )
869 fgState.Modifiers = fgPlatformGetModifiers( event.xbutton.state );
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,
881 * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
882 * " 6 and 7 " " one; ...
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).
889 * XXX Note that {button} has already been decremented
890 * XXX in mapping from X button numbering to GLUT.
892 * XXX Should add support for partial wheel turns as Windows does -- 5/27/11
894 int wheel_number = (button - glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS )) / 2;
900 INVOKE_WCB( *window, MouseWheel, ( wheel_number,
906 fgState.Modifiers = INVALID_MODIFIERS;
913 FGCBKeyboard keyboard_cb;
914 FGCBSpecial special_cb;
919 /* Detect auto repeated keys, if configured globally or per-window */
921 if ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE )
923 if (event.type==KeyRelease)
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.
931 XQueryKeymap( fgDisplay.pDisplay.Display, keys ); /* Look at X11 keystate to detect repeat mode */
933 if ( event.xkey.keycode<256 ) /* XQueryKeymap is limited to 256 keycodes */
935 if ( keys[event.xkey.keycode>>3] & (1<<(event.xkey.keycode%8)) )
936 window->State.KeyRepeating = GL_TRUE;
938 window->State.KeyRepeating = GL_FALSE;
943 window->State.KeyRepeating = GL_FALSE;
945 /* Cease processing this event if it is auto repeated */
947 if (window->State.KeyRepeating)
949 if (event.type == KeyPress) window->State.KeyRepeating = GL_FALSE;
953 if( event.type == KeyPress )
955 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, Keyboard ));
956 special_cb = (FGCBSpecial) ( FETCH_WCB( *window, Special ));
960 keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, KeyboardUp ));
961 special_cb = (FGCBSpecial) ( FETCH_WCB( *window, SpecialUp ));
964 /* Is there a keyboard/special callback hooked for this window? */
965 if( keyboard_cb || special_cb )
967 XComposeStatus composeStatus;
968 char asciiCode[ 32 ];
972 /* Check for the ASCII/KeySym codes associated with the event: */
973 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode),
974 &keySym, &composeStatus
977 /* GLUT API tells us to have two separate callbacks... */
980 /* ...one for the ASCII translateable keypresses... */
983 fgSetWindow( window );
984 fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state );
985 keyboard_cb( asciiCode[ 0 ],
986 event.xkey.x, event.xkey.y
988 fgState.Modifiers = INVALID_MODIFIERS;
996 * ...and one for all the others, which need to be
997 * translated to GLUT_KEY_Xs...
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;
1015 case XK_Left: special = GLUT_KEY_LEFT; break;
1017 case XK_Right: special = GLUT_KEY_RIGHT; break;
1019 case XK_Up: special = GLUT_KEY_UP; break;
1021 case XK_Down: special = GLUT_KEY_DOWN; break;
1024 case XK_Prior: special = GLUT_KEY_PAGE_UP; break;
1026 case XK_Next: special = GLUT_KEY_PAGE_DOWN; break;
1028 case XK_Home: special = GLUT_KEY_HOME; break;
1030 case XK_End: special = GLUT_KEY_END; break;
1032 case XK_Insert: special = GLUT_KEY_INSERT; break;
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;
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;
1047 * Execute the callback (if one has been specified),
1048 * given that the special code seems to be valid...
1050 if( special_cb && (special != -1) )
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;
1062 case ReparentNotify:
1063 break; /* XXX Should disable this event */
1070 /* enter handling of Extension Events here */
1071 #ifdef HAVE_X11_EXTENSIONS_XINPUT2_H
1072 fgHandleExtensionEvents( &event );
1080 void fgPlatformMainLoopPreliminaryWork ( void )