Skip to contentSkip to conversation

Release mouse at window edges

PATCH In review [email protected] 4 participants 8 messages Feb 26, 2005 – Mar 2, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Release mouse at window edges

Hello,
the attached patch makes Qemu ungrab the mouse cursor when the guest mouse cursor hits the window edges. It also makes qemu automatically grab the mouse when the host mouse cursor enters the window.

The patch uses the hardware cursor position (in cirrus_vga.c); this means that it only works when the guest system uses a hardware cursor and when Cirrus VGA is used.

For Linux guest (tested with GamesKnoppix), it works when enabling "hw_cursor" in XF86Config-4 (in "Device" setion for "cirrus" driver). For Win98se, it works when setting display hardware acceleration to maximum and using the standard b/w, non-animated cursor set.
For Win2k Professional, it requires a newer Cirrus driver (old standard Win2k driver for "GD5446 compatible" is from 1999, new driver from ISDCorp is from 2000). It can be found
at
<http://www.hofnet.com/fire/browse.asp?target=%5CCIRRUS%20LOGIC%5CCL-GD5446%5CDRIVERS%5CWIN2000>.
Besides the new driver, you have to do the same as under Win98, and you must disable mouse shadow.

The patch applies cleanly to qemu-snapshot-2005-02-25_23.tar.bz2.

Oliver Gerlich

Signature · 2 lines
Lassen Sie Ihren Gedanken freien Lauf... z.B. per FreeSMS
GMX bietet bis zu 100 FreeSMS/Monat: http://www.gmx.net/de/go/mail
hw/cirrus_vga.c +15 -0
diff -Naur qemu-snapshot-2005-02-10_23/hw/cirrus_vga.c qemu-snapshot-2005-02-10_23.patched/hw/cirrus_vga.c
@@ -1172,6 +1172,13 @@
     case 0xf0:			// Graphics Cursor X
 	s->sr[0x10] = reg_value;
 	s->hw_cursor_x = (reg_value << 3) | (reg_index >> 5);
+#ifdef DEBUG_CIRRUS
+	if (s->hw_cursor_x < 10 || s->hw_cursor_x > s->last_scr_width - 10)
+		printf("hw_cursor_x= %d; last_cursor_x= %d\n", s->hw_cursor_x, s->last_hw_cursor_x);
+#endif
+	if ( (s->hw_cursor_x <= 1 || s->hw_cursor_x >= s->last_scr_width - 12) &&
+	    abs(s->hw_cursor_x - s->last_hw_cursor_x) > 4 )
+		s->ds->cursor_release(s->ds, s->hw_cursor_x, s->hw_cursor_y);
 	break;
     case 0x11:
     case 0x31:
@@ -1183,6 +1190,13 @@
     case 0xf1:			// Graphics Cursor Y
 	s->sr[0x11] = reg_value;
 	s->hw_cursor_y = (reg_value << 3) | (reg_index >> 5);
+#ifdef DEBUG_CIRRUS
+	if (s->hw_cursor_y < 10 || s->hw_cursor_y > s->last_scr_height - 10)
+		printf("hw_cursor_y= %d; last_cursor_y= %d\n", s->hw_cursor_y, s->last_hw_cursor_y);
+#endif
+	if ( (s->hw_cursor_y <= 1 || s->hw_cursor_y >= s->last_scr_height - 12) &&
+	    abs(s->hw_cursor_y - s->last_hw_cursor_y) > 4 )
+		s->ds->cursor_release(s->ds, s->hw_cursor_x, s->hw_cursor_y);
 	break;
     case 0x07:			// Extended Sequencer Mode
     case 0x08:			// EEPROM Control
@@ -2085,6 +2099,7 @@
         invalidate_cursor1(s);
         
         s->last_hw_cursor_size = size;
+        s->ds->hw_cursor_size = size;
         s->last_hw_cursor_x = s->hw_cursor_x;
         s->last_hw_cursor_y = s->hw_cursor_y;
         /* compute the real cursor min and max y */
sdl.c +18 -4
diff -Naur qemu-snapshot-2005-02-10_23/sdl.c qemu-snapshot-2005-02-10_23.patched/sdl.c
@@ -308,6 +308,17 @@
     vga_update_display();
 }
 
