input_linux.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "golang.org/x/sys/unix"
  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 device
  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 (d * Device) Topology() (string, error) {
  68. buffer := [256]byte{}
  69. err := d.Ioctl(linux.EVIOCGPHYS(uintptr(len(buffer))), unsafe.Pointer(&buffer))
  70. return string(buffer[0:len(buffer)]), err
  71. }
  72. func TestBit(array []uint8, bit uint) bool {
  73. elem := uint(array[ bit / 8 ])
  74. flag := uint(1) << uint(bit % 8)
  75. return (elem & flag ) != 0
  76. }
  77. type SupportedEvent uint
  78. func (se SupportedEvent) Name() string {
  79. return linux.EvToString(uint(se))
  80. }
  81. func (se SupportedEvent) String() string {
  82. return se.Name()
  83. }
  84. func (d * Device) SupportedEvents() ([]SupportedEvent, error) {
  85. var bits [linux.EV_MAX / 8 + 1]uint8
  86. size := unsafe.Sizeof(bits)
  87. err := d.Ioctl(linux.EVIOCGBIT(0, uintptr(size)), unsafe.Pointer(&bits));
  88. if err != nil {
  89. return nil, err
  90. }
  91. fmt.Printf("size %d, bits: %v\n", size, bits)
  92. result := []SupportedEvent{}
  93. for i := uint(0); i < uint(linux.EV_MAX); i++ {
  94. if (TestBit(bits[0:len(bits)],i)) {
  95. result = append(result, SupportedEvent(uint(i)))
  96. }
  97. }
  98. return result, nil
  99. }
  100. // More Go-like names for the low leve kernel api structs
  101. type InputAbsinfo linux.INPUT_absinfo
  102. type InputEvent linux.INPUT_event
  103. type AbsoluteAxis struct {
  104. Index uint
  105. InputAbsinfo
  106. }
  107. func (ax AbsoluteAxis) Name() string {
  108. return linux.AbsToString(ax.Index)
  109. }
  110. func (ax AbsoluteAxis) String() string {
  111. return fmt.Sprintf("%s %d (min:%d max:%d flat:%d fuzz:%d)",
  112. ax.Name(),
  113. ax.Value,
  114. ax.Minimum,
  115. ax.Maximum,
  116. ax.Flat,
  117. ax.Fuzz)
  118. }
  119. func (ev InputEvent) String() string {
  120. return fmt.Sprintf("Event: Time: %d Type: %s, Code: %d, Value:%d",
  121. ev.Time,
  122. linux.EvToString(uint(ev.Type)),
  123. ev.Code,
  124. ev.Value)
  125. }
  126. // Read performs a read syscall on the given device
  127. func (d * Device) Read(pointer unsafe.Pointer, size uintptr) (uintptr, error) {
  128. fmt.Printf("read: %d %d %d\n", uintptr(d.Fd()), uintptr(pointer), uintptr(size))
  129. read, _, errno := syscall.Syscall(
  130. syscall.SYS_READ,
  131. uintptr(d.Fd()),
  132. uintptr(pointer),
  133. uintptr(size))
  134. if (errno != 0) {
  135. return read, errno
  136. }
  137. return read, nil
  138. }
  139. const READ_EVENTS_MAX = 64
  140. func (d * Device) ReadEvents() ([]InputEvent, error) {
  141. var events [READ_EVENTS_MAX]InputEvent
  142. size := unsafe.Sizeof(events)
  143. read, err := d.Read(unsafe.Pointer(&events), size)
  144. if err != nil {
  145. return nil, err
  146. }
  147. len := read / unsafe.Sizeof(events[0])
  148. return events[0:len], nil
  149. }
  150. func (d * Device) SupportedAxes() ([]AbsoluteAxis, error) {
  151. var abs_feat InputAbsinfo;
  152. var abs_b [linux.ABS_MAX/8 + 1]uint8;
  153. size := unsafe.Sizeof(abs_b)
  154. err := d.Ioctl(linux.EVIOCGBIT(linux.EV_ABS, size), unsafe.Pointer(&abs_b))
  155. if err != nil {
  156. return nil, err
  157. }
  158. result := []AbsoluteAxis{}
  159. fmt.Printf("Supported Absolute axes:\n");
  160. for yalv := uint(0); yalv < linux.ABS_MAX; yalv++ {
  161. if TestBit(abs_b[0:len(abs_b)], uint(yalv)) {
  162. fmt.Printf(" Absolute axis 0x%02x %s", yalv, linux.AbsToString(yalv))
  163. err = d.Ioctl(linux.EVIOCGABS(uint32(yalv)), unsafe.Pointer(&abs_feat))
  164. if err != nil {
  165. return result, err
  166. }
  167. axis := AbsoluteAxis{Index: yalv, InputAbsinfo: abs_feat}
  168. result = append(result, axis)
  169. }
  170. }
  171. return result, nil
  172. }
  173. /*
  174. printf("Supported events:\n");
  175. for (i = 0; i < EV_MAX; i++)
  176. if (TestBit(i, bit[0])) {
  177. }
  178. printf("Testing ... (interrupt to exit)\n");
  179. while (1) {
  180. rd = read(fd, ev, sizeof(struct input_event) * 64);
  181. if (rd < (int) sizeof(struct input_event)) {
  182. printf("yyy\n");
  183. perror("\nevtest: error reading");
  184. return 1;
  185. }
  186. for (i = 0; i < rd / sizeof(struct input_event); i++)
  187. if (ev[i].type == EV_SYN) {
  188. printf("Event: time %ld.%06ld, -------------- %s ------------\n",
  189. ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].code ? "Config Sync" : "Report Sync" );
  190. } else if (ev[i].type == EV_MSC && (ev[i].code == MSC_RAW || ev[i].code == MSC_SCAN)) {
  191. printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %02x\n",
  192. ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
  193. events[ev[i].type] ? events[ev[i].type] : "?",
  194. ev[i].code,
  195. names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
  196. ev[i].value);
  197. } else {
  198. printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %d\n",
  199. ev[i].time.tv_sec, ev[i].time.tv_usec, ev[i].type,
  200. events[ev[i].type] ? events[ev[i].type] : "?",
  201. ev[i].code,
  202. names[ev[i].type] ? (names[ev[i].type][ev[i].code] ? names[ev[i].type][ev[i].code] : "?") : "?",
  203. ev[i].value);
  204. }
  205. }
  206. }
  207. */