aboutsummaryrefslogtreecommitdiffhomepage
path: root/linux/src
diff options
context:
space:
mode:
Diffstat (limited to 'linux/src')
-rw-r--r--linux/src/Makefile10
-rw-r--r--linux/src/event.c75
2 files changed, 85 insertions, 0 deletions
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 <stdio.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <unistd.h>
+#include <time.h>
+
+#include <sys/time.h>
+#include <linux/input-event-codes.h>
+
+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);
+ }
+}