Re: [AD] set_mouse_speed for X

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On 2007-01-31, Elias Pschernig <elias@xxxxxxxxxx> wrote:
> On Fri, 2006-12-22 at 17:12 +0100, Elias Pschernig wrote:
> > 
> > That was fast, thanks :) I still think, we don't need an arbitrary limit
> > at 10x.. if a user passes a speed of 1000, then let them live with the
> > result.
> > 
> > Also - do you think it would work fine using hundreths instead of tenth?
> > E.g. if someone has 1/3 (0.33) as default speed, it would now get 3/10
> > (0.30) - probably not noticable, but with 1/100 instead of 1/10 we would
> > be at the safe side :)
> > 
> > Hm, and set_mouse_speed really is a bad API, only way to speed up is by
> > passing the value 1. Anyway, nothing that can be done about that.
> > 
> 
> Yes, it's been some time, I know, but it has been committed now, mostly
> like that. There had to be a cap as X11 would complain otherwise, and we
> also added the missing XLOCK/XUNLOCK compared to the original patch
> (tested it all in #allegro).

Here's a patch that makes the setting only stick while the cursor is in
the Allegro window.  Once it moves out it reverts to the original X speed.
When it moves back in, the user setting is restored.

Peter
--- src/x/xmouse.c	(revision 7756)
+++ src/x/xmouse.c	(local)
@@ -22,6 +22,7 @@
 #include "xwin.h"
 #include <X11/cursorfont.h>
 
+
 /* TRUE if the requested mouse range extends beyond the regular
  * (0, 0, SCREEN_W-1, SCREEN_H-1) range. This is aimed at detecting
  * whether the user mouse coordinates are relative to the 'screen'
@@ -44,6 +45,11 @@ static int mouse_mult = -1;       /* mou
 static int mouse_div = -1;        /* mouse acceleration divisor */
 static int mouse_threshold = -1;  /* mouse acceleration threshold */
 
+static int last_xspeed = -1;      /* latest set_mouse_speed() settings */
+static int last_yspeed = -1;
+
+
+
 static int _xwin_mousedrv_init(void);
 static void _xwin_mousedrv_exit(void);
 static void _xwin_mousedrv_position(int x, int y);
@@ -52,6 +58,8 @@ static void _xwin_mousedrv_set_speed(int
 static void _xwin_mousedrv_get_mickeys(int *mickeyx, int *mickeyy);
 static int _xwin_select_system_cursor(AL_CONST int cursor);
 
+static void _xwin_set_mouse_speed(int xspeed, int yspeed);
+
 static MOUSE_DRIVER mouse_xwin =
 {
    MOUSE_XWINDOWS,
@@ -118,6 +126,9 @@ static int _xwin_mousedrv_init(void)
    num_buttons = _xwin_get_pointer_mapping(map, sizeof(map));
    num_buttons = MID(2, num_buttons, 3);
 
+   last_xspeed = -1;
+   last_yspeed = -1;
+
    XLOCK();
 
    _xwin_mouse_interrupt = _xwin_mousedrv_handler;
@@ -195,34 +206,18 @@ static void _xwin_mousedrv_set_range(int
 
 
 /* _xwin_mousedrv_set_speed:
- *  Sets the speed of the mickey-mode mouse.
- *  Each step slows down or speeds the mouse up by 0.5x.
+ *  Sets the speed of the mouse cursor.  We don't set the speed if the cursor
+ *  isn't in the window, but we remember the setting so it will be set the
+ *  next time the cursor enters the window.
  */
 static void _xwin_mousedrv_set_speed(int xspeed, int yspeed)
 {
-   int speed;
-   int hundredths;
-
-   XLOCK();
-
-   if (mouse_mult < 0)
-      XGetPointerControl(_xwin.display, &mouse_mult, &mouse_div,
-         &mouse_threshold);
-
-   speed = MAX(1, (xspeed + yspeed) / 2);
-
-   if (mouse_div == 0)
-      hundredths = mouse_mult * 100;
-   else
-      hundredths = (mouse_mult * 100 / mouse_div);
-   hundredths -= (speed - 2) * 50;
-   if (hundredths < 0)
-      hundredths = 0;
-
-   XChangePointerControl(_xwin.display, 1, 1, hundredths,
-      100, mouse_threshold);
+   if (_mouse_on) {
+      _xwin_set_mouse_speed(xspeed, yspeed);
+   }
 
-   XUNLOCK();
+   last_xspeed = xspeed;
+   last_yspeed = yspeed;
 }
 
 
@@ -282,3 +277,65 @@ static int _xwin_select_system_cursor(AL
 
    return cursor;
 }
+
+
+
+/* _xwin_set_mouse_speed:
+ *  The actual function that sets the speed of the mouse cursor.
+ *  Each step slows down or speeds the mouse up by 0.5x.
+ */
+static void _xwin_set_mouse_speed(int xspeed, int yspeed)
+{
+   int speed;
+   int hundredths;
+
+   XLOCK();
+
+   if (mouse_mult < 0)
+      XGetPointerControl(_xwin.display, &mouse_mult, &mouse_div,
+         &mouse_threshold);
+
+   speed = MAX(1, (xspeed + yspeed) / 2);
+
+   if (mouse_div == 0)
+      hundredths = mouse_mult * 100;
+   else
+      hundredths = (mouse_mult * 100 / mouse_div);
+   hundredths -= (speed - 2) * 50;
+   if (hundredths < 0)
+      hundredths = 0;
+
+   XChangePointerControl(_xwin.display, 1, 1, hundredths,
+      100, mouse_threshold);
+
+   XUNLOCK();
+}
+
+
+
+/* _xwin_mouse_leave_notify:
+ *  Reset the mouse speed to its original value when the cursor leave the
+ *  Allegro window.
+ */
+void _xwin_mouse_leave_notify(void)
+{
+   if (mouse_mult >= 0) {
+      XLOCK();
+      XChangePointerControl(_xwin.display, 1, 1, mouse_mult,
+         mouse_div, mouse_threshold);
+      XUNLOCK();
+   }
+}
+
+
+
+/* _xwin_mouse_enter_notify:
+ *  Restore the mouse speed setting when the mouse cursor re-enters the
+ *  Allegro window.
+ */
+void _xwin_mouse_enter_notify(void)
+{
+   if (last_xspeed >= 0) {
+      _xwin_set_mouse_speed(last_xspeed, last_yspeed);
+   }
+}
--- src/x/xwin.c	(revision 7756)
+++ src/x/xwin.c	(local)
@@ -2375,6 +2375,7 @@ static void _xwin_private_process_event(
       case EnterNotify:
 	 /* Mouse entered window.  */
 	 _mouse_on = TRUE;
+	 _xwin_mouse_enter_notify();
 	 mouse_savedx = event->xcrossing.x;
 	 mouse_savedy = event->xcrossing.y;
 	 mouse_was_warped = 0;
@@ -2392,6 +2393,7 @@ static void _xwin_private_process_event(
 	 _mouse_on = FALSE;
 	 if (_xwin_mouse_interrupt)
 	    (*_xwin_mouse_interrupt)(0, 0, 0, mouse_buttons);
+	 _xwin_mouse_leave_notify();
 	 break;
       case Expose:
 	 /* Request to redraw part of the window.  */
--- src/x/xwin.h	(revision 7756)
+++ src/x/xwin.h	(local)
@@ -78,5 +78,9 @@ AL_FUNC(void, _xwin_keyboard_handler, (X
 AL_FUNC(void, _xwin_get_keyboard_mapping, (void));
 AL_FUNC(void, _xwin_keyboard_focus_handler, (XFocusChangeEvent *event));
 
+/* Defined in xmouse.c. */
+AL_FUNC(void, _xwin_mouse_leave_notify, (void));
+AL_FUNC(void, _xwin_mouse_enter_notify, (void));
+
 #endif /* !__bma_xwin_h */
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/