Move input_autodetect code to input_autodetect.c

This commit is contained in:
twinaphex 2014-09-30 17:48:31 +02:00
parent 03378289d7
commit d43b9082e7
7 changed files with 190 additions and 145 deletions

View File

@ -103,6 +103,7 @@ OBJ += frontend/frontend.o \
rewind.o \ rewind.o \
gfx/gfx_common.o \ gfx/gfx_common.o \
gfx/fonts/bitmapfont.o \ gfx/fonts/bitmapfont.o \
input/input_autodetect.o \
input/input_common.o \ input/input_common.o \
input/keyboard_line.o \ input/keyboard_line.o \
input/overlay.o \ input/overlay.o \

View File

@ -284,6 +284,7 @@ FONTS
/*============================================================ /*============================================================
INPUT INPUT
============================================================ */ ============================================================ */
#include "../input/input_autodetect.c"
#include "../input/input_common.c" #include "../input/input_common.c"
#include "../input/keyboard_line.c" #include "../input/keyboard_line.c"

153
input/input_autodetect.c Normal file
View File

@ -0,0 +1,153 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2014 - 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 "input_common.h"
#include "input_autodetect.h"
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "../general.h"
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[i].base, &binds[i]);
input_config_parse_joy_axis(conf, "input",
input_config_bind_map[i].base, &binds[i]);
}
}
static bool input_try_autoconfigure_joypad_from_conf(config_file_t *conf,
unsigned index, const char *name, const char *driver,
bool block_osd_spam)
{
if (!conf)
return false;
char ident[1024], ident_idx[1024];
char input_driver[1024];
bool cond_found_idx, cond_found_general;
*ident = *input_driver = '\0';
config_get_array(conf, "input_device", ident, sizeof(ident));
config_get_array(conf, "input_driver", input_driver, sizeof(input_driver));
snprintf(ident_idx, sizeof(ident_idx), "%s_p%u", ident, index);
//RARCH_LOG("ident_idx: %s\n", ident_idx);
cond_found_idx = !strcmp(ident_idx, name);
cond_found_general = !strcmp(ident, name) && !strcmp(driver, input_driver);
if (cond_found_idx)
goto found;
else if (cond_found_general)
goto found;
return false;
found:
g_settings.input.autoconfigured[index] = true;
input_autoconfigure_joypad_conf(conf, g_settings.input.autoconf_binds[index]);
char msg[512];
snprintf(msg, sizeof(msg), "Joypad port #%u (%s) configured.",
index, name);
if (!block_osd_spam)
msg_queue_push(g_extern.msg_queue, msg, 0, 60);
RARCH_LOG("%s\n", msg);
return true;
}
void input_config_autoconfigure_joypad(unsigned index,
const char *name, const char *driver)
{
size_t i;
if (!g_settings.input.autodetect_enable)
return;
/* This will be the case if input driver is reinit.
* No reason to spam autoconfigure messages
* every time (fine in log). */
bool block_osd_spam = g_settings.input.autoconfigured[index] && name;
for (i = 0; i < RARCH_BIND_LIST_END; i++)
{
g_settings.input.autoconf_binds[index][i].joykey = NO_BTN;
g_settings.input.autoconf_binds[index][i].joyaxis = AXIS_NONE;
}
g_settings.input.autoconfigured[index] = false;
if (!name)
return;
/* false = load from both cfg files and internal */
bool internal_only = !*g_settings.input.autoconfig_dir;
#if defined(HAVE_BUILTIN_AUTOCONFIG) && (!defined(_WIN32) || defined(HAVE_WINXINPUT))
/* First internal */
for (i = 0; input_builtin_autoconfs[i]; i++)
{
config_file_t *conf = (config_file_t*)
config_file_new_from_string(input_builtin_autoconfs[i]);
bool success = input_try_autoconfigure_joypad_from_conf(conf,
index, name, driver, block_osd_spam);
config_file_free(conf);
if (success)
break;
}
#endif
/* Now try files */
if (!internal_only)
{
struct string_list *list = dir_list_new(
g_settings.input.autoconfig_dir, "cfg", false);
if (!list)
return;
for (i = 0; i < list->size; i++)
{
config_file_t *conf = config_file_new(list->elems[i].data);
if (!conf)
continue;
bool success = input_try_autoconfigure_joypad_from_conf(conf,
index, name, driver, block_osd_spam);
config_file_free(conf);
if (success)
break;
}
string_list_free(list);
}
}
const struct retro_keybind *input_get_auto_bind(unsigned port, unsigned id)
{
int joy_index = g_settings.input.joypad_map[port];
if (joy_index < 0)
return NULL;
return &g_settings.input.autoconf_binds[joy_index][id];
}

