blob: e1835b407a4ae406169aea0fc1f37b9971edb152 [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
Sean O'Brien23f8ea72018-02-28 08:16:49 -080054static void Event_Msc(EvdevPtr, struct input_event*);
55
Dennis Kempina9963ba2012-06-08 10:32:23 -070056static void Event_Get_Time(struct timeval*, bool);
57
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -070058static int Event_Is_Valid(struct input_event*);
59
Chung-yih Wang1359d8b2012-11-21 11:25:33 +080060const char*
61Evdev_Get_Version() {
62 return VCSID;
63}
64
Dennis Kempina9963ba2012-06-08 10:32:23 -070065/**
66 * Input Device Event Property accessors
67 */
68int
69Event_Get_Left(EvdevPtr device)
70{
Yufeng Shen86e52252012-07-05 11:44:36 -040071 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_X];
Dennis Kempina9963ba2012-06-08 10:32:23 -070072 return absinfo->minimum;
73}
74
75int
76Event_Get_Right(EvdevPtr device)
77{
Yufeng Shen86e52252012-07-05 11:44:36 -040078 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_X];
Dennis Kempina9963ba2012-06-08 10:32:23 -070079 return absinfo->maximum;
80}
81
82int
83Event_Get_Top(EvdevPtr device)
84{
Yufeng Shen86e52252012-07-05 11:44:36 -040085 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_Y];
Dennis Kempina9963ba2012-06-08 10:32:23 -070086 return absinfo->minimum;
87}
88
89int
90Event_Get_Bottom(EvdevPtr device)
91{
Yufeng Shen86e52252012-07-05 11:44:36 -040092 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_Y];
Dennis Kempina9963ba2012-06-08 10:32:23 -070093 return absinfo->maximum;
94}
95
96int
97Event_Get_Res_Y(EvdevPtr device)
98{
Yufeng Shen86e52252012-07-05 11:44:36 -040099 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_Y];
Dennis Kempina9963ba2012-06-08 10:32:23 -0700100 return absinfo->resolution;
101}
102
103int
104Event_Get_Res_X(EvdevPtr device)
105{
Yufeng Shen86e52252012-07-05 11:44:36 -0400106 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_POSITION_X];
Dennis Kempina9963ba2012-06-08 10:32:23 -0700107 return absinfo->resolution;
108}
109
110int
Che-Liang Chioua2563012012-10-11 11:41:06 -0700111Event_Get_Orientation_Minimum(EvdevPtr device)
112{
113 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_ORIENTATION];
114 return absinfo->minimum;
115}
116
117int
118Event_Get_Orientation_Maximum(EvdevPtr device)
119{
120 struct input_absinfo* absinfo = &device->info.absinfo[ABS_MT_ORIENTATION];
121 return absinfo->maximum;
122}
123
124int
Dennis Kempina9963ba2012-06-08 10:32:23 -0700125Event_Get_Button_Pad(EvdevPtr device)
126{
127 return TestBit(INPUT_PROP_BUTTONPAD, device->info.prop_bitmask);
128}
129
130int
131Event_Get_Semi_MT(EvdevPtr device)
132{
133 return TestBit(INPUT_PROP_SEMI_MT, device->info.prop_bitmask);
134}
135
136int
137Event_Get_T5R2(EvdevPtr device)
138{
139 EventStatePtr evstate = device->evstate;
140 if (Event_Get_Semi_MT(device))
141 return 0;
142 return (Event_Get_Touch_Count_Max(device) > evstate->slot_count);
143}
144
145int
146Event_Get_Touch_Count_Max(EvdevPtr device)
147{
148
149 if (TestBit(BTN_TOOL_QUINTTAP, device->info.key_bitmask))
150 return 5;
151 if (TestBit(BTN_TOOL_QUADTAP, device->info.key_bitmask))
152 return 4;
153 if (TestBit(BTN_TOOL_TRIPLETAP, device->info.key_bitmask))
154 return 3;
155 if (TestBit(BTN_TOOL_DOUBLETAP, device->info.key_bitmask))
156 return 2;
157 return 1;
158}
159
160int
161Event_Get_Touch_Count(EvdevPtr device)
162{
163
Charlie Mooney6647f242016-07-13 13:53:28 -0700164 if (TestBit(BTN_TOUCH, device->key_state_bitmask)) {
165 if (TestBit(BTN_TOOL_QUINTTAP, device->key_state_bitmask))
166 return 5;
167 if (TestBit(BTN_TOOL_QUADTAP, device->key_state_bitmask))
168 return 4;
169 if (TestBit(BTN_TOOL_TRIPLETAP, device->key_state_bitmask))
170 return 3;
171 if (TestBit(BTN_TOOL_DOUBLETAP, device->key_state_bitmask))
172 return 2;
173 if (TestBit(BTN_TOOL_FINGER, device->key_state_bitmask))
174 return 1;
175 }
Dennis Kempina9963ba2012-06-08 10:32:23 -0700176 return 0;
177}
178
179int
180Event_Get_Slot_Count(EvdevPtr device)
181{
182 EventStatePtr evstate = device->evstate;
183 return evstate->slot_count;
184}
185
186int
187Event_Get_Button_Left(EvdevPtr device)
188{
Dennis Kempine2a65d82014-02-24 13:27:40 -0800189 return Event_Get_Button(device, BTN_LEFT);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700190}
191
192int
193Event_Get_Button_Middle(EvdevPtr device)
194{
Dennis Kempine2a65d82014-02-24 13:27:40 -0800195 return Event_Get_Button(device, BTN_MIDDLE);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700196}
197
198int
199Event_Get_Button_Right(EvdevPtr device)
200{
Dennis Kempine2a65d82014-02-24 13:27:40 -0800201 return Event_Get_Button(device, BTN_RIGHT);
202}
203
204int
205Event_Get_Button(EvdevPtr device, int button)
206{
207 return TestBit(button, device->key_state_bitmask);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700208}
209
Sean O'Brien23f8ea72018-02-28 08:16:49 -0800210int
211Event_Get_Timestamp(EvdevPtr device)
212{
213 EventStatePtr evstate = device->evstate;
214 return evstate->msc_timestamp;
215}
216
Dennis Kempina9963ba2012-06-08 10:32:23 -0700217#define CASE_RETURN(s) \
218 case (s):\
219 return #s
220
221
222const char *
223Event_To_String(int type, int code) {
224 switch (type) {
225 case EV_SYN:
226 switch (code) {
227 CASE_RETURN(SYN_REPORT);
228 CASE_RETURN(SYN_MT_REPORT);
229 default:
230 break;
231 }
232 break;
233 case EV_ABS:
234 switch (code) {
235 CASE_RETURN(ABS_X);
236 CASE_RETURN(ABS_Y);
237 CASE_RETURN(ABS_Z);
238 CASE_RETURN(ABS_PRESSURE);
239 CASE_RETURN(ABS_TOOL_WIDTH);
240 CASE_RETURN(ABS_MT_TOUCH_MAJOR);
241 CASE_RETURN(ABS_MT_TOUCH_MINOR);
242 CASE_RETURN(ABS_MT_WIDTH_MAJOR);
243 CASE_RETURN(ABS_MT_WIDTH_MINOR);
244 CASE_RETURN(ABS_MT_ORIENTATION);
245 CASE_RETURN(ABS_MT_POSITION_X);
246 CASE_RETURN(ABS_MT_POSITION_Y);
247 CASE_RETURN(ABS_MT_TOOL_TYPE);
248 CASE_RETURN(ABS_MT_BLOB_ID);
249 CASE_RETURN(ABS_MT_TRACKING_ID);
250 CASE_RETURN(ABS_MT_PRESSURE);
251 CASE_RETURN(ABS_MT_SLOT);
252 default:
253 break;
254 }
255 break;
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700256 case EV_REL:
257 switch (code) {
258 CASE_RETURN(REL_X);
259 CASE_RETURN(REL_Y);
260 CASE_RETURN(REL_WHEEL);
261 CASE_RETURN(REL_HWHEEL);
262 default:
263 break;
264 }
265 break;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700266 case EV_KEY:
267 switch (code) {
268 CASE_RETURN(BTN_LEFT);
269 CASE_RETURN(BTN_RIGHT);
270 CASE_RETURN(BTN_MIDDLE);
Dennis Kempine2a65d82014-02-24 13:27:40 -0800271 CASE_RETURN(BTN_BACK);
272 CASE_RETURN(BTN_FORWARD);
273 CASE_RETURN(BTN_EXTRA);
274 CASE_RETURN(BTN_SIDE);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700275 CASE_RETURN(BTN_TOUCH);
276 CASE_RETURN(BTN_TOOL_FINGER);
277 CASE_RETURN(BTN_TOOL_DOUBLETAP);
278 CASE_RETURN(BTN_TOOL_TRIPLETAP);
279 CASE_RETURN(BTN_TOOL_QUADTAP);
280 CASE_RETURN(BTN_TOOL_QUINTTAP);
281 default:
282 break;
283 }
284 break;
Sean O'Brien23f8ea72018-02-28 08:16:49 -0800285 case EV_MSC:
286 switch (code) {
287 CASE_RETURN(MSC_TIMESTAMP);
288 default:
289 break;
290 }
291 break;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700292 default:
293 break;
294 }
295 return "?";
296}
297#undef CASE_RETURN
298
299const char *
300Event_Type_To_String(int type) {
301 switch (type) {
302 case EV_SYN: return "SYN";
303 case EV_KEY: return "KEY";
304 case EV_REL: return "REL";
305 case EV_ABS: return "ABS";
306 case EV_MSC: return "MSC";
307 case EV_SW: return "SW";
308 case EV_LED: return "LED";
309 case EV_SND: return "SND";
310 case EV_REP: return "REP";
311 case EV_FF: return "FF";
312 case EV_PWR: return "PWR";
313 default: return "?";
314 }
315}
316
317
318/**
319 * Probe Device Input Event Support
320 */
321int
322Event_Init(EvdevPtr device)
323{
324 int i;
325 EventStatePtr evstate;
326
327 evstate = device->evstate;
328 if (EvdevProbe(device) != Success) {
329 return !Success;
330 }
331
Dennis Kempina9963ba2012-06-08 10:32:23 -0700332 for (i = ABS_X; i <= ABS_MAX; i++) {
333 if (TestBit(i, device->info.abs_bitmask)) {
334 struct input_absinfo* absinfo = &device->info.absinfo[i];
335 if (i == ABS_MT_SLOT) {
336 int rc;
337 rc = MTB_Init(device, absinfo->minimum, absinfo->maximum,
338 absinfo->value);
339 if (rc != Success)
340 return rc;
341 } else if (IS_ABS_MT(i)) {
342 evstate->mt_axes[MT_CODE(i)] = absinfo;
343 }
344 }
345 }
Sean O'Brien23f8ea72018-02-28 08:16:49 -0800346 evstate->msc_timestamp = 0;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700347 return Success;
348}
349
350void
351Event_Free(EvdevPtr device)
352{
353 MT_Free(device);
354}
355
356void
357Event_Open(EvdevPtr device)
358{
359 /* Select monotonic input event timestamps, if supported by kernel */
360 device->info.is_monotonic = (EvdevEnableMonotonic(device) == Success);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700361 LOG_DEBUG(device, "Using %s input event time stamps\n",
362 device->info.is_monotonic ? "monotonic" : "realtime");
Daniel Kurtz000bdff2012-09-17 15:45:29 +0800363
364 /* Synchronize all MT slots with kernel evdev driver */
365 Event_Sync_State(device);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700366}
367
368static void
369Event_Get_Time(struct timeval *t, bool use_monotonic) {
370 struct timespec now;
371 clockid_t clockid = (use_monotonic) ? CLOCK_MONOTONIC : CLOCK_REALTIME;
372
373 clock_gettime(clockid, &now);
374 t->tv_sec = now.tv_sec;
375 t->tv_usec = now.tv_nsec / 1000;
376}
377
378/**
379 * Synchronize the current state with kernel evdev driver. For cmt, there are
380 * only four components required to be synced: current touch count, the MT
381 * slots information, current slot id and physical button states. However, as
382 * pressure readings are missing in ABS_MT_PRESSURE field of MT slots for
383 * semi_mt touchpad device (e.g. Cr48), we also need need to extract it with
384 * extra EVIOCGABS query.
385 */
386void
387Event_Sync_State(EvdevPtr device)
388{
389 int i;
390
391 Event_Get_Time(&device->before_sync_time, device->info.is_monotonic);
392
393 EvdevProbeKeyState(device);
394
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800395 /* Get current pressure information for single-pressure device */
396 if (EvdevIsSinglePressureDevice(device) == Success) {
397 struct input_event ev;
398 ev.code = ABS_PRESSURE;
399 ev.value = device->info.absinfo[ABS_PRESSURE].value;
400 Event_Abs_Update_Pressure(device, &ev);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700401 }
402
403 /* TODO(cywang): Sync all ABS_ states for completeness */
404
405 /* Get current MT information for each slot */
406 for (i = _ABS_MT_FIRST; i <= _ABS_MT_LAST; i++) {
407 MTSlotInfo req;
408
409 if (!TestBit(i, device->info.abs_bitmask))
410 continue;
411 /*
412 * TODO(cywang): Scale the size of slots in MTSlotInfo based on the
413 * evstate->slot_count.
414 */
415
416 req.code = i;
417 if (EvdevProbeMTSlot(device, &req) != Success) {
418 continue;
419 }
420 MT_Slot_Sync(device, &req);
421 }
422
Chung-yih Wang8cb858a2014-04-21 16:38:23 +0800423 /* Get current slot id for multi-touch devices*/
424 if (TestBit(ABS_MT_SLOT, device->info.abs_bitmask) &&
425 (EvdevProbeAbsinfo(device, ABS_MT_SLOT) == Success)) {
Dennis Kempina9963ba2012-06-08 10:32:23 -0700426 MT_Slot_Set(device, device->info.absinfo[ABS_MT_SLOT].value);
Chung-yih Wang8cb858a2014-04-21 16:38:23 +0800427 }
Dennis Kempina9963ba2012-06-08 10:32:23 -0700428
429 Event_Get_Time(&device->after_sync_time, device->info.is_monotonic);
430
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700431 /* Initialize EV_REL event state */
432 Event_Clear_Ev_Rel_State(device);
433
Daniel Erat9f7a1962016-09-16 10:14:56 -0600434 LOG_DEBUG(device, "Event_Sync_State: before %ld.%ld after %ld.%ld\n",
435 (long)device->before_sync_time.tv_sec,
436 (long)device->before_sync_time.tv_usec,
437 (long)device->after_sync_time.tv_sec,
438 (long)device->after_sync_time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700439}
440
441static void
442Event_Print(EvdevPtr device, struct input_event* ev)
443{
444 switch (ev->type) {
445 case EV_SYN:
446 switch (ev->code) {
447 case SYN_REPORT:
Dennis Kempinde0712f2012-06-11 15:13:50 -0700448 LOG_DEBUG(device, "@ %ld.%06ld ---------- SYN_REPORT -------\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500449 (long)ev->time.tv_sec, (long)ev->time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700450 return;
451 case SYN_MT_REPORT:
Dennis Kempinde0712f2012-06-11 15:13:50 -0700452 LOG_DEBUG(device, "@ %ld.%06ld ........ SYN_MT_REPORT ......\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500453 (long)ev->time.tv_sec, (long)ev->time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700454 return;
455 case SYN_DROPPED:
Dennis Kempin85a14442012-06-25 11:24:59 -0700456 LOG_WARNING(device, "@ %ld.%06ld ++++++++ SYN_DROPPED ++++++++\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500457 (long)ev->time.tv_sec, (long)ev->time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700458 return;
459 default:
Dennis Kempin85a14442012-06-25 11:24:59 -0700460 LOG_WARNING(device, "@ %ld.%06ld ?????? SYN_UNKNOWN (%d) ?????\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500461 (long)ev->time.tv_sec, (long)ev->time.tv_usec, ev->code);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700462 return;
463 }
464 break;
465 case EV_ABS:
466 if (ev->code == ABS_MT_SLOT) {
Dennis Kempinde0712f2012-06-11 15:13:50 -0700467 LOG_DEBUG(device, "@ %ld.%06ld .......... MT SLOT %d ........\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500468 (long)ev->time.tv_sec, (long)ev->time.tv_usec, ev->value);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700469 return;
470 }
471 break;
472 default:
473 break;
474 }
475
Dennis Kempinde0712f2012-06-11 15:13:50 -0700476 LOG_DEBUG(device, "@ %ld.%06ld %s[%d] (%s) = %d\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500477 (long)ev->time.tv_sec, (long)ev->time.tv_usec,
478 Event_Type_To_String(ev->type), ev->code,
479 Event_To_String(ev->type, ev->code), ev->value);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700480}
481
482/**
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700483 * Process Input Events. It returns TRUE if SYN_DROPPED detected.
Dennis Kempina9963ba2012-06-08 10:32:23 -0700484 */
485bool
486Event_Process(EvdevPtr device, struct input_event* ev)
487{
Dennis Kempina9963ba2012-06-08 10:32:23 -0700488 Event_Print(device, ev);
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700489 if (Event_Is_Valid(ev)) {
490 if (!(ev->type == EV_SYN && ev->code == SYN_REPORT))
491 device->got_valid_event = 1;
492 } else {
493 return false;
494 }
Dennis Kempina9963ba2012-06-08 10:32:23 -0700495
496 switch (ev->type) {
497 case EV_SYN:
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700498 return Event_Syn(device, ev);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700499
500 case EV_KEY:
501 Event_Key(device, ev);
502 break;
503
504 case EV_ABS:
505 Event_Abs(device, ev);
506 break;
507
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700508 case EV_REL:
509 Event_Rel(device, ev);
510 break;
511
Sean O'Brien23f8ea72018-02-28 08:16:49 -0800512 case EV_MSC:
513 Event_Msc(device, ev);
514 break;
515
Dennis Kempina9963ba2012-06-08 10:32:23 -0700516 default:
517 break;
518 }
519 return false;
520}
521
522/**
523 * Dump the log of input events to disk
524 */
525void
Dennis Kempin328acb82014-04-10 13:58:25 -0700526Event_Dump_Debug_Log(void* vinfo) {
527 Event_Dump_Debug_Log_To(vinfo, "/var/log/xorg/cmt_input_events.dat");
528}
529
530void
531Event_Dump_Debug_Log_To(void* vinfo, const char* filename)
Dennis Kempina9963ba2012-06-08 10:32:23 -0700532{
533 EvdevPtr device = (EvdevPtr) vinfo;
534 size_t i;
Dennis Kempin69c91ed2013-02-12 14:53:10 -0800535 int ret;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700536 EventStatePtr evstate = device->evstate;
537
Dennis Kempin328acb82014-04-10 13:58:25 -0700538 FILE* fp = fopen(filename, "wb");
Dennis Kempina9963ba2012-06-08 10:32:23 -0700539 if (!fp) {
540 LOG_ERROR(device, "fopen() failed for debug log");
541 return;
542 }
Dennis Kempin69c91ed2013-02-12 14:53:10 -0800543
544 ret = EvdevWriteInfoToFile(fp, &device->info);
545 if (ret <= 0) {
546 LOG_ERROR(device, "EvdevWriteInfoToFile failed. Log without info.");
547 }
548
Dennis Kempina9963ba2012-06-08 10:32:23 -0700549 for (i = 0; i < DEBUG_BUF_SIZE; i++) {
Dennis Kempina9963ba2012-06-08 10:32:23 -0700550 struct input_event *ev =
551 &evstate->debug_buf[(evstate->debug_buf_tail + i) % DEBUG_BUF_SIZE];
552 if (ev->time.tv_sec == 0 && ev->time.tv_usec == 0)
553 continue;
Dennis Kempin69c91ed2013-02-12 14:53:10 -0800554 ret = EvdevWriteEventToFile(fp, ev);
555 if (ret <= 0) {
556 LOG_ERROR(device, "EvdevWriteEventToFile failed. Log is short.");
Dennis Kempina9963ba2012-06-08 10:32:23 -0700557 break;
558 }
559 }
560 fclose(fp);
561}
562
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700563/**
Dennis Kempind6ae1672014-01-08 11:41:33 -0800564 * Clear Debug Buffer
565 */
566void
567Event_Clear_Debug_Log(void* vinfo)
568{
569 EvdevPtr device = (EvdevPtr) vinfo;
570 EventStatePtr evstate = device->evstate;
571
572 memset(evstate->debug_buf, 0, sizeof(evstate->debug_buf));
573 evstate->debug_buf_tail = 0;
574}
575
576/**
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700577 * Clear EV_REL event state. This function should be called after a EV_SYN
578 * event is processed because EV_REL event state is not accumulative.
579 */
Dennis Kempina9963ba2012-06-08 10:32:23 -0700580static void
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700581Event_Clear_Ev_Rel_State(EvdevPtr device)
582{
583 EventStatePtr evstate;
584
585 evstate = device->evstate;
586 evstate->rel_x = 0;
587 evstate->rel_y = 0;
588 evstate->rel_wheel = 0;
589 evstate->rel_hwheel = 0;
590}
591
592/**
593 * Process EV_SYN events. It returns TRUE if SYN_DROPPED detected.
594 */
595static bool
Dennis Kempina9963ba2012-06-08 10:32:23 -0700596Event_Syn(EvdevPtr device, struct input_event* ev)
597{
598 switch (ev->code) {
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700599 case SYN_DROPPED:
600 /*
601 * Technically we don't need to call Event_Clear_Ev_Rel_State() here
602 * because when SYN_DROPPED is detected, Event_Sync_State() will be
603 * called and Event_Sync_State() will call Event_Clear_Ev_Rel_State()
604 * to re-initialize EV_REL event state.
605 */
606 return true;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700607 case SYN_REPORT:
608 Event_Syn_Report(device, ev);
609 break;
610 case SYN_MT_REPORT:
611 Event_Syn_MT_Report(device, ev);
612 break;
613 }
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700614 return false;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700615}
616
617static void
618Event_Syn_Report(EvdevPtr device, struct input_event* ev)
619{
620 EventStatePtr evstate = device->evstate;
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700621 if (device->got_valid_event)
622 device->syn_report(device->syn_report_udata, evstate, &ev->time);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700623
624 MT_Print_Slots(device);
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700625
626 Event_Clear_Ev_Rel_State(device);
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700627 device->got_valid_event = 0;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700628}
629
630static void
631Event_Syn_MT_Report(EvdevPtr device, struct input_event* ev)
632{
633 /* TODO(djkurtz): Handle MT-A */
634}
635
636static void
637Event_Key(EvdevPtr device, struct input_event* ev)
638{
639 AssignBit(device->key_state_bitmask, ev->code, ev->value);
640}
641
642static void
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800643Event_Abs_Update_Pressure(EvdevPtr device, struct input_event* ev)
Dennis Kempina9963ba2012-06-08 10:32:23 -0700644{
645 /*
646 * Update all active slots with the same ABS_PRESSURE value if it is a
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800647 * single-pressure device.
Dennis Kempina9963ba2012-06-08 10:32:23 -0700648 */
649 EventStatePtr evstate = device->evstate;
650
651 for (int i = 0; i < evstate->slot_count; i++) {
652 MtSlotPtr slot = &evstate->slots[i];
653 slot->pressure = ev->value;
654 }
655}
656
657static void
658Event_Abs(EvdevPtr device, struct input_event* ev)
659{
660 if (ev->code == ABS_MT_SLOT)
661 MT_Slot_Set(device, ev->value);
662 else if (IS_ABS_MT(ev->code))
663 Event_Abs_MT(device, ev);
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800664 else if ((ev->code == ABS_PRESSURE) && EvdevIsSinglePressureDevice(device))
665 Event_Abs_Update_Pressure(device, ev);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700666}
667
668static void
669Event_Abs_MT(EvdevPtr device, struct input_event* ev)
670{
671 EventStatePtr evstate = device->evstate;
672 struct input_absinfo* axis = evstate->mt_axes[MT_CODE(ev->code)];
673 MtSlotPtr slot = evstate->slot_current;
674
675 if (axis == NULL) {
Dennis Kempin85a14442012-06-25 11:24:59 -0700676 LOG_WARNING(device, "ABS_MT[%02x] was not reported by this device\n",
Dennis Kempina9963ba2012-06-08 10:32:23 -0700677 ev->code);
678 return;
679 }
680
681 /* Warn about out of range data, but don't ignore */
682 if ((ev->code != ABS_MT_TRACKING_ID)
683 && ((ev->value < axis->minimum)
684 || (ev->value > axis->maximum))) {
685 LOG_WARNING(device, "ABS_MT[%02x] = %d : value out of range [%d .. %d]\n",
686 ev->code, ev->value, axis->minimum, axis->maximum);
Chung-yih Wangf573f892013-01-04 14:29:02 +0800687 /* Update the effective boundary as we already print out the warning */
688 if (ev->value < axis->minimum)
689 axis->minimum = ev->value;
690 else if (ev->value > axis->maximum)
691 axis->maximum = ev->value;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700692 }
693
694 if (slot == NULL) {
Dennis Kempin85a14442012-06-25 11:24:59 -0700695 LOG_WARNING(device, "MT slot not set. Ignoring ABS_MT event\n");
Dennis Kempina9963ba2012-06-08 10:32:23 -0700696 return;
697 }
698
699 MT_Slot_Value_Set(slot, ev->code, ev->value);
700}
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700701
702static void
703Event_Rel(EvdevPtr device, struct input_event* ev)
704{
705 EventStatePtr evstate = device->evstate;
706
707 switch (ev->code) {
708 case REL_X:
709 evstate->rel_x = ev->value;
710 break;
711 case REL_Y:
712 evstate->rel_y = ev->value;
713 break;
714 case REL_WHEEL:
715 evstate->rel_wheel = ev->value;
716 break;
717 case REL_HWHEEL:
718 evstate->rel_hwheel = ev->value;
719 break;
720 }
721}
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700722
Sean O'Brien23f8ea72018-02-28 08:16:49 -0800723static void
724Event_Msc(EvdevPtr device, struct input_event* ev)
725{
726 EventStatePtr evstate = device->evstate;
727
728 switch (ev->code) {
729 case MSC_TIMESTAMP:
730 evstate->msc_timestamp = ev->value;
731 break;
732 }
733}
734
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700735static int Event_Is_Valid(struct input_event* ev)
736{
737 /* Key repeats are invalid. They're handled by X anyway */
738 if (ev->type == EV_KEY &&
739 ev->value == 2)
740 return 0;
741 return 1;
742}