RetroArch/input/linuxraw_input.c

255 lines
6.7 KiB
C
Raw Normal View History

2012-06-19 19:01:34 +00:00
/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2012-2014 - Michael Lelli
2012-06-19 19:01:34 +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 "../driver.h"
#include <sys/ioctl.h>
#include <linux/input.h>
#include <linux/kd.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include "../general.h"
2012-09-22 07:57:04 +00:00
#include "input_common.h"
2012-06-19 19:01:34 +00:00
2012-06-22 17:16:53 +00:00
static long oldKbmd = 0xffff;
2012-06-19 19:01:34 +00:00
static struct termios oldTerm, newTerm;
2012-09-28 20:38:42 +00:00
typedef struct linuxraw_input
{
const rarch_joypad_driver_t *joypad;
bool state[0x80];
} linuxraw_input_t;
static void linuxraw_resetKbmd(void)
2012-06-19 19:01:34 +00:00
{
2012-06-22 17:16:53 +00:00
if (oldKbmd != 0xffff)
2012-06-19 19:01:34 +00:00
{
ioctl(0, KDSKBMODE, oldKbmd);
tcsetattr(0, TCSAFLUSH, &oldTerm);
2012-06-22 17:16:53 +00:00
oldKbmd = 0xffff;
2012-06-19 19:01:34 +00:00
}
2012-09-23 22:00:10 +00:00
driver.stdin_claimed = false;
2012-06-19 19:01:34 +00:00
}
static void linuxraw_exitGracefully(int sig)
{
linuxraw_resetKbmd();
kill(getpid(), sig);
}
static void *linuxraw_input_init(void)
{
// only work on terminals
if (!isatty(0))
return NULL;
if (driver.stdin_claimed)
{
RARCH_WARN("stdin is already used for ROM loading. Cannot use stdin for input.\n");
return NULL;
}
2012-06-19 19:01:34 +00:00
linuxraw_input_t *linuxraw = (linuxraw_input_t*)calloc(1, sizeof(*linuxraw));
if (!linuxraw)
return NULL;
2012-06-22 17:16:53 +00:00
if (oldKbmd == 0xffff)
2012-06-19 19:01:34 +00:00
{
tcgetattr(0, &oldTerm);
newTerm = oldTerm;
newTerm.c_lflag &= ~(ECHO | ICANON | ISIG);
newTerm.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
newTerm.c_cc[VMIN] = 0;
newTerm.c_cc[VTIME] = 0;
if (ioctl(0, KDGKBMODE, &oldKbmd) != 0)
2013-11-03 10:44:12 +00:00
{
free(linuxraw);
2012-06-19 19:01:34 +00:00
return NULL;
2013-11-03 10:44:12 +00:00
}
2012-06-19 19:01:34 +00:00
}
tcsetattr(0, TCSAFLUSH, &newTerm);
if (ioctl(0, KDSKBMODE, K_MEDIUMRAW) != 0)
{
linuxraw_resetKbmd();
2013-11-03 10:44:12 +00:00
free(linuxraw);
2012-06-19 19:01:34 +00:00
return NULL;
}
struct sigaction sa;
sa.sa_handler = linuxraw_exitGracefully;
sa.sa_flags = SA_RESTART | SA_RESETHAND;
sigemptyset(&sa.sa_mask);
// trap some standard termination codes so we can restore the keyboard before we lose control
sigaction(SIGABRT, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
sigaction(SIGFPE, &sa, NULL);
sigaction(SIGILL, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGSEGV, &sa, NULL);
atexit(linuxraw_resetKbmd);
2013-05-04 08:22:31 +00:00
linuxraw->joypad = input_joypad_init_driver(g_settings.input.joypad_driver);
2013-12-07 13:13:40 +00:00
input_init_keyboard_lut(rarch_key_map_linux);
2012-06-19 19:01:34 +00:00
2012-07-24 00:47:28 +00:00
driver.stdin_claimed = true; // We need to disable use of stdin command interface if stdin is supposed to be used for input.
2012-06-19 19:01:34 +00:00
return linuxraw;
}
static bool linuxraw_key_pressed(linuxraw_input_t *linuxraw, int key)
{
2013-12-07 13:13:40 +00:00
unsigned sym = input_translate_rk_to_keysym((enum retro_key)key);
return linuxraw->state[sym];
2012-06-19 19:01:34 +00:00
}
2012-07-07 15:19:32 +00:00
static bool linuxraw_is_pressed(linuxraw_input_t *linuxraw, const struct retro_keybind *binds, unsigned id)
2012-06-19 19:01:34 +00:00
{
if (id < RARCH_BIND_LIST_END)
{
2012-07-07 15:19:32 +00:00
const struct retro_keybind *bind = &binds[id];
2012-06-19 19:01:34 +00:00
return bind->valid && linuxraw_key_pressed(linuxraw, binds[id].key);
}
else
return false;
}
static int16_t linuxraw_analog_pressed(linuxraw_input_t *linuxraw,
const struct retro_keybind *binds, unsigned index, unsigned id)
{
unsigned id_minus = 0;
unsigned id_plus = 0;
input_conv_analog_id_to_bind_id(index, id, &id_minus, &id_plus);
int16_t pressed_minus = linuxraw_is_pressed(linuxraw,
binds, id_minus) ? -0x7fff : 0;
int16_t pressed_plus = linuxraw_is_pressed(linuxraw,
binds, id_plus) ? 0x7fff : 0;
return pressed_plus + pressed_minus;
}
2012-06-19 19:01:34 +00:00
static bool linuxraw_bind_button_pressed(void *data, int key)
{
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
return linuxraw_is_pressed(linuxraw, g_settings.input.binds[0], key) ||
2013-04-26 12:36:36 +00:00
input_joypad_pressed(linuxraw->joypad, 0, g_settings.input.binds[0], key);
2012-09-21 23:04:13 +00:00
}
2012-07-07 15:19:32 +00:00
static int16_t linuxraw_input_state(void *data, const struct retro_keybind **binds, unsigned port, unsigned device, unsigned index, unsigned id)
2012-06-19 19:01:34 +00:00
{
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
int16_t ret;
2012-06-19 19:01:34 +00:00
switch (device)
{
case RETRO_DEVICE_JOYPAD:
return linuxraw_is_pressed(linuxraw, binds[port], id) ||
2013-04-26 12:36:36 +00:00
input_joypad_pressed(linuxraw->joypad, port, binds[port], id);
2012-06-19 19:01:34 +00:00
2012-09-21 23:04:13 +00:00
case RETRO_DEVICE_ANALOG:
ret = linuxraw_analog_pressed(linuxraw, binds[port], index, id);
if (!ret)
ret = input_joypad_analog(linuxraw->joypad, port, index, id, binds[port]);
return ret;
2012-09-21 23:04:13 +00:00
2012-06-19 19:01:34 +00:00
default:
return 0;
}
}
static void linuxraw_input_free(void *data)
{
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
2012-09-28 20:38:42 +00:00
2014-05-20 00:33:09 +00:00
if (!linuxraw)
return;
2012-09-28 20:38:42 +00:00
if (linuxraw->joypad)
linuxraw->joypad->destroy();
2012-06-19 19:01:34 +00:00
linuxraw_resetKbmd();
free(data);
}
static bool linuxraw_set_rumble(void *data, unsigned port, enum retro_rumble_effect effect, uint16_t strength)
{
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
return input_joypad_set_rumble(linuxraw->joypad, port, effect, strength);
}
static const rarch_joypad_driver_t *linuxraw_get_joypad_driver(void *data)
{
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
return linuxraw->joypad;
}
2012-06-19 19:01:34 +00:00
static void linuxraw_input_poll(void *data)
{
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
uint8_t c;
uint16_t t;
while (read(STDIN_FILENO, &c, 1) > 0)
2012-06-19 19:01:34 +00:00
{
if (c == KEY_C && (linuxraw->state[KEY_LEFTCTRL] || linuxraw->state[KEY_RIGHTCTRL]))
kill(getpid(), SIGINT);
2012-06-19 19:01:34 +00:00
bool pressed = !(c & 0x80);
c &= ~0x80;
// ignore extended scancodes
if (!c)
read(STDIN_FILENO, &t, 2);
2012-06-19 19:01:34 +00:00
else
linuxraw->state[c] = pressed;
}
2012-09-28 20:38:42 +00:00
input_joypad_poll(linuxraw->joypad);
2012-06-19 19:01:34 +00:00
}
static uint64_t linuxraw_get_capabilities(void *data)
{
2013-12-07 13:13:40 +00:00
(void)data;
uint64_t caps = 0;
caps |= (1 << RETRO_DEVICE_JOYPAD);
caps |= (1 << RETRO_DEVICE_ANALOG);
return caps;
}
2012-06-19 19:01:34 +00:00
const input_driver_t input_linuxraw = {
linuxraw_input_init,
linuxraw_input_poll,
linuxraw_input_state,
linuxraw_bind_button_pressed,
linuxraw_input_free,
NULL,
NULL,
NULL,
linuxraw_get_capabilities,
NULL,
"linuxraw",
NULL,
linuxraw_set_rumble,
linuxraw_get_joypad_driver,
2012-06-19 19:01:34 +00:00
};