changeset 2679:69c1093f8726

Ignore devices with 3-axes and no buttons since these are generally not gamepads/joysticks but accelerometers or gyros
author Michael Pavone <pavone@retrodev.com>
date Wed, 26 Mar 2025 01:17:04 -0700
parents 844ca8377b45
children e3394457427e
files render_sdl.c
diffstat 1 files changed, 20 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/render_sdl.c	Wed Mar 26 01:15:52 2025 -0700
+++ b/render_sdl.c	Wed Mar 26 01:17:04 2025 -0700
@@ -946,24 +946,29 @@
 	case SDL_JOYAXISMOTION:
 		handle_joy_axis(lock_joystick_index(render_find_joystick_index(event->jaxis.which), -1), event->jaxis.axis, event->jaxis.value);
 		break;
-	case SDL_JOYDEVICEADDED:
-		if (event->jdevice.which < MAX_JOYSTICKS) {
-			int index = lowest_unused_joystick_index();
-			if (index >= 0) {
-				SDL_Joystick * joy = joysticks[index] = SDL_JoystickOpen(event->jdevice.which);
-				joystick_sdl_index[index] = event->jdevice.which;
-				joystick_index_locked[index] = 0;
-				if (gc_events_enabled) {
-					controllers[index] = SDL_GameControllerOpen(event->jdevice.which);
-				}
-				if (joy) {
-					debug_message("Joystick %d added: %s\n", index, SDL_JoystickName(joy));
-					debug_message("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy));
-					handle_joy_added(index);
-				}
+	case SDL_JOYDEVICEADDED: {
+		int index = lowest_unused_joystick_index();
+		if (index >= 0) {
+			SDL_Joystick * joy = SDL_JoystickOpen(event->jdevice.which);
+			if (SDL_JoystickNumAxes(joy) == 3 && SDL_JoystickNumButtons(joy) == 0) {
+				//probably just an acclerometer or gyro
+				SDL_JoystickClose(joy);
+				break;
+			}
+			joysticks[index] = joy;
+			joystick_sdl_index[index] = event->jdevice.which;
+			joystick_index_locked[index] = 0;
+			if (gc_events_enabled) {
+				controllers[index] = SDL_GameControllerOpen(event->jdevice.which);
+			}
+			if (joy) {
+				debug_message("Joystick %d added: %s\n", index, SDL_JoystickName(joy));
+				debug_message("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy));
+				handle_joy_added(index);
 			}
 		}
 		break;
+	}
 	case SDL_JOYDEVICEREMOVED: {
 		int index = render_find_joystick_index(event->jdevice.which);
 		if (index >= 0) {