summaryrefslogtreecommitdiff
path: root/test/mtdev.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/mtdev.c')
-rw-r--r--test/mtdev.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/test/mtdev.c b/test/mtdev.c
new file mode 100644
index 0000000..995a4e2
--- /dev/null
+++ b/test/mtdev.c
@@ -0,0 +1,95 @@
1/*****************************************************************************
2 *
3 * mtdev - MT device event converter (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-mapping.h>
30#include <stdio.h>
31#include <fcntl.h>
32
33/* year-proof millisecond event time */
34typedef __u64 mstime_t;
35
36static int use_event(const struct input_event *ev)
37{
38#if 0
39 return ev->type == EV_ABS && mtdev_is_absmt(ev->code);
40#else
41 return 1;
42#endif
43}
44
45static void print_event(const struct input_event *ev)
46{
47 static const mstime_t ms = 1000;
48 static int slot;
49 mstime_t evtime = ev->time.tv_usec / ms + ev->time.tv_sec * ms;
50 if (ev->type == EV_ABS && ev->code == ABS_MT_SLOT)
51 slot = ev->value;
52 fprintf(stderr, "%012llx %02d %01d %04x %d\n",
53 evtime, slot, ev->type, ev->code, ev->value);
54}
55
56static void loop_device(int fd)
57{
58 struct mtdev dev;
59 struct input_event ev;
60 int ret = mtdev_open(&dev, fd);
61 if (ret) {
62 fprintf(stderr, "error: could not open device: %d\n", ret);
63 return;
64 }
65 while (mtdev_pull(&dev, fd, 1) > 0) {
66 while (!mtdev_empty(&dev)) {
67 mtdev_get(&dev, &ev);
68 if (use_event(&ev))
69 print_event(&ev);
70 }
71 }
72 mtdev_close(&dev);
73}
74
75int main(int argc, char *argv[])
76{
77 int fd;
78 if (argc < 2) {
79 fprintf(stderr, "Usage: mtdev <device>\n");
80 return -1;
81 }
82 fd = open(argv[1], O_RDONLY);
83 if (fd < 0) {
84 fprintf(stderr, "error: could not open device\n");
85 return -1;
86 }
87 if (ioctl(fd, EVIOCGRAB, 1)) {
88 fprintf(stderr, "error: could not grab the device\n");
89 return -1;
90 }
91 loop_device(fd);
92 ioctl(fd, EVIOCGRAB, 0);
93 close(fd);
94 return 0;
95}