RetroArch/input/common/linux_common.c

124 lines
3.1 KiB
C
Raw Normal View History

2015-11-14 07:50:10 +00:00
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 12:40:32 +00:00
* Copyright (C) 2011-2017 - Daniel De Matteis
*
2015-11-14 07:50:10 +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/>.
*/
2015-11-14 21:03:37 +00:00
#include <stdlib.h>
#include <signal.h>
2015-11-14 07:50:10 +00:00
#include <linux/input.h>
#include <linux/kd.h>
#include <termios.h>
2015-11-14 21:03:37 +00:00
#include <unistd.h>
2015-11-14 07:50:10 +00:00
#include "linux_common.h"
2020-07-07 01:49:54 +00:00
/* TODO/FIXME - static globals */
2020-07-24 20:23:52 +00:00
static struct termios old_term, new_term;
static long old_kbmd = 0xffff;
2015-11-14 07:50:10 +00:00
static bool linux_stdin_claimed = false;
void linux_terminal_restore_input(void)
{
2020-07-24 20:23:52 +00:00
if (old_kbmd == 0xffff)
2015-11-14 08:01:41 +00:00
return;
2020-07-24 20:23:52 +00:00
if (ioctl(0, KDSKBMODE, old_kbmd) < 0)
2017-06-28 04:59:00 +00:00
return;
2020-07-24 20:23:52 +00:00
tcsetattr(0, TCSAFLUSH, &old_term);
old_kbmd = 0xffff;
2015-11-14 07:50:10 +00:00
linux_stdin_claimed = false;
}
2015-11-14 08:01:41 +00:00
/* Disables input */
2020-07-24 20:23:52 +00:00
static bool linux_terminal_init(void)
2015-11-14 07:50:10 +00:00
{
2020-07-24 20:23:52 +00:00
if (old_kbmd != 0xffff)
2015-11-14 08:01:41 +00:00
return false;
2020-07-24 20:23:52 +00:00
if (tcgetattr(0, &old_term) < 0)
2015-11-14 08:01:41 +00:00
return false;
2020-07-24 20:23:52 +00:00
new_term = old_term;
new_term.c_lflag &= ~(ECHO | ICANON | ISIG);
new_term.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
new_term.c_cc[VMIN] = 0;
new_term.c_cc[VTIME] = 0;
2015-11-14 08:01:41 +00:00
/* Be careful about recovering the terminal. */
2020-07-24 20:23:52 +00:00
if (ioctl(0, KDGKBMODE, &old_kbmd) < 0)
2015-11-14 08:01:41 +00:00
return false;
2020-07-24 20:23:52 +00:00
if (tcsetattr(0, TCSAFLUSH, &new_term) < 0)
2015-11-14 08:01:41 +00:00
return false;
2015-11-14 07:50:10 +00:00
return true;
}
2020-07-24 20:23:52 +00:00
/* We need to disable use of stdin command interface if
* stdin is supposed to be used for input. */
2015-11-14 07:50:10 +00:00
void linux_terminal_claim_stdin(void)
{
linux_stdin_claimed = true;
2015-11-14 07:50:10 +00:00
}
bool linux_terminal_grab_stdin(void *data)
{
return linux_stdin_claimed;
}
2015-11-14 21:03:37 +00:00
static void linux_terminal_restore_signal(int sig)
{
linux_terminal_restore_input();
kill(getpid(), sig);
}
2015-11-14 21:07:11 +00:00
bool linux_terminal_disable_input(void)
2015-11-14 21:03:37 +00:00
{
struct sigaction sa = {{0}};
/* Avoid accidentally typing stuff. */
if (!isatty(0))
2015-11-14 21:07:11 +00:00
return false;
2015-11-14 21:03:37 +00:00
if (!linux_terminal_init())
return false;
if (ioctl(0, KDSKBMODE, K_MEDIUMRAW) < 0)
2015-11-14 21:03:37 +00:00
{
2020-07-24 20:23:52 +00:00
tcsetattr(0, TCSAFLUSH, &old_term);
2015-11-14 21:07:11 +00:00
return false;
2015-11-14 21:03:37 +00:00
}
sa.sa_handler = linux_terminal_restore_signal;
sa.sa_flags = SA_RESTART | SA_RESETHAND;
sigemptyset(&sa.sa_mask);
/* Trap some standard termination codes so we
2015-11-14 21:07:11 +00:00
* can restore the keyboard before we lose control. */
2015-11-14 21:03:37 +00:00
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(linux_terminal_restore_input);
2015-11-14 21:07:11 +00:00
return true;
2015-11-14 21:03:37 +00:00
}