input_linux.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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(bits uint) uint {
  65. return ((bits) + (8 * SIZEOF_LONG) - 1) / (8 * SIZEOF_LONG)
  66. }
  67. func TestBit(bit uint, array []linux.UnsignedLong) bool {
  68. elem := array[ bit / ( 8 * SIZEOF_LONG)]
  69. flag := linux.UnsignedLong(1) << linux.UnsignedLong(bit % (8 * SIZEOF_LONG))
  70. return (elem & flag ) != 0
  71. }
  72. func (d * Device) Topology() (string, error) {
  73. buffer := [256]byte{}
  74. err := d.Ioctl(linux.EVIOCGPHYS(uintptr(len(buffer))), unsafe.Pointer(&buffer))
  75. return string(buffer[0:len(buffer)]), err
  76. }
  77. func (d * Device) SupportedEvents() ([]uint, error) {
  78. size := BitsToLong(linux.EV_MAX)
  79. bits := make([]linux.UnsignedLong, size)
  80. for i := uint(0); i < size; i ++ {
  81. bits[i] = 0
  82. }
  83. err := d.Ioctl(linux.EVIOCGBIT(0, uintptr(size)), unsafe.Pointer(&bits));
  84. if err != nil {
  85. return nil, err
  86. }
  87. fmt.Printf("size %d, bits: %v\n", size, bits)
  88. result := []uint{}
  89. for i := uint(0); i < uint(linux.EV_MAX); i++ {
  90. if (TestBit(i, bits)) {
  91. result = append(result, uint(i))
  92. }
  93. }
  94. return result, nil
  95. }
  96. /*
  97. printf("Supported events:\n");
  98. for (i = 0; i < EV_MAX; i++)
  99. if (TestBit(i, bit[0])) {
  100. }
  101. printf("Testing ... (interrupt to exit)\n");
  102. while (1) {
  103. rd = read(fd, ev, sizeof(struct input_event) * 64);
  104. if (rd < (int) sizeof(struct input_event)) {
  105. printf("yyy\n");
  106. perror("\nevtest: error reading");
  107. return 1;
  108. }
  109. for (i = 0; i < rd / sizeof(struct input_event); i++)
  110. if (ev[i].type == EV_SYN) {
  111. printf("Event: time %ld.%06ld, -------------- %s ------------\n",
  112. ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].code ? "Config Sync" : "Report Sync" );
  113. } else if (ev[i].type == EV_MSC && (ev[i].code == MSC_RAW || ev[i].code == MSC_SCAN)) {
  114. printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %02x\n",
  115. ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
  116. events[ev[i].type] ? events[ev[i].type] : "?",
  117. ev[i].code,
  118. names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
  119. ev[i].value);
  120. } else {
  121. printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %d\n",
  122. ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
  123. events[ev[i].type] ? events[ev[i].type] : "?",
  124. ev[i].code,
  125. names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
  126. ev[i].value);
  127. }
  128. }
  129. }
  130. */