-
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <sys/time.h>
- #include "linux/input.h"
-
- int stop = 0;
-
- const char * button2name (const int button) {
- static char buffer [256];
- if (button >= BTN_MOUSE && button < BTN_JOYSTICK)
- snprintf (buffer, 256, "Mouse button %d", button - BTN_MOUSE + 1);
- else
- snprintf (buffer, 256, "unknown button %d (0x%x)", button, button);
- return buffer;
- }
-
- const char * axis2name (const int axis) {
- const char * names[10] = { "X-Axis", "Y-Axis", "Z-Axis", "RX-Axis", "RY-Axis", "RZ-Axis", "HWheel",
- "Dial", "Wheel", "Misc" };
- if (axis < 0 || axis > 9) return "unknown";
- else return names[axis];
- }
-
- const char * bus2name (const int bus) {
- const char * names[] = { NULL, "PCI", "ISPNP", "USB", "HIL", "BLUETOOTH", NULL, NULL,
- NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
- "ISA", "8042", "XTKBD", "RS232", "GAMEPORT", "PARPORT", "AMIGA", "ADB",
- "I2C", "HOST", "GSC" };
-
- if (bus < 1 || bus > 0x1A || (bus > 5 && bus < 0x10)) return "unknown";
- else return names[bus];
- }
-
- int run_evdev (char * name) {
- int fd;
- int result;
- struct input_id id;
- struct input_event event;
- int version;
- char devicename [256], devicephys[256];
-
- fd = open (name, O_RDONLY);
- if (fd == -1) { perror ("open()"); return 1; }
- if (ioctl (fd, EVIOCGVERSION, &version)) { perror ("ioctl(EVIOCGVERSION)"); return 1; }
- printf ("Event Interface version %d.%d.%d\n", (version >> 16) & 255, (version >> 8) & 255, version & 255);
- if ((result = ioctl (fd, EVIOCGNAME(256), &devicename)) < 0) { perror ("ioctl(EVIOCGNAME)"); return 1; }
- printf ("Running on %s\n", devicename);
- if ((result = ioctl (fd, EVIOCGPHYS(256), &devicephys)) < 0) { perror ("ioctl(EVIOCGPHYS)"); return 1; }
- printf ("Connected at %s\n", devicephys);
- if ((result = ioctl (fd, EVIOCGID, &id)) < 0) { perror ("ioctl(EVIOCGID)"); return 1; }
- printf ("Connected to %s\n", bus2name (id.bustype));
-
- while (!stop) {
- result = read (fd, &event, sizeof (event));
- if (result != sizeof (event)) stop = 1;
- else {
- switch (event.type) {
- case EV_SYN:
- // printf ("Synchronisation event\n");
- break;
- case EV_KEY:
- printf ("Key %s event on %s\n", event.value ? "press" : "release", button2name (event.code));
- break;
- case EV_REL:
- printf ("Relative movement event on %s\n", axis2name (event.code));
- break;
- case EV_ABS:
- printf ("Absolute movement event\n");
- break;
- case EV_MSC:
- printf ("Miscellaneous event\n");
- break;
- case EV_SW:
- printf ("Switch event: ");
- switch (event.code) {
- case SW_LID:
- printf ("Lid %s\n", event.value ? "closed" : "opened");
- break;
- default:
- printf ("Unknown switch %d, state %d\n", event.code, event.value);
- }
- break;
- default:
- printf ("Unused event type %d\n", event.type);
- }
- }
- }
-
-
- close (fd);
- return 0;
- }
-
- int main (int argc, char ** argv) {
- struct stat statbuf;
-
- if (argc != 2) return 1;
- if (stat (argv[1], &statbuf)) return 1;
- if (!S_ISCHR (statbuf.st_mode)) return 1;
-
- return run_evdev (argv[1]);
- }
-