input_linux.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 (d * Device) Id() (linux.INPUT_id, error) {
  54. var result linux.INPUT_id
  55. err := d.Ioctl(linux.EVIOCGID, unsafe.Pointer(&result))
  56. return result, err
  57. }
  58. // The Linux developers thought it was a great idea a to use an array of
  59. // unsigned longs for the bit flags of input devices. Of course, the length
  60. // of an unsigned long is platform dependent which then entails all sorts of
  61. // gymnastics to extract the bits from the array...
  62. const SIZEOF_LONG = uint(unsafe.Sizeof(*((*linux.UnsignedLong)(nil))))
  63. const BITS_PER_LONG = SIZEOF_LONG * 8
  64. func BitsToLong(ebits linux.EventType) uint {
  65. bits := uint(ebits)
  66. return ((bits) + 8 * SIZEOF_LONG - 1) / (8 * SIZEOF_LONG)
  67. }
  68. func TestBit(bit uint, array []linux.UnsignedLong) bool {
  69. return ((array[ bit / BITS_PER_LONG ]) & (1 << (bit%BITS_PER_LONG))) != 0
  70. }
  71. func (d * Device) SupportedEvents() ([]linux.EventType, error) {
  72. size := BitsToLong(linux.EV_MAX)
  73. bits := make([]linux.UnsignedLong, size)
  74. for i := uint(0); i < size; i ++ {
  75. bits[i] = 0
  76. }
  77. err := d.Ioctl(linux.EVIOCGBIT(0, uintptr(size)), unsafe.Pointer(&bits));
  78. if err != nil {
  79. return nil, err
  80. }
  81. fmt.Printf("size %d, bits: %v\n", size, bits)
  82. result := []linux.EventType{}
  83. for i := uint(0); i < uint(linux.EV_MAX); i++ {
  84. if (TestBit(i, bits)) {
  85. result = append(result, linux.EventType(i))
  86. }
  87. }
  88. return result, nil
  89. }
  90. /*
  91. printf("Supported events:\n");
  92. for (i = 0; i < EV_MAX; i++)
  93. if (TestBit(i, bit[0])) {
  94. }
  95. printf("Testing ... (interrupt to exit)\n");
  96. while (1) {
  97. rd = read(fd, ev, sizeof(struct input_event) * 64);
  98. if (rd < (int) sizeof(struct input_event)) {
  99. printf("yyy\n");
  100. perror("\nevtest: error reading");
  101. return 1;
  102. }
  103. for (i = 0; i < rd / sizeof(struct input_event); i++)
  104. if (ev[i].type == EV_SYN) {
  105. printf("Event: time %ld.%06ld, -------------- %s ------------\n",
  106. ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].code ? "Config Sync" : "Report Sync" );
  107. } else if (ev[i].type == EV_MSC && (ev[i].code == MSC_RAW || ev[i].code == MSC_SCAN)) {
  108. printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %02x\n",
  109. ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
  110. events[ev[i].type] ? events[ev[i].type] : "?",
  111. ev[i].code,
  112. names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
  113. ev[i].value);
  114. } else {
  115. printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %d\n",
  116. ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
  117. events[ev[i].type] ? events[ev[i].type] : "?",
  118. ev[i].code,
  119. names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
  120. ev[i].value);
  121. }
  122. }
  123. }
  124. */