Fixed mousewheel callbacks under X11. (bug #247, github issue #66)
[freeglut] / src / x11 / fg_main_x11.c
index 897b7ee..e3e2a22 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * freeglut_main_x11.c
+ * fg_main_x11.c
  *
  * The X11-specific windows message processing methods.
  *
 
 #include <GL/freeglut.h>
 #include "../fg_internal.h"
-#ifdef HAVE_ERRNO_H
-#    include <errno.h>
-#endif
+#include <errno.h>
 #include <stdarg.h>
-#ifdef  HAVE_VFPRINTF
-#    define VFPRINTF(s,f,a) vfprintf((s),(f),(a))
-#elif defined(HAVE__DOPRNT)
-#    define VFPRINTF(s,f,a) _doprnt((f),(a),(s))
-#else
-#    define VFPRINTF(s,f,a)
-#endif
 
 
 /*
@@ -125,10 +116,8 @@ void fgPlatformSleepForEvents( fg_time_t msec )
         wait.tv_usec = (msec % 1000) * 1000;
         err = select( socket+1, &fdset, NULL, NULL, &wait );
 
-#ifdef HAVE_ERRNO_H
         if( ( -1 == err ) && ( errno != EINTR ) )
             fgWarning ( "freeglut select() error: %d", errno );
-#endif
     }
 }
 
@@ -818,11 +807,8 @@ void fgPlatformProcessSingleEvent ( void )
         case ButtonRelease:
         case ButtonPress:
         {
-            GLboolean pressed = GL_TRUE;
-            int button;
-
-            if( event.type == ButtonRelease )
-                pressed = GL_FALSE ;
+            GLboolean pressed;
+            int button, x, y;
 
             /*
              * A mouse button has been pressed or released. Traditionally,
@@ -840,59 +826,46 @@ void fgPlatformProcessSingleEvent ( void )
              */
             button = event.xbutton.button - 1;
 
+            pressed = event.type == ButtonPress ? GL_TRUE : GL_FALSE;
+            x = event.xbutton.x;
+            y = event.xbutton.y;
+
             /*
              * Do not execute the application's mouse callback if a menu
              * is hooked to this button.  In that case an appropriate
              * private call should be generated.
              */
-            if( fgCheckActiveMenu( window, button, pressed,
-                                   event.xbutton.x, event.xbutton.y ) )
+            if(fgCheckActiveMenu( window, button, pressed, x, y))
                 break;
 
             /*
              * Check if there is a mouse or mouse wheel callback hooked to the
              * window
              */
-            if( ! FETCH_WCB( *window, Mouse ) &&
-                ! FETCH_WCB( *window, MouseWheel ) )
+            if(!FETCH_WCB(*window, Mouse) && !FETCH_WCB(*window, MouseWheel))
                 break;
 
-            fgState.Modifiers = fgPlatformGetModifiers( event.xbutton.state );
+            fgState.Modifiers = fgPlatformGetModifiers(event.xbutton.state);
 
-            /* Finally execute the mouse or mouse wheel callback */
-            if( ( button < glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS ) ) || ( ! FETCH_WCB( *window, MouseWheel ) ) )
-                INVOKE_WCB( *window, Mouse, ( button,
-                                              pressed ? GLUT_DOWN : GLUT_UP,
-                                              event.xbutton.x,
-                                              event.xbutton.y )
-                );
-            else
-            {
-                /*
-                 * Map 4 and 5 to wheel zero; EVEN to +1, ODD to -1
-                 *  "  6 and 7 "    "   one; ...
-                 *
-                 * XXX This *should* be behind some variables/macros,
-                 * XXX since the order and numbering isn't certain
-                 * XXX See XFree86 configuration docs (even back in the
-                 * XXX 3.x days, and especially with 4.x).
-                 *
-                 * XXX Note that {button} has already been decremented
-                 * XXX in mapping from X button numbering to GLUT.
-                                *
-                                * XXX Should add support for partial wheel turns as Windows does -- 5/27/11
-                 */
-                int wheel_number = (button - glutDeviceGet ( GLUT_NUM_MOUSE_BUTTONS )) / 2;
-                int direction = -1;
-                if( button % 2 )
-                    direction = 1;
-
-                if( pressed )
-                    INVOKE_WCB( *window, MouseWheel, ( wheel_number,
-                                                       direction,
-                                                       event.xbutton.x,
-                                                       event.xbutton.y )
-                    );
+            /* Finally execute the mouse or mouse wheel callback.
+             * The mouse wheel is reported as buttons 4 (down) and 5 (up) by
+             * the X server. "button" has been converted to 0-based above, so
+             * that's 3 and 4 for us.
+             * If a wheel callback hasn't been registered, we simply treat them
+             * as button presses and pass them to the mouse handler. This is
+             * important for compatibility with the original GLUT.
+             */
+            if(button < 3 || button > 4 || !FETCH_WCB(*window, MouseWheel)) {
+                INVOKE_WCB(*window, Mouse, (button, pressed ? GLUT_DOWN : GLUT_UP, x, y));
+            } else {
+                if(pressed) {
+                    int dir = button & 1 ? 1 : -1;
+                    /* there's no way to know if X buttons after 5 are more
+                     * wheels/wheel axes, or regular buttons. So we'll only
+                     * ever invoke the wheel CB for wheel 0.
+                     */
+                    INVOKE_WCB(*window, MouseWheel, (0, dir, x, y));
+                }
             }
             fgState.Modifiers = INVALID_MODIFIERS;
         }
