input_linux.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. package linux
  2. import "unsafe"
  3. // This file is a Go version of <linux/input.h>
  4. type input_event struct {
  5. time timeval
  6. type_ uint16
  7. code uint16
  8. value int32
  9. }
  10. /*
  11. * Protocol version.
  12. */
  13. const EV_VERSION=0x010001
  14. /*
  15. * IOCTLs (0x00 - 0x7f)
  16. */
  17. type input_id struct {
  18. bustype uint16
  19. vendor uint16
  20. product uint16
  21. version uint16
  22. }
  23. /**
  24. * struct input_absinfo - used by EVIOCGABS/EVIOCSABS ioctls
  25. * @value: latest reported value for the axis.
  26. * @minimum: specifies minimum value for the axis.
  27. * @maximum: specifies maximum value for the axis.
  28. * @fuzz: specifies fuzz value that is used to filter noise from
  29. * the event stream.
  30. * @flat: values that are within this value will be discarded by
  31. * joydev interface and reported as 0 instead.
  32. * @resolution: specifies resolution for the values reported for
  33. * the axis.
  34. *
  35. * Note that input core does not clamp reported values to the
  36. * [minimum, maximum] limits, such task is left to userspace.
  37. *
  38. * Resolution for main axes (ABS_X, ABS_Y, ABS_Z) is reported in
  39. * units per millimeter (units/mm), resolution for rotational axes
  40. * (ABS_RX, ABS_RY, ABS_RZ) is reported in units per radian.
  41. */
  42. type input_absinfo struct {
  43. value int32;
  44. minimum int32;
  45. maximum int32;
  46. fuzz int32;
  47. flat int32;
  48. resolution int32;
  49. }
  50. const INPUT_KEYMAP_BYINDEX = (1 << 0)
  51. /**
  52. * struct input_keymap_entry - used by EVIOCGKEYCODE/EVIOCSKEYCODE ioctls
  53. * @scancode: scancode represented in machine-endian form.
  54. * @len: length of the scancode that resides in @scancode buffer.
  55. * @index: index in the keymap, may be used instead of scancode
  56. * @flags: allows to specify how kernel should handle the request. For
  57. * example, setting INPUT_KEYMAP_BYINDEX flag indicates that kernel
  58. * should perform lookup in keymap by @index instead of @scancode
  59. * @keycode: key code assigned to this scancode
  60. *
  61. * The structure is used to retrieve and modify keymap data. Users have
  62. * option of performing lookup either by @scancode itself or by @index
  63. * in keymap entry. EVIOCGKEYCODE will also return scancode or index
  64. * (depending on which element was used to perform lookup).
  65. */
  66. type input_keymap_entry struct {
  67. flags uint8;
  68. len uint8;
  69. index uint16;
  70. keycode uint32;
  71. scancode [32]uint8;
  72. };
  73. type input_mask struct {
  74. type_ uint32;
  75. codes_size uint32;
  76. codes_ptr uint32;
  77. };
  78. type uint32p = [2]uint32
  79. const sizeof_uint32p = unsafe.Sizeof(*((*uint32p)(nil)))
  80. const sizeof_int32 = unsafe.Sizeof(*((*int32)(nil)))
  81. const sizeof_uint32 = unsafe.Sizeof(*((*uint32)(nil)))
  82. const sizeof_input_id = unsafe.Sizeof(*((*input_id)(nil)))
  83. const sizeof_input_keymap_entry = unsafe.Sizeof(*((*input_keymap_entry)(nil)))
  84. const sizeof_input_absinfo = unsafe.Sizeof(*((*input_absinfo)(nil)))
  85. const sizeof_input_mask = unsafe.Sizeof(*((*input_mask)(nil)))
  86. const sizeof_ff_effect = unsafe.Sizeof(*((*ff_effect)(nil)))
  87. var EVIOCGVERSION = IOR('E', 0x01, sizeof_int32) /* get driver version */
  88. var EVIOCGID = IOR('E', 0x02, sizeof_input_id) /* get device ID */
  89. var EVIOCGREP = IOR('E', 0x03, sizeof_uint32p) /* get repeat settings */
  90. var EVIOCSREP = IOW('E', 0x03, sizeof_uint32p) /* set repeat settings */
  91. var EVIOCGKEYCODE = IOR('E', 0x04, sizeof_uint32) /* get keycode */
  92. var EVIOCGKEYCODE_V2= IOR('E', 0x04, sizeof_input_keymap_entry)
  93. var EVIOCSKEYCODE = IOW('E', 0x04, sizeof_uint32p) /* set keycode */
  94. var EVIOCSKEYCODE_V2= IOW('E', 0x04, sizeof_input_keymap_entry)
  95. func EVIOCGNAME(len uintptr) uint32{
  96. return IOC(IOC_READ, 'E', 0x06, len) /* get device name */
  97. }
  98. func EVIOCGPHYS(len uintptr) uint32 {
  99. return IOC(IOC_READ, 'E', 0x07, len) /* get physical location */
  100. }
  101. func EVIOCGUNIQ(len uintptr) uint32 {
  102. return IOC(IOC_READ, 'E', 0x08, len) /* get unique identifier */
  103. }
  104. func EVIOCGPROP(len uintptr) uint32 {
  105. return IOC(IOC_READ, 'E', 0x09, len) /* get device properties */
  106. }
  107. /**
  108. * EVIOCGMTSLOTS(len uintptr) uint32 { - get MT slot values
  109. * @len: size of the data buffer in bytes
  110. *
  111. * The ioctl buffer argument should be binary equivalent to
  112. *
  113. * type input_mt_request_layout struct {
  114. * __u32 code;
  115. * __s32 values[num_slots];
  116. * };
  117. *
  118. * where num_slots is the (arbitrary) number of MT slots to extract.
  119. *
  120. * The ioctl size argument (len uintptr) uint32 { is the size of the buffer, which
  121. * should satisfy len = (num_slots + 1) * sizeof(__s32). If len is
  122. * too small to fit all available slots, the first num_slots are
  123. * returned.
  124. *
  125. * Before the call, code is set to the wanted ABS_MT event type. On
  126. * return, values[] is filled with the slot values for the specified
  127. * ABS_MT code.
  128. *
  129. * If the request code is not an ABS_MT value, -EINVAL is returned.
  130. */
  131. func EVIOCGMTSLOTS(len uintptr) uint32 {
  132. return IOC(IOC_READ, 'E', 0x0a, len)
  133. }
  134. func EVIOCGKEY(len uintptr) uint32 {
  135. return IOC(IOC_READ, 'E', 0x18, len) /* get global key state */
  136. }
  137. func EVIOCGLED(len uintptr) uint32 {
  138. return IOC(IOC_READ, 'E', 0x19, len) /* get all LEDs */
  139. }
  140. func EVIOCGSND(len uintptr) uint32 {
  141. return IOC(IOC_READ, 'E', 0x1a, len) /* get all sounds status */
  142. }
  143. func EVIOCGSW(len uintptr) uint32 {
  144. return IOC(IOC_READ, 'E', 0x1b, len) /* get all switch states */
  145. }
  146. func EVIOCGBIT(ev uint32, len uintptr) uint32 {
  147. return IOC(IOC_READ, 'E', 0x20 + (ev), len) /* get event bits */
  148. }
  149. func EVIOCGABS(abs uint32) uint32 {
  150. return IOR('E', 0x40 + (abs), sizeof_input_absinfo) /* get abs value/limits */
  151. }
  152. func EVIOCSABS(abs uint32) uint32 {
  153. return IOW('E', 0xc0 + (abs), sizeof_input_absinfo) /* set abs value/limits */
  154. }
  155. var EVIOCSFF = IOW('E', 0x80, sizeof_ff_effect) /* send a force effect to a force feedback device */
  156. var EVIOCRMFF = IOW('E', 0x81, sizeof_int32) /* Erase a force effect */
  157. var EVIOCGEFFECTS = IOR('E', 0x84, sizeof_int32) /* Report number of effects playable at the same time */
  158. var EVIOCGRAB = IOW('E', 0x90, sizeof_int32) /* Grab/Release device */
  159. var EVIOCREVOKE = IOW('E', 0x91, sizeof_int32) /* Revoke device access */
  160. /**
  161. * EVIOCGMASK - Retrieve current event mask
  162. *
  163. * This ioctl allows user to retrieve the current event mask for specific
  164. * event type. The argument must be of type "struct input_mask" and
  165. * specifies the event type to query, the address of the receive buffer and
  166. * the size of the receive buffer.
  167. *
  168. * The event mask is a per-client mask that specifies which events are
  169. * forwarded to the client. Each event code is represented by a single bit
  170. * in the event mask. If the bit is set, the event is passed to the client
  171. * normally. Otherwise, the event is filtered and will never be queued on
  172. * the client's receive buffer.
  173. *
  174. * Event masks do not affect global state of the input device. They only
  175. * affect the file descriptor they are applied to.
  176. *
  177. * The default event mask for a client has all bits set, i.e. all events
  178. * are forwarded to the client. If the kernel is queried for an unknown
  179. * event type or if the receive buffer is larger than the number of
  180. * event codes known to the kernel, the kernel returns all zeroes for those
  181. * codes.
  182. *
  183. * At maximum, codes_size bytes are copied.
  184. *
  185. * This ioctl may fail with ENODEV in case the file is revoked, EFAULT
  186. * if the receive-buffer points to invalid memory, or EINVAL if the kernel
  187. * does not implement the ioctl.
  188. */
  189. var EVIOCGMASK = IOR('E', 0x92, sizeof_input_mask) /* Get event-masks */
  190. /**
  191. * EVIOCSMASK - Set event mask
  192. *
  193. * This ioctl is the counterpart to EVIOCGMASK. Instead of receiving the
  194. * current event mask, this changes the client's event mask for a specific
  195. * type. See EVIOCGMASK for a description of event-masks and the
  196. * argument-type.
  197. *
  198. * This ioctl provides full forward compatibility. If the passed event type
  199. * is unknown to the kernel, or if the number of event codes specified in
  200. * the mask is bigger than what is known to the kernel, the ioctl is still
  201. * accepted and applied. However, any unknown codes are left untouched and
  202. * stay cleared. That means, the kernel always filters unknown codes
  203. * regardless of what the client requests. If the new mask doesn't cover
  204. * all known event-codes, all remaining codes are automatically cleared and
  205. * thus filtered.
  206. *
  207. * This ioctl may fail with ENODEV in case the file is revoked. EFAULT is
  208. * returned if the receive-buffer points to invalid memory. EINVAL is returned
  209. * if the kernel does not implement the ioctl.
  210. */
  211. var EVIOCSMASK = IOW('E', 0x93, sizeof_input_mask) /* Set event-masks */
  212. var EVIOCSCLOCKID = IOW('E', 0xa0, sizeof_int32) /* Set clockid to be used for timestamps */
  213. /*
  214. * IDs.
  215. */
  216. const ID_BUS = 0
  217. const ID_VENDOR = 1
  218. const ID_PRODUCT = 2
  219. const ID_VERSION = 3
  220. const BUS_PCI = 0x01
  221. const BUSISAPNP = 0x02
  222. const BUS_USB = 0x03
  223. const BUS_HIL = 0x04
  224. const BUS_BLUETOOTH = 0x05
  225. const BUS_VIRTUAL = 0x06
  226. const BUSISA = 0x10
  227. const BUSI8042 = 0x11
  228. const BUS_XTKBD = 0x12
  229. const BUS_RS232 = 0x13
  230. const BUS_GAMEPORT = 0x14
  231. const BUS_PARPORT = 0x15
  232. const BUS_AMIGA = 0x16
  233. const BUS_ADB = 0x17
  234. const BUSI2C = 0x18
  235. const BUS_HOST = 0x19
  236. const BUS_GSC = 0x1A
  237. const BUS_ATARI = 0x1B
  238. const BUS_SPI = 0x1C
  239. /*
  240. * MT_TOOL types
  241. */
  242. const MT_TOOL_FINGER = 0
  243. const MT_TOOL_PEN = 1
  244. const MT_TOOL_PALM = 2
  245. const MT_TOOL_MAX = 2
  246. /*
  247. * Values describing the status of a force-feedback effect
  248. */
  249. const FF_STATUS_STOPPED = 0x00
  250. const FF_STATUS_PLAYING = 0x01
  251. const FF_STATUS_MAX = 0x01
  252. /*
  253. * Structures used in ioctls to upload effects to a device
  254. * They are pieces of a bigger structure (called ff_effect)
  255. */
  256. /*
  257. * All duration values are expressed in ms. Values above 32767 ms (0x7fff)
  258. * should not be used and have unspecified results.
  259. */
  260. /**
  261. * struct ff_replay - defines scheduling of the force-feedback effect
  262. * @length: duration of the effect
  263. * @delay: delay before effect should start playing
  264. */
  265. type ff_replay struct {
  266. length uint16
  267. delay uint16
  268. };
  269. /**
  270. * struct ff_trigger - defines what triggers the force-feedback effect
  271. * @button: number of the button triggering the effect
  272. * @interval: controls how soon the effect can be re-triggered
  273. */
  274. type ff_trigger struct {
  275. button uint16
  276. interval uint16
  277. };
  278. /**
  279. * struct ff_envelope - generic force-feedback effect envelope
  280. * @attack_length: duration of the attack (ms)
  281. * @attack_level: level at the beginning of the attack
  282. * @fade_length: duration of fade (ms)
  283. * @fade_level: level at the end of fade
  284. *
  285. * The @attack_level and @fade_level are absolute values; when applying
  286. * envelope force-feedback core will convert to positive/negative
  287. * value based on polarity of the default level of the effect.
  288. * Valid range for the attack and fade levels is 0x0000 - 0x7fff
  289. */
  290. type ff_envelope struct {
  291. attack_length uint16
  292. attack_level uint16
  293. fade_length uint16
  294. fade_level uint16
  295. };
  296. /**
  297. * struct ff_constant_effect - defines parameters of a constant force-feedback effect
  298. * @level: strength of the effect; may be negative
  299. * @envelope: envelope data
  300. */
  301. type ff_constant_effect struct {
  302. level int16
  303. envelope ff_envelope
  304. };
  305. /**
  306. * struct ff_ramp_effect - defines parameters of a ramp force-feedback effect
  307. * @start_level: beginning strength of the effect; may be negative
  308. * @end_level: final strength of the effect; may be negative
  309. * @envelope: envelope data
  310. */
  311. type ff_ramp_effect struct {
  312. start_level int16
  313. end_level int16
  314. envelope ff_envelope
  315. };
  316. /**
  317. * struct ff_condition_effect - defines a spring or friction force-feedback effect
  318. * @right_saturation: maximum level when joystick moved all way to the right
  319. * @left_saturation: same for the left side
  320. * @right_coeff: controls how fast the force grows when the joystick moves
  321. * to the right
  322. * @left_coeff: same for the left side
  323. * @deadband: size of the dead zone, where no force is produced
  324. * @center: position of the dead zone
  325. */
  326. type ff_condition_effect struct {
  327. right_saturation uint16
  328. left_saturation uint16
  329. right_coeff int16
  330. left_coeff int16
  331. deadband uint16
  332. center int16
  333. };
  334. /**
  335. * struct ff_periodic_effect - defines parameters of a periodic force-feedback effect
  336. * @waveform: kind of the effect (wave)
  337. * @period: period of the wave (ms)
  338. * @magnitude: peak value
  339. * @offset: mean value of the wave (roughly)
  340. * @phase: 'horizontal' shift
  341. * @envelope: envelope data
  342. * @custom_len: number of samples (FF_CUSTOM only)
  343. * @custom_data: buffer of samples (FF_CUSTOM only)
  344. *
  345. * Known waveforms - FF_SQUARE, FF_TRIANGLE, FF_SINE, FF_SAW_UP,
  346. * FF_SAW_DOWN, FF_CUSTOM. The exact syntax FF_CUSTOM is undefined
  347. * for the time being as no driver supports it yet.
  348. *
  349. * Note: the data pointed by custom_data is copied by the driver.
  350. * You can therefore dispose of the memory after the upload/update.
  351. */
  352. type ff_periodic_effect struct {
  353. waveform uint16
  354. period uint16
  355. magnitude int16
  356. offset int16
  357. phase uint16
  358. envelope ff_envelope
  359. custom_len uint32
  360. custom_data *int16;
  361. };
  362. /**
  363. * struct ff_rumble_effect - defines parameters of a periodic force-feedback effect
  364. * @strong_magnitude: magnitude of the heavy motor
  365. * @weak_magnitude: magnitude of the light one
  366. *
  367. * Some rumble pads have two motors of different weight. Strong_magnitude
  368. * represents the magnitude of the vibration generated by the heavy one.
  369. */
  370. type ff_rumble_effect struct {
  371. strong_magnitude uint16
  372. weak_magnitude uint16
  373. };
  374. const ff_effect_union_size = unsafe.Sizeof(*((*ff_periodic_effect)(nil)))
  375. /**
  376. * struct ff_effect - defines force feedback effect
  377. * @type: type of the effect (FF_CONSTANT, FF_PERIODIC, FF_RAMP, FF_SPRING,
  378. * FF_FRICTION, FF_DAMPER, FF_RUMBLE, FFINERTIA, or FF_CUSTOM)
  379. * @id: an unique id assigned to an effect
  380. * @direction: direction of the effect
  381. * @trigger: trigger conditions (struct ff_trigger)
  382. * @replay: scheduling of the effect (struct ff_replay)
  383. * @u: effect-specific structure (one of ff_constant_effect, ff_ramp_effect,
  384. * ff_periodic_effect, ff_condition_effect, ff_rumble_effect) further
  385. * defining effect parameters
  386. *
  387. * This structure is sent through ioctl from the application to the driver.
  388. * To create a new effect application should set its @id to -1; the kernel
  389. * will return assigned @id which can later be used to update or delete
  390. * this effect.
  391. *
  392. * Direction of the effect is encoded as follows:
  393. * 0 deg -> 0x0000 (down)
  394. * 90 deg -> 0x4000 (left)
  395. * 180 deg -> 0x8000 (up)
  396. * 270 deg -> 0xC000 (right)
  397. */
  398. type ff_effect struct {
  399. type_ uint16
  400. id int16
  401. direction uint16
  402. trigger ff_trigger
  403. replay ff_replay
  404. // This was a union in C, somulate with a byte buffer with size of largest element
  405. u [ff_effect_union_size]byte
  406. };
  407. type ff_effect_u_constant struct {
  408. type_ uint16
  409. id int16
  410. direction uint16
  411. trigger ff_trigger
  412. replay ff_replay
  413. constant ff_constant_effect
  414. };
  415. type ff_effect_u_ramp struct {
  416. type_ uint16
  417. id int16
  418. direction uint16
  419. trigger ff_trigger
  420. replay ff_replay
  421. ramp ff_ramp_effect
  422. };
  423. type ff_effect_u_periodic struct {
  424. type_ uint16
  425. id int16
  426. direction uint16
  427. trigger ff_trigger
  428. replay ff_replay
  429. periodic ff_periodic_effect
  430. };
  431. type ff_effect_u_condition struct {
  432. type_ uint16
  433. id int16
  434. direction uint16
  435. trigger ff_trigger
  436. replay ff_replay
  437. condition [2]ff_condition_effect; /* One for each axis */
  438. };
  439. type ff_effect_u_rumble struct {
  440. type_ uint16
  441. id int16
  442. direction uint16
  443. trigger ff_trigger
  444. replay ff_replay
  445. rumble ff_rumble_effect
  446. };
  447. /*
  448. * Force feedback effect types
  449. */
  450. const FF_RUMBLE = 0x50
  451. const FF_PERIODIC = 0x51
  452. const FF_CONSTANT = 0x52
  453. const FF_SPRING = 0x53
  454. const FF_FRICTION = 0x54
  455. const FF_DAMPER = 0x55
  456. const FFINERTIA = 0x56
  457. const FF_RAMP = 0x57
  458. const FF_EFFECT_MIN = FF_RUMBLE
  459. const FF_EFFECT_MAX = FF_RAMP
  460. /*
  461. * Force feedback periodic effect types
  462. */
  463. const FF_SQUARE = 0x58
  464. const FF_TRIANGLE = 0x59
  465. const FF_SINE = 0x5a
  466. const FF_SAW_UP = 0x5b
  467. const FF_SAW_DOWN = 0x5c
  468. const FF_CUSTOM = 0x5d
  469. const FF_WAVEFORM_MIN = FF_SQUARE
  470. const FF_WAVEFORM_MAX = FF_CUSTOM
  471. /*
  472. * Set ff device properties
  473. */
  474. const FF_GAIN = 0x60
  475. const FF_AUTOCENTER = 0x61
  476. /*
  477. * ff->playback(effect_id = FF_GAIN) is the first effect_id to
  478. * cause a collision with another ff method, in this case ff->set_gain().
  479. * Therefore the greatest safe value for effect_id is FF_GAIN - 1,
  480. * and thus the total number of effects should never exceed FF_GAIN.
  481. */
  482. const FF_MAX_EFFECTS = FF_GAIN
  483. const FF_MAX = 0x7f
  484. const FF_CNT = (FF_MAX+1)