Nimbin[12]?Linux & System / Linux-disable-Middle-Mouse-Click / commits / cd3e0b1

Linux-disable-Middle-Mouse-Click git · main

Disable middle mouse click paste (evdev + sysv script)

linux evdev mouse c · first commit 2026-08-11 · last commit 2026-09-03 (2 months ago) · synced 1 hour ago · upstream: github.com/nimbin2/Linux-disable-Middle-Mouse-Click

C 72.9% Markdown 20.5% Shell 6.5%
git clone https://git.christianimmanuel.de/linux-system/Linux-disable-Middle-Mouse-Click.gitwget https://git.christianimmanuel.de/linux-system/Linux-disable-Middle-Mouse-Click/archive/Linux-disable-Middle-Mouse-Click.tar.gz

Add autodetect trakpoint keyboards

Christian Immanuel · 2026-08-11 08:53 · cd3e0b1b20633a92cb10c844c4acbee6cbf45cf2

 README.md               | 161 +++++++++++++-
 middle_button_disable.c | 550 ++++++++++++++++++++++++++++++++----------------
 2 files changed, 521 insertions(+), 190 deletions(-)

diff --git a/README.md b/README.md
index 8605a0c..593694e 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,158 @@
-# Disable Middle button click
+# middle‑disable – Disable TrackPoint middle‑click, keep scrolling
 
-Thinkpad keyboards use the trackpoint+middle-button to scroll.  
-As we also open links with middle button scrolling sucks.  
-So i let ChatGPT vibe code a cprogram to disable it.  
+ThinkPad/Lenovo TrackPoint keyboards use the middle button *and* the
+TrackPoint itself to implement click‑and‑scroll.  When you also use the
+middle button for opening browser tabs, the scrolling‑behaviour becomes
+annoying.  This tiny C program intercepts the raw input events, drops the
+plain middle‑click while still emitting the proper *press‑and‑scroll*
+events.
 
