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