input_linux_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package input
  2. import "testing"
  3. import "time"
  4. // import "os"
  5. // import "syscall"
  6. import "fmt"
  7. import "gitlab.com/beoran/galago/os/linux"
  8. const IN1 = "by-id/usb-0583_USB_2-axis_8-button_gamepad-event-joystick"
  9. const IN2 = "by-id/usb-PixArt_USB_Optical_Mouse-event-mouse"
  10. const IN3 = "by-id/usb-Logitech_USB_Receiver-if02-event-mouse"
  11. const IN = IN3
  12. func TestList(t * testing.T) {
  13. names, err := List()
  14. if err != nil {
  15. t.Errorf("Error List: %s\n", err)
  16. return
  17. }
  18. for _, name := range names {
  19. t.Logf("Device: %s\n", name)
  20. }
  21. }
  22. func TestGetDriverVersion(t * testing.T) {
  23. device , err := Open(IN)
  24. if err != nil {
  25. t.Errorf("Error Open: %s\n", err)
  26. return
  27. }
  28. defer device.Close()
  29. version, err := device.DriverVersion()
  30. if err != nil {
  31. t.Errorf("Error GetDriverVersion: %s (%s %x)\n", err,
  32. "EVIOCGVERSION", linux.EVIOCGVERSION)
  33. return
  34. }
  35. if version != linux.EV_VERSION {
  36. t.Errorf("Version %x should be %x", version, linux.EV_VERSION)
  37. }
  38. t.Logf("GetDriverVersion: %d", version)
  39. }
  40. func TestGetName(t * testing.T) {
  41. device , err := Open(IN)
  42. if err != nil {
  43. t.Errorf("Error Open: %s\n", err)
  44. return
  45. }
  46. defer device.Close()
  47. name, err := device.Name()
  48. if err != nil {
  49. t.Errorf("Error GetName: %s\n", err)
  50. return
  51. }
  52. t.Logf("GetName: %s", name)
  53. }
  54. func TestGetTopology(t * testing.T) {
  55. device , err := Open(IN)
  56. if err != nil {
  57. t.Errorf("Error Open: %s\n", err)
  58. return
  59. }
  60. defer device.Close()
  61. name, err := device.Topology()
  62. if err != nil {
  63. t.Errorf("Error Topology: %s\n", err)
  64. return
  65. }
  66. t.Logf("Topology: %s", name)
  67. }
  68. func TestGetId(t * testing.T) {
  69. device , err := Open(IN)
  70. if err != nil {
  71. t.Errorf("Error Open: %s\n", err)
  72. return
  73. }
  74. defer device.Close()
  75. id, err := device.Id()
  76. if err != nil {
  77. t.Errorf("Error GetId: %s\n", err)
  78. return
  79. }
  80. t.Logf("GetId: bustype 0x%x, vendor 0x%x, product 0x%x, version 0x%x",
  81. id.Bustype, id.Vendor, id.Product, id.Version)
  82. }
  83. func TestSupportedEvents(t * testing.T) {
  84. device , err := Open(IN)
  85. if err != nil {
  86. t.Errorf("Error Open: %s\n", err)
  87. return
  88. }
  89. defer device.Close()
  90. events, err := device.SupportedEvents()
  91. t.Logf("Supported events:\n")
  92. for i, ev := range events {
  93. t.Logf("Supported: %d: %d %s", i, int(ev), ev.Name())
  94. }
  95. }
  96. func TestSupportedKeys(t * testing.T) {
  97. device , err := Open(IN)
  98. if err != nil {
  99. t.Errorf("Error Open: %s\n", err)
  100. return
  101. }
  102. defer device.Close()
  103. keys, err := device.SupportedKeys()
  104. t.Logf("Supported keys:\n")
  105. for i, key := range keys {
  106. t.Logf("Supported: %d: %d %s", i, int(key), key.Name())
  107. }
  108. }
  109. func TestSupportedAxes(t * testing.T) {
  110. device , err := Open(IN)
  111. if err != nil {
  112. t.Errorf("Error Open: %s\n", err)
  113. return
  114. }
  115. defer device.Close()
  116. axes, err := device.SupportedAxes()
  117. t.Logf("Supported axes:\n")
  118. for i, ax := range axes {
  119. t.Logf("Supported: %d: %d %s\n", i, uint(ax.Index), ax.String())
  120. }
  121. }
  122. func TestRelativeAxes(t * testing.T) {
  123. device , err := Open(IN)
  124. if err != nil {
  125. t.Errorf("Error Open: %s\n", err)
  126. return
  127. }
  128. defer device.Close()
  129. events, err := device.SupportedRelatives()
  130. t.Logf("Supported relative axes:\n")
  131. for i, ev := range events {
  132. t.Logf("Supported: %d: %d %s", i, int(ev), ev.Name())
  133. }
  134. }
  135. func TestReadEvents(t * testing.T) {
  136. device , err := Open(IN)
  137. if err != nil {
  138. t.Errorf("Error Open: %s\n", err)
  139. return
  140. }
  141. defer device.Close()
  142. events, err := device.ReadEvents()
  143. if err != nil {
  144. t.Errorf("Error ReadEvents: %s\n", err)
  145. return
  146. }
  147. t.Logf("Events:\n")
  148. for i, ev := range events {
  149. t.Logf("Event: %d: %s\n", i, ev.String())
  150. }
  151. }
  152. func TestReadEventsBlock(t * testing.T) {
  153. device , err := Open(IN)
  154. if err != nil {
  155. t.Errorf("Error Open: %s\n", err)
  156. return
  157. }
  158. defer device.Close()
  159. events, err := device.ReadEventsBlock()
  160. if err != nil {
  161. t.Errorf("Error ReadEvents: %s\n", err)
  162. return
  163. }
  164. t.Logf("Events:\n")
  165. for i, ev := range events {
  166. t.Logf("Event: %d: %s\n", i, ev.String())
  167. }
  168. }
  169. func TestDriver(t * testing.T) {
  170. driver := NewDriver()
  171. done := false
  172. go func() {
  173. for !done {
  174. dev := <- driver.Connect
  175. fmt.Printf("Connected: %s\n", dev.String())
  176. }
  177. }()
  178. go func() {
  179. for !done {
  180. dev := <- driver.Disconnect
  181. fmt.Printf("Disconnected: %s\n", dev.String())
  182. }
  183. }()
  184. go func() {
  185. for !done {
  186. err := <- driver.Errors
  187. fmt.Printf("Error: %s\n", err)
  188. }
  189. }()
  190. go func() {
  191. for !done {
  192. eve := <- driver.Events
  193. fmt.Printf("%s\n", eve.String())
  194. if eve.Type == linux.EV_KEY && (
  195. eve.Code == linux.BTN_JOYSTICK || eve.Code == linux.BTN_MOUSE) {
  196. done = true
  197. }
  198. }
  199. }()
  200. driver.Start()
  201. for ! done {
  202. time.Sleep(1 * time.Second)
  203. }
  204. done = true
  205. }