1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>
#include <poll.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <X11/Xlib.h>
#include <X11/extensions/XInput.h>
#include <X11/extensions/XInput2.h>
struct multitouch {
Display *dpy;
int screen_no;
Screen* screen;
Window root;
Window win;
GC gc;
Visual *visual;
int xi_opcode;
Pixmap pixmap;
int width;
int height;
cairo_t *cr;
cairo_t *cr_win;
cairo_surface_t *surface;
cairo_surface_t *surface_win;
};
static void expose(struct multitouch *mt, int x1, int y1, int x2, int y2);
static int error(const char *fmt, ...)
{
va_list args;
fprintf(stderr, "E: ");
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
return EXIT_FAILURE;
}
static void msg(const char *fmt, ...)
{
va_list args;
printf("M: ");
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
static int running = 1;
static void sighandler(int signal)
{
running = 0;
error("signal received, shutting down\n");
}
static Window init_window(struct multitouch *mt)
{
Window win;
XEvent event;
XIEventMask evmask;
unsigned char mask[XIMaskLen(XI_LASTEVENT)];
win = XCreateSimpleWindow(mt->dpy, mt->root, 0, 0, mt->width, mt->height,
0, 0, WhitePixel(mt->dpy, mt->screen_no));
XSelectInput(mt->dpy, win, ExposureMask);
XMapWindow(mt->dpy, win);
XMaskEvent(mt->dpy, ExposureMask, &event);
evmask.mask = mask;
evmask.mask_len = sizeof(mask);
memset(mask, 0, sizeof(mask));
evmask.deviceid = XIAllMasterDevices;
XISetMask(mask, XI_TouchBegin);
XISetMask(mask, XI_TouchUpdate);
XISetMask(mask, XI_TouchEnd);
XISelectEvents(mt->dpy, win, &evmask, 1);
XSync(mt->dpy, False);
return win;
}
static Pixmap init_pixmap(struct multitouch *mt)
{
Pixmap p;
p = XCreatePixmap(mt->dpy, mt->win, mt->width, mt->height,
DefaultDepth(mt->dpy, mt->screen_no));
return p;
}
static GC init_gc(struct multitouch *mt)
{
GC gc;
gc = XCreateGC(mt->dpy, mt->win, 0, NULL);
return gc;
}
static int init_x11(struct multitouch *mt, int width, int height)
{
Display *dpy;
int major = 2, minor = 2;
int xi_opcode, xi_error, xi_event;
dpy = XOpenDisplay(NULL);
if (!dpy)
return error("Invalid DISPLAY.\n");
if (!XQueryExtension(dpy, INAME, &xi_opcode, &xi_event, &xi_error))
return error("No X Input extension\n");
if (XIQueryVersion(dpy, &major, &minor) != Success ||
major * 10 + minor < 22)
return error("Need XI 2.2\n");
mt->dpy = dpy;
mt->screen_no = DefaultScreen(dpy);
mt->screen = DefaultScreenOfDisplay(dpy);
mt->root = DefaultRootWindow(dpy);
mt->visual = DefaultVisual(dpy, mt->screen_no);
mt->xi_opcode = xi_opcode;
mt->width = width;
mt->height = height;
mt->win = init_window(mt);
mt->pixmap = init_pixmap(mt);
mt->gc = init_gc(mt);
if (!mt->win)
return error("Failed to create window.\n");
XFlush(mt->dpy);
return EXIT_SUCCESS;
}
static void teardown(struct multitouch *mt)
{
if (mt->win)
XUnmapWindow(mt->dpy, mt->win);
XCloseDisplay(mt->dpy);
}
static void print_event(struct multitouch *mt, XIDeviceEvent* event)
{
const char *type;
switch(event->evtype)
{
case XI_TouchBegin: type = "TouchBegin"; break;
case XI_TouchUpdate: type = "TouchUpdate"; break;
case XI_TouchEnd: type = "TouchEnd"; break;
}
msg("Event: %s (%d)\n", type, event->deviceid);
msg("\t%.2f/%.2f\n", event->root_x, event->root_y);
msg("\ttouchid: %d\n", event->detail);
}
static int init_cairo(struct multitouch *mt)
{
cairo_surface_t *surface;
cairo_t *cr;
/* frontbuffer */
surface = cairo_xlib_surface_create(mt->dpy, mt->win,
mt->visual, mt->width, mt->height);
if (!surface)
return error("Failed to create cairo surface\n");
mt->surface_win = surface;
cr = cairo_create(surface);
if (!cr)
return error("Failed to create cairo context\n");
mt->cr_win = cr;
/* backbuffer */
surface = cairo_surface_create_similar(surface,
CAIRO_CONTENT_COLOR_ALPHA,
mt->width, mt->height);
if (!surface)
return error("Failed to create cairo surface\n");
mt->surface = surface;
cr = cairo_create(surface);
if (!cr)
return error("Failed to create cairo context\n");
cairo_set_line_width(cr, 1);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_rectangle(cr, 0, 0, mt->width, mt->height);
cairo_fill(cr);
cairo_move_to(cr, 10, 10);
cairo_line_to(cr, 100, 100);
cairo_stroke(cr);
mt->cr = cr;
expose(mt, 0, 0, mt->width, mt->height);
return EXIT_SUCCESS;
}
static void expose(struct multitouch *mt, int x1, int y1, int x2, int y2)
{
cairo_set_source_surface(mt->cr_win, mt->surface, 0, 0);
cairo_paint(mt->cr_win);
}
static int main_loop(struct multitouch *mt)
{
struct pollfd fd;
fd.fd = ConnectionNumber(mt->dpy);
fd.events = POLLIN;
while (running)
{
if (poll(&fd, 1, 500) <= 0)
continue;
while (XPending(mt->dpy)) {
XEvent ev;
XGenericEventCookie *cookie = &ev.xcookie;
XNextEvent(mt->dpy, &ev);
if (ev.type == Expose) {
expose(mt, ev.xexpose.x, ev.xexpose.y,
ev.xexpose.x + ev.xexpose.width,
ev.xexpose.y + ev.xexpose.height);
} else if (ev.type == GenericEvent &&
XGetEventData(mt->dpy, cookie) &&
cookie->type == GenericEvent &&
cookie->extension == mt->xi_opcode)
{
print_event(mt, cookie->data);
}
XFreeEventData(mt->dpy, cookie);
}
}
return EXIT_SUCCESS;
}
int main(int argc, char **argv)
{
int rc;
struct multitouch mt = { 0 };
rc = init_x11(&mt, 800, 600);
if (rc != EXIT_SUCCESS)
return rc;
rc = init_cairo(&mt);
if (rc != EXIT_SUCCESS)
return rc;
signal(SIGINT, sighandler);
main_loop(&mt);
teardown(&mt);
return EXIT_SUCCESS;
}
|