Re: [AD] Using system mouse cursor

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


On Saturday 25 September 2004 15:22, Evert Glebbeek wrote:
> Calling show_mouse() at any time disables the os cursor again and 
switches 
> back to Allegro's normal cursor. Not that calling show_mouse() would be 
> needed if you want to use the OS cursor...

Upon closer consideration, it's better to have show_mouse() just return 
immediately if the OS cursor is in use and have the OS cursor selection 
fail if Allegro's cusom cursor is in use. This makes it very easy to use 
the OS cursor if it is available and fall back to Allegro's mouse drawing 
if it is not. See the attached patch, which makes the grabber use the 
window manager cursors by default using this new behavior.

Evert
diff -ur allegro/src/beos/bmouse.c alleg_work/src/beos/bmouse.c
--- allegro/src/beos/bmouse.c	2004-09-25 10:35:38.000000000 +0200
+++ alleg_work/src/beos/bmouse.c	2004-09-25 14:20:14.000000000 +0200
@@ -39,5 +39,6 @@
    be_mouse_set_speed,	// AL_METHOD(void, set_speed, (int xspeed, int yspeed));
    be_mouse_get_mickeys,// AL_METHOD(void, get_mickeys, (int *mickeyx, int *mickeyy));
    NULL,                // AL_METHOD(int,  analyse_data, (const char *buffer, int size));
-   NULL                 // AL_METHOD(void,  enable_hardware_cursor, (AL_CONST int mode));
+   NULL,                // AL_METHOD(void,  enable_hardware_cursor, (AL_CONST int mode));
+   NULL                 // AL_METHOD(int,  select_os_cursor, (AL_CONST int cursor));
 };
diff -ur allegro/src/dos/dmouse.c alleg_work/src/dos/dmouse.c
--- allegro/src/dos/dmouse.c	2004-09-25 10:35:38.000000000 +0200
+++ alleg_work/src/dos/dmouse.c	2004-09-25 14:19:56.000000000 +0200
@@ -96,6 +96,7 @@
    mick_set_speed,
    mick_get_mickeys,
    NULL,
+   NULL,
    NULL
 };
 
diff -ur allegro/src/linux/lmseev.c alleg_work/src/linux/lmseev.c
--- allegro/src/linux/lmseev.c	2004-09-13 08:31:11.000000000 +0200
+++ alleg_work/src/linux/lmseev.c	2004-09-25 14:19:04.000000000 +0200
@@ -681,7 +681,9 @@
    mouse_set_range,
    mouse_set_speed,
    mouse_get_mickeys,
-   analyse_data
+   analyse_data,
+   NULL,
+   NULL
 };
 
 #endif
diff -ur allegro/src/linux/lmsegpmd.c alleg_work/src/linux/lmsegpmd.c
--- allegro/src/linux/lmsegpmd.c	2003-01-23 17:22:57.000000000 +0100
+++ alleg_work/src/linux/lmsegpmd.c	2004-09-25 14:18:46.000000000 +0200
@@ -140,6 +140,8 @@
 	__al_linux_mouse_set_range,
 	__al_linux_mouse_set_speed,
 	__al_linux_mouse_get_mickeys,
-	NULL  /* analyse_data */
+	NULL,  /* analyse_data */
+        NULL,
+        NULL
 };
 
diff -ur allegro/src/linux/lmsems.c alleg_work/src/linux/lmsems.c
--- allegro/src/linux/lmsems.c	2003-01-23 17:22:57.000000000 +0100
+++ alleg_work/src/linux/lmsems.c	2004-09-25 14:18:32.000000000 +0200
@@ -223,7 +223,9 @@
 	__al_linux_mouse_set_range,
 	__al_linux_mouse_set_speed,
 	__al_linux_mouse_get_mickeys,
-	analyse_data
+	analyse_data,
+        NULL,
+        NULL
 };
 
 MOUSE_DRIVER mousedrv_linux_ims =
diff -ur allegro/src/linux/lmseps2.c alleg_work/src/linux/lmseps2.c
--- allegro/src/linux/lmseps2.c	2003-03-14 06:05:10.000000000 +0100
+++ alleg_work/src/linux/lmseps2.c	2004-09-25 14:18:18.000000000 +0200
@@ -240,7 +240,9 @@
 	__al_linux_mouse_set_range,
 	__al_linux_mouse_set_speed,
 	__al_linux_mouse_get_mickeys,
-	analyse_data
+	analyse_data,
+        NULL,
+        NULL
 };
 
 MOUSE_DRIVER mousedrv_linux_ips2 =
@@ -257,5 +259,7 @@
 	__al_linux_mouse_set_range,
 	__al_linux_mouse_set_speed,
 	__al_linux_mouse_get_mickeys,
