From e7e04d9925d74d176da8f7f64ed9969323c0c11d Mon Sep 17 00:00:00 2001 From: Cooliokid956 <68075390+Cooliokid956@users.noreply.github.com> Date: Sat, 31 May 2025 01:04:53 -0500 Subject: [PATCH] Add Scroll Binds and fix a five year oob array index/write --- src/pc/controller/controller_bind_mapping.c | 16 +++++++++++----- src/pc/controller/controller_mouse.c | 5 +++++ src/pc/controller/controller_sdl.h | 2 ++ src/pc/controller/controller_sdl2.c | 8 +++----- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/pc/controller/controller_bind_mapping.c b/src/pc/controller/controller_bind_mapping.c index 8c56299f2..758e8a248 100644 --- a/src/pc/controller/controller_bind_mapping.c +++ b/src/pc/controller/controller_bind_mapping.c @@ -106,11 +106,17 @@ const char* translate_bind_to_name(int bind) { // mouse if (bind >= VK_BASE_SDL_MOUSE) { int mouse_button = (bind - VK_BASE_SDL_MOUSE); - if (mouse_button == 1) { return "L Mouse"; } - if (mouse_button == 2) { return "M Mouse"; } - if (mouse_button == 3) { return "R Mouse"; } - snprintf(name, 8, "Mouse %d", bind - VK_BASE_SDL_MOUSE); - return name; + switch (mouse_button) { + case 1: return "L Mouse"; + case 2: return "M Mouse"; + case 3: return "R Mouse"; + case 6: return "Scroll Up"; + case 7: return "Scroll Down"; + default: { + snprintf(name, 8, "Mouse %d", mouse_button); + return name; + } + } } // gamepad diff --git a/src/pc/controller/controller_mouse.c b/src/pc/controller/controller_mouse.c index 84c10a635..bb047ead5 100644 --- a/src/pc/controller/controller_mouse.c +++ b/src/pc/controller/controller_mouse.c @@ -116,6 +116,11 @@ void controller_mouse_read_relative(void) { #elif defined(CAPI_SDL1) || defined(CAPI_SDL2) mouse_buttons = SDL_GetRelativeMouseState(&mouse_x, &mouse_y); #endif + if (mouse_scroll_y > 0) { + mouse_buttons |= MWHEELUP; + } else if (mouse_scroll_y < 0) { + mouse_buttons |= MWHEELDOWN; + } } void controller_mouse_enter_relative(void) { diff --git a/src/pc/controller/controller_sdl.h b/src/pc/controller/controller_sdl.h index bab54eddc..b6235365d 100644 --- a/src/pc/controller/controller_sdl.h +++ b/src/pc/controller/controller_sdl.h @@ -8,6 +8,8 @@ // mouse buttons are also in the controller namespace, just offset 0x100 #define VK_OFS_SDL_MOUSE 0x0100 #define VK_BASE_SDL_MOUSE (VK_BASE_SDL_GAMEPAD + VK_OFS_SDL_MOUSE) +#define MWHEELUP 0x20 +#define MWHEELDOWN 0x40 extern struct ControllerAPI controller_sdl; diff --git a/src/pc/controller/controller_sdl2.c b/src/pc/controller/controller_sdl2.c index 373065415..a38087c37 100644 --- a/src/pc/controller/controller_sdl2.c +++ b/src/pc/controller/controller_sdl2.c @@ -60,11 +60,11 @@ static s16 invert_s16(s16 val) { static inline void controller_add_binds(const u32 mask, const u32 *btns) { for (u32 i = 0; i < MAX_BINDS; ++i) { if (btns[i] >= VK_BASE_SDL_GAMEPAD && btns[i] <= VK_BASE_SDL_GAMEPAD + VK_SIZE) { - if (btns[i] >= VK_BASE_SDL_MOUSE && num_joy_binds < MAX_JOYBINDS) { + if (btns[i] >= VK_BASE_SDL_MOUSE && num_mouse_binds < MAX_JOYBINDS) { mouse_binds[num_mouse_binds][0] = btns[i] - VK_BASE_SDL_MOUSE; mouse_binds[num_mouse_binds][1] = mask; ++num_mouse_binds; - } else if (num_mouse_binds < MAX_JOYBINDS) { + } else if (num_joy_binds < MAX_JOYBINDS) { joy_binds[num_joy_binds][0] = btns[i] - VK_BASE_SDL_GAMEPAD; joy_binds[num_joy_binds][1] = mask; ++num_joy_binds; @@ -285,9 +285,7 @@ static void controller_sdl_read(OSContPad *pad) { u32 buttons_down = 0; for (u32 i = 0; i < num_joy_binds; ++i) if (joy_buttons[joy_binds[i][0]]) - buttons_down |= joy_binds[i][1]; - - pad->button |= buttons_down; + pad->button |= joy_binds[i][1]; const u32 xstick = buttons_down & STICK_XMASK; const u32 ystick = buttons_down & STICK_YMASK;