RetroArch/tasks/task_autodetect.c

538 lines
14 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 13:40:32 +01:00
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
2015-07-10 09:15:55 +02:00
#include <compat/strl.h>
#include <lists/dir_list.h>
2015-07-09 23:12:35 -05:00
#include <file/file_path.h>
2017-04-29 13:20:50 +02:00
#include <file/config_file.h>
2015-12-26 07:54:17 +01:00
#include <string/stdstring.h>
#include "../input/input_driver.h"
2015-07-10 09:15:55 +02:00
2016-02-07 13:25:55 +01:00
#include "../configuration.h"
2016-06-11 21:51:28 +02:00
#include "../file_path_special.h"
#include "../list_special.h"
2015-11-23 12:03:38 +01:00
#include "../verbosity.h"
#include "tasks_internal.h"
2017-05-13 22:30:45 +02:00
typedef struct autoconfig_disconnect autoconfig_disconnect_t;
typedef struct autoconfig_params autoconfig_params_t;
struct autoconfig_disconnect
2016-12-01 22:36:38 +01:00
{
unsigned idx;
2017-09-29 06:16:35 +02:00
char *msg;
2017-05-13 22:30:45 +02:00
};
2016-12-01 22:36:38 +01:00
2017-05-13 22:30:45 +02:00
struct autoconfig_params
2016-12-31 07:43:34 +01:00
{
int32_t vid;
int32_t pid;
2017-05-13 22:30:45 +02:00
unsigned idx;
uint32_t max_users;
2017-09-29 06:16:35 +02:00
char *name;
char *autoconfig_directory;
2017-05-13 22:30:45 +02:00
};
2016-12-31 07:43:34 +01:00
static bool input_autoconfigured[MAX_USERS];
static unsigned input_device_name_index[MAX_USERS];
static bool input_autoconfigure_swap_override;
bool input_autoconfigure_get_swap_override(void)
{
return input_autoconfigure_swap_override;
}
/* Adds an index for devices with the same name,
* so they can be identified in the GUI. */
static void input_autoconfigure_joypad_reindex_devices(autoconfig_params_t *params)
{
2015-11-25 19:27:33 +01:00
unsigned i;
for(i = 0; i < params->max_users; i++)
input_device_name_index[i] = 0;
2015-11-25 19:27:33 +01:00
for(i = 0; i < params->max_users; i++)
{
2015-11-25 19:27:33 +01:00
unsigned j;
2017-04-25 15:53:30 +02:00
const char *tmp = input_config_get_device_name(i);
2015-11-25 19:27:33 +01:00
int k = 1;
for(j = 0; j < params->max_users; j++)
{
2017-04-25 16:36:31 +02:00
if(string_is_equal(tmp, input_config_get_device_name(j))
&& input_device_name_index[i] == 0)
input_device_name_index[j] = k++;
}
}
}
static void input_autoconfigure_joypad_conf(config_file_t *conf,
struct retro_keybind *binds)
{
unsigned i;
for (i = 0; i < RARCH_BIND_LIST_END; i++)
{
input_config_parse_joy_button(conf, "input",
input_config_bind_map_get_base(i), &binds[i]);
input_config_parse_joy_axis(conf, "input",
input_config_bind_map_get_base(i), &binds[i]);
}
}
2016-12-01 18:52:34 +01:00
static int input_autoconfigure_joypad_try_from_conf(config_file_t *conf,
autoconfig_params_t *params)
{
char ident[256];
char input_driver[32];
int tmp_int = 0;
int input_vid = 0;
int input_pid = 0;
int score = 0;
ident[0] = input_driver[0] = '\0';
config_get_array(conf, "input_device", ident, sizeof(ident));
config_get_array(conf, "input_driver", input_driver, sizeof(input_driver));
if (config_get_int (conf, "input_vendor_id", &tmp_int))
input_vid = tmp_int;
if (config_get_int (conf, "input_product_id", &tmp_int))
input_pid = tmp_int;
/* Check for VID/PID */
2015-03-27 17:27:21 +01:00
if ( (params->vid == input_vid)
&& (params->pid == input_pid)
2016-12-01 18:38:11 +01:00
&& (params->vid != 0)
&& (params->pid != 0)
&& (input_vid != 0)
&& (input_pid != 0))
score += 3;
/* Check for name match */
2017-09-30 06:08:09 +02:00
if (!string_is_empty(params->name)
2017-09-29 06:16:35 +02:00
&& string_is_equal(ident, params->name))
score += 2;
2016-09-12 18:39:46 +02:00
return score;
2015-03-27 17:27:21 +01:00
}
2016-02-05 14:06:43 +01:00
static void input_autoconfigure_joypad_add(config_file_t *conf,
autoconfig_params_t *params, retro_task_t *task)
2015-03-27 17:27:21 +01:00
{
2017-04-26 18:48:28 +02:00
char msg[128], display_name[128], device_type[128];
/* This will be the case if input driver is reinitialized.
* No reason to spam autoconfigure messages every time. */
bool block_osd_spam =
input_autoconfigured[params->idx]
2017-09-30 06:08:09 +02:00
&& !string_is_empty(params->name);
2015-03-27 17:34:09 +01:00
msg[0] = display_name[0] = device_type[0] = '\0';
2016-02-05 14:06:43 +01:00
config_get_array(conf, "input_device_display_name",
display_name, sizeof(display_name));
config_get_array(conf, "input_device_type", device_type,
sizeof(device_type));
input_autoconfigured[params->idx] = true;
2017-04-25 15:31:32 +02:00
2015-03-27 17:27:21 +01:00
input_autoconfigure_joypad_conf(conf,
input_autoconf_binds[params->idx]);
if (string_is_equal_fast(device_type, "remote", 6))
2015-07-31 23:09:25 -05:00
{
2017-04-26 18:48:28 +02:00
static bool remote_is_bound = false;
2016-10-22 04:57:46 +02:00
snprintf(msg, sizeof(msg), "%s configured.",
2017-09-29 06:16:35 +02:00
(string_is_empty(display_name) &&
2017-09-30 06:08:09 +02:00
!string_is_empty(params->name)) ? params->name : display_name);
2015-08-01 00:07:28 -05:00
if(!remote_is_bound)
{
task_free_title(task);
task_set_title(task, strdup(msg));
}
2015-07-31 23:09:25 -05:00
remote_is_bound = true;
if (params->idx == 0)
input_autoconfigure_swap_override = true;
2015-07-31 23:09:25 -05:00
}
else
2015-07-31 23:09:25 -05:00
{
bool tmp = false;
2016-10-22 04:57:46 +02:00
snprintf(msg, sizeof(msg), "%s %s #%u.",
2017-09-29 06:16:35 +02:00
(string_is_empty(display_name) &&
2017-09-30 06:08:09 +02:00
!string_is_empty(params->name))
2017-09-29 06:16:35 +02:00
? params->name : display_name,
2016-10-22 04:57:46 +02:00
msg_hash_to_str(MSG_DEVICE_CONFIGURED_IN_PORT),
2016-01-20 04:07:24 +01:00
params->idx);
/* allow overriding the swap menu controls for player 1*/
if (params->idx == 0)
2017-04-25 15:31:32 +02:00
{
if (config_get_bool(conf, "input_swap_override", &tmp))
input_autoconfigure_swap_override = tmp;
else
input_autoconfigure_swap_override = false;
2017-04-25 15:31:32 +02:00
}
2015-08-01 00:07:28 -05:00
if (!block_osd_spam)
{
task_free_title(task);
task_set_title(task, strdup(msg));
}
2015-07-31 23:09:25 -05:00
}
input_autoconfigure_joypad_reindex_devices(params);
}
static int input_autoconfigure_joypad_from_conf(
config_file_t *conf, autoconfig_params_t *params, retro_task_t *task)
{
2016-12-01 18:52:34 +01:00
int ret = input_autoconfigure_joypad_try_from_conf(conf,
params);
if (ret)
input_autoconfigure_joypad_add(conf, params, task);
config_file_free(conf);
return ret;
}
2015-03-27 18:05:43 +01:00
static bool input_autoconfigure_joypad_from_conf_dir(
autoconfig_params_t *params, retro_task_t *task)
{
size_t i;
char path[PATH_MAX_LENGTH];
2015-09-29 17:35:28 +02:00
int ret = 0;
int index = -1;
int current_best = 0;
config_file_t *conf = NULL;
struct string_list *list = NULL;
2015-07-09 23:12:35 -05:00
path[0] = '\0';
2016-06-11 21:51:28 +02:00
fill_pathname_application_special(path, sizeof(path),
APPLICATION_SPECIAL_DIRECTORY_AUTOCONFIG);
2015-09-29 17:35:28 +02:00
list = dir_list_new_special(path, DIR_LIST_AUTOCONFIG, "cfg");
if (!list || !list->size)
2016-05-23 21:28:43 +02:00
{
if (list)
string_list_free(list);
2017-09-30 06:38:53 +02:00
if (params && !string_is_empty(params->autoconfig_directory))
2017-09-29 06:16:35 +02:00
list = dir_list_new_special(params->autoconfig_directory,
DIR_LIST_AUTOCONFIG, "cfg");
2016-05-23 21:28:43 +02:00
}
if(!list)
2015-03-27 18:05:43 +01:00
return false;
2017-06-17 18:32:55 -05:00
RARCH_LOG("[Autoconf]: %d profiles found.\n", list->size);
for (i = 0; i < list->size; i++)
{
conf = config_file_new(list->elems[i].data);
2016-10-04 10:40:37 +02:00
if (conf)
2016-12-01 18:52:34 +01:00
ret = input_autoconfigure_joypad_try_from_conf(conf, params);
2016-09-12 18:39:46 +02:00
2015-07-10 22:36:28 -05:00
if(ret >= current_best)
{
index = (int)i;
current_best = ret;
}
config_file_free(conf);
}
if(index >= 0 && current_best > 0)
{
2015-07-09 22:52:52 -05:00
conf = config_file_new(list->elems[index].data);
2015-11-15 22:28:57 +01:00
if (conf)
{
char conf_path[PATH_MAX_LENGTH];
conf_path[0] = '\0';
2015-11-15 22:28:57 +01:00
config_get_config_path(conf, conf_path, sizeof(conf_path));
2017-06-17 18:32:55 -05:00
RARCH_LOG("[Autoconf]: selected configuration: %s\n", conf_path);
input_autoconfigure_joypad_add(conf, params, task);
2015-11-15 22:28:57 +01:00
config_file_free(conf);
ret = 1;
}
2015-07-09 22:52:52 -05:00
}
else
ret = 0;
string_list_free(list);
2015-03-27 18:05:43 +01:00
if (ret == 0)
return false;
return true;
}
static bool input_autoconfigure_joypad_from_conf_internal(
autoconfig_params_t *params, retro_task_t *task)
{
size_t i;
2015-03-27 17:55:00 +01:00
/* Load internal autoconfig files */
for (i = 0; input_builtin_autoconfs[i]; i++)
{
2015-03-27 17:47:15 +01:00
config_file_t *conf = config_file_new_from_string(
input_builtin_autoconfs[i]);
if (conf && input_autoconfigure_joypad_from_conf(conf, params, task))
return true;
}
2017-09-29 06:16:35 +02:00
if (!params->autoconfig_directory ||
string_is_empty(params->autoconfig_directory))
return true;
return false;
}
2017-09-29 06:16:35 +02:00
static void input_autoconfigure_params_free(autoconfig_params_t *params)
{
if (!params)
return;
2017-09-30 06:08:09 +02:00
if (!string_is_empty(params->name))
2017-09-29 06:16:35 +02:00
free(params->name);
2017-09-30 06:08:09 +02:00
if (!string_is_empty(params->autoconfig_directory))
2017-09-29 06:16:35 +02:00
free(params->autoconfig_directory);
params->name = NULL;
params->autoconfig_directory = NULL;
}
static void input_autoconfigure_connect_handler(retro_task_t *task)
2015-03-27 18:05:43 +01:00
{
2016-12-03 06:09:55 +01:00
autoconfig_params_t *params = (autoconfig_params_t*)task->state;
if (!params || string_is_empty(params->name))
2017-01-03 03:52:08 +01:00
goto end;
if ( !input_autoconfigure_joypad_from_conf_dir(params, task)
&& !input_autoconfigure_joypad_from_conf_internal(params, task))
2016-12-01 20:41:41 +01:00
{
char msg[255];
2016-12-01 20:41:41 +01:00
msg[0] = '\0';
2017-05-13 23:41:13 +02:00
#ifdef ANDROID
2017-09-30 06:38:53 +02:00
if (!string_is_empty(params->name))
free(params->name);
2017-09-29 06:16:35 +02:00
params->name = strdup("Android Gamepad");
2017-02-06 22:58:02 -05:00
if(input_autoconfigure_joypad_from_conf_internal(params, task))
2017-02-05 14:24:34 -05:00
{
2017-06-17 18:32:55 -05:00
RARCH_LOG("[Autoconf]: no profiles found for %s (%d/%d). Using fallback\n",
2017-09-30 06:29:07 +02:00
!string_is_empty(params->name) ? params->name : "N/A",
params->vid, params->pid);
2017-02-05 14:24:34 -05:00
snprintf(msg, sizeof(msg), "%s (%ld/%ld) %s.",
2017-09-30 06:29:07 +02:00
!string_is_empty(params->name) ? params->name : "N/A",
(long)params->vid, (long)params->pid,
2017-02-05 14:24:34 -05:00
msg_hash_to_str(MSG_DEVICE_NOT_CONFIGURED_FALLBACK));
}
2017-05-13 23:41:13 +02:00
#else
2017-06-17 18:32:55 -05:00
RARCH_LOG("[Autoconf]: no profiles found for %s (%d/%d).\n",
2017-09-30 06:29:07 +02:00
!string_is_empty(params->name) ? params->name : "N/A",
params->vid, params->pid);
2017-05-13 23:41:13 +02:00
snprintf(msg, sizeof(msg), "%s (%ld/%ld) %s.",
2017-09-30 06:29:07 +02:00
!string_is_empty(params->name) ? params->name : "N/A",
(long)params->vid, (long)params->pid,
2017-05-13 23:41:13 +02:00
msg_hash_to_str(MSG_DEVICE_NOT_CONFIGURED));
2017-02-05 14:24:34 -05:00
#endif
task_free_title(task);
task_set_title(task, strdup(msg));
2016-12-01 20:41:41 +01:00
}
2017-01-03 03:52:08 +01:00
end:
2017-09-29 06:16:35 +02:00
if (params)
{
input_autoconfigure_params_free(params);
free(params);
}
task_set_finished(task, true);
2016-12-01 22:36:38 +01:00
}
2016-12-01 22:36:38 +01:00
static void input_autoconfigure_disconnect_handler(retro_task_t *task)
{
2016-12-03 06:09:55 +01:00
autoconfig_disconnect_t *params = (autoconfig_disconnect_t*)task->state;
2016-12-01 22:36:38 +01:00
task_set_title(task, strdup(params->msg));
task_set_finished(task, true);
2016-12-01 22:36:38 +01:00
RARCH_LOG("%s: %s\n", msg_hash_to_str(MSG_AUTODETECT), params->msg);
2016-12-04 05:50:50 +01:00
2017-09-30 06:08:09 +02:00
if (!string_is_empty(params->msg))
2017-09-29 06:16:35 +02:00
free(params->msg);
2016-12-04 05:50:50 +01:00
free(params);
}
2016-12-01 22:36:38 +01:00
bool input_autoconfigure_disconnect(unsigned i, const char *ident)
{
2016-10-27 09:32:07 +02:00
char msg[255];
retro_task_t *task = (retro_task_t*)calloc(1, sizeof(*task));
2017-09-29 06:16:35 +02:00
autoconfig_disconnect_t *state = (autoconfig_disconnect_t*)malloc(sizeof(*state));
2016-12-21 01:45:19 +01:00
if (!state || !task)
goto error;
msg[0] = '\0';
2017-09-29 06:16:35 +02:00
state->msg = NULL;
state->idx = i;
2016-10-22 04:57:46 +02:00
snprintf(msg, sizeof(msg), "%s #%u (%s).",
msg_hash_to_str(MSG_DEVICE_DISCONNECTED_FROM_PORT),
i, ident);
2016-12-01 22:36:38 +01:00
2017-09-29 06:16:35 +02:00
state->msg = strdup(msg);
2016-12-01 22:36:38 +01:00
2017-04-25 15:53:30 +02:00
input_config_clear_device_name(state->idx);
2016-12-01 22:36:38 +01:00
task->state = state;
task->handler = input_autoconfigure_disconnect_handler;
2017-05-14 20:43:39 +02:00
task_queue_push(task);
2016-12-01 22:36:38 +01:00
return true;
error:
if (state)
2017-09-29 06:16:35 +02:00
{
2017-09-30 06:08:09 +02:00
if (!string_is_empty(state->msg))
2017-09-29 06:16:35 +02:00
free(state->msg);
2016-12-01 22:36:38 +01:00
free(state);
2017-09-29 06:16:35 +02:00
}
2016-12-01 22:36:38 +01:00
if (task)
free(task);
return false;
}
void input_autoconfigure_reset(void)
{
unsigned i, j;
for (i = 0; i < MAX_USERS; i++)
{
for (j = 0; j < RARCH_BIND_LIST_END; j++)
{
input_autoconf_binds[i][j].joykey = NO_BTN;
input_autoconf_binds[i][j].joyaxis = AXIS_NONE;
}
input_device_name_index[i] = 0;
input_autoconfigured[i] = 0;
}
input_autoconfigure_swap_override = false;
}
bool input_is_autoconfigured(unsigned i)
{
return input_autoconfigured[i];
}
unsigned input_autoconfigure_get_device_name_index(unsigned i)
{
return input_device_name_index[i];
}
2016-12-31 07:43:34 +01:00
bool input_autoconfigure_connect(
const char *name,
const char *display_name,
const char *driver,
unsigned idx,
unsigned vid,
unsigned pid)
{
unsigned i;
retro_task_t *task = (retro_task_t*)calloc(1, sizeof(*task));
2017-09-29 06:16:35 +02:00
autoconfig_params_t *state = (autoconfig_params_t*)malloc(sizeof(*state));
settings_t *settings = config_get_ptr();
2017-04-29 00:39:29 +02:00
const char *dir_autoconf = settings->paths.directory_autoconfig;
2017-04-28 13:43:47 +02:00
if (!task || !state || !settings->bools.input_autodetect_enable)
goto error;
2017-09-29 17:06:38 +02:00
state->name = NULL;
state->autoconfig_directory = NULL;
2016-12-31 07:43:34 +01:00
if (!string_is_empty(name))
2017-09-29 17:06:38 +02:00
state->name = strdup(name);
2016-12-31 07:43:34 +01:00
if (!string_is_empty(dir_autoconf))
2017-09-29 06:16:35 +02:00
state->autoconfig_directory = strdup(dir_autoconf);
2017-09-29 17:06:38 +02:00
state->idx = idx;
state->vid = vid;
state->pid = pid;
state->max_users = *(
input_driver_get_uint(INPUT_ACTION_MAX_USERS));
2017-09-30 06:38:53 +02:00
if (!string_is_empty(state->name))
input_config_set_device_name(state->idx, state->name);
input_config_set_pid(state->idx, state->pid);
input_config_set_vid(state->idx, state->vid);
for (i = 0; i < RARCH_BIND_LIST_END; i++)
{
input_autoconf_binds[state->idx][i].joykey = NO_BTN;
input_autoconf_binds[state->idx][i].joyaxis = AXIS_NONE;
2017-09-30 06:08:09 +02:00
if (
!string_is_empty(input_autoconf_binds[state->idx][i].joykey_label))
2017-09-27 20:08:37 +02:00
free(input_autoconf_binds[state->idx][i].joykey_label);
2017-09-30 06:08:09 +02:00
if (
!string_is_empty(input_autoconf_binds[state->idx][i].joyaxis_label))
2017-09-27 20:11:44 +02:00
free(input_autoconf_binds[state->idx][i].joyaxis_label);
input_autoconf_binds[state->idx][i].joykey_label = NULL;
input_autoconf_binds[state->idx][i].joyaxis_label = NULL;
}
2017-04-25 15:31:32 +02:00
input_autoconfigured[state->idx] = false;
2016-12-01 22:17:36 +01:00
task->state = state;
task->handler = input_autoconfigure_connect_handler;
2017-05-14 20:43:39 +02:00
task_queue_push(task);
return true;
error:
if (state)
2017-09-29 06:16:35 +02:00
{
input_autoconfigure_params_free(state);
free(state);
2017-09-29 06:16:35 +02:00
}
if (task)
free(task);
return false;
}