blob: 4df9d4aa553c551950a69034c92a7a1bb39a7988 [file] [log] [blame]
Dennis Kempina9963ba2012-06-08 10:32:23 -07001/*
2 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
Dennis Kempin320aef12012-06-11 15:26:52 -07007#include <libevdev/libevdev_event.h>
Dennis Kempina9963ba2012-06-08 10:32:23 -07008
9#include <errno.h>
10#include <linux/input.h>
11#include <stdbool.h>
12#include <time.h>
13
Dennis Kempin320aef12012-06-11 15:26:52 -070014#include <libevdev/libevdev.h>
15#include <libevdev/libevdev_util.h>
Dennis Kempina9963ba2012-06-08 10:32:23 -070016
17#ifndef BTN_TOOL_QUINTTAP
18#define BTN_TOOL_QUINTTAP 0x148 /* Five fingers on trackpad */
19#endif
20
21/* Set clockid to be used for timestamps */
22#ifndef EVIOCSCLOCKID
23#define EVIOCSCLOCKID _IOW('E', 0xa0, int)
24#endif
25
26#ifndef EVIOCGMTSLOTS
27#define EVIOCGMTSLOTS(len) _IOC(_IOC_READ, 'E', 0x0a, len)
28#endif
29
30/* SYN_DROPPED added in kernel v2.6.38-rc4 */
31#ifndef SYN_DROPPED
32#define SYN_DROPPED 3
33#endif
34
Chung-yih Wang1359d8b2012-11-21 11:25:33 +080035/* make VCSID as version number */
36#ifndef VCSID
37#define VCSID "Unknown"
38#endif
Dennis Kempina9963ba2012-06-08 10:32:23 -070039
Che-Liang Chiouf616b122012-10-24 17:08:51 -070040static void Event_Clear_Ev_Rel_State(EvdevPtr);
Dennis Kempina9963ba2012-06-08 10:32:23 -070041
Che-Liang Chiouf616b122012-10-24 17:08:51 -070042static bool Event_Syn(EvdevPtr, struct input_event*);
Dennis Kempina9963ba2012-06-08 10:32:23 -070043static void Event_Syn_Report(EvdevPtr, struct input_event*);
44static void Event_Syn_MT_Report(EvdevPtr, struct input_event*);
45
46static void Event_Key(EvdevPtr, struct input_event*);
47
48static void Event_Abs(EvdevPtr, struct input_event*);
49static void Event_Abs_MT(EvdevPtr, struct input_event*);
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +080050static void Event_Abs_Update_Pressure(EvdevPtr, struct input_event*);
Dennis Kempina9963ba2012-06-08 10:32:23 -070051
Che-Liang Chiouf616b122012-10-24 17:08:51 -070052static void Event_Rel(EvdevPtr, struct input_event*);
53
Dennis Kempina9963ba2012-06-08 10:32:23 -070054static void Event_Get_Time(struct timeval*, bool);
55
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -070056static int Event_Is_Valid(struct input_event*);
57
Chung-yih Wang1359d8b2012-11-21 11:25:33 +080058const char*
59Evdev_Get_Version() {
60 return VCSID;
61}
62
Dennis Kempina9963ba2012-06-08 10:32:23 -070063/**
64 * Input Device Event Property accessors
65 */
66int
67Event_Get_Left(EvdevPtr device)
68{
Yufeng Shen86e52252012-07-05 11:44:36 -040069 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_X];
Dennis Kempina9963ba2012-06-08 10:32:23 -070070 return absinfo->minimum;
71}
72
73int
74Event_Get_Right(EvdevPtr device)
75{
Yufeng Shen86e52252012-07-05 11:44:36 -040076 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_X];
Dennis Kempina9963ba2012-06-08 10:32:23 -070077 return absinfo->maximum;
78}
79
80int
81Event_Get_Top(EvdevPtr device)
82{
Yufeng Shen86e52252012-07-05 11:44:36 -040083 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_Y];
Dennis Kempina9963ba2012-06-08 10:32:23 -070084 return absinfo->minimum;
85}
86
87int
88Event_Get_Bottom(EvdevPtr device)
89{
Yufeng Shen86e52252012-07-05 11:44:36 -040090 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_Y];
Dennis Kempina9963ba2012-06-08 10:32:23 -070091 return absinfo->maximum;
92}
93
94int
95Event_Get_Res_Y(EvdevPtr device)
96{
Yufeng Shen86e52252012-07-05 11:44:36 -040097 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_Y];
Dennis Kempina9963ba2012-06-08 10:32:23 -070098 return absinfo->resolution;
99}
100
101int
102Event_Get_Res_X(EvdevPtr device)
103{
Yufeng Shen86e52252012-07-05 11:44:36 -0400104 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_X];
Dennis Kempina9963ba2012-06-08 10:32:23 -0700105 return absinfo->resolution;
106}
107
108int
Che-Liang Chioua2563012012-10-11 11:41:06 -0700109Event_Get_Orientation_Minimum(EvdevPtr device)
110{
111 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_ORIENTATION];
112 return absinfo->minimum;
113}
114
115int
116Event_Get_Orientation_Maximum(EvdevPtr device)
117{
118 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_ORIENTATION];
119 return absinfo->maximum;
120}
121
122int
Dennis Kempina9963ba2012-06-08 10:32:23 -0700123Event_Get_Button_Pad(EvdevPtr device)
124{
125 return TestBit(INPUT_PROP_BUTTONPAD, device->info.prop_bitmask);
126}
127
128int
129Event_Get_Semi_MT(EvdevPtr device)
130{
131 return TestBit(INPUT_PROP_SEMI_MT, device->info.prop_bitmask);
132}
133
134int
135Event_Get_T5R2(EvdevPtr device)
136{
137 EventStatePtr evstate = device->evstate;
138 if (Event_Get_Semi_MT(device))
139 return 0;
140 return (Event_Get_Touch_Count_Max(device) > evstate->slot_count);
141}
142
143int
144Event_Get_Touch_Count_Max(EvdevPtr device)
145{
146
147 if (TestBit(BTN_TOOL_QUINTTAP, device->info.key_bitmask))
148 return 5;
149 if (TestBit(BTN_TOOL_QUADTAP, device->info.key_bitmask))
150 return 4;
151 if (TestBit(BTN_TOOL_TRIPLETAP, device->info.key_bitmask))
152 return 3;
153 if (TestBit(BTN_TOOL_DOUBLETAP, device->info.key_bitmask))
154 return 2;
155 return 1;
156}
157
158int
159Event_Get_Touch_Count(EvdevPtr device)
160{
161
Charlie Mooney6647f242016-07-13 13:53:28 -0700162 if (TestBit(BTN_TOUCH, device->key_state_bitmask)) {
163 if (TestBit(BTN_TOOL_QUINTTAP, device->key_state_bitmask))
164 return 5;
165 if (TestBit(BTN_TOOL_QUADTAP, device->key_state_bitmask))
166 return 4;
167 if (TestBit(BTN_TOOL_TRIPLETAP, device->key_state_bitmask))
168 return 3;
169 if (TestBit(BTN_TOOL_DOUBLETAP, device->key_state_bitmask))
170 return 2;
171 if (TestBit(BTN_TOOL_FINGER, device->key_state_bitmask))
172 return 1;
173 }
Dennis Kempina9963ba2012-06-08 10:32:23 -0700174 return 0;
175}
176
177int
178Event_Get_Slot_Count(EvdevPtr device)
179{
180 EventStatePtr evstate = device->evstate;
181 return evstate->slot_count;
182}
183
184int
185Event_Get_Button_Left(EvdevPtr device)
186{
Dennis Kempine2a65d82014-02-24 13:27:40 -0800187 return Event_Get_Button(device, BTN_LEFT);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700188}
189
190int
191Event_Get_Button_Middle(EvdevPtr device)
192{
Dennis Kempine2a65d82014-02-24 13:27:40 -0800193 return Event_Get_Button(device, BTN_MIDDLE);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700194}
195
196int
197Event_Get_Button_Right(EvdevPtr device)
198{
Dennis Kempine2a65d82014-02-24 13:27:40 -0800199 return Event_Get_Button(device, BTN_RIGHT);
200}
201
202int
203Event_Get_Button(EvdevPtr device, int button)
204{
205 return TestBit(button, device->key_state_bitmask);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700206}
207
208#define CASE_RETURN(s) \
209 case (s):\
210 return #s
211
212
213const char *
214Event_To_String(int type, int code) {
215 switch (type) {
216 case EV_SYN:
217 switch (code) {
218 CASE_RETURN(SYN_REPORT);
219 CASE_RETURN(SYN_MT_REPORT);
220 default:
221 break;
222 }
223 break;
224 case EV_ABS:
225 switch (code) {
226 CASE_RETURN(ABS_X);
227 CASE_RETURN(ABS_Y);
228 CASE_RETURN(ABS_Z);
229 CASE_RETURN(ABS_PRESSURE);
230 CASE_RETURN(ABS_TOOL_WIDTH);
231 CASE_RETURN(ABS_MT_TOUCH_MAJOR);
232 CASE_RETURN(ABS_MT_TOUCH_MINOR);
233 CASE_RETURN(ABS_MT_WIDTH_MAJOR);
234 CASE_RETURN(ABS_MT_WIDTH_MINOR);
235 CASE_RETURN(ABS_MT_ORIENTATION);
236 CASE_RETURN(ABS_MT_POSITION_X);
237 CASE_RETURN(ABS_MT_POSITION_Y);
238 CASE_RETURN(ABS_MT_TOOL_TYPE);
239 CASE_RETURN(ABS_MT_BLOB_ID);
240 CASE_RETURN(ABS_MT_TRACKING_ID);
241 CASE_RETURN(ABS_MT_PRESSURE);
242 CASE_RETURN(ABS_MT_SLOT);
243 default:
244 break;
245 }
246 break;
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700247 case EV_REL:
248 switch (code) {
249 CASE_RETURN(REL_X);
250 CASE_RETURN(REL_Y);
251 CASE_RETURN(REL_WHEEL);
252 CASE_RETURN(REL_HWHEEL);
253 default:
254 break;
255 }
256 break;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700257 case EV_KEY:
258 switch (code) {
259 CASE_RETURN(BTN_LEFT);
260 CASE_RETURN(BTN_RIGHT);
261 CASE_RETURN(BTN_MIDDLE);
Dennis Kempine2a65d82014-02-24 13:27:40 -0800262 CASE_RETURN(BTN_BACK);
263 CASE_RETURN(BTN_FORWARD);
264 CASE_RETURN(BTN_EXTRA);
265 CASE_RETURN(BTN_SIDE);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700266 CASE_RETURN(BTN_TOUCH);
267 CASE_RETURN(BTN_TOOL_FINGER);
268 CASE_RETURN(BTN_TOOL_DOUBLETAP);
269 CASE_RETURN(BTN_TOOL_TRIPLETAP);
270 CASE_RETURN(BTN_TOOL_QUADTAP);
271 CASE_RETURN(BTN_TOOL_QUINTTAP);
272 default:
273 break;
274 }
275 break;
276 default:
277 break;
278 }
279 return "?";
280}
281#undef CASE_RETURN
282
283const char *
284Event_Type_To_String(int type) {
285 switch (type) {
286 case EV_SYN: return "SYN";
287 case EV_KEY: return "KEY";
288 case EV_REL: return "REL";
289 case EV_ABS: return "ABS";
290 case EV_MSC: return "MSC";
291 case EV_SW: return "SW";
292 case EV_LED: return "LED";
293 case EV_SND: return "SND";
294 case EV_REP: return "REP";
295 case EV_FF: return "FF";
296 case EV_PWR: return "PWR";
297 default: return "?";
298 }
299}
300
301
302/**
303 * Probe Device Input Event Support
304 */
305int
306Event_Init(EvdevPtr device)
307{
308 int i;
309 EventStatePtr evstate;
310
311 evstate = device->evstate;
312 if (EvdevProbe(device) != Success) {
313 return !Success;
314 }
315
Dennis Kempina9963ba2012-06-08 10:32:23 -0700316 for (i = ABS_X; i <= ABS_MAX; i++) {
317 if (TestBit(i, device->info.abs_bitmask)) {
318 struct input_absinfo* absinfo = &device->info.absinfo[i];
319 if (i == ABS_MT_SLOT) {
320 int rc;
321 rc = MTB_Init(device, absinfo->minimum, absinfo->maximum,
322 absinfo->value);
323 if (rc != Success)
324 return rc;
325 } else if (IS_ABS_MT(i)) {
326 evstate->mt_axes[MT_CODE(i)] = absinfo;
327 }
328 }
329 }
Dennis Kempina9963ba2012-06-08 10:32:23 -0700330 return Success;
331}
332
333void
334Event_Free(EvdevPtr device)
335{
336 MT_Free(device);
337}
338
339void
340Event_Open(EvdevPtr device)
341{
342 /* Select monotonic input event timestamps, if supported by kernel */
343 device->info.is_monotonic = (EvdevEnableMonotonic(device) == Success);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700344 LOG_DEBUG(device, "Using %s input event time stamps\n",
345 device->info.is_monotonic ? "monotonic" : "realtime");
Daniel Kurtz000bdff2012-09-17 15:45:29 +0800346
347 /* Synchronize all MT slots with kernel evdev driver */
348 Event_Sync_State(device);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700349}
350
351static void
352Event_Get_Time(struct timeval *t, bool use_monotonic) {
353 struct timespec now;
354 clockid_t clockid = (use_monotonic) ? CLOCK_MONOTONIC : CLOCK_REALTIME;
355
356 clock_gettime(clockid, &now);
357 t->tv_sec = now.tv_sec;
358 t->tv_usec = now.tv_nsec / 1000;
359}
360
361/**
362 * Synchronize the current state with kernel evdev driver. For cmt, there are
363 * only four components required to be synced: current touch count, the MT
364 * slots information, current slot id and physical button states. However, as
365 * pressure readings are missing in ABS_MT_PRESSURE field of MT slots for
366 * semi_mt touchpad device (e.g. Cr48), we also need need to extract it with
367 * extra EVIOCGABS query.
368 */
369void
370Event_Sync_State(EvdevPtr device)
371{
372 int i;
373
374 Event_Get_Time(&device->before_sync_time, device->info.is_monotonic);
375
376 EvdevProbeKeyState(device);
377
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800378 /* Get current pressure information for single-pressure device */
379 if (EvdevIsSinglePressureDevice(device) == Success) {
380 struct input_event ev;
381 ev.code = ABS_PRESSURE;
382 ev.value = device->info.absinfo[ABS_PRESSURE].value;
383 Event_Abs_Update_Pressure(device, &ev);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700384 }
385
386 /* TODO(cywang): Sync all ABS_ states for completeness */
387
388 /* Get current MT information for each slot */
389 for (i = _ABS_MT_FIRST; i <= _ABS_MT_LAST; i++) {
390 MTSlotInfo req;
391
392 if (!TestBit(i, device->info.abs_bitmask))
393 continue;
394 /*
395 * TODO(cywang): Scale the size of slots in MTSlotInfo based on the
396 * evstate->slot_count.
397 */
398
399 req.code = i;
400 if (EvdevProbeMTSlot(device, &req) != Success) {
401 continue;
402 }
403 MT_Slot_Sync(device, &req);
404 }
405
Chung-yih Wang8cb858a2014-04-21 16:38:23 +0800406 /* Get current slot id for multi-touch devices*/
407 if (TestBit(ABS_MT_SLOT, device->info.abs_bitmask) &&
408 (EvdevProbeAbsinfo(device, ABS_MT_SLOT) == Success)) {
Dennis Kempina9963ba2012-06-08 10:32:23 -0700409 MT_Slot_Set(device, device->info.absinfo[ABS_MT_SLOT].value);
Chung-yih Wang8cb858a2014-04-21 16:38:23 +0800410 }
Dennis Kempina9963ba2012-06-08 10:32:23 -0700411
412 Event_Get_Time(&device->after_sync_time, device->info.is_monotonic);
413
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700414 /* Initialize EV_REL event state */
415 Event_Clear_Ev_Rel_State(device);
416
Daniel Erat9f7a1962016-09-16 10:14:56 -0600417 LOG_DEBUG(device, "Event_Sync_State: before %ld.%ld after %ld.%ld\n",
418 (long)device->before_sync_time.tv_sec,
419 (long)device->before_sync_time.tv_usec,
420 (long)device->after_sync_time.tv_sec,
421 (long)device->after_sync_time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700422}
423
424static void
425Event_Print(EvdevPtr device, struct input_event* ev)
426{
427 switch (ev->type) {
428 case EV_SYN:
429 switch (ev->code) {
430 case SYN_REPORT:
Dennis Kempinde0712f2012-06-11 15:13:50 -0700431 LOG_DEBUG(device, "@ %ld.%06ld ---------- SYN_REPORT -------\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500432 (long)ev->time.tv_sec, (long)ev->time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700433 return;
434 case SYN_MT_REPORT:
Dennis Kempinde0712f2012-06-11 15:13:50 -0700435 LOG_DEBUG(device, "@ %ld.%06ld ........ SYN_MT_REPORT ......\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500436 (long)ev->time.tv_sec, (long)ev->time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700437 return;
438 case SYN_DROPPED:
Dennis Kempin85a14442012-06-25 11:24:59 -0700439 LOG_WARNING(device, "@ %ld.%06ld ++++++++ SYN_DROPPED ++++++++\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500440 (long)ev->time.tv_sec, (long)ev->time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700441 return;
442 default:
Dennis Kempin85a14442012-06-25 11:24:59 -0700443 LOG_WARNING(device, "@ %ld.%06ld ?????? SYN_UNKNOWN (%d) ?????\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500444 (long)ev->time.tv_sec, (long)ev->time.tv_usec, ev->code);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700445 return;
446 }
447 break;
448 case EV_ABS:
449 if (ev->code == ABS_MT_SLOT) {
Dennis Kempinde0712f2012-06-11 15:13:50 -0700450 LOG_DEBUG(device, "@ %ld.%06ld .......... MT SLOT %d ........\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500451 (long)ev->time.tv_sec, (long)ev->time.tv_usec, ev->value);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700452 return;
453 }
454 break;
455 default:
456 break;
457 }
458
Dennis Kempinde0712f2012-06-11 15:13:50 -0700459 LOG_DEBUG(device, "@ %ld.%06ld %s[%d] (%s) = %d\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500460 (long)ev->time.tv_sec, (long)ev->time.tv_usec,
461 Event_Type_To_String(ev->type), ev->code,
462 Event_To_String(ev->type, ev->code), ev->value);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700463}
464
465/**
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700466 * Process Input Events. It returns TRUE if SYN_DROPPED detected.
Dennis Kempina9963ba2012-06-08 10:32:23 -0700467 */
468bool
469Event_Process(EvdevPtr device, struct input_event* ev)
470{
Dennis Kempina9963ba2012-06-08 10:32:23 -0700471 Event_Print(device, ev);
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700472 if (Event_Is_Valid(ev)) {
473 if (!(ev->type == EV_SYN && ev->code == SYN_REPORT))
474 device->got_valid_event = 1;
475 } else {
476 return false;
477 }
Dennis Kempina9963ba2012-06-08 10:32:23 -0700478
479 switch (ev->type) {
480 case EV_SYN:
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700481 return Event_Syn(device, ev);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700482
483 case EV_KEY:
484 Event_Key(device, ev);
485 break;
486
487 case EV_ABS:
488 Event_Abs(device, ev);
489 break;
490
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700491 case EV_REL:
492 Event_Rel(device, ev);
493 break;
494
Dennis Kempina9963ba2012-06-08 10:32:23 -0700495 default:
496 break;
497 }
498 return false;
499}
500
501/**
502 * Dump the log of input events to disk
503 */
504void
Dennis Kempin328acb82014-04-10 13:58:25 -0700505Event_Dump_Debug_Log(void* vinfo) {
506 Event_Dump_Debug_Log_To(vinfo, "/var/log/xorg/cmt_input_events.dat");
507}
508
509void
510Event_Dump_Debug_Log_To(void* vinfo, const char* filename)
Dennis Kempina9963ba2012-06-08 10:32:23 -0700511{
512 EvdevPtr device = (EvdevPtr) vinfo;
513 size_t i;
Dennis Kempin69c91ed2013-02-12 14:53:10 -0800514 int ret;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700515 EventStatePtr evstate = device->evstate;
516
Dennis Kempin328acb82014-04-10 13:58:25 -0700517 FILE* fp = fopen(filename, "wb");
Dennis Kempina9963ba2012-06-08 10:32:23 -0700518 if (!fp) {
519 LOG_ERROR(device, "fopen() failed for debug log");
520 return;
521 }
Dennis Kempin69c91ed2013-02-12 14:53:10 -0800522
523 ret = EvdevWriteInfoToFile(fp, &device->info);
524 if (ret <= 0) {
525 LOG_ERROR(device, "EvdevWriteInfoToFile failed. Log without info.");
526 }
527
Dennis Kempina9963ba2012-06-08 10:32:23 -0700528 for (i = 0; i < DEBUG_BUF_SIZE; i++) {
Dennis Kempina9963ba2012-06-08 10:32:23 -0700529 struct input_event *ev =
530 &evstate->debug_buf[(evstate->debug_buf_tail + i) % DEBUG_BUF_SIZE];
531 if (ev->time.tv_sec == 0 && ev->time.tv_usec == 0)
532 continue;
Dennis Kempin69c91ed2013-02-12 14:53:10 -0800533 ret = EvdevWriteEventToFile(fp, ev);
534 if (ret <= 0) {
535 LOG_ERROR(device, "EvdevWriteEventToFile failed. Log is short.");
Dennis Kempina9963ba2012-06-08 10:32:23 -0700536 break;
537 }
538 }
539 fclose(fp);
540}
541
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700542/**
Dennis Kempind6ae1672014-01-08 11:41:33 -0800543 * Clear Debug Buffer
544 */
545void
546Event_Clear_Debug_Log(void* vinfo)
547{
548 EvdevPtr device = (EvdevPtr) vinfo;
549 EventStatePtr evstate = device->evstate;
550
551 memset(evstate->debug_buf, 0, sizeof(evstate->debug_buf));
552 evstate->debug_buf_tail = 0;
553}
554
555/**
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700556 * Clear EV_REL event state. This function should be called after a EV_SYN
557 * event is processed because EV_REL event state is not accumulative.
558 */
Dennis Kempina9963ba2012-06-08 10:32:23 -0700559static void
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700560Event_Clear_Ev_Rel_State(EvdevPtr device)
561{
562 EventStatePtr evstate;
563
564 evstate = device->evstate;
565 evstate->rel_x = 0;
566 evstate->rel_y = 0;
567 evstate->rel_wheel = 0;
568 evstate->rel_hwheel = 0;
569}
570
571/**
572 * Process EV_SYN events. It returns TRUE if SYN_DROPPED detected.
573 */
574static bool
Dennis Kempina9963ba2012-06-08 10:32:23 -0700575Event_Syn(EvdevPtr device, struct input_event* ev)
576{
577 switch (ev->code) {
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700578 case SYN_DROPPED:
579 /*
580 * Technically we don't need to call Event_Clear_Ev_Rel_State() here
581 * because when SYN_DROPPED is detected, Event_Sync_State() will be
582 * called and Event_Sync_State() will call Event_Clear_Ev_Rel_State()
583 * to re-initialize EV_REL event state.
584 */
585 return true;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700586 case SYN_REPORT:
587 Event_Syn_Report(device, ev);
588 break;
589 case SYN_MT_REPORT:
590 Event_Syn_MT_Report(device, ev);
591 break;
592 }
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700593 return false;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700594}
595
596static void
597Event_Syn_Report(EvdevPtr device, struct input_event* ev)
598{
599 EventStatePtr evstate = device->evstate;
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700600 if (device->got_valid_event)
601 device->syn_report(device->syn_report_udata, evstate, &ev->time);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700602
603 MT_Print_Slots(device);
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700604
605 Event_Clear_Ev_Rel_State(device);
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700606 device->got_valid_event = 0;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700607}
608
609static void
610Event_Syn_MT_Report(EvdevPtr device, struct input_event* ev)
611{
612 /* TODO(djkurtz): Handle MT-A */
613}
614
615static void
616Event_Key(EvdevPtr device, struct input_event* ev)
617{
618 AssignBit(device->key_state_bitmask, ev->code, ev->value);
619}
620
621static void
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800622Event_Abs_Update_Pressure(EvdevPtr device, struct input_event* ev)
Dennis Kempina9963ba2012-06-08 10:32:23 -0700623{
624 /*
625 * Update all active slots with the same ABS_PRESSURE value if it is a
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800626 * single-pressure device.
Dennis Kempina9963ba2012-06-08 10:32:23 -0700627 */
628 EventStatePtr evstate = device->evstate;
629
630 for (int i = 0; i < evstate->slot_count; i++) {
631 MtSlotPtr slot = &evstate->slots[i];
632 slot->pressure = ev->value;
633 }
634}
635
636static void
637Event_Abs(EvdevPtr device, struct input_event* ev)
638{
639 if (ev->code == ABS_MT_SLOT)
640 MT_Slot_Set(device, ev->value);
641 else if (IS_ABS_MT(ev->code))
642 Event_Abs_MT(device, ev);
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800643 else if ((ev->code == ABS_PRESSURE) && EvdevIsSinglePressureDevice(device))
644 Event_Abs_Update_Pressure(device, ev);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700645}
646
647static void
648Event_Abs_MT(EvdevPtr device, struct input_event* ev)
649{
650 EventStatePtr evstate = device->evstate;
651 struct input_absinfo* axis = evstate->mt_axes[MT_CODE(ev->code)];
652 MtSlotPtr slot = evstate->slot_current;
653
654 if (axis == NULL) {
Dennis Kempin85a14442012-06-25 11:24:59 -0700655 LOG_WARNING(device, "ABS_MT[%02x] was not reported by this device\n",
Dennis Kempina9963ba2012-06-08 10:32:23 -0700656 ev->code);
657 return;
658 }
659
660 /* Warn about out of range data, but don't ignore */
661 if ((ev->code != ABS_MT_TRACKING_ID)
662 && ((ev->value < axis->minimum)
663 || (ev->value > axis->maximum))) {
664 LOG_WARNING(device, "ABS_MT[%02x] = %d : value out of range [%d .. %d]\n",
665 ev->code, ev->value, axis->minimum, axis->maximum);
Chung-yih Wangf573f892013-01-04 14:29:02 +0800666 /* Update the effective boundary as we already print out the warning */
667 if (ev->value < axis->minimum)
668 axis->minimum = ev->value;
669 else if (ev->value > axis->maximum)
670 axis->maximum = ev->value;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700671 }
672
673 if (slot == NULL) {
Dennis Kempin85a14442012-06-25 11:24:59 -0700674 LOG_WARNING(device, "MT slot not set. Ignoring ABS_MT event\n");
Dennis Kempina9963ba2012-06-08 10:32:23 -0700675 return;
676 }
677
678 MT_Slot_Value_Set(slot, ev->code, ev->value);
679}
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700680
681static void
682Event_Rel(EvdevPtr device, struct input_event* ev)
683{
684 EventStatePtr evstate = device->evstate;
685
686 switch (ev->code) {
687 case REL_X:
688 evstate->rel_x = ev->value;
689 break;
690 case REL_Y:
691 evstate->rel_y = ev->value;
692 break;
693 case REL_WHEEL:
694 evstate->rel_wheel = ev->value;
695 break;
696 case REL_HWHEEL:
697 evstate->rel_hwheel = ev->value;
698 break;
699 }
700}
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700701
702static int Event_Is_Valid(struct input_event* ev)
703{
704 /* Key repeats are invalid. They're handled by X anyway */
705 if (ev->type == EV_KEY &&
706 ev->value == 2)
707 return 0;
708 return 1;
709}