+void sdl_cursor_release(struct DisplayState *s, int x, int y)
+{
+	//printf("releasing cursor at %d/%d\n", x, y);
+	if (gui_grab && !gui_fullscreen && is_active_console(vga_console) &&
+	    SDL_GetModState() == KMOD_NONE && !SDL_GetMouseState(NULL, NULL))
+	{
+		sdl_grab_end();
+		SDL_WarpMouse(x, y);
+	}
+}
+
 static void sdl_refresh(DisplayState *ds)
 {
     SDL_Event ev1, *ev = &ev1;
@@ -446,10 +457,12 @@
             }
             break;
         case SDL_ACTIVEEVENT:
-            if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0 &&
-                !gui_fullscreen_initial_grab) {
-                sdl_grab_end();
-            }
+	    if (ev->active.state == SDL_APPMOUSEFOCUS && ev->active.gain == 1 &&
+		!gui_grab && !gui_fullscreen && is_active_console(vga_console) &&
+		ds->hw_cursor_size)
+	    {
+	    	sdl_grab_start();
+	    }
             break;
         default:
             break;
@@ -491,6 +504,7 @@
     ds->dpy_update = sdl_update;
     ds->dpy_resize = sdl_resize;
     ds->dpy_refresh = sdl_refresh;
+    ds->cursor_release = sdl_cursor_release;
 
     sdl_resize(ds, 640, 400);
     sdl_update_caption();
vl.h +2 -0
diff -Naur qemu-snapshot-2005-02-10_23/vl.h qemu-snapshot-2005-02-10_23.patched/vl.h
@@ -542,9 +542,11 @@
     int depth;
     int width;
     int height;
+    int hw_cursor_size;
     void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h);
     void (*dpy_resize)(struct DisplayState *s, int w, int h);
     void (*dpy_refresh)(struct DisplayState *s);
+    void (*cursor_release)(struct DisplayState *s, int x, int y);
 };
 
 static inline void dpy_update(DisplayState *s, int x, int y, int w, int h)
#1 lore raw
Quoted 5 lines · [email protected] wrote:

Hello,
the attached patch makes Qemu ungrab the mouse cursor when the guest mouse cursor hits the window edges. It also makes qemu automatically grab the mouse when the host mouse cursor enters the window.

This patch just causes qemu to abort on startup for me.

bklaptop:~>strace -o log qemu -hda /tracks/qemu.skm.img.4 -m 256 -localtime Connected to host network interface: tun0
kill -9 stale dhcpd server
Could not open SDL display

What version of SDL are you using?
I'm using Debian testing with libsdl-1.2.7+1.2.8 (which appears to be SDL CVS from October 2004).

Regards,
Brad

Signature · 3 lines
"Human beings, who are almost unique in having the ability
to learn from the experience of others, are also remarkable
for their apparent disinclination to do so." -- Douglas Adams
#2 lore raw
Quoted 10 lines · Brad Campbell
Quoted 5 lines · [email protected] wrote:

Hello,
the attached patch makes Qemu ungrab the mouse cursor when the guest mouse cursor hits the window edges. It also makes qemu automatically grab the mouse when the host mouse cursor enters the window.

This patch just causes qemu to abort on startup for me.

I stand corrected. The patch makes some changes to vl.h which do not cause the source files that depend on it to be re-built. A make clean ; configure ; make solved it.

The recommended graphics drivers for 2k can be shoehorned into XP-SP1a. Just make sure you select 16 bit mode before you install the drivers, as they just puke in 24 bit mode and garble the display such as to make it pretty hard to actually do anything to reconfigure it after the fact!

The mouse positioning has its fair share of quirks, but once the right drivers are installed it works as designed. Good job!

Regards,
Brad

Signature · 3 lines
"Human beings, who are almost unique in having the ability
to learn from the experience of others, are also remarkable
for their apparent disinclination to do so." -- Douglas Adams
#3 lore raw
In reply to [email protected]

The idea is interesting. Does it cause regressions in the normal mode ? If so, an option to enable it may be good. I think it is a problem that your patch is dependent on the cursor size.

