From 2dfbc312e6ccb88f838170d8e777d48aacde2ff5 Mon Sep 17 00:00:00 2001 From: johannst Date: Sun, 13 Nov 2022 14:13:35 +0000 Subject: deploy: 026d679006e5d470bacdc74bb3082072edf31e36 --- linux/coredump.html | 2 +- linux/cryptsetup.html | 2 +- linux/index.html | 3 +- linux/input.html | 298 ++++++++++++++++++++++++++++++++++++++++++++++++ linux/ptrace_scope.html | 2 +- linux/src/Makefile | 10 ++ linux/src/event.c | 75 ++++++++++++ linux/swap.html | 6 +- linux/systemd.html | 2 +- 9 files changed, 392 insertions(+), 8 deletions(-) create mode 100644 linux/input.html create mode 100644 linux/src/Makefile create mode 100644 linux/src/event.c (limited to 'linux') diff --git a/linux/coredump.html b/linux/coredump.html index 34164eb..49dd420 100644 --- a/linux/coredump.html +++ b/linux/coredump.html @@ -75,7 +75,7 @@ diff --git a/linux/cryptsetup.html b/linux/cryptsetup.html index 12fa885..c95efb8 100644 --- a/linux/cryptsetup.html +++ b/linux/cryptsetup.html @@ -75,7 +75,7 @@ diff --git a/linux/index.html b/linux/index.html index 66b03d2..0f11d8d 100644 --- a/linux/index.html +++ b/linux/index.html @@ -75,7 +75,7 @@ @@ -144,6 +144,7 @@
  • ptrace_scope
  • cryptsetup
  • swap
  • +
  • input
  • diff --git a/linux/input.html b/linux/input.html new file mode 100644 index 0000000..66eba36 --- /dev/null +++ b/linux/input.html @@ -0,0 +1,298 @@ + + + + + + input - Notes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    + +
    + + + + + + + +
    +
    +

    Linux input

    +

    Some notes on using /dev/input/* device driver files.

    +

    mouseX / mice

    +

    These device files are created by the [mousedev] driver.

    +
      +
    • /dev/input/mouseX represents the input stream for a SINGLE mouse device.
    • +
    • /dev/input/mice represents the merged input stream for ALL mouse devices.
    • +
    +

    The data stream consists of 3 bytes per event. An event is encoded as (BTN, X, Y).

    +
      +
    • BTN button pressed
    • +
    • X movement in x-direction -1 -> left and 1 -> right
    • +
    • Y movement in y-direction -1 -> down and 1 -> up
    • +
    +

    The raw data stream can be inspected as follows.

    +
    sudo cat /dev/input/mice | od -tx1 -w3 -v
    +
    +

    eventX

    +

    These device files are created by the [evdev] driver.

    +
      +
    • /dev/input/eventX represents the generic input event interface a SINGLE input deivece.
    • +
    +

    Input events are encoded as given by the input_event struct below. Reading +from the eventX device file will always yield whole number of input events.

    +
    struct input_event {
    +    struct timeval time;
    +    unsigned short type;
    +    unsigned short code;
    +    unsigned int value;
    +};
    +
    +

    On most 64bit machines the raw data stream can be inspected as follows.

    +
    sudo cat /dev/input/event4 | od -tx1 -w24 -v
    +
    +

    Identifyin device files.

    +

    To find out which device file is assigned to which input device the following +file /proc/bus/input/devices in the proc filesystem can be consulted.

    +

    This yields entries as follows and shows which Handlers are assigned to which +Name.

    +
    I: Bus=0018 Vendor=04f3 Product=0033 Version=0000
    +N: Name="Elan Touchpad"
    +...
    +H: Handlers=event15 mouse0
    +...
    +
    +

    Example: Toying with /dev/input/eventX

    +

    Once compiled, the example should be run as sudo ./event /dev/input/eventX.

    +
    #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);
    +    }
    +}
    +
    +

    [mousedev]: TODO /home/johannst/dev/linux/drivers/input/mousedev.c +[evdev]: TODO /home/johannst/dev/linux/drivers/input/evdev.c

    + +
    + + +
    +
    + + + +
    + + + + + + + + + + + + diff --git a/linux/ptrace_scope.html b/linux/ptrace_scope.html index 4c813c7..d927fde 100644 --- a/linux/ptrace_scope.html +++ b/linux/ptrace_scope.html @@ -75,7 +75,7 @@ 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); + } +} diff --git a/linux/swap.html b/linux/swap.html index 594e238..7452069 100644 --- a/linux/swap.html +++ b/linux/swap.html @@ -75,7 +75,7 @@ @@ -187,7 +187,7 @@ For example as systemd service:

    -
    @@ -199,7 +199,7 @@ For example as systemd service:

    - diff --git a/linux/systemd.html b/linux/systemd.html index 382fbab..fe18c3a 100644 --- a/linux/systemd.html +++ b/linux/systemd.html @@ -75,7 +75,7 @@ -- cgit v1.2.3