Add linux_common.c

This commit is contained in:
twinaphex 2015-11-14 08:50:10 +01:00
parent 81b741e359
commit d59215a678
5 changed files with 111 additions and 47 deletions

View File

@ -77,6 +77,7 @@ endif
ifneq ($(findstring Linux,$(OS)),)
LIBS += -lrt
OBJ += input/drivers/linuxraw_input.o \
input/drivers/linux_common.o \
input/drivers_joypad/linuxraw_joypad.o \
frontend/drivers/platform_linux.o
endif

View File

@ -367,6 +367,7 @@ INPUT
#endif
#if defined(__linux__) && !defined(ANDROID)
#include "../input/drivers/linux_common.c"
#include "../input/drivers/linuxraw_input.c"
#include "../input/drivers_joypad/linuxraw_joypad.c"
#endif

View File

@ -0,0 +1,68 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
*
* 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 <linux/input.h>
#include <linux/kd.h>
#include <termios.h>
#include "linux_common.h"
static struct termios oldTerm, newTerm;
static long oldKbmd = 0xffff;
static bool linux_stdin_claimed = false;
void linux_terminal_restore_input(void)
{
if (oldKbmd != 0xffff)
{
ioctl(0, KDSKBMODE, oldKbmd);
tcsetattr(0, TCSAFLUSH, &oldTerm);
oldKbmd = 0xffff;
}
linux_stdin_claimed = false;
}
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 (ioctl(0, KDGKBMODE, &oldKbmd) != 0)
return false;
}
tcsetattr(0, TCSAFLUSH, &newTerm);
return true;
}
void linux_terminal_claim_stdin(void)
{
/* We need to disable use of stdin command interface if
* stdin is supposed to be used for input. */
linux_stdin_claimed = true;
}
bool linux_terminal_grab_stdin(void *data)
{
return linux_stdin_claimed;
}

View File

@ -0,0 +1,29 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
*
* 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/>.
*/
#ifndef _LINUX_COMMON_H
#define _LINUX_COMMON_H
#include <boolean.h>
void linux_terminal_restore_input(void);
bool linux_terminal_init(void);
void linux_terminal_claim_stdin(void);
bool linux_terminal_grab_stdin(void *data);
#endif

View File

@ -19,21 +19,17 @@
#include <sys/ioctl.h>
#include <linux/input.h>
#include <linux/kd.h>
#include <termios.h>
#include <signal.h>
#include <boolean.h>
#include "../../general.h"
#include "linux_common.h"
#include "../input_keymaps.h"
#include "../input_common.h"
#include "../input_joypad.h"
static long oldKbmd = 0xffff;
static bool linuxraw_stdin_claimed = false;
static struct termios oldTerm, newTerm;
typedef struct linuxraw_input
{
bool blocked;
@ -41,22 +37,9 @@ typedef struct linuxraw_input
bool state[0x80];
} linuxraw_input_t;
static void linuxraw_reset_kbmd(void)
{
if (oldKbmd != 0xffff)
{
ioctl(0, KDSKBMODE, oldKbmd);
tcsetattr(0, TCSAFLUSH, &oldTerm);
oldKbmd = 0xffff;
}
linuxraw_stdin_claimed = false;
}
static void linuxraw_exit_gracefully(int sig)
{
linuxraw_reset_kbmd();
linux_terminal_restore_input();
kill(getpid(), sig);
}
@ -71,7 +54,7 @@ static void *linuxraw_input_init(void)
if (!isatty(0))
return NULL;
if (linuxraw_stdin_claimed)
if (linux_terminal_grab_stdin(NULL))
{
RARCH_WARN("stdin is already used for content loading. Cannot use stdin for input.\n");
return NULL;
@ -81,27 +64,16 @@ static void *linuxraw_input_init(void)
if (!linuxraw)
return NULL;
if (oldKbmd == 0xffff)
if (!linux_terminal_init())
{
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)
{
free(linuxraw);
return NULL;
}
free(linuxraw);
return NULL;
}
tcsetattr(0, TCSAFLUSH, &newTerm);
if (ioctl(0, KDSKBMODE, K_MEDIUMRAW) != 0)
{
linuxraw_reset_kbmd();
linux_terminal_restore_input();
free(linuxraw);
return NULL;
}
@ -119,24 +91,17 @@ static void *linuxraw_input_init(void)
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGSEGV, &sa, NULL);
atexit(linuxraw_reset_kbmd);
atexit(linux_terminal_restore_input);
linuxraw->joypad = input_joypad_init_driver(
settings->input.joypad_driver, linuxraw);
input_keymaps_init_keyboard_lut(rarch_key_map_linux);
/* We need to disable use of stdin command interface if
* stdin is supposed to be used for input. */
linuxraw_stdin_claimed = true;
linux_terminal_claim_stdin();
return linuxraw;
}
static bool linuxraw_grab_stdin(void *data)
{
return linuxraw_stdin_claimed;
}
static bool linuxraw_key_pressed(linuxraw_input_t *linuxraw, int key)
{
unsigned sym = input_keymaps_translate_rk_to_keysym((enum retro_key)key);
@ -225,7 +190,7 @@ static void linuxraw_input_free(void *data)
if (linuxraw->joypad)
linuxraw->joypad->destroy();
linuxraw_reset_kbmd();
linux_terminal_restore_input();
free(data);
}
@ -319,7 +284,7 @@ input_driver_t input_linuxraw = {
linuxraw_get_capabilities,
"linuxraw",
linuxraw_grab_mouse,
linuxraw_grab_stdin,
linux_terminal_grab_stdin,
linuxraw_set_rumble,
linuxraw_get_joypad_driver,
linuxraw_keyboard_mapping_is_blocked,