2014-09-30 15:48:31 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2015-01-07 16:46:50 +00:00
|
|
|
* Copyright (C) 2011-2015 - Daniel De Matteis
|
2014-09-30 15:48:31 +00:00
|
|
|
*
|
|
|
|
* 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 <file/dir_list.h>
|
2015-07-10 04:12:35 +00:00
|
|
|
#include <file/file_path.h>
|
2014-09-30 15:48:31 +00:00
|
|
|
|
2015-07-10 07:15:55 +00:00
|
|
|
#include "input_common.h"
|
|
|
|
#include "input_autodetect.h"
|
|
|
|
|
2014-09-30 15:48:31 +00:00
|
|
|
#include "../general.h"
|
|
|
|
|
2015-07-10 07:06:00 +00:00
|
|
|
#if 0
|
|
|
|
enum
|
2015-04-02 23:22:55 +00:00
|
|
|
{
|
|
|
|
AUTODETECT_MATCH_NONE = 0,
|
|
|
|
AUTODETECT_MATCH_VID,
|
|
|
|
AUTODETECT_MATCH_PID,
|
|
|
|
AUTODETECT_MATCH_IDENT,
|
|
|
|
AUTODETECT_MATCH_DRIVER,
|
2015-06-26 16:35:35 +00:00
|
|
|
AUTODETECT_MATCH_NAME
|
2015-07-10 07:06:00 +00:00
|
|
|
};
|
|
|
|
#endif
|
2015-04-02 23:22:55 +00:00
|
|
|
|
2014-09-30 15:48:31 +00:00
|
|
|
static void input_autoconfigure_joypad_conf(config_file_t *conf,
|
|
|
|
struct retro_keybind *binds)
|
|
|
|
{
|
|
|
|
unsigned i;
|
2015-01-09 23:06:49 +00:00
|
|
|
|
2014-09-30 15:48:31 +00:00
|
|
|
for (i = 0; i < RARCH_BIND_LIST_END; i++)
|
|
|
|
{
|
|
|
|
input_config_parse_joy_button(conf, "input",
|
|
|
|
input_config_bind_map[i].base, &binds[i]);
|
|
|
|
input_config_parse_joy_axis(conf, "input",
|
|
|
|
input_config_bind_map[i].base, &binds[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 21:28:07 +00:00
|
|
|
static int input_try_autoconfigure_joypad_from_conf(config_file_t *conf,
|
2015-07-10 03:46:28 +00:00
|
|
|
autoconfig_params_t *params)
|
2014-09-30 15:48:31 +00:00
|
|
|
{
|
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;
|
2015-07-10 07:06:00 +00:00
|
|
|
int score = 0;
|
2014-09-30 15:48:31 +00:00
|
|
|
|
2015-01-09 17:04:29 +00:00
|
|
|
if (!conf)
|
|
|
|
return false;
|
|
|
|
|
2014-09-30 15:48:31 +00:00
|
|
|
*ident = *input_driver = '\0';
|
|
|
|
|
|
|
|
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);
|
2014-09-30 15:48:31 +00:00
|
|
|
|
2015-07-10 03:46:28 +00:00
|
|
|
/* 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)
|
2015-03-27 17:50:40 +00:00
|
|
|
{
|
2015-07-10 03:46:28 +00:00
|
|
|
score += 3;
|
2015-07-10 03:54:49 +00:00
|
|
|
//RARCH_LOG("Autoconf: VID/PID match score=%d\n", score);
|
2015-03-27 17:50:40 +00:00
|
|
|
}
|
2015-07-03 02:48:06 +00:00
|
|
|
|
2015-04-30 21:28:07 +00:00
|
|
|
/* Check for name match */
|
|
|
|
if (!strcmp(ident, params->name))
|
|
|
|
{
|
2015-07-10 03:46:28 +00:00
|
|
|
score += 2;
|
2015-07-10 03:54:49 +00:00
|
|
|
//RARCH_LOG("Autoconf: exact name match score=%d\n", score);
|
2015-07-03 02:48:06 +00:00
|
|
|
}
|
2015-07-10 03:46:28 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ident[0] != '\0' && !strncmp(params->name, ident, strlen(ident)))
|
|
|
|
{
|
|
|
|
score += 1;
|
2015-07-10 07:06:00 +00:00
|
|
|
//RARCH_LOG("Autoconf: partial name match score=%d\n", score);
|
|
|
|
}
|
2015-07-10 03:46:28 +00:00
|
|
|
}
|
|
|
|
RARCH_LOG("Autoconf: configuration score=%d\n", score);
|
|
|
|
return score;
|
2015-03-27 16:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void input_autoconfigure_joypad_add(
|
|
|
|
config_file_t *conf,
|
2015-03-27 16:31:59 +00:00
|
|
|
autoconfig_params_t *params)
|
2015-03-27 16:27:21 +00:00
|
|
|
{
|
2015-06-12 21:52:52 +00:00
|
|
|
char msg[PATH_MAX_LENGTH] = {0};
|
|
|
|
settings_t *settings = config_get_ptr();
|
2015-03-27 16:34:09 +00:00
|
|
|
|
|
|
|
/* This will be the case if input driver is reinitialized.
|
|
|
|
* No reason to spam autoconfigure messages every time. */
|
2015-07-03 02:48:06 +00:00
|
|
|
bool block_osd_spam = settings &&
|
2015-03-28 21:20:05 +00:00
|
|
|
settings->input.autoconfigured[params->idx] && *params->name;
|
2015-03-27 16:27:21 +00:00
|
|
|
|
|
|
|
if (!settings)
|
|
|
|
return;
|
2014-09-30 15:48:31 +00:00
|
|
|
|
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]);
|
2014-09-30 15:48:31 +00:00
|
|
|
|
2015-04-14 03:36:43 +00:00
|
|
|
snprintf(msg, sizeof(msg), "Device port #%u (%s) configured.",
|
2015-03-27 15:57:58 +00:00
|
|
|
params->idx, params->name);
|
2014-09-30 15:48:31 +00:00
|
|
|
|
|
|
|
if (!block_osd_spam)
|
2015-03-15 01:47:23 +00:00
|
|
|
rarch_main_msg_queue_push(msg, 0, 60, false);
|
2014-09-30 15:48:31 +00:00
|
|
|
RARCH_LOG("%s\n", msg);
|
|
|
|
}
|
|
|
|
|
2015-07-10 05:16:21 +00:00
|
|
|
#if defined(HAVE_BUILTIN_AUTOCONFIG)
|
2015-05-08 15:25:55 +00:00
|
|
|
static int input_autoconfigure_joypad_from_conf(
|
|
|
|
config_file_t *conf, autoconfig_params_t *params)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!conf)
|
|
|
|
return false;
|
2015-07-03 02:48:06 +00:00
|
|
|
|
2015-05-08 15:25:55 +00:00
|
|
|
ret = input_try_autoconfigure_joypad_from_conf(conf,
|
2015-07-10 03:46:28 +00:00
|
|
|
params);
|
2015-05-08 15:25:55 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
input_autoconfigure_joypad_add(conf, params);
|
|
|
|
|
|
|
|
config_file_free(conf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2015-07-10 04:59:14 +00:00
|
|
|
#endif
|
2015-05-08 15:25:55 +00:00
|
|
|
|
2015-03-27 17:05:43 +00:00
|
|
|
static bool input_autoconfigure_joypad_from_conf_dir(
|
2015-03-27 17:02:21 +00:00
|
|
|
autoconfig_params_t *params)
|
2014-09-30 15:48:31 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
2015-07-10 07:06:00 +00:00
|
|
|
int ret = 0;
|
|
|
|
int index = 0;
|
2015-07-10 03:52:52 +00:00
|
|
|
int current_best = 0;
|
2015-07-10 07:06:00 +00:00
|
|
|
config_file_t *conf = NULL;
|
|
|
|
struct string_list *list = NULL;
|
2015-07-10 04:12:35 +00:00
|
|
|
char path[PATH_MAX_LENGTH] = {0};
|
2015-07-10 07:06:00 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2015-07-10 04:12:35 +00:00
|
|
|
|
2015-07-10 07:06:00 +00:00
|
|
|
fill_pathname_join(path,settings->input.autoconfig_dir,
|
|
|
|
settings->input.joypad_driver,
|
|
|
|
sizeof(path));
|
|
|
|
|
|
|
|
if (settings)
|
|
|
|
list = dir_list_new(settings->input.autoconfig_dir, "cfg", false);
|
2014-09-30 15:48:31 +00:00
|
|
|
|
2015-07-10 04:51:39 +00:00
|
|
|
if (!list || !list->size)
|
|
|
|
{
|
2015-07-10 07:06:00 +00:00
|
|
|
fill_pathname_join(path,settings->input.autoconfig_dir,
|
|
|
|
settings->input.joypad_driver,
|
|
|
|
sizeof(path));
|
2015-07-10 04:51:39 +00:00
|
|
|
|
2015-07-10 07:06:00 +00:00
|
|
|
if (settings)
|
|
|
|
list = dir_list_new(path, "cfg", false);
|
2015-07-10 04:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!list)
|
2015-03-27 17:05:43 +00:00
|
|
|
return false;
|
2014-09-30 15:48:31 +00:00
|
|
|
|
2015-07-10 04:51:39 +00:00
|
|
|
RARCH_LOG("Autoconfig: %d profiles found\n", list->size);
|
2015-03-27 17:02:21 +00:00
|
|
|
|
2015-07-10 03:46:28 +00:00
|
|
|
for (i = 0; i < list->size; i++)
|
|
|
|
{
|
|
|
|
conf = config_file_new(list->elems[i].data);
|
|
|
|
ret = input_try_autoconfigure_joypad_from_conf(conf, params);
|
|
|
|
if(ret > current_best)
|
|
|
|
{
|
2015-07-10 07:06:00 +00:00
|
|
|
index = i;
|
|
|
|
current_best = ret;
|
2015-07-10 03:46:28 +00:00
|
|
|
}
|
|
|
|
config_file_free(conf);
|
2014-09-30 15:48:31 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 03:52:52 +00:00
|
|
|
if(index)
|
|
|
|
{
|
2015-07-10 03:54:49 +00:00
|
|
|
RARCH_LOG("Autoconf: best configuration score=%d\n", current_best);
|
2015-07-10 03:52:52 +00:00
|
|
|
conf = config_file_new(list->elems[index].data);
|
|
|
|
input_autoconfigure_joypad_add(conf, params);
|
|
|
|
config_file_free(conf);
|
|
|
|
}
|
|
|
|
else
|
2015-07-10 07:06:00 +00:00
|
|
|
ret = 0;
|
2015-07-10 03:46:28 +00:00
|
|
|
|
2015-03-27 17:02:21 +00:00
|
|
|
string_list_free(list);
|
2015-03-27 17:05:43 +00:00
|
|
|
|
2015-07-10 07:06:00 +00:00
|
|
|
if (ret == 0)
|
|
|
|
return false;
|
|
|
|
return true;
|
2015-03-27 17:02:21 +00:00
|
|
|
}
|
2014-09-30 15:48:31 +00:00
|
|
|
|
2015-01-09 00:10:51 +00:00
|
|
|
#if defined(HAVE_BUILTIN_AUTOCONFIG)
|
2015-03-27 17:02:21 +00:00
|
|
|
static bool input_autoconfigure_joypad_from_conf_internal(
|
|
|
|
autoconfig_params_t *params)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
settings_t *settings = config_get_ptr();
|
2015-07-10 07:06:00 +00:00
|
|
|
bool ret = false;
|
2015-03-27 17:02:21 +00:00
|
|
|
|
2015-03-27 16:55:00 +00:00
|
|
|
/* Load internal autoconfig files */
|
2014-09-30 15:48:31 +00:00
|
|
|
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]);
|
2015-05-08 15:25:55 +00:00
|
|
|
|
|
|
|
if ((ret = input_autoconfigure_joypad_from_conf(conf, params)))
|
|
|
|
break;
|
2014-09-30 15:48:31 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 16:47:15 +00:00
|
|
|
if (ret || !*settings->input.autoconfig_dir)
|
2015-03-27 17:02:21 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2015-01-09 23:06:49 +00:00
|
|
|
|
2015-03-27 17:05:43 +00:00
|
|
|
static bool input_config_autoconfigure_joypad_init(autoconfig_params_t *params)
|
2015-03-27 17:02:21 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
settings_t *settings = config_get_ptr();
|
2015-01-09 23:06:49 +00:00
|
|
|
|
2015-03-27 17:02:21 +00:00
|
|
|
if (!settings || !settings->input.autodetect_enable)
|
2015-03-27 17:05:43 +00:00
|
|
|
return false;
|
2015-01-09 23:06:49 +00:00
|
|
|
|
2015-03-27 17:02:21 +00:00
|
|
|
for (i = 0; i < RARCH_BIND_LIST_END; i++)
|
2014-09-30 15:48:31 +00:00
|
|
|
{
|
2015-03-27 17:02:21 +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';
|
2014-09-30 15:48:31 +00:00
|
|
|
}
|
2015-03-27 17:02:21 +00:00
|
|
|
settings->input.autoconfigured[params->idx] = false;
|
2015-01-09 23:06:49 +00:00
|
|
|
|
2015-03-27 17:05:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-03 02:48:06 +00:00
|
|
|
bool input_config_autoconfigure_joypad(autoconfig_params_t *params)
|
2015-03-27 17:05:43 +00:00
|
|
|
{
|
|
|
|
bool ret = false;
|
2015-07-03 02:48:06 +00:00
|
|
|
|
2015-03-27 17:05:43 +00:00
|
|
|
if (!input_config_autoconfigure_joypad_init(params))
|
2015-07-03 02:48:06 +00:00
|
|
|
return ret;
|
2015-03-27 17:05:43 +00:00
|
|
|
|
2015-03-28 21:20:05 +00:00
|
|
|
if (!*params->name)
|
2015-07-03 02:48:06 +00:00
|
|
|
return ret;
|
2015-03-27 17:02:21 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_BUILTIN_AUTOCONFIG)
|
|
|
|
ret = input_autoconfigure_joypad_from_conf_internal(params);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!ret)
|
2015-03-27 17:05:43 +00:00
|
|
|
ret = input_autoconfigure_joypad_from_conf_dir(params);
|
2015-07-03 02:48:06 +00:00
|
|
|
|
|
|
|
return ret;
|
2014-09-30 15:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-07-03 02:48:06 +00:00
|
|
|
|
2015-03-27 16:34:09 +00:00
|
|
|
if (settings)
|
|
|
|
joy_idx = settings->input.joypad_map[port];
|
2015-01-09 23:06:49 +00:00
|
|
|
|
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];
|
2015-01-09 23:06:49 +00:00
|
|
|
return NULL;
|
2014-09-30 15:48:31 +00:00
|
|
|
}
|
2015-06-03 16:55:04 +00:00
|
|
|
|
|
|
|
void input_config_autoconfigure_disconnect(unsigned i, const char *ident)
|
|
|
|
{
|
2015-06-12 21:52:52 +00:00
|
|
|
char msg[PATH_MAX_LENGTH] = {0};
|
2015-07-10 07:06:00 +00:00
|
|
|
|
2015-06-03 16:55:04 +00:00
|
|
|
snprintf(msg, sizeof(msg), "Device #%u (%s) disconnected.", i, ident);
|
|
|
|
rarch_main_msg_queue_push(msg, 0, 60, false);
|
|
|
|
RARCH_LOG("%s\n", msg);
|
|
|
|
}
|