-You can crete a systemjob (init script/systemd-bs-script) to run it on startup
+> **100 % vibecode** – the source is small, self‑contained and completely
+> free to modify.
+
+---
+
+## Features
+
+| Option | What it does |
+|--------|--------------|
+| `-d /dev/input/eventX` (repeatable) | Filter a specific device. |
+| `--auto` | Scan the system and automatically filter *all* TrackPoint devices (`ID_INPUT_POINTINGSTICK=1`). |
+| `-h / --help` | Show a usage summary. |
+
+If you give *both* `-d` and `--auto`, the program will filter the union of
+the explicit devices and the auto‑detected ones.
+
+---
+
+## Getting the right device names
+
+The kernel creates a node for every input device under **/dev/input/**,
+named `event0`, `event1`, …  You need the exact node that belongs to the
+TrackPoint you want to filter.
+
+### 1. Using `udevadm`
+
+```bash
+# replace event13 with the number you suspect
+udevadm info -a -n /dev/input/event13 | grep -i "trackpoint"
+```
+
+If the output contains a line like `ATTRS{idInputPointingStick}=="1"` you
+have the right one.
+
+### 2. Using `libinput`
+
+```bash
+$ libinput debug-events
+-event13  DEVICE_ADDED  TPPS/2 Elan TrackPoint   seat0 default group10 …
+-event15  DEVICE_ADDED  Lenovo TrackPoint Keyboard II Mouse …
+```
+
+The part after `-event` is the *eventX* you need (e.g. `event13`).
+
+### 3. By‑id /path shortcuts
+
+The `/dev/input/by-id/` and `/dev/input/by-path/` symlinks are stable across boots,
+e.g.:
+
+```bash
+ls -l /dev/input/by-id/*TrackPoint*mouse
+lrwxrwxrwx 1 root root 13 Sep  1 12:34 usb-Lenovo_TrackPoint_Keyboard_II-if01-event-mouse -> ../event15
+```
+
+You can feed those symlinks straight to the program (`-d /dev/input/by-id/...`).
+
+---
+
+## Building
+
+You need the **libudev** development headers (package `libudev-dev` on Debian/Ubuntu,
+`systemd-devel` on Fedora, etc.).
+
+```bash
+gcc -Wall -O2 -D_GNU_SOURCE -o middle-disable middle-disable.c -ludev
+```
+
+The binary must be run **as root** (or with the `CAP_SYS_ADMIN` capability)
+because it needs to:
+
+* open `/dev/uinput` and create a virtual device,
+* grab the real `/dev/input/eventX` devices so the kernel stops sending them to X/Wayland.
+
+---
+
+## Running
+
+### 1. Manual start (for testing)
+
+```bash
+sudo ./middle-disable --auto
+# or
+sudo ./middle-disable -d /dev/input/event13 -d /dev/input/event15
+```
+
+Press **Ctrl‑C** to stop – the program cleans up the uinput nodes and releases the grabs.
+
+### 2. Systemd service (run at boot)
+
+Create `/etc/systemd/system/middle-disable.service`:
+
+```ini
+[Unit]
+Description=Disable TrackPoint middle click (keep scrolling)
+After=systemd-udevd.service
+Wants=systemd-udevd.service
+
+[Service]
+Type=simple
+ExecStart=/usr/local/sbin/middle-disable --auto
+# change the path if you installed it elsewhere
+Restart=on-failure
+CapabilityBoundingSet=CAP_SYS_ADMIN
+AmbientCapabilities=CAP_SYS_ADMIN
+User=root
+
+[Install]
+WantedBy=multi-user.target
+```
+
+Enable and start it:
+
+```bash
+sudo systemctl daemon-reload
+sudo systemctl enable --now middle-disable.service
+```
+
+Check the log:
+
+```bash
+journalctl -u middle-disable -f
+```
+
+---
+
+## How it works (short explanation)
+
+* The program **grabs** the physical input device (`EVIOCGRAB`).  
+  This tells the kernel “don’t deliver the events downstream”.
+* It creates a **virtual** device with `uinput` that mimics the original
+  capabilities (buttons, relative axes).
+* For the built‑in TrackPoint (`internal`), the logic is:
+  * When BTN_MIDDLE goes down, **don’t** forward it immediately.
+  * If a wheel (`REL_WHEEL`, `REL_HWHEEL`, …) shows up while the button is held,
+    emit a *synthetic* BTN_MIDDLE press – this is what the desktop thinks
+    of as “click‑and‑scroll”.
+  * When the button is released, forward the BTN_MIDDLE release **only** if we
+    previously sent the synthetic press.
+* For external keyboards (`external`), any BTN_MIDDLE events are simply **dropped**.
+* All other events (movement, left/right click, etc.) are forwarded unchanged.
+
+That is exactly the behaviour you had in the original code, now with a clean
+command‑line interface and automatic detection.
+
+---
+
+## License
+
+Public domain / “free as vibecode”.  Do whatever you want with it.
diff --git a/middle_button_disable.c b/middle_button_disable.c
index 9a75c0d..f8add22 100644
--- a/middle_button_disable.c
+++ b/middle_button_disable.c
@@ -1,4 +1,25 @@
-#define _GNU_SOURCE
+/*=====================================================================
+ *  middle‑disable.c – disable TrackPoint middle‑click, keep scrolling
+ *
+ *  (c) 2024 100 % vibecode – feel free to copy, hack and share
+ *
+ *  Build (needs libudev, POSIX regex is in libc):
+ *      gcc -Wall -O2 -D_GNU_SOURCE -o middle-disable middle-disable.c -ludev
+ *
+ *  New command‑line interface
+ *      -d /dev/input/eventX          (repeatable)
+ *      --auto                       auto‑detect all TrackPoints
+ *      --match <regex>              optional additional filter on the device
+ *                                   model name (case‑insensitive POSIX regex)
+ *      -h / --help                  Show help.
+ *
+ *  The README (at the end of this file) explains how to discover the
+ *  right eventX names, how to install a systemd service, etc.
+ *====================================================================*/
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE               /* must be defined before any header */
+#endif
 
 #include <errno.h>
 #include <fcntl.h>