-	analyse_data
+	analyse_data,
+        NULL,
+        NULL
 };
diff -ur allegro/src/mac/madb.c alleg_work/src/mac/madb.c
--- allegro/src/mac/madb.c	2001-11-06 18:16:40.000000000 +0100
+++ alleg_work/src/mac/madb.c	2004-09-25 14:17:35.000000000 +0200
@@ -90,6 +90,7 @@
       mouse_adb_set_range,
       NULL,
       mouse_adb_get_mickeys,
+      NULL,
       NULL
 };
 
diff -ur allegro/src/mac/msys.c alleg_work/src/mac/msys.c
--- allegro/src/mac/msys.c	2004-07-02 18:18:24.000000000 +0200
+++ alleg_work/src/mac/msys.c	2004-09-25 14:17:23.000000000 +0200
@@ -122,6 +122,7 @@
    NULL,
    NULL,
    NULL,
+   NULL,
    NULL
 };
 
diff -ur allegro/src/macosx/qzmouse.m alleg_work/src/macosx/qzmouse.m
--- allegro/src/macosx/qzmouse.m	2004-09-25 10:35:38.000000000 +0200
+++ alleg_work/src/macosx/qzmouse.m	2004-09-25 14:17:11.000000000 +0200
@@ -47,7 +47,8 @@
    NULL,       // AL_METHOD(void, set_speed, (int xspeed, int yspeed));
    osx_mouse_get_mickeys,
    NULL,       // AL_METHOD(int,  analyse_data, (AL_CONST char *buffer, int size));
-   NULL        // AL_METHOD(void,  enable_hardware_cursor, (AL_CONST int mode));
+   NULL,       // AL_METHOD(void,  enable_hardware_cursor, (AL_CONST int mode));
+   NULL        // AL_METHOD(int,  select_os_cursor, (AL_CONST int cursor));
 };
 
 
diff -ur allegro/src/mouse.c alleg_work/src/mouse.c
--- allegro/src/mouse.c	2004-09-25 13:49:51.000000000 +0200
+++ alleg_work/src/mouse.c	2004-09-25 15:38:56.000000000 +0200
@@ -101,6 +101,7 @@
 
 BITMAP *_mouse_screen = NULL;          /* where to draw the pointer */
 
+static int use_os_cursor = FALSE;      /* Use native cursor? */
 static int got_hw_cursor = FALSE;      /* hardware pointer available? */
 static int hw_cursor_dirty = FALSE;    /* need to set a new pointer? */
 
@@ -390,6 +391,30 @@
 
 
 
+/* select_os_cursor:
+ *  Selects the default OS cursor. Returns non-zero on succes
+ *  This will fail if Allegro is already drawing its own cursor.
+ */
+int select_os_cursor(AL_CONST int cursor)
+{
+   if (_mouse_screen)
+      return 0;
+      
+   if (mouse_driver && mouse_driver->select_os_cursor) {
+      use_os_cursor = mouse_driver->select_os_cursor(cursor);
+   } else {
+      use_os_cursor = 0;
+   }
+   
+   if (use_os_cursor)
+      gfx_capabilities |= GFX_HW_CURSOR;
+   
+   return use_os_cursor;
+}
+
+
+
+
 /* set_mouse_sprite:
  *  Sets the sprite to be used for the mouse pointer. If the sprite is
  *  NULL, restores the default arrow.
@@ -463,13 +488,11 @@
  *  module is active. The mouse pointer will be drawn onto the bitmap bmp, 
  *  which should normally be the hardware screen. To turn off the mouse 
  *  pointer, which you must do before you draw anything onto the screen, call 
- *  show_mouse(NULL). If you forget to turn off the mouse pointer when 
- *  drawing something, the SVGA bank switching code will become confused and 
- *  will produce garbage all over the screen.
+ *  show_mouse(NULL).
  */
 void show_mouse(BITMAP *bmp)
 {
-   if (!mouse_driver)
+   if (!mouse_driver || use_os_cursor)
       return;
 
    remove_int(mouse_move);
@@ -880,6 +903,7 @@
    LOCK_VARIABLE(mtemp);
    LOCK_VARIABLE(mouse_polled);
    LOCK_VARIABLE(mouse_semaphore);
+   LOCK_VARIABLE(use_os_cursor);
    LOCK_FUNCTION(draw_mouse_doublebuffer);
    LOCK_FUNCTION(draw_mouse);
    LOCK_FUNCTION(update_mouse);
diff -ur allegro/src/qnx/qmouse.c alleg_work/src/qnx/qmouse.c
--- allegro/src/qnx/qmouse.c	2004-09-25 10:35:38.000000000 +0200
+++ alleg_work/src/qnx/qmouse.c	2004-09-25 14:16:56.000000000 +0200
@@ -49,7 +49,8 @@
    NULL,       // AL_METHOD(void, set_speed, (int xspeed, int yspeed));
    qnx_mouse_get_mickeys,
    NULL,       // AL_METHOD(int,  analyse_data, (AL_CONST char *buffer, int size));
-   NULL        // AL_METHOD(void,  enable_hardware_cursor, (AL_CONST int mode));
+   NULL,       // AL_METHOD(void,  enable_hardware_cursor, (AL_CONST int mode));
+   NULL        // AL_METHOD(int,  select_os_cursor, (AL_CONST int cursor));
 };
 
 
