1 /*****************************************************************************
2 *
3 * mtdev - Multitouch Protocol Translation Library (MIT license)
4 *
5 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
6 * Copyright (C) 2010 Canonical Ltd.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 ****************************************************************************/
28
29 #include <mtdev.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <stdint.h>
34
35 #ifndef input_event_sec
36 #define input_event_sec time.tv_sec
37 #define input_event_usec time.tv_usec
38 #endif
39
40 /* year-proof millisecond event time */
41 typedef uint64_t mstime_t;
42
use_event(const struct input_event * ev)43 static int use_event(const struct input_event *ev)
44 {
45 #if 0
46 return ev->type == EV_ABS && mtdev_is_absmt(ev->code);
47 #else
48 return 1;
49 #endif
50 }
51
print_event(const struct input_event * ev)52 static void print_event(const struct input_event *ev)
53 {
54 static const mstime_t ms = 1000;
55 static int slot;
56 mstime_t evtime = ev->input_event_usec / ms + ev->input_event_sec * ms;
57 if (ev->type == EV_ABS && ev->code == ABS_MT_SLOT)
58 slot = ev->value;
59 fprintf(stderr, "%012llx %02d %01d %04x %d\n",
60 evtime, slot, ev->type, ev->code, ev->value);
61 }
62
63 #define CHECK(dev, name) \
64 if (mtdev_has_mt_event(dev, name)) \
65 fprintf(stderr, " %s\n", #name)
66
show_props(const struct mtdev * dev)67 static void show_props(const struct mtdev *dev)
68 {
69 fprintf(stderr, "supported mt events:\n");
70 CHECK(dev, ABS_MT_SLOT);
71 CHECK(dev, ABS_MT_TOUCH_MAJOR);
72 CHECK(dev, ABS_MT_TOUCH_MINOR);
73 CHECK(dev, ABS_MT_WIDTH_MAJOR);
74 CHECK(dev, ABS_MT_WIDTH_MINOR);
75 CHECK(dev, ABS_MT_ORIENTATION);
76 CHECK(dev, ABS_MT_POSITION_X);
77 CHECK(dev, ABS_MT_POSITION_Y);
78 CHECK(dev, ABS_MT_TOOL_TYPE);
79 CHECK(dev, ABS_MT_BLOB_ID);
80 CHECK(dev, ABS_MT_TRACKING_ID);
81 CHECK(dev, ABS_MT_PRESSURE);
82 CHECK(dev, ABS_MT_DISTANCE);
83 }
84
loop_device(int fd)85 static void loop_device(int fd)
86 {
87 struct mtdev dev;
88 struct input_event ev;
89 int ret = mtdev_open(&dev, fd);
90 if (ret) {
91 fprintf(stderr, "error: could not open device: %d\n", ret);
92 return;
93 }
94 show_props(&dev);
95 /* while the device has not been inactive for five seconds */
96 while (!mtdev_idle(&dev, fd, 5000)) {
97 /* extract all available processed events */
98 while (mtdev_get(&dev, fd, &ev, 1) > 0) {
99 if (use_event(&ev))
100 print_event(&ev);
101 }
102 }
103 mtdev_close(&dev);
104 }
105
main(int argc,char * argv[])106 int main(int argc, char *argv[])
107 {
108 int fd;
109 if (argc < 2) {
110 fprintf(stderr, "Usage: mtdev <device>\n");
111 return -1;
112 }
113 fd = open(argv[1], O_RDONLY | O_NONBLOCK);
114 if (fd < 0) {
115 fprintf(stderr, "error: could not open device\n");
116 return -1;
117 }
118 if (ioctl(fd, EVIOCGRAB, 1)) {
119 fprintf(stderr, "error: could not grab the device\n");
120 return -1;
121 }
122 loop_device(fd);
123 ioctl(fd, EVIOCGRAB, 0);
124 close(fd);
125 return 0;
126 }
127