From ffa146f0bc811e0944044629d421b89c8336eb52 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 29 Jun 2023 14:38:26 -0700
Subject: [PATCH] On Windows the mouse speed is affected by the content scale
Fixes https://github.com/libsdl-org/SDL/issues/7802
---
src/events/SDL_mouse.c | 49 +++++++++++++++++++++++++++---------------
1 file changed, 32 insertions(+), 17 deletions(-)
diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c
index c805ff5c2e9b..e25695df6c6d 100644
--- a/src/events/SDL_mouse.c
+++ b/src/events/SDL_mouse.c
@@ -350,7 +350,7 @@ int SDL_SendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseI
return SDL_PrivateSendMouseMotion(timestamp, window, mouseID, relative, x, y);
}
-static float CalculateSystemScale(SDL_Mouse *mouse, const float *x, const float *y)
+static float CalculateSystemScale(SDL_Mouse *mouse, SDL_Window *window, const float *x, const float *y)
{
int i;
int n = mouse->num_system_scale_values;
@@ -359,23 +359,38 @@ static float CalculateSystemScale(SDL_Mouse *mouse, const float *x, const float
/* If we're using a single scale value, return that */
if (n == 1) {
- return v[0];
+ scale = v[0];
+ } else {
+ speed = SDL_sqrtf((*x * *x) + (*y * *y));
+ for (i = 0; i < (n - 2); i += 2) {
+ if (speed < v[i + 2]) {
+ break;
+ }
+ }
+ if (i == (n - 2)) {
+ scale = v[n - 1];
+ } else if (speed <= v[i]) {
+ scale = v[i + 1];
+ } else {
+ coef = (speed - v[i]) / (v[i + 2] - v[i]);
+ scale = v[i + 1] + (coef * (v[i + 3] - v[i + 1]));
+ }
}
+#ifdef __WIN32__
+ {
+ /* On Windows the mouse speed is affected by the content scale */
+ SDL_VideoDisplay *display;
- speed = SDL_sqrtf((*x * *x) + (*y * *y));
- for (i = 0; i < (n - 2); i += 2) {
- if (speed < v[i + 2]) {
- break;
+ if (window) {
+ display = SDL_GetVideoDisplayForWindow(window);
+ } else {
+ display = SDL_GetVideoDisplay(SDL_GetPrimaryDisplay());
+ }
+ if (display) {
+ scale *= display->content_scale;
}
}
- if (i == (n - 2)) {
- scale = v[n - 1];
- } else if (speed <= v[i]) {
- scale = v[i + 1];
- } else {
- coef = (speed - v[i]) / (v[i + 2] - v[i]);
- scale = v[i + 1] + (coef * (v[i + 3] - v[i + 1]));
- }
+#endif
return scale;
}
@@ -421,14 +436,14 @@ int SDL_SetMouseSystemScale(int num_values, const float *values)
return 0;
}
-static void GetScaledMouseDeltas(SDL_Mouse *mouse, float *x, float *y)
+static void GetScaledMouseDeltas(SDL_Mouse *mouse, SDL_Window *window, float *x, float *y)
{
if (mouse->relative_mode) {
if (mouse->enable_relative_speed_scale) {
*x *= mouse->relative_speed_scale;
*y *= mouse->relative_speed_scale;
} else if (mouse->enable_relative_system_scale && mouse->num_system_scale_values > 0) {
- float relative_system_scale = CalculateSystemScale(mouse, x, y);
+ float relative_system_scale = CalculateSystemScale(mouse, window, x, y);
*x *= relative_system_scale;
*y *= relative_system_scale;
}
@@ -529,7 +544,7 @@ static int SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_
}
if (relative) {
- GetScaledMouseDeltas(mouse, &x, &y);
+ GetScaledMouseDeltas(mouse, window, &x, &y);
xrel = x;
yrel = y;
x = (mouse->last_x + xrel);