Create an inputcat binary

This commit is contained in:
mae 2024-11-10 14:51:45 +01:00
parent 7104f1e9e1
commit 166f5384c0
3 changed files with 83 additions and 0 deletions

View file

@ -14,6 +14,11 @@
packages.${system} = packages.${system} =
{ {
kobo-color-inputdev = pkgs.callPackage ./default.nix { }; 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; default = self.packages.${system}.kobo-color-inputdev;
}; };
devShells.${system}.default = import ./shell.nix { inherit pkgs; }; devShells.${system}.default = import ./shell.nix { inherit pkgs; };

View file

@ -4,3 +4,6 @@ project('kobo-color-inputdev', 'c',
main = executable('kobo-color-inputdev', ['src/main.c', './src/device.c'], main = executable('kobo-color-inputdev', ['src/main.c', './src/device.c'],
install : true) install : true)
inputcat = executable('inputcat', ['src/inputcat.c'],
install : true)

75
src/inputcat.c Normal file
View file

@ -0,0 +1,75 @@
#include <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#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;
}