File: ei-debug-events.c

package info (click to toggle)
libei 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,916 kB
  • sloc: ansic: 23,868; python: 2,712; xml: 1,243; sh: 142; makefile: 63; cpp: 12; lisp: 2
file content (467 lines) | stat: -rw-r--r-- 11,800 bytes parent folder | download | duplicates (2)
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
457
458
459
460
461
462
463
464
465
466
467
/* SPDX-License-Identifier: MIT */
/*
 * Copyright © 2022 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/* A simple tool that provides a libei client that sends a fixed set of
 * events every second.
 *
 * This tool is useful for testing EIS implementations, to make sure we can
 * a connection, we receive devices and that we can send events.
 *
 * Usually, you'd want to:
 * - run the eis-demo-server (or some other EIS implementation)
 * - export LIBEI_SOCKET=eis-0, or whatever value was given
 * - run the ei-demo-client
 */

#include "config.h"

#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>
#include <poll.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>

#if HAVE_LIBEVDEV
#include <libevdev/libevdev.h>
#else
#define libevdev_event_code_get_name(...) ""
#endif

#include "libei.h"

#include "src/util-macros.h"
#include "src/util-mem.h"
#include "src/util-io.h"

DEFINE_UNREF_CLEANUP_FUNC(ei);
DEFINE_UNREF_CLEANUP_FUNC(ei_event);

#define truefalse(v_) (v_) ? "true" : "false"

static void
usage(FILE *fp, const char *argv0)
{
	fprintf(fp,
		"Usage: %s [--verbose] [--socketfd=<int>]\n"
		"\n"
		"Start an EI client and print communication from the EIS implementation in YAML format.\n"
		"The client will bind to all available capabilities on all seats.\n"
		"\n"
		"By default, this client connects to $LIBEI_SOCKET if set or $XDG_RUNTIME_DIR/eis-0\n"
		"\n"
		"Options:\n"
		" --socketfd    Use the given fd as socket to the EIS implementation\n"
		" --verbose	Enable debugging output\n"
		" --receiver    Enable receiver mode\n"
		" --sender      Enable sender mode (default)\n"
		"",
		argv0);
}

static void
print_header(void)
{
	printf("ei:\n");
	printf("  version: %s\n", EI_VERSION);
}

static void
print_event_header(struct ei_event *event)
{
	run_only_once {
		printf("events:\n");
	}
	printf("- type: %s\n", ei_event_type_to_string(ei_event_get_type(event)));
}

static void
print_seat_event(struct ei_event *event)
{
	struct ei_seat *seat = ei_event_get_seat(event);
	char *caps[6] = {NULL};
	size_t idx = 0;

	if (ei_seat_has_capability(seat, EI_DEVICE_CAP_POINTER))
		caps[idx++] = "pointer";
	if (ei_seat_has_capability(seat, EI_DEVICE_CAP_POINTER_ABSOLUTE))
		caps[idx++] = "pointer-absolute";
	if (ei_seat_has_capability(seat, EI_DEVICE_CAP_KEYBOARD))
		caps[idx++] = "keyboard";
	if (ei_seat_has_capability(seat, EI_DEVICE_CAP_TOUCH))
		caps[idx++] = "touch";

	_cleanup_free_ char *capabilities = strv_join(caps, ", ");

	printf("  seat: %s\n", ei_seat_get_name(seat));
	printf("  capabilities: [%s]\n", capabilities);
}

static void
print_device(struct ei_event *event)
{
	struct ei_device *device = ei_event_get_device(event);
	struct ei_seat *seat = ei_event_get_seat(event);

	printf("  device: %s\n", ei_device_get_name(device));
	printf("  seat: %s\n", ei_seat_get_name(seat));
}

static void
print_device_details(struct ei_event *event)
{
	struct ei_device *device = ei_event_get_device(event);

	print_device(event);

	char *caps[6] = {NULL};
	size_t idx = 0;

	if (ei_device_has_capability(device, EI_DEVICE_CAP_POINTER))
		caps[idx++] = "pointer";
	if (ei_device_has_capability(device, EI_DEVICE_CAP_POINTER_ABSOLUTE))
		caps[idx++] = "pointer-absolute";
	if (ei_device_has_capability(device, EI_DEVICE_CAP_KEYBOARD))
		caps[idx++] = "keyboard";
	if (ei_device_has_capability(device, EI_DEVICE_CAP_TOUCH))
		caps[idx++] = "touch";
	_cleanup_free_ char *capabilities = strv_join(caps, ", ");

	printf("  type: %s\n", ei_device_get_type(device) == EI_DEVICE_TYPE_VIRTUAL ? "virtual" : "physical");
	printf("  capabilities: [%s]\n", capabilities);

	idx = 0;
	struct ei_region *region;
	while ((region = ei_device_get_region(device, idx++))) {
	       if (idx == 1)
			printf("  regions:\n");

		uint32_t w = ei_region_get_width(region);
		uint32_t h = ei_region_get_height(region);
		uint32_t x = ei_region_get_x(region);
		uint32_t y = ei_region_get_y(region);
		double scale = ei_region_get_physical_scale(region);

		const char *mapping_id = ei_region_get_mapping_id(region);
		printf("    - { x: %u, y: %u, w: %u, h: %u, scale: %.2f, mapping_id: '%s' }\n",
		       x, y, w, h, scale,
		       mapping_id ? mapping_id : "<none>");
	}

	struct ei_keymap *keymap = ei_device_keyboard_get_keymap(device);
	if (keymap) {
		switch (ei_keymap_get_type(keymap)) {
		case EI_KEYMAP_TYPE_XKB:
			printf("  keymap: xkb\n");
			break;
		}
	}
}

