Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix erroneous mouse motion when alt-tabbing in CS2 #11460

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions src/events/SDL_mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ static void SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL
SDL_Mouse *mouse = SDL_GetMouse();
float xrel = 0.0f;
float yrel = 0.0f;
bool window_is_relative = mouse->focus && (mouse->focus->flags & SDL_WINDOW_MOUSE_RELATIVE_MODE);

if ((!mouse->relative_mode || mouse->warp_emulation_active) && mouseID != SDL_TOUCH_MOUSEID) {
// We're not in relative mode, so all mouse events are global mouse events
Expand Down Expand Up @@ -794,7 +795,6 @@ static void SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL
ConstrainMousePosition(mouse, window, &x, &y);
} else {
ConstrainMousePosition(mouse, window, &x, &y);

if (mouse->has_position) {
xrel = x - mouse->last_x;
yrel = y - mouse->last_y;
Expand All @@ -814,25 +814,30 @@ static void SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL
yrel = 0.0f;
}

if (mouse->has_position) {
// Update internal mouse coordinates
if (!mouse->relative_mode) {
{ // modify internal state
if (relative) {
if (mouse->has_position) {
mouse->x += xrel;
mouse->y += yrel;
ConstrainMousePosition(mouse, window, &mouse->x, &mouse->y);
} else {
mouse->x = x;
mouse->y = y;
}
mouse->last_x = mouse->x;
mouse->last_y = mouse->y;
mouse->x_accu += xrel;
mouse->y_accu += yrel;
} else {
// Use unclamped values if we're getting events outside the window
mouse->x = x;
mouse->y = y;
} else {
mouse->x += xrel;
mouse->y += yrel;
ConstrainMousePosition(mouse, window, &mouse->x, &mouse->y);
mouse->last_x = x;
mouse->last_y = y;
}
} else {
mouse->x = x;
mouse->y = y;
mouse->has_position = true;
}

mouse->xdelta += xrel;
mouse->ydelta += yrel;
expikr marked this conversation as resolved.
Show resolved Hide resolved

// Move the mouse cursor, if needed
if (mouse->cursor_shown && !mouse->relative_mode &&
mouse->MoveCursor && mouse->cur_cursor) {
Expand All @@ -841,6 +846,11 @@ static void SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL

// Post the event, if desired
if (SDL_EventEnabled(SDL_EVENT_MOUSE_MOTION)) {
if (!relative && window_is_relative) {
if (!mouse->relative_mode_warp_motion) return;
expikr marked this conversation as resolved.
Show resolved Hide resolved
xrel = 0.0f;
yrel = 0.0f;
}
SDL_Event event;
event.type = SDL_EVENT_MOUSE_MOTION;
event.common.timestamp = timestamp;
Expand All @@ -855,14 +865,6 @@ static void SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL
event.motion.yrel = yrel;
SDL_PushEvent(&event);
}
if (relative) {
mouse->last_x = mouse->x;
mouse->last_y = mouse->y;
} else {
// Use unclamped values if we're getting events outside the window
mouse->last_x = x;
mouse->last_y = y;
}
}

static SDL_MouseInputSource *GetMouseInputSource(SDL_Mouse *mouse, SDL_MouseID mouseID, bool down, Uint8 button)
Expand Down Expand Up @@ -1178,13 +1180,13 @@ SDL_MouseButtonFlags SDL_GetRelativeMouseState(float *x, float *y)
SDL_Mouse *mouse = SDL_GetMouse();

if (x) {
*x = mouse->xdelta;
*x = mouse->x_accu;
}
if (y) {
*y = mouse->ydelta;
*y = mouse->y_accu;
}
mouse->xdelta = 0.0f;
mouse->ydelta = 0.0f;
mouse->x_accu = 0.0f;
mouse->y_accu = 0.0f;
return SDL_GetMouseButtonState(mouse, SDL_GLOBAL_MOUSE_ID, true);
}

Expand Down
4 changes: 2 additions & 2 deletions src/events/SDL_mouse_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ typedef struct
SDL_Window *focus;
float x;
float y;
float xdelta;
float ydelta;
float x_accu;
float y_accu;
float last_x, last_y; // the last reported x and y coordinates
bool has_position;
bool relative_mode;
Expand Down
Loading