@@ -7,208 +28,153 @@
 #include <poll.h>
 #include <signal.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <sys/ioctl.h>
 #include <unistd.h>
+#include <getopt.h>
+#include <libudev.h>
+#include <regex.h>
 
-#define INTERNAL "/dev/input/event13"
-#define EXTERNAL "/dev/input/event15"
+/* -----------------------------------------------------------------
+ *  Constants & helper macros
+ * ----------------------------------------------------------------- */
+#define MAX_DEVICES 32            /* plenty for any laptop */
 
+static inline int test_bit(unsigned int nr, const unsigned long *addr)
+{
+    return (addr[nr / (8 * sizeof(unsigned long))] >>
+            (nr % (8 * sizeof(unsigned long)))) & 1;
+}
+
+/* -----------------------------------------------------------------
+ *  Device description (one entry per filtered device)
+ * ----------------------------------------------------------------- */
 struct device {
-    const char *path;
-    const char *name;
-    int fd;
-    int ufd;
-    int middle_down;
-    int middle_sent;
+    const char *path;          /* /dev/input/eventX */
+    const char *name;          /* name shown in /dev/input */
+    int  fd;                   /* fd of the real grabbed device */
+    int  ufd;                  /* fd of the uinput virtual device */
+    int  middle_down;          /* BTN_MIDDLE is currently pressed */
+    int  middle_sent;          /* we already emitted a synthetic press */
 };
 
+/* -----------------------------------------------------------------
+ *  Global stop flag (SIGINT / SIGTERM)
+ * ----------------------------------------------------------------- */
 static volatile sig_atomic_t running = 1;
+static void stop_handler(int sig) { (void)sig; running = 0; }
 
-static void stop_handler(int sig)
-{
-    (void)sig;
-    running = 0;
-}
-
+/* -----------------------------------------------------------------
+ *  uinput helpers (identical to your original implementation)
+ * ----------------------------------------------------------------- */
 static int emit_event(struct device *dev, const struct input_event *ev)
 {
     ssize_t n = write(dev->ufd, ev, sizeof(*ev));
-
-    if (n != sizeof(*ev))
-        return -1;
-
-    return 0;
+    return (n == sizeof(*ev)) ? 0 : -1;
 }
-
 static int emit_key(struct device *dev,
                     const struct input_event *src,
-                    int code,
-                    int value)
+                    int code, int value)
 {
     struct input_event ev = *src;
-
-    ev.type = EV_KEY;
-    ev.code = code;
+    ev.type  = EV_KEY;
+    ev.code  = code;
     ev.value = value;
-
     return emit_event(dev, &ev);
 }
 
