multi-touch-protocol.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. Multi-touch (MT) Protocol
  2. -------------------------
  3. Copyright (C) 2009-2010 Henrik Rydberg <rydberg@euromail.se>
  4. Introduction
  5. ------------
  6. In order to utilize the full power of the new multi-touch and multi-user
  7. devices, a way to report detailed data from multiple contacts, i.e.,
  8. objects in direct contact with the device surface, is needed. This
  9. document describes the multi-touch (MT) protocol which allows kernel
  10. drivers to report details for an arbitrary number of contacts.
  11. The protocol is divided into two types, depending on the capabilities of the
  12. hardware. For devices handling anonymous contacts (type A), the protocol
  13. describes how to send the raw data for all contacts to the receiver. For
  14. devices capable of tracking identifiable contacts (type B), the protocol
  15. describes how to send updates for individual contacts via event slots.
  16. Protocol Usage
  17. --------------
  18. Contact details are sent sequentially as separate packets of ABS_MT
  19. events. Only the ABS_MT events are recognized as part of a contact
  20. packet. Since these events are ignored by current single-touch (ST)
  21. applications, the MT protocol can be implemented on top of the ST protocol
  22. in an existing driver.
  23. Drivers for type A devices separate contact packets by calling
  24. input_mt_sync() at the end of each packet. This generates a SYN_MT_REPORT
  25. event, which instructs the receiver to accept the data for the current
  26. contact and prepare to receive another.
  27. Drivers for type B devices separate contact packets by calling
  28. input_mt_slot(), with a slot as argument, at the beginning of each packet.
  29. This generates an ABS_MT_SLOT event, which instructs the receiver to
  30. prepare for updates of the given slot.
  31. All drivers mark the end of a multi-touch transfer by calling the usual
  32. input_sync() function. This instructs the receiver to act upon events
  33. accumulated since last EV_SYN/SYN_REPORT and prepare to receive a new set
  34. of events/packets.
  35. The main difference between the stateless type A protocol and the stateful
  36. type B slot protocol lies in the usage of identifiable contacts to reduce
  37. the amount of data sent to userspace. The slot protocol requires the use of
  38. the ABS_MT_TRACKING_ID, either provided by the hardware or computed from
  39. the raw data [5].
  40. For type A devices, the kernel driver should generate an arbitrary
  41. enumeration of the full set of anonymous contacts currently on the
  42. surface. The order in which the packets appear in the event stream is not
  43. important. Event filtering and finger tracking is left to user space [3].
  44. For type B devices, the kernel driver should associate a slot with each
  45. identified contact, and use that slot to propagate changes for the contact.
  46. Creation, replacement and destruction of contacts is achieved by modifying
  47. the ABS_MT_TRACKING_ID of the associated slot. A non-negative tracking id
  48. is interpreted as a contact, and the value -1 denotes an unused slot. A
  49. tracking id not previously present is considered new, and a tracking id no
  50. longer present is considered removed. Since only changes are propagated,
  51. the full state of each initiated contact has to reside in the receiving
  52. end. Upon receiving an MT event, one simply updates the appropriate
  53. attribute of the current slot.
  54. Some devices identify and/or track more contacts than they can report to the
  55. driver. A driver for such a device should associate one type B slot with each
  56. contact that is reported by the hardware. Whenever the identity of the
  57. contact associated with a slot changes, the driver should invalidate that
  58. slot by changing its ABS_MT_TRACKING_ID. If the hardware signals that it is
  59. tracking more contacts than it is currently reporting, the driver should use
  60. a BTN_TOOL_*TAP event to inform userspace of the total number of contacts
  61. being tracked by the hardware at that moment. The driver should do this by
  62. explicitly sending the corresponding BTN_TOOL_*TAP event and setting
  63. use_count to false when calling input_mt_report_pointer_emulation().
  64. The driver should only advertise as many slots as the hardware can report.
  65. Userspace can detect that a driver can report more total contacts than slots
  66. by noting that the largest supported BTN_TOOL_*TAP event is larger than the
  67. total number of type B slots reported in the absinfo for the ABS_MT_SLOT axis.
  68. Velocity tracking and temporal precision can be improved if device provides
  69. exact timestamp for touches reported through SYN_TIME_SEC and SYN_TIME_NSEC.
  70. The timestamp should be reported ahead of everything else in the packet.
  71. Protocol Example A
  72. ------------------
  73. Here is what a minimal event sequence for a two-contact touch would look
  74. like for a type A device:
  75. ABS_MT_POSITION_X x[0]
  76. ABS_MT_POSITION_Y y[0]
  77. SYN_MT_REPORT
  78. ABS_MT_POSITION_X x[1]
  79. ABS_MT_POSITION_Y y[1]
  80. SYN_MT_REPORT
  81. SYN_REPORT
  82. The sequence after moving one of the contacts looks exactly the same; the
  83. raw data for all present contacts are sent between every synchronization
  84. with SYN_REPORT.
  85. Here is the sequence after lifting the first contact:
  86. ABS_MT_POSITION_X x[1]
  87. ABS_MT_POSITION_Y y[1]
  88. SYN_MT_REPORT
  89. SYN_REPORT
  90. And here is the sequence after lifting the second contact:
  91. SYN_MT_REPORT
  92. SYN_REPORT
  93. If the driver reports one of BTN_TOUCH or ABS_PRESSURE in addition to the
  94. ABS_MT events, the last SYN_MT_REPORT event may be omitted. Otherwise, the
  95. last SYN_REPORT will be dropped by the input core, resulting in no
  96. zero-contact event reaching userland.
  97. Protocol Example B
  98. ------------------
  99. Here is what a minimal event sequence for a two-contact touch would look
  100. like for a type B device:
  101. ABS_MT_SLOT 0
  102. ABS_MT_TRACKING_ID 45
  103. ABS_MT_POSITION_X x[0]
  104. ABS_MT_POSITION_Y y[0]
  105. ABS_MT_SLOT 1
  106. ABS_MT_TRACKING_ID 46
  107. ABS_MT_POSITION_X x[1]
  108. ABS_MT_POSITION_Y y[1]
  109. SYN_REPORT
  110. Here is the sequence after moving contact 45 in the x direction:
  111. ABS_MT_SLOT 0
  112. ABS_MT_POSITION_X x[0]
  113. SYN_REPORT
  114. Here is the sequence after lifting the contact in slot 0:
  115. ABS_MT_TRACKING_ID -1
  116. SYN_REPORT
  117. The slot being modified is already 0, so the ABS_MT_SLOT is omitted. The
  118. message removes the association of slot 0 with contact 45, thereby
  119. destroying contact 45 and freeing slot 0 to be reused for another contact.
  120. Finally, here is the sequence after lifting the second contact:
  121. ABS_MT_SLOT 1
  122. ABS_MT_TRACKING_ID -1
  123. SYN_REPORT
  124. Event Usage
  125. -----------
  126. A set of ABS_MT events with the desired properties is defined. The events
  127. are divided into categories, to allow for partial implementation. The
  128. minimum set consists of ABS_MT_POSITION_X and ABS_MT_POSITION_Y, which
  129. allows for multiple contacts to be tracked. If the device supports it, the
  130. ABS_MT_TOUCH_MAJOR and ABS_MT_WIDTH_MAJOR may be used to provide the size
  131. of the contact area and approaching contact, respectively.
  132. The TOUCH and WIDTH parameters have a geometrical interpretation; imagine
  133. looking through a window at someone gently holding a finger against the
  134. glass. You will see two regions, one inner region consisting of the part
  135. of the finger actually touching the glass, and one outer region formed by
  136. the perimeter of the finger. The diameter of the inner region is the
  137. ABS_MT_TOUCH_MAJOR, the diameter of the outer region is
  138. ABS_MT_WIDTH_MAJOR. Now imagine the person pressing the finger harder
  139. against the glass. The inner region will increase, and in general, the
  140. ratio ABS_MT_TOUCH_MAJOR / ABS_MT_WIDTH_MAJOR, which is always smaller than
  141. unity, is related to the contact pressure. For pressure-based devices,
  142. ABS_MT_PRESSURE may be used to provide the pressure on the contact area
  143. instead. Devices capable of contact hovering can use ABS_MT_DISTANCE to
  144. indicate the distance between the contact and the surface.
  145. In addition to the MAJOR parameters, the oval shape of the contact can be
  146. described by adding the MINOR parameters, such that MAJOR and MINOR are the
  147. major and minor axis of an ellipse. Finally, the orientation of the oval
  148. shape can be describe with the ORIENTATION parameter.
  149. For type A devices, further specification of the touch shape is possible
  150. via ABS_MT_BLOB_ID.
  151. The ABS_MT_TOOL_TYPE may be used to specify whether the touching tool is a
  152. finger or a pen or something else. Finally, the ABS_MT_TRACKING_ID event
  153. may be used to track identified contacts over time [5].
  154. In the type B protocol, ABS_MT_TOOL_TYPE and ABS_MT_TRACKING_ID are
  155. implicitly handled by input core; drivers should instead call
  156. input_mt_report_slot_state().
  157. Event Semantics
  158. ---------------
  159. ABS_MT_TOUCH_MAJOR
  160. The length of the major axis of the contact. The length should be given in
  161. surface units. If the surface has an X times Y resolution, the largest
  162. possible value of ABS_MT_TOUCH_MAJOR is sqrt(X^2 + Y^2), the diagonal [4].
  163. ABS_MT_TOUCH_MINOR
  164. The length, in surface units, of the minor axis of the contact. If the
  165. contact is circular, this event can be omitted [4].
  166. ABS_MT_WIDTH_MAJOR
  167. The length, in surface units, of the major axis of the approaching
  168. tool. This should be understood as the size of the tool itself. The
  169. orientation of the contact and the approaching tool are assumed to be the
  170. same [4].
  171. ABS_MT_WIDTH_MINOR
  172. The length, in surface units, of the minor axis of the approaching
  173. tool. Omit if circular [4].
  174. The above four values can be used to derive additional information about
  175. the contact. The ratio ABS_MT_TOUCH_MAJOR / ABS_MT_WIDTH_MAJOR approximates
  176. the notion of pressure. The fingers of the hand and the palm all have
  177. different characteristic widths [1].
  178. ABS_MT_PRESSURE
  179. The pressure, in arbitrary units, on the contact area. May be used instead
  180. of TOUCH and WIDTH for pressure-based devices or any device with a spatial
  181. signal intensity distribution.
  182. ABS_MT_DISTANCE
  183. The distance, in surface units, between the contact and the surface. Zero
  184. distance means the contact is touching the surface. A positive number means
  185. the contact is hovering above the surface.
  186. ABS_MT_ORIENTATION
  187. The orientation of the ellipse. The value should describe a signed quarter
  188. of a revolution clockwise around the touch center. The signed value range
  189. is arbitrary, but zero should be returned for a finger aligned along the Y
  190. axis of the surface, a negative value when finger is turned to the left, and
  191. a positive value when finger turned to the right. When completely aligned with
  192. the X axis, the range max should be returned. Orientation can be omitted
  193. if the touching object is circular, or if the information is not available
  194. in the kernel driver. Partial orientation support is possible if the device
  195. can distinguish between the two axis, but not (uniquely) any values in
  196. between. In such cases, the range of ABS_MT_ORIENTATION should be [0, 1]
  197. [4].
  198. ABS_MT_POSITION_X
  199. The surface X coordinate of the center of the touching ellipse.
  200. ABS_MT_POSITION_Y
  201. The surface Y coordinate of the center of the touching ellipse.
  202. ABS_MT_TOOL_TYPE
  203. The type of approaching tool. A lot of kernel drivers cannot distinguish
  204. between different tool types, such as a finger or a pen. In such cases, the
  205. event should be omitted. The protocol currently supports MT_TOOL_FINGER and
  206. MT_TOOL_PEN [2]. For type B devices, this event is handled by input core;
  207. drivers should instead use input_mt_report_slot_state().
  208. ABS_MT_BLOB_ID
  209. The BLOB_ID groups several packets together into one arbitrarily shaped
  210. contact. The sequence of points forms a polygon which defines the shape of
  211. the contact. This is a low-level anonymous grouping for type A devices, and
  212. should not be confused with the high-level trackingID [5]. Most type A
  213. devices do not have blob capability, so drivers can safely omit this event.
  214. ABS_MT_TRACKING_ID
  215. The TRACKING_ID identifies an initiated contact throughout its life cycle
  216. [5]. The value range of the TRACKING_ID should be large enough to ensure
  217. unique identification of a contact maintained over an extended period of
  218. time. For type B devices, this event is handled by input core; drivers
  219. should instead use input_mt_report_slot_state().
  220. Event Computation
  221. -----------------
  222. The flora of different hardware unavoidably leads to some devices fitting
  223. better to the MT protocol than others. To simplify and unify the mapping,
  224. this section gives recipes for how to compute certain events.
  225. For devices reporting contacts as rectangular shapes, signed orientation
  226. cannot be obtained. Assuming X and Y are the lengths of the sides of the
  227. touching rectangle, here is a simple formula that retains the most
  228. information possible:
  229. ABS_MT_TOUCH_MAJOR := max(X, Y)
  230. ABS_MT_TOUCH_MINOR := min(X, Y)
  231. ABS_MT_ORIENTATION := bool(X > Y)
  232. The range of ABS_MT_ORIENTATION should be set to [0, 1], to indicate that
  233. the device can distinguish between a finger along the Y axis (0) and a
  234. finger along the X axis (1).
  235. Finger Tracking
  236. ---------------
  237. The process of finger tracking, i.e., to assign a unique trackingID to each
  238. initiated contact on the surface, is a Euclidian Bipartite Matching
  239. problem. At each event synchronization, the set of actual contacts is
  240. matched to the set of contacts from the previous synchronization. A full
  241. implementation can be found in [3].
  242. Gestures
  243. --------
  244. In the specific application of creating gesture events, the TOUCH and WIDTH
  245. parameters can be used to, e.g., approximate finger pressure or distinguish
  246. between index finger and thumb. With the addition of the MINOR parameters,
  247. one can also distinguish between a sweeping finger and a pointing finger,
  248. and with ORIENTATION, one can detect twisting of fingers.
  249. Notes
  250. -----
  251. In order to stay compatible with existing applications, the data reported
  252. in a finger packet must not be recognized as single-touch events.
  253. For type A devices, all finger data bypasses input filtering, since
  254. subsequent events of the same type refer to different fingers.
  255. For example usage of the type A protocol, see the bcm5974 driver. For
  256. example usage of the type B protocol, see the hid-egalax driver.
  257. [1] With the extension ABS_MT_APPROACH_X and ABS_MT_APPROACH_Y, the
  258. difference between the contact position and the approaching tool position
  259. could be used to derive tilt.
  260. [2] The list can of course be extended.
  261. [3] The mtdev project: http://bitmath.org/code/mtdev/.
  262. [4] See the section on event computation.
  263. [5] See the section on finger tracking.