static void
print_motion_event(struct ei_event *event)
{
	print_device(event);

	double dx = ei_event_pointer_get_dx(event);
	double dy = ei_event_pointer_get_dy(event);

	printf("  motion: [%f, %f]\n", dx, dy);
}

static void
print_abs_event(struct ei_event *event)
{
	print_device(event);

	double x = ei_event_pointer_get_absolute_x(event);
	double y = ei_event_pointer_get_absolute_y(event);

	printf("  position: [%f, %f]\n", x, y);
}

static void
print_button_event(struct ei_event *event)
{
	print_device(event);

	uint32_t button = ei_event_button_get_button(event);
	bool press = ei_event_button_get_is_press(event);

	printf("  button: %u # %s\n", button, libevdev_event_code_get_name(EV_KEY, button));
	printf("  press: %s\n", press ? "true" : "false");
}

static void
print_key_event(struct ei_event *event)
{
	print_device(event);

	uint32_t key = ei_event_keyboard_get_key(event);
	bool press = ei_event_keyboard_get_key_is_press(event);

	printf("  key: %u # %s\n", key, libevdev_event_code_get_name(EV_KEY, key));
	printf("  press: %s\n", press ? "true" : "false");
}

static void
print_touch_event(struct ei_event *event)
{
	print_device(event);

	uint32_t touchid = ei_event_touch_get_id(event);
	printf("   touchid: %u\n", touchid);

	if (ei_event_get_type(event) != EI_EVENT_TOUCH_UP) {
		double x = ei_event_touch_get_x(event);
		double y = ei_event_touch_get_y(event);

		printf("   position: [%f, %f]\n", x, y);
	}
}

static void
print_scroll_event(struct ei_event *event)
{
	print_device(event);

	double x = ei_event_scroll_get_dx(event);
	double y = ei_event_scroll_get_dy(event);

	printf("  scroll: [%f, %f]\n", x, y);
}

static void
print_scroll_discrete_event(struct ei_event *event)
{
	print_device(event);

	int32_t x = ei_event_scroll_get_discrete_dx(event);
	int32_t y = ei_event_scroll_get_discrete_dy(event);

	printf("  scroll: [%d, %d]\n", x, y);
}

static void
print_scroll_stop_event(struct ei_event *event)
{
	print_device(event);

	bool x = ei_event_scroll_get_stop_x(event);
	bool y = ei_event_scroll_get_stop_y(event);

	printf("  scroll: [%s, %s]\n", truefalse(x), truefalse(y));
}

static void
print_modifiers_event(struct ei_event *event)
{
	print_device(event);

	printf("  group: %u\n", ei_event_keyboard_get_xkb_group(event));
	printf("  depressed: \"0x%x\"\n", ei_event_keyboard_get_xkb_mods_depressed(event));
	printf("  latched: \"0x%x\"\n", ei_event_keyboard_get_xkb_mods_latched(event));
	printf("  locked: \"0x%x\"\n", ei_event_keyboard_get_xkb_mods_locked(event));
}

static void
print_pong_event(struct ei_event *event)
{
	struct ei_ping *ping = ei_event_pong_get_ping(event);

	printf("  id: %#" PRIx64 "\n", ei_ping_get_id(ping));
}