diff -ur allegro/src/win/wddraw.c alleg_work/src/win/wddraw.c
--- allegro/src/win/wddraw.c	2004-09-25 10:24:51.000000000 +0200
+++ alleg_work/src/win/wddraw.c	2004-09-25 15:12:54.000000000 +0200
@@ -384,6 +384,8 @@
    HBITMAP hOldXorMaskBitmap;
 
    if (hcursor) {
+      if (_win_hcursor == hcursor)
+         _win_hcursor = NULL;
       DestroyIcon(hcursor);
       hcursor = NULL;
    }
diff -ur allegro/src/win/wmouse.c alleg_work/src/win/wmouse.c
--- allegro/src/win/wmouse.c	2004-09-25 10:35:38.000000000 +0200
+++ alleg_work/src/win/wmouse.c	2004-09-25 15:11:21.000000000 +0200
@@ -46,7 +46,7 @@
 static void mouse_directx_set_range(int x1, int y1, int x2, int y2);
 static void mouse_directx_set_speed(int xspeed, int yspeed);
 static void mouse_directx_get_mickeys(int *mickeyx, int *mickeyy);
-
+static int mouse_directx_select_os_cursor(AL_CONST int cursor);
 
 MOUSE_DRIVER mouse_directx =
 {
@@ -63,7 +63,8 @@
    mouse_directx_set_speed,
    mouse_directx_get_mickeys,
    NULL,                       // AL_METHOD(int, analyse_data, (AL_CONST char *buffer, int size));
-   NULL                        // AL_METHOD(void,  enable_hardware_cursor, (AL_CONST int mode));
+   NULL,                       // AL_METHOD(void,  enable_hardware_cursor, (AL_CONST int mode));
+   mouse_directx_select_os_cursor
 };
 
 
@@ -748,3 +749,32 @@
    *mickeyy = temp_y;
 }
 
+
+
+/* mouse_directx_select_os_cursor:
+ *  Select an OS native cursor 
+ */
+static int mouse_directx_select_os_cursor (AL_CONST int cursor)
+{
+   HCURSOR wc;
+   
+   switch(cursor) {
+      case MOUSE_OS_ARROW:
+         wc = LoadCursor(NULL, IDC_ARROW);
+         break;
+      case MOUSE_OS_BUSY:
+         wc = LoadCursor(NULL, IDC_WAIT);
+         break;
+      case MOUSE_OS_QUESTION:
+         wc = LoadCursor(NULL, IDC_HELP);
+         break;
+      default:
+         return 0;
+   }
+
+   _win_hcursor = wc;
+   SetCursor(_win_hcursor);
+   PostMessage(allegro_wnd, WM_MOUSEMOVE, 0, 0);
+   
+   return cursor;
+}
diff -ur allegro/src/x/xmouse.c alleg_work/src/x/xmouse.c
--- allegro/src/x/xmouse.c	2004-09-25 10:35:38.000000000 +0200
+++ alleg_work/src/x/xmouse.c	2004-09-25 14:56:12.000000000 +0200
@@ -20,7 +20,7 @@
 #include "allegro/internal/aintern.h"
 #include "allegro/platform/aintunix.h"
 #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
@@ -48,7 +48,7 @@
 static void _xwin_mousedrv_set_range(int x1, int y1, int x2, int y2);
 static void _xwin_mousedrv_set_speed(int xspeed, int yspeed);
 static void _xwin_mousedrv_get_mickeys(int *mickeyx, int *mickeyy);
-
+static int _xwin_select_os_cursor(AL_CONST int cursor);
 
 static MOUSE_DRIVER mouse_xwin =
 {
@@ -65,7 +65,8 @@
    _xwin_mousedrv_set_speed,
    _xwin_mousedrv_get_mickeys,
    NULL,
-   _xwin_enable_hardware_cursor
+   _xwin_enable_hardware_cursor,
+   _xwin_select_os_cursor
 };
 
 
