input_linux.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* package input is a thing Go wrapper around the Linux kernel input event
  2. * system. */
  3. package input
  4. import "gitlab.com/beoran/galago/os/linux"
  5. import "os"
  6. import "unsafe"
  7. import "syscall"
  8. import "fmt"
  9. import "path/filepath"
  10. // Device models an input device
  11. type Device struct {
  12. *os.File
  13. }
  14. const Directory = "/dev/input"
  15. func Open(name string) (*Device, error) {
  16. f, err := os.OpenFile(filepath.Join(Directory, name), os.O_RDWR, 0666)
  17. if err != nil {
  18. return nil, err
  19. }
  20. return &Device{File: f}, nil
  21. }
  22. func List() ([]string, error) {
  23. dir, err := os.Open(Directory)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return dir.Readdirnames(-1)
  28. }
  29. // Icotl performs an ioctl on the given de
  30. func (d * Device) Ioctl(code uint32, pointer unsafe.Pointer) error {
  31. fmt.Printf("ioctl: %d %d %d\n", uintptr(d.Fd()), uintptr(code), uintptr(pointer))
  32. _, _, errno := syscall.Syscall(
  33. syscall.SYS_IOCTL,
  34. uintptr(d.Fd()),
  35. uintptr(code),
  36. uintptr(pointer))
  37. if (errno != 0) {
  38. return errno
  39. }
  40. return nil
  41. }
  42. func (d * Device) DriverVersion() (int32, error) {
  43. res := int32(0)
  44. data := unsafe.Pointer(&res)
  45. err := d.Ioctl(linux.EVIOCGVERSION, data)
  46. return res, err
  47. }
  48. func (d * Device) Name() (string, error) {
  49. buffer := [256]byte{}
  50. err := d.Ioctl(linux.EVIOCGNAME(uintptr(len(buffer))), unsafe.Pointer(&buffer))
  51. return string(buffer[0:len(buffer)]), err
  52. }
  53. func BitsToLong(bits int) int {
  54. var ul linux.UnsignedLong
  55. return ((bits) + 8 * unsafe.Sizeof(ul) - 1) / (8 * unsafe.Sizeof(ul))
  56. }
  57. func TestBit(bit int, array []uint8) {
  58. const BITS_PER_LONG = sizeof(long) * 8)
  59. #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
  60. #define OFF(x) ((x)%BITS_PER_LONG)
  61. #define BIT(x) (1UL<<OFF(x))
  62. #define LONG(x) ((x)/BITS_PER_LONG)
  63. #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
  64. func (d * Device) Id() (linux.INPUT_id, error) {
  65. var result linux.INPUT_id
  66. err := d.Ioctl(linux.EVIOCGID, unsafe.Pointer(&result))
  67. return result, err
  68. int fd, rd, i, j, k;
  69. struct input_event ev[64];
  70. int version;
  71. unsigned short id[4];
  72. unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
  73. char name[256] = "Unknown";
  74. int abs[5];
  75. func (d * Device) SupportedEvents() ([]uint32, error) (
  76. var bit [linux.EV_MAX] uint64
  77. device.Ioctl(fd, EVIOCGBIT(0, EV_MAX), unsafe.Pointer(&bit));
  78. printf("Supported events:\n");
  79. for (i = 0; i < EV_MAX; i++)
  80. if (test_bit(i, bit[0])) {
  81. }
  82. printf("Testing ... (interrupt to exit)\n");
  83. while (1) {
  84. rd = read(fd, ev, sizeof(struct input_event) * 64);
  85. if (rd < (int) sizeof(struct input_event)) {
  86. printf("yyy\n");
  87. perror("\nevtest: error reading");
  88. return 1;
  89. }
  90. for (i = 0; i < rd / sizeof(struct input_event); i++)
  91. if (ev[i].type == EV_SYN) {
  92. printf("Event: time %ld.%06ld, -------------- %s ------------\n",
  93. ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].code ? "Config Sync" : "Report Sync" );
  94. } else if (ev[i].type == EV_MSC && (ev[i].code == MSC_RAW || ev[i].code == MSC_SCAN)) {
  95. printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %02x\n",
  96. ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
  97. events[ev[i].type] ? events[ev[i].type] : "?",
  98. ev[i].code,
  99. names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
  100. ev[i].value);
  101. } else {
  102. printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %d\n",
  103. ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
  104. events[ev[i].type] ? events[ev[i].type] : "?",
  105. ev[i].code,
  106. names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
  107. ev[i].value);
  108. }
  109. }
  110. }