RetroArch/input/drivers/linuxraw_input.c

236 lines
6.1 KiB
C
Raw Normal View History

2012-06-19 15:01:34 -04:00
/* RetroArch - A frontend for libretro.
2014-01-01 01:50:59 +01:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2019 - Daniel De Matteis
2015-01-07 17:46:50 +01:00
* Copyright (C) 2012-2015 - Michael Lelli
*
2012-06-19 15:01:34 -04: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/>.
*/
2016-09-05 17:29:19 +02:00
#include <stdlib.h>
#include <unistd.h>
2012-06-19 15:01:34 -04:00
#include <sys/ioctl.h>
#include <linux/input.h>
#include <linux/kd.h>
#include <signal.h>
#include <boolean.h>
2015-06-03 18:12:06 +02:00
2015-11-23 12:03:38 +01:00
#include "../../verbosity.h"
2015-06-03 18:12:06 +02:00
2015-11-17 06:46:32 +01:00
#include "../common/linux_common.h"
2015-01-12 06:16:52 +01:00
#include "../input_keymaps.h"
#include "../input_driver.h"
2012-06-19 15:01:34 -04:00
2019-02-03 15:49:35 -08:00
/* TODO/FIXME -
* fix game focus toggle */
2012-09-28 22:38:42 +02:00
typedef struct linuxraw_input
{
bool state[0x80];
} linuxraw_input_t;
static void *linuxraw_input_init(const char *joypad_driver)
2012-06-19 15:01:34 -04:00
{
2015-06-03 21:38:27 +02:00
linuxraw_input_t *linuxraw = NULL;
/* Only work on terminals. */
2012-06-19 15:01:34 -04:00
if (!isatty(0))
return NULL;
2015-11-14 08:50:10 +01:00
if (linux_terminal_grab_stdin(NULL))
{
RARCH_WARN("stdin is already used for content loading. Cannot use stdin for input.\n");
return NULL;
}
linuxraw = (linuxraw_input_t*)calloc(1, sizeof(*linuxraw));
2012-06-19 15:01:34 -04:00
if (!linuxraw)
return NULL;
2015-11-14 22:07:11 +01:00
if (!linux_terminal_disable_input())
2012-06-19 15:01:34 -04:00
{
2015-11-14 08:50:10 +01:00
linux_terminal_restore_input();
2013-11-03 11:44:12 +01:00
free(linuxraw);
2012-06-19 15:01:34 -04:00
return NULL;
}
input_keymaps_init_keyboard_lut(rarch_key_map_linux);
2012-06-19 15:01:34 -04:00
2015-11-14 08:50:10 +01:00
linux_terminal_claim_stdin();
2012-06-19 15:01:34 -04:00
return linuxraw;
}
2020-08-30 05:29:32 +02:00
static int16_t linuxraw_input_state(
void *data,
const input_device_driver_t *joypad,
const input_device_driver_t *sec_joypad,
2020-02-27 07:33:14 +01:00
rarch_joypad_info_t *joypad_info,
const struct retro_keybind **binds, unsigned port,
2014-10-20 20:31:00 +02:00
unsigned device, unsigned idx, unsigned id)
2012-06-19 15:01:34 -04:00
{
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
2012-06-19 15:01:34 -04:00
switch (device)
{
case RETRO_DEVICE_JOYPAD:
if (id == RETRO_DEVICE_ID_JOYPAD_MASK)
{
unsigned i;
2020-08-30 05:29:32 +02:00
int16_t ret = joypad->state(
joypad_info, binds[port], port);
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
{
2020-06-12 18:28:07 +02:00
if (binds[port][i].valid)
{
if (
2019-07-22 01:20:00 +02:00
linuxraw->state[rarch_keysym_lut[
(enum retro_key)binds[port][i].key]]
2020-06-12 18:28:07 +02:00
)
ret |= (1 << i);
2019-07-22 01:20:00 +02:00
}
}
2019-07-22 01:20:00 +02:00
return ret;
}
else
{
2020-06-12 18:28:07 +02:00
if (id < RARCH_BIND_LIST_END)
{
if (binds[port][id].valid)
{
if (
button_is_pressed(
2020-08-30 05:29:32 +02:00
joypad, joypad_info, binds[port],
2020-06-12 18:28:07 +02:00
port, id)
)
return 1;
else if ((linuxraw->state[rarch_keysym_lut
[(enum retro_key)binds[port][id].key]]
))
return 1;
}
}
}
2019-07-22 01:20:00 +02:00
break;
2012-09-22 01:04:13 +02:00
case RETRO_DEVICE_ANALOG:
if (binds[port])
{
int id_minus_key = 0;
int id_plus_key = 0;
unsigned id_minus = 0;
unsigned id_plus = 0;
int16_t ret = 0;
bool id_plus_valid = false;
bool id_minus_valid = false;
input_conv_analog_id_to_bind_id(idx, id, id_minus, id_plus);
id_minus_valid = binds[port][id_minus].valid;
id_plus_valid = binds[port][id_plus].valid;
id_minus_key = binds[port][id_minus].key;
id_plus_key = binds[port][id_plus].key;
if (id_plus_valid && id_plus_key < RETROK_LAST)
{
unsigned sym = rarch_keysym_lut[(enum retro_key)id_plus_key];
if (linuxraw->state[sym] & 0x80)
ret = 0x7fff;
}
if (id_minus_valid && id_minus_key < RETROK_LAST)
{
unsigned sym = rarch_keysym_lut[(enum retro_key)id_minus_key];
if (linuxraw->state[sym] & 0x80)
ret += -0x7fff;
}
return ret;
}
break;
2012-06-19 15:01:34 -04:00
}
return 0;
2012-06-19 15:01:34 -04:00
}
static void linuxraw_input_free(void *data)
{
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
2012-09-28 22:38:42 +02:00
2014-05-20 02:33:09 +02:00
if (!linuxraw)
return;
2015-11-14 08:50:10 +01:00
linux_terminal_restore_input();
2012-06-19 15:01:34 -04:00
free(data);
}
2020-08-30 05:29:32 +02:00
static bool linuxraw_set_rumble(
const input_device_driver_t *joypad,
const input_device_driver_t *sec_joypad,
unsigned port,
enum retro_rumble_effect effect, uint16_t strength)
{
2020-08-30 05:29:32 +02:00
if (joypad)
return input_joypad_set_rumble(joypad, port, effect, strength);
return false;
}
2012-06-19 15:01:34 -04:00
static void linuxraw_input_poll(void *data)
{
uint8_t c;
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
2012-06-19 15:01:34 -04:00
while (read(STDIN_FILENO, &c, 1) > 0)
2012-06-19 15:01:34 -04:00
{
bool pressed;
2017-06-07 01:12:35 +02:00
uint16_t t;
if (c == KEY_C && (linuxraw->state[KEY_LEFTCTRL] || linuxraw->state[KEY_RIGHTCTRL]))
kill(getpid(), SIGINT);
pressed = !(c & 0x80);
2012-06-19 15:01:34 -04:00
c &= ~0x80;
2015-06-26 15:53:18 +02:00
/* ignore extended scancodes */
2012-06-19 15:01:34 -04:00
if (!c)
read(STDIN_FILENO, &t, 2);
2012-06-19 15:01:34 -04:00
else
linuxraw->state[c] = pressed;
}
}
static uint64_t linuxraw_get_capabilities(void *data)
{
uint64_t caps = 0;
caps |= (1 << RETRO_DEVICE_JOYPAD);
caps |= (1 << RETRO_DEVICE_ANALOG);
return caps;
}
2014-09-11 07:06:20 +02:00
input_driver_t input_linuxraw = {
2012-06-19 15:01:34 -04:00
linuxraw_input_init,
linuxraw_input_poll,
linuxraw_input_state,
linuxraw_input_free,
NULL,
NULL,
linuxraw_get_capabilities,
"linuxraw",
2020-08-30 05:29:32 +02:00
NULL, /* grab_mouse */
2015-11-14 08:50:10 +01:00
linux_terminal_grab_stdin,
linuxraw_set_rumble,
false
2012-06-19 15:01:34 -04:00
};