// Probe whether an input device is exclusively grabbed (EVIOCGRAB) by another
// process (e.g. xochitl). If we can grab it, it was free -> a background watcher
// can read it alongside xochitl. If EBUSY, xochitl holds it exclusively.
#include <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main(int argc, char **argv)
{
    const char *dev = argc > 1 ? argv[1] : "/dev/input/event3";
    int fd = open(dev, O_RDONLY);
    if (fd < 0) { printf("%s: open failed: %s\n", dev, strerror(errno)); return 1; }
    int r = ioctl(fd, EVIOCGRAB, (void *)1);
    if (r == 0) {
        ioctl(fd, EVIOCGRAB, (void *)0); // release immediately
        printf("%s: NOT exclusively grabbed -> a watcher can read it\n", dev);
    } else {
        printf("%s: GRABBED by another process (%s)\n", dev, strerror(errno));
    }
    close(fd);
    return 0;
}