@@ -214,3 +215,34 @@
    _xwin_set_warped_mouse_mode(TRUE);
 }
 
+
+
+/* _xwin_select_os_cursor:
+ *  Select an OS native cursor 
+ */
+static int _xwin_select_os_cursor(AL_CONST int cursor)
+{
+   switch(cursor) {
+      case MOUSE_OS_ARROW:
+         _xwin.cursor_shape = XC_left_ptr;
+         break;
+      case MOUSE_OS_BUSY:
+         _xwin.cursor_shape = XC_watch;
+         break;
+      case MOUSE_OS_QUESTION:
+         _xwin.cursor_shape = XC_question_arrow;
+         break;
+      default:
+         return 0;
+   }
+   
+   if (_xwin.cursor != None) {
+      XUndefineCursor(_xwin.display, _xwin.window);
+      XFreeCursor(_xwin.display, _xwin.cursor);
+   }   
+
+   _xwin.cursor = XCreateFontCursor(_xwin.display, _xwin.cursor_shape);
+   XDefineCursor(_xwin.display, _xwin.window, _xwin.cursor);
+   
+   return cursor;
+}
diff -ur allegro/include/allegro/mouse.h alleg_work/include/allegro/mouse.h
--- allegro/include/allegro/mouse.h	2004-09-25 10:35:38.000000000 +0200
+++ alleg_work/include/allegro/mouse.h	2004-09-25 14:53:47.000000000 +0200
@@ -44,7 +44,8 @@
    AL_METHOD(void, set_speed, (int xspeed, int yspeed));
    AL_METHOD(void, get_mickeys, (int *mickeyx, int *mickeyy));
    AL_METHOD(int,  analyse_data, (AL_CONST char *buffer, int size));
-   AL_METHOD(void,  enable_hardware_cursor, (AL_CONST int mode));
+   AL_METHOD(void, enable_hardware_cursor, (AL_CONST int mode));
+   AL_METHOD(int,  select_os_cursor, (AL_CONST int cursor));
 } MOUSE_DRIVER;
 
 
@@ -61,6 +62,14 @@
 AL_FUNC(void, enable_hardware_cursor, (void));
 AL_FUNC(void, disable_hardware_cursor, (void));
 
+AL_FUNC(int,  select_os_cursor, (AL_CONST int cursor));
+
+/* OS cursors */
+#define MOUSE_OS_NONE      0
+#define MOUSE_OS_ARROW     1
+#define MOUSE_OS_BUSY      2
+#define MOUSE_OS_QUESTION  3
+
 AL_VAR(struct BITMAP *, mouse_sprite);
 AL_VAR(int, mouse_x_focus);
 AL_VAR(int, mouse_y_focus);
diff -ur allegro/tools/grabber.c alleg_work/tools/grabber.c
--- allegro/tools/grabber.c	2004-07-04 14:02:33.000000000 +0200
+++ alleg_work/tools/grabber.c	2004-09-25 15:42:02.000000000 +0200
@@ -881,11 +881,13 @@
 static void set_busy_mouse(int busy)
 {
    if (busy) {
-      set_mouse_sprite(my_busy_pointer);
+      if (!select_os_cursor(MOUSE_OS_BUSY))
+         set_mouse_sprite(my_busy_pointer);
       busy_mouse = TRUE;
    }
    else {
-      set_mouse_sprite(my_mouse_pointer);
+      if (!select_os_cursor(MOUSE_OS_ARROW))
+         set_mouse_sprite(my_mouse_pointer);
       busy_mouse = FALSE;
    }
 }
@@ -3488,11 +3490,12 @@
    vsprintf(buf, fmt, args);
    va_end(args);
 
-   set_mouse_sprite(my_mouse_pointer);
+   if (!select_os_cursor(MOUSE_OS_ARROW))
+      set_mouse_sprite(my_mouse_pointer);
 
    alert(buf, NULL, NULL, "Oh dear", NULL, 13, 0);
 
-   if (busy_mouse)
+   if (busy_mouse && !select_os_cursor(MOUSE_OS_BUSY))
       set_mouse_sprite(my_busy_pointer);
 }
 
@@ -3511,11 +3514,12 @@
 
    strcat(buf, "?");
 
-   set_mouse_sprite(my_mouse_pointer);
+   if (!select_os_cursor(MOUSE_OS_ARROW))
+      set_mouse_sprite(my_mouse_pointer);
 
    ret = alert(buf, NULL, NULL, "Yes", "Cancel", 'y', 27);
 
-   if (busy_mouse)
+   if (busy_mouse && !select_os_cursor(MOUSE_OS_BUSY))
       set_mouse_sprite(my_busy_pointer);
 
    if (ret == 1)


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