mirror of
https://github.com/captain-amygdala/pistorm.git
synced 2026-02-06 00:04:54 +00:00
[WIP] Keyboard event handling
This commit is contained in:
50
emulator.c
50
emulator.c
@@ -89,7 +89,7 @@ char mouse_buttons = 0;
|
||||
#define KICKBASE 0xF80000
|
||||
#define KICKSIZE 0x7FFFF
|
||||
|
||||
int mem_fd, mouse_fd = -1;
|
||||
int mem_fd, mouse_fd = -1, keyboard_fd = -1;
|
||||
int mem_fd_gpclk;
|
||||
int gayle_emulation_enabled = 1;
|
||||
void *gpio_map;
|
||||
@@ -99,6 +99,7 @@ void *gpclk_map;
|
||||
unsigned int cpu_type = M68K_CPU_TYPE_68000;
|
||||
unsigned int loop_cycles = 300;
|
||||
struct emulator_config *cfg = NULL;
|
||||
char keyboard_file[256] = "/dev/input/event1";
|
||||
|
||||
// I/O access
|
||||
volatile unsigned int *gpio;
|
||||
@@ -237,6 +238,11 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
}
|
||||
|
||||
keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
|
||||
if (keyboard_fd == -1) {
|
||||
printf("Failed to open keyboard event source.\n");
|
||||
}
|
||||
|
||||
sched_setscheduler(0, SCHED_FIFO, &priority);
|
||||
mlockall(MCL_CURRENT); // lock in memory to keep us from paging out
|
||||
|
||||
@@ -349,6 +355,7 @@ int main(int argc, char *argv[]) {
|
||||
else
|
||||
printf("\n IPL Thread created successfully\n");
|
||||
*/
|
||||
char c = 0;
|
||||
|
||||
m68k_pulse_reset();
|
||||
while (42) {
|
||||
@@ -362,8 +369,7 @@ int main(int argc, char *argv[]) {
|
||||
m68k_execute(loop_cycles);
|
||||
|
||||
// FIXME: Rework this to use keyboard events instead.
|
||||
/*while (kbhit()) {
|
||||
char c = getchar();
|
||||
while (get_key_char(&c)) {
|
||||
if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
|
||||
kb_hook_enabled = 1;
|
||||
printf("Keyboard hook enabled.\n");
|
||||
@@ -372,25 +378,27 @@ int main(int argc, char *argv[]) {
|
||||
kb_hook_enabled = 0;
|
||||
printf("Keyboard hook disabled.\n");
|
||||
}
|
||||
if (c == cfg->mouse_toggle_key) {
|
||||
mouse_hook_enabled ^= 1;
|
||||
printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
|
||||
mouse_dx = mouse_dy = mouse_buttons = 0;
|
||||
if (!kb_hook_enabled) {
|
||||
if (c == cfg->mouse_toggle_key) {
|
||||
mouse_hook_enabled ^= 1;
|
||||
printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
|
||||
mouse_dx = mouse_dy = mouse_buttons = 0;
|
||||
}
|
||||
if (c == 'r') {
|
||||
cpu_emulation_running ^= 1;
|
||||
printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
|
||||
}
|
||||
if (c == 'R') {
|
||||
cpu_pulse_reset();
|
||||
m68k_pulse_reset();
|
||||
printf("CPU emulation reset.\n");
|
||||
}
|
||||
if (c == 'q') {
|
||||
printf("Quitting and exiting emulator.\n");
|
||||
goto stop_cpu_emulation;
|
||||
}
|
||||
}
|
||||
if (c == 'r') {
|
||||
cpu_emulation_running ^= 1;
|
||||
printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
|
||||
}
|
||||
if (c == 'R') {
|
||||
cpu_pulse_reset();
|
||||
m68k_pulse_reset();
|
||||
printf("CPU emulation reset.\n");
|
||||
}
|
||||
if (c == 'q') {
|
||||
printf("Quitting and exiting emulator.\n");
|
||||
goto stop_cpu_emulation;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
/*
|
||||
if (toggle == 1){
|
||||
srdata = read_reg();
|
||||
|
||||
109
input/input.c
109
input/input.c
@@ -2,12 +2,113 @@
|
||||
#include <unistd.h>
|
||||
#include <linux/input.h>
|
||||
|
||||
int kbhit()
|
||||
{
|
||||
return 0;
|
||||
static int lshift = 0, rshift = 0, lctrl = 0, rctrl = 0, lalt = 0, altgr = 0;
|
||||
extern int mouse_fd;
|
||||
extern int keyboard_fd;
|
||||
|
||||
enum keypress_type {
|
||||
KEYPRESS_RELEASE,
|
||||
KEYPRESS_PRESS,
|
||||
KEYPRESS_REPEAT,
|
||||
};
|
||||
|
||||
int handle_modifier(struct input_event *ev) {
|
||||
int *target_modifier = NULL;
|
||||
if (ev->value != KEYPRESS_REPEAT && (ev->code == KEY_LEFTSHIFT || ev->code == KEY_RIGHTSHIFT || ev->code == KEY_LEFTALT || ev->code == KEY_RIGHTALT || ev->code == KEY_LEFTCTRL || ev->code == KEY_RIGHTCTRL)) {
|
||||
switch(ev->code) {
|
||||
case KEY_LEFTSHIFT:
|
||||
target_modifier = &lshift;
|
||||
break;
|
||||
case KEY_RIGHTSHIFT:
|
||||
target_modifier = &rshift;
|
||||
break;
|
||||
case KEY_LEFTALT:
|
||||
target_modifier = &lalt;
|
||||
break;
|
||||
case KEY_RIGHTALT:
|
||||
target_modifier = &altgr;
|
||||
break;
|
||||
case KEY_LEFTCTRL:
|
||||
target_modifier = &lshift;
|
||||
break;
|
||||
case KEY_RIGHTCTRL:
|
||||
target_modifier = &rshift;
|
||||
break;
|
||||
}
|
||||
*target_modifier = (ev->value == KEYPRESS_RELEASE) ? 0 : 1;
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
extern int mouse_fd;
|
||||
#define KEYCASE(a, b, c)case a: return (lshift || rshift) ? c : b;
|
||||
|
||||
char char_from_input_event(struct input_event *ev) {
|
||||
switch(ev->code) {
|
||||
KEYCASE(KEY_A, 'a', 'A');
|
||||
KEYCASE(KEY_B, 'b', 'B');
|
||||
KEYCASE(KEY_C, 'c', 'C');
|
||||
KEYCASE(KEY_D, 'd', 'D');
|
||||
KEYCASE(KEY_E, 'e', 'E');
|
||||
KEYCASE(KEY_F, 'f', 'F');
|
||||
KEYCASE(KEY_G, 'g', 'G');
|
||||
KEYCASE(KEY_H, 'h', 'H');
|
||||
KEYCASE(KEY_I, 'i', 'I');
|
||||
KEYCASE(KEY_J, 'j', 'J');
|
||||
KEYCASE(KEY_K, 'k', 'K');
|
||||
KEYCASE(KEY_L, 'l', 'L');
|
||||
KEYCASE(KEY_M, 'm', 'M');
|
||||
KEYCASE(KEY_N, 'n', 'N');
|
||||
KEYCASE(KEY_O, 'o', 'O');
|
||||
KEYCASE(KEY_P, 'p', 'P');
|
||||
KEYCASE(KEY_Q, 'q', 'Q');
|
||||
KEYCASE(KEY_R, 'r', 'R');
|
||||
KEYCASE(KEY_S, 's', 'S');
|
||||
KEYCASE(KEY_T, 't', 'T');
|
||||
KEYCASE(KEY_U, 'u', 'U');
|
||||
KEYCASE(KEY_V, 'c', 'V');
|
||||
KEYCASE(KEY_W, 'w', 'W');
|
||||
KEYCASE(KEY_X, 'x', 'X');
|
||||
KEYCASE(KEY_Y, 'y', 'Y');
|
||||
KEYCASE(KEY_Z, 'z', 'Z');
|
||||
KEYCASE(KEY_1, '1', '!');
|
||||
KEYCASE(KEY_2, '2', '@');
|
||||
KEYCASE(KEY_3, '3', '#');
|
||||
KEYCASE(KEY_4, '4', '$');
|
||||
KEYCASE(KEY_5, '5', '%');
|
||||
KEYCASE(KEY_6, '6', '^');
|
||||
KEYCASE(KEY_7, '7', '&');
|
||||
KEYCASE(KEY_8, '8', '*');
|
||||
KEYCASE(KEY_9, '9', '(');
|
||||
KEYCASE(KEY_0, '0', ')');
|
||||
KEYCASE(KEY_F12, 0x1B, 0x1B);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int get_key_char(char *c)
|
||||
{
|
||||
if (keyboard_fd == -1)
|
||||
return 0;
|
||||
|
||||
struct input_event ie;
|
||||
while(read(keyboard_fd, &ie, sizeof(struct input_event)) != -1) {
|
||||
if (ie.type == EV_KEY) {
|
||||
if (handle_modifier(&ie))
|
||||
continue;
|
||||
char ret = char_from_input_event(&ie);
|
||||
if (ret != 0) {
|
||||
*c = ret;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_mouse_status(char *x, char *y, char *b) {
|
||||
struct input_event ie;
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
int get_mouse_status(char *x, char *y, char *b);
|
||||
int kbhit();
|
||||
int get_key_char(char *c);
|
||||
|
||||
Reference in New Issue
Block a user