spacepaste

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <sys/ioctl.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <sys/time.h>
  11. #include "linux/input.h"
  12. int stop = 0;
  13. const char * button2name (const int button) {
  14. static char buffer [256];
  15. if (button >= BTN_MOUSE && button < BTN_JOYSTICK)
  16. snprintf (buffer, 256, "Mouse button %d", button - BTN_MOUSE + 1);
  17. else
  18. snprintf (buffer, 256, "unknown button %d (0x%x)", button, button);
  19. return buffer;
  20. }
  21. const char * axis2name (const int axis) {
  22. const char * names[10] = { "X-Axis", "Y-Axis", "Z-Axis", "RX-Axis", "RY-Axis", "RZ-Axis", "HWheel",
  23. "Dial", "Wheel", "Misc" };
  24. if (axis < 0 || axis > 9) return "unknown";
  25. else return names[axis];
  26. }
  27. const char * bus2name (const int bus) {
  28. const char * names[] = { NULL, "PCI", "ISPNP", "USB", "HIL", "BLUETOOTH", NULL, NULL,
  29. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  30. "ISA", "8042", "XTKBD", "RS232", "GAMEPORT", "PARPORT", "AMIGA", "ADB",
  31. "I2C", "HOST", "GSC" };
  32. if (bus < 1 || bus > 0x1A || (bus > 5 && bus < 0x10)) return "unknown";
  33. else return names[bus];
  34. }
  35. int run_evdev (char * name) {
  36. int fd;
  37. int result;
  38. struct input_id id;
  39. struct input_event event;
  40. int version;
  41. char devicename [256], devicephys[256];
  42. fd = open (name, O_RDONLY);
  43. if (fd == -1) { perror ("open()"); return 1; }
  44. if (ioctl (fd, EVIOCGVERSION, &version)) { perror ("ioctl(EVIOCGVERSION)"); return 1; }
  45. printf ("Event Interface version %d.%d.%d\n", (version >> 16) & 255, (version >> 8) & 255, version & 255);
  46. if ((result = ioctl (fd, EVIOCGNAME(256), &devicename)) < 0) { perror ("ioctl(EVIOCGNAME)"); return 1; }
  47. printf ("Running on %s\n", devicename);
  48. if ((result = ioctl (fd, EVIOCGPHYS(256), &devicephys)) < 0) { perror ("ioctl(EVIOCGPHYS)"); return 1; }
  49. printf ("Connected at %s\n", devicephys);
  50. if ((result = ioctl (fd, EVIOCGID, &id)) < 0) { perror ("ioctl(EVIOCGID)"); return 1; }
  51. printf ("Connected to %s\n", bus2name (id.bustype));
  52. while (!stop) {
  53. result = read (fd, &event, sizeof (event));
  54. if (result != sizeof (event)) stop = 1;
  55. else {
  56. switch (event.type) {
  57. case EV_SYN:
  58. // printf ("Synchronisation event\n");
  59. break;
  60. case EV_KEY:
  61. printf ("Key %s event on %s\n", event.value ? "press" : "release", button2name (event.code));
  62. break;
  63. case EV_REL:
  64. printf ("Relative movement event on %s\n", axis2name (event.code));
  65. break;
  66. case EV_ABS:
  67. printf ("Absolute movement event\n");
  68. break;
  69. case EV_MSC:
  70. printf ("Miscellaneous event\n");
  71. break;
  72. case EV_SW:
  73. printf ("Switch event: ");
  74. switch (event.code) {
  75. case SW_LID:
  76. printf ("Lid %s\n", event.value ? "closed" : "opened");
  77. break;
  78. default:
  79. printf ("Unknown switch %d, state %d\n", event.code, event.value);
  80. }
  81. break;
  82. default:
  83. printf ("Unused event type %d\n", event.type);
  84. }
  85. }
  86. }
  87. close (fd);
  88. return 0;
  89. }
  90. int main (int argc, char ** argv) {
  91. struct stat statbuf;
  92. if (argc != 2) return 1;
  93. if (stat (argv[1], &statbuf)) return 1;
  94. if (!S_ISCHR (statbuf.st_mode)) return 1;
  95. return run_evdev (argv[1]);
  96. }
  97.