int main(int argc, char **argv)
{
	enum {
		MODE_RECEIVER,
		MODE_SENDER,
	} mode = MODE_SENDER;
	bool verbose = false;
	_cleanup_close_ int socketfd = -1;

	while (1) {
		enum {
			OPT_SOCKETFD,
			OPT_VERBOSE,
			OPT_RECEIVER,
			OPT_SENDER,
		};
		static struct option long_opts[] = {
			{"socketfd",	required_argument, 0, OPT_SOCKETFD},
			{"verbose",	no_argument, 0, OPT_VERBOSE},
			{"receiver",	no_argument, 0, OPT_RECEIVER},
			{"sender",	no_argument, 0, OPT_SENDER},
			{"help",	no_argument, 0, 'h'},
			{.name = NULL},
		};

		int optind = 0;
		int c = getopt_long(argc, argv, "h", long_opts, &optind);
		if (c == -1)
			break;

		switch(c) {
		case 'h':
			usage(stdout, argv[0]);
			return EXIT_SUCCESS;
		case OPT_VERBOSE:
			verbose = true;
			break;
		case OPT_SOCKETFD:
			if (!xatoi(optarg, &socketfd)) {
				fprintf(stderr, "Invalid socketfd: %s", optarg);
				return 2;
			}
			break;
		case OPT_SENDER:
			mode = MODE_SENDER;
			break;
		case OPT_RECEIVER:
			mode = MODE_RECEIVER;
			break;
		default:
			usage(stderr, argv[0]);
			return EXIT_FAILURE;
		}
	}

	_unref_(ei) *ei = NULL;
	if (mode == MODE_RECEIVER)
		ei = ei_new_receiver(NULL);
	else
		ei = ei_new_sender(NULL);

	if (verbose)
		ei_log_set_priority(ei, EI_LOG_PRIORITY_DEBUG);

	ei_configure_name(ei, "ei-debug-events");

	int rc = -EINVAL;
	if (socketfd == -1) {
		const char SOCKETNAME[] = "eis-0";
		rc = ei_setup_backend_socket(ei, getenv("LIBEI_SOCKET") ? NULL : SOCKETNAME);
	} else {
		rc = ei_setup_backend_fd(ei, socketfd);
	}

	if (rc != 0) {
		fprintf(stderr, "Failed to setup backend: %s\n", strerror(errno));
		return 1;
	}

	print_header();

	struct pollfd fds = {
		.fd = ei_get_fd(ei),
		.events = POLLIN,
		.revents = 0,
	};

	while (poll(&fds, 1, 2000) > -1) {
		ei_dispatch(ei);

		while (true) {
			_unref_(ei_event) *e = ei_get_event(ei);
			if (!e)
				break;

			struct ei_seat *seat = ei_event_get_seat(e);

			print_event_header(e);

			switch(ei_event_get_type(e)) {
			case EI_EVENT_CONNECT:
				break;
			case EI_EVENT_DISCONNECT:
				goto finished;
			case EI_EVENT_SEAT_ADDED:
				ei_seat_bind_capabilities(seat, EI_DEVICE_CAP_POINTER,
							  EI_DEVICE_CAP_POINTER_ABSOLUTE,
							  EI_DEVICE_CAP_KEYBOARD,
							  EI_DEVICE_CAP_TOUCH,
							  EI_DEVICE_CAP_BUTTON,
							  EI_DEVICE_CAP_SCROLL,
							  NULL);
				_fallthrough_;
			case EI_EVENT_SEAT_REMOVED:
				print_seat_event(e);
				break;
			case EI_EVENT_DEVICE_ADDED:
				print_device_details(e);
				break;
			case EI_EVENT_DEVICE_REMOVED:
			case EI_EVENT_DEVICE_RESUMED:
			case EI_EVENT_DEVICE_PAUSED:
			case EI_EVENT_DEVICE_START_EMULATING:
			case EI_EVENT_DEVICE_STOP_EMULATING:
			case EI_EVENT_FRAME:
				print_device(e);
				break;
			case EI_EVENT_POINTER_MOTION:
				print_motion_event(e);
				break;
			case EI_EVENT_POINTER_MOTION_ABSOLUTE:
				print_abs_event(e);
				break;
			case EI_EVENT_BUTTON_BUTTON:
				print_button_event(e);
				break;
			case EI_EVENT_KEYBOARD_KEY:
				print_key_event(e);
				break;
			case EI_EVENT_TOUCH_DOWN:
			case EI_EVENT_TOUCH_MOTION:
			case EI_EVENT_TOUCH_UP:
				print_touch_event(e);
				break;
			case EI_EVENT_SCROLL_DELTA:
				print_scroll_event(e);
				break;
			case EI_EVENT_SCROLL_DISCRETE:
				print_scroll_discrete_event(e);
				break;
			case EI_EVENT_SCROLL_STOP:
			case EI_EVENT_SCROLL_CANCEL:
				print_scroll_stop_event(e);
				break;
			case EI_EVENT_KEYBOARD_MODIFIERS:
				print_modifiers_event(e);
				break;
			case EI_EVENT_PONG:
				print_pong_event(e);
				break;
			case EI_EVENT_SYNC:
				break;
			}
		}
	}

finished:

	return 0;
}