-
Notifications
You must be signed in to change notification settings - Fork 0
/
geistest.cpp
456 lines (398 loc) · 10.9 KB
/
geistest.cpp
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// g++ geistest.cpp -o geistest.xps0 -fpermissive -lgeis -lxcb
/**
* geistest.c Demo code for programming against the geis interface.
*
* Copyright 2010 Canonical Ltd.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <errno.h>
#include <geis/geis.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <unistd.h>
#include <xcb/xcb.h>
GeisSize g_smw = 0;
const char* s_gestures[] = {
GEIS_GESTURE_TYPE_DRAG2, GEIS_GESTURE_TYPE_DRAG3,
GEIS_GESTURE_TYPE_PINCH2, GEIS_GESTURE_TYPE_PINCH3,
GEIS_GESTURE_TYPE_ROTATE2, GEIS_GESTURE_TYPE_ROTATE3,
GEIS_GESTURE_TYPE_TAP2, GEIS_GESTURE_TYPE_TAP3, GEIS_GESTURE_TYPE_TAP4,
GEIS_GESTURE_TYPE_TOUCH3, GEIS_GESTURE_TYPE_TOUCH4,
NULL
};
GeisSize g_device_count = 0;
GeisInputDeviceId *g_devices = GEIS_ALL_INPUT_DEVICES;
static void
print_attr(GeisGestureAttr *attr)
{
fprintf(stdout, "\tattr \"%s\" = ", attr->name);
switch (attr->type)
{
case GEIS_ATTR_TYPE_BOOLEAN:
fprintf(stdout, "%s\n", attr->boolean_val ? "true" : "false");
break;
case GEIS_ATTR_TYPE_FLOAT:
fprintf(stdout, "%f\n", attr->float_val);
break;
case GEIS_ATTR_TYPE_INTEGER:
fprintf(stdout, "%d\n", attr->integer_val);
break;
case GEIS_ATTR_TYPE_STRING:
fprintf(stdout, "\"%s\"\n", attr->string_val);
break;
default:
fprintf(stdout, "<unknown>\n");
break;
}
}
static void
input_device_added(void *cookie, GeisInputDeviceId device_id, void *attrs)
{
GeisGestureAttr *a;
fprintf(stdout, "Device %d added\n", device_id);
for (a = attrs; a->name; ++a)
{
print_attr(a);
}
}
static void
input_device_changed(void *cookie, GeisInputDeviceId device_id, void *attrs)
{
}
static void
input_device_removed(void *cookie, GeisInputDeviceId device_id, void *attrs)
{
}
static void
gesture_added(void *cookie,
GeisGestureType gesture_type,
GeisGestureId gesture_id,
GeisSize attr_count,
GeisGestureAttr *attrs)
{
int i = 0;
fprintf(stdout, "Gesture type %d added\n", gesture_type);
for (i = 0; i < attr_count; ++i)
print_attr(&attrs[i]);
}
static void
gesture_removed(void *cookie,
GeisGestureType gesture_type,
GeisGestureId gesture_id,
GeisSize attr_count,
GeisGestureAttr *attrs)
{
int i = 0;
fprintf(stdout, "Gesture type %d removed\n", gesture_type);
for (i = 0; i < attr_count; ++i)
print_attr(&attrs[i]);
}
static void
gesture_start(void *cookie,
GeisGestureType gesture_type,
GeisGestureId gesture_id,
GeisSize attr_count,
GeisGestureAttr *attrs)
{
int i = 0;
fprintf(stdout, "Gesture id %d type %d started\n", gesture_id, gesture_type);
for (i = 0; i < attr_count; ++i)
print_attr(&attrs[i]);
}
static void
gesture_update(void *cookie,
GeisGestureType gesture_type,
GeisGestureId gesture_id,
GeisSize attr_count,
GeisGestureAttr *attrs)
{
int i = 0;
fprintf(stdout, "Gesture id %d type %d updated\n", gesture_id, gesture_type);
for (i = 0; i < attr_count; ++i)
print_attr(&attrs[i]);
}
static void
gesture_finish(void *cookie,
GeisGestureType gesture_type,
GeisGestureId gesture_id,
GeisSize attr_count,
GeisGestureAttr *attrs)
{
int i = 0;
fprintf(stdout, "Gesture id %d type %d finished\n", gesture_id, gesture_type);
for (i = 0; i < attr_count; ++i)
print_attr(&attrs[i]);
}
GeisInputFuncs input_funcs = {
input_device_added,
input_device_changed,
input_device_removed
};
GeisGestureFuncs gesture_funcs = {
gesture_added,
gesture_removed,
gesture_start,
gesture_update,
gesture_finish
};
static void
_add_device_id(GeisInteger id)
{
GeisInputDeviceId *d = realloc(g_devices, g_device_count + 2);
if (!d)
{
fprintf(stderr, "error allocating device list.\n");
exit(1);
}
g_devices = d;
g_devices[g_device_count] = id;
g_devices[g_device_count+1] = 0;
++g_device_count;
}
int
parse_opts(int argc, char* argv[], uint32_t *window_id)
{
int opt;
while ((opt = getopt(argc, argv, "w:d:")) != -1)
{
switch (opt)
{
case 'w':
*window_id = strtol(optarg, NULL, 0);
break;
case 'd':
{
GeisInteger id = strtol(optarg, NULL, 0);
if (0 == id)
{
fprintf(stderr, "invalid device id '%s'\n'", optarg);
return 0;
}
_add_device_id(id);
}
break;
default:
return 0;
}
}
return 1;
}
static GeisInstance
subscribe_window(uint32_t window_id)
{
GeisStatus status = GEIS_UNKNOWN_ERROR;
GeisXcbWinInfo xcb_win_info = {
.display_name = NULL,
.screenp = NULL,
.window_id = window_id
};
GeisWinInfo win_info = {
GEIS_XCB_FULL_WINDOW,
&xcb_win_info
};
GeisInstance instance;
status = geis_init(&win_info, &instance);
if (status != GEIS_STATUS_SUCCESS)
{
fprintf(stderr, "error in geis_init\n");
return NULL;
}
status = geis_input_devices(instance, &input_funcs, NULL);
if (status != GEIS_STATUS_SUCCESS)
{
fprintf(stderr, "error subscribing to input devices\n");
return NULL;
}
status = geis_subscribe(instance,
g_devices,
s_gestures,
&gesture_funcs,
NULL);
if (status != GEIS_STATUS_SUCCESS)
{
fprintf(stderr, "error subscribing to gestures\n");
return NULL;
}
return instance;
}
static int num_children;
static xcb_window_t *children;
static xcb_query_tree_cookie_t tree_cookie;
static xcb_query_tree_reply_t *tree_reply;
static size_t
subscribe_windows_preorder(xcb_connection_t *xcb,
xcb_window_t window,
size_t instance_table_size,
GeisInstance **instance_table)
{
++instance_table_size;
GeisInstance *new_instance_table;
new_instance_table = realloc(*instance_table,
instance_table_size * sizeof(GeisInstance));
new_instance_table[instance_table_size-1] = subscribe_window(window);
if (!new_instance_table[instance_table_size-1])
{
fprintf(stderr, "error creating geis instance for window 0x%08x\n", window);
free(new_instance_table);
instance_table_size = 0;
goto error_exit;
}
xcb_generic_error_t *error;
tree_cookie = xcb_query_tree(xcb, window);
tree_reply = xcb_query_tree_reply(xcb,
tree_cookie,
&error);
if (!tree_reply)
{
fprintf(stderr, "failed to query tree for window 0x%x\n", window);
goto error_exit;
}
num_children = xcb_query_tree_children_length(tree_reply);
if (num_children <= 0)
goto tree_exit;
children = xcb_query_tree_children(tree_reply);
if (!children)
{
fprintf(stderr, "failed to retrieve children of window 0x%x\n", window);
goto tree_exit;
}
int i;
for (i = 0; i < num_children; i++)
{
instance_table_size = subscribe_windows_preorder(xcb,
children[i],
instance_table_size,
&new_instance_table);
if (instance_table_size == 0)
break;
}
tree_exit:
free(tree_reply);
error_exit:
*instance_table = new_instance_table;
return instance_table_size;
}
static size_t
subscribe_all_windows(GeisInstance **instance_table)
{
int instance_table_size = 0;
xcb_connection_t *xcb = xcb_connect(NULL, NULL);
if (!xcb) {
fprintf(stderr, "error connecting to X server\n");
return -1;
}
const xcb_setup_t *setup = xcb_get_setup(xcb);
if (!setup)
{
fprintf(stderr, "error getting xcb setup\n");
return -1;
}
xcb_screen_iterator_t screen = xcb_setup_roots_iterator(setup);
while (screen.rem)
{
instance_table_size = subscribe_windows_preorder(xcb,
screen.data->root,
instance_table_size,
instance_table);
xcb_screen_next(&screen);
}
xcb_disconnect(xcb);
return instance_table_size;
}
int
main(int argc, char* argv[])
{
int result = -1;
uint32_t window_id = 0;
int fd = -1;
GeisStatus status = GEIS_UNKNOWN_ERROR;
GeisInstance *instance_table = NULL;
size_t instance_table_size = 0;
if (!parse_opts(argc, argv, &window_id))
{
fprintf(stderr, "usage: %s windowid\n", argv[0]);
return -1;
}
if (window_id != 0)
{
instance_table_size = 1;
instance_table = calloc(instance_table_size, sizeof(GeisInstance));
instance_table[0] = subscribe_window(window_id);
if (!instance_table[0])
{
fprintf(stderr, "can not continue, exiting....\n");
goto error_exit;
}
}
else
{
instance_table_size = subscribe_all_windows(&instance_table);
if (instance_table_size == 0)
{
fprintf(stderr, "can not continue, exiting....\n");
goto error_exit;
}
}
status = geis_configuration_supported(instance_table[0], GEIS_CONFIG_UNIX_FD);
if (status != GEIS_STATUS_SUCCESS)
{
fprintf(stderr, "GEIS does not support Unix fd\n");
goto fail_exit;
}
status = geis_configuration_get_value(instance_table[0], GEIS_CONFIG_UNIX_FD, &fd);
if (status != GEIS_STATUS_SUCCESS)
{
fprintf(stderr, "error retrieving GEIS fd\n");
goto fail_exit;
}
while(1)
{
fd_set read_fds;
FD_ZERO(&read_fds);
FD_SET(0, &read_fds);
FD_SET(fd, &read_fds);
int sstat = select(fd+1, &read_fds, NULL, NULL, NULL);
if (sstat < 0)
{
fprintf(stderr, "error %d in select(): %s\n", errno, strerror(errno));
break;
}
if (FD_ISSET(fd, &read_fds))
{
geis_event_dispatch(instance_table[0]);
}
if (FD_ISSET(0, &read_fds))
{
break;
}
}
fail_exit:
{
int i;
for (i = 0; i < instance_table_size; ++i)
{
geis_finish(instance_table[i]);
}
}
error_exit:
if (instance_table_size > 0)
{
free(instance_table);
}
return result;
}