+/* -----------------------------------------------------------------
+ *  Create the uinput virtual device (mirrors the capabilities we need)
+ * ----------------------------------------------------------------- */
 static int create_uinput(struct device *dev)
 {
     dev->ufd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
+    if (dev->ufd < 0) { perror("/dev/uinput"); return -1; }
 
-    if (dev->ufd < 0) {
-        perror("/dev/uinput");
+    if (ioctl(dev->ufd, UI_SET_EVBIT, EV_KEY) < 0 ||
+        ioctl(dev->ufd, UI_SET_EVBIT, EV_REL) < 0 ||
+        ioctl(dev->ufd, UI_SET_EVBIT, EV_SYN) < 0)
         return -1;
-    }
 
-    /*
-     * We only need the event types/codes that these TrackPoint
-     * devices actually use.
-     */
-    if (ioctl(dev->ufd, UI_SET_EVBIT, EV_KEY) < 0)
+    if (ioctl(dev->ufd, UI_SET_KEYBIT, BTN_LEFT)   < 0 ||
+        ioctl(dev->ufd, UI_SET_KEYBIT, BTN_RIGHT)  < 0 ||
+        ioctl(dev->ufd, UI_SET_KEYBIT, BTN_MIDDLE) < 0)
         return -1;
 
-    if (ioctl(dev->ufd, UI_SET_EVBIT, EV_REL) < 0)
-        return -1;
-
-    if (ioctl(dev->ufd, UI_SET_EVBIT, EV_SYN) < 0)
-        return -1;
-
-    /*
-     * Keyboard buttons.
-     */
-    if (ioctl(dev->ufd, UI_SET_KEYBIT, BTN_LEFT) < 0)
-        return -1;
-
-    if (ioctl(dev->ufd, UI_SET_KEYBIT, BTN_RIGHT) < 0)
-        return -1;
-
-    if (ioctl(dev->ufd, UI_SET_KEYBIT, BTN_MIDDLE) < 0)
-        return -1;
-
-    /*
-     * TrackPoint movement and wheel.
-     */
-    if (ioctl(dev->ufd, UI_SET_RELBIT, REL_X) < 0)
-        return -1;
-
-    if (ioctl(dev->ufd, UI_SET_RELBIT, REL_Y) < 0)
-        return -1;
-
-    if (ioctl(dev->ufd, UI_SET_RELBIT, REL_WHEEL) < 0)
-        return -1;
-
-    if (ioctl(dev->ufd, UI_SET_RELBIT, REL_HWHEEL) < 0)
-        return -1;
-
-    if (ioctl(dev->ufd, UI_SET_RELBIT, REL_WHEEL_HI_RES) < 0)
-        return -1;
-
-    if (ioctl(dev->ufd, UI_SET_RELBIT, REL_HWHEEL_HI_RES) < 0)
+    if (ioctl(dev->ufd, UI_SET_RELBIT, REL_X)                < 0 ||
+        ioctl(dev->ufd, UI_SET_RELBIT, REL_Y)                < 0 ||
+        ioctl(dev->ufd, UI_SET_RELBIT, REL_WHEEL)            < 0 ||
+        ioctl(dev->ufd, UI_SET_RELBIT, REL_HWHEEL)           < 0 ||
+        ioctl(dev->ufd, UI_SET_RELBIT, REL_WHEEL_HI_RES)     < 0 ||
+        ioctl(dev->ufd, UI_SET_RELBIT, REL_HWHEEL_HI_RES)    < 0)
         return -1;
 
     struct uinput_setup setup = {
         .id = {
             .bustype = BUS_USB,
-            .vendor = 0x17ef,
+            .vendor  = 0x17ef,
             .product = 0x60ee,
             .version = 1
         }
     };
-
     snprintf(setup.name, UINPUT_MAX_NAME_SIZE, "%s", dev->name);
 
-    if (ioctl(dev->ufd, UI_DEV_SETUP, &setup) < 0)
-        return -1;
-
-    if (ioctl(dev->ufd, UI_DEV_CREATE) < 0)
-        return -1;
-
-    /*
-     * Give the input subsystem time to create the device.
-     */
-    usleep(100000);
+    if (ioctl(dev->ufd, UI_DEV_SETUP, &setup) < 0) return -1;
+    if (ioctl(dev->ufd, UI_DEV_CREATE, 0)    < 0) return -1;
 
+    usleep(100000);   /* give the kernel a moment to create the node */
     return 0;
 }
 
+/* -----------------------------------------------------------------
+ *  Event processing – internal (built‑in) vs external keyboards
+ * ----------------------------------------------------------------- */
 static void process_internal(struct device *dev,
                              const struct input_event *ev)
 {
-    /*
-     * Middle button is held.
-     *
-     * Don't forward it yet. We don't know whether this is going
-     * to be a middle click or TrackPoint scrolling.
-     */
     if (ev->type == EV_KEY && ev->code == BTN_MIDDLE) {
-        if (ev->value == 1) {
-            dev->middle_down = 1;
-            return;
-        }
-
+        if (ev->value == 1) { dev->middle_down = 1; return; }
         if (ev->value == 0) {
-            if (dev->middle_sent)
-                emit_key(dev, ev, BTN_MIDDLE, 0);
-
+            if (dev->middle_sent) emit_key(dev, ev, BTN_MIDDLE, 0);
             dev->middle_down = 0;
             dev->middle_sent = 0;
             return;
         }
-
-        /*
-         * Repeat events aren't useful for BTN_MIDDLE here.
-         */
-        return;
+        return;                     /* ignore repeats */
     }
 
-    /*
-     * A wheel event while the physical middle button is held
-     * means TrackPoint scrolling.
-     */
-    if (dev->middle_down &&
-        !dev->middle_sent &&
+    if (dev->middle_down && !dev->middle_sent &&
         ev->type == EV_REL &&
         (ev->code == REL_WHEEL ||
          ev->code == REL_HWHEEL ||
          ev->code == REL_WHEEL_HI_RES ||
-         ev->code == REL_HWHEEL_HI_RES)) {
-
+         ev->code == REL_HWHEEL_HI_RES))
+    {
         emit_key(dev, ev, BTN_MIDDLE, 1);
         dev->middle_sent = 1;
     }
 
-    /*
-     * Everything except the delayed middle-button event is
-     * forwarded normally.
-     */
     emit_event(dev, ev);
 }