Fabrice.

Quoted 24 lines · [email protected] wrote:

Hello,
the attached patch makes Qemu ungrab the mouse cursor when the guest mouse cursor hits the window edges. It also makes qemu automatically grab the mouse when the host mouse cursor enters the window.

The patch uses the hardware cursor position (in cirrus_vga.c); this means that it only works when the guest system uses a hardware cursor and when Cirrus VGA is used.

For Linux guest (tested with GamesKnoppix), it works when enabling "hw_cursor" in XF86Config-4 (in "Device" setion for "cirrus" driver). For Win98se, it works when setting display hardware acceleration to maximum and using the standard b/w, non-animated cursor set.
For Win2k Professional, it requires a newer Cirrus driver (old standard Win2k driver for "GD5446 compatible" is from 1999, new driver from ISDCorp is from 2000). It can be found
at
<http://www.hofnet.com/fire/browse.asp?target=%5CCIRRUS%20LOGIC%5CCL-GD5446%5CDRIVERS%5CWIN2000>.
Besides the new driver, you have to do the same as under Win98, and you must disable mouse shadow.

The patch applies cleanly to qemu-snapshot-2005-02-25_23.tar.bz2.

Oliver Gerlich

#4 lore raw
Quoted 2 lines · Fabrice Bellard

The idea is interesting. Does it cause regressions in the normal mode ? If so, an option to enable it may be good. I think it is a problem that your patch is dependent on the cursor size.

This is a good point. I have had to revert it for WinXP as every time the cursor changes shape (and it does all the time as you move over stuff in Office2003) it releases the grab.

In addition I have had all manner of painting and rendering issues with the IDG display driver and have had to revert to the built in driver to get some software to paint properly.

Nice idea, might need a little work still.

Brad

Signature · 3 lines
"Human beings, who are almost unique in having the ability
to learn from the experience of others, are also remarkable
for their apparent disinclination to do so." -- Douglas Adams
#5 lore raw
Quoted 5 lines · Brad Campbell
Quoted 2 lines · Fabrice Bellard

The idea is interesting. Does it cause regressions in the normal mode ? If so, an option to enable it may be good. I think it is a problem that your patch is dependent on the cursor size.

Quoted 7 lines

This is a good point. I have had to revert it for WinXP as every time the cursor changes shape (and it does all the time as you move over stuff in Office2003) it releases the grab.

In addition I have had all manner of painting and rendering issues with the IDG display driver and have had to revert to the built in driver to get some software to paint properly.

Nice idea, might need a little work still.

Brad

An extra switch would be good, indeed. Currently, grabbing/ungrabbing is disabled if no hw cursor is used, but it's not reliable enough (for example, Linux initializes hw cursor at framebuffer initialization, although no cursor is visible).

The problem with cursor size appears differently on different systems. On Win98, hw_cursor_x goes from 1 to 1023 (in 1024x768), while on Win2k, it goes from 0 to 1013... The patch circumvents this problem with an ugly hack, but I have no idea how to get around this cleanly.

Brad: your problems with Office might be related to another bug I discovered (Internet Explorer 5 "disables" hw cursor by moving the cursor to 2047/2047 which ungrabs the cursor as soon as it enters an IE window). I'll post an improved patch tonight, with this and other bugs fixed.

BTW. is there a "make realclean" (or similar) which also deletes qemu-doc.html, manpages, kqemu temp files etc, so that I can easily diff my patched snapshot to an unpatched one?

Oliver Gerlich

#6 lore raw
Quoted 3 lines

BTW. is there a "make realclean" (or similar) which also deletes qemu-doc.html, manpages, kqemu temp files etc, so that I can easily diff my patched snapshot to an unpatched one?

I think "make distclean" should do it...

Thanks,
Hetz

#7 lore raw
In reply to Oliver Gerlich
Quoted 6 lines

Brad: your problems with Office might be related to another bug I discovered (Internet Explorer 5 "disables" hw cursor by moving the cursor to 2047/2047 which ungrabs the cursor as soon as it enters an IE window). I'll post an improved patch tonight, with this and other bugs fixed.

