(udev) Reuse linux_common code

This commit is contained in:
twinaphex 2015-11-14 09:01:41 +01:00
parent d59215a678
commit 49dad39bf4
3 changed files with 39 additions and 49 deletions

View File

@ -23,34 +23,45 @@ static struct termios oldTerm, newTerm;
static long oldKbmd = 0xffff;
static bool linux_stdin_claimed = false;
void linux_terminal_flush(void)
{
tcsetattr(0, TCSAFLUSH, &oldTerm);
}
void linux_terminal_restore_input(void)
{
if (oldKbmd != 0xffff)
{
ioctl(0, KDSKBMODE, oldKbmd);
tcsetattr(0, TCSAFLUSH, &oldTerm);
oldKbmd = 0xffff;
}
if (oldKbmd == 0xffff)
return;
ioctl(0, KDSKBMODE, oldKbmd);
linux_terminal_flush();
oldKbmd = 0xffff;
linux_stdin_claimed = false;
}
/* Disables input */
bool linux_terminal_init(void)
{
if (oldKbmd == 0xffff)
{
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 (oldKbmd != 0xffff)
return false;
if (ioctl(0, KDGKBMODE, &oldKbmd) != 0)
return false;
}
if (tcgetattr(0, &oldTerm) < 0)
return false;
tcsetattr(0, TCSAFLUSH, &newTerm);
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;
/* Be careful about recovering the terminal. */
if (ioctl(0, KDGKBMODE, &oldKbmd) < 0)
return false;
if (tcsetattr(0, TCSAFLUSH, &newTerm) < 0)
return false;
return true;
}

View File

@ -18,6 +18,8 @@
#include <boolean.h>
void linux_terminal_flush(void);
void linux_terminal_restore_input(void);
bool linux_terminal_init(void);

View File

@ -27,12 +27,13 @@
#include <linux/types.h>
#include <linux/input.h>
#include <linux/kd.h>
#include <termios.h>
#include <signal.h>
#include <unistd.h>
#include <file/file_path.h>
#include "linux_common.h"
#include "../input_common.h"
#include "../input_joypad.h"
#include "../input_keymaps.h"
@ -696,22 +697,9 @@ static bool open_devices(udev_input_t *udev, const char *type, device_handle_cb
return true;
}
static long oldkbmd = 0xffff;
static struct termios oldterm, newterm;
static void restore_terminal_input(void)
{
if (oldkbmd == 0xffff)
return;
ioctl(0, KDSKBMODE, oldkbmd);
tcsetattr(0, TCSAFLUSH, &oldterm);
oldkbmd = 0xffff;
}
static void restore_terminal_signal(int sig)
{
restore_terminal_input();
linux_terminal_restore_input();
kill(getpid(), sig);
}
@ -720,26 +708,15 @@ static void disable_terminal_input(void)
struct sigaction sa = {{0}};
/* Avoid accidentally typing stuff. */
if (!isatty(0) || oldkbmd != 0xffff)
if (!isatty(0))
return;
if (tcgetattr(0, &oldterm) < 0)
if (!linux_terminal_init())
return;
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;
/* Be careful about recovering the terminal ... */
if (ioctl(0, KDGKBMODE, &oldkbmd) < 0)
return;
if (tcsetattr(0, TCSAFLUSH, &newterm) < 0)
return;
if (ioctl(0, KDSKBMODE, K_MEDIUMRAW) < 0)
{
tcsetattr(0, TCSAFLUSH, &oldterm);
linux_terminal_flush();
return;
}
@ -755,7 +732,7 @@ static void disable_terminal_input(void)
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGSEGV, &sa, NULL);
atexit(restore_terminal_input);
atexit(linux_terminal_restore_input);
}
static void *udev_input_init(void)
@ -944,7 +921,7 @@ input_driver_t input_udev = {
udev_input_get_capabilities,
"udev",
udev_input_grab_mouse,
NULL,
linux_terminal_grab_stdin,
udev_input_set_rumble,
udev_input_get_joypad_driver,
udev_input_keyboard_mapping_is_blocked,