Removed SDL_HINT_MOUSE_RELATIVE_MODE_WARP

This complicated mouse handling and is a rarely tested path. Real relative mode is much better performance and higher precision.
This commit is contained in:
Sam Lantinga
2024-12-18 17:49:51 -08:00
parent 6e0df0af24
commit e08f34c3be
10 changed files with 16 additions and 90 deletions

View File

@@ -694,30 +694,6 @@ static void SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL
return;
}
if (mouseID != SDL_TOUCH_MOUSEID && mouse->relative_mode_warp) {
int w = 0, h = 0;
float center_x, center_y;
SDL_GetWindowSize(window, &w, &h);
center_x = (float)w / 2.0f;
center_y = (float)h / 2.0f;
if (x >= SDL_floorf(center_x) && x <= SDL_ceilf(center_x) &&
y >= SDL_floorf(center_y) && y <= SDL_ceilf(center_y)) {
mouse->last_x = center_x;
mouse->last_y = center_y;
if (!mouse->relative_mode_warp_motion) {
return;
}
} else {
if (window && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
if (mouse->WarpMouse) {
mouse->WarpMouse(window, center_x, center_y);
} else {
SDL_PrivateSendMouseMotion(timestamp, window, mouseID, false, center_x, center_y);
}
}
}
}
if (relative) {
if (mouse->relative_mode) {
if (mouse->enable_relative_speed_scale) {
@@ -760,19 +736,14 @@ static void SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL
yrel = 0.0f;
}
// TODO: should rework overall so that relative bool arg conveys intent,
// and do this logic at the SDL_SendMouseMotion level instead of here.
bool cmd_is_meant_as_delta = relative || (mouse->relative_mode && mouse->relative_mode_warp);
bool cmd_is_hardware_delta = relative;
// modify internal state
{
if (cmd_is_meant_as_delta) {
if (relative) {
mouse->x_accu += xrel;
mouse->y_accu += yrel;
}
if (cmd_is_meant_as_delta && mouse->has_position) {
if (relative && mouse->has_position) {
mouse->x += xrel;
mouse->y += yrel;
ConstrainMousePosition(mouse, window, &mouse->x, &mouse->y);
@@ -783,8 +754,8 @@ static void SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL
mouse->has_position = true;
// Use unclamped values if we're getting events outside the window
mouse->last_x = cmd_is_hardware_delta ? mouse->x : x;
mouse->last_y = cmd_is_hardware_delta ? mouse->y : y;
mouse->last_x = relative ? mouse->x : x;
mouse->last_y = relative ? mouse->y : y;
}
// Move the mouse cursor, if needed
@@ -795,7 +766,7 @@ static void SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL
// Post the event, if desired
if (SDL_EventEnabled(SDL_EVENT_MOUSE_MOTION)) {
if (!cmd_is_meant_as_delta && window_is_relative) {
if (!relative && window_is_relative) {
if (!mouse->relative_mode_warp_motion) {
return;
}
@@ -1195,8 +1166,7 @@ void SDL_PerformWarpMouseInWindow(SDL_Window *window, float x, float y, bool ign
}
}
if (mouse->WarpMouse &&
(!mouse->relative_mode || mouse->relative_mode_warp)) {
if (mouse->WarpMouse && !mouse->relative_mode) {
mouse->WarpMouse(window, x, y);
} else {
SDL_PrivateSendMouseMotion(0, window, SDL_GLOBAL_MOUSE_ID, false, x, y);
@@ -1265,16 +1235,6 @@ bool SDL_WarpMouseGlobal(float x, float y)
return SDL_Unsupported();
}
static bool SDL_ShouldUseRelativeModeWarp(SDL_Mouse *mouse)
{
if (!mouse->WarpMouse) {
// Need this functionality for relative mode warp implementation
return false;
}
return SDL_GetHintBoolean(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, false);
}
bool SDL_SetRelativeMouseMode(bool enabled)
{
SDL_Mouse *mouse = SDL_GetMouse();
@@ -1290,17 +1250,9 @@ bool SDL_SetRelativeMouseMode(bool enabled)
}
// Set the relative mode
if (!enabled && mouse->relative_mode_warp) {
mouse->relative_mode_warp = false;
} else if (enabled && SDL_ShouldUseRelativeModeWarp(mouse)) {
mouse->relative_mode_warp = true;
} else if (!mouse->SetRelativeMouseMode || !mouse->SetRelativeMouseMode(enabled)) {
if (!mouse->SetRelativeMouseMode || !mouse->SetRelativeMouseMode(enabled)) {
if (enabled) {
// Fall back to warp mode if native relative mode failed
if (!mouse->WarpMouse) {
return SDL_SetError("No relative mode implementation available");
}
mouse->relative_mode_warp = true;
return SDL_SetError("No relative mode implementation available");
}
}
mouse->relative_mode = enabled;
@@ -1312,12 +1264,6 @@ bool SDL_SetRelativeMouseMode(bool enabled)
if (enabled && focusWindow) {
SDL_SetMouseFocus(focusWindow);
if (mouse->relative_mode_warp) {
float center_x = (float)focusWindow->w / 2.0f;
float center_y = (float)focusWindow->h / 2.0f;
SDL_PerformWarpMouseInWindow(focusWindow, center_x, center_y, true);
}
}
if (focusWindow) {

View File

@@ -95,7 +95,6 @@ typedef struct
float last_x, last_y; // the last reported x and y coordinates
bool has_position;
bool relative_mode;
bool relative_mode_warp;
bool relative_mode_warp_motion;
bool relative_mode_cursor_visible;
bool relative_mode_center;

View File

@@ -4012,9 +4012,6 @@ void SDL_OnWindowFocusGained(SDL_Window *window)
if (mouse && mouse->relative_mode) {
SDL_SetMouseFocus(window);
if (mouse->relative_mode_warp) {
SDL_PerformWarpMouseInWindow(window, (float)window->w / 2.0f, (float)window->h / 2.0f, true);
}
}
SDL_UpdateWindowGrab(window);

View File

@@ -954,7 +954,7 @@ static NSCursor *Cocoa_GetDesiredCursor(void)
mouse->WarpMouseGlobal(pendingWindowWarpX, pendingWindowWarpY);
pendingWindowWarpX = pendingWindowWarpY = FLT_MAX;
}
if (mouse->relative_mode && !mouse->relative_mode_warp && mouse->focus == _data.window) {
if (mouse->relative_mode && mouse->focus == _data.window) {
// Move the cursor to the nearest point in the window
{
float x, y;

View File

@@ -1655,7 +1655,7 @@ void WIN_UpdateClipCursor(SDL_Window *window)
// }
SDL_Mouse *mouse = SDL_GetMouse();
bool lock_to_ctr = (mouse->relative_mode_center && mouse->relative_mode && !mouse->relative_mode_warp);
bool lock_to_ctr = (mouse->relative_mode && mouse->relative_mode_center);
RECT client;
if (!GetClientScreenRect(data->hwnd, &client)) {

View File

@@ -905,7 +905,7 @@ void X11_HandleButtonPress(SDL_VideoDevice *_this, SDL_WindowData *windowdata, S
#endif
SDL_Mouse *mouse = SDL_GetMouse();
if ((!mouse->relative_mode || mouse->relative_mode_warp) && (x != mouse->x || y != mouse->y)) {
if (!mouse->relative_mode && (x != mouse->x || y != mouse->y)) {
X11_ProcessHitTest(_this, windowdata, x, y, false);
SDL_SendMouseMotion(0, window, mouseID, false, x, y);
}
@@ -1556,7 +1556,7 @@ static void X11_DispatchEvent(SDL_VideoDevice *_this, XEvent *xevent)
}
SDL_Mouse *mouse = SDL_GetMouse();
if (!mouse->relative_mode || mouse->relative_mode_warp) {
if (!mouse->relative_mode) {
#ifdef DEBUG_MOTION
SDL_Log("window 0x%lx: X11 motion: %d,%d\n", xevent->xany.window, xevent->xmotion.x, xevent->xmotion.y);
#endif

View File

@@ -325,7 +325,7 @@ void X11_HandleXinput2Event(SDL_VideoDevice *_this, XGenericEventCookie *cookie)
break; // Pens check for XI_Motion instead
}
if (!mouse->relative_mode || mouse->relative_mode_warp) {
if (!mouse->relative_mode) {
break;
}
@@ -469,7 +469,7 @@ void X11_HandleXinput2Event(SDL_VideoDevice *_this, XGenericEventCookie *cookie)
} else if (!pointer_emulated && xev->deviceid == videodata->xinput_master_pointer_device) {
// Use the master device for non-relative motion, as the slave devices can seemingly lag behind.
SDL_Mouse *mouse = SDL_GetMouse();
if (!mouse->relative_mode || mouse->relative_mode_warp) {
if (!mouse->relative_mode) {
SDL_Window *window = xinput2_get_sdlwindow(videodata, xev->event);
if (window) {
X11_ProcessHitTest(_this, window->internal, (float)xev->event_x, (float)xev->event_y, false);