@@ -901,8 +874,10 @@ void fgPlatformProcessSingleEvent ( void )
         case KeyRelease:
         case KeyPress:
         {
-            FGCBKeyboard keyboard_cb;
-            FGCBSpecial special_cb;
+            FGCBKeyboardUC keyboard_cb;
+            FGCBSpecialUC special_cb;
+            FGCBUserData keyboard_ud;
+            FGCBUserData special_ud;
 
             GETWINDOW( xkey );
             GETMOUSE( xkey );
@@ -943,13 +918,17 @@ void fgPlatformProcessSingleEvent ( void )
 
             if( event.type == KeyPress )
             {
-                keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, Keyboard ));
-                special_cb  = (FGCBSpecial) ( FETCH_WCB( *window, Special  ));
+                keyboard_cb = (FGCBKeyboardUC)( FETCH_WCB( *window, Keyboard ));
+                special_cb  = (FGCBSpecialUC) ( FETCH_WCB( *window, Special  ));
+                keyboard_ud = FETCH_USER_DATA_WCB( *window, Keyboard );
+                special_ud  = FETCH_USER_DATA_WCB( *window, Special  );
             }
             else
             {
-                keyboard_cb = (FGCBKeyboard)( FETCH_WCB( *window, KeyboardUp ));
-                special_cb  = (FGCBSpecial) ( FETCH_WCB( *window, SpecialUp  ));
+                keyboard_cb = (FGCBKeyboardUC)( FETCH_WCB( *window, KeyboardUp ));
+                special_cb  = (FGCBSpecialUC) ( FETCH_WCB( *window, SpecialUp  ));
+                keyboard_ud = FETCH_USER_DATA_WCB( *window, KeyboardUp );
+                special_ud  = FETCH_USER_DATA_WCB( *window, SpecialUp  );
             }
 
             /* Is there a keyboard/special callback hooked for this window? */
@@ -974,7 +953,8 @@ void fgPlatformProcessSingleEvent ( void )
                         fgSetWindow( window );
                         fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state );
                         keyboard_cb( asciiCode[ 0 ],
-                                     event.xkey.x, event.xkey.y
+                                     event.xkey.x, event.xkey.y,
+                                     keyboard_ud
                         );
                         fgState.Modifiers = INVALID_MODIFIERS;
                     }
@@ -1042,7 +1022,7 @@ void fgPlatformProcessSingleEvent ( void )
                     {
                         fgSetWindow( window );
                         fgState.Modifiers = fgPlatformGetModifiers( event.xkey.state );
-                        special_cb( special, event.xkey.x, event.xkey.y );
+                        special_cb( special, event.xkey.x, event.xkey.y, special_ud );
                         fgState.Modifiers = INVALID_MODIFIERS;
                     }
                 }
