summaryrefslogtreecommitdiff
path: root/tools/utouch-frame-test-xi2.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/utouch-frame-test-xi2.c')
-rw-r--r--tools/utouch-frame-test-xi2.c238
1 files changed, 238 insertions, 0 deletions
diff --git a/tools/utouch-frame-test-xi2.c b/tools/utouch-frame-test-xi2.c
new file mode 100644
index 0000000..df68c1a
--- /dev/null
+++ b/tools/utouch-frame-test-xi2.c
@@ -0,0 +1,238 @@
1/*****************************************************************************
2 *
3 * utouch-frame - Touch Frame Library
4 *
5 * Copyright (C) 2010-2011 Canonical Ltd.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * Authors:
21 * Henrik Rydberg <rydberg@bitmath.org>
22 *
23 ****************************************************************************/
24
25#define MTDEV_NO_LEGACY_API
26
27#include <utouch/frame-xi2.h>
28#include <string.h>
29#include <stdio.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <stdlib.h>
33
34static int opcode;
35
36struct frame_test {
37 Display *display;
38 Window root, win;
39 XIDeviceInfo *info, *dev;
40 utouch_frame_handle fh;
41};
42
43static int init_xi2(struct frame_test *test, int id)
44{
45 Display *dpy;
46 int ndevices, i;
47 int event, error;
48
49 dpy = XOpenDisplay(NULL);
50 if (!dpy)
51 return -1;
52
53 if (!XQueryExtension(dpy, "XInputExtension", &opcode, &event, &error))
54 return -1;
55
56 test->display = dpy;
57 test->info = XIQueryDevice(dpy, XIAllDevices, &ndevices);
58 test->dev = 0;
59 for (i = 0; i < ndevices; i++)
60 if (test->info[i].deviceid == id)
61 test->dev = &test->info[i];
62 if (!test->dev)
63 return -1;
64
65 test->root = DefaultRootWindow(dpy);
66 test->win = XCreateSimpleWindow(dpy, test->root,
67 0, 0, 400, 400, 0,
68 BlackPixel(dpy, 0),
69 WhitePixel(dpy, 0));
70
71 XMapWindow(test->display, test->win);
72 XFlush(test->display);
73
74 return 0;
75}
76
77static int init_frame(struct frame_test *test)
78{
79 test->fh = utouch_frame_new_engine(100, 32, 100);
80 if (!test->fh)
81 return -1;
82 return utouch_frame_init_xi2(test->fh, test->display, test->dev);
83}
84
85static void destroy_all(struct frame_test *test)
86{
87 utouch_frame_delete_engine(test->fh);
88 XIFreeDeviceInfo(test->info);
89 XDestroyWindow(test->display, test->win);
90 XCloseDisplay(test->display);
91 memset(test, 0, sizeof(*test));
92}
93
94static void report_device_caps(struct frame_test *test)
95{
96 struct utouch_surface *s = utouch_frame_get_surface(test->fh);
97 fprintf(stderr, "device props:\n");
98 if (s->needs_pointer)
99 fprintf(stderr, "\tpointer\n");
100 if (s->is_direct)
101 fprintf(stderr, "\tdirect\n");
102 if (s->is_buttonpad)
103 fprintf(stderr, "\tbuttonpad\n");
104 if (s->is_semi_mt)
105 fprintf(stderr, "\tsemi_mt\n");
106 fprintf(stderr, "device mt events:\n");
107 if (s->use_touch_major)
108 fprintf(stderr, "\ttouch_major\n");
109 if (s->use_touch_minor)
110 fprintf(stderr, "\ttouch_minor\n");
111 if (s->use_width_major)
112 fprintf(stderr, "\twidth_major\n");
113 if (s->use_width_minor)
114 fprintf(stderr, "\twidth_minor\n");
115 if (s->use_orientation)
116 fprintf(stderr, "\torientation\n");
117 if (s->use_pressure)
118 fprintf(stderr, "\tpressure\n");
119 if (s->use_distance)
120 fprintf(stderr, "\tdistance\n");
121}
122
123static void report_frame(const struct utouch_frame *frame,
124 const XIDeviceEvent *ev)
125{
126 int i;
127
128 for (i = 0; i < frame->num_active; i++) {
129 const struct utouch_contact *t = frame->active[i];
130
131 fprintf(stderr, "touch %d\n", i);
132 fprintf(stderr, " slot: %d\n", t->slot);
133 fprintf(stderr, " id: %d\n", t->id);
134 fprintf(stderr, " tool_type: %d\n", t->tool_type);
135 fprintf(stderr, " x: %f\n", t->x);
136 fprintf(stderr, " y: %f\n", t->y);
137 fprintf(stderr, " touch_major: %f\n", t->touch_major);
138 fprintf(stderr, " touch_minor: %f\n", t->touch_minor);
139 fprintf(stderr, " width_major: %f\n", t->width_major);
140 fprintf(stderr, " width_minor: %f\n", t->width_minor);
141 fprintf(stderr, " angle: %f\n", t->orientation);
142 fprintf(stderr, " pressure: %f\n", t->pressure);
143 fprintf(stderr, " distance: %f\n", t->distance);
144 }
145
146 fprintf(stderr, "sync %d %ld %d %d %d %f %f\n",
147 frame->num_active,
148 frame->time,
149 frame->sequence_id,
150 frame->revision,
151 frame->slot_revision,
152 ev->root_x,
153 ev->root_y);
154}
155
156static void handle_event(struct frame_test *test, XEvent *ev)
157{
158 XGenericEventCookie *gev = &ev->xcookie;
159 const struct utouch_frame *frame;
160 XConfigureEvent *cev;
161 XIDeviceEvent *dev;
162
163 switch(ev->type) {
164 case ConfigureNotify:
165 cev = (XConfigureEvent *)ev;
166 if (cev->window == XDefaultRootWindow(cev->display))
167 utouch_frame_configure_xi2(test->fh, cev);
168 break;
169 case GenericEvent:
170 if (!XGetEventData(test->display, gev))
171 break;
172 dev = gev->data;
173 /* null for some requests, quite odd */
174 dev->display = ev->xany.display;
175 if (gev->type == GenericEvent && gev->extension == opcode) {
176 frame = utouch_frame_pump_xi2(test->fh, dev);
177 if (frame)
178 report_frame(frame, dev);
179 }
180 XFreeEventData(test->display, gev);
181 break;
182 }
183}
184
185static void loop_device(struct frame_test *test)
186{
187 XIEventMask mask;
188
189 mask.deviceid = XIAllDevices;
190 mask.mask_len = XIMaskLen(XI_TouchMotion);
191 mask.mask = calloc(mask.mask_len, sizeof(char));
192
193 XISetMask(mask.mask, XI_TouchBegin);
194 XISetMask(mask.mask, XI_TouchMotion);
195 XISetMask(mask.mask, XI_TouchEnd);
196 XISetMask(mask.mask, XI_PropertyEvent);
197 XISelectEvents(test->display, test->win, &mask, 1);
198
199 XSelectInput(test->display, test->root, StructureNotifyMask);
200
201 while (1) {
202 XEvent ev;
203 XNextEvent(test->display, &ev);
204 handle_event(test, &ev);
205 }
206}
207
208int main(int argc, char *argv[])
209{
210 struct frame_test test;
211 int id;
212
213 if (argc < 2) {
214 fprintf(stderr, "Usage: %s <xinput_device_number>\n", argv[0]);
215 return -1;
216 }
217 id = atoi(argv[1]);
218
219 if (init_xi2(&test, id)) {
220 fprintf(stderr, "error: could not describe device\n");
221 return -1;
222 }
223 if (!utouch_frame_is_supported_xi2(test.display, test.dev)) {
224 fprintf(stderr, "error: unsupported device\n");
225 return -1;
226 }
227 if (init_frame(&test)) {
228 fprintf(stderr, "error: could not init frame\n");
229 return -1;
230 }
231
232 report_device_caps(&test);
233
234 loop_device(&test);
235
236 destroy_all(&test);
237 return 0;
238}