input_linux.go 16 KB

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