blob: 694ed5a6074d9303a125472cc2e5ebd7e252d0a5 [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
162 if (TestBit(BTN_TOOL_QUINTTAP, device->key_state_bitmask))
163 return 5;
164 if (TestBit(BTN_TOOL_QUADTAP, device->key_state_bitmask))
165 return 4;
166 if (TestBit(BTN_TOOL_TRIPLETAP, device->key_state_bitmask))
167 return 3;
168 if (TestBit(BTN_TOOL_DOUBLETAP, device->key_state_bitmask))
169 return 2;
170 if (TestBit(BTN_TOOL_FINGER, device->key_state_bitmask))
171 return 1;
172 return 0;
173}
174
175int
176Event_Get_Slot_Count(EvdevPtr device)
177{
178 EventStatePtr evstate = device->evstate;
179 return evstate->slot_count;
180}
181
182int
183Event_Get_Button_Left(EvdevPtr device)
184{
Dennis Kempine2a65d82014-02-24 13:27:40 -0800185 return Event_Get_Button(device, BTN_LEFT);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700186}
187
188int
189Event_Get_Button_Middle(EvdevPtr device)
190{
Dennis Kempine2a65d82014-02-24 13:27:40 -0800191 return Event_Get_Button(device, BTN_MIDDLE);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700192}
193
194int
195Event_Get_Button_Right(EvdevPtr device)
196{
Dennis Kempine2a65d82014-02-24 13:27:40 -0800197 return Event_Get_Button(device, BTN_RIGHT);
198}
199
200int
201Event_Get_Button(EvdevPtr device, int button)
202{
203 return TestBit(button, device->key_state_bitmask);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700204}
205
206#define CASE_RETURN(s) \
207 case (s):\
208 return #s
209
210
211const char *
212Event_To_String(int type, int code) {
213 switch (type) {
214 case EV_SYN:
215 switch (code) {
216 CASE_RETURN(SYN_REPORT);
217 CASE_RETURN(SYN_MT_REPORT);
218 default:
219 break;
220 }
221 break;
222 case EV_ABS:
223 switch (code) {
224 CASE_RETURN(ABS_X);
225 CASE_RETURN(ABS_Y);
226 CASE_RETURN(ABS_Z);
227 CASE_RETURN(ABS_PRESSURE);
228 CASE_RETURN(ABS_TOOL_WIDTH);
229 CASE_RETURN(ABS_MT_TOUCH_MAJOR);
230 CASE_RETURN(ABS_MT_TOUCH_MINOR);
231 CASE_RETURN(ABS_MT_WIDTH_MAJOR);
232 CASE_RETURN(ABS_MT_WIDTH_MINOR);
233 CASE_RETURN(ABS_MT_ORIENTATION);
234 CASE_RETURN(ABS_MT_POSITION_X);
235 CASE_RETURN(ABS_MT_POSITION_Y);
236 CASE_RETURN(ABS_MT_TOOL_TYPE);
237 CASE_RETURN(ABS_MT_BLOB_ID);
238 CASE_RETURN(ABS_MT_TRACKING_ID);
239 CASE_RETURN(ABS_MT_PRESSURE);
240 CASE_RETURN(ABS_MT_SLOT);
241 default:
242 break;
243 }
244 break;
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700245 case EV_REL:
246 switch (code) {
247 CASE_RETURN(REL_X);
248 CASE_RETURN(REL_Y);
249 CASE_RETURN(REL_WHEEL);
250 CASE_RETURN(REL_HWHEEL);
251 default:
252 break;
253 }
254 break;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700255 case EV_KEY:
256 switch (code) {
257 CASE_RETURN(BTN_LEFT);
258 CASE_RETURN(BTN_RIGHT);
259 CASE_RETURN(BTN_MIDDLE);
Dennis Kempine2a65d82014-02-24 13:27:40 -0800260 CASE_RETURN(BTN_BACK);
261 CASE_RETURN(BTN_FORWARD);
262 CASE_RETURN(BTN_EXTRA);
263 CASE_RETURN(BTN_SIDE);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700264 CASE_RETURN(BTN_TOUCH);
265 CASE_RETURN(BTN_TOOL_FINGER);
266 CASE_RETURN(BTN_TOOL_DOUBLETAP);
267 CASE_RETURN(BTN_TOOL_TRIPLETAP);
268 CASE_RETURN(BTN_TOOL_QUADTAP);
269 CASE_RETURN(BTN_TOOL_QUINTTAP);
270 default:
271 break;
272 }
273 break;
274 default:
275 break;
276 }
277 return "?";
278}
279#undef CASE_RETURN
280
281const char *
282Event_Type_To_String(int type) {
283 switch (type) {
284 case EV_SYN: return "SYN";
285 case EV_KEY: return "KEY";
286 case EV_REL: return "REL";
287 case EV_ABS: return "ABS";
288 case EV_MSC: return "MSC";
289 case EV_SW: return "SW";
290 case EV_LED: return "LED";
291 case EV_SND: return "SND";
292 case EV_REP: return "REP";
293 case EV_FF: return "FF";
294 case EV_PWR: return "PWR";
295 default: return "?";
296 }
297}
298
299
300/**
301 * Probe Device Input Event Support
302 */
303int
304Event_Init(EvdevPtr device)
305{
306 int i;
307 EventStatePtr evstate;
308
309 evstate = device->evstate;
310 if (EvdevProbe(device) != Success) {
311 return !Success;
312 }
313
Dennis Kempina9963ba2012-06-08 10:32:23 -0700314 for (i = ABS_X; i <= ABS_MAX; i++) {
315 if (TestBit(i, device->info.abs_bitmask)) {
316 struct input_absinfo* absinfo = &device->info.absinfo[i];
317 if (i == ABS_MT_SLOT) {
318 int rc;
319 rc = MTB_Init(device, absinfo->minimum, absinfo->maximum,
320 absinfo->value);
321 if (rc != Success)
322 return rc;
323 } else if (IS_ABS_MT(i)) {
324 evstate->mt_axes[MT_CODE(i)] = absinfo;
325 }
326 }
327 }
Dennis Kempina9963ba2012-06-08 10:32:23 -0700328 return Success;
329}
330
331void
332Event_Free(EvdevPtr device)
333{
334 MT_Free(device);
335}
336
337void
338Event_Open(EvdevPtr device)
339{
340 /* Select monotonic input event timestamps, if supported by kernel */
341 device->info.is_monotonic = (EvdevEnableMonotonic(device) == Success);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700342 LOG_DEBUG(device, "Using %s input event time stamps\n",
343 device->info.is_monotonic ? "monotonic" : "realtime");
Daniel Kurtz000bdff2012-09-17 15:45:29 +0800344
345 /* Synchronize all MT slots with kernel evdev driver */
346 Event_Sync_State(device);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700347}
348
349static void
350Event_Get_Time(struct timeval *t, bool use_monotonic) {
351 struct timespec now;
352 clockid_t clockid = (use_monotonic) ? CLOCK_MONOTONIC : CLOCK_REALTIME;
353
354 clock_gettime(clockid, &now);
355 t->tv_sec = now.tv_sec;
356 t->tv_usec = now.tv_nsec / 1000;
357}
358
359/**
360 * Synchronize the current state with kernel evdev driver. For cmt, there are
361 * only four components required to be synced: current touch count, the MT
362 * slots information, current slot id and physical button states. However, as
363 * pressure readings are missing in ABS_MT_PRESSURE field of MT slots for
364 * semi_mt touchpad device (e.g. Cr48), we also need need to extract it with
365 * extra EVIOCGABS query.
366 */
367void
368Event_Sync_State(EvdevPtr device)
369{
370 int i;
371
372 Event_Get_Time(&device->before_sync_time, device->info.is_monotonic);
373
374 EvdevProbeKeyState(device);
375
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800376 /* Get current pressure information for single-pressure device */
377 if (EvdevIsSinglePressureDevice(device) == Success) {
378 struct input_event ev;
379 ev.code = ABS_PRESSURE;
380 ev.value = device->info.absinfo[ABS_PRESSURE].value;
381 Event_Abs_Update_Pressure(device, &ev);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700382 }
383
384 /* TODO(cywang): Sync all ABS_ states for completeness */
385
386 /* Get current MT information for each slot */
387 for (i = _ABS_MT_FIRST; i <= _ABS_MT_LAST; i++) {
388 MTSlotInfo req;
389
390 if (!TestBit(i, device->info.abs_bitmask))
391 continue;
392 /*
393 * TODO(cywang): Scale the size of slots in MTSlotInfo based on the
394 * evstate->slot_count.
395 */
396
397 req.code = i;
398 if (EvdevProbeMTSlot(device, &req) != Success) {
399 continue;
400 }
401 MT_Slot_Sync(device, &req);
402 }
403
Chung-yih Wang8cb858a2014-04-21 16:38:23 +0800404 /* Get current slot id for multi-touch devices*/
405 if (TestBit(ABS_MT_SLOT, device->info.abs_bitmask) &&
406 (EvdevProbeAbsinfo(device, ABS_MT_SLOT) == Success)) {
Dennis Kempina9963ba2012-06-08 10:32:23 -0700407 MT_Slot_Set(device, device->info.absinfo[ABS_MT_SLOT].value);
Chung-yih Wang8cb858a2014-04-21 16:38:23 +0800408 }
Dennis Kempina9963ba2012-06-08 10:32:23 -0700409
410 Event_Get_Time(&device->after_sync_time, device->info.is_monotonic);
411
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700412 /* Initialize EV_REL event state */
413 Event_Clear_Ev_Rel_State(device);
414
Chung-yih Wangb709c082012-07-20 16:26:15 +0800415 LOG_WARNING(device, "Event_Sync_State: before %ld.%ld after %ld.%ld\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500416 (long)device->before_sync_time.tv_sec,
417 (long)device->before_sync_time.tv_usec,
418 (long)device->after_sync_time.tv_sec,
419 (long)device->after_sync_time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700420}
421
422static void
423Event_Print(EvdevPtr device, struct input_event* ev)
424{
425 switch (ev->type) {
426 case EV_SYN:
427 switch (ev->code) {
428 case SYN_REPORT:
Dennis Kempinde0712f2012-06-11 15:13:50 -0700429 LOG_DEBUG(device, "@ %ld.%06ld ---------- SYN_REPORT -------\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500430 (long)ev->time.tv_sec, (long)ev->time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700431 return;
432 case SYN_MT_REPORT:
Dennis Kempinde0712f2012-06-11 15:13:50 -0700433 LOG_DEBUG(device, "@ %ld.%06ld ........ SYN_MT_REPORT ......\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500434 (long)ev->time.tv_sec, (long)ev->time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700435 return;
436 case SYN_DROPPED:
Dennis Kempin85a14442012-06-25 11:24:59 -0700437 LOG_WARNING(device, "@ %ld.%06ld ++++++++ SYN_DROPPED ++++++++\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500438 (long)ev->time.tv_sec, (long)ev->time.tv_usec);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700439 return;
440 default:
Dennis Kempin85a14442012-06-25 11:24:59 -0700441 LOG_WARNING(device, "@ %ld.%06ld ?????? SYN_UNKNOWN (%d) ?????\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500442 (long)ev->time.tv_sec, (long)ev->time.tv_usec, ev->code);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700443 return;
444 }
445 break;
446 case EV_ABS:
447 if (ev->code == ABS_MT_SLOT) {
Dennis Kempinde0712f2012-06-11 15:13:50 -0700448 LOG_DEBUG(device, "@ %ld.%06ld .......... MT SLOT %d ........\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500449 (long)ev->time.tv_sec, (long)ev->time.tv_usec, ev->value);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700450 return;
451 }
452 break;
453 default:
454 break;
455 }
456
Dennis Kempinde0712f2012-06-11 15:13:50 -0700457 LOG_DEBUG(device, "@ %ld.%06ld %s[%d] (%s) = %d\n",
Mike Frysingerda3e4732012-12-23 13:00:24 -0500458 (long)ev->time.tv_sec, (long)ev->time.tv_usec,
459 Event_Type_To_String(ev->type), ev->code,
460 Event_To_String(ev->type, ev->code), ev->value);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700461}
462
463/**
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700464 * Process Input Events. It returns TRUE if SYN_DROPPED detected.
Dennis Kempina9963ba2012-06-08 10:32:23 -0700465 */
466bool
467Event_Process(EvdevPtr device, struct input_event* ev)
468{
Dennis Kempina9963ba2012-06-08 10:32:23 -0700469 Event_Print(device, ev);
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700470 if (Event_Is_Valid(ev)) {
471 if (!(ev->type == EV_SYN && ev->code == SYN_REPORT))
472 device->got_valid_event = 1;
473 } else {
474 return false;
475 }
Dennis Kempina9963ba2012-06-08 10:32:23 -0700476
477 switch (ev->type) {
478 case EV_SYN:
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700479 return Event_Syn(device, ev);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700480
481 case EV_KEY:
482 Event_Key(device, ev);
483 break;
484
485 case EV_ABS:
486 Event_Abs(device, ev);
487 break;
488
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700489 case EV_REL:
490 Event_Rel(device, ev);
491 break;
492
Dennis Kempina9963ba2012-06-08 10:32:23 -0700493 default:
494 break;
495 }
496 return false;
497}
498
499/**
500 * Dump the log of input events to disk
501 */
502void
Dennis Kempin328acb82014-04-10 13:58:25 -0700503Event_Dump_Debug_Log(void* vinfo) {
504 Event_Dump_Debug_Log_To(vinfo, "/var/log/xorg/cmt_input_events.dat");
505}
506
507void
508Event_Dump_Debug_Log_To(void* vinfo, const char* filename)
Dennis Kempina9963ba2012-06-08 10:32:23 -0700509{
510 EvdevPtr device = (EvdevPtr) vinfo;
511 size_t i;
Dennis Kempin69c91ed2013-02-12 14:53:10 -0800512 int ret;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700513 EventStatePtr evstate = device->evstate;
514
Dennis Kempin328acb82014-04-10 13:58:25 -0700515 FILE* fp = fopen(filename, "wb");
Dennis Kempina9963ba2012-06-08 10:32:23 -0700516 if (!fp) {
517 LOG_ERROR(device, "fopen() failed for debug log");
518 return;
519 }
Dennis Kempin69c91ed2013-02-12 14:53:10 -0800520
521 ret = EvdevWriteInfoToFile(fp, &device->info);
522 if (ret <= 0) {
523 LOG_ERROR(device, "EvdevWriteInfoToFile failed. Log without info.");
524 }
525
Dennis Kempina9963ba2012-06-08 10:32:23 -0700526 for (i = 0; i < DEBUG_BUF_SIZE; i++) {
Dennis Kempina9963ba2012-06-08 10:32:23 -0700527 struct input_event *ev =
528 &evstate->debug_buf[(evstate->debug_buf_tail + i) % DEBUG_BUF_SIZE];
529 if (ev->time.tv_sec == 0 && ev->time.tv_usec == 0)
530 continue;
Dennis Kempin69c91ed2013-02-12 14:53:10 -0800531 ret = EvdevWriteEventToFile(fp, ev);
532 if (ret <= 0) {
533 LOG_ERROR(device, "EvdevWriteEventToFile failed. Log is short.");
Dennis Kempina9963ba2012-06-08 10:32:23 -0700534 break;
535 }
536 }
537 fclose(fp);
538}
539
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700540/**
Dennis Kempind6ae1672014-01-08 11:41:33 -0800541 * Clear Debug Buffer
542 */
543void
544Event_Clear_Debug_Log(void* vinfo)
545{
546 EvdevPtr device = (EvdevPtr) vinfo;
547 EventStatePtr evstate = device->evstate;
548
549 memset(evstate->debug_buf, 0, sizeof(evstate->debug_buf));
550 evstate->debug_buf_tail = 0;
551}
552
553/**
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700554 * Clear EV_REL event state. This function should be called after a EV_SYN
555 * event is processed because EV_REL event state is not accumulative.
556 */
Dennis Kempina9963ba2012-06-08 10:32:23 -0700557static void
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700558Event_Clear_Ev_Rel_State(EvdevPtr device)
559{
560 EventStatePtr evstate;
561
562 evstate = device->evstate;
563 evstate->rel_x = 0;
564 evstate->rel_y = 0;
565 evstate->rel_wheel = 0;
566 evstate->rel_hwheel = 0;
567}
568
569/**
570 * Process EV_SYN events. It returns TRUE if SYN_DROPPED detected.
571 */
572static bool
Dennis Kempina9963ba2012-06-08 10:32:23 -0700573Event_Syn(EvdevPtr device, struct input_event* ev)
574{
575 switch (ev->code) {
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700576 case SYN_DROPPED:
577 /*
578 * Technically we don't need to call Event_Clear_Ev_Rel_State() here
579 * because when SYN_DROPPED is detected, Event_Sync_State() will be
580 * called and Event_Sync_State() will call Event_Clear_Ev_Rel_State()
581 * to re-initialize EV_REL event state.
582 */
583 return true;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700584 case SYN_REPORT:
585 Event_Syn_Report(device, ev);
586 break;
587 case SYN_MT_REPORT:
588 Event_Syn_MT_Report(device, ev);
589 break;
590 }
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700591 return false;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700592}
593
594static void
595Event_Syn_Report(EvdevPtr device, struct input_event* ev)
596{
597 EventStatePtr evstate = device->evstate;
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700598 if (device->got_valid_event)
599 device->syn_report(device->syn_report_udata, evstate, &ev->time);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700600
601 MT_Print_Slots(device);
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700602
603 Event_Clear_Ev_Rel_State(device);
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700604 device->got_valid_event = 0;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700605}
606
607static void
608Event_Syn_MT_Report(EvdevPtr device, struct input_event* ev)
609{
610 /* TODO(djkurtz): Handle MT-A */
611}
612
613static void
614Event_Key(EvdevPtr device, struct input_event* ev)
615{
616 AssignBit(device->key_state_bitmask, ev->code, ev->value);
617}
618
619static void
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800620Event_Abs_Update_Pressure(EvdevPtr device, struct input_event* ev)
Dennis Kempina9963ba2012-06-08 10:32:23 -0700621{
622 /*
623 * Update all active slots with the same ABS_PRESSURE value if it is a
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800624 * single-pressure device.
Dennis Kempina9963ba2012-06-08 10:32:23 -0700625 */
626 EventStatePtr evstate = device->evstate;
627
628 for (int i = 0; i < evstate->slot_count; i++) {
629 MtSlotPtr slot = &evstate->slots[i];
630 slot->pressure = ev->value;
631 }
632}
633
634static void
635Event_Abs(EvdevPtr device, struct input_event* ev)
636{
637 if (ev->code == ABS_MT_SLOT)
638 MT_Slot_Set(device, ev->value);
639 else if (IS_ABS_MT(ev->code))
640 Event_Abs_MT(device, ev);
Chung-yih Wang8b2fa0a2012-07-23 16:49:10 +0800641 else if ((ev->code == ABS_PRESSURE) && EvdevIsSinglePressureDevice(device))
642 Event_Abs_Update_Pressure(device, ev);
Dennis Kempina9963ba2012-06-08 10:32:23 -0700643}
644
645static void
646Event_Abs_MT(EvdevPtr device, struct input_event* ev)
647{
648 EventStatePtr evstate = device->evstate;
649 struct input_absinfo* axis = evstate->mt_axes[MT_CODE(ev->code)];
650 MtSlotPtr slot = evstate->slot_current;
651
652 if (axis == NULL) {
Dennis Kempin85a14442012-06-25 11:24:59 -0700653 LOG_WARNING(device, "ABS_MT[%02x] was not reported by this device\n",
Dennis Kempina9963ba2012-06-08 10:32:23 -0700654 ev->code);
655 return;
656 }
657
658 /* Warn about out of range data, but don't ignore */
659 if ((ev->code != ABS_MT_TRACKING_ID)
660 && ((ev->value < axis->minimum)
661 || (ev->value > axis->maximum))) {
662 LOG_WARNING(device, "ABS_MT[%02x] = %d : value out of range [%d .. %d]\n",
663 ev->code, ev->value, axis->minimum, axis->maximum);
Chung-yih Wangf573f892013-01-04 14:29:02 +0800664 /* Update the effective boundary as we already print out the warning */
665 if (ev->value < axis->minimum)
666 axis->minimum = ev->value;
667 else if (ev->value > axis->maximum)
668 axis->maximum = ev->value;
Dennis Kempina9963ba2012-06-08 10:32:23 -0700669 }
670
671 if (slot == NULL) {
Dennis Kempin85a14442012-06-25 11:24:59 -0700672 LOG_WARNING(device, "MT slot not set. Ignoring ABS_MT event\n");
Dennis Kempina9963ba2012-06-08 10:32:23 -0700673 return;
674 }
675
676 MT_Slot_Value_Set(slot, ev->code, ev->value);
677}
Che-Liang Chiouf616b122012-10-24 17:08:51 -0700678
679static void
680Event_Rel(EvdevPtr device, struct input_event* ev)
681{
682 EventStatePtr evstate = device->evstate;
683
684 switch (ev->code) {
685 case REL_X:
686 evstate->rel_x = ev->value;
687 break;
688 case REL_Y:
689 evstate->rel_y = ev->value;
690 break;
691 case REL_WHEEL:
692 evstate->rel_wheel = ev->value;
693 break;
694 case REL_HWHEEL:
695 evstate->rel_hwheel = ev->value;
696 break;
697 }
698}
Andrew de los Reyes15ecd0c2014-06-26 09:38:32 -0700699
700static int Event_Is_Valid(struct input_event* ev)
701{
702 /* Key repeats are invalid. They're handled by X anyway */
703 if (ev->type == EV_KEY &&
704 ev->value == 2)
705 return 0;
706 return 1;
707}