From 0447c2f3c3f3bdf5a302bda50509862903825b5b Mon Sep 17 00:00:00 2001
From: Cameron Gutman <[EMAIL REDACTED]>
Date: Thu, 1 May 2025 21:39:18 -0500
Subject: [PATCH] events: Add integer wheel fields for sdl2-compat
It's way simpler to just add them back to SDL3 than emulate them purely in sdl2-compat.
---
include/SDL3/SDL_events.h | 2 ++
src/events/SDL_events.c | 3 ++-
src/events/SDL_mouse.c | 27 +++++++++++++++++++--------
src/events/SDL_mouse_c.h | 4 ++--
4 files changed, 25 insertions(+), 11 deletions(-)
diff --git a/include/SDL3/SDL_events.h b/include/SDL3/SDL_events.h
index 300a08c367ecb..0201834ec247d 100644
--- a/include/SDL3/SDL_events.h
+++ b/include/SDL3/SDL_events.h
@@ -492,6 +492,8 @@ typedef struct SDL_MouseWheelEvent
SDL_MouseWheelDirection direction; /**< Set to one of the SDL_MOUSEWHEEL_* defines. When FLIPPED the values in X and Y will be opposite. Multiply by -1 to change them back */
float mouse_x; /**< X coordinate, relative to window */
float mouse_y; /**< Y coordinate, relative to window */
+ Sint32 integer_x; /**< The amount scrolled horizontally, accumulated to whole scroll "ticks" (added in 3.2.12) */
+ Sint32 integer_y; /**< The amount scrolled vertically, accumulated to whole scroll "ticks" (added in 3.2.12) */
} SDL_MouseWheelEvent;
/**
diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c
index 52a6f416faa8f..950575c13a197 100644
--- a/src/events/SDL_events.c
+++ b/src/events/SDL_events.c
@@ -625,9 +625,10 @@ int SDL_GetEventDescription(const SDL_Event *event, char *buf, int buflen)
#undef PRINT_MBUTTON_EVENT
SDL_EVENT_CASE(SDL_EVENT_MOUSE_WHEEL)
- (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u x=%g y=%g direction=%s)",
+ (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u x=%g y=%g integer_x=%d integer_y=%d direction=%s)",
(uint)event->wheel.timestamp, (uint)event->wheel.windowID,
(uint)event->wheel.which, event->wheel.x, event->wheel.y,
+ (int)event->wheel.integer_x, (int)event->wheel.integer_y,
event->wheel.direction == SDL_MOUSEWHEEL_NORMAL ? "normal" : "flipped");
break;
diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c
index e2e9e4abc64d9..166518a6f83e4 100644
--- a/src/events/SDL_mouse.c
+++ b/src/events/SDL_mouse.c
@@ -1040,18 +1040,14 @@ void SDL_SendMouseWheel(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseI
SDL_SetMouseFocus(window);
}
- // Accumulate fractional wheel motion if integer mode is enabled
- if (mouse->integer_mode_flags & 2) {
- mouse->integer_mode_residual_scroll_x = SDL_modff(mouse->integer_mode_residual_scroll_x + x, &x);
- mouse->integer_mode_residual_scroll_y = SDL_modff(mouse->integer_mode_residual_scroll_y + y, &y);
- }
-
if (x == 0.0f && y == 0.0f) {
return;
}
// Post the event, if desired
if (SDL_EventEnabled(SDL_EVENT_MOUSE_WHEEL)) {
+ float integer_x, integer_y;
+
if (!mouse->relative_mode || mouse->warp_emulation_active) {
// We're not in relative mode, so all mouse events are global mouse events
mouseID = SDL_GLOBAL_MOUSE_ID;
@@ -1062,11 +1058,26 @@ void SDL_SendMouseWheel(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseI
event.common.timestamp = timestamp;
event.wheel.windowID = mouse->focus ? mouse->focus->id : 0;
event.wheel.which = mouseID;
- event.wheel.x = x;
- event.wheel.y = y;
event.wheel.direction = direction;
event.wheel.mouse_x = mouse->x;
event.wheel.mouse_y = mouse->y;
+
+ mouse->residual_scroll_x = SDL_modff(mouse->residual_scroll_x + x, &integer_x);
+ event.wheel.integer_x = (Sint32)integer_x;
+
+ mouse->residual_scroll_y = SDL_modff(mouse->residual_scroll_y + y, &integer_y);
+ event.wheel.integer_y = (Sint32)integer_y;
+
+ // Return the accumulated values in x/y when integer wheel mode is enabled.
+ // This is necessary for compatibility with sdl2-compat 2.32.54.
+ if (mouse->integer_mode_flags & 2) {
+ event.wheel.x = integer_x;
+ event.wheel.y = integer_y;
+ } else {
+ event.wheel.x = x;
+ event.wheel.y = y;
+ }
+
SDL_PushEvent(&event);
}
}
diff --git a/src/events/SDL_mouse_c.h b/src/events/SDL_mouse_c.h
index 07a761b79789c..0bc913b94e16e 100644
--- a/src/events/SDL_mouse_c.h
+++ b/src/events/SDL_mouse_c.h
@@ -99,8 +99,6 @@ typedef struct
Uint8 integer_mode_flags; // 1 to enable mouse quantization, 2 to enable wheel quantization
float integer_mode_residual_motion_x;
float integer_mode_residual_motion_y;
- float integer_mode_residual_scroll_x;
- float integer_mode_residual_scroll_y;
// Data common to all mice
SDL_Window *focus;
@@ -109,6 +107,8 @@ typedef struct
float x_accu;
float y_accu;
float last_x, last_y; // the last reported x and y coordinates
+ float residual_scroll_x;
+ float residual_scroll_y;
double click_motion_x;
double click_motion_y;
bool has_position;