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
|
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>
|
2015-06-19 03:45:23 +02:00
|
|
|
#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>
|
2015-06-19 03:45:23 +02:00
|
|
|
|
|
|
|
#include <boolean.h>
|
2015-06-03 18:12:06 +02:00
|
|
|
|
2015-12-06 19:31:47 +01:00
|
|
|
#include "../../configuration.h"
|
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"
|
2015-11-28 16:22:49 +01:00
|
|
|
#include "../input_joypad_driver.h"
|
2012-06-19 15:01:34 -04:00
|
|
|
|
2012-09-28 22:38:42 +02:00
|
|
|
typedef struct linuxraw_input
|
|
|
|
{
|
2015-06-19 03:45:23 +02:00
|
|
|
bool blocked;
|
2015-04-14 16:37:59 +02:00
|
|
|
const input_device_driver_t *joypad;
|
2012-09-28 22:38:42 +02:00
|
|
|
bool state[0x80];
|
|
|
|
} linuxraw_input_t;
|
|
|
|
|
2012-06-19 15:01:34 -04:00
|
|
|
static void *linuxraw_input_init(void)
|
|
|
|
{
|
2015-06-03 21:38:27 +02:00
|
|
|
linuxraw_input_t *linuxraw = NULL;
|
|
|
|
settings_t *settings = config_get_ptr();
|
2015-01-11 01:29:19 +01:00
|
|
|
|
|
|
|
/* 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))
|
2012-07-24 08:15:00 +02:00
|
|
|
{
|
2014-10-15 00:30:23 +02:00
|
|
|
RARCH_WARN("stdin is already used for content loading. Cannot use stdin for input.\n");
|
2012-07-24 08:15:00 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-11 01:29:19 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-06-03 18:22:54 +02:00
|
|
|
linuxraw->joypad = input_joypad_init_driver(
|
|
|
|
settings->input.joypad_driver, linuxraw);
|
2015-01-12 02:52:52 +01:00
|
|
|
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();
|
2015-01-11 01:29:19 +01:00
|
|
|
|
2012-06-19 15:01:34 -04:00
|
|
|
return linuxraw;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool linuxraw_key_pressed(linuxraw_input_t *linuxraw, int key)
|
|
|
|
{
|
2015-01-12 02:52:52 +01:00
|
|
|
unsigned sym = input_keymaps_translate_rk_to_keysym((enum retro_key)key);
|
2013-12-07 14:13:40 +01:00
|
|
|
return linuxraw->state[sym];
|
2012-06-19 15:01:34 -04:00
|
|
|
}
|
|
|
|
|
2014-10-15 00:30:23 +02:00
|
|
|
static bool linuxraw_is_pressed(linuxraw_input_t *linuxraw,
|
|
|
|
const struct retro_keybind *binds, unsigned id)
|
2012-06-19 15:01:34 -04:00
|
|
|
{
|
2016-10-26 10:29:26 +02:00
|
|
|
const struct retro_keybind *bind = binds ? &binds[id] : NULL;
|
2015-01-11 01:29:19 +01:00
|
|
|
|
|
|
|
if (id >= RARCH_BIND_LIST_END)
|
|
|
|
return false;
|
|
|
|
|
2016-10-26 10:29:26 +02:00
|
|
|
return bind && bind->valid && linuxraw_key_pressed(linuxraw, binds[id].key);
|
2012-06-19 15:01:34 -04:00
|
|
|
}
|
|
|
|
|
2013-11-09 01:01:18 +01:00
|
|
|
static int16_t linuxraw_analog_pressed(linuxraw_input_t *linuxraw,
|
2014-10-20 20:31:00 +02:00
|
|
|
const struct retro_keybind *binds, unsigned idx, unsigned id)
|
2013-11-09 01:01:18 +01:00
|
|
|
{
|
2015-01-11 01:29:19 +01:00
|
|
|
int16_t pressed_minus = 0, pressed_plus = 0;
|
2013-11-09 01:01:18 +01:00
|
|
|
unsigned id_minus = 0;
|
|
|
|
unsigned id_plus = 0;
|
2015-01-12 02:52:52 +01:00
|
|
|
|
2014-10-20 20:31:00 +02:00
|
|
|
input_conv_analog_id_to_bind_id(idx, id, &id_minus, &id_plus);
|
2013-11-09 01:01:18 +01:00
|
|
|
|
2015-01-11 01:29:19 +01:00
|
|
|
if (linuxraw_is_pressed(linuxraw, binds, id_minus))
|
|
|
|
pressed_minus = -0x7fff;
|
|
|
|
if (linuxraw_is_pressed(linuxraw, binds, id_plus))
|
|
|
|
pressed_plus = 0x7fff;
|
|
|
|
|
2013-11-09 01:01:18 +01:00
|
|
|
return pressed_plus + pressed_minus;
|
|
|
|
}
|
|
|
|
|
2015-11-07 20:59:12 +01:00
|
|
|
static bool linuxraw_input_meta_key_pressed(void *data, int key)
|
2015-07-17 03:31:51 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-15 00:30:23 +02:00
|
|
|
static int16_t linuxraw_input_state(void *data,
|
|
|
|
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
|
|
|
{
|
2016-10-27 01:21:01 +02:00
|
|
|
int16_t ret = 0;
|
2015-01-12 02:52:52 +01:00
|
|
|
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
|
2012-06-19 15:01:34 -04:00
|
|
|
|
|
|
|
switch (device)
|
|
|
|
{
|
|
|
|
case RETRO_DEVICE_JOYPAD:
|
2016-10-26 10:29:26 +02:00
|
|
|
if (binds[port] && binds[port][id].valid)
|
2016-10-23 20:37:12 +02:00
|
|
|
return linuxraw_is_pressed(linuxraw, binds[port], id) ||
|
|
|
|
input_joypad_pressed(linuxraw->joypad, port, binds[port], id);
|
|
|
|
break;
|
2012-09-22 01:04:13 +02:00
|
|
|
case RETRO_DEVICE_ANALOG:
|
2016-10-27 01:21:01 +02:00
|
|
|
if (binds[port])
|
|
|
|
ret = linuxraw_analog_pressed(linuxraw, binds[port], idx, id);
|
2016-10-26 10:29:26 +02:00
|
|
|
if (!ret && binds[port])
|
2014-10-20 20:31:00 +02:00
|
|
|
ret = input_joypad_analog(linuxraw->joypad, port, idx, id, binds[port]);
|
2013-11-09 01:01:18 +01:00
|
|
|
return ret;
|
2012-06-19 15:01:34 -04:00
|
|
|
}
|
2015-01-11 01:29:19 +01: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;
|
|
|
|
|
2012-09-28 22:38:42 +02:00
|
|
|
if (linuxraw->joypad)
|
|
|
|
linuxraw->joypad->destroy();
|
|
|
|
|
2015-11-14 08:50:10 +01:00
|
|
|
linux_terminal_restore_input();
|
2012-06-19 15:01:34 -04:00
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
2014-10-15 00:30:23 +02:00
|
|
|
static bool linuxraw_set_rumble(void *data, unsigned port,
|
|
|
|
enum retro_rumble_effect effect, uint16_t strength)
|
2013-09-25 22:59:05 +02:00
|
|
|
{
|
|
|
|
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
|
2015-01-11 01:29:19 +01:00
|
|
|
if (!linuxraw)
|
|
|
|
return false;
|
|
|
|
return input_joypad_set_rumble(linuxraw->joypad, port, effect, strength);
|
2013-09-25 22:59:05 +02:00
|
|
|
}
|
|
|
|
|
2015-04-14 16:37:59 +02:00
|
|
|
static const input_device_driver_t *linuxraw_get_joypad_driver(void *data)
|
2013-09-29 20:52:51 +02:00
|
|
|
{
|
|
|
|
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
|
2015-01-11 01:29:19 +01:00
|
|
|
if (!linuxraw)
|
2015-03-16 16:57:27 +01:00
|
|
|
return NULL;
|
2015-01-11 01:29:19 +01:00
|
|
|
return linuxraw->joypad;
|
2013-09-29 20:52:51 +02:00
|
|
|
}
|
|
|
|
|
2012-06-19 15:01:34 -04:00
|
|
|
static void linuxraw_input_poll(void *data)
|
|
|
|
{
|
|
|
|
uint8_t c;
|
|
|
|
uint16_t t;
|
2015-01-11 01:29:19 +01:00
|
|
|
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
|
2012-06-19 15:01:34 -04:00
|
|
|
|
2012-07-24 08:15:00 +02:00
|
|
|
while (read(STDIN_FILENO, &c, 1) > 0)
|
2012-06-19 15:01:34 -04:00
|
|
|
{
|
2015-01-11 01:29:19 +01:00
|
|
|
bool pressed;
|
|
|
|
|
2012-06-19 15:26:09 -04:00
|
|
|
if (c == KEY_C && (linuxraw->state[KEY_LEFTCTRL] || linuxraw->state[KEY_RIGHTCTRL]))
|
|
|
|
kill(getpid(), SIGINT);
|
|
|
|
|
2015-01-11 01:29:19 +01:00
|
|
|
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)
|
2012-07-24 08:15:00 +02:00
|
|
|
read(STDIN_FILENO, &t, 2);
|
2012-06-19 15:01:34 -04:00
|
|
|
else
|
|
|
|
linuxraw->state[c] = pressed;
|
|
|
|
}
|
|
|
|
|
2014-08-03 00:44:07 +02:00
|
|
|
if (linuxraw->joypad)
|
|
|
|
linuxraw->joypad->poll();
|
2012-06-19 15:01:34 -04:00
|
|
|
}
|
|
|
|
|
2013-11-02 21:16:57 +01:00
|
|
|
static uint64_t linuxraw_get_capabilities(void *data)
|
|
|
|
{
|
|
|
|
uint64_t caps = 0;
|
|
|
|
|
2015-01-11 01:29:19 +01:00
|
|
|
(void)data;
|
|
|
|
|
2013-11-02 21:16:57 +01:00
|
|
|
caps |= (1 << RETRO_DEVICE_JOYPAD);
|
|
|
|
caps |= (1 << RETRO_DEVICE_ANALOG);
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|
|
|
|
|
2015-03-24 07:51:50 +01:00
|
|
|
static void linuxraw_grab_mouse(void *data, bool state)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
(void)state;
|
|
|
|
}
|
|
|
|
|
2015-06-19 03:45:23 +02:00
|
|
|
static bool linuxraw_keyboard_mapping_is_blocked(void *data)
|
|
|
|
{
|
|
|
|
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
|
|
|
|
if (!linuxraw)
|
|
|
|
return false;
|
|
|
|
return linuxraw->blocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void linuxraw_keyboard_mapping_set_block(void *data, bool value)
|
|
|
|
{
|
|
|
|
linuxraw_input_t *linuxraw = (linuxraw_input_t*)data;
|
|
|
|
if (!linuxraw)
|
2015-06-21 19:50:45 -03:00
|
|
|
return;
|
2015-06-19 03:45:23 +02:00
|
|
|
linuxraw->blocked = value;
|
|
|
|
}
|
|
|
|
|
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,
|
2015-07-17 03:31:51 +02:00
|
|
|
linuxraw_input_meta_key_pressed,
|
2012-06-19 15:01:34 -04:00
|
|
|
linuxraw_input_free,
|
2013-03-13 16:33:39 +01:00
|
|
|
NULL,
|
2013-11-03 00:27:58 +01:00
|
|
|
NULL,
|
2013-11-02 21:16:57 +01:00
|
|
|
linuxraw_get_capabilities,
|
2013-09-25 22:59:05 +02:00
|
|
|
"linuxraw",
|
2015-03-24 07:51:50 +01:00
|
|
|
linuxraw_grab_mouse,
|
2015-11-14 08:50:10 +01:00
|
|
|
linux_terminal_grab_stdin,
|
2013-09-25 22:59:05 +02:00
|
|
|
linuxraw_set_rumble,
|
2013-09-29 20:52:51 +02:00
|
|
|
linuxraw_get_joypad_driver,
|
2015-11-16 02:42:10 +01:00
|
|
|
NULL,
|
2015-06-19 03:45:23 +02:00
|
|
|
linuxraw_keyboard_mapping_is_blocked,
|
|
|
|
linuxraw_keyboard_mapping_set_block,
|
2012-06-19 15:01:34 -04:00
|
|
|
};
|