blob: 041c14c68f793d786a082667d64aeff91e6a9d30 [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
Harry Cutts61ae22b2018-09-20 17:08:43 -070021#ifndef REL_WHEEL_HI_RES
22#define REL_WHEEL_HI_RES 0x0b
23#endif
24
Dennis Kempina9963ba2012-06-08 10:32:23 -070025/* Set clockid to be used for timestamps */
26#ifndef EVIOCSCLOCKID
27#define EVIOCSCLOCKID _IOW('E', 0xa0, int)
28#endif
29
30#ifndef EVIOCGMTSLOTS
31#define EVIOCGMTSLOTS(len) _IOC(_IOC_READ, 'E', 0x0a, len)
32#endif
33
34/* SYN_DROPPED added in kernel v2.6.38-rc4 */
35#ifndef SYN_DROPPED
36#define SYN_DROPPED 3
37#endif
38
Chung-yih Wang1359d8b2012-11-21 11:25:33 +080039/* make VCSID as version number */
40#ifndef VCSID
41#define VCSID "Unknown"
42#endif
Dennis Kempina9963ba2012-06-08 10:32:23 -070043
Che-Liang Chiouf616b122012-10-24 17:08:51 -070044static void Event_Clear_Ev_Rel_State(EvdevPtr);
Dennis Kempina9963ba2012-06-08 10:32:23 -070045
Che-Liang Chiouf616b122012-10-24 17:08:51 -070046static bool Event_Syn(EvdevPtr, struct input_event*);
Dennis Kempina9963ba2012-06-08 10:32:23 -070047static void Event_Syn_Report(EvdevPtr, struct input_event*);
48static void Event_Syn_MT_Report(EvdevPtr, struct input_event*);
49
50static void Event_Key(EvdevPtr, struct input_event*);
51
52static void Event_Abs(EvdevPtr, struct input_event*);
53static void Event_Abs_MT(EvdevPtr, struct input_event*);
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +080054static void Event_Abs_Update_Pressure(EvdevPtr, struct input_event*);
Dennis Kempina9963ba2012-06-08 10:32:23 -070055
Che-Liang Chiouf616b122012-10-24 17:08:51 -070056static void Event_Rel(EvdevPtr, struct input_event*);
57
Sean O'Brien23f8ea72018-02-28 08:16:49 -080058static void Event_Msc(EvdevPtr, struct input_event*);
59
Dennis Kempina9963ba2012-06-08 10:32:23 -070060static void Event_Get_Time(struct timeval*, bool);
61
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -070062static int Event_Is_Valid(struct input_event*);
63
Chung-yih Wang1359d8b2012-11-21 11:25:33 +080064const char*
65Evdev_Get_Version() {
66 return VCSID;
67}
68
Dennis Kempina9963ba2012-06-08 10:32:23 -070069/**
70 * Input Device Event Property accessors
71 */
72int
73Event_Get_Left(EvdevPtr device)
74{
Yufeng Shen86e52252012-07-05 11:44:36 -040075 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_X];
Dennis Kempina9963ba2012-06-08 10:32:23 -070076 return absinfo->minimum;
77}
78
79int
80Event_Get_Right(EvdevPtr device)
81{
Yufeng Shen86e52252012-07-05 11:44:36 -040082 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_X];
Dennis Kempina9963ba2012-06-08 10:32:23 -070083 return absinfo->maximum;
84}
85
86int
87Event_Get_Top(EvdevPtr device)
88{
Yufeng Shen86e52252012-07-05 11:44:36 -040089 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_Y];
Dennis Kempina9963ba2012-06-08 10:32:23 -070090 return absinfo->minimum;
91}
92
93int
94Event_Get_Bottom(EvdevPtr device)
95{
Yufeng Shen86e52252012-07-05 11:44:36 -040096 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_Y];
Dennis Kempina9963ba2012-06-08 10:32:23 -070097 return absinfo->maximum;
98}
99
100int
101Event_Get_Res_Y(EvdevPtr device)
102{
Yufeng Shen86e52252012-07-05 11:44:36 -0400103 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_Y];
Dennis Kempina9963ba2012-06-08 10:32:23 -0700104 return absinfo->resolution;
105}
106
107int
108Event_Get_Res_X(EvdevPtr device)
109{
Yufeng Shen86e52252012-07-05 11:44:36 -0400110 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_X];
Dennis Kempina9963ba2012-06-08 10:32:23 -0700111 return absinfo->resolution;
112}
113
114int
Che-Liang Chioua2563012012-10-11 11:41:06 -0700115Event_Get_Orientation_Minimum(EvdevPtr device)
116{
117 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_ORIENTATION];
118 return absinfo->minimum;
119}
120
121int
122Event_Get_Orientation_Maximum(EvdevPtr device)
123{
124 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_ORIENTATION];
125 return absinfo->maximum;
126}
127
128int
Dennis Kempina9963ba2012-06-08 10:32:23 -0700129Event_Get_Button_Pad(EvdevPtr device)
130{
131 return TestBit(INPUT_PROP_BUTTONPAD, device->info.prop_bitmask);
132}
133
134int
135Event_Get_Semi_MT(EvdevPtr device)
136{
137 return TestBit(INPUT_PROP_SEMI_MT, device->info.prop_bitmask);
138}
139
140int
141Event_Get_T5R2(EvdevPtr device)
142{
143 EventStatePtr evstate = device->evstate;
144 if (Event_Get_Semi_MT(device))
145 return 0;
146 return (Event_Get_Touch_Count_Max(device) > evstate->slot_count);
147}
148
149int
150Event_Get_Touch_Count_Max(EvdevPtr device)
151{
152
153 if (TestBit(BTN_TOOL_QUINTTAP, device->info.key_bitmask))
154 return 5;
155 if (TestBit(BTN_TOOL_QUADTAP, device->info.key_bitmask))
156 return 4;
157 if (TestBit(BTN_TOOL_TRIPLETAP, device->info.key_bitmask))
158 return 3;
159 if (TestBit(BTN_TOOL_DOUBLETAP, device->info.key_bitmask))
160 return 2;
161 return 1;
162}
163
164int
165Event_Get_Touch_Count(EvdevPtr device)
166{
167
Charlie Mooney6647f242016-07-13 13:53:28 -0700168 if (TestBit(BTN_TOUCH, device->key_state_bitmask)) {
169 if (TestBit(BTN_TOOL_QUINTTAP, device->key_state_bitmask))
170 return 5;
171 if (TestBit(BTN_TOOL_QUADTAP, device->key_state_bitmask))
172 return 4;
173 if (TestBit(BTN_TOOL_TRIPLETAP, device->key_state_bitmask))
174 return 3;
175 if (TestBit(BTN_TOOL_DOUBLETAP, device->key_state_bitmask))
176 return 2;
177 if (TestBit(BTN_TOOL_FINGER, device->key_state_bitmask))
178 return 1;
179 }
Dennis Kempina9963ba2012-06-08 10:32:23 -0700180 return 0;
181}
182
183int
184Event_Get_Slot_Count(EvdevPtr device)
185{
186 EventStatePtr evstate = device->evstate;
187 return evstate->slot_count;
188}
189
190int
191Event_Get_Button_Left(EvdevPtr device)
192{
Dennis Kempine2a65d82014-02-24 13:27:40 -0800193 return Event_Get_Button(device, BTN_LEFT);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700194}
195
196int
197Event_Get_Button_Middle(EvdevPtr device)
198{
Dennis Kempine2a65d82014-02-24 13:27:40 -0800199 return Event_Get_Button(device, BTN_MIDDLE);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700200}
201
202int
203Event_Get_Button_Right(EvdevPtr device)
204{
Dennis Kempine2a65d82014-02-24 13:27:40 -0800205 return Event_Get_Button(device, BTN_RIGHT);
206}
207
208int
209Event_Get_Button(EvdevPtr device, int button)
210{
211 return TestBit(button, device->key_state_bitmask);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700212}
213
Sean O'Brien23f8ea72018-02-28 08:16:49 -0800214int
215Event_Get_Timestamp(EvdevPtr device)
216{
217 EventStatePtr evstate = device->evstate;
218 return evstate->msc_timestamp;
219}
220
Dennis Kempina9963ba2012-06-08 10:32:23 -0700221#define CASE_RETURN(s) \
222 case (s):\
223 return #s
224
225
226const char *
227Event_To_String(int type, int code) {
228 switch (type) {
229 case EV_SYN:
230 switch (code) {
231 CASE_RETURN(SYN_REPORT);
232 CASE_RETURN(SYN_MT_REPORT);
233 default:
234 break;
235 }
236 break;
237 case EV_ABS:
238 switch (code) {
239 CASE_RETURN(ABS_X);
240 CASE_RETURN(ABS_Y);
241 CASE_RETURN(ABS_Z);
242 CASE_RETURN(ABS_PRESSURE);
243 CASE_RETURN(ABS_TOOL_WIDTH);
244 CASE_RETURN(ABS_MT_TOUCH_MAJOR);
245 CASE_RETURN(ABS_MT_TOUCH_MINOR);
246 CASE_RETURN(ABS_MT_WIDTH_MAJOR);
247 CASE_RETURN(ABS_MT_WIDTH_MINOR);
248 CASE_RETURN(ABS_MT_ORIENTATION);
249 CASE_RETURN(ABS_MT_POSITION_X);
250 CASE_RETURN(ABS_MT_POSITION_Y);
251 CASE_RETURN(ABS_MT_TOOL_TYPE);
252 CASE_RETURN(ABS_MT_BLOB_ID);
253 CASE_RETURN(ABS_MT_TRACKING_ID);
254 CASE_RETURN(ABS_MT_PRESSURE);
255 CASE_RETURN(ABS_MT_SLOT);
256 default:
257 break;
258 }
259 break;
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700260 case EV_REL:
261 switch (code) {
262 CASE_RETURN(REL_X);
263 CASE_RETURN(REL_Y);
264 CASE_RETURN(REL_WHEEL);
Harry Cutts61ae22b2018-09-20 17:08:43 -0700265 CASE_RETURN(REL_WHEEL_HI_RES);
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700266 CASE_RETURN(REL_HWHEEL);
267 default:
268 break;
269 }
270 break;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700271 case EV_KEY:
272 switch (code) {
273 CASE_RETURN(BTN_LEFT);
274 CASE_RETURN(BTN_RIGHT);
275 CASE_RETURN(BTN_MIDDLE);
Dennis Kempine2a65d82014-02-24 13:27:40 -0800276 CASE_RETURN(BTN_BACK);
277 CASE_RETURN(BTN_FORWARD);
278 CASE_RETURN(BTN_EXTRA);
279 CASE_RETURN(BTN_SIDE);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700280 CASE_RETURN(BTN_TOUCH);
281 CASE_RETURN(BTN_TOOL_FINGER);
282 CASE_RETURN(BTN_TOOL_DOUBLETAP);
283 CASE_RETURN(BTN_TOOL_TRIPLETAP);
284 CASE_RETURN(BTN_TOOL_QUADTAP);
285 CASE_RETURN(BTN_TOOL_QUINTTAP);
286 default:
287 break;
288 }
289 break;
Sean O'Brien23f8ea72018-02-28 08:16:49 -0800290 case EV_MSC:
291 switch (code) {
292 CASE_RETURN(MSC_TIMESTAMP);
293 default:
294 break;
295 }
296 break;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700297 default:
298 break;
299 }
300 return "?";
301}
302#undef CASE_RETURN
303
304const char *
305Event_Type_To_String(int type) {
306 switch (type) {
307 case EV_SYN: return "SYN";
308 case EV_KEY: return "KEY";
309 case EV_REL: return "REL";
310 case EV_ABS: return "ABS";
311 case EV_MSC: return "MSC";
312 case EV_SW: return "SW";
313 case EV_LED: return "LED";
314 case EV_SND: return "SND";
315 case EV_REP: return "REP";
316 case EV_FF: return "FF";
317 case EV_PWR: return "PWR";
318 default: return "?";
319 }
320}
321
322
323/**
324 * Probe Device Input Event Support
325 */
326int
327Event_Init(EvdevPtr device)
328{
329 int i;
330 EventStatePtr evstate;
331
332 evstate = device->evstate;
333 if (EvdevProbe(device) != Success) {
334 return !Success;
335 }
336
Dennis Kempina9963ba2012-06-08 10:32:23 -0700337 for (i = ABS_X; i <= ABS_MAX; i++) {
338 if (TestBit(i, device->info.abs_bitmask)) {
339 struct input_absinfo* absinfo = &device->info.absinfo[i];
340 if (i == ABS_MT_SLOT) {
341 int rc;
342 rc = MTB_Init(device, absinfo->minimum, absinfo->maximum,
343 absinfo->value);
344 if (rc != Success)
345 return rc;
346 } else if (IS_ABS_MT(i)) {
347 evstate->mt_axes[MT_CODE(i)] = absinfo;
348 }
349 }
350 }
Sean O'Brien23f8ea72018-02-28 08:16:49 -0800351 evstate->msc_timestamp = 0;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700352 return Success;
353}
354
355void
356Event_Free(EvdevPtr device)
357{
358 MT_Free(device);
359}
360
361void
362Event_Open(EvdevPtr device)
363{
364 /* Select monotonic input event timestamps, if supported by kernel */
365 device->info.is_monotonic = (EvdevEnableMonotonic(device) == Success);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700366 LOG_DEBUG(device, "Using %s input event time stamps\n",
367 device->info.is_monotonic ? "monotonic" : "realtime");
Daniel Kurtz000bdff2012-09-17 15:45:29 +0800368
369 /* Synchronize all MT slots with kernel evdev driver */
370 Event_Sync_State(device);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700371}
372
373static void
374Event_Get_Time(struct timeval *t, bool use_monotonic) {
375 struct timespec now;
376 clockid_t clockid = (use_monotonic) ? CLOCK_MONOTONIC : CLOCK_REALTIME;
377
378 clock_gettime(clockid, &now);
379 t->tv_sec = now.tv_sec;
380 t->tv_usec = now.tv_nsec / 1000;
381}
382
383/**
384 * Synchronize the current state with kernel evdev driver. For cmt, there are
385 * only four components required to be synced: current touch count, the MT
386 * slots information, current slot id and physical button states. However, as
387 * pressure readings are missing in ABS_MT_PRESSURE field of MT slots for
388 * semi_mt touchpad device (e.g. Cr48), we also need need to extract it with
389 * extra EVIOCGABS query.
390 */
391void
392Event_Sync_State(EvdevPtr device)
393{
394 int i;
395
396 Event_Get_Time(&device->before_sync_time, device->info.is_monotonic);
397
398 EvdevProbeKeyState(device);
399
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800400 /* Get current pressure information for single-pressure device */
401 if (EvdevIsSinglePressureDevice(device) == Success) {
402 struct input_event ev;
403 ev.code = ABS_PRESSURE;
404 ev.value = device->info.absinfo[ABS_PRESSURE].value;
405 Event_Abs_Update_Pressure(device, &ev);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700406 }
407
408 /* TODO(cywang): Sync all ABS_ states for completeness */
409
410 /* Get current MT information for each slot */
411 for (i = _ABS_MT_FIRST; i <= _ABS_MT_LAST; i++) {
412 MTSlotInfo req;
413
414 if (!TestBit(i, device->info.abs_bitmask))
415 continue;
416 /*
417 * TODO(cywang): Scale the size of slots in MTSlotInfo based on the
418 * evstate->slot_count.
419 */
420
421 req.code = i;
422 if (EvdevProbeMTSlot(device, &req) != Success) {
423 continue;
424 }
425 MT_Slot_Sync(device, &req);
426 }
427
Chung-yih Wang8cb858a2014-04-21 16:38:23 +0800428 /* Get current slot id for multi-touch devices*/
429 if (TestBit(ABS_MT_SLOT, device->info.abs_bitmask) &&
430 (EvdevProbeAbsinfo(device, ABS_MT_SLOT) == Success)) {
Dennis Kempina9963ba2012-06-08 10:32:23 -0700431 MT_Slot_Set(device, device->info.absinfo[ABS_MT_SLOT].value);
Chung-yih Wang8cb858a2014-04-21 16:38:23 +0800432 }
Dennis Kempina9963ba2012-06-08 10:32:23 -0700433
434 Event_Get_Time(&device->after_sync_time, device->info.is_monotonic);
435
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700436 /* Initialize EV_REL event state */
437 Event_Clear_Ev_Rel_State(device);
438
Daniel Erat9f7a1962016-09-16 10:14:56 -0600439 LOG_DEBUG(device, "Event_Sync_State: before %ld.%ld after %ld.%ld\n",
440 (long)device->before_sync_time.tv_sec,
441 (long)device->before_sync_time.tv_usec,
442 (long)device->after_sync_time.tv_sec,
443 (long)device->after_sync_time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700444}
445
446static void
447Event_Print(EvdevPtr device, struct input_event* ev)
448{
449 switch (ev->type) {
450 case EV_SYN:
451 switch (ev->code) {
452 case SYN_REPORT:
Dennis Kempinde0712f2012-06-11 15:13:50 -0700453 LOG_DEBUG(device, "@ %ld.%06ld ---------- SYN_REPORT -------\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500454 (long)ev->time.tv_sec, (long)ev->time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700455 return;
456 case SYN_MT_REPORT:
Dennis Kempinde0712f2012-06-11 15:13:50 -0700457 LOG_DEBUG(device, "@ %ld.%06ld ........ SYN_MT_REPORT ......\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500458 (long)ev->time.tv_sec, (long)ev->time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700459 return;
460 case SYN_DROPPED:
Dennis Kempin85a14442012-06-25 11:24:59 -0700461 LOG_WARNING(device, "@ %ld.%06ld ++++++++ SYN_DROPPED ++++++++\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500462 (long)ev->time.tv_sec, (long)ev->time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700463 return;
464 default:
Dennis Kempin85a14442012-06-25 11:24:59 -0700465 LOG_WARNING(device, "@ %ld.%06ld ?????? SYN_UNKNOWN (%d) ?????\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500466 (long)ev->time.tv_sec, (long)ev->time.tv_usec, ev->code);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700467 return;
468 }
469 break;
470 case EV_ABS:
471 if (ev->code == ABS_MT_SLOT) {
Dennis Kempinde0712f2012-06-11 15:13:50 -0700472 LOG_DEBUG(device, "@ %ld.%06ld .......... MT SLOT %d ........\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500473 (long)ev->time.tv_sec, (long)ev->time.tv_usec, ev->value);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700474 return;
475 }
476 break;
477 default:
478 break;
479 }
480
Dennis Kempinde0712f2012-06-11 15:13:50 -0700481 LOG_DEBUG(device, "@ %ld.%06ld %s[%d] (%s) = %d\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500482 (long)ev->time.tv_sec, (long)ev->time.tv_usec,
483 Event_Type_To_String(ev->type), ev->code,
484 Event_To_String(ev->type, ev->code), ev->value);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700485}
486
487/**
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700488 * Process Input Events. It returns TRUE if SYN_DROPPED detected.
Dennis Kempina9963ba2012-06-08 10:32:23 -0700489 */
490bool
491Event_Process(EvdevPtr device, struct input_event* ev)
492{
Dennis Kempina9963ba2012-06-08 10:32:23 -0700493 Event_Print(device, ev);
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700494 if (Event_Is_Valid(ev)) {
495 if (!(ev->type == EV_SYN && ev->code == SYN_REPORT))
496 device->got_valid_event = 1;
497 } else {
498 return false;
499 }
Dennis Kempina9963ba2012-06-08 10:32:23 -0700500
501 switch (ev->type) {
502 case EV_SYN:
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700503 return Event_Syn(device, ev);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700504
505 case EV_KEY:
506 Event_Key(device, ev);
507 break;
508
509 case EV_ABS:
510 Event_Abs(device, ev);
511 break;
512
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700513 case EV_REL:
514 Event_Rel(device, ev);
515 break;
516
Sean O'Brien23f8ea72018-02-28 08:16:49 -0800517 case EV_MSC:
518 Event_Msc(device, ev);
519 break;
520
Dennis Kempina9963ba2012-06-08 10:32:23 -0700521 default:
522 break;
523 }
524 return false;
525}
526
527/**
528 * Dump the log of input events to disk
529 */
530void
Dennis Kempin328acb82014-04-10 13:58:25 -0700531Event_Dump_Debug_Log(void* vinfo) {
532 Event_Dump_Debug_Log_To(vinfo, "/var/log/xorg/cmt_input_events.dat");
533}
534
535void
536Event_Dump_Debug_Log_To(void* vinfo, const char* filename)
Dennis Kempina9963ba2012-06-08 10:32:23 -0700537{
538 EvdevPtr device = (EvdevPtr) vinfo;
539 size_t i;
Dennis Kempin69c91ed2013-02-12 14:53:10 -0800540 int ret;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700541 EventStatePtr evstate = device->evstate;
542
Dennis Kempin328acb82014-04-10 13:58:25 -0700543 FILE* fp = fopen(filename, "wb");
Dennis Kempina9963ba2012-06-08 10:32:23 -0700544 if (!fp) {
545 LOG_ERROR(device, "fopen() failed for debug log");
546 return;
547 }
Dennis Kempin69c91ed2013-02-12 14:53:10 -0800548
549 ret = EvdevWriteInfoToFile(fp, &device->info);
550 if (ret <= 0) {
551 LOG_ERROR(device, "EvdevWriteInfoToFile failed. Log without info.");
552 }
553
Dennis Kempina9963ba2012-06-08 10:32:23 -0700554 for (i = 0; i < DEBUG_BUF_SIZE; i++) {
Dennis Kempina9963ba2012-06-08 10:32:23 -0700555 struct input_event *ev =
556 &evstate->debug_buf[(evstate->debug_buf_tail + i) % DEBUG_BUF_SIZE];
557 if (ev->time.tv_sec == 0 && ev->time.tv_usec == 0)
558 continue;
Dennis Kempin69c91ed2013-02-12 14:53:10 -0800559 ret = EvdevWriteEventToFile(fp, ev);
560 if (ret <= 0) {
561 LOG_ERROR(device, "EvdevWriteEventToFile failed. Log is short.");
Dennis Kempina9963ba2012-06-08 10:32:23 -0700562 break;
563 }
564 }
565 fclose(fp);
566}
567
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700568/**
Dennis Kempind6ae1672014-01-08 11:41:33 -0800569 * Clear Debug Buffer
570 */
571void
572Event_Clear_Debug_Log(void* vinfo)
573{
574 EvdevPtr device = (EvdevPtr) vinfo;
575 EventStatePtr evstate = device->evstate;
576
577 memset(evstate->debug_buf, 0, sizeof(evstate->debug_buf));
578 evstate->debug_buf_tail = 0;
579}
580
581/**
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700582 * Clear EV_REL event state. This function should be called after a EV_SYN
583 * event is processed because EV_REL event state is not accumulative.
584 */
Dennis Kempina9963ba2012-06-08 10:32:23 -0700585static void
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700586Event_Clear_Ev_Rel_State(EvdevPtr device)
587{
588 EventStatePtr evstate;
589
590 evstate = device->evstate;
591 evstate->rel_x = 0;
592 evstate->rel_y = 0;
593 evstate->rel_wheel = 0;
Harry Cutts61ae22b2018-09-20 17:08:43 -0700594 evstate->rel_wheel_hi_res = 0;
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700595 evstate->rel_hwheel = 0;
596}
597
598/**
599 * Process EV_SYN events. It returns TRUE if SYN_DROPPED detected.
600 */
601static bool
Dennis Kempina9963ba2012-06-08 10:32:23 -0700602Event_Syn(EvdevPtr device, struct input_event* ev)
603{
604 switch (ev->code) {
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700605 case SYN_DROPPED:
606 /*
607 * Technically we don't need to call Event_Clear_Ev_Rel_State() here
608 * because when SYN_DROPPED is detected, Event_Sync_State() will be
609 * called and Event_Sync_State() will call Event_Clear_Ev_Rel_State()
610 * to re-initialize EV_REL event state.
611 */
612 return true;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700613 case SYN_REPORT:
614 Event_Syn_Report(device, ev);
615 break;
616 case SYN_MT_REPORT:
617 Event_Syn_MT_Report(device, ev);
618 break;
619 }
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700620 return false;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700621}
622
623static void
624Event_Syn_Report(EvdevPtr device, struct input_event* ev)
625{
626 EventStatePtr evstate = device->evstate;
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700627 if (device->got_valid_event)
628 device->syn_report(device->syn_report_udata, evstate, &ev->time);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700629
630 MT_Print_Slots(device);
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700631
632 Event_Clear_Ev_Rel_State(device);
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700633 device->got_valid_event = 0;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700634}
635
636static void
637Event_Syn_MT_Report(EvdevPtr device, struct input_event* ev)
638{
639 /* TODO(djkurtz): Handle MT-A */
640}
641
642static void
643Event_Key(EvdevPtr device, struct input_event* ev)
644{
645 AssignBit(device->key_state_bitmask, ev->code, ev->value);
646}
647
648static void
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800649Event_Abs_Update_Pressure(EvdevPtr device, struct input_event* ev)
Dennis Kempina9963ba2012-06-08 10:32:23 -0700650{
651 /*
652 * Update all active slots with the same ABS_PRESSURE value if it is a
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800653 * single-pressure device.
Dennis Kempina9963ba2012-06-08 10:32:23 -0700654 */
655 EventStatePtr evstate = device->evstate;
656
657 for (int i = 0; i < evstate->slot_count; i++) {
658 MtSlotPtr slot = &evstate->slots[i];
659 slot->pressure = ev->value;
660 }
661}
662
663static void
664Event_Abs(EvdevPtr device, struct input_event* ev)
665{
666 if (ev->code == ABS_MT_SLOT)
667 MT_Slot_Set(device, ev->value);
668 else if (IS_ABS_MT(ev->code))
669 Event_Abs_MT(device, ev);
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800670 else if ((ev->code == ABS_PRESSURE) && EvdevIsSinglePressureDevice(device))
671 Event_Abs_Update_Pressure(device, ev);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700672}
673
674static void
675Event_Abs_MT(EvdevPtr device, struct input_event* ev)
676{
677 EventStatePtr evstate = device->evstate;
678 struct input_absinfo* axis = evstate->mt_axes[MT_CODE(ev->code)];
679 MtSlotPtr slot = evstate->slot_current;
680
681 if (axis == NULL) {
Dennis Kempin85a14442012-06-25 11:24:59 -0700682 LOG_WARNING(device, "ABS_MT[%02x] was not reported by this device\n",
Dennis Kempina9963ba2012-06-08 10:32:23 -0700683 ev->code);
684 return;
685 }
686
687 /* Warn about out of range data, but don't ignore */
688 if ((ev->code != ABS_MT_TRACKING_ID)
689 && ((ev->value < axis->minimum)
690 || (ev->value > axis->maximum))) {
691 LOG_WARNING(device, "ABS_MT[%02x] = %d : value out of range [%d .. %d]\n",
692 ev->code, ev->value, axis->minimum, axis->maximum);
Chung-yih Wangf573f892013-01-04 14:29:02 +0800693 /* Update the effective boundary as we already print out the warning */
694 if (ev->value < axis->minimum)
695 axis->minimum = ev->value;
696 else if (ev->value > axis->maximum)
697 axis->maximum = ev->value;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700698 }
699
700 if (slot == NULL) {
Dennis Kempin85a14442012-06-25 11:24:59 -0700701 LOG_WARNING(device, "MT slot not set. Ignoring ABS_MT event\n");
Dennis Kempina9963ba2012-06-08 10:32:23 -0700702 return;
703 }
704
705 MT_Slot_Value_Set(slot, ev->code, ev->value);
706}
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700707
708static void
709Event_Rel(EvdevPtr device, struct input_event* ev)
710{
711 EventStatePtr evstate = device->evstate;
712
713 switch (ev->code) {
714 case REL_X:
715 evstate->rel_x = ev->value;
716 break;
717 case REL_Y:
718 evstate->rel_y = ev->value;
719 break;
720 case REL_WHEEL:
721 evstate->rel_wheel = ev->value;
722 break;
Harry Cutts61ae22b2018-09-20 17:08:43 -0700723 case REL_WHEEL_HI_RES:
724 evstate->rel_wheel_hi_res = ev->value;
725 break;
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700726 case REL_HWHEEL:
727 evstate->rel_hwheel = ev->value;
728 break;
729 }
730}
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700731
Sean O'Brien23f8ea72018-02-28 08:16:49 -0800732static void
733Event_Msc(EvdevPtr device, struct input_event* ev)
734{
735 EventStatePtr evstate = device->evstate;
736
737 switch (ev->code) {
738 case MSC_TIMESTAMP:
739 evstate->msc_timestamp = ev->value;
740 break;
741 }
742}
743
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700744static int Event_Is_Valid(struct input_event* ev)
745{
746 /* Key repeats are invalid. They're handled by X anyway */
747 if (ev->type == EV_KEY &&
748 ev->value == 2)
749 return 0;
750 return 1;
751}