Here it is. Solves the problem with IE (and maybe with Office as well), and generally smoothes (un)grabbing. Still found no way to move guest mouse cursor on grab, but now at least it stays where it was at last ungrab. There is also now a command line switch (-autograb) which switches autograb/ungrab on.

Oliver Gerlich

Signature · 2 lines
DSL Komplett von GMX +++ Supergünstig und stressfrei einsteigen!
AKTION "Kein Einrichtungspreis" nutzen: http://www.gmx.net/de/go/dsl
hw/cirrus_vga.c +25 -0
diff -ru qemu-snapshot-2005-02-10_23/hw/cirrus_vga.c qemu-snapshot-2005-02-10_23.patched/hw/cirrus_vga.c
@@ -1172,6 +1172,18 @@
     case 0xf0:			// Graphics Cursor X
 	s->sr[0x10] = reg_value;
 	s->hw_cursor_x = (reg_value << 3) | (reg_index >> 5);
+#ifdef DEBUG_CIRRUS
+	if (s->hw_cursor_x < 10 || s->hw_cursor_x > s->last_scr_width - 20)
+		printf("1; hw_cursor_x= %d; last_cursor_x= %d\n", s->hw_cursor_x, s->last_hw_cursor_x);
+#endif
+	if ( (s->hw_cursor_x <= 1 && s->hw_cursor_x >= 0 && s->last_hw_cursor_x - (int) s->hw_cursor_x > 4))
+	{
+		s->ds->cursor_release(s->ds, 0, s->hw_cursor_y);
+	}
+	else if (s->hw_cursor_x >= s->last_scr_width - 12 && s->hw_cursor_x <= s->last_scr_width + 1 && (int) s->hw_cursor_x - s->last_hw_cursor_x > 4)
+	{
+		s->ds->cursor_release(s->ds, s->last_scr_width + 1, s->hw_cursor_y);
+	}
 	break;
     case 0x11:
     case 0x31:
@@ -1183,6 +1195,18 @@
     case 0xf1:			// Graphics Cursor Y
 	s->sr[0x11] = reg_value;
 	s->hw_cursor_y = (reg_value << 3) | (reg_index >> 5);
+#ifdef DEBUG_CIRRUS
+	if (s->hw_cursor_y < 10 || s->hw_cursor_y > s->last_scr_height - 20)
+		printf("4; hw_cursor_y= %d; last_cursor_y= %d\n", s->hw_cursor_y, s->last_hw_cursor_y);
+#endif
+	if (s->hw_cursor_y <= 1 && s->hw_cursor_y >= 0 && s->last_hw_cursor_y - (int) s->hw_cursor_y > 4)
+	{
+		s->ds->cursor_release(s->ds, s->hw_cursor_x, 0);
+	}
+	else if (s->hw_cursor_y >= s->last_scr_height - 12 && s->hw_cursor_y <= s->last_scr_height + 1 && (int) s->hw_cursor_y - s->last_hw_cursor_y > 4)
+	{
+		s->ds->cursor_release(s->ds, s->hw_cursor_x, s->last_scr_height + 1);
+	}
 	break;
     case 0x07:			// Extended Sequencer Mode
     case 0x08:			// EEPROM Control
@@ -2085,6 +2109,7 @@
         invalidate_cursor1(s);
         
         s->last_hw_cursor_size = size;
+        s->ds->hw_cursor_size = size;
         s->last_hw_cursor_x = s->hw_cursor_x;
         s->last_hw_cursor_y = s->hw_cursor_y;
         /* compute the real cursor min and max y */
sdl.c +28 -1
diff -ru qemu-snapshot-2005-02-10_23/sdl.c qemu-snapshot-2005-02-10_23.patched/sdl.c
@@ -39,6 +39,7 @@
 static int gui_fullscreen_initial_grab;
 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
 static uint8_t modifiers_state[256];
