From 8dc49fee718bcab2231302879e0cab6d78f7151b Mon Sep 17 00:00:00 2001 From: kichikuou Date: Tue, 4 Jan 2022 10:28:40 +0900 Subject: [PATCH] Handle SDL_JOYDEVICEADDED event --- src/sdl_event.c | 3 +++ src/sdl_private.h | 7 ++++--- src/sdl_video.c | 22 ++++++++++++---------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/sdl_event.c b/src/sdl_event.c index f75fb2c..2c073e3 100644 --- a/src/sdl_event.c +++ b/src/sdl_event.c @@ -204,6 +204,9 @@ static void sdl_getEvent(void) { mousey = e.tfinger.y * view_h; break; + case SDL_JOYDEVICEADDED: + sdl_joy_open(e.jdevice.which); + break; case SDL_JOYAXISMOTION: if (abs(e.jaxis.value) < 0x4000) { diff --git a/src/sdl_private.h b/src/sdl_private.h index 097c2c6..efdd147 100644 --- a/src/sdl_private.h +++ b/src/sdl_private.h @@ -55,9 +55,10 @@ struct sdl_private_data { boolean (*custom_event_handler)(const SDL_Event *); }; -extern void sdl_cursor_init(void); -extern void sdl_shadow_init(void); -extern int sdl_nearest_color(int r, int g, int b); +void sdl_cursor_init(void); +void sdl_shadow_init(void); +int sdl_nearest_color(int r, int g, int b); +boolean sdl_joy_open(int index); extern struct sdl_private_data *sdl_videodev; diff --git a/src/sdl_video.c b/src/sdl_video.c index 7c4b940..d6ad094 100644 --- a/src/sdl_video.c +++ b/src/sdl_video.c @@ -45,7 +45,10 @@ static boolean integer_scaling = FALSE; static SDL_Joystick *js; -static boolean joy_open_index(int index) { +boolean sdl_joy_open(int index) { + if (js) + return FALSE; + js = SDL_JoystickOpen(index); if (!js) return FALSE; @@ -58,16 +61,15 @@ static boolean joy_open_index(int index) { return TRUE; } -static int joy_open(void) { - if (joy_device_index >= 0) { - return joy_open_index(joy_device_index) ? 1 : -1; - } else { - for (int i = 0; i < SDL_NumJoysticks(); i++) { - if (joy_open_index(i)) - return 1; - } +static boolean joy_open(void) { + if (joy_device_index >= 0) + return sdl_joy_open(joy_device_index); + + for (int i = 0; i < SDL_NumJoysticks(); i++) { + if (sdl_joy_open(i)) + return TRUE; } - return -1; + return FALSE; } /* SDL の初期化 */