Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Send mouse leave updates for Windows and X11.
Browse files Browse the repository at this point in the history
We now generate a final SDL_MOUSEMOTION before the cursor leaves the
window, followed by a SDL_WINDOWEVENT_LEAVE.
  • Loading branch information
jorgenpt committed Apr 23, 2013
1 parent 25a7838 commit a61a2d5
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 10 deletions.
26 changes: 18 additions & 8 deletions src/events/SDL_mouse.c
Expand Up @@ -33,6 +33,8 @@
/* The mouse state */
static SDL_Mouse SDL_mouse;

static int
SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y);

/* Public functions */
int
Expand Down Expand Up @@ -154,8 +156,9 @@ SDL_UpdateMouseFocus(SDL_Window * window, int x, int y, Uint32 buttonstate)
#endif
if (window == mouse->focus) {
#ifdef DEBUG_MOUSE
printf("Mouse left window, synthesizing focus lost event\n");
printf("Mouse left window, synthesizing move & focus lost event\n");
#endif
SDL_PrivateSendMouseMotion(window, mouse->mouseID, 0, x, y);
SDL_SetMouseFocus(NULL);
}
return SDL_FALSE;
Expand All @@ -176,18 +179,25 @@ SDL_UpdateMouseFocus(SDL_Window * window, int x, int y, Uint32 buttonstate)
int
SDL_SendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y)
{
SDL_Mouse *mouse = SDL_GetMouse();
int posted;
int xrel;
int yrel;
int x_max = 0, y_max = 0;

if (window && !relative) {
SDL_Mouse *mouse = SDL_GetMouse();
if (!SDL_UpdateMouseFocus(window, x, y, mouse->buttonstate)) {
return 0;
}
}

return SDL_PrivateSendMouseMotion(window, mouseID, relative, x, y);
}

static int
SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y)
{
SDL_Mouse *mouse = SDL_GetMouse();
int posted;
int xrel;
int yrel;
int x_max = 0, y_max = 0;

/* relative motion is calculated regarding the system cursor last position */
if (relative) {
xrel = x;
Expand Down Expand Up @@ -676,4 +686,4 @@ SDL_ShowCursor(int toggle)
return shown;
}

/* vi: set ts=4 sw=4 expandtab: */
/* vi: set ts=4 sw=4 expandtab: */
2 changes: 2 additions & 0 deletions src/events/SDL_windowevents.c
Expand Up @@ -148,12 +148,14 @@ SDL_SendWindowEvent(SDL_Window * window, Uint8 windowevent, int data1,
return 0;
}
window->flags |= SDL_WINDOW_MOUSE_FOCUS;
SDL_OnWindowEnter(window);
break;
case SDL_WINDOWEVENT_LEAVE:
if (!(window->flags & SDL_WINDOW_MOUSE_FOCUS)) {
return 0;
}
window->flags &= ~SDL_WINDOW_MOUSE_FOCUS;
SDL_OnWindowLeave(window);
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
Expand Down
3 changes: 3 additions & 0 deletions src/video/SDL_sysvideo.h
Expand Up @@ -201,6 +201,7 @@ struct SDL_VideoDevice
int (*CreateWindowFramebuffer) (_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch);
int (*UpdateWindowFramebuffer) (_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects);
void (*DestroyWindowFramebuffer) (_THIS, SDL_Window * window);
void (*OnWindowEnter) (_THIS, SDL_Window * window);

/* * * */
/*
Expand Down Expand Up @@ -367,6 +368,8 @@ extern void SDL_OnWindowHidden(SDL_Window * window);
extern void SDL_OnWindowResized(SDL_Window * window);
extern void SDL_OnWindowMinimized(SDL_Window * window);
extern void SDL_OnWindowRestored(SDL_Window * window);
extern void SDL_OnWindowEnter(SDL_Window * window);
extern void SDL_OnWindowLeave(SDL_Window * window);
extern void SDL_OnWindowFocusGained(SDL_Window * window);
extern void SDL_OnWindowFocusLost(SDL_Window * window);
extern void SDL_UpdateWindowGrab(SDL_Window * window);
Expand Down
13 changes: 13 additions & 0 deletions src/video/SDL_video.c
Expand Up @@ -2028,6 +2028,19 @@ SDL_OnWindowRestored(SDL_Window * window)
}
}

void
SDL_OnWindowEnter(SDL_Window * window)
{
if (_this->OnWindowEnter) {
_this->OnWindowEnter(_this, window);
}
}

void
SDL_OnWindowLeave(SDL_Window * window)
{
}

void
SDL_OnWindowFocusGained(SDL_Window * window)
{
Expand Down
10 changes: 8 additions & 2 deletions src/video/windows/SDL_windowsevents.c
Expand Up @@ -445,10 +445,16 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
}

#ifdef WM_MOUSELEAVE
/* FIXME: Do we need the SDL 1.2 hack to generate WM_MOUSELEAVE now? */
case WM_MOUSELEAVE:
if (SDL_GetMouseFocus() == data->window) {
SDL_SetMouseFocus(NULL);
if (!SDL_GetMouse()->relative_mode) {
POINT cursorPos;
GetCursorPos(&cursorPos);
ScreenToClient(hwnd, &cursorPos);
SDL_SendMouseMotion(data->window, 0, 0, cursorPos.x, cursorPos.y);
}

SDL_SetMouseFocus(NULL);
}
returnCode = 0;
break;
Expand Down
1 change: 1 addition & 0 deletions src/video/windows/SDL_windowsvideo.c
Expand Up @@ -121,6 +121,7 @@ WIN_CreateDevice(int devindex)
device->CreateWindowFramebuffer = WIN_CreateWindowFramebuffer;
device->UpdateWindowFramebuffer = WIN_UpdateWindowFramebuffer;
device->DestroyWindowFramebuffer = WIN_DestroyWindowFramebuffer;
device->OnWindowEnter = WIN_OnWindowEnter;

device->shape_driver.CreateShaper = Win32_CreateShaper;
device->shape_driver.SetWindowShape = Win32_SetWindowShape;
Expand Down
19 changes: 19 additions & 0 deletions src/video/windows/SDL_windowswindow.c
Expand Up @@ -678,6 +678,25 @@ SDL_HelperWindowDestroy(void)
}
}

