(task_autodetect.c) Cleanups - don't check for variables that

we already know are non-NULL, cleanup variable scope, etc.
This commit is contained in:
libretroadmin 2022-07-09 18:08:25 +02:00
parent ad87f88b72
commit cda235d124
1 changed files with 124 additions and 151 deletions

View File

@ -54,9 +54,6 @@ typedef struct
static void free_autoconfig_handle(autoconfig_handle_t *autoconfig_handle) static void free_autoconfig_handle(autoconfig_handle_t *autoconfig_handle)
{ {
if (!autoconfig_handle)
return;
if (autoconfig_handle->dir_autoconfig) if (autoconfig_handle->dir_autoconfig)
{ {
free(autoconfig_handle->dir_autoconfig); free(autoconfig_handle->dir_autoconfig);
@ -82,12 +79,7 @@ static void free_autoconfig_handle(autoconfig_handle_t *autoconfig_handle)
static void input_autoconfigure_free(retro_task_t *task) static void input_autoconfigure_free(retro_task_t *task)
{ {
autoconfig_handle_t *autoconfig_handle = NULL; autoconfig_handle_t *autoconfig_handle = NULL;
if (task && (autoconfig_handle = (autoconfig_handle_t*)task->state))
if (!task)
return;
autoconfig_handle = (autoconfig_handle_t*)task->state;
free_autoconfig_handle(autoconfig_handle); free_autoconfig_handle(autoconfig_handle);
} }
@ -111,8 +103,8 @@ static unsigned input_autoconfigure_get_config_file_affinity(
uint16_t config_pid = 0; uint16_t config_pid = 0;
bool pid_match = false; bool pid_match = false;
unsigned affinity = 0; unsigned affinity = 0;
struct config_entry_list struct config_entry_list *entry = NULL;
*entry = NULL;
/* Parse config file */ /* Parse config file */
if (config_get_int(config, "input_vendor_id", &tmp_int)) if (config_get_int(config, "input_vendor_id", &tmp_int))
@ -128,16 +120,18 @@ static unsigned input_autoconfigure_get_config_file_affinity(
#endif #endif
/* Check for matching VID+PID */ /* Check for matching VID+PID */
pid_match = (autoconfig_handle->device_info.vid == config_vid) && pid_match =
(autoconfig_handle->device_info.pid == config_pid) && (autoconfig_handle->device_info.vid == config_vid)
(autoconfig_handle->device_info.vid != 0) && && (autoconfig_handle->device_info.pid == config_pid)
(autoconfig_handle->device_info.pid != 0); && (autoconfig_handle->device_info.vid != 0)
&& (autoconfig_handle->device_info.pid != 0);
/* > More Bliss-Box shenanigans... */ /* > More Bliss-Box shenanigans... */
#ifdef HAVE_BLISSBOX #ifdef HAVE_BLISSBOX
pid_match = pid_match && if (pid_match)
(autoconfig_handle->device_info.vid != BLISSBOX_VID) && pid_match =
(autoconfig_handle->device_info.pid != BLISSBOX_PID); (autoconfig_handle->device_info.vid != BLISSBOX_VID)
&& (autoconfig_handle->device_info.pid != BLISSBOX_PID);
#endif #endif
if (pid_match) if (pid_match)
@ -201,14 +195,12 @@ static bool input_autoconfigure_scan_config_files_external(
const char *dir_autoconfig = autoconfig_handle->dir_autoconfig; const char *dir_autoconfig = autoconfig_handle->dir_autoconfig;
const char *dir_driver_autoconfig = autoconfig_handle->dir_driver_autoconfig; const char *dir_driver_autoconfig = autoconfig_handle->dir_driver_autoconfig;
struct string_list *config_file_list = NULL; struct string_list *config_file_list = NULL;
config_file_t *best_config = NULL;
unsigned max_affinity = 0;
bool match_found = false; bool match_found = false;
/* Attempt to fetch file listing from driver-specific /* Attempt to fetch file listing from driver-specific
* autoconfig directory */ * autoconfig directory */
if (!string_is_empty(dir_driver_autoconfig) && if ( !string_is_empty (dir_driver_autoconfig)
path_is_directory(dir_driver_autoconfig)) && path_is_directory(dir_driver_autoconfig))
config_file_list = dir_list_new_special( config_file_list = dir_list_new_special(
dir_driver_autoconfig, DIR_LIST_AUTOCONFIG, dir_driver_autoconfig, DIR_LIST_AUTOCONFIG,
"cfg", false); "cfg", false);
@ -223,15 +215,17 @@ static bool input_autoconfigure_scan_config_files_external(
config_file_list = NULL; config_file_list = NULL;
} }
if (!string_is_empty(dir_autoconfig) && if ( !string_is_empty (dir_autoconfig)
path_is_directory(dir_autoconfig)) && path_is_directory(dir_autoconfig))
config_file_list = dir_list_new_special( config_file_list = dir_list_new_special(
dir_autoconfig, DIR_LIST_AUTOCONFIG, dir_autoconfig, DIR_LIST_AUTOCONFIG,
"cfg", false); "cfg", false);
} }
if (!config_file_list || (config_file_list->size < 1)) if (config_file_list && (config_file_list->size >= 1))
goto end; {
config_file_t *best_config = NULL;
unsigned max_affinity = 0;
/* Loop through external config files */ /* Loop through external config files */
for (i = 0; i < config_file_list->size; i++) for (i = 0; i < config_file_list->size; i++)
@ -248,11 +242,8 @@ static bool input_autoconfigure_scan_config_files_external(
continue; continue;
/* Check for a match */ /* Check for a match */
if (autoconfig_handle && config) if ((affinity = input_autoconfigure_get_config_file_affinity(
affinity = input_autoconfigure_get_config_file_affinity( autoconfig_handle, config)) > max_affinity)
autoconfig_handle, config);
if (affinity > max_affinity)
{ {
if (best_config) if (best_config)
{ {
@ -282,15 +273,11 @@ static bool input_autoconfigure_scan_config_files_external(
* been cached, then we have a match */ * been cached, then we have a match */
if (best_config) if (best_config)
{ {
if (autoconfig_handle && best_config)
input_autoconfigure_set_config_file( input_autoconfigure_set_config_file(
autoconfig_handle, best_config); autoconfig_handle, best_config);
match_found = true; match_found = true;
} }
end:
if (config_file_list)
{
string_list_free(config_file_list); string_list_free(config_file_list);
config_file_list = NULL; config_file_list = NULL;
} }
@ -313,7 +300,6 @@ static bool input_autoconfigure_scan_config_files_internal(
{ {
char *autoconfig_str = NULL; char *autoconfig_str = NULL;
config_file_t *config = NULL; config_file_t *config = NULL;
unsigned affinity = 0;
if (string_is_empty(input_builtin_autoconfs[i])) if (string_is_empty(input_builtin_autoconfs[i]))
continue; continue;
@ -327,24 +313,20 @@ static bool input_autoconfigure_scan_config_files_internal(
free(autoconfig_str); free(autoconfig_str);
autoconfig_str = NULL; autoconfig_str = NULL;
if (config)
{
/* Check for a match */ /* Check for a match */
if (autoconfig_handle && config) if (input_autoconfigure_get_config_file_affinity(
affinity = input_autoconfigure_get_config_file_affinity( autoconfig_handle, config) > 0)
autoconfig_handle, config); {
/* > In the case of internal autoconfigs, any kind /* > In the case of internal autoconfigs, any kind
* of match is considered to be a success */ * of match is considered to be a success */
if (affinity > 0)
{
if (autoconfig_handle && config)
input_autoconfigure_set_config_file( input_autoconfigure_set_config_file(
autoconfig_handle, config); autoconfig_handle, config);
return true; return true;
} }
/* No match - clean up */ /* No match - clean up */
if (config)
{
config_file_free(config); config_file_free(config);
config = NULL; config = NULL;
} }
@ -569,22 +551,18 @@ static void input_autoconfigure_connect_handler(retro_task_t *task)
} }
static bool autoconfigure_connect_finder(retro_task_t *task, void *user_data) static bool autoconfigure_connect_finder(retro_task_t *task, void *user_data)
{
if (task && user_data && task->handler == input_autoconfigure_connect_handler)
{ {
autoconfig_handle_t *autoconfig_handle = NULL; autoconfig_handle_t *autoconfig_handle = NULL;
unsigned *port = NULL; if ((autoconfig_handle = (autoconfig_handle_t*)task->state))
{
if (!task || !user_data) unsigned *port = (unsigned*)user_data;
return false;
if (task->handler != input_autoconfigure_connect_handler)
return false;
if (!(autoconfig_handle = (autoconfig_handle_t*)task->state))
return false;
port = (unsigned*)user_data;
return (*port == autoconfig_handle->port); return (*port == autoconfig_handle->port);
} }
}
return false;
}
bool input_autoconfigure_connect( bool input_autoconfigure_connect(
const char *name, const char *name,
@ -714,6 +692,7 @@ bool input_autoconfigure_connect(
/* Configure task */ /* Configure task */
if (!(task = task_init())) if (!(task = task_init()))
{ {
if (autoconfig_handle)
free_autoconfig_handle(autoconfig_handle); free_autoconfig_handle(autoconfig_handle);
autoconfig_handle = NULL; autoconfig_handle = NULL;
return false; return false;
@ -767,21 +746,20 @@ static void cb_input_autoconfigure_disconnect(
static void input_autoconfigure_disconnect_handler(retro_task_t *task) static void input_autoconfigure_disconnect_handler(retro_task_t *task)
{ {
autoconfig_handle_t *autoconfig_handle = NULL; autoconfig_handle_t *autoconfig_handle = NULL;
char task_title[NAME_MAX_LENGTH + 16];
task_title[0] = '\0';
if (!task) if (!task)
return; return;
autoconfig_handle = (autoconfig_handle_t*)task->state; if (!(autoconfig_handle = (autoconfig_handle_t*)task->state))
if (!autoconfig_handle)
{ {
task_set_finished(task, true); task_set_finished(task, true);
return; return;
} }
task_free_title(task);
if (!autoconfig_handle->suppress_notifcations)
{
char task_title[NAME_MAX_LENGTH + 16];
/* Set task title */ /* Set task title */
if (!string_is_empty(autoconfig_handle->device_info.name)) if (!string_is_empty(autoconfig_handle->device_info.name))
snprintf(task_title, sizeof(task_title), "%s %u (%s)", snprintf(task_title, sizeof(task_title), "%s %u (%s)",
@ -792,31 +770,25 @@ static void input_autoconfigure_disconnect_handler(retro_task_t *task)
snprintf(task_title, sizeof(task_title), "%s %u", snprintf(task_title, sizeof(task_title), "%s %u",
msg_hash_to_str(MSG_DEVICE_DISCONNECTED_FROM_PORT), msg_hash_to_str(MSG_DEVICE_DISCONNECTED_FROM_PORT),
autoconfig_handle->port + 1); autoconfig_handle->port + 1);
task_free_title(task);
if (!autoconfig_handle->suppress_notifcations)
task_set_title(task, strdup(task_title)); task_set_title(task, strdup(task_title));
}
task_set_finished(task, true); task_set_finished(task, true);
} }
static bool autoconfigure_disconnect_finder(retro_task_t *task, void *user_data) static bool autoconfigure_disconnect_finder(retro_task_t *task, void *user_data)
{
if (task && user_data && task->handler == input_autoconfigure_disconnect_handler)
{ {
autoconfig_handle_t *autoconfig_handle = NULL; autoconfig_handle_t *autoconfig_handle = NULL;
unsigned *port = NULL; if ((autoconfig_handle = (autoconfig_handle_t*)task->state))
{
if (!task || !user_data) unsigned *port = (unsigned*)user_data;
return false;
if (task->handler != input_autoconfigure_disconnect_handler)
return false;
if (!(autoconfig_handle = (autoconfig_handle_t*)task->state))
return false;
port = (unsigned*)user_data;
return (*port == autoconfig_handle->port); return (*port == autoconfig_handle->port);
} }
}
return false;
}
/* Note: There is no real need for autoconfigure /* Note: There is no real need for autoconfigure
* 'disconnect' to be a task - we are merely setting * 'disconnect' to be a task - we are merely setting
@ -862,6 +834,7 @@ bool input_autoconfigure_disconnect(unsigned port, const char *name)
/* Configure task */ /* Configure task */
if (!(task = task_init())) if (!(task = task_init()))
{ {
if (autoconfig_handle)
free_autoconfig_handle(autoconfig_handle); free_autoconfig_handle(autoconfig_handle);
autoconfig_handle = NULL; autoconfig_handle = NULL;
return false; return false;