diff --git a/Makefile b/Makefile index 3d41a4a..8548dbc 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ UXN_HEAD := $(BASE_UXN)/src/uxn.h TAL_SRC ?= $(BASE_UXN)/projects/examples/devices/screen.tal UXN_ROM ?= $(BUILD_DIR)/rom.rom UXN_ASM ?= $(BUILD_DIR)/uxnasm +KBD_PATH ?= /dev/input/event1 CC ?= cc CFLAGS := -Wall -Wextra -pedantic @@ -29,7 +30,7 @@ endif main: $(BIN) $(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): mkdir -p $(BUILD_DIR) diff --git a/src/main.c b/src/main.c index 445fb17..aac3211 100644 --- a/src/main.c +++ b/src/main.c @@ -33,35 +33,39 @@ time_elapsed(Time since){ return (now.tv_sec - since.tv_sec) * 1e9 + (now.tv_nsec - since.tv_nsec); } -typedef struct Keyboard { - int fd; +typedef struct Input { + int kb_fd; char map[KEY_MAX / 8 + 1]; u8 controller; -} Keyboard; +} Input; -static Keyboard keyboard; +static Input 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"; - keyboard.fd = open(dev, O_RDONLY); - if (keyboard.fd == -1) { - // TODO: should this be a warning and still work for applications that - // don't require a keyboard? + const char *dev = KBD_PATH; + input.kb_fd = open(dev, O_RDONLY); + if (input.kb_fd == -1) { + // NOTE: Some applications may not require a keyboard so this is + // optional, but we are still displaying an error. fprintf(stderr, "error: no couldn't open keyboard %s: %s.\n", dev, strerror(errno)); - exit(EXIT_FAILURE); } } void poll_keyboard(void) { + if (input.kb_fd == -1) { + return; + } + char map[KEY_MAX / 8 + 1]; 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++) { - keyboard.map[i] |= map[i]; + input.map[i] |= map[i]; } } @@ -72,9 +76,9 @@ handle_keyboard(void) { // bool ctrl_mod = false; // bool alt_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++) { - char key = keyboard.map[i] & (1 << j); + char key = input.map[i] & (1 << j); if (key) { char key_code = i * 8 + j; switch (key_code) { @@ -94,9 +98,9 @@ handle_keyboard(void) { // Handle normal keys. 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++) { - char key = keyboard.map[i] & (1 << j); + char key = input.map[i] & (1 << j); if (key) { char key_code = i * 8 + j; // Normal keys. @@ -200,15 +204,15 @@ handle_keyboard(void) { } } - if (controller_now != keyboard.controller) { + if (controller_now != input.controller) { devctrl->dat[2] = controller_now; 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; - memset(keyboard.map, 0, sizeof(keyboard.map)); + memset(input.map, 0, sizeof(input.map)); } void