28
input/input_autodetect.h Normal file
View File

@ -0,0 +1,28 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2014 - 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/>.
*/
#ifndef _INPUT_AUTODETECT_H
#define _INPUT_AUTODETECT_H
const struct retro_keybind *input_get_auto_bind(unsigned port,
unsigned id);
void input_config_autoconfigure_joypad(unsigned index,
const char *name, const char *driver);
extern const char* const input_builtin_autoconfs[];
#endif

View File

@ -1,5 +1,6 @@
/* RetroArch - A frontend for libretro. /* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen * Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2014 - Daniel De Matteis
* *
* RetroArch is free software: you can redistribute it and/or modify it under the terms * 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- * of the GNU General Public License as published by the Free Software Found-
@ -1362,136 +1363,6 @@ void input_config_parse_joy_axis(config_file_t *conf, const char *prefix,
} }
#if !defined(IS_JOYCONFIG) && !defined(IS_RETROLAUNCH) #if !defined(IS_JOYCONFIG) && !defined(IS_RETROLAUNCH)
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[i].base, &binds[i]);
input_config_parse_joy_axis(conf, "input",
input_config_bind_map[i].base, &binds[i]);
}
}
static bool input_try_autoconfigure_joypad_from_conf(config_file_t *conf,
unsigned index, const char *name, const char *driver,
bool block_osd_spam)
{
if (!conf)
return false;
char ident[1024], ident_idx[1024];
char input_driver[1024];
bool cond_found_idx, cond_found_general;
*ident = *input_driver = '\0';
config_get_array(conf, "input_device", ident, sizeof(ident));
config_get_array(conf, "input_driver", input_driver, sizeof(input_driver));
snprintf(ident_idx, sizeof(ident_idx), "%s_p%u", ident, index);
//RARCH_LOG("ident_idx: %s\n", ident_idx);
cond_found_idx = !strcmp(ident_idx, name);
cond_found_general = !strcmp(ident, name) && !strcmp(driver, input_driver);
if (cond_found_idx)
goto found;
else if (cond_found_general)
goto found;
return false;
found:
g_settings.input.autoconfigured[index] = true;
input_autoconfigure_joypad_conf(conf, g_settings.input.autoconf_binds[index]);
char msg[512];
snprintf(msg, sizeof(msg), "Joypad port #%u (%s) configured.",
index, name);
if (!block_osd_spam)
msg_queue_push(g_extern.msg_queue, msg, 0, 60);
RARCH_LOG("%s\n", msg);
return true;
}
void input_config_autoconfigure_joypad(unsigned index,
const char *name, const char *driver)
{
size_t i;
if (!g_settings.input.autodetect_enable)
return;
/* This will be the case if input driver is reinit.
* No reason to spam autoconfigure messages
* every time (fine in log). */
bool block_osd_spam = g_settings.input.autoconfigured[index] && name;
for (i = 0; i < RARCH_BIND_LIST_END; i++)
{
g_settings.input.autoconf_binds[index][i].joykey = NO_BTN;
g_settings.input.autoconf_binds[index][i].joyaxis = AXIS_NONE;
}
g_settings.input.autoconfigured[index] = false;
if (!name)
return;
/* false = load from both cfg files and internal */
bool internal_only = !*g_settings.input.autoconfig_dir;
#if defined(HAVE_BUILTIN_AUTOCONFIG) && (!defined(_WIN32) || defined(HAVE_WINXINPUT))
/* First internal */
for (i = 0; input_builtin_autoconfs[i]; i++)
{
config_file_t *conf = (config_file_t*)
config_file_new_from_string(input_builtin_autoconfs[i]);
bool success = input_try_autoconfigure_joypad_from_conf(conf,
index, name, driver, block_osd_spam);
config_file_free(conf);
if (success)
break;
}
#endif
/* Now try files */
if (!internal_only)
{
struct string_list *list = dir_list_new(
g_settings.input.autoconfig_dir, "cfg", false);
if (!list)
return;
for (i = 0; i < list->size; i++)
{
config_file_t *conf = config_file_new(list->elems[i].data);
if (!conf)
continue;
bool success = input_try_autoconfigure_joypad_from_conf(conf,
index, name, driver, block_osd_spam);
config_file_free(conf);
if (success)
break;
}
string_list_free(list);
}
}
const struct retro_keybind *input_get_auto_bind(unsigned port, unsigned id)
{
int joy_index = g_settings.input.joypad_map[port];
if (joy_index < 0)
return NULL;
return &g_settings.input.autoconf_binds[joy_index][id];
}
static void input_get_bind_string_joykey(char *buf, const char *prefix, static void input_get_bind_string_joykey(char *buf, const char *prefix,
const struct retro_keybind *bind, size_t size) const struct retro_keybind *bind, size_t size)
{ {
@ -1555,14 +1426,6 @@ void input_get_bind_string(char *buf, const struct retro_keybind *bind,
strlcat(buf, keybuf, size); strlcat(buf, keybuf, size);
#endif #endif
} }
#else
void input_config_autoconfigure_joypad(unsigned index, const char *name,
const char *driver)
{
(void)index;
(void)name;
(void)driver;
}
#endif #endif
void input_push_analog_dpad(struct retro_keybind *binds, unsigned mode) void input_push_analog_dpad(struct retro_keybind *binds, unsigned mode)

View File

@ -16,6 +16,7 @@
#ifndef INPUT_COMMON_H__ #ifndef INPUT_COMMON_H__
#define INPUT_COMMON_H__ #define INPUT_COMMON_H__
#include "input_autodetect.h"
#include "../driver.h" #include "../driver.h"
#include "../conf/config_file.h" #include "../conf/config_file.h"
#include "../general.h" #include "../general.h"
@ -173,8 +174,6 @@ struct input_bind_map
extern const struct input_bind_map input_config_bind_map[]; extern const struct input_bind_map input_config_bind_map[];
const struct retro_keybind *input_get_auto_bind(unsigned port, unsigned id);
/* auto_bind can be NULL. */ /* auto_bind can be NULL. */
void input_get_bind_string(char *buf, const struct retro_keybind *bind, void input_get_bind_string(char *buf, const struct retro_keybind *bind,
const struct retro_keybind *auto_bind, size_t size); const struct retro_keybind *auto_bind, size_t size);
@ -188,8 +187,6 @@ extern const struct input_key_map input_config_key_map[];
void input_translate_rk_to_str(enum retro_key key, char *buf, size_t size); void input_translate_rk_to_str(enum retro_key key, char *buf, size_t size);
enum retro_key input_translate_str_to_rk(const char *str); enum retro_key input_translate_str_to_rk(const char *str);
extern const char* const input_builtin_autoconfs[];
const char *input_config_get_prefix(unsigned player, bool meta); const char *input_config_get_prefix(unsigned player, bool meta);
/* Returns RARCH_BIND_LIST_END on not found. */ /* Returns RARCH_BIND_LIST_END on not found. */
@ -205,9 +202,6 @@ void input_config_parse_joy_button(config_file_t *conf, const char *prefix,
void input_config_parse_joy_axis(config_file_t *conf, const char *prefix, void input_config_parse_joy_axis(config_file_t *conf, const char *prefix,
const char *axis, struct retro_keybind *bind); const char *axis, struct retro_keybind *bind);
void input_config_autoconfigure_joypad(unsigned index,
const char *name, const char *driver);
void input_push_analog_dpad(struct retro_keybind *binds, unsigned mode); void input_push_analog_dpad(struct retro_keybind *binds, unsigned mode);
void input_pop_analog_dpad(struct retro_keybind *binds); void input_pop_analog_dpad(struct retro_keybind *binds);

View File

@ -430,6 +430,11 @@ static void parse_input(int argc, char *argv[])
} }
void input_config_autoconfigure_joypad(unsigned index,
const char *name, const char *driver)
{
}
// Need SDL_main on OSX. // Need SDL_main on OSX.
#ifndef __APPLE__ #ifndef __APPLE__
#undef main #undef main