void WIN_OnWindowEnter(_THIS, SDL_Window * window)
{
#ifdef WM_MOUSELEAVE
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
TRACKMOUSEEVENT trackMouseEvent;

if (!data || !data->hwnd) {
/* The window wasn't fully initialized */
return;
}

trackMouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
trackMouseEvent.dwFlags = TME_LEAVE;
trackMouseEvent.hwndTrack = data->hwnd;

TrackMouseEvent(&trackMouseEvent);
#endif /* WM_MOUSELEAVE */
}

#endif /* SDL_VIDEO_DRIVER_WINDOWS */

/* vi: set ts=4 sw=4 expandtab: */
1 change: 1 addition & 0 deletions src/video/windows/SDL_windowswindow.h
Expand Up @@ -56,6 +56,7 @@ extern void WIN_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed);
extern void WIN_DestroyWindow(_THIS, SDL_Window * window);
extern SDL_bool WIN_GetWindowWMInfo(_THIS, SDL_Window * window,
struct SDL_SysWMinfo *info);
extern void WIN_OnWindowEnter(_THIS, SDL_Window * window);

#endif /* _SDL_windowswindow_h */

Expand Down
4 changes: 4 additions & 0 deletions src/video/x11/SDL_x11events.c
Expand Up @@ -324,6 +324,10 @@ X11_DispatchEvent(_THIS)
if (xevent.xcrossing.mode == NotifyUngrab)
printf("Mode: NotifyUngrab\n");
#endif
if (!SDL_GetMouse()->relative_mode) {
SDL_SendMouseMotion(data->window, 0, 0, xevent.xcrossing.x, xevent.xcrossing.y);
}

if (xevent.xcrossing.mode != NotifyGrab &&
xevent.xcrossing.mode != NotifyUngrab &&
xevent.xcrossing.detail != NotifyInferior) {
Expand Down

0 comments on commit a61a2d5

Please sign in to comment.