Work around bug in SDL_ShowCursor(0) in libsdl-1.2.9
PATCH 1/1 Work around bug in SDL_ShowCursor(0) in libsdl-1.2.9
Hi,
There appears to bug a bug in the latest version of SDL (1.2.9) that causes the position of the cursor to be wrongly reported after calling SDL_ShowCursor(0).
When the cursor is disabled, relative and absolute mouse events are reported as garbage.
This patch works around this by using a invisible cursor instead of disabling the cursor. This has the same functionality of disabling the cursor but avoids these problems.
For what it's worth, disabling the cursor in older versions of SDL also has strange side effects. For instance, the cursor gets warped when you hit the edge of the bounding box. If you're using an absolute input device (emulating a Wacom or Synaptics touchpad for instance), this results in weird mouse behavior (when you exit this window, your mouse ends up in the middle of the window!).
Avoiding SDL_ShowCursor(0) is probably a good idea regardless of this particular bug.
Regards,
Anthony Liguori
sdl.c +12 -2
@@ -39,6 +39,8 @@static int gui_fullscreen_initial_grab;static int gui_grab_code =KMOD_LALT |KMOD_LCTRL ;static uint8_t modifiers_state[256 ]; +static SDL_Cursor *sdl_cursor_normal; +static SDL_Cursor *sdl_cursor_hidden;static void sdl_update (DisplayState *ds,int x,int y,int w,int h) {
@@ -273,7 +275,8 @@static void sdl_grab_start (void ) { -SDL_ShowCursor (0 ); + sdl_cursor_normal =SDL_GetCursor (); +SDL_SetCursor (sdl_cursor_hidden);SDL_WM_GrabInput (SDL_GRAB_ON );/* dummy read to avoid moving the mouse */ SDL_GetRelativeMouseState (NULL ,NULL );
@@ -284,7 +287,7 @@static void sdl_grab_end (void ) {SDL_WM_GrabInput (SDL_GRAB_OFF ); -SDL_ShowCursor (1 ); +SDL_SetCursor (sdl_cursor_normal); gui_grab =0 ;sdl_update_caption (); }
@@ -475,6 +478,7 @@void sdl_display_init (DisplayState *ds,int full_screen) {int flags; +uint8_t data =0 ;#if defined(__APPLE__)/* always use generic keymaps */
@@ -508,6 +512,12 @@SDL_EnableUNICODE (1 ); gui_grab =0 ; +/* work around a bug in libsdl that causes mouse position to be reported +relative to the upper left corner of the screen instead of the window +after SDL_ShowCursor(0) by using a custom invisible cursor. +*/ + sdl_cursor_hidden =SDL_CreateCursor (&data, &data,8 ,1 ,0 ,0 ); +atexit (sdl_cleanup);if (full_screen) { gui_fullscreen =1 ;