int main (int argc, char **argv) { int rc; int fd; const char *path; struct libevdev *dev; struct measurements measurements = {0}; if (argc < 2) return usage(); path = argv[1]; if (path[0] == '-') return usage(); fd = open(path, O_RDONLY|O_NONBLOCK); if (fd < 0) { fprintf(stderr, "Error opening the device: %s\n", strerror(errno)); return 1; } rc = libevdev_new_from_fd(fd, &dev); if (rc != 0) { fprintf(stderr, "Error fetching the device info: %s\n", strerror(-rc)); return 1; } if (libevdev_grab(dev, LIBEVDEV_GRAB) != 0) { fprintf(stderr, "Error: cannot grab the device, something else is grabbing it.\n"); fprintf(stderr, "Use 'fuser -v %s' to find processes with an open fd\n", path); return 1; } libevdev_grab(dev, LIBEVDEV_UNGRAB); printf("Mouse %s on %s\n", libevdev_get_name(dev), path); printf("Move the device 250mm/10in or more along the x-axis.\n"); printf("Pause 3 seconds before movement to reset, Ctrl+C to exit.\n"); setbuf(stdout, NULL); rc = mainloop(dev, &measurements); printf("\n"); print_summary(&measurements); printf("\n"); printf("Entry for hwdb match (replace XXX with the resolution in DPI):\n" "mouse:%s:v%04xp%04x:name:%s:\n" " MOUSE_DPI=XXX@%d\n", bustype(libevdev_get_id_bustype(dev)), libevdev_get_id_vendor(dev), libevdev_get_id_product(dev), libevdev_get_name(dev), (int)measurements.max_frequency); libevdev_free(dev); close(fd); return rc; }
int main(int argc, char **argv) { struct libevdev *dev = NULL; struct libevdev_uinput *uidev = NULL; int fd, ret; if (argc < 2){ fprintf(stderr, "Usage: %s <event device>\n", argv[0]); return 1; } //open the event device file descriptor fd = open(argv[1], O_RDONLY); if (fd < 0) {perror("Failed to open device"); return fd;} //create a libevdev device from the event file descriptor ret = libevdev_new_from_fd(fd, &dev); if (ret < 0) {perror("Failed to create libevdev device"); return ret;} //create a device connected to the virtual device interface /dev/uinput ret = libevdev_uinput_create_from_device( dev, LIBEVDEV_UINPUT_OPEN_MANAGED, &uidev); if (ret < 0) {perror("Failed to create uinput device"); return ret;} sleep(1); //EVIOCGRAB the event, this means no other client will receive the events from the device, including your window manager. // "This is generally a bad idea. Don't do this." - libevdev documentation //We need to do this to avoid douplicate keyboard events. ret = libevdev_grab(dev, LIBEVDEV_GRAB); //start the interceptor and never return intercept(uidev, dev); libevdev_uinput_destroy(uidev); libevdev_grab(dev, LIBEVDEV_UNGRAB); libevdev_free(dev); return ret; }