RetroArch/input/input_driver.c

292 lines
7.3 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2015 - 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>
2015-09-05 20:03:27 +02:00
2015-01-12 18:39:27 +01:00
#include "../general.h"
2015-10-26 08:59:23 +01:00
#include "../string_list_special.h"
2015-11-23 12:03:38 +01:00
#include "../verbosity.h"
2015-04-11 08:14:45 +02:00
static const input_driver_t *input_drivers[] = {
#ifdef __CELLOS_LV2__
&input_ps3,
#endif
#if defined(SN_TARGET_PSP2) || defined(PSP) || defined(VITA)
&input_psp,
#endif
2015-04-01 22:14:13 +01:00
#if defined(_3DS)
&input_ctr,
#endif
#if defined(HAVE_SDL) || defined(HAVE_SDL2)
&input_sdl,
#endif
#ifdef HAVE_DINPUT
&input_dinput,
#endif
#ifdef HAVE_X11
&input_x,
#endif
#ifdef XENON
&input_xenon360,
#endif
#if defined(HAVE_XINPUT2) || defined(HAVE_XINPUT_XBOX1)
&input_xinput,
#endif
#ifdef GEKKO
&input_gx,
#endif
#ifdef ANDROID
&input_android,
#endif
#ifdef HAVE_UDEV
&input_udev,
#endif
#if defined(__linux__) && !defined(ANDROID)
&input_linuxraw,
#endif
#if defined(HAVE_COCOA) || defined(HAVE_COCOATOUCH)
&input_cocoa,
#endif
#ifdef __QNX__
&input_qnx,
#endif
#ifdef EMSCRIPTEN
&input_rwebinput,
#endif
&input_null,
NULL,
};
/**
* input_driver_find_handle:
* @idx : index of driver to get handle to.
*
* Returns: handle to input driver at index. Can be NULL
* if nothing found.
**/
const void *input_driver_find_handle(int idx)
{
const void *drv = input_drivers[idx];
if (!drv)
return NULL;
return drv;
}
/**
* input_driver_find_ident:
* @idx : index of driver to get handle to.
*
* Returns: Human-readable identifier of input driver at index. Can be NULL
* if nothing found.
**/
const char *input_driver_find_ident(int idx)
{
const input_driver_t *drv = input_drivers[idx];
if (!drv)
return NULL;
return drv->ident;
}
/**
* config_get_input_driver_options:
*
* Get an enumerated list of all input driver names, separated by '|'.
*
* Returns: string listing of all input driver names, separated by '|'.
**/
const char* config_get_input_driver_options(void)
{
2015-10-26 19:41:20 +01:00
return char_list_new_special(STRING_LIST_INPUT_DRIVERS, NULL);
}
void find_input_driver(void)
{
driver_t *driver = driver_get_ptr();
2015-03-20 21:22:38 +01:00
settings_t *settings = config_get_ptr();
int i = find_driver_index("input_driver", settings->input.driver);
if (i >= 0)
driver->input = (const input_driver_t*)input_driver_find_handle(i);
else
{
unsigned d;
RARCH_ERR("Couldn't find any input driver named \"%s\"\n",
2015-03-20 21:22:38 +01:00
settings->input.driver);
RARCH_LOG_OUTPUT("Available input drivers are:\n");
for (d = 0; input_driver_find_handle(d); d++)
RARCH_LOG_OUTPUT("\t%s\n", input_driver_find_ident(d));
RARCH_WARN("Going to default to first input driver...\n");
driver->input = (const input_driver_t*)input_driver_find_handle(0);
if (!driver->input)
retro_fail(1, "find_input_driver()");
}
}
2015-11-24 02:18:41 +01:00
const input_driver_t *input_get_ptr(void *data)
2015-04-11 08:14:45 +02:00
{
2015-11-24 02:18:41 +01:00
driver_t *driver = (driver_t*)data;
2015-04-11 08:14:45 +02:00
if (!driver)
return NULL;
return driver->input;
}
/**
* input_driver_set_rumble_state:
* @port : User number.
* @effect : Rumble effect.
* @strength : Strength of rumble effect.
*
* Sets the rumble state.
* Used by RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE.
**/
bool input_driver_set_rumble_state(unsigned port,
enum retro_rumble_effect effect, uint16_t strength)
{
2015-04-11 08:14:45 +02:00
driver_t *driver = driver_get_ptr();
const input_driver_t *input = input_get_ptr(driver);
if (input->set_rumble)
return input->set_rumble(driver->input_data,
port, effect, strength);
return false;
2015-04-11 08:14:45 +02:00
}
2015-03-23 03:04:05 +01:00
int16_t input_driver_state(const struct retro_keybind **retro_keybinds,
unsigned port, unsigned device, unsigned index, unsigned id)
{
2015-04-11 08:14:45 +02:00
driver_t *driver = driver_get_ptr();
const input_driver_t *input = input_get_ptr(driver);
2015-03-23 03:17:43 +01:00
2015-04-11 08:14:45 +02:00
return input->input_state(driver->input_data, retro_keybinds,
port, device, index, id);
2015-03-23 03:17:43 +01:00
}
2015-03-23 03:22:57 +01:00
const input_device_driver_t *input_driver_get_joypad_driver(void)
2015-03-23 03:40:09 +01:00
{
2015-04-11 08:17:55 +02:00
driver_t *driver = driver_get_ptr();
const input_driver_t *input = input_get_ptr(driver);
2015-03-23 03:40:09 +01:00
2015-04-11 08:17:55 +02:00
if (input->get_joypad_driver)
return input->get_joypad_driver(driver->input_data);
2015-03-23 03:40:09 +01:00
return NULL;
}
2015-03-23 04:18:27 +01:00
2015-11-16 02:39:38 +01:00
const input_device_driver_t *input_driver_get_sec_joypad_driver(void)
{
driver_t *driver = driver_get_ptr();
const input_driver_t *input = input_get_ptr(driver);
if (input->get_sec_joypad_driver)
return input->get_sec_joypad_driver(driver->input_data);
return NULL;
}
2015-03-23 04:18:27 +01:00
uint64_t input_driver_get_capabilities(void)
{
2015-04-11 08:17:55 +02:00
driver_t *driver = driver_get_ptr();
const input_driver_t *input = input_get_ptr(driver);
2015-03-23 04:18:27 +01:00
2015-04-11 08:17:55 +02:00
if (input->get_capabilities)
return input->get_capabilities(driver->input_data);
2015-03-23 04:18:27 +01:00
return 0;
}
2015-03-23 04:31:11 +01:00
bool input_driver_grab_mouse(bool state)
{
2015-04-11 08:17:55 +02:00
driver_t *driver = driver_get_ptr();
const input_driver_t *input = input_get_ptr(driver);
2015-03-23 04:31:11 +01:00
2015-04-11 08:17:55 +02:00
if (input->grab_mouse)
2015-03-23 07:15:41 +01:00
{
2015-04-11 08:17:55 +02:00
input->grab_mouse(driver->input_data, state);
2015-03-23 07:15:41 +01:00
return true;
}
return false;
2015-03-23 04:31:11 +01:00
}
2015-03-23 07:07:28 +01:00
bool input_driver_grab_stdin(void)
{
driver_t *driver = driver_get_ptr();
const input_driver_t *input = input_get_ptr(driver);
if (input->grab_stdin)
return input->grab_stdin(driver->input_data);
return false;
}
2015-11-25 03:15:22 +01:00
bool input_driver_init(void)
2015-03-23 07:07:28 +01:00
{
driver_t *driver = driver_get_ptr();
2015-03-24 08:35:55 +01:00
if (driver && driver->input)
2015-11-25 03:15:22 +01:00
driver->input_data = driver->input->init();
if (!driver->input_data)
return false;
return true;
2015-03-23 07:15:41 +01:00
}
2015-11-25 03:25:31 +01:00
void input_driver_set(const input_driver_t **input, void **input_data)
{
driver_t *driver = driver_get_ptr();
if (input && input_data)
{
*input = driver->input;
*input_data = driver->input_data;
}
driver->input_data_own = true;
}
2015-03-23 07:15:41 +01:00
void input_driver_free(void)
{
driver_t *driver = driver_get_ptr();
2015-03-24 08:35:55 +01:00
if (driver && driver->input)
2015-03-23 07:15:41 +01:00
driver->input->free(driver->input_data);
2015-03-23 07:07:28 +01:00
}
2015-11-25 03:25:31 +01:00
void input_driver_destroy(void)
{
driver_t *driver = driver_get_ptr();
if (driver)
driver->input_data = NULL;
}
bool input_driver_keyboard_mapping_is_blocked(void)
{
driver_t *driver = driver_get_ptr();
const input_driver_t *input = input_get_ptr(driver);
if (input->keyboard_mapping_is_blocked)
return driver->input->keyboard_mapping_is_blocked(
driver->input_data);
return false;
}
void input_driver_keyboard_mapping_set_block(bool value)
{
driver_t *driver = driver_get_ptr();
const input_driver_t *input = input_get_ptr(driver);
if (input->keyboard_mapping_set_block)
driver->input->keyboard_mapping_set_block(driver->input_data, value);
}