+static int ignoreMouseEvent = 0;
 
 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
 {
@@ -283,6 +284,12 @@
 {
     int dx, dy, state, buttons;
     state = SDL_GetRelativeMouseState(&dx, &dy);
+    if (ignoreMouseEvent)
+    {
+    	ignoreMouseEvent = 0;
+	return;
+    }
+    
     buttons = 0;
     if (state & SDL_BUTTON(SDL_BUTTON_LEFT))
         buttons |= MOUSE_EVENT_LBUTTON;
@@ -308,6 +315,18 @@
     vga_update_display();
 }
 
+void sdl_cursor_release(struct DisplayState *s, int x, int y)
+{
+	//printf("releasing cursor at %d/%d; grab=%d; fullscreen=%d; vga_console=%d; modstate=%d; mousestate=%d\n",
+	//	x, y, gui_grab, gui_fullscreen, is_active_console(vga_console), SDL_GetModState(), SDL_GetMouseState(NULL, NULL));
+	if (mouse_autograb && gui_grab && !gui_fullscreen && is_active_console(vga_console) &&
+	    SDL_GetModState() | KMOD_NUM == KMOD_NUM && !SDL_GetMouseState(NULL, NULL))
+	{
+		sdl_grab_end();
+		SDL_WarpMouse(x, y);
+	}
+}
+
 static void sdl_refresh(DisplayState *ds)
 {
     SDL_Event ev1, *ev = &ev1;
@@ -446,10 +465,17 @@
             }
             break;
         case SDL_ACTIVEEVENT:
-            if (gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0 &&
+            if (!mouse_autograb && gui_grab && (ev->active.gain & SDL_ACTIVEEVENTMASK) == 0 &&
                 !gui_fullscreen_initial_grab) {
                 sdl_grab_end();
             }
+	    else if (mouse_autograb && ev->active.state == SDL_APPMOUSEFOCUS && ev->active.gain == 1 &&
+		!gui_grab && !gui_fullscreen && is_active_console(vga_console) &&
+		ds->hw_cursor_size)
+	    {
+	    	ignoreMouseEvent = 1;
+	    	sdl_grab_start();
+	    }
             break;
         default:
             break;
@@ -491,6 +517,7 @@
     ds->dpy_update = sdl_update;
     ds->dpy_resize = sdl_resize;
     ds->dpy_refresh = sdl_refresh;
+    ds->cursor_release = sdl_cursor_release;
 
     sdl_resize(ds, 640, 400);
     sdl_update_caption();
vl.c +6 -0
diff -ru qemu-snapshot-2005-02-10_23/vl.c qemu-snapshot-2005-02-10_23.patched/vl.c
@@ -137,6 +137,7 @@
 TextConsole *vga_console;
 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
+int mouse_autograb = 0;
 
 /***********************************************************/
 /* x86 ISA bus support */
@@ -2852,6 +2853,7 @@
     QEMU_OPTION_full_screen,
     QEMU_OPTION_pidfile,
     QEMU_OPTION_no_kqemu,
+    QEMU_OPTION_mouse_autograb,
 };
 
 typedef struct QEMUOption {
@@ -2918,6 +2920,7 @@
     { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
     { "full-screen", 0, QEMU_OPTION_full_screen },
     { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
+    { "autograb", 0, QEMU_OPTION_mouse_autograb },
 
     /* temporary options */
     { "pci", 0, QEMU_OPTION_pci },
@@ -3370,6 +3373,9 @@
                 kqemu_allowed = 0;
                 break;
 #endif
+            case QEMU_OPTION_mouse_autograb:
+                mouse_autograb = 1;
+                break;
             }
         }
     }
vl.h +3 -0
diff -ru qemu-snapshot-2005-02-10_23/vl.h qemu-snapshot-2005-02-10_23.patched/vl.h
@@ -125,6 +125,7 @@
 extern int graphic_depth;
 extern const char *keyboard_layout;
 extern int kqemu_allowed;
+extern int mouse_autograb;
 
 /* XXX: make it dynamic */
 #if defined (TARGET_PPC)
@@ -542,9 +543,11 @@
     int depth;
     int width;
     int height;
+    int hw_cursor_size;
     void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h);
     void (*dpy_resize)(struct DisplayState *s, int w, int h);
     void (*dpy_refresh)(struct DisplayState *s);
+    void (*cursor_release)(struct DisplayState *s, int x, int y);
 };
 
 static inline void dpy_update(DisplayState *s, int x, int y, int w, int h)