RetroArch/input/input_autodetect.c

361 lines
9.7 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2016-01-10 03:33:01 +00:00
* Copyright (C) 2011-2016 - 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 07:15:55 +00:00
#include <lists/dir_list.h>
2015-07-10 04:12:35 +00:00
#include <file/file_path.h>
2015-12-26 06:54:17 +00:00
#include <string/stdstring.h>
#include "input_config.h"
2015-07-10 07:15:55 +00:00
#include "input_autodetect.h"
2016-02-07 12:25:55 +00:00
#include "../configuration.h"
#include "../list_special.h"
2016-02-07 12:25:55 +00:00
#include "../runloop.h"
2015-11-23 11:03:38 +00:00
#include "../verbosity.h"
/* Adds an index for devices with the same name,
* so they can be identified in the GUI. */
2016-01-20 12:28:13 +00:00
static void input_reindex_devices(void)
{
2015-11-25 18:27:33 +00:00
unsigned i;
settings_t *settings = config_get_ptr();
2015-11-25 18:27:33 +00:00
2016-05-01 18:50:26 +00:00
for(i = 0; i < settings->input.max_users; i++)
settings->input.device_name_index[i]=0;
2015-11-25 18:27:33 +00:00
2016-05-01 18:50:26 +00:00
for(i = 0; i < settings->input.max_users; i++)
{
2015-11-25 18:27:33 +00:00
unsigned j;
const char *tmp = settings->input.device_names[i];
2015-11-25 18:27:33 +00:00
int k = 1;
2015-11-25 18:27:33 +00:00
for(j = 0; j < settings->input.max_users; j++)
{
2016-01-20 03:07:24 +00:00
if(string_is_equal(tmp, settings->input.device_names[j])
&& settings->input.device_name_index[i] == 0)
settings->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]);
}
}
static int input_try_autoconfigure_joypad_from_conf(config_file_t *conf,
autoconfig_params_t *params)
{
2015-06-12 21:52:52 +00:00
char ident[PATH_MAX_LENGTH] = {0};
char input_driver[PATH_MAX_LENGTH] = {0};
int input_vid = 0;
int input_pid = 0;
int score = 0;
2015-01-09 17:04:29 +00:00
if (!conf)
return false;
config_get_array(conf, "input_device", ident, sizeof(ident));
config_get_array(conf, "input_driver", input_driver, sizeof(input_driver));
2015-03-27 16:27:21 +00:00
config_get_int (conf, "input_vendor_id", &input_vid);
config_get_int (conf, "input_product_id", &input_pid);
/* Check for VID/PID */
2015-03-27 16:27:21 +00:00
if ( (params->vid == input_vid)
&& (params->pid == input_pid)
&& params->vid != 0
&& params->pid != 0
&& input_vid != 0
&& input_pid != 0)
{
score += 3;
2015-07-12 06:08:27 +00:00
#if 0
2015-08-28 02:35:05 +00:00
RARCH_LOG("Autodetect: VID/PID match score=%d\n", score);
2015-07-12 06:08:27 +00:00
#endif
}
/* Check for name match */
2016-01-20 03:07:24 +00:00
if (string_is_equal(ident, params->name))
{
score += 2;
2015-07-12 06:08:27 +00:00
#if 0
2015-08-28 02:35:05 +00:00
RARCH_LOG("Autodetect: exact name match score=%d\n", score);
2015-07-12 06:08:27 +00:00
#endif
}
else
{
2016-02-05 13:06:43 +00:00
if (!string_is_empty(ident)
&& !strncmp(params->name, ident, strlen(ident)))
{
score += 1;
2015-07-12 06:08:27 +00:00
#if 0
2015-08-28 02:35:05 +00:00
RARCH_LOG("Autodetect: partial name match score=%d\n", score);
2015-07-12 06:08:27 +00:00
#endif
}
}
2015-10-22 02:46:05 +00:00
#if 0
2016-02-05 13:06:43 +00:00
RARCH_LOG("Autodetect: configuration file: %s score: %d\n",
conf->path, score);
2015-10-22 02:46:05 +00:00
#endif
return score;
2015-03-27 16:27:21 +00:00
}
2016-02-05 13:06:43 +00:00
static void input_autoconfigure_joypad_add(config_file_t *conf,
autoconfig_params_t *params)
2015-03-27 16:27:21 +00:00
{
2015-10-12 19:02:07 +00:00
bool block_osd_spam;
static bool remote_is_bound = false;
char msg[PATH_MAX_LENGTH] = {0};
2015-08-01 04:09:25 +00:00
char display_name[PATH_MAX_LENGTH] = {0};
char device_type[PATH_MAX_LENGTH] = {0};
settings_t *settings = config_get_ptr();
2015-03-27 16:34:09 +00:00
2016-02-05 13:06:43 +00: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));
2015-03-27 16:27:21 +00:00
if (!settings)
return;
2015-08-01 04:40:30 +00:00
2015-10-12 19:02:07 +00:00
/* This will be the case if input driver is reinitialized.
* No reason to spam autoconfigure messages every time. */
block_osd_spam = settings->input.autoconfigured[params->idx]
&& *params->name;
2015-03-27 15:57:58 +00:00
settings->input.autoconfigured[params->idx] = true;
2015-03-27 16:27:21 +00:00
input_autoconfigure_joypad_conf(conf,
settings->input.autoconf_binds[params->idx]);
2016-01-20 03:07:24 +00:00
if (string_is_equal(device_type,"remote"))
2015-08-01 04:09:25 +00:00
{
2016-01-20 03:07:24 +00:00
snprintf(msg, sizeof(msg), "%s configured",
string_is_empty(display_name) ? params->name : display_name);
2015-08-01 05:07:28 +00:00
if(!remote_is_bound)
2015-12-07 14:32:14 +00:00
runloop_msg_queue_push(msg, 0, 60, false);
2015-08-01 04:09:25 +00:00
remote_is_bound = true;
}
else
2015-08-01 04:09:25 +00:00
{
2016-01-20 03:07:24 +00:00
snprintf(msg, sizeof(msg), "%s configured in port #%u.",
string_is_empty(display_name) ? params->name : display_name,
params->idx);
2015-08-01 05:07:28 +00:00
if (!block_osd_spam)
2015-12-07 14:32:14 +00:00
runloop_msg_queue_push(msg, 0, 60, false);
2015-08-01 04:09:25 +00:00
}
input_reindex_devices();
2015-10-22 02:46:05 +00:00
#if 0
2015-08-28 02:35:05 +00:00
RARCH_LOG("Autodetect: %s\n", msg);
2015-10-22 02:46:05 +00:00
#endif
}
2015-07-10 05:16:21 +00:00
#if defined(HAVE_BUILTIN_AUTOCONFIG)
static int input_autoconfigure_joypad_from_conf(
config_file_t *conf, autoconfig_params_t *params)
{
int ret = 0;
if (!conf)
return false;
ret = input_try_autoconfigure_joypad_from_conf(conf,
params);
if (ret)
input_autoconfigure_joypad_add(conf, params);
config_file_free(conf);
return ret;
}
2015-07-10 04:59:14 +00:00
#endif
2015-03-27 17:05:43 +00:00
static bool input_autoconfigure_joypad_from_conf_dir(
autoconfig_params_t *params)
{
size_t i;
2015-09-29 15:35:28 +00:00
char path[PATH_MAX_LENGTH];
int ret = 0;
int index = -1;
int current_best = 0;
config_file_t *conf = NULL;
struct string_list *list = NULL;
settings_t *settings = config_get_ptr();
2015-07-10 04:12:35 +00:00
2015-09-29 15:35:28 +00:00
if (!settings)
return false;
fill_pathname_join(path,
2016-04-28 18:49:13 +00:00
settings->directory.autoconfig,
settings->input.joypad_driver,
sizeof(path));
list = dir_list_new_special(path, DIR_LIST_AUTOCONFIG, "cfg");
if (!list || !list->size)
list = dir_list_new_special(settings->directory.autoconfig,
DIR_LIST_AUTOCONFIG, "cfg");
if(!list)
2015-03-27 17:05:43 +00:00
return false;
2015-08-28 02:35:05 +00:00
RARCH_LOG("Autodetect: %d profiles found\n", list->size);
for (i = 0; i < list->size; i++)
{
conf = config_file_new(list->elems[i].data);
ret = input_try_autoconfigure_joypad_from_conf(conf, params);
2015-07-11 03:36:28 +00:00
if(ret >= current_best)
{
index = i;
current_best = ret;
}
config_file_free(conf);
}
if(index >= 0 && current_best > 0)
{
2015-07-10 03:52:52 +00:00
conf = config_file_new(list->elems[index].data);
2015-11-15 21:28:57 +00:00
if (conf)
{
char conf_path[PATH_MAX_LENGTH];
config_get_config_path(conf, conf_path, sizeof(conf_path));
RARCH_LOG("Autodetect: selected configuration: %s\n", conf_path);
input_autoconfigure_joypad_add(conf, params);
config_file_free(conf);
ret = 1;
}
2015-07-10 03:52:52 +00:00
}
else
ret = 0;
string_list_free(list);
2015-03-27 17:05:43 +00:00
if (ret == 0)
return false;
return true;
}
#if defined(HAVE_BUILTIN_AUTOCONFIG)
static bool input_autoconfigure_joypad_from_conf_internal(
autoconfig_params_t *params)
{
size_t i;
settings_t *settings = config_get_ptr();
bool ret = false;
2015-03-27 16:55:00 +00:00
/* Load internal autoconfig files */
for (i = 0; input_builtin_autoconfs[i]; i++)
{
2015-03-27 16:47:15 +00:00
config_file_t *conf = config_file_new_from_string(
input_builtin_autoconfs[i]);
if ((ret = input_autoconfigure_joypad_from_conf(conf, params)))
break;
}
2016-04-28 18:52:30 +00:00
if (ret || !*settings->directory.autoconfig)
return true;
return false;
}
#endif
2015-03-27 17:05:43 +00:00
static bool input_config_autoconfigure_joypad_init(autoconfig_params_t *params)
{
size_t i;
settings_t *settings = config_get_ptr();
if (!settings || !settings->input.autodetect_enable)
2015-03-27 17:05:43 +00:00
return false;
for (i = 0; i < RARCH_BIND_LIST_END; i++)
{
2015-12-26 06:54:17 +00:00
settings->input.autoconf_binds[params->idx][i].joykey = NO_BTN;
settings->input.autoconf_binds[params->idx][i].joyaxis = AXIS_NONE;
settings->input.autoconf_binds[params->idx][i].joykey_label[0] = '\0';
settings->input.autoconf_binds[params->idx][i].joyaxis_label[0] = '\0';
}
settings->input.autoconfigured[params->idx] = false;
2015-03-27 17:05:43 +00:00
return true;
}
bool input_config_autoconfigure_joypad(autoconfig_params_t *params)
2015-03-27 17:05:43 +00:00
{
char msg[PATH_MAX_LENGTH];
2015-03-27 17:05:43 +00:00
if (!input_config_autoconfigure_joypad_init(params))
goto error;
2015-03-27 17:05:43 +00:00
2015-03-28 21:20:05 +00:00
if (!*params->name)
goto error;
if (input_autoconfigure_joypad_from_conf_dir(params))
2016-05-06 02:51:00 +00:00
return true;
#if defined(HAVE_BUILTIN_AUTOCONFIG)
if (input_autoconfigure_joypad_from_conf_internal(params))
2016-05-06 02:51:00 +00:00
return true;
#endif
RARCH_LOG("Autodetect: no profiles found for %s (%d/%d)\n",
params->name, params->vid, params->pid);
snprintf(msg, sizeof(msg), "%s (%ld/%ld) not configured",
params->name, (long)params->vid, (long)params->pid);
runloop_msg_queue_push(msg, 0, 60, false);
error:
return false;
}
const struct retro_keybind *input_get_auto_bind(unsigned port, unsigned id)
{
2015-03-20 20:22:38 +00:00
settings_t *settings = config_get_ptr();
2015-03-27 16:34:09 +00:00
unsigned joy_idx = 0;
2015-03-27 16:34:09 +00:00
if (settings)
joy_idx = settings->input.joypad_map[port];
2015-01-05 00:58:00 +00:00
if (joy_idx < MAX_USERS)
2015-03-20 20:22:38 +00:00
return &settings->input.autoconf_binds[joy_idx][id];
return NULL;
}
void input_config_autoconfigure_disconnect(unsigned i, const char *ident)
{
2015-12-07 14:32:14 +00:00
char msg[PATH_MAX_LENGTH];
snprintf(msg, sizeof(msg), "Device #%u (%s) disconnected.", i, ident);
2015-12-07 14:32:14 +00:00
runloop_msg_queue_push(msg, 0, 60, false);
2015-08-28 02:35:05 +00:00
RARCH_LOG("Autodetect: %s\n", msg);
}