Re: [AD] Using system mouse cursor

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


> > This makes me wonder.. how hard would it be to make the hardware cursor
> > be the X11 cursor?
> 
> Trivial, actually - just an extra function call. 

Patch attached. This adds a function
int select_os_cursor(int cursor)
to the API. It returns non-zero on a succesful call, and the requested 
cursor can currently be any of

MOUSE_OS_NONE - no OS cursor displayed
MOUSE_OS_ARROW - standard arrow pointer
MOUSE_OS_BUSY - busy pointer (hourglass)
MOUSE_OS_QUESTION  - question mark pointer

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...
Tested under Linux/X11. The patch contains code for Windows which is 
untested but should work (it isn't any really different from the X11 
code).
Docs to follow.

BTW, the mouse API is starting to look a bit cluttered with all these 
things that can be enabled and disabled and conflict with each other. 
Maybe some structured docs can remedy that though.

Evert
diff -ur allegro/src/CVS/Entries alleg_work/src/CVS/Entries
--- allegro/src/CVS/Entries	2004-09-25 13:50:29.000000000 +0200
+++ alleg_work/src/CVS/Entries	2004-09-25 13:36:42.000000000 +0200
@@ -69,4 +69,4 @@
 /vtable8.c/1.5/Fri Aug 27 13:51:10 2004//
 /graphics.c/1.57/Mon Aug 30 16:27:10 2004//
 /digmid.c/1.17/Fri Sep  3 09:30:22 2004//
-/mouse.c/1.25/Sat Sep 25 11:49:51 2004//
+/mouse.c/1.24/Sat Sep 25 08:35:38 2004//
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 14:41:28.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,26 @@
 
 
 
+/* select_os_cursor:
+ *  Selects the default OS cursor. Returns non-zero on succes
+ */
+int select_os_cursor(AL_CONST int cursor)
+{
+   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.
@@ -880,6 +901,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);


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