From 166f5384c0c9a9f7fee113eecef4c394bfeb2e19 Mon Sep 17 00:00:00 2001 From: mae Date: Sun, 10 Nov 2024 14:51:45 +0100 Subject: [PATCH] Create an inputcat binary --- flake.nix | 5 ++++ meson.build | 3 ++ src/inputcat.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 src/inputcat.c diff --git a/flake.nix b/flake.nix index e2bd257..fbc35dc 100644 --- a/flake.nix +++ b/flake.nix @@ -14,6 +14,11 @@ packages.${system} = { kobo-color-inputdev = pkgs.callPackage ./default.nix { }; + + # Cross compile to run on the kobo itself + cross.armv7l-unknown-linux-musleabihf.kobo-color-inputdev = + pkgs.pkgsCross.armv7l-hf-multiplatform.pkgsStatic.callPackage ./default.nix { }; + default = self.packages.${system}.kobo-color-inputdev; }; devShells.${system}.default = import ./shell.nix { inherit pkgs; }; diff --git a/meson.build b/meson.build index ab5947e..0e839f4 100644 --- a/meson.build +++ b/meson.build @@ -4,3 +4,6 @@ project('kobo-color-inputdev', 'c', main = executable('kobo-color-inputdev', ['src/main.c', './src/device.c'], install : true) + +inputcat = executable('inputcat', ['src/inputcat.c'], + install : true) diff --git a/src/inputcat.c b/src/inputcat.c new file mode 100644 index 0000000..333a32d --- /dev/null +++ b/src/inputcat.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define EXECUTABLE_NAME "inputcat" +#define DEFAULT_INPUTDEV "/dev/input/event1" + +int write_all(int fd, char *buf, int n) { + int written = 0; + int res = 0; + while (res >= 0 && written < n) { + res = write(fd, buf + written, n - written); + written += res; + }; + if (res >= 0) { + return 0; + } + return res; +} + +int cat(int fd) { + char buf[4096]; + int res = 0; + + while (!res) { + int n = read(fd, buf, sizeof(buf)); + res = write_all(STDOUT_FILENO, buf, n); + } + + if (res != EOF) { + fprintf(stderr, "Error writing input events to stdout: %s! Aborting\n", + strerror(res)); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} + +void print_usage(void) { + printf("Usage: %s [INPUT]\n" + "Grab exclusive access to INPUT and write it's events to stdout\n\n" + "If not specified %s will be used", + EXECUTABLE_NAME, DEFAULT_INPUTDEV); +} + +int main(int argc, char **argv) { + char *input_dev = "/dev/input/event1"; + if (argc > 2) { + print_usage(); + return EXIT_FAILURE; + } + if (argc == 2) { + input_dev = argv[1]; + } + + int src = open(input_dev, O_RDONLY); + if (ioctl(src, EVIOCGRAB, 1)) { + fprintf(stderr, "Error grabbing exclusive access to event device %s: %s\n", + input_dev, strerror(errno)); + return EXIT_FAILURE; + } + + int res = cat(src); + + ioctl(src, EVIOCGRAB, 0); + close(src); + + return res; +}