• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2012 Philipp Brüschweiler
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "config.h"
25 
26 #include <errno.h>
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33 #include <assert.h>
34 #include <ctype.h>
35 #include <unistd.h>
36 
37 #include <wayland-client.h>
38 
39 #include "shared/helpers.h"
40 #include "shared/os-compatibility.h"
41 #include "shared/xalloc.h"
42 #include <libweston/zalloc.h>
43 #include "presentation-time-client-protocol.h"
44 #include "linux-dmabuf-unstable-v1-client-protocol.h"
45 #include "tablet-unstable-v2-client-protocol.h"
46 #include "xdg-output-unstable-v1-client-protocol.h"
47 
48 typedef void (*print_info_t)(void *info);
49 typedef void (*destroy_info_t)(void *info);
50 
51 struct global_info {
52 	struct wl_list link;
53 
54 	uint32_t id;
55 	uint32_t version;
56 	char *interface;
57 
58 	print_info_t print;
59 	destroy_info_t destroy;
60 };
61 
62 struct output_mode {
63 	struct wl_list link;
64 
65 	uint32_t flags;
66 	int32_t width, height;
67 	int32_t refresh;
68 };
69 
70 struct output_info {
71 	struct global_info global;
72 	struct wl_list global_link;
73 
74 	struct wl_output *output;
75 
76 	int32_t version;
77 
78 	struct {
79 		int32_t x, y;
80 		int32_t scale;
81 		int32_t physical_width, physical_height;
82 		enum wl_output_subpixel subpixel;
83 		enum wl_output_transform output_transform;
84 		char *make;
85 		char *model;
86 	} geometry;
87 
88 	struct wl_list modes;
89 };
90 
91 struct shm_format {
92 	struct wl_list link;
93 
94 	uint32_t format;
95 };
96 
97 struct shm_info {
98 	struct global_info global;
99 	struct wl_shm *shm;
100 
101 	struct wl_list formats;
102 };
103 
104 struct linux_dmabuf_modifier {
105 	struct wl_list link;
106 
107 	uint32_t format;
108 	uint64_t modifier;
109 };
110 
111 struct linux_dmabuf_info {
112 	struct global_info global;
113 	struct zwp_linux_dmabuf_v1 *dmabuf;
114 
115 	struct wl_list modifiers;
116 };
117 
118 struct seat_info {
119 	struct global_info global;
120 	struct wl_list global_link;
121 	struct wl_seat *seat;
122 	struct weston_info *info;
123 
124 	struct wl_keyboard *keyboard;
125 	uint32_t capabilities;
126 	char *name;
127 
128 	int32_t repeat_rate;
129 	int32_t repeat_delay;
130 };
131 
132 struct tablet_v2_path {
133 	struct wl_list link;
134 	char *path;
135 };
136 
137 struct tablet_tool_info {
138 	struct wl_list link;
139 	struct zwp_tablet_tool_v2 *tool;
140 
141 	uint64_t hardware_serial;
142 	uint64_t hardware_id_wacom;
143 	enum zwp_tablet_tool_v2_type type;
144 
145 	bool has_tilt;
146 	bool has_pressure;
147 	bool has_distance;
148 	bool has_rotation;
149 	bool has_slider;
150 	bool has_wheel;
151 };
152 
153 struct tablet_pad_group_info {
154 	struct wl_list link;
155 	struct zwp_tablet_pad_group_v2 *group;
156 
157 	uint32_t modes;
158 	size_t button_count;
159 	int *buttons;
160 	size_t strips;
161 	size_t rings;
162 };
163 
164 struct tablet_pad_info {
165 	struct wl_list link;
166 	struct zwp_tablet_pad_v2 *pad;
167 
168 	uint32_t buttons;
169 	struct wl_list paths;
170 	struct wl_list groups;
171 };
172 
173 struct tablet_info {
174 	struct wl_list link;
175 	struct zwp_tablet_v2 *tablet;
176 
177 	char *name;
178 	uint32_t vid, pid;
179 	struct wl_list paths;
180 };
181 
182 struct tablet_seat_info {
183 	struct wl_list link;
184 
185 	struct zwp_tablet_seat_v2 *seat;
186 	struct seat_info *seat_info;
187 
188 	struct wl_list tablets;
189 	struct wl_list tools;
190 	struct wl_list pads;
191 };
192 
193 struct tablet_v2_info {
194 	struct global_info global;
195 	struct zwp_tablet_manager_v2 *manager;
196 	struct weston_info *info;
197 
198 	struct wl_list seats;
199 };
200 
201 struct xdg_output_v1_info {
202 	struct wl_list link;
203 
204 	struct zxdg_output_v1 *xdg_output;
205 	struct output_info *output;
206 
207 	struct {
208 		int32_t x, y;
209 		int32_t width, height;
210 	} logical;
211 
212 	char *name, *description;
213 };
214 
215 struct xdg_output_manager_v1_info {
216 	struct global_info global;
217 	struct zxdg_output_manager_v1 *manager;
218 	struct weston_info *info;
219 
220 	struct wl_list outputs;
221 };
222 
223 struct presentation_info {
224 	struct global_info global;
225 	struct wp_presentation *presentation;
226 
227 	clockid_t clk_id;
228 };
229 
230 struct weston_info {
231 	struct wl_display *display;
232 	struct wl_registry *registry;
233 
234 	struct wl_list infos;
235 	bool roundtrip_needed;
236 
237 	/* required for tablet-unstable-v2 */
238 	struct wl_list seats;
239 	struct tablet_v2_info *tablet_info;
240 
241 	/* required for xdg-output-unstable-v1 */
242 	struct wl_list outputs;
243 	struct xdg_output_manager_v1_info *xdg_output_manager_v1_info;
244 };
245 
246 static void
print_global_info(void * data)247 print_global_info(void *data)
248 {
249 	struct global_info *global = data;
250 
251 	printf("interface: '%s', version: %u, name: %u\n",
252 	       global->interface, global->version, global->id);
253 }
254 
255 static void
init_global_info(struct weston_info * info,struct global_info * global,uint32_t id,const char * interface,uint32_t version)256 init_global_info(struct weston_info *info,
257 		 struct global_info *global, uint32_t id,
258 		 const char *interface, uint32_t version)
259 {
260 	global->id = id;
261 	global->version = version;
262 	global->interface = xstrdup(interface);
263 
264 	wl_list_insert(info->infos.prev, &global->link);
265 }
266 
267 static void
print_output_info(void * data)268 print_output_info(void *data)
269 {
270 	struct output_info *output = data;
271 	struct output_mode *mode;
272 	const char *subpixel_orientation;
273 	const char *transform;
274 
275 	print_global_info(data);
276 
277 	switch (output->geometry.subpixel) {
278 	case WL_OUTPUT_SUBPIXEL_UNKNOWN:
279 		subpixel_orientation = "unknown";
280 		break;
281 	case WL_OUTPUT_SUBPIXEL_NONE:
282 		subpixel_orientation = "none";
283 		break;
284 	case WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB:
285 		subpixel_orientation = "horizontal rgb";
286 		break;
287 	case WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR:
288 		subpixel_orientation = "horizontal bgr";
289 		break;
290 	case WL_OUTPUT_SUBPIXEL_VERTICAL_RGB:
291 		subpixel_orientation = "vertical rgb";
292 		break;
293 	case WL_OUTPUT_SUBPIXEL_VERTICAL_BGR:
294 		subpixel_orientation = "vertical bgr";
295 		break;
296 	default:
297 		fprintf(stderr, "unknown subpixel orientation %u\n",
298 			output->geometry.subpixel);
299 		subpixel_orientation = "unexpected value";
300 		break;
301 	}
302 
303 	switch (output->geometry.output_transform) {
304 	case WL_OUTPUT_TRANSFORM_NORMAL:
305 		transform = "normal";
306 		break;
307 	case WL_OUTPUT_TRANSFORM_90:
308 		transform = "90°";
309 		break;
310 	case WL_OUTPUT_TRANSFORM_180:
311 		transform = "180°";
312 		break;
313 	case WL_OUTPUT_TRANSFORM_270:
314 		transform = "270°";
315 		break;
316 	case WL_OUTPUT_TRANSFORM_FLIPPED:
317 		transform = "flipped";
318 		break;
319 	case WL_OUTPUT_TRANSFORM_FLIPPED_90:
320 		transform = "flipped 90°";
321 		break;
322 	case WL_OUTPUT_TRANSFORM_FLIPPED_180:
323 		transform = "flipped 180°";
324 		break;
325 	case WL_OUTPUT_TRANSFORM_FLIPPED_270:
326 		transform = "flipped 270°";
327 		break;
328 	default:
329 		fprintf(stderr, "unknown output transform %u\n",
330 			output->geometry.output_transform);
331 		transform = "unexpected value";
332 		break;
333 	}
334 
335 	printf("\tx: %d, y: %d,",
336 	       output->geometry.x, output->geometry.y);
337 	if (output->version >= 2)
338 		printf(" scale: %d,", output->geometry.scale);
339 	printf("\n");
340 
341 	printf("\tphysical_width: %d mm, physical_height: %d mm,\n",
342 	       output->geometry.physical_width,
343 	       output->geometry.physical_height);
344 	printf("\tmake: '%s', model: '%s',\n",
345 	       output->geometry.make, output->geometry.model);
346 	printf("\tsubpixel_orientation: %s, output_transform: %s,\n",
347 	       subpixel_orientation, transform);
348 
349 	wl_list_for_each(mode, &output->modes, link) {
350 		printf("\tmode:\n");
351 
352 		printf("\t\twidth: %d px, height: %d px, refresh: %.3f Hz,\n",
353 		       mode->width, mode->height,
354 		       (float) mode->refresh / 1000);
355 
356 		printf("\t\tflags:");
357 		if (mode->flags & WL_OUTPUT_MODE_CURRENT)
358 			printf(" current");
359 		if (mode->flags & WL_OUTPUT_MODE_PREFERRED)
360 			printf(" preferred");
361 		printf("\n");
362 	}
363 }
364 
365 static char
bits2graph(uint32_t value,unsigned bitoffset)366 bits2graph(uint32_t value, unsigned bitoffset)
367 {
368 	int c = (value >> bitoffset) & 0xff;
369 
370 	if (isgraph(c) || isspace(c))
371 		return c;
372 
373 	return '?';
374 }
375 
376 static void
fourcc2str(uint32_t format,char * str,int len)377 fourcc2str(uint32_t format, char *str, int len)
378 {
379 	int i;
380 
381 	assert(len >= 5);
382 
383 	for (i = 0; i < 4; i++)
384 		str[i] = bits2graph(format, i * 8);
385 	str[i] = '\0';
386 }
387 
388 static void
print_shm_info(void * data)389 print_shm_info(void *data)
390 {
391 	char str[5];
392 	struct shm_info *shm = data;
393 	struct shm_format *format;
394 
395 	print_global_info(data);
396 
397 	printf("\tformats:");
398 
399 	wl_list_for_each(format, &shm->formats, link)
400 		switch (format->format) {
401 		case WL_SHM_FORMAT_ARGB8888:
402 			printf(" ARGB8888");
403 			break;
404 		case WL_SHM_FORMAT_XRGB8888:
405 			printf(" XRGB8888");
406 			break;
407 		case WL_SHM_FORMAT_RGB565:
408 			printf(" RGB565");
409 			break;
410 		default:
411 			fourcc2str(format->format, str, sizeof(str));
412 			printf(" '%s'(0x%08x)", str, format->format);
413 			break;
414 		}
415 
416 	printf("\n");
417 }
418 
419 static void
print_linux_dmabuf_info(void * data)420 print_linux_dmabuf_info(void *data)
421 {
422 	char str[5];
423 	struct linux_dmabuf_info *dmabuf = data;
424 	struct linux_dmabuf_modifier *modifier;
425 
426 	print_global_info(data);
427 
428 	printf("\tformats:");
429 
430 	wl_list_for_each(modifier, &dmabuf->modifiers, link) {
431 		fourcc2str(modifier->format, str, sizeof(str));
432 		printf("\n\t'%s'(0x%08x), modifier: 0x%016"PRIx64, str, modifier->format, modifier->modifier);
433 	}
434 
435 	printf("\n");
436 }
437 
438 static void
print_seat_info(void * data)439 print_seat_info(void *data)
440 {
441 	struct seat_info *seat = data;
442 
443 	print_global_info(data);
444 
445 	printf("\tname: %s\n", seat->name);
446 	printf("\tcapabilities:");
447 
448 	if (seat->capabilities & WL_SEAT_CAPABILITY_POINTER)
449 		printf(" pointer");
450 	if (seat->capabilities & WL_SEAT_CAPABILITY_KEYBOARD)
451 		printf(" keyboard");
452 	if (seat->capabilities & WL_SEAT_CAPABILITY_TOUCH)
453 		printf(" touch");
454 
455 	printf("\n");
456 
457 	if (seat->repeat_rate > 0)
458 		printf("\tkeyboard repeat rate: %d\n", seat->repeat_rate);
459 	if (seat->repeat_delay > 0)
460 		printf("\tkeyboard repeat delay: %d\n", seat->repeat_delay);
461 }
462 
463 static void
keyboard_handle_keymap(void * data,struct wl_keyboard * keyboard,uint32_t format,int fd,uint32_t size)464 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
465 		       uint32_t format, int fd, uint32_t size)
466 {
467 	/* Just so we don’t leak the keymap fd */
468 	close(fd);
469 }
470 
471 static void
keyboard_handle_enter(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface,struct wl_array * keys)472 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
473 		      uint32_t serial, struct wl_surface *surface,
474 		      struct wl_array *keys)
475 {
476 }
477 
478 static void
keyboard_handle_leave(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface)479 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
480 		      uint32_t serial, struct wl_surface *surface)
481 {
482 }
483 
484 static void
keyboard_handle_key(void * data,struct wl_keyboard * keyboard,uint32_t serial,uint32_t time,uint32_t key,uint32_t state)485 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
486 		    uint32_t serial, uint32_t time, uint32_t key,
487 		    uint32_t state)
488 {
489 }
490 
491 static void
keyboard_handle_modifiers(void * data,struct wl_keyboard * keyboard,uint32_t serial,uint32_t mods_depressed,uint32_t mods_latched,uint32_t mods_locked,uint32_t group)492 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
493 			  uint32_t serial, uint32_t mods_depressed,
494 			  uint32_t mods_latched, uint32_t mods_locked,
495 			  uint32_t group)
496 {
497 }
498 
499 static void
keyboard_handle_repeat_info(void * data,struct wl_keyboard * keyboard,int32_t rate,int32_t delay)500 keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
501 			    int32_t rate, int32_t delay)
502 {
503 	struct seat_info *seat = data;
504 
505 	seat->repeat_rate = rate;
506 	seat->repeat_delay = delay;
507 }
508 
509 static const struct wl_keyboard_listener keyboard_listener = {
510 	keyboard_handle_keymap,
511 	keyboard_handle_enter,
512 	keyboard_handle_leave,
513 	keyboard_handle_key,
514 	keyboard_handle_modifiers,
515 	keyboard_handle_repeat_info,
516 };
517 
518 static void
seat_handle_capabilities(void * data,struct wl_seat * wl_seat,enum wl_seat_capability caps)519 seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
520 			 enum wl_seat_capability caps)
521 {
522 	struct seat_info *seat = data;
523 
524 	seat->capabilities = caps;
525 
526 	/* we want listen for repeat_info from wl_keyboard, but only
527 	 * do so if the seat info is >= 4 and if we actually have a
528 	 * keyboard */
529 	if (seat->global.version < 4)
530 		return;
531 
532 	if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
533 		seat->keyboard = wl_seat_get_keyboard(seat->seat);
534 		wl_keyboard_add_listener(seat->keyboard, &keyboard_listener,
535 					 seat);
536 
537 		seat->info->roundtrip_needed = true;
538 	}
539 }
540 
541 static void
seat_handle_name(void * data,struct wl_seat * wl_seat,const char * name)542 seat_handle_name(void *data, struct wl_seat *wl_seat,
543 		 const char *name)
544 {
545 	struct seat_info *seat = data;
546 	seat->name = xstrdup(name);
547 }
548 
549 static const struct wl_seat_listener seat_listener = {
550 	seat_handle_capabilities,
551 	seat_handle_name,
552 };
553 
554 static void
destroy_seat_info(void * data)555 destroy_seat_info(void *data)
556 {
557 	struct seat_info *seat = data;
558 
559 	wl_seat_destroy(seat->seat);
560 
561 	if (seat->name != NULL)
562 		free(seat->name);
563 
564 	if (seat->keyboard)
565 		wl_keyboard_destroy(seat->keyboard);
566 
567 	wl_list_remove(&seat->global_link);
568 }
569 
570 static const char *
tablet_tool_type_to_str(enum zwp_tablet_tool_v2_type type)571 tablet_tool_type_to_str(enum zwp_tablet_tool_v2_type type)
572 {
573 	switch (type) {
574 	case ZWP_TABLET_TOOL_V2_TYPE_PEN:
575 		return "pen";
576 	case ZWP_TABLET_TOOL_V2_TYPE_ERASER:
577 		return "eraser";
578 	case ZWP_TABLET_TOOL_V2_TYPE_BRUSH:
579 		return "brush";
580 	case ZWP_TABLET_TOOL_V2_TYPE_PENCIL:
581 		return "pencil";
582 	case ZWP_TABLET_TOOL_V2_TYPE_AIRBRUSH:
583 		return "airbrush";
584 	case ZWP_TABLET_TOOL_V2_TYPE_FINGER:
585 		return "finger";
586 	case ZWP_TABLET_TOOL_V2_TYPE_MOUSE:
587 		return "mouse";
588 	case ZWP_TABLET_TOOL_V2_TYPE_LENS:
589 		return "lens";
590 	}
591 
592 	return "Unknown type";
593 }
594 
595 static void
print_tablet_tool_info(const struct tablet_tool_info * info)596 print_tablet_tool_info(const struct tablet_tool_info *info)
597 {
598 	printf("\t\ttablet_tool: %s\n", tablet_tool_type_to_str(info->type));
599 	if (info->hardware_serial) {
600 		printf("\t\t\thardware serial: %" PRIx64 "\n", info->hardware_serial);
601 	}
602 	if (info->hardware_id_wacom) {
603 		printf("\t\t\thardware wacom: %" PRIx64 "\n", info->hardware_id_wacom);
604 	}
605 
606 	printf("\t\t\tcapabilities:");
607 
608 	if (info->has_tilt) {
609 		printf(" tilt");
610 	}
611 	if (info->has_pressure) {
612 		printf(" pressure");
613 	}
614 	if (info->has_distance) {
615 		printf(" distance");
616 	}
617 	if (info->has_rotation) {
618 		printf(" rotation");
619 	}
620 	if (info->has_slider) {
621 		printf(" slider");
622 	}
623 	if (info->has_wheel) {
624 		printf(" wheel");
625 	}
626 	printf("\n");
627 }
628 
629 static void
destroy_tablet_tool_info(struct tablet_tool_info * info)630 destroy_tablet_tool_info(struct tablet_tool_info *info)
631 {
632 	wl_list_remove(&info->link);
633 	zwp_tablet_tool_v2_destroy(info->tool);
634 	free(info);
635 }
636 
637 static void
print_tablet_pad_group_info(const struct tablet_pad_group_info * info)638 print_tablet_pad_group_info(const struct tablet_pad_group_info *info)
639 {
640 	size_t i;
641 	printf("\t\t\tgroup:\n");
642 	printf("\t\t\t\tmodes: %u\n", info->modes);
643 	printf("\t\t\t\tstrips: %zu\n", info->strips);
644 	printf("\t\t\t\trings: %zu\n", info->rings);
645 	printf("\t\t\t\tbuttons:");
646 
647 	for (i = 0; i < info->button_count; ++i) {
648 		printf(" %d", info->buttons[i]);
649 	}
650 
651 	printf("\n");
652 }
653 
654 static void
destroy_tablet_pad_group_info(struct tablet_pad_group_info * info)655 destroy_tablet_pad_group_info(struct tablet_pad_group_info *info)
656 {
657 	wl_list_remove(&info->link);
658 	zwp_tablet_pad_group_v2_destroy(info->group);
659 
660 	if (info->buttons) {
661 		free(info->buttons);
662 	}
663 	free(info);
664 }
665 
666 static void
print_tablet_pad_info(const struct tablet_pad_info * info)667 print_tablet_pad_info(const struct tablet_pad_info *info)
668 {
669 	const struct tablet_v2_path *path;
670 	const struct tablet_pad_group_info *group;
671 
672 	printf("\t\tpad:\n");
673 	printf("\t\t\tbuttons: %u\n", info->buttons);
674 
675 	wl_list_for_each(path, &info->paths, link) {
676 		printf("\t\t\tpath: %s\n", path->path);
677 	}
678 
679 	wl_list_for_each(group, &info->groups, link) {
680 		print_tablet_pad_group_info(group);
681 	}
682 }
683 
684 static void
destroy_tablet_pad_info(struct tablet_pad_info * info)685 destroy_tablet_pad_info(struct tablet_pad_info *info)
686 {
687 	struct tablet_v2_path *path;
688 	struct tablet_v2_path *tmp_path;
689 	struct tablet_pad_group_info *group;
690 	struct tablet_pad_group_info *tmp_group;
691 
692 	wl_list_remove(&info->link);
693 	zwp_tablet_pad_v2_destroy(info->pad);
694 
695 	wl_list_for_each_safe(path, tmp_path, &info->paths, link) {
696 		wl_list_remove(&path->link);
697 		free(path->path);
698 		free(path);
699 	}
700 
701 	wl_list_for_each_safe(group, tmp_group, &info->groups, link) {
702 		destroy_tablet_pad_group_info(group);
703 	}
704 
705 	free(info);
706 }
707 
708 static void
print_tablet_info(const struct tablet_info * info)709 print_tablet_info(const struct tablet_info *info)
710 {
711 	const struct tablet_v2_path *path;
712 
713 	printf("\t\ttablet: %s\n", info->name);
714 	printf("\t\t\tvendor: %u\n", info->vid);
715 	printf("\t\t\tproduct: %u\n", info->pid);
716 
717 	wl_list_for_each(path, &info->paths, link) {
718 		printf("\t\t\tpath: %s\n", path->path);
719 	}
720 }
721 
722 static void
destroy_tablet_info(struct tablet_info * info)723 destroy_tablet_info(struct tablet_info *info)
724 {
725 	struct tablet_v2_path *path;
726 	struct tablet_v2_path *tmp;
727 
728 	wl_list_remove(&info->link);
729 	zwp_tablet_v2_destroy(info->tablet);
730 
731 	if (info->name) {
732 		free(info->name);
733 	}
734 
735 	wl_list_for_each_safe(path, tmp, &info->paths, link) {
736 		wl_list_remove(&path->link);
737 		free(path->path);
738 		free(path);
739 	}
740 
741 	free(info);
742 }
743 
744 static void
print_tablet_seat_info(const struct tablet_seat_info * info)745 print_tablet_seat_info(const struct tablet_seat_info *info)
746 {
747 	const struct tablet_info *tablet;
748 	const struct tablet_pad_info *pad;
749 	const struct tablet_tool_info *tool;
750 
751 	printf("\ttablet_seat: %s\n", info->seat_info->name);
752 
753 	wl_list_for_each(tablet, &info->tablets, link) {
754 		print_tablet_info(tablet);
755 	}
756 
757 	wl_list_for_each(pad, &info->pads, link) {
758 		print_tablet_pad_info(pad);
759 	}
760 
761 	wl_list_for_each(tool, &info->tools, link) {
762 		print_tablet_tool_info(tool);
763 	}
764 }
765 
766 static void
destroy_tablet_seat_info(struct tablet_seat_info * info)767 destroy_tablet_seat_info(struct tablet_seat_info *info)
768 {
769 	struct tablet_info *tablet;
770 	struct tablet_info *tmp_tablet;
771 	struct tablet_pad_info *pad;
772 	struct tablet_pad_info *tmp_pad;
773 	struct tablet_tool_info *tool;
774 	struct tablet_tool_info *tmp_tool;
775 
776 	wl_list_remove(&info->link);
777 	zwp_tablet_seat_v2_destroy(info->seat);
778 
779 	wl_list_for_each_safe(tablet, tmp_tablet, &info->tablets, link) {
780 		destroy_tablet_info(tablet);
781 	}
782 
783 	wl_list_for_each_safe(pad, tmp_pad, &info->pads, link) {
784 		destroy_tablet_pad_info(pad);
785 	}
786 
787 	wl_list_for_each_safe(tool, tmp_tool, &info->tools, link) {
788 		destroy_tablet_tool_info(tool);
789 	}
790 
791 	free(info);
792 }
793 
794 static void
print_tablet_v2_info(void * data)795 print_tablet_v2_info(void *data)
796 {
797 	struct tablet_v2_info *info = data;
798 	struct tablet_seat_info *seat;
799 	print_global_info(data);
800 
801 	wl_list_for_each(seat, &info->seats, link) {
802 		/* Skip tablet_seats without a tablet, they are irrelevant */
803 		if (wl_list_empty(&seat->pads) &&
804 		    wl_list_empty(&seat->tablets) &&
805 		    wl_list_empty(&seat->tools)) {
806 			continue;
807 		}
808 
809 		print_tablet_seat_info(seat);
810 	}
811 }
812 
813 static void
destroy_tablet_v2_info(void * data)814 destroy_tablet_v2_info(void *data)
815 {
816 	struct tablet_v2_info *info = data;
817 	struct tablet_seat_info *seat;
818 	struct tablet_seat_info *tmp;
819 
820 	zwp_tablet_manager_v2_destroy(info->manager);
821 
822 	wl_list_for_each_safe(seat, tmp, &info->seats, link) {
823 		destroy_tablet_seat_info(seat);
824 	}
825 }
826 
827 static void
handle_tablet_v2_tablet_tool_done(void * data,struct zwp_tablet_tool_v2 * tool)828 handle_tablet_v2_tablet_tool_done(void *data, struct zwp_tablet_tool_v2 *tool)
829 {
830 	/* don't bother waiting for this; there's no good reason a
831 	 * compositor will wait more than one roundtrip before sending
832 	 * these initial events. */
833 }
834 
835 static void
handle_tablet_v2_tablet_tool_removed(void * data,struct zwp_tablet_tool_v2 * tool)836 handle_tablet_v2_tablet_tool_removed(void *data, struct zwp_tablet_tool_v2 *tool)
837 {
838 	/* don't bother waiting for this; we never make any request either way. */
839 }
840 
841 static void
handle_tablet_v2_tablet_tool_type(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t tool_type)842 handle_tablet_v2_tablet_tool_type(void *data, struct zwp_tablet_tool_v2 *tool,
843                                   uint32_t tool_type)
844 {
845 	struct tablet_tool_info *info = data;
846 	info->type = tool_type;
847 }
848 
849 static void
handle_tablet_v2_tablet_tool_hardware_serial(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t serial_hi,uint32_t serial_lo)850 handle_tablet_v2_tablet_tool_hardware_serial(void *data,
851                                              struct zwp_tablet_tool_v2 *tool,
852                                              uint32_t serial_hi,
853                                              uint32_t serial_lo)
854 {
855 	struct tablet_tool_info *info = data;
856 
857 	info->hardware_serial = ((uint64_t) serial_hi) << 32 |
858 		(uint64_t) serial_lo;
859 }
860 
861 static void
handle_tablet_v2_tablet_tool_hardware_id_wacom(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t id_hi,uint32_t id_lo)862 handle_tablet_v2_tablet_tool_hardware_id_wacom(void *data,
863                                                struct zwp_tablet_tool_v2 *tool,
864                                                uint32_t id_hi, uint32_t id_lo)
865 {
866 	struct tablet_tool_info *info = data;
867 
868 	info->hardware_id_wacom = ((uint64_t) id_hi) << 32 | (uint64_t) id_lo;
869 }
870 
871 static void
handle_tablet_v2_tablet_tool_capability(void * data,struct zwp_tablet_tool_v2 * tool,uint32_t capability)872 handle_tablet_v2_tablet_tool_capability(void *data,
873                                         struct zwp_tablet_tool_v2 *tool,
874                                         uint32_t capability)
875 {
876 	struct tablet_tool_info *info = data;
877 	enum zwp_tablet_tool_v2_capability cap = capability;
878 
879 	switch(cap) {
880 	case ZWP_TABLET_TOOL_V2_CAPABILITY_TILT:
881 		info->has_tilt = true;
882 		break;
883 	case ZWP_TABLET_TOOL_V2_CAPABILITY_PRESSURE:
884 		info->has_pressure = true;
885 		break;
886 	case ZWP_TABLET_TOOL_V2_CAPABILITY_DISTANCE:
887 		info->has_distance = true;
888 		break;
889 	case ZWP_TABLET_TOOL_V2_CAPABILITY_ROTATION:
890 		info->has_rotation = true;
891 		break;
892 	case ZWP_TABLET_TOOL_V2_CAPABILITY_SLIDER:
893 		info->has_slider = true;
894 		break;
895 	case ZWP_TABLET_TOOL_V2_CAPABILITY_WHEEL:
896 		info->has_wheel = true;
897 		break;
898 	}
899 }
900 
901 static void
handle_tablet_v2_tablet_tool_proximity_in(void * data,struct zwp_tablet_tool_v2 * zwp_tablet_tool_v2,uint32_t serial,struct zwp_tablet_v2 * tablet,struct wl_surface * surface)902 handle_tablet_v2_tablet_tool_proximity_in(void *data,
903                                  struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
904                                  uint32_t serial, struct zwp_tablet_v2 *tablet,
905                                  struct wl_surface *surface)
906 {
907 
908 }
909 
910 static void
handle_tablet_v2_tablet_tool_proximity_out(void * data,struct zwp_tablet_tool_v2 * zwp_tablet_tool_v2)911 handle_tablet_v2_tablet_tool_proximity_out(void *data,
912                                  struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2)
913 {
914 
915 }
916 
917 static void
handle_tablet_v2_tablet_tool_down(void * data,struct zwp_tablet_tool_v2 * zwp_tablet_tool_v2,uint32_t serial)918 handle_tablet_v2_tablet_tool_down(void *data,
919                                  struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
920                                  uint32_t serial)
921 {
922 
923 }
924 
925 static void
handle_tablet_v2_tablet_tool_up(void * data,struct zwp_tablet_tool_v2 * zwp_tablet_tool_v2)926 handle_tablet_v2_tablet_tool_up(void *data,
927                                 struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2)
928 {
929 
930 }
931 
932 
933 static void
handle_tablet_v2_tablet_tool_motion(void * data,struct zwp_tablet_tool_v2 * zwp_tablet_tool_v2,wl_fixed_t x,wl_fixed_t y)934 handle_tablet_v2_tablet_tool_motion(void *data,
935                                  struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
936                                  wl_fixed_t x,
937                                  wl_fixed_t y)
938 {
939 
940 }
941 
942 static void
handle_tablet_v2_tablet_tool_pressure(void * data,struct zwp_tablet_tool_v2 * zwp_tablet_tool_v2,uint32_t pressure)943 handle_tablet_v2_tablet_tool_pressure(void *data,
944                                  struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
945                                  uint32_t pressure)
946 {
947 
948 }
949 
950 static void
handle_tablet_v2_tablet_tool_distance(void * data,struct zwp_tablet_tool_v2 * zwp_tablet_tool_v2,uint32_t distance)951 handle_tablet_v2_tablet_tool_distance(void *data,
952                                  struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
953                                  uint32_t distance)
954 {
955 
956 }
957 
958 static void
handle_tablet_v2_tablet_tool_tilt(void * data,struct zwp_tablet_tool_v2 * zwp_tablet_tool_v2,wl_fixed_t tilt_x,wl_fixed_t tilt_y)959 handle_tablet_v2_tablet_tool_tilt(void *data,
960                                  struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
961                                  wl_fixed_t tilt_x,
962                                  wl_fixed_t tilt_y)
963 {
964 
965 }
966 
967 static void
handle_tablet_v2_tablet_tool_rotation(void * data,struct zwp_tablet_tool_v2 * zwp_tablet_tool_v2,wl_fixed_t degrees)968 handle_tablet_v2_tablet_tool_rotation(void *data,
969                                  struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
970                                  wl_fixed_t degrees)
971 {
972 
973 }
974 
975 static void
handle_tablet_v2_tablet_tool_slider(void * data,struct zwp_tablet_tool_v2 * zwp_tablet_tool_v2,int32_t position)976 handle_tablet_v2_tablet_tool_slider(void *data,
977                                  struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
978                                  int32_t position)
979 {
980 
981 }
982 
983 static void
handle_tablet_v2_tablet_tool_wheel(void * data,struct zwp_tablet_tool_v2 * zwp_tablet_tool_v2,wl_fixed_t degrees,int32_t clicks)984 handle_tablet_v2_tablet_tool_wheel(void *data,
985                                  struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
986                                  wl_fixed_t degrees,
987                                  int32_t clicks)
988 {
989 
990 }
991 
992 static void
handle_tablet_v2_tablet_tool_button(void * data,struct zwp_tablet_tool_v2 * zwp_tablet_tool_v2,uint32_t serial,uint32_t button,uint32_t state)993 handle_tablet_v2_tablet_tool_button(void *data,
994                                  struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
995                                  uint32_t serial,
996                                  uint32_t button,
997                                  uint32_t state)
998 {
999 
1000 }
1001 
1002 static void
handle_tablet_v2_tablet_tool_frame(void * data,struct zwp_tablet_tool_v2 * zwp_tablet_tool_v2,uint32_t time)1003 handle_tablet_v2_tablet_tool_frame(void *data,
1004                                  struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
1005                                  uint32_t time)
1006 {
1007 
1008 }
1009 
1010 static const struct zwp_tablet_tool_v2_listener tablet_tool_listener = {
1011 	.removed = handle_tablet_v2_tablet_tool_removed,
1012 	.done = handle_tablet_v2_tablet_tool_done,
1013 	.type = handle_tablet_v2_tablet_tool_type,
1014 	.hardware_serial = handle_tablet_v2_tablet_tool_hardware_serial,
1015 	.hardware_id_wacom = handle_tablet_v2_tablet_tool_hardware_id_wacom,
1016 	.capability = handle_tablet_v2_tablet_tool_capability,
1017 
1018 	.proximity_in = handle_tablet_v2_tablet_tool_proximity_in,
1019 	.proximity_out = handle_tablet_v2_tablet_tool_proximity_out,
1020 	.down = handle_tablet_v2_tablet_tool_down,
1021 	.up = handle_tablet_v2_tablet_tool_up,
1022 
1023 	.motion = handle_tablet_v2_tablet_tool_motion,
1024 	.pressure = handle_tablet_v2_tablet_tool_pressure,
1025 	.distance = handle_tablet_v2_tablet_tool_distance,
1026 	.tilt = handle_tablet_v2_tablet_tool_tilt,
1027 	.rotation = handle_tablet_v2_tablet_tool_rotation,
1028 	.slider = handle_tablet_v2_tablet_tool_slider,
1029 	.wheel = handle_tablet_v2_tablet_tool_wheel,
1030 	.button = handle_tablet_v2_tablet_tool_button,
1031 	.frame = handle_tablet_v2_tablet_tool_frame,
1032 };
1033 
add_tablet_v2_tablet_tool_info(void * data,struct zwp_tablet_seat_v2 * tablet_seat_v2,struct zwp_tablet_tool_v2 * tool)1034 static void add_tablet_v2_tablet_tool_info(void *data,
1035                                      struct zwp_tablet_seat_v2 *tablet_seat_v2,
1036                                      struct zwp_tablet_tool_v2 *tool)
1037 {
1038 	struct tablet_seat_info *tablet_seat = data;
1039 	struct tablet_tool_info *tool_info = xzalloc(sizeof *tool_info);
1040 
1041 	tool_info->tool = tool;
1042 	wl_list_insert(&tablet_seat->tools, &tool_info->link);
1043 
1044 	zwp_tablet_tool_v2_add_listener(tool, &tablet_tool_listener, tool_info);
1045 }
1046 
1047 static void
handle_tablet_v2_tablet_pad_group_mode_switch(void * data,struct zwp_tablet_pad_group_v2 * zwp_tablet_pad_group_v2,uint32_t time,uint32_t serial,uint32_t mode)1048 handle_tablet_v2_tablet_pad_group_mode_switch(void *data,
1049                        struct zwp_tablet_pad_group_v2 *zwp_tablet_pad_group_v2,
1050                        uint32_t time, uint32_t serial, uint32_t mode)
1051 {
1052 	/* This shouldn't ever happen  */
1053 }
1054 
1055 static void
handle_tablet_v2_tablet_pad_group_done(void * data,struct zwp_tablet_pad_group_v2 * group)1056 handle_tablet_v2_tablet_pad_group_done(void *data,
1057                                        struct zwp_tablet_pad_group_v2 *group)
1058 {
1059 	/* don't bother waiting for this; there's no good reason a
1060 	 * compositor will wait more than one roundtrip before sending
1061 	 * these initial events. */
1062 }
1063 
1064 static void
handle_tablet_v2_tablet_pad_group_modes(void * data,struct zwp_tablet_pad_group_v2 * group,uint32_t modes)1065 handle_tablet_v2_tablet_pad_group_modes(void *data,
1066                                         struct zwp_tablet_pad_group_v2 *group,
1067                                         uint32_t modes)
1068 {
1069 	struct tablet_pad_group_info *info = data;
1070 	info->modes = modes;
1071 }
1072 
1073 static void
handle_tablet_v2_tablet_pad_group_buttons(void * data,struct zwp_tablet_pad_group_v2 * group,struct wl_array * buttons)1074 handle_tablet_v2_tablet_pad_group_buttons(void *data,
1075                                           struct zwp_tablet_pad_group_v2 *group,
1076                                           struct wl_array *buttons)
1077 {
1078 	struct tablet_pad_group_info *info = data;
1079 
1080 	info->button_count = buttons->size / sizeof(int);
1081 	info->buttons = xzalloc(buttons->size);
1082 	memcpy(info->buttons, buttons->data, buttons->size);
1083 }
1084 
1085 static void
handle_tablet_v2_tablet_pad_group_ring(void * data,struct zwp_tablet_pad_group_v2 * group,struct zwp_tablet_pad_ring_v2 * ring)1086 handle_tablet_v2_tablet_pad_group_ring(void *data,
1087                                        struct zwp_tablet_pad_group_v2 *group,
1088                                        struct zwp_tablet_pad_ring_v2 *ring)
1089 {
1090 	struct tablet_pad_group_info *info = data;
1091 	++info->rings;
1092 
1093 	zwp_tablet_pad_ring_v2_destroy(ring);
1094 }
1095 
1096 static void
handle_tablet_v2_tablet_pad_group_strip(void * data,struct zwp_tablet_pad_group_v2 * group,struct zwp_tablet_pad_strip_v2 * strip)1097 handle_tablet_v2_tablet_pad_group_strip(void *data,
1098                                         struct zwp_tablet_pad_group_v2 *group,
1099                                         struct zwp_tablet_pad_strip_v2 *strip)
1100 {
1101 	struct tablet_pad_group_info *info = data;
1102 	++info->strips;
1103 
1104 	zwp_tablet_pad_strip_v2_destroy(strip);
1105 }
1106 
1107 static const struct zwp_tablet_pad_group_v2_listener tablet_pad_group_listener = {
1108 	.buttons = handle_tablet_v2_tablet_pad_group_buttons,
1109 	.modes = handle_tablet_v2_tablet_pad_group_modes,
1110 	.ring = handle_tablet_v2_tablet_pad_group_ring,
1111 	.strip = handle_tablet_v2_tablet_pad_group_strip,
1112 	.done = handle_tablet_v2_tablet_pad_group_done,
1113 	.mode_switch = handle_tablet_v2_tablet_pad_group_mode_switch,
1114 };
1115 
1116 static void
handle_tablet_v2_tablet_pad_group(void * data,struct zwp_tablet_pad_v2 * zwp_tablet_pad_v2,struct zwp_tablet_pad_group_v2 * pad_group)1117 handle_tablet_v2_tablet_pad_group(void *data,
1118                                   struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2,
1119                                   struct zwp_tablet_pad_group_v2 *pad_group)
1120 {
1121 	struct tablet_pad_info *pad_info = data;
1122 	struct tablet_pad_group_info *group = xzalloc(sizeof *group);
1123 
1124 	wl_list_insert(&pad_info->groups, &group->link);
1125 	group->group = pad_group;
1126 	zwp_tablet_pad_group_v2_add_listener(pad_group,
1127 	                                     &tablet_pad_group_listener, group);
1128 }
1129 
1130 static void
handle_tablet_v2_tablet_pad_path(void * data,struct zwp_tablet_pad_v2 * pad,const char * path)1131 handle_tablet_v2_tablet_pad_path(void *data, struct zwp_tablet_pad_v2 *pad,
1132                                  const char *path)
1133 {
1134 	struct tablet_pad_info *pad_info = data;
1135 	struct tablet_v2_path *path_elem = xzalloc(sizeof *path_elem);
1136 	path_elem->path = xstrdup(path);
1137 
1138 	wl_list_insert(&pad_info->paths, &path_elem->link);
1139 }
1140 
1141 static void
handle_tablet_v2_tablet_pad_buttons(void * data,struct zwp_tablet_pad_v2 * pad,uint32_t buttons)1142 handle_tablet_v2_tablet_pad_buttons(void *data, struct zwp_tablet_pad_v2 *pad,
1143                                     uint32_t buttons)
1144 {
1145 	struct tablet_pad_info *pad_info = data;
1146 
1147 	pad_info->buttons = buttons;
1148 }
1149 
1150 static void
handle_tablet_v2_tablet_pad_done(void * data,struct zwp_tablet_pad_v2 * pad)1151 handle_tablet_v2_tablet_pad_done(void *data, struct zwp_tablet_pad_v2 *pad)
1152 {
1153 	/* don't bother waiting for this; there's no good reason a
1154 	 * compositor will wait more than one roundtrip before sending
1155 	 * these initial events. */
1156 }
1157 
1158 static void
handle_tablet_v2_tablet_pad_removed(void * data,struct zwp_tablet_pad_v2 * pad)1159 handle_tablet_v2_tablet_pad_removed(void *data, struct zwp_tablet_pad_v2 *pad)
1160 {
1161 	/* don't bother waiting for this; We never make any request that's not
1162 	 * allowed to be issued either way. */
1163 }
1164 
1165 static void
handle_tablet_v2_tablet_pad_button(void * data,struct zwp_tablet_pad_v2 * pad,uint32_t time,uint32_t button,uint32_t state)1166 handle_tablet_v2_tablet_pad_button(void *data, struct zwp_tablet_pad_v2 *pad,
1167                                    uint32_t time, uint32_t button, uint32_t state)
1168 {
1169 	/* we don't have a surface, so this can't ever happen */
1170 }
1171 
1172 static void
handle_tablet_v2_tablet_pad_enter(void * data,struct zwp_tablet_pad_v2 * pad,uint32_t serial,struct zwp_tablet_v2 * tablet,struct wl_surface * surface)1173 handle_tablet_v2_tablet_pad_enter(void *data, struct zwp_tablet_pad_v2 *pad,
1174                                   uint32_t serial,
1175                                   struct zwp_tablet_v2 *tablet,
1176                                   struct wl_surface *surface)
1177 {
1178 	/* we don't have a surface, so this can't ever happen */
1179 }
1180 
1181 static void
handle_tablet_v2_tablet_pad_leave(void * data,struct zwp_tablet_pad_v2 * pad,uint32_t serial,struct wl_surface * surface)1182 handle_tablet_v2_tablet_pad_leave(void *data, struct zwp_tablet_pad_v2 *pad,
1183 		uint32_t serial, struct wl_surface *surface)
1184 {
1185 	/* we don't have a surface, so this can't ever happen */
1186 }
1187 
1188 static const struct zwp_tablet_pad_v2_listener tablet_pad_listener = {
1189 	.group = handle_tablet_v2_tablet_pad_group,
1190 	.path = handle_tablet_v2_tablet_pad_path,
1191 	.buttons = handle_tablet_v2_tablet_pad_buttons,
1192 	.done = handle_tablet_v2_tablet_pad_done,
1193 	.removed = handle_tablet_v2_tablet_pad_removed,
1194 	.button = handle_tablet_v2_tablet_pad_button,
1195 	.enter = handle_tablet_v2_tablet_pad_enter,
1196 	.leave = handle_tablet_v2_tablet_pad_leave,
1197 };
1198 
add_tablet_v2_tablet_pad_info(void * data,struct zwp_tablet_seat_v2 * tablet_seat_v2,struct zwp_tablet_pad_v2 * pad)1199 static void add_tablet_v2_tablet_pad_info(void *data,
1200                                      struct zwp_tablet_seat_v2 *tablet_seat_v2,
1201                                      struct zwp_tablet_pad_v2 *pad)
1202 {
1203 	struct tablet_seat_info *tablet_seat = data;
1204 	struct tablet_pad_info *pad_info = xzalloc(sizeof *pad_info);
1205 
1206 	wl_list_init(&pad_info->paths);
1207 	wl_list_init(&pad_info->groups);
1208 	pad_info->pad = pad;
1209 	wl_list_insert(&tablet_seat->pads, &pad_info->link);
1210 
1211 	zwp_tablet_pad_v2_add_listener(pad, &tablet_pad_listener, pad_info);
1212 }
1213 
1214 static void
handle_tablet_v2_tablet_name(void * data,struct zwp_tablet_v2 * zwp_tablet_v2,const char * name)1215 handle_tablet_v2_tablet_name(void *data, struct zwp_tablet_v2 *zwp_tablet_v2,
1216                              const char *name)
1217 {
1218 	struct tablet_info *tablet_info = data;
1219 	tablet_info->name = xstrdup(name);
1220 }
1221 
1222 static void
handle_tablet_v2_tablet_path(void * data,struct zwp_tablet_v2 * zwp_tablet_v2,const char * path)1223 handle_tablet_v2_tablet_path(void *data, struct zwp_tablet_v2 *zwp_tablet_v2,
1224                              const char *path)
1225 {
1226 	struct tablet_info *tablet_info = data;
1227 	struct tablet_v2_path *path_elem = xzalloc(sizeof *path_elem);
1228 	path_elem->path = xstrdup(path);
1229 
1230 	wl_list_insert(&tablet_info->paths, &path_elem->link);
1231 }
1232 
1233 static void
handle_tablet_v2_tablet_id(void * data,struct zwp_tablet_v2 * zwp_tablet_v2,uint32_t vid,uint32_t pid)1234 handle_tablet_v2_tablet_id(void *data, struct zwp_tablet_v2 *zwp_tablet_v2,
1235                            uint32_t vid, uint32_t pid)
1236 {
1237 	struct tablet_info *tablet_info = data;
1238 
1239 	tablet_info->vid = vid;
1240 	tablet_info->pid = pid;
1241 }
1242 
1243 static void
handle_tablet_v2_tablet_done(void * data,struct zwp_tablet_v2 * zwp_tablet_v2)1244 handle_tablet_v2_tablet_done(void *data, struct zwp_tablet_v2 *zwp_tablet_v2)
1245 {
1246 	/* don't bother waiting for this; there's no good reason a
1247 	 * compositor will wait more than one roundtrip before sending
1248 	 * these initial events. */
1249 }
1250 
1251 static void
handle_tablet_v2_tablet_removed(void * data,struct zwp_tablet_v2 * zwp_tablet_v2)1252 handle_tablet_v2_tablet_removed(void *data, struct zwp_tablet_v2 *zwp_tablet_v2)
1253 {
1254 	/* don't bother waiting for this; We never make any request that's not
1255 	 * allowed to be issued either way. */
1256 }
1257 
1258 static const struct zwp_tablet_v2_listener tablet_listener = {
1259 	.name = handle_tablet_v2_tablet_name,
1260 	.id = handle_tablet_v2_tablet_id,
1261 	.path = handle_tablet_v2_tablet_path,
1262 	.done = handle_tablet_v2_tablet_done,
1263 	.removed = handle_tablet_v2_tablet_removed
1264 };
1265 
1266 static void
add_tablet_v2_tablet_info(void * data,struct zwp_tablet_seat_v2 * tablet_seat_v2,struct zwp_tablet_v2 * tablet)1267 add_tablet_v2_tablet_info(void *data, struct zwp_tablet_seat_v2 *tablet_seat_v2,
1268                           struct zwp_tablet_v2 *tablet)
1269 {
1270 	struct tablet_seat_info *tablet_seat = data;
1271 	struct tablet_info *tablet_info = xzalloc(sizeof *tablet_info);
1272 
1273 	wl_list_init(&tablet_info->paths);
1274 	tablet_info->tablet = tablet;
1275 	wl_list_insert(&tablet_seat->tablets, &tablet_info->link);
1276 
1277 	zwp_tablet_v2_add_listener(tablet, &tablet_listener, tablet_info);
1278 }
1279 
1280 static const struct zwp_tablet_seat_v2_listener tablet_seat_listener =  {
1281 	.tablet_added = add_tablet_v2_tablet_info,
1282 	.pad_added = add_tablet_v2_tablet_pad_info,
1283 	.tool_added = add_tablet_v2_tablet_tool_info,
1284 };
1285 
1286 static void
add_tablet_seat_info(struct tablet_v2_info * tablet_info,struct seat_info * seat)1287 add_tablet_seat_info(struct tablet_v2_info *tablet_info, struct seat_info *seat)
1288 {
1289 	struct tablet_seat_info *tablet_seat = xzalloc(sizeof *tablet_seat);
1290 
1291 	wl_list_insert(&tablet_info->seats, &tablet_seat->link);
1292 	tablet_seat->seat = zwp_tablet_manager_v2_get_tablet_seat(
1293 		tablet_info->manager, seat->seat);
1294 	zwp_tablet_seat_v2_add_listener(tablet_seat->seat,
1295 		&tablet_seat_listener, tablet_seat);
1296 
1297 	wl_list_init(&tablet_seat->pads);
1298 	wl_list_init(&tablet_seat->tablets);
1299 	wl_list_init(&tablet_seat->tools);
1300 	tablet_seat->seat_info = seat;
1301 
1302 	tablet_info->info->roundtrip_needed = true;
1303 }
1304 
1305 static void
add_tablet_v2_info(struct weston_info * info,uint32_t id,uint32_t version)1306 add_tablet_v2_info(struct weston_info *info, uint32_t id, uint32_t version)
1307 {
1308 	struct seat_info *seat;
1309 	struct tablet_v2_info *tablet = xzalloc(sizeof *tablet);
1310 
1311 	wl_list_init(&tablet->seats);
1312 	tablet->info = info;
1313 
1314 	init_global_info(info, &tablet->global, id,
1315 		zwp_tablet_manager_v2_interface.name, version);
1316 	tablet->global.print = print_tablet_v2_info;
1317 	tablet->global.destroy = destroy_tablet_v2_info;
1318 
1319 	tablet->manager = wl_registry_bind(info->registry,
1320 		id, &zwp_tablet_manager_v2_interface, 1);
1321 
1322 	wl_list_for_each(seat, &info->seats, global_link) {
1323 		add_tablet_seat_info(tablet, seat);
1324 	}
1325 
1326 	info->tablet_info = tablet;
1327 }
1328 
1329 static void
destroy_xdg_output_v1_info(struct xdg_output_v1_info * info)1330 destroy_xdg_output_v1_info(struct xdg_output_v1_info *info)
1331 {
1332 	wl_list_remove(&info->link);
1333 	zxdg_output_v1_destroy(info->xdg_output);
1334 	free(info->name);
1335 	free(info->description);
1336 	free(info);
1337 }
1338 
1339 static void
print_xdg_output_v1_info(const struct xdg_output_v1_info * info)1340 print_xdg_output_v1_info(const struct xdg_output_v1_info *info)
1341 {
1342 	printf("\txdg_output_v1\n");
1343 	printf("\t\toutput: %d\n", info->output->global.id);
1344 	if (info->name)
1345 		printf("\t\tname: '%s'\n", info->name);
1346 	if (info->description)
1347 		printf("\t\tdescription: '%s'\n", info->description);
1348 	printf("\t\tlogical_x: %d, logical_y: %d\n",
1349 		info->logical.x, info->logical.y);
1350 	printf("\t\tlogical_width: %d, logical_height: %d\n",
1351 		info->logical.width, info->logical.height);
1352 }
1353 
1354 static void
print_xdg_output_manager_v1_info(void * data)1355 print_xdg_output_manager_v1_info(void *data)
1356 {
1357 	struct xdg_output_manager_v1_info *info = data;
1358 	struct xdg_output_v1_info *output;
1359 
1360 	print_global_info(data);
1361 
1362 	wl_list_for_each(output, &info->outputs, link)
1363 		print_xdg_output_v1_info(output);
1364 }
1365 
1366 static void
destroy_xdg_output_manager_v1_info(void * data)1367 destroy_xdg_output_manager_v1_info(void *data)
1368 {
1369 	struct xdg_output_manager_v1_info *info = data;
1370 	struct xdg_output_v1_info *output, *tmp;
1371 
1372 	zxdg_output_manager_v1_destroy(info->manager);
1373 
1374 	wl_list_for_each_safe(output, tmp, &info->outputs, link)
1375 		destroy_xdg_output_v1_info(output);
1376 }
1377 
1378 static void
handle_xdg_output_v1_logical_position(void * data,struct zxdg_output_v1 * output,int32_t x,int32_t y)1379 handle_xdg_output_v1_logical_position(void *data, struct zxdg_output_v1 *output,
1380                                       int32_t x, int32_t y)
1381 {
1382 	struct xdg_output_v1_info *xdg_output = data;
1383 	xdg_output->logical.x = x;
1384 	xdg_output->logical.y = y;
1385 }
1386 
1387 static void
handle_xdg_output_v1_logical_size(void * data,struct zxdg_output_v1 * output,int32_t width,int32_t height)1388 handle_xdg_output_v1_logical_size(void *data, struct zxdg_output_v1 *output,
1389                                       int32_t width, int32_t height)
1390 {
1391 	struct xdg_output_v1_info *xdg_output = data;
1392 	xdg_output->logical.width = width;
1393 	xdg_output->logical.height = height;
1394 }
1395 
1396 static void
handle_xdg_output_v1_done(void * data,struct zxdg_output_v1 * output)1397 handle_xdg_output_v1_done(void *data, struct zxdg_output_v1 *output)
1398 {
1399 	/* Don't bother waiting for this; there's no good reason a
1400 	 * compositor will wait more than one roundtrip before sending
1401 	 * these initial events. */
1402 }
1403 
1404 static void
handle_xdg_output_v1_name(void * data,struct zxdg_output_v1 * output,const char * name)1405 handle_xdg_output_v1_name(void *data, struct zxdg_output_v1 *output,
1406                           const char *name)
1407 {
1408 	struct xdg_output_v1_info *xdg_output = data;
1409 	xdg_output->name = strdup(name);
1410 }
1411 
1412 static void
handle_xdg_output_v1_description(void * data,struct zxdg_output_v1 * output,const char * description)1413 handle_xdg_output_v1_description(void *data, struct zxdg_output_v1 *output,
1414                           const char *description)
1415 {
1416 	struct xdg_output_v1_info *xdg_output = data;
1417 	xdg_output->description = strdup(description);
1418 }
1419 
1420 static const struct zxdg_output_v1_listener xdg_output_v1_listener = {
1421 	.logical_position = handle_xdg_output_v1_logical_position,
1422 	.logical_size = handle_xdg_output_v1_logical_size,
1423 	.done = handle_xdg_output_v1_done,
1424 	.name = handle_xdg_output_v1_name,
1425 	.description = handle_xdg_output_v1_description,
1426 };
1427 
1428 static void
add_xdg_output_v1_info(struct xdg_output_manager_v1_info * manager_info,struct output_info * output)1429 add_xdg_output_v1_info(struct xdg_output_manager_v1_info *manager_info,
1430                        struct output_info *output)
1431 {
1432 	struct xdg_output_v1_info *xdg_output = xzalloc(sizeof *xdg_output);
1433 
1434 	wl_list_insert(&manager_info->outputs, &xdg_output->link);
1435 	xdg_output->xdg_output = zxdg_output_manager_v1_get_xdg_output(
1436 		manager_info->manager, output->output);
1437 	zxdg_output_v1_add_listener(xdg_output->xdg_output,
1438 		&xdg_output_v1_listener, xdg_output);
1439 
1440 	xdg_output->output = output;
1441 
1442 	manager_info->info->roundtrip_needed = true;
1443 }
1444 
1445 static void
add_xdg_output_manager_v1_info(struct weston_info * info,uint32_t id,uint32_t version)1446 add_xdg_output_manager_v1_info(struct weston_info *info, uint32_t id,
1447                                uint32_t version)
1448 {
1449 	struct output_info *output;
1450 	struct xdg_output_manager_v1_info *manager = xzalloc(sizeof *manager);
1451 
1452 	wl_list_init(&manager->outputs);
1453 	manager->info = info;
1454 
1455 	init_global_info(info, &manager->global, id,
1456 		zxdg_output_manager_v1_interface.name, version);
1457 	manager->global.print = print_xdg_output_manager_v1_info;
1458 	manager->global.destroy = destroy_xdg_output_manager_v1_info;
1459 
1460 	manager->manager = wl_registry_bind(info->registry, id,
1461 		&zxdg_output_manager_v1_interface, version > 2 ? 2 : version);
1462 
1463 	wl_list_for_each(output, &info->outputs, global_link)
1464 		add_xdg_output_v1_info(manager, output);
1465 
1466 	info->xdg_output_manager_v1_info = manager;
1467 }
1468 
1469 static void
add_seat_info(struct weston_info * info,uint32_t id,uint32_t version)1470 add_seat_info(struct weston_info *info, uint32_t id, uint32_t version)
1471 {
1472 	struct seat_info *seat = xzalloc(sizeof *seat);
1473 
1474 	/* required to set roundtrip_needed to true in capabilities
1475 	 * handler */
1476 	seat->info = info;
1477 
1478 	init_global_info(info, &seat->global, id, "wl_seat", version);
1479 	seat->global.print = print_seat_info;
1480 	seat->global.destroy = destroy_seat_info;
1481 
1482 	seat->seat = wl_registry_bind(info->registry,
1483 				      id, &wl_seat_interface, MIN(version, 4));
1484 	wl_seat_add_listener(seat->seat, &seat_listener, seat);
1485 
1486 	seat->repeat_rate = seat->repeat_delay = -1;
1487 
1488 	info->roundtrip_needed = true;
1489 	wl_list_insert(&info->seats, &seat->global_link);
1490 
1491 	if (info->tablet_info) {
1492 		add_tablet_seat_info(info->tablet_info, seat);
1493 	}
1494 }
1495 
1496 static void
shm_handle_format(void * data,struct wl_shm * wl_shm,uint32_t format)1497 shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
1498 {
1499 	struct shm_info *shm = data;
1500 	struct shm_format *shm_format = xzalloc(sizeof *shm_format);
1501 
1502 	wl_list_insert(&shm->formats, &shm_format->link);
1503 	shm_format->format = format;
1504 }
1505 
1506 static const struct wl_shm_listener shm_listener = {
1507 	shm_handle_format,
1508 };
1509 
1510 static void
destroy_shm_info(void * data)1511 destroy_shm_info(void *data)
1512 {
1513 	struct shm_info *shm = data;
1514 	struct shm_format *format, *tmp;
1515 
1516 	wl_list_for_each_safe(format, tmp, &shm->formats, link) {
1517 		wl_list_remove(&format->link);
1518 		free(format);
1519 	}
1520 
1521 	wl_shm_destroy(shm->shm);
1522 }
1523 
1524 static void
add_shm_info(struct weston_info * info,uint32_t id,uint32_t version)1525 add_shm_info(struct weston_info *info, uint32_t id, uint32_t version)
1526 {
1527 	struct shm_info *shm = xzalloc(sizeof *shm);
1528 
1529 	init_global_info(info, &shm->global, id, "wl_shm", version);
1530 	shm->global.print = print_shm_info;
1531 	shm->global.destroy = destroy_shm_info;
1532 
1533 	wl_list_init(&shm->formats);
1534 
1535 	shm->shm = wl_registry_bind(info->registry,
1536 				    id, &wl_shm_interface, 1);
1537 	wl_shm_add_listener(shm->shm, &shm_listener, shm);
1538 
1539 	info->roundtrip_needed = true;
1540 }
1541 
1542 static void
linux_dmabuf_handle_format(void * data,struct zwp_linux_dmabuf_v1 * zwp_linux_dmabuf_v1,uint32_t format)1543 linux_dmabuf_handle_format(void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf_v1, uint32_t format)
1544 {
1545 	/* This is a deprecated event, don’t use it. */
1546 }
1547 
1548 static void
linux_dmabuf_handle_modifier(void * data,struct zwp_linux_dmabuf_v1 * zwp_linux_dmabuf_v1,uint32_t format,uint32_t modifier_hi,uint32_t modifier_lo)1549 linux_dmabuf_handle_modifier(void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf_v1, uint32_t format, uint32_t modifier_hi, uint32_t modifier_lo)
1550 {
1551 	struct linux_dmabuf_info *dmabuf = data;
1552 	struct linux_dmabuf_modifier *linux_dmabuf_modifier = xzalloc(sizeof *linux_dmabuf_modifier);
1553 
1554 	wl_list_insert(&dmabuf->modifiers, &linux_dmabuf_modifier->link);
1555 	linux_dmabuf_modifier->format = format;
1556 	linux_dmabuf_modifier->modifier = ((uint64_t)modifier_hi) << 32 | modifier_lo;
1557 }
1558 
1559 static const struct zwp_linux_dmabuf_v1_listener linux_dmabuf_listener = {
1560 	linux_dmabuf_handle_format,
1561 	linux_dmabuf_handle_modifier,
1562 };
1563 
1564 static void
destroy_linux_dmabuf_info(void * data)1565 destroy_linux_dmabuf_info(void *data)
1566 {
1567 	struct linux_dmabuf_info *dmabuf = data;
1568 	struct linux_dmabuf_modifier *modifier, *tmp;
1569 
1570 	wl_list_for_each_safe(modifier, tmp, &dmabuf->modifiers, link) {
1571 		wl_list_remove(&modifier->link);
1572 		free(modifier);
1573 	}
1574 
1575 	zwp_linux_dmabuf_v1_destroy(dmabuf->dmabuf);
1576 }
1577 
1578 static void
add_linux_dmabuf_info(struct weston_info * info,uint32_t id,uint32_t version)1579 add_linux_dmabuf_info(struct weston_info *info, uint32_t id, uint32_t version)
1580 {
1581 	struct linux_dmabuf_info *dmabuf = xzalloc(sizeof *dmabuf);
1582 
1583 	init_global_info(info, &dmabuf->global, id, "zwp_linux_dmabuf_v1", version);
1584 	dmabuf->global.print = print_linux_dmabuf_info;
1585 	dmabuf->global.destroy = destroy_linux_dmabuf_info;
1586 
1587 	wl_list_init(&dmabuf->modifiers);
1588 
1589 	if (version >= 3) {
1590 		dmabuf->dmabuf = wl_registry_bind(info->registry,
1591 		                                  id, &zwp_linux_dmabuf_v1_interface, 3);
1592 		zwp_linux_dmabuf_v1_add_listener(dmabuf->dmabuf, &linux_dmabuf_listener, dmabuf);
1593 
1594 		info->roundtrip_needed = true;
1595 	}
1596 }
1597 
1598 static void
output_handle_geometry(void * data,struct wl_output * wl_output,int32_t x,int32_t y,int32_t physical_width,int32_t physical_height,int32_t subpixel,const char * make,const char * model,int32_t output_transform)1599 output_handle_geometry(void *data, struct wl_output *wl_output,
1600 		       int32_t x, int32_t y,
1601 		       int32_t physical_width, int32_t physical_height,
1602 		       int32_t subpixel,
1603 		       const char *make, const char *model,
1604 		       int32_t output_transform)
1605 {
1606 	struct output_info *output = data;
1607 
1608 	output->geometry.x = x;
1609 	output->geometry.y = y;
1610 	output->geometry.physical_width = physical_width;
1611 	output->geometry.physical_height = physical_height;
1612 	output->geometry.subpixel = subpixel;
1613 	output->geometry.make = xstrdup(make);
1614 	output->geometry.model = xstrdup(model);
1615 	output->geometry.output_transform = output_transform;
1616 }
1617 
1618 static void
output_handle_mode(void * data,struct wl_output * wl_output,uint32_t flags,int32_t width,int32_t height,int32_t refresh)1619 output_handle_mode(void *data, struct wl_output *wl_output,
1620 		   uint32_t flags, int32_t width, int32_t height,
1621 		   int32_t refresh)
1622 {
1623 	struct output_info *output = data;
1624 	struct output_mode *mode = xmalloc(sizeof *mode);
1625 
1626 	mode->flags = flags;
1627 	mode->width = width;
1628 	mode->height = height;
1629 	mode->refresh = refresh;
1630 
1631 	wl_list_insert(output->modes.prev, &mode->link);
1632 }
1633 
1634 static void
output_handle_done(void * data,struct wl_output * wl_output)1635 output_handle_done(void *data, struct wl_output *wl_output)
1636 {
1637 	/* don't bother waiting for this; there's no good reason a
1638 	 * compositor will wait more than one roundtrip before sending
1639 	 * these initial events. */
1640 }
1641 
1642 static void
output_handle_scale(void * data,struct wl_output * wl_output,int32_t scale)1643 output_handle_scale(void *data, struct wl_output *wl_output,
1644 		    int32_t scale)
1645 {
1646 	struct output_info *output = data;
1647 
1648 	output->geometry.scale = scale;
1649 }
1650 
1651 static const struct wl_output_listener output_listener = {
1652 	output_handle_geometry,
1653 	output_handle_mode,
1654 	output_handle_done,
1655 	output_handle_scale,
1656 };
1657 
1658 static void
destroy_output_info(void * data)1659 destroy_output_info(void *data)
1660 {
1661 	struct output_info *output = data;
1662 	struct output_mode *mode, *tmp;
1663 
1664 	wl_output_destroy(output->output);
1665 
1666 	if (output->geometry.make != NULL)
1667 		free(output->geometry.make);
1668 	if (output->geometry.model != NULL)
1669 		free(output->geometry.model);
1670 
1671 	wl_list_for_each_safe(mode, tmp, &output->modes, link) {
1672 		wl_list_remove(&mode->link);
1673 		free(mode);
1674 	}
1675 }
1676 
1677 static void
add_output_info(struct weston_info * info,uint32_t id,uint32_t version)1678 add_output_info(struct weston_info *info, uint32_t id, uint32_t version)
1679 {
1680 	struct output_info *output = xzalloc(sizeof *output);
1681 
1682 	init_global_info(info, &output->global, id, "wl_output", version);
1683 	output->global.print = print_output_info;
1684 	output->global.destroy = destroy_output_info;
1685 
1686 	output->version = MIN(version, 2);
1687 	output->geometry.scale = 1;
1688 	wl_list_init(&output->modes);
1689 
1690 	output->output = wl_registry_bind(info->registry, id,
1691 					  &wl_output_interface, output->version);
1692 	wl_output_add_listener(output->output, &output_listener,
1693 			       output);
1694 
1695 	info->roundtrip_needed = true;
1696 	wl_list_insert(&info->outputs, &output->global_link);
1697 
1698 	if (info->xdg_output_manager_v1_info)
1699 		add_xdg_output_v1_info(info->xdg_output_manager_v1_info,
1700 				       output);
1701 }
1702 
1703 static void
destroy_presentation_info(void * info)1704 destroy_presentation_info(void *info)
1705 {
1706 	struct presentation_info *prinfo = info;
1707 
1708 	wp_presentation_destroy(prinfo->presentation);
1709 }
1710 
1711 static const char *
clock_name(clockid_t clk_id)1712 clock_name(clockid_t clk_id)
1713 {
1714 	static const char *names[] = {
1715 		[CLOCK_REALTIME] =		"CLOCK_REALTIME",
1716 		[CLOCK_MONOTONIC] =		"CLOCK_MONOTONIC",
1717 		[CLOCK_MONOTONIC_RAW] =		"CLOCK_MONOTONIC_RAW",
1718 		[CLOCK_REALTIME_COARSE] =	"CLOCK_REALTIME_COARSE",
1719 		[CLOCK_MONOTONIC_COARSE] =	"CLOCK_MONOTONIC_COARSE",
1720 #ifdef CLOCK_BOOTTIME
1721 		[CLOCK_BOOTTIME] =		"CLOCK_BOOTTIME",
1722 #endif
1723 	};
1724 
1725 	if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
1726 		return "unknown";
1727 
1728 	return names[clk_id];
1729 }
1730 
1731 static void
print_presentation_info(void * info)1732 print_presentation_info(void *info)
1733 {
1734 	struct presentation_info *prinfo = info;
1735 
1736 	print_global_info(info);
1737 
1738 	printf("\tpresentation clock id: %d (%s)\n",
1739 		prinfo->clk_id, clock_name(prinfo->clk_id));
1740 }
1741 
1742 static void
presentation_handle_clock_id(void * data,struct wp_presentation * presentation,uint32_t clk_id)1743 presentation_handle_clock_id(void *data, struct wp_presentation *presentation,
1744 			     uint32_t clk_id)
1745 {
1746 	struct presentation_info *prinfo = data;
1747 
1748 	prinfo->clk_id = clk_id;
1749 }
1750 
1751 static const struct wp_presentation_listener presentation_listener = {
1752 	presentation_handle_clock_id
1753 };
1754 
1755 static void
add_presentation_info(struct weston_info * info,uint32_t id,uint32_t version)1756 add_presentation_info(struct weston_info *info, uint32_t id, uint32_t version)
1757 {
1758 	struct presentation_info *prinfo = xzalloc(sizeof *prinfo);
1759 
1760 	init_global_info(info, &prinfo->global, id,
1761 			 wp_presentation_interface.name, version);
1762 	prinfo->global.print = print_presentation_info;
1763 	prinfo->global.destroy = destroy_presentation_info;
1764 
1765 	prinfo->clk_id = -1;
1766 	prinfo->presentation = wl_registry_bind(info->registry, id,
1767 						&wp_presentation_interface, 1);
1768 	wp_presentation_add_listener(prinfo->presentation,
1769 				     &presentation_listener, prinfo);
1770 
1771 	info->roundtrip_needed = true;
1772 }
1773 
1774 static void
destroy_global_info(void * data)1775 destroy_global_info(void *data)
1776 {
1777 }
1778 
1779 static void
add_global_info(struct weston_info * info,uint32_t id,const char * interface,uint32_t version)1780 add_global_info(struct weston_info *info, uint32_t id,
1781 		const char *interface, uint32_t version)
1782 {
1783 	struct global_info *global = xzalloc(sizeof *global);
1784 
1785 	init_global_info(info, global, id, interface, version);
1786 	global->print = print_global_info;
1787 	global->destroy = destroy_global_info;
1788 }
1789 
1790 static void
global_handler(void * data,struct wl_registry * registry,uint32_t id,const char * interface,uint32_t version)1791 global_handler(void *data, struct wl_registry *registry, uint32_t id,
1792 	       const char *interface, uint32_t version)
1793 {
1794 	struct weston_info *info = data;
1795 
1796 	if (!strcmp(interface, "wl_seat"))
1797 		add_seat_info(info, id, version);
1798 	else if (!strcmp(interface, "wl_shm"))
1799 		add_shm_info(info, id, version);
1800 	else if (!strcmp(interface, "zwp_linux_dmabuf_v1"))
1801 		add_linux_dmabuf_info(info, id, version);
1802 	else if (!strcmp(interface, "wl_output"))
1803 		add_output_info(info, id, version);
1804 	else if (!strcmp(interface, wp_presentation_interface.name))
1805 		add_presentation_info(info, id, version);
1806 	else if (!strcmp(interface, zwp_tablet_manager_v2_interface.name))
1807 		add_tablet_v2_info(info, id, version);
1808 	else if (!strcmp(interface, zxdg_output_manager_v1_interface.name))
1809 		add_xdg_output_manager_v1_info(info, id, version);
1810 	else
1811 		add_global_info(info, id, interface, version);
1812 }
1813 
1814 static void
global_remove_handler(void * data,struct wl_registry * registry,uint32_t name)1815 global_remove_handler(void *data, struct wl_registry *registry, uint32_t name)
1816 {
1817 }
1818 
1819 static const struct wl_registry_listener registry_listener = {
1820 	global_handler,
1821 	global_remove_handler
1822 };
1823 
1824 static void
print_infos(struct wl_list * infos)1825 print_infos(struct wl_list *infos)
1826 {
1827 	struct global_info *info;
1828 
1829 	wl_list_for_each(info, infos, link)
1830 		info->print(info);
1831 }
1832 
1833 static void
destroy_info(void * data)1834 destroy_info(void *data)
1835 {
1836 	struct global_info *global = data;
1837 
1838 	global->destroy(data);
1839 	wl_list_remove(&global->link);
1840 	free(global->interface);
1841 	free(data);
1842 }
1843 
1844 static void
destroy_infos(struct wl_list * infos)1845 destroy_infos(struct wl_list *infos)
1846 {
1847 	struct global_info *info, *tmp;
1848 	wl_list_for_each_safe(info, tmp, infos, link)
1849 		destroy_info(info);
1850 }
1851 
1852 int
main(int argc,char ** argv)1853 main(int argc, char **argv)
1854 {
1855 	struct weston_info info;
1856 
1857 	info.display = wl_display_connect(NULL);
1858 	if (!info.display) {
1859 		fprintf(stderr, "failed to create display: %s\n",
1860 			strerror(errno));
1861 		return -1;
1862 	}
1863 
1864 	fprintf(stderr, "\n");
1865 	fprintf(stderr, "*** Please use wayland-info instead\n");
1866 	fprintf(stderr, "*** weston-info is deprecated and will be removed in a future version\n");
1867 	fprintf(stderr, "\n");
1868 
1869 	info.tablet_info = NULL;
1870 	info.xdg_output_manager_v1_info = NULL;
1871 	wl_list_init(&info.infos);
1872 	wl_list_init(&info.seats);
1873 	wl_list_init(&info.outputs);
1874 
1875 	info.registry = wl_display_get_registry(info.display);
1876 	wl_registry_add_listener(info.registry, &registry_listener, &info);
1877 
1878 	do {
1879 		info.roundtrip_needed = false;
1880 		wl_display_roundtrip(info.display);
1881 	} while (info.roundtrip_needed);
1882 
1883 	print_infos(&info.infos);
1884 	destroy_infos(&info.infos);
1885 
1886 	wl_registry_destroy(info.registry);
1887 	wl_display_disconnect(info.display);
1888 
1889 	return 0;
1890 }
1891