From 2dfbc312e6ccb88f838170d8e777d48aacde2ff5 Mon Sep 17 00:00:00 2001 From: johannst Date: Sun, 13 Nov 2022 14:13:35 +0000 Subject: deploy: 026d679006e5d470bacdc74bb3082072edf31e36 --- linux/src/Makefile | 10 ++++++++ linux/src/event.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 linux/src/Makefile create mode 100644 linux/src/event.c (limited to 'linux/src') diff --git a/linux/src/Makefile b/linux/src/Makefile new file mode 100644 index 0000000..9b6e47a --- /dev/null +++ b/linux/src/Makefile @@ -0,0 +1,10 @@ +SRC = event.c +BIN = $(SRC:.c=) + +all: $(BIN) + +%: %.c + bash $< + +clean: + $(RM) $(BIN) diff --git a/linux/src/event.c b/linux/src/event.c new file mode 100644 index 0000000..d047416 --- /dev/null +++ b/linux/src/event.c @@ -0,0 +1,75 @@ +#if NODEF +gcc -o event event.c -Wall -Wextra -Werror -g +exit 0 +#endif +// Copyright (C) 2022 johannst + +#include +#include +#include +#include +#include + +#include +#include + +struct input_event { + struct timeval time; + unsigned short type; + unsigned short code; + unsigned int value; +}; + +const char* type(unsigned short t) { + static char buf[32]; + const char* fmt = "0x%x"; + switch (t) { +#define FMT(TYPE) case TYPE: fmt = #TYPE"(0x%x)"; break + FMT(EV_SYN); + FMT(EV_KEY); + FMT(EV_REL); + FMT(EV_ABS); +#undef FMT + } + snprintf(buf, sizeof(buf), fmt, t); + return buf; +} + +const char* code(unsigned short c) { + static char buf[32]; + const char* fmt = "0x%x"; + switch (c) { +#define FMT(CODE) case CODE: fmt = #CODE"(0x%x)"; break + FMT(BTN_LEFT); + FMT(BTN_RIGHT); + FMT(BTN_MIDDLE); + FMT(REL_X); + FMT(REL_Y); +#undef FMT + } + snprintf(buf, sizeof(buf), fmt, c); + return buf; +} + +const char* timefmt(const struct timeval* t) { + assert(t); + struct tm* lt = localtime(&t->tv_sec); // Returns pointer to static tm object. + static char buf[64]; + strftime(buf, sizeof(buf), "%H:%M:%S", lt); + return buf; +} + +int main(int argc, char* argv[]) { + assert(argc == 2); + + int fd = open(argv[1], O_RDONLY); + assert(fd != -1); + + struct input_event inp; + while (1) { + int ret = read(fd, &inp, sizeof(inp)); + assert(ret == sizeof(inp)); + printf("time: %s type: %s code: %s value: 0x%x\n", + timefmt(&inp.time), type(inp.type), code(inp.code), inp.value); + } +} -- cgit v1.2.3