fixed closing window crash, thanks Philipp!
[freeglut] / src / x11 / fg_main_x11.c
index e72db37..8387ba8 100644 (file)
@@ -40,6 +40,8 @@
 #    define VFPRINTF(s,f,a)
 #endif
 
+#include "fg_main.h"
+
 /*
  * Try to get the maximum value allowed for ints, falling back to the minimum
  * guaranteed by ISO C99 if there is no suitable header.
@@ -55,6 +57,9 @@
 #    define MIN(a,b) (((a)<(b)) ? (a) : (b))
 #endif
 
+/* used in the event handling code to match and discard stale mouse motion events */
+static Bool match_motion(Display *dpy, XEvent *xev, XPointer arg);
+
 /*
  * TODO BEFORE THE STABLE RELEASE:
  *
@@ -68,9 +73,7 @@
  
  
 /*
- * Handle a window configuration change. When no reshape
- * callback is hooked, the viewport size is updated to
- * match the new window size.
+ * Request a window resize
  */
 void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height )
 {
@@ -295,7 +298,7 @@ static const char* fghColormapStateToString( int state )
     }
 }
 
-static void fghPrintEvent( XEvent *event )
+__fg_unused static void fghPrintEvent( XEvent *event )
 {
     switch( event->type ) {
 
@@ -625,10 +628,11 @@ void fgPlatformProcessSingleEvent ( void )
         switch( event.type )
         {
         case ClientMessage:
-            if(fgIsSpaceballXEvent(&event)) {
-                fgSpaceballHandleXEvent(&event);
-                break;
-            }
+            if (fgStructure.CurrentWindow)
+                if(fgIsSpaceballXEvent(&event)) {
+                    fgSpaceballHandleXEvent(&event);
+                    break;
+                }
             /* Destroy the window when the WM_DELETE_WINDOW message arrives */
             if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.pDisplay.DeleteWindow )
             {
@@ -788,6 +792,13 @@ void fgPlatformProcessSingleEvent ( void )
 
         case MotionNotify:
         {
+            /* if GLUT_SKIP_STALE_MOTION_EVENTS is true, then discard all but
+             * the last motion event from the queue
+             */
+            if(fgState.SkipStaleMotion) {
+                while(XCheckIfEvent(fgDisplay.pDisplay.Display, &event, match_motion, 0));
+            }
+
             GETWINDOW( xmotion );
             GETMOUSE( xmotion );
 
@@ -1077,6 +1088,11 @@ void fgPlatformProcessSingleEvent ( void )
 }
 
 
+static Bool match_motion(Display *dpy, XEvent *xev, XPointer arg)
+{
+    return xev->type == MotionNotify;
+}
+
 void fgPlatformMainLoopPreliminaryWork ( void )
 {
 }