changeset 2627:df6dbf229e2f

Prevent gamepad binds from firing while remapping a gamepad
author Michael Pavone <pavone@retrodev.com>
date Sat, 22 Feb 2025 23:33:12 -0800
parents 36aa9ead0e62
children 32ce6c588bc9
files bindings.c bindings.h nuklear_ui/blastem_nuklear.c
diffstat 3 files changed, 17 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/bindings.c	Sat Feb 22 23:11:35 2025 -0800
+++ b/bindings.c	Sat Feb 22 23:33:12 2025 -0800
@@ -86,6 +86,7 @@
 #define DEFAULT_JOYBUTTON_ALLOC 12
 static keybinding *bindings[0x10000];
 static joystick joysticks[MAX_JOYSTICKS];
+static uint8_t joystick_enabled[MAX_JOYSTICKS] = {1,1,1,1,1,1,1,1};
 static mousebinding mice[MAX_MICE];
 const uint8_t dpadbits[] = {RENDER_DPAD_UP, RENDER_DPAD_DOWN, RENDER_DPAD_LEFT, RENDER_DPAD_RIGHT};
 
@@ -205,6 +206,14 @@
 	content_binds_enabled = enabled;
 }
 
+void bindings_set_joy_state(int joystick, uint8_t enabled)
+{
+	if (joystick >= MAX_JOYSTICKS || joystick < 0) {
+		return;
+	}
+	joystick_enabled[joystick] = enabled;
+}
+
 void handle_binding_down(keybinding * binding)
 {
 	if (!current_system || !content_binds_enabled) {
@@ -235,7 +244,7 @@
 
 void handle_joydown(int joystick, int button)
 {
-	if (joystick >= MAX_JOYSTICKS || button >= joysticks[joystick].num_buttons) {
+	if (joystick >= MAX_JOYSTICKS || button >= joysticks[joystick].num_buttons || !joystick_enabled[joystick]) {
 		return;
 	}
 	keybinding * binding = joysticks[joystick].buttons + button;
@@ -515,7 +524,7 @@
 
 void handle_joyup(int joystick, int button)
 {
-	if (joystick >= MAX_JOYSTICKS  || button >= joysticks[joystick].num_buttons) {
+	if (joystick >= MAX_JOYSTICKS  || button >= joysticks[joystick].num_buttons || !joystick_enabled[joystick]) {
 		return;
 	}
 	keybinding * binding = joysticks[joystick].buttons + button;
@@ -524,7 +533,7 @@
 
 void handle_joy_dpad(int joystick, int dpadnum, uint8_t value)
 {
-	if (joystick >= MAX_JOYSTICKS  || dpadnum >= joysticks[joystick].num_dpads) {
+	if (joystick >= MAX_JOYSTICKS  || dpadnum >= joysticks[joystick].num_dpads || !joystick_enabled[joystick]) {
 		return;
 	}
 	joydpad * dpad = joysticks[joystick].dpads + dpadnum;
@@ -542,7 +551,7 @@
 
 void handle_joy_axis(int joystick, int axis, int16_t value)
 {
-	if (joystick >= MAX_JOYSTICKS  || axis >= joysticks[joystick].num_axes) {
+	if (joystick >= MAX_JOYSTICKS  || axis >= joysticks[joystick].num_axes || !joystick_enabled[joystick]) {
 		return;
 	}
 	joyaxis *jaxis = joysticks[joystick].axes + axis;
--- a/bindings.h	Sat Feb 22 23:11:35 2025 -0800
+++ b/bindings.h	Sat Feb 22 23:33:12 2025 -0800
@@ -30,5 +30,6 @@
 void bindings_release_capture(void);
 void bindings_reacquire_capture(void);
 void set_content_binding_state(uint8_t enabled);
+void bindings_set_joy_state(int joystick, uint8_t enabled);
 
 #endif //BINDINGS_H_
--- a/nuklear_ui/blastem_nuklear.c	Sat Feb 22 23:11:35 2025 -0800
+++ b/nuklear_ui/blastem_nuklear.c	Sat Feb 22 23:33:12 2025 -0800
@@ -1514,6 +1514,7 @@
 					save_controller_mapping(selected_controller, mapping_string);
 					free(mapping_string);
 					pop_view();
+					bindings_set_joy_state(selected_controller, 1);
 					if (initial_controller_config) {
 						push_view(view_controller_bindings);
 						controller_binding_changed = 0;
@@ -1554,6 +1555,8 @@
 	}
 
 	push_view(view_controller_mappings);
+	bindings_set_joy_state(selected_controller, 0);
+	
 }
 
 static void view_controller_variant(struct nk_context *context)