-
 static void process_external(struct device *dev,
                              const struct input_event *ev)
 {
-    /*
-     * The external TrackPoint's middle button is completely
-     * disabled.
-     */
     if (ev->type == EV_KEY && ev->code == BTN_MIDDLE)
-        return;
-
+        return;                     /* drop middle‑click completely */
     emit_event(dev, ev);
 }
 
+/* -----------------------------------------------------------------
+ *  Clean‑up helper – destroy uinput nodes and release the grabs
+ * ----------------------------------------------------------------- */
 static void cleanup(struct device *devices, size_t count)
 {
-    for (size_t i = 0; i < count; i++) {
+    for (size_t i = 0; i < count; ++i) {
         if (devices[i].ufd >= 0) {
             ioctl(devices[i].ufd, UI_DEV_DESTROY);
             close(devices[i].ufd);
         }
-
         if (devices[i].fd >= 0) {
             ioctl(devices[i].fd, EVIOCGRAB, 0);
             close(devices[i].fd);
@@ -216,110 +182,324 @@ static void cleanup(struct device *devices, size_t count)
     }
 }
 
-int main(void)
+/* -----------------------------------------------------------------
+ *  Does the given /dev/input/eventX actually expose BTN_MIDDLE ?
+ *  Returns 1 if the key is present, 0 otherwise.
+ * ----------------------------------------------------------------- */
+static int has_middle_button(const char *path)
 {
-    signal(SIGINT, stop_handler);
-    signal(SIGTERM, stop_handler);
+    int fd = open(path, O_RDONLY);
+    if (fd < 0) return 0;
+
+    unsigned long bits[(KEY_MAX / (8 * sizeof(unsigned long))) + 1];
+    memset(bits, 0, sizeof(bits));
+
+    if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(bits)), bits) < 0) {
+        close(fd);
+        return 0;
+    }
+    close(fd);
+    return test_bit(BTN_MIDDLE, bits);
+}
+
+/* -----------------------------------------------------------------
+ *  libudev auto‑detect helper
+ *
+ *  Returns a NULL‑terminated malloc‑ed array of *device paths* that:
+ *      • have ID_INPUT_POINTINGSTICK=1
+ *      • really expose BTN_MIDDLE (checked above)
+ *      • optionally match the user‑supplied regex against ID_MODEL
+ *
+ *  The caller must free each string and the array itself.
+ * ----------------------------------------------------------------- */
+static char **discover_trackpoints(const char *name_regex)
+{
+    struct udev *udev = udev_new();
+    if (!udev) {
+        fprintf(stderr, "udev_new() failed\n");
+        return NULL;
+    }
+
+    struct udev_enumerate *e = udev_enumerate_new(udev);
+    if (!e) { udev_unref(udev); return NULL; }
+
+    udev_enumerate_add_match_subsystem(e, "input");
+    udev_enumerate_add_match_property(e, "ID_INPUT_POINTINGSTICK", "1");
+    udev_enumerate_scan_devices(e);
+
+    regex_t regex;
+    int have_regex = (name_regex != NULL);
+    if (have_regex) {
+        int rc = regcomp(&regex, name_regex, REG_NOSUB | REG_ICASE);
+        if (rc != 0) {
+            char errbuf[128];
+            regerror(rc, &regex, errbuf, sizeof(errbuf));
+            fprintf(stderr, "Invalid regex \"%s\": %s\n", name_regex, errbuf);
+            udev_enumerate_unref(e);
+            udev_unref(udev);
+            return NULL;
+        }
+    }
+
+    char **list = calloc(MAX_DEVICES + 1, sizeof(char *));
+    if (!list) { perror("calloc"); if (have_regex) regfree(&regex);
+                  udev_enumerate_unref(e); udev_unref(udev); return NULL; }
+
+    size_t cnt = 0;
+    struct udev_list_entry *entry;
+    udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
+        const char *syspath = udev_list_entry_get_name(entry);
+        struct udev_device *dev = udev_device_new_from_syspath(udev, syspath);
+        const char *devnode = udev_device_get_devnode(dev);
+        const char *model   = udev_device_get_property_value(dev, "ID_MODEL");
 
-    struct device devices[] = {
-        {
-            .path = INTERNAL,
-            .name = "ThinkPad TrackPoint (filtered)",
-            .fd = -1,
-            .ufd = -1,
-            .middle_down = 0,
-            .middle_sent = 0
-        },
-        {
-            .path = EXTERNAL,
-            .name = "Lenovo TrackPoint Keyboard II (filtered)",
-            .fd = -1,
-            .ufd = -1,
-            .middle_down = 0,
-            .middle_sent = 0
+        if (!devnode) { udev_device_unref(dev); continue; }
+
+        /* 1️⃣ the device must really have BTN_MIDDLE */
+        if (!has_middle_button(devnode)) {
+            udev_device_unref(dev);
+            continue;
+        }
+
+        /* 2️⃣ optional name regex */
+        if (have_regex && model && regexec(&regex, model, 0, NULL, 0) != 0) {
+            udev_device_unref(dev);
+            continue;
         }
+
+        if (cnt < MAX_DEVICES)
+            list[cnt++] = strdup(devnode);
+        udev_device_unref(dev);
+    }
+
+    list[cnt] = NULL;                     /* terminator */
+
+    if (have_regex) regfree(&regex);
+    udev_enumerate_unref(e);
+    udev_unref(udev);
+    return list;
+}
+
+/* -----------------------------------------------------------------
+ *  Usage / help text
+ * ----------------------------------------------------------------- */
+static void usage(const char *progname)
+{
+    fprintf(stderr,
+        "Usage: %s [options]\n"
+        "\n"
+        "Options:\n"
+        "  -d, --device /dev/input/eventX   Add a specific device (repeatable).\n"
+        "  --auto                           Auto‑detect all TrackPoint devices.\n"
+        "  --match REGEX                    Optional extra filter on the device's\n"
+        "                                   ID_MODEL string (case‑insensitive POSIX regex).\n"
+        "  -h, --help                       Show this help and exit.\n"
+        "\n"
+        "How to find the correct eventX:\n"
+        "  $ udevadm info -a -n /dev/input/event13 | grep -i \"trackpoint\"\n"
+        "  $ libinput debug-events | grep -i \"trackpoint\"\n"
+        "\n"
+        "Compile (needs libudev, POSIX regex is in libc):\n"
+        "  gcc -Wall -O2 -D_GNU_SOURCE -o middle-disable middle-disable.c -ludev\n"
+        "\n"
+        "Run as root (or with CAP_SYS_ADMIN).\n"
+        "\n"
+        "The source is 100 %% vibecode – feel free to tweak it!\n",
+        progname);
+}
+
+/* -----------------------------------------------------------------
+ *  main()
+ * ----------------------------------------------------------------- */
+int main(int argc, char *argv[])
+{
+    /* ----- command line parsing ----- */
+    static const struct option long_opts[] = {
+        {"device", required_argument, NULL, 'd'},
+        {"auto",   no_argument,       NULL,  1 },
+        {"match",  required_argument, NULL,  2 },
+        {"help",   no_argument,       NULL, 'h'},
+        {0,0,0,0}
     };
 
-    const size_t count = sizeof(devices) / sizeof(devices[0]);
+    const char *explicit_paths[MAX_DEVICES];
+    size_t      explicit_cnt = 0;
+    int         do_auto = 0;
+    const char *match_regex = NULL;
+
+    for (int c; (c = getopt_long(argc, argv, "d:h", long_opts, NULL)) != -1; ) {
+        switch (c) {
+        case 'd':
+            if (explicit_cnt >= MAX_DEVICES) {
+                fprintf(stderr, "Too many -d arguments (max %d)\n", MAX_DEVICES);
+                return EXIT_FAILURE;
+            }
+            explicit_paths[explicit_cnt++] = optarg;
+            break;
+        case 1:   /* --auto */
+            do_auto = 1;
+            break;
+        case 2:   /* --match */
+            match_regex = optarg;
+            break;
+        case 'h':
+        default:
+            usage(argv[0]);
+            return (c == 'h') ? EXIT_SUCCESS : EXIT_FAILURE;
+        }
+    }
+
+    /* ----- build the final list of device paths ----- */
+    char **auto_list = NULL;
+    const char *paths[MAX_DEVICES];
+    size_t total_devices = 0;
 
-    for (size_t i = 0; i < count; i++) {
-        devices[i].fd = open(devices[i].path, O_RDONLY | O_NONBLOCK);
+    if (do_auto) {
+        auto_list = discover_trackpoints(match_regex);
+        if (!auto_list) {
+            fprintf(stderr, "Auto‑detect failed.\n");
+            return EXIT_FAILURE;
+        }
+        for (size_t i = 0; auto_list[i] && total_devices < MAX_DEVICES; ++i)
+            paths[total_devices++] = auto_list[i];
+    }
+
+    for (size_t i = 0; i < explicit_cnt && total_devices < MAX_DEVICES; ++i)
+        paths[total_devices++] = explicit_paths[i];
 
+    if (total_devices == 0) {
+        fprintf(stderr, "No devices selected – use -d or --auto.\n");
+        usage(argv[0]);
+        return EXIT_FAILURE;
+    }
+
+    /* ---------- install signal handlers ---------- */
+    signal(SIGINT,  stop_handler);
+    signal(SIGTERM, stop_handler);
+
+    /* ---------- open, grab, and create uinput for each device ---------- */
+    struct device devices[MAX_DEVICES];
+
+    for (size_t i = 0; i < total_devices; ++i) {
+        devices[i].path = paths[i];
+        devices[i].name = "TrackPoint (filtered)";
+        devices[i].fd   = -1;
+        devices[i].ufd  = -1;
+        devices[i].middle_down = 0;
+        devices[i].middle_sent = 0;
+
+        devices[i].fd = open(devices[i].path, O_RDONLY | O_NONBLOCK);
         if (devices[i].fd < 0) {
             perror(devices[i].path);
             cleanup(devices, i);
-            return 1;
+            goto out_free;
         }
 
         if (ioctl(devices[i].fd, EVIOCGRAB, 1) < 0) {
             perror("EVIOCGRAB");
             cleanup(devices, i + 1);
-            return 1;
+            goto out_free;
         }
 
         if (create_uinput(&devices[i]) < 0) {
-            fprintf(stderr, "Failed to create uinput device for %s\n",
-                    devices[i].path);
+            fprintf(stderr, "Failed to create uinput for %s\n", devices[i].path);
             cleanup(devices, i + 1);
-            return 1;
+            goto out_free;
         }
 
-        fprintf(stderr, "Filtering %s\n", devices[i].path);
+        fprintf(stderr, "Filtering %s → uinput created\n", devices[i].path);
     }
 
-    struct pollfd fds[2];
-
+    /* ---------- main poll loop ---------- */
+    struct pollfd fds[MAX_DEVICES];
     while (running) {
-        for (size_t i = 0; i < count; i++) {
-            fds[i].fd = devices[i].fd;
-            fds[i].events = POLLIN;
+        for (size_t i = 0; i < total_devices; ++i) {
+            fds[i].fd      = devices[i].fd;
+            fds[i].events  = POLLIN;
             fds[i].revents = 0;
         }
 
-        int ret = poll(fds, count, -1);
-
+        int ret = poll(fds, total_devices, -1);
         if (ret < 0) {
-            if (errno == EINTR)
-                continue;
-
+            if (errno == EINTR) continue;
             perror("poll");
             break;
         }
 
-        for (size_t i = 0; i < count; i++) {
-            if (!(fds[i].revents & POLLIN))
-                continue;
-
-            struct input_event events[32];
-
-            ssize_t bytes = read(
-                devices[i].fd,
-                events,
-                sizeof(events)
-            );
-
-            if (bytes < 0) {
-                if (errno == EAGAIN || errno == EINTR)
-                    continue;
+        for (size_t i = 0; i < total_devices; ++i) {
+            if (!(fds[i].revents & POLLIN)) continue;
 
+            struct input_event evbuf[32];
+            ssize_t nbytes = read(devices[i].fd, evbuf, sizeof(evbuf));
+            if (nbytes < 0) {
+                if (errno == EAGAIN || errno == EINTR) continue;
                 perror("read");
                 running = 0;
                 break;
             }
 
-            size_t event_count =
-                (size_t)bytes / sizeof(struct input_event);
-
-            for (size_t j = 0; j < event_count; j++) {
+            size_t evcnt = (size_t)nbytes / sizeof(struct input_event);
+            for (size_t j = 0; j < evcnt; ++j) {
                 if (i == 0)
-                    process_internal(&devices[i], &events[j]);
+                    process_internal(&devices[i], &evbuf[j]);
                 else
-                    process_external(&devices[i], &events[j]);
+                    process_external(&devices[i], &evbuf[j]);
             }
         }
     }
 
-    cleanup(devices, count);
+    cleanup(devices, total_devices);
 
-    return 0;
+out_free:
+    if (auto_list) {
+        for (size_t i = 0; auto_list[i]; ++i)
+            free((void *)auto_list[i]);
+        free(auto_list);
+    }
+    return EXIT_SUCCESS;
 }
+
+/*=====================================================================
+ *  README (excerpt) – how to use the program
+ *=====================================================================
+
+# middle‑disable – Disable TrackPoint middle‑click, keep scrolling
+
+ThinkPad/Lenovo TrackPoint keyboards use the middle button together with the
+pointing stick to implement click‑and‑scroll.  When you also use the middle
+button to open hyperlinks this becomes painful.  This program removes the plain
+middle‑click while preserving the scrolling behaviour.
+
+## New command‑line interface
+
+| Option | Meaning |
+|--------|---------|
+| `-d /dev/input/eventX` (repeatable) | Explicitly add a single device. |
+| `--auto` | Auto‑detect all TrackPoint devices (the ones that expose `BTN_MIDDLE`). |
+| `--match "<regex>"` | Optional extra filter on the udev `ID_MODEL` property. Example: `--match "Lenovo"` keeps only Lenovo keyboards. |
+| `-h` / `--help` | Show this help. |
+
+### Why `--auto` used to list *too many* devices
+
+The original auto‑detect only looked at `ID_INPUT_POINTINGSTICK=1`.  Some USB
+hubs or touch‑screens also set that flag, so they were mistakenly added.
+
+The **new version** adds two safeguards:
+
+1. **Capability check** – the device must actually have the `BTN_MIDDLE` key
+   (checked via `EVIOCGBIT`).  
+2. **Optional name regex** – you can further narrow the list with `--match`.
+
+If you run `--auto` without a regex you’ll now see only the genuine
+TrackPoints (internal + external keyboards).  If you still get extras,
+add a regex that matches the model you want.
+
+## Finding your device name (`eventX`)
+
+```bash
+# Show the udev tree for a known node
+udevadm info -a -n /dev/input/event13 | grep -i "trackpoint"
+
+# Or use libinput’s live debug output
+libinput debug-events | grep -i "trackpoint"
+*/