RetroArch/input/input_mapper.c

192 lines
5.1 KiB
C
Raw Normal View History

2017-09-08 03:30:42 +00:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
* Copyright (C) 2016-2017 - Andrés Suárez
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif
#include <compat/strl.h>
#include <compat/posix_string.h>
#include <retro_miscellaneous.h>
#include <libretro.h>
2017-12-27 03:02:11 +00:00
2017-12-31 16:53:11 +00:00
#ifdef HAVE_MENU
2017-12-27 03:02:11 +00:00
#include "../menu/menu_driver.h"
2017-12-31 16:53:11 +00:00
#endif
2017-09-08 03:30:42 +00:00
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include "input_mapper.h"
#include "../configuration.h"
#include "../msg_hash.h"
#include "../verbosity.h"
2017-09-09 19:21:00 +00:00
#define MAPPER_GET_KEY(state, key) (((state)->keys[(key) / 32] >> ((key) % 32)) & 1)
#define MAPPER_SET_KEY(state, key) (state)->keys[(key) / 32] |= 1 << ((key) % 32)
#define MAPPER_UNSET_KEY(state, key) (state)->keys[(key) / 32] |= 0 << ((key) % 32)
2017-09-08 03:30:42 +00:00
struct input_mapper
{
2017-09-09 19:21:00 +00:00
/* The controller port that will be polled*/
uint8_t port;
2017-09-08 03:30:42 +00:00
/* Left X, Left Y, Right X, Right Y */
2017-09-09 19:21:00 +00:00
int16_t analog[4];
2017-09-08 03:30:42 +00:00
/* the whole keyboard state */
uint32_t keys[RETROK_LAST / 32 + 1];
2017-11-12 16:02:14 +00:00
/* This is a bitmask of (1 << key_bind_id). */
retro_bits_t buttons;
2017-09-09 19:21:00 +00:00
};
2017-09-08 03:30:42 +00:00
input_mapper_t *input_mapper_new(uint16_t port)
{
2017-09-09 19:21:00 +00:00
input_mapper_t* handle = (input_mapper_t*)
calloc(1, sizeof(*handle));
2017-09-09 19:21:00 +00:00
if (!handle)
return NULL;
2017-11-12 16:02:14 +00:00
2017-09-09 19:21:00 +00:00
handle->port = port;
2017-09-09 19:21:00 +00:00
return handle;
2017-09-08 03:30:42 +00:00
}
void input_mapper_free(input_mapper_t *handle)
{
if (!handle)
return;
2017-09-09 19:21:00 +00:00
free (handle);
2017-09-08 03:30:42 +00:00
}
bool flag = false;
bool input_mapper_button_pressed(input_mapper_t *handle, int id)
{
return BIT256_GET(handle->buttons, id);
}
2017-09-09 19:21:00 +00:00
void input_mapper_poll(input_mapper_t *handle)
2017-09-08 03:30:42 +00:00
{
int i, j;
2017-09-09 19:21:00 +00:00
settings_t *settings = config_get_ptr();
unsigned device = settings->uints.input_libretro_device[handle->port];
bool key_event[RARCH_CUSTOM_BIND_LIST_END];
2017-12-31 16:53:11 +00:00
#ifdef HAVE_MENU
2017-12-26 06:20:36 +00:00
bool menu_is_alive = menu_driver_is_alive();
2017-12-31 16:53:11 +00:00
#endif
device &= RETRO_DEVICE_MASK;
2017-09-09 19:21:00 +00:00
2017-12-31 16:53:11 +00:00
#ifdef HAVE_MENU
if (menu_is_alive)
return;
#endif
memset(handle->keys, 0, sizeof(handle->keys));
i = 0;
if (device == RETRO_DEVICE_KEYBOARD)
{
for (i = 0; i < MAX_USERS; i++)
2017-09-09 19:21:00 +00:00
{
for (j = 0; j < RARCH_CUSTOM_BIND_LIST_END; j++)
2017-09-09 19:21:00 +00:00
{
if (j < RETROK_LAST)
{
if (input_state(i, RETRO_DEVICE_JOYPAD, 0, j) &&
settings->uints.input_keymapper_ids[i][j] != RETROK_UNKNOWN)
{
MAPPER_SET_KEY (handle,
settings->uints.input_keymapper_ids[i][j]);
input_keyboard_event(true,
settings->uints.input_keymapper_ids[i][j],
0, 0, RETRO_DEVICE_KEYBOARD);
key_event[j] = true;
}
else
{
if (key_event[j] == false &&
settings->uints.input_keymapper_ids[i][j] != RETROK_UNKNOWN)
{
input_keyboard_event(false,
settings->uints.input_keymapper_ids[i][j],
0, 0, RETRO_DEVICE_KEYBOARD);
}
}
}
2017-09-09 19:21:00 +00:00
}
}
}
if (device == RETRO_DEVICE_JOYPAD)
{
retro_bits_t current_input;
input_keys_pressed(settings, &current_input);
BIT256_CLEAR_ALL(handle->buttons);
for (i = 0; i < MAX_USERS; i++)
{
for (j = 0; j < RARCH_CUSTOM_BIND_LIST_END; j++)
{
int aux = BIT256_GET(current_input, j);
int remap = settings->uints.input_remap_ids[i][j];
if (aux == 1 && j != remap)
BIT256_SET(handle->buttons, remap);
}
}
}
2017-09-08 03:30:42 +00:00
}
void input_mapper_state(
input_mapper_t *handle,
2017-09-08 03:30:42 +00:00
int16_t *ret,
unsigned port,
unsigned device,
unsigned idx,
unsigned id,
bool clear)
2017-09-08 03:30:42 +00:00
{
if (!handle)
return;
2017-09-09 19:21:00 +00:00
switch (device)
{
case RETRO_DEVICE_JOYPAD:
/* we should get the new buttons here via input_remapper_button_pressed but it doesn't work because the old state is still there
so both actions trigger */
if (input_mapper_button_pressed(handle, id))
*ret = 1;
break;
2017-09-09 19:21:00 +00:00
case RETRO_DEVICE_KEYBOARD:
if (id < RETROK_LAST)
{
if (MAPPER_GET_KEY(handle, id))
2017-09-09 19:21:00 +00:00
*ret |= 1;
}
break;
default:
break;
2017-09-09 19:21:00 +00:00
}
return;
2017-09-16 23:06:55 +00:00
}