Skip to content

Commit

Permalink
Draft for input device reservation.
Browse files Browse the repository at this point in the history
Reservation config option and matching function lifted from:
PatrickStankard libretro#16269

Moved it to autoconfig to be more generic. Testing and GUI updates
are needed, refinements also possible.
  • Loading branch information
zoltanvb committed May 18, 2024
1 parent ae84c59 commit 247489b
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 6 deletions.
19 changes: 19 additions & 0 deletions configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,7 @@ bool config_overlay_enable_default(void)
static struct config_array_setting *populate_settings_array(
settings_t *settings, int *size)
{
unsigned i = 0;
unsigned count = 0;
struct config_array_setting *tmp = (struct config_array_setting*)calloc(1, (*size + 1) * sizeof(struct config_array_setting));

Expand Down Expand Up @@ -1511,6 +1512,24 @@ static struct config_array_setting *populate_settings_array(
SETTING_ARRAY("input_android_physical_keyboard", settings->arrays.input_android_physical_keyboard, false, NULL, true);
#endif

for (i = 0; i < MAX_USERS; i++)
{
size_t _len;
char formatted_number[4];
char prefix[16];
char key[32];

formatted_number[0] = '\0';

snprintf(formatted_number, sizeof(formatted_number), "%u", i + 1);
_len = strlcpy(prefix, "input_player", sizeof(prefix));
strlcpy(prefix + _len, formatted_number, sizeof(prefix) - _len);
_len = strlcpy(key, prefix, sizeof(key));
strlcpy(key + _len, "_reserved_device", sizeof(key) - _len);

SETTING_ARRAY(strdup(key), settings->arrays.input_reserved_devices[i], false, NULL, true);
}

#ifdef HAVE_MENU
SETTING_ARRAY("menu_driver", settings->arrays.menu_driver, false, NULL, true);
#endif
Expand Down
2 changes: 2 additions & 0 deletions configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ typedef struct settings
char input_android_physical_keyboard[255];
#endif

char input_reserved_devices[MAX_USERS][255];

char audio_device[255];
char camera_device[255];
char netplay_mitm_server[255];
Expand Down
11 changes: 9 additions & 2 deletions input/drivers_joypad/test_joypad.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,20 @@ static const char *test_joypad_name(unsigned pad)

static void test_joypad_autodetect_add(unsigned autoconf_pad)
{
int vid = 0;
int pid = 0;

sscanf(strstr(test_joypad_name(autoconf_pad), "(") + 1, "%04x:%04x", &vid, &pid);
//sscanf(test_joypad_name(autoconf_pad), "%04x:%04x ", &vid, &pid);
RARCH_DBG("[Test input driver]: Autoconf vid/pid %x:%x\n",vid,pid);

input_autoconfigure_connect(
test_joypad_name(autoconf_pad),
NULL,
"test",
autoconf_pad,
0,
0
vid,
pid
);
}

Expand Down
52 changes: 52 additions & 0 deletions tasks/task_autodetect.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,55 @@ static bool input_autoconfigure_scan_config_files_internal(
return false;
}

static void set_reserved_port(unsigned detected_port, int vendor_id,
int product_id, const char *device_name)
{
settings_t *settings = config_get_ptr();

unsigned port;
bool is_reserved;
char settings_value[256];
int settings_value_vendor_id;
int settings_value_product_id;
char settings_value_device_name[256];

for (port = 0; port < MAX_USERS; port++)
{
strlcpy(settings_value, settings->arrays.input_reserved_devices[port], sizeof(settings_value));

if (!string_is_empty(settings_value))
{
RARCH_LOG("[Autoconfig]: Examining reserved device for port %d: %s against %x:%x\n",port, settings_value, vendor_id, product_id);
if (sscanf(settings_value, "%04x:%04x ", &settings_value_vendor_id, &settings_value_product_id) != 2)
{
strlcpy(settings_value_device_name, settings_value, sizeof(settings_value_device_name));
is_reserved = string_is_equal(device_name, settings_value_device_name);
}
else
is_reserved = (vendor_id == settings_value_vendor_id && product_id == settings_value_product_id);

if (is_reserved)
{
unsigned prev_assigned_port = settings->uints.input_joypad_index[port];
RARCH_LOG("[Autoconfig]: Device \"%s\" (%x:%x) is reserved for port %d, setting.\n",
device_name, vendor_id, product_id, port+1);
if(detected_port != prev_assigned_port)
{
// todo: swap unused ports around, fix the pushed info message
settings->uints.input_joypad_index[port] = detected_port;
if(settings->uints.input_joypad_index[detected_port] == detected_port)
settings->uints.input_joypad_index[detected_port] = prev_assigned_port;
}
return;
}
}
}
RARCH_LOG("[Autoconfig]: Device \"%s\" (%d:%d) has no reserved port.\n",
device_name, vendor_id, product_id);

return;
}

/*************************/
/* Autoconfigure Connect */
/*************************/
Expand Down Expand Up @@ -427,6 +476,9 @@ static void cb_input_autoconfigure_connect(
if (autoconfig_handle->device_info.autoconfigured)
input_config_set_autoconfig_binds(port,
autoconfig_handle->autoconfig_file);

set_reserved_port(port,autoconfig_handle->device_info.vid, autoconfig_handle->device_info.pid,autoconfig_handle->device_info.name);

}

static void input_autoconfigure_connect_handler(retro_task_t *task)
Expand Down
27 changes: 27 additions & 0 deletions tests-other/autoconf/TestpadA.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
input_driver = "test"
input_device = "Test joypad device A (0001:0002)"
input_b_btn = "0"
input_y_btn = "1"
input_select_btn = "2"
input_start_btn = "3"
input_up_btn = "4"
input_down_btn = "5"
input_left_btn = "6"
input_right_btn = "7"
input_a_btn = "8"
input_x_btn = "9"
input_l_btn = "10"
input_r_btn = "11"
input_l2_btn = "12"
input_r2_btn = "13"
input_l3_btn = "14"
input_r3_btn = "15"
input_l_x_plus_axis = "+0"
input_l_x_minus_axis = "-0"
input_l_y_plus_axis = "+1"
input_l_y_minus_axis = "-1"
input_r_x_plus_axis = "+2"
input_r_x_minus_axis = "-2"
input_r_y_plus_axis = "+3"
input_r_y_minus_axis = "-3"

27 changes: 27 additions & 0 deletions tests-other/autoconf/TestpadB.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
input_driver = "test"
input_device = "Test joypad device B (0003:0004)"
input_b_btn = "0"
input_y_btn = "1"
input_select_btn = "2"
input_start_btn = "3"
input_up_btn = "4"
input_down_btn = "5"
input_left_btn = "6"
input_right_btn = "7"
input_a_btn = "8"
input_x_btn = "9"
input_l_btn = "10"
input_r_btn = "11"
input_l2_btn = "12"
input_r2_btn = "13"
input_l3_btn = "14"
input_r3_btn = "15"
input_l_x_plus_axis = "+0"
input_l_x_minus_axis = "-0"
input_l_y_plus_axis = "+1"
input_l_y_minus_axis = "-1"
input_r_x_plus_axis = "+2"
input_r_x_minus_axis = "-2"
input_r_y_plus_axis = "+3"
input_r_y_minus_axis = "-3"

27 changes: 27 additions & 0 deletions tests-other/autoconf/TestpadC.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
input_driver = "test"
input_device = "Test joypad device C (0005:0006)"
input_b_btn = "0"
input_y_btn = "1"
input_select_btn = "2"
input_start_btn = "3"
input_up_btn = "4"
input_down_btn = "5"
input_left_btn = "6"
input_right_btn = "7"
input_a_btn = "8"
input_x_btn = "9"
input_l_btn = "10"
input_r_btn = "11"
input_l2_btn = "12"
input_r2_btn = "13"
input_l3_btn = "14"
input_r3_btn = "15"
input_l_x_plus_axis = "+0"
input_l_x_minus_axis = "-0"
input_l_y_plus_axis = "+1"
input_l_y_minus_axis = "-1"
input_r_x_plus_axis = "+2"
input_r_x_minus_axis = "-2"
input_r_y_plus_axis = "+3"
input_r_y_minus_axis = "-3"

8 changes: 4 additions & 4 deletions tests-other/test_input_joypad.ratst
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
{
"action": 1,
"param_num": 0,
"param_str": "Test joypad device"
"param_str": "Test joypad device A (0001:0002)"
},
{
"action": 1,
"param_num": 1,
"param_str": "Test joypad device",
"param_str": "Test joypad device B (0003:0004)",
"frame": 0
},
{
"action": 1,
"param_num": 2,
"param_str": "Test joypad device"
"param_str": "Test joypad device C (0005:0006)"
},
{
"action": 16,
Expand Down Expand Up @@ -224,4 +224,4 @@
"action": 1002,
"param_num": 0
}
]
]
1 change: 1 addition & 0 deletions tests-other/testinput.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ frontend_log_level = "0"
libretro_log_level = "0"
log_verbosity = "true"
config_save_on_exit = "false"
input_player4_reserved_device = "0003:0004 "

0 comments on commit 247489b

Please sign in to comment.