Make keyboard path a compile time constant

This commit is contained in:
Bad Diode 2022-03-05 10:35:36 +01:00
parent 9005cb1eb9
commit d08a7dfac6
2 changed files with 27 additions and 22 deletions

View File

@ -8,6 +8,7 @@ UXN_HEAD := $(BASE_UXN)/src/uxn.h
TAL_SRC ?= $(BASE_UXN)/projects/examples/devices/screen.tal TAL_SRC ?= $(BASE_UXN)/projects/examples/devices/screen.tal
UXN_ROM ?= $(BUILD_DIR)/rom.rom UXN_ROM ?= $(BUILD_DIR)/rom.rom
UXN_ASM ?= $(BUILD_DIR)/uxnasm UXN_ASM ?= $(BUILD_DIR)/uxnasm
KBD_PATH ?= /dev/input/event1
CC ?= cc CC ?= cc
CFLAGS := -Wall -Wextra -pedantic CFLAGS := -Wall -Wextra -pedantic
@ -29,7 +30,7 @@ endif
main: $(BIN) main: $(BIN)
$(BIN): $(SRC_MAIN) $(BUILD_DIR) $(UXN_HEAD) $(BIN): $(SRC_MAIN) $(BUILD_DIR) $(UXN_HEAD)
$(CC) $(CFLAGS) -o $(BIN) $(SRC_MAIN) $(CC) $(CFLAGS) -o $(BIN) $(SRC_MAIN) -DKBD_PATH=\"$(KBD_PATH)\"
$(BUILD_DIR): $(BUILD_DIR):
mkdir -p $(BUILD_DIR) mkdir -p $(BUILD_DIR)

View File

@ -33,35 +33,39 @@ time_elapsed(Time since){
return (now.tv_sec - since.tv_sec) * 1e9 + (now.tv_nsec - since.tv_nsec); return (now.tv_sec - since.tv_sec) * 1e9 + (now.tv_nsec - since.tv_nsec);
} }
typedef struct Keyboard { typedef struct Input {
int fd; int kb_fd;
char map[KEY_MAX / 8 + 1]; char map[KEY_MAX / 8 + 1];
u8 controller; u8 controller;
} Keyboard; } Input;
static Keyboard keyboard; static Input input;
void void
init_input(void) { init_input(void) {
memset(&keyboard, 0, sizeof(Keyboard)); memset(&input, 0, sizeof(input));
input.kb_fd = -1;
const char *dev = "/dev/input/by-id/usb-Apple_Inc._Magic_Keyboard_with_Numeric_Keypad_F0T0167000DHTHKAD-if01-event-kbd"; const char *dev = KBD_PATH;
keyboard.fd = open(dev, O_RDONLY); input.kb_fd = open(dev, O_RDONLY);
if (keyboard.fd == -1) { if (input.kb_fd == -1) {
// TODO: should this be a warning and still work for applications that // NOTE: Some applications may not require a keyboard so this is
// don't require a keyboard? // optional, but we are still displaying an error.
fprintf(stderr, "error: no couldn't open keyboard %s: %s.\n", dev, strerror(errno)); fprintf(stderr, "error: no couldn't open keyboard %s: %s.\n", dev, strerror(errno));
exit(EXIT_FAILURE);
} }
} }
void void
poll_keyboard(void) { poll_keyboard(void) {
if (input.kb_fd == -1) {
return;
}
char map[KEY_MAX / 8 + 1]; char map[KEY_MAX / 8 + 1];
memset(map, 0, sizeof(map)); memset(map, 0, sizeof(map));
ioctl(keyboard.fd, EVIOCGKEY(sizeof(map)), map); ioctl(input.kb_fd, EVIOCGKEY(sizeof(map)), map);
for (size_t i = 0; i < sizeof(map); i++) { for (size_t i = 0; i < sizeof(map); i++) {
keyboard.map[i] |= map[i]; input.map[i] |= map[i];
} }
} }
@ -72,9 +76,9 @@ handle_keyboard(void) {
// bool ctrl_mod = false; // bool ctrl_mod = false;
// bool alt_mod = false; // bool alt_mod = false;
// bool meta_mod = false; // bool meta_mod = false;
for (size_t i = 0; i < sizeof(keyboard.map); i++) { for (size_t i = 0; i < sizeof(input.map); i++) {
for (size_t j = 0; j < 8; j++) { for (size_t j = 0; j < 8; j++) {
char key = keyboard.map[i] & (1 << j); char key = input.map[i] & (1 << j);
if (key) { if (key) {
char key_code = i * 8 + j; char key_code = i * 8 + j;
switch (key_code) { switch (key_code) {
@ -94,9 +98,9 @@ handle_keyboard(void) {
// Handle normal keys. // Handle normal keys.
u8 controller_now = 0; u8 controller_now = 0;
for (size_t i = 0; i < sizeof(keyboard.map); i++) { for (size_t i = 0; i < sizeof(input.map); i++) {
for (size_t j = 0; j < 8; j++) { for (size_t j = 0; j < 8; j++) {
char key = keyboard.map[i] & (1 << j); char key = input.map[i] & (1 << j);
if (key) { if (key) {
char key_code = i * 8 + j; char key_code = i * 8 + j;
// Normal keys. // Normal keys.
@ -200,15 +204,15 @@ handle_keyboard(void) {
} }
} }
if (controller_now != keyboard.controller) { if (controller_now != input.controller) {
devctrl->dat[2] = controller_now; devctrl->dat[2] = controller_now;
uxn_eval(&u, mempeek16(devctrl->dat, 0)); uxn_eval(&u, mempeek16(devctrl->dat, 0));
keyboard.controller = controller_now; input.controller = controller_now;
} }
// Reset keyboard state. // Reset input state.
devctrl->dat[3] = 0; devctrl->dat[3] = 0;
memset(keyboard.map, 0, sizeof(keyboard.map)); memset(input.map, 0, sizeof(input.map));
} }
void void