Input: docs - update joystick documentation a bit
[linux-2.6-block.git] / Documentation / input / multi-touch-protocol.rst
CommitLineData
eba31a3a
MCC
1.. include:: <isonum.txt>
2
3=========================
eacaad01 4Multi-touch (MT) Protocol
eba31a3a
MCC
5=========================
6
7:Copyright: |copy| 2009-2010 Henrik Rydberg <rydberg@euromail.se>
eacaad01
HR
8
9
10Introduction
11------------
12
72c8a94a
HR
13In order to utilize the full power of the new multi-touch and multi-user
14devices, a way to report detailed data from multiple contacts, i.e.,
15objects in direct contact with the device surface, is needed. This
16document describes the multi-touch (MT) protocol which allows kernel
17drivers to report details for an arbitrary number of contacts.
18
19The protocol is divided into two types, depending on the capabilities of the
20hardware. For devices handling anonymous contacts (type A), the protocol
21describes how to send the raw data for all contacts to the receiver. For
22devices capable of tracking identifiable contacts (type B), the protocol
23describes how to send updates for individual contacts via event slots.
24
25
26Protocol Usage
27--------------
28
29Contact details are sent sequentially as separate packets of ABS_MT
30events. Only the ABS_MT events are recognized as part of a contact
31packet. Since these events are ignored by current single-touch (ST)
32applications, the MT protocol can be implemented on top of the ST protocol
33in an existing driver.
34
35Drivers for type A devices separate contact packets by calling
36input_mt_sync() at the end of each packet. This generates a SYN_MT_REPORT
37event, which instructs the receiver to accept the data for the current
38contact and prepare to receive another.
39
40Drivers for type B devices separate contact packets by calling
41input_mt_slot(), with a slot as argument, at the beginning of each packet.
42This generates an ABS_MT_SLOT event, which instructs the receiver to
43prepare for updates of the given slot.
44
45All drivers mark the end of a multi-touch transfer by calling the usual
46input_sync() function. This instructs the receiver to act upon events
47accumulated since last EV_SYN/SYN_REPORT and prepare to receive a new set
48of events/packets.
49
50The main difference between the stateless type A protocol and the stateful
51type B slot protocol lies in the usage of identifiable contacts to reduce
52the amount of data sent to userspace. The slot protocol requires the use of
53the ABS_MT_TRACKING_ID, either provided by the hardware or computed from
eba31a3a 54the raw data [#f5]_.
72c8a94a
HR
55
56For type A devices, the kernel driver should generate an arbitrary
57enumeration of the full set of anonymous contacts currently on the
58surface. The order in which the packets appear in the event stream is not
eba31a3a 59important. Event filtering and finger tracking is left to user space [#f3]_.
72c8a94a
HR
60
61For type B devices, the kernel driver should associate a slot with each
62identified contact, and use that slot to propagate changes for the contact.
63Creation, replacement and destruction of contacts is achieved by modifying
64the ABS_MT_TRACKING_ID of the associated slot. A non-negative tracking id
65is interpreted as a contact, and the value -1 denotes an unused slot. A
66tracking id not previously present is considered new, and a tracking id no
67longer present is considered removed. Since only changes are propagated,
68the full state of each initiated contact has to reside in the receiving
69end. Upon receiving an MT event, one simply updates the appropriate
70attribute of the current slot.
71
a93bd154
DK
72Some devices identify and/or track more contacts than they can report to the
73driver. A driver for such a device should associate one type B slot with each
74contact that is reported by the hardware. Whenever the identity of the
75contact associated with a slot changes, the driver should invalidate that
76slot by changing its ABS_MT_TRACKING_ID. If the hardware signals that it is
77tracking more contacts than it is currently reporting, the driver should use
78a BTN_TOOL_*TAP event to inform userspace of the total number of contacts
79being tracked by the hardware at that moment. The driver should do this by
80explicitly sending the corresponding BTN_TOOL_*TAP event and setting
81use_count to false when calling input_mt_report_pointer_emulation().
82The driver should only advertise as many slots as the hardware can report.
83Userspace can detect that a driver can report more total contacts than slots
84by noting that the largest supported BTN_TOOL_*TAP event is larger than the
85total number of type B slots reported in the absinfo for the ABS_MT_SLOT axis.
72c8a94a 86
257867dc
PH
87The minimum value of the ABS_MT_SLOT axis must be 0.
88
72c8a94a
HR
89Protocol Example A
90------------------
91
92Here is what a minimal event sequence for a two-contact touch would look
eba31a3a 93like for a type A device::
72c8a94a
HR
94
95 ABS_MT_POSITION_X x[0]
96 ABS_MT_POSITION_Y y[0]
97 SYN_MT_REPORT
98 ABS_MT_POSITION_X x[1]
99 ABS_MT_POSITION_Y y[1]
100 SYN_MT_REPORT
101 SYN_REPORT
eacaad01 102
72c8a94a
HR
103The sequence after moving one of the contacts looks exactly the same; the
104raw data for all present contacts are sent between every synchronization
105with SYN_REPORT.
eacaad01 106
eba31a3a 107Here is the sequence after lifting the first contact::
eacaad01 108
72c8a94a
HR
109 ABS_MT_POSITION_X x[1]
110 ABS_MT_POSITION_Y y[1]
111 SYN_MT_REPORT
112 SYN_REPORT
113
eba31a3a 114And here is the sequence after lifting the second contact::
72c8a94a
HR
115
116 SYN_MT_REPORT
117 SYN_REPORT
118
119If the driver reports one of BTN_TOUCH or ABS_PRESSURE in addition to the
120ABS_MT events, the last SYN_MT_REPORT event may be omitted. Otherwise, the
121last SYN_REPORT will be dropped by the input core, resulting in no
122zero-contact event reaching userland.
123
124
125Protocol Example B
126------------------
127
128Here is what a minimal event sequence for a two-contact touch would look
eba31a3a 129like for a type B device::
72c8a94a
HR
130
131 ABS_MT_SLOT 0
132 ABS_MT_TRACKING_ID 45
133 ABS_MT_POSITION_X x[0]
134 ABS_MT_POSITION_Y y[0]
135 ABS_MT_SLOT 1
136 ABS_MT_TRACKING_ID 46
137 ABS_MT_POSITION_X x[1]
138 ABS_MT_POSITION_Y y[1]
139 SYN_REPORT
140
eba31a3a 141Here is the sequence after moving contact 45 in the x direction::
72c8a94a
HR
142
143 ABS_MT_SLOT 0
144 ABS_MT_POSITION_X x[0]
145 SYN_REPORT
146
eba31a3a 147Here is the sequence after lifting the contact in slot 0::
72c8a94a
HR
148
149 ABS_MT_TRACKING_ID -1
150 SYN_REPORT
151
152The slot being modified is already 0, so the ABS_MT_SLOT is omitted. The
153message removes the association of slot 0 with contact 45, thereby
154destroying contact 45 and freeing slot 0 to be reused for another contact.
155
eba31a3a 156Finally, here is the sequence after lifting the second contact::
72c8a94a
HR
157
158 ABS_MT_SLOT 1
159 ABS_MT_TRACKING_ID -1
160 SYN_REPORT
161
162
163Event Usage
164-----------
eacaad01
HR
165
166A set of ABS_MT events with the desired properties is defined. The events
167are divided into categories, to allow for partial implementation. The
f6bdc230 168minimum set consists of ABS_MT_POSITION_X and ABS_MT_POSITION_Y, which
72c8a94a 169allows for multiple contacts to be tracked. If the device supports it, the
f6bdc230 170ABS_MT_TOUCH_MAJOR and ABS_MT_WIDTH_MAJOR may be used to provide the size
cab7faca 171of the contact area and approaching tool, respectively.
f6bdc230
HR
172
173The TOUCH and WIDTH parameters have a geometrical interpretation; imagine
174looking through a window at someone gently holding a finger against the
175glass. You will see two regions, one inner region consisting of the part
176of the finger actually touching the glass, and one outer region formed by
cab7faca
HR
177the perimeter of the finger. The center of the touching region (a) is
178ABS_MT_POSITION_X/Y and the center of the approaching finger (b) is
179ABS_MT_TOOL_X/Y. The touch diameter is ABS_MT_TOUCH_MAJOR and the finger
180diameter is ABS_MT_WIDTH_MAJOR. Now imagine the person pressing the finger
181harder against the glass. The touch region will increase, and in general,
182the ratio ABS_MT_TOUCH_MAJOR / ABS_MT_WIDTH_MAJOR, which is always smaller
183than unity, is related to the contact pressure. For pressure-based devices,
f6bdc230 184ABS_MT_PRESSURE may be used to provide the pressure on the contact area
e42a98b5
HR
185instead. Devices capable of contact hovering can use ABS_MT_DISTANCE to
186indicate the distance between the contact and the surface.
f6bdc230 187
eba31a3a
MCC
188::
189
cab7faca
HR
190
191 Linux MT Win8
192 __________ _______________________
193 / \ | |
194 / \ | |
195 / ____ \ | |
196 / / \ \ | |
197 \ \ a \ \ | a |
198 \ \____/ \ | |
199 \ \ | |
200 \ b \ | b |
201 \ \ | |
202 \ \ | |
203 \ \ | |
204 \ / | |
205 \ / | |
206 \ / | |
207 \__________/ |_______________________|
208
209
210In addition to the MAJOR parameters, the oval shape of the touch and finger
211regions can be described by adding the MINOR parameters, such that MAJOR
212and MINOR are the major and minor axis of an ellipse. The orientation of
213the touch ellipse can be described with the ORIENTATION parameter, and the
214direction of the finger ellipse is given by the vector (a - b).
f6bdc230 215
22f075a8
HR
216For type A devices, further specification of the touch shape is possible
217via ABS_MT_BLOB_ID.
218
f6bdc230 219The ABS_MT_TOOL_TYPE may be used to specify whether the touching tool is a
22f075a8 220finger or a pen or something else. Finally, the ABS_MT_TRACKING_ID event
eba31a3a 221may be used to track identified contacts over time [#f5]_.
22f075a8
HR
222
223In the type B protocol, ABS_MT_TOOL_TYPE and ABS_MT_TRACKING_ID are
224implicitly handled by input core; drivers should instead call
225input_mt_report_slot_state().
f9fcfc3b 226
eacaad01
HR
227
228Event Semantics
229---------------
230
eacaad01 231ABS_MT_TOUCH_MAJOR
eba31a3a
MCC
232 The length of the major axis of the contact. The length should be given in
233 surface units. If the surface has an X times Y resolution, the largest
234 possible value of ABS_MT_TOUCH_MAJOR is sqrt(X^2 + Y^2), the diagonal [#f4]_.
eacaad01
HR
235
236ABS_MT_TOUCH_MINOR
eba31a3a
MCC
237 The length, in surface units, of the minor axis of the contact. If the
238 contact is circular, this event can be omitted [#f4]_.
eacaad01
HR
239
240ABS_MT_WIDTH_MAJOR
eba31a3a
MCC
241 The length, in surface units, of the major axis of the approaching
242 tool. This should be understood as the size of the tool itself. The
243 orientation of the contact and the approaching tool are assumed to be the
244 same [#f4]_.
eacaad01
HR
245
246ABS_MT_WIDTH_MINOR
eba31a3a
MCC
247 The length, in surface units, of the minor axis of the approaching
248 tool. Omit if circular [#f4]_.
eacaad01 249
eba31a3a
MCC
250 The above four values can be used to derive additional information about
251 the contact. The ratio ABS_MT_TOUCH_MAJOR / ABS_MT_WIDTH_MAJOR approximates
252 the notion of pressure. The fingers of the hand and the palm all have
253 different characteristic widths.
eacaad01 254
f6bdc230 255ABS_MT_PRESSURE
eba31a3a
MCC
256 The pressure, in arbitrary units, on the contact area. May be used instead
257 of TOUCH and WIDTH for pressure-based devices or any device with a spatial
258 signal intensity distribution.
f6bdc230 259
e42a98b5 260ABS_MT_DISTANCE
eba31a3a
MCC
261 The distance, in surface units, between the contact and the surface. Zero
262 distance means the contact is touching the surface. A positive number means
263 the contact is hovering above the surface.
e42a98b5 264
eacaad01 265ABS_MT_ORIENTATION
eba31a3a
MCC
266 The orientation of the touching ellipse. The value should describe a signed
267 quarter of a revolution clockwise around the touch center. The signed value
268 range is arbitrary, but zero should be returned for an ellipse aligned with
269 the Y axis of the surface, a negative value when the ellipse is turned to
270 the left, and a positive value when the ellipse is turned to the
271 right. When completely aligned with the X axis, the range max should be
272 returned.
273
274 Touch ellipsis are symmetrical by default. For devices capable of true 360
275 degree orientation, the reported orientation must exceed the range max to
276 indicate more than a quarter of a revolution. For an upside-down finger,
277 range max * 2 should be returned.
278
279 Orientation can be omitted if the touch area is circular, or if the
280 information is not available in the kernel driver. Partial orientation
281 support is possible if the device can distinguish between the two axis, but
282 not (uniquely) any values in between. In such cases, the range of
283 ABS_MT_ORIENTATION should be [0, 1] [#f4]_.
eacaad01
HR
284
285ABS_MT_POSITION_X
eba31a3a 286 The surface X coordinate of the center of the touching ellipse.
eacaad01
HR
287
288ABS_MT_POSITION_Y
eba31a3a 289 The surface Y coordinate of the center of the touching ellipse.
eacaad01 290
cab7faca 291ABS_MT_TOOL_X
eba31a3a
MCC
292 The surface X coordinate of the center of the approaching tool. Omit if
293 the device cannot distinguish between the intended touch point and the
294 tool itself.
cab7faca
HR
295
296ABS_MT_TOOL_Y
eba31a3a
MCC
297 The surface Y coordinate of the center of the approaching tool. Omit if the
298 device cannot distinguish between the intended touch point and the tool
299 itself.
cab7faca 300
eba31a3a
MCC
301 The four position values can be used to separate the position of the touch
302 from the position of the tool. If both positions are present, the major
303 tool axis points towards the touch point [#f1]_. Otherwise, the tool axes are
304 aligned with the touch axes.
cab7faca 305
eacaad01 306ABS_MT_TOOL_TYPE
eba31a3a
MCC
307 The type of approaching tool. A lot of kernel drivers cannot distinguish
308 between different tool types, such as a finger or a pen. In such cases, the
309 event should be omitted. The protocol currently supports MT_TOOL_FINGER,
310 MT_TOOL_PEN, and MT_TOOL_PALM [#f2]_. For type B devices, this event is
311 handled by input core; drivers should instead use
312 input_mt_report_slot_state(). A contact's ABS_MT_TOOL_TYPE may change over
313 time while still touching the device, because the firmware may not be able
314 to determine which tool is being used when it first appears.
eacaad01
HR
315
316ABS_MT_BLOB_ID
eba31a3a
MCC
317 The BLOB_ID groups several packets together into one arbitrarily shaped
318 contact. The sequence of points forms a polygon which defines the shape of
319 the contact. This is a low-level anonymous grouping for type A devices, and
320 should not be confused with the high-level trackingID [#f5]_. Most type A
321 devices do not have blob capability, so drivers can safely omit this event.
f9fcfc3b
HR
322
323ABS_MT_TRACKING_ID
eba31a3a
MCC
324 The TRACKING_ID identifies an initiated contact throughout its life cycle
325 [#f5]_. The value range of the TRACKING_ID should be large enough to ensure
326 unique identification of a contact maintained over an extended period of
327 time. For type B devices, this event is handled by input core; drivers
328 should instead use input_mt_report_slot_state().
f9fcfc3b
HR
329
330
331Event Computation
332-----------------
333
334The flora of different hardware unavoidably leads to some devices fitting
335better to the MT protocol than others. To simplify and unify the mapping,
336this section gives recipes for how to compute certain events.
337
338For devices reporting contacts as rectangular shapes, signed orientation
339cannot be obtained. Assuming X and Y are the lengths of the sides of the
340touching rectangle, here is a simple formula that retains the most
eba31a3a 341information possible::
f9fcfc3b
HR
342
343 ABS_MT_TOUCH_MAJOR := max(X, Y)
344 ABS_MT_TOUCH_MINOR := min(X, Y)
345 ABS_MT_ORIENTATION := bool(X > Y)
346
347The range of ABS_MT_ORIENTATION should be set to [0, 1], to indicate that
348the device can distinguish between a finger along the Y axis (0) and a
349finger along the X axis (1).
eacaad01 350
eba31a3a 351For win8 devices with both T and C coordinates, the position mapping is::
cab7faca
HR
352
353 ABS_MT_POSITION_X := T_X
354 ABS_MT_POSITION_Y := T_Y
355 ABS_MT_TOOL_X := C_X
b1452723 356 ABS_MT_TOOL_Y := C_Y
cab7faca
HR
357
358Unfortunately, there is not enough information to specify both the touching
359ellipse and the tool ellipse, so one has to resort to approximations. One
eba31a3a 360simple scheme, which is compatible with earlier usage, is::
cab7faca
HR
361
362 ABS_MT_TOUCH_MAJOR := min(X, Y)
363 ABS_MT_TOUCH_MINOR := <not used>
364 ABS_MT_ORIENTATION := <not used>
365 ABS_MT_WIDTH_MAJOR := min(X, Y) + distance(T, C)
366 ABS_MT_WIDTH_MINOR := min(X, Y)
367
368Rationale: We have no information about the orientation of the touching
369ellipse, so approximate it with an inscribed circle instead. The tool
df5cbb27 370ellipse should align with the vector (T - C), so the diameter must
cab7faca
HR
371increase with distance(T, C). Finally, assume that the touch diameter is
372equal to the tool thickness, and we arrive at the formulas above.
eacaad01
HR
373
374Finger Tracking
375---------------
376
f9fcfc3b 377The process of finger tracking, i.e., to assign a unique trackingID to each
72c8a94a
HR
378initiated contact on the surface, is a Euclidian Bipartite Matching
379problem. At each event synchronization, the set of actual contacts is
380matched to the set of contacts from the previous synchronization. A full
eba31a3a 381implementation can be found in [#f3]_.
f9fcfc3b
HR
382
383
f6bdc230
HR
384Gestures
385--------
386
387In the specific application of creating gesture events, the TOUCH and WIDTH
388parameters can be used to, e.g., approximate finger pressure or distinguish
389between index finger and thumb. With the addition of the MINOR parameters,
390one can also distinguish between a sweeping finger and a pointing finger,
391and with ORIENTATION, one can detect twisting of fingers.
392
393
eacaad01
HR
394Notes
395-----
396
22f075a8
HR
397In order to stay compatible with existing applications, the data reported
398in a finger packet must not be recognized as single-touch events.
399
400For type A devices, all finger data bypasses input filtering, since
401subsequent events of the same type refer to different fingers.
eacaad01 402
22f075a8
HR
403For example usage of the type A protocol, see the bcm5974 driver. For
404example usage of the type B protocol, see the hid-egalax driver.
eacaad01 405
eba31a3a
MCC
406.. [#f1] Also, the difference (TOOL_X - POSITION_X) can be used to model tilt.
407.. [#f2] The list can of course be extended.
408.. [#f3] The mtdev project: http://bitmath.org/code/mtdev/.
409.. [#f4] See the section on event computation.
410.. [#f5] See the section on finger tracking.