@@ -1078,38 +1058,19 @@ void fgPlatformMainLoopPreliminaryWork ( void )
 }
 
 
-/* Step through the work list */
-void fgPlatformProcessWork(SFG_Window *window)
+/* deal with work list items */
+void fgPlatformInitWork(SFG_Window* window)
 {
-    unsigned int workMask = window->State.WorkMask;
-    /* Now clear it so that any callback generated by the actions below can set work again */
-    window->State.WorkMask = 0;
-
-    if (workMask&~GLUT_DISPLAY_WORK)    /* Display work is the common case, skip all the below at once */
-    {    
-    /* This is before the first display callback: call a few callbacks to inform user of window size, position, etc
-     * we know this is before the first display callback of a window as for all windows GLUT_INIT_WORK is set when
-     * they are opened, and work is done before displaying in the mainloop.
+    /* Notify windowStatus/visibility, position and size get notified on window creation with message handlers above 
+     * XXX CHECK: do the messages happen too early like on windows, so client code cannot have registered
+     * a callback yet and the message is thus never received by client?
+     * -> this is a no-op
      */
-    if (workMask & GLUT_INIT_WORK)
-    {
-        /* Notify windowStatus/visibility, position and size get notified on window creation with message handlers above 
-         * XXX CHECK: do the messages happen too early like on windows, so client code cannot have registered
-         * a callback yet and the message is thus never received by client?
-         */
-
-        /* Call init context callback */
-        INVOKE_WCB( *window, InitContext, ());
-
-        /* Lastly, check if we have a display callback, error out if not
-         * This is the right place to do it, as the redisplay will be
-         * next right after we exit this function, so there is no more
-         * opportunity for the user to register a callback for this window.
-         */
-        if (!FETCH_WCB(*window, Display))
-            fgError ( "ERROR:  No display callback registered for window %d\n", window->ID );
-    }
+     return;
+}
 
+void fgPlatformPosResZordWork(SFG_Window* window, unsigned int workMask)
+{
     if (workMask & GLUT_FULL_SCREEN_WORK)
         fgPlatformFullScreenToggle( window );
     if (workMask & GLUT_POSITION_WORK)
@@ -1123,38 +1084,28 @@ void fgPlatformProcessWork(SFG_Window *window)
         else
             fgPlatformPopWindow( window );
     }
+}
 
-    if (workMask & GLUT_VISIBILITY_WORK)
-    {
-        /* Visibility status of window gets updated in the window message handlers above 
-         * XXX: is this really the case? check
-         */
-        SFG_Window *win = window;
-        switch (window->State.DesiredVisibility)
-        {
-        case DesireHiddenState:
-            fgPlatformHideWindow( window );
-            break;
-        case DesireIconicState:
-            /* Call on top-level window */
-            while (win->Parent)
-                win = win->Parent;
-            fgPlatformIconifyWindow( win );
-            break;
-        case DesireNormalState:
-            fgPlatformShowWindow( window );
-            break;
-        }
-    }
-    }
-
-    if (workMask & GLUT_DISPLAY_WORK)
+void fgPlatformVisibilityWork(SFG_Window* window)
+{
+    /* Visibility status of window gets updated in the window message handlers above 
+     * XXX: is this really the case? check
+     */
+    SFG_Window *win = window;
+    switch (window->State.DesiredVisibility)
     {
-        if( window->State.Visible )
-            fghRedrawWindow ( window );
-
-        /* Strip out display work that might have ended up on work list now as some of the above genereates callbacks */
-        window->State.WorkMask &= ~GLUT_DISPLAY_WORK;
+    case DesireHiddenState:
+        fgPlatformHideWindow( window );
+        break;
+    case DesireIconicState:
+        /* Call on top-level window */
+        while (win->Parent)
+            win = win->Parent;
+        fgPlatformIconifyWindow( win );
+        break;
+    case DesireNormalState:
+        fgPlatformShowWindow( window );
+        break;
     }
 }