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 <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32 
33 #include <wayland-client.h>
34 
35 #include "shared/helpers.h"
36 #include "shared/os-compatibility.h"
37 #include "presentation_timing-client-protocol.h"
38 
39 typedef void (*print_info_t)(void *info);
40 typedef void (*destroy_info_t)(void *info);
41 
42 struct global_info {
43 	struct wl_list link;
44 
45 	uint32_t id;
46 	uint32_t version;
47 	char *interface;
48 
49 	print_info_t print;
50 	destroy_info_t destroy;
51 };
52 
53 struct output_mode {
54 	struct wl_list link;
55 
56 	uint32_t flags;
57 	int32_t width, height;
58 	int32_t refresh;
59 };
60 
61 struct output_info {
62 	struct global_info global;
63 
64 	struct wl_output *output;
65 
66 	struct {
67 		int32_t x, y;
68 		int32_t physical_width, physical_height;
69 		enum wl_output_subpixel subpixel;
70 		enum wl_output_transform output_transform;
71 		char *make;
72 		char *model;
73 	} geometry;
74 
75 	struct wl_list modes;
76 };
77 
78 struct shm_format {
79 	struct wl_list link;
80 
81 	uint32_t format;
82 };
83 
84 struct shm_info {
85 	struct global_info global;
86 	struct wl_shm *shm;
87 
88 	struct wl_list formats;
89 };
90 
91 struct seat_info {
92 	struct global_info global;
93 	struct wl_seat *seat;
94 	struct weston_info *info;
95 
96 	uint32_t capabilities;
97 	char *name;
98 
99 	int32_t repeat_rate;
100 	int32_t repeat_delay;
101 };
102 
103 struct presentation_info {
104 	struct global_info global;
105 	struct presentation *presentation;
106 
107 	clockid_t clk_id;
108 };
109 
110 struct weston_info {
111 	struct wl_display *display;
112 	struct wl_registry *registry;
113 
114 	struct wl_list infos;
115 	bool roundtrip_needed;
116 };
117 
118 static void *
fail_on_null(void * p)119 fail_on_null(void *p)
120 {
121 	if (p == NULL) {
122 		fprintf(stderr, "%s: out of memory\n", getprogname());
123 		exit(EXIT_FAILURE);
124 	}
125 
126 	return p;
127 }
128 
129 static void *
xmalloc(size_t s)130 xmalloc(size_t s)
131 {
132 	return fail_on_null(malloc(s));
133 }
134 
135 static void *
xzalloc(size_t s)136 xzalloc(size_t s)
137 {
138 	return fail_on_null(calloc(1, s));
139 }
140 
141 static char *
xstrdup(const char * s)142 xstrdup(const char *s)
143 {
144 	return fail_on_null(strdup(s));
145 }
146 
147 static void
print_global_info(void * data)148 print_global_info(void *data)
149 {
150 	struct global_info *global = data;
151 
152 	printf("interface: '%s', version: %u, name: %u\n",
153 	       global->interface, global->version, global->id);
154 }
155 
156 static void
init_global_info(struct weston_info * info,struct global_info * global,uint32_t id,const char * interface,uint32_t version)157 init_global_info(struct weston_info *info,
158 		 struct global_info *global, uint32_t id,
159 		 const char *interface, uint32_t version)
160 {
161 	global->id = id;
162 	global->version = version;
163 	global->interface = xstrdup(interface);
164 
165 	wl_list_insert(info->infos.prev, &global->link);
166 }
167 
168 static void
print_output_info(void * data)169 print_output_info(void *data)
170 {
171 	struct output_info *output = data;
172 	struct output_mode *mode;
173 	const char *subpixel_orientation;
174 	const char *transform;
175 
176 	print_global_info(data);
177 
178 	switch (output->geometry.subpixel) {
179 	case WL_OUTPUT_SUBPIXEL_UNKNOWN:
180 		subpixel_orientation = "unknown";
181 		break;
182 	case WL_OUTPUT_SUBPIXEL_NONE:
183 		subpixel_orientation = "none";
184 		break;
185 	case WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB:
186 		subpixel_orientation = "horizontal rgb";
187 		break;
188 	case WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR:
189 		subpixel_orientation = "horizontal bgr";
190 		break;
191 	case WL_OUTPUT_SUBPIXEL_VERTICAL_RGB:
192 		subpixel_orientation = "vertical rgb";
193 		break;
194 	case WL_OUTPUT_SUBPIXEL_VERTICAL_BGR:
195 		subpixel_orientation = "vertical bgr";
196 		break;
197 	default:
198 		fprintf(stderr, "unknown subpixel orientation %u\n",
199 			output->geometry.subpixel);
200 		subpixel_orientation = "unexpected value";
201 		break;
202 	}
203 
204 	switch (output->geometry.output_transform) {
205 	case WL_OUTPUT_TRANSFORM_NORMAL:
206 		transform = "normal";
207 		break;
208 	case WL_OUTPUT_TRANSFORM_90:
209 		transform = "90°";
210 		break;
211 	case WL_OUTPUT_TRANSFORM_180:
212 		transform = "180°";
213 		break;
214 	case WL_OUTPUT_TRANSFORM_270:
215 		transform = "270°";
216 		break;
217 	case WL_OUTPUT_TRANSFORM_FLIPPED:
218 		transform = "flipped";
219 		break;
220 	case WL_OUTPUT_TRANSFORM_FLIPPED_90:
221 		transform = "flipped 90°";
222 		break;
223 	case WL_OUTPUT_TRANSFORM_FLIPPED_180:
224 		transform = "flipped 180°";
225 		break;
226 	case WL_OUTPUT_TRANSFORM_FLIPPED_270:
227 		transform = "flipped 270°";
228 		break;
229 	default:
230 		fprintf(stderr, "unknown output transform %u\n",
231 			output->geometry.output_transform);
232 		transform = "unexpected value";
233 		break;
234 	}
235 
236 	printf("\tx: %d, y: %d,\n",
237 	       output->geometry.x, output->geometry.y);
238 	printf("\tphysical_width: %d mm, physical_height: %d mm,\n",
239 	       output->geometry.physical_width,
240 	       output->geometry.physical_height);
241 	printf("\tmake: '%s', model: '%s',\n",
242 	       output->geometry.make, output->geometry.model);
243 	printf("\tsubpixel_orientation: %s, output_transform: %s,\n",
244 	       subpixel_orientation, transform);
245 
246 	wl_list_for_each(mode, &output->modes, link) {
247 		printf("\tmode:\n");
248 
249 		printf("\t\twidth: %d px, height: %d px, refresh: %.f Hz,\n",
250 		       mode->width, mode->height,
251 		       (float) mode->refresh / 1000);
252 
253 		printf("\t\tflags:");
254 		if (mode->flags & WL_OUTPUT_MODE_CURRENT)
255 			printf(" current");
256 		if (mode->flags & WL_OUTPUT_MODE_PREFERRED)
257 			printf(" preferred");
258 		printf("\n");
259 	}
260 }
261 
262 static void
print_shm_info(void * data)263 print_shm_info(void *data)
264 {
265 	struct shm_info *shm = data;
266 	struct shm_format *format;
267 
268 	print_global_info(data);
269 
270 	printf("\tformats:");
271 
272 	wl_list_for_each(format, &shm->formats, link)
273 		switch (format->format) {
274 		case WL_SHM_FORMAT_ARGB8888:
275 			printf(" ARGB8888");
276 			break;
277 		case WL_SHM_FORMAT_XRGB8888:
278 			printf(" XRGB8888");
279 			break;
280 		case WL_SHM_FORMAT_RGB565:
281 			printf(" RGB565");
282 			break;
283 		default:
284 			printf(" unknown(%08x)", format->format);
285 			break;
286 		}
287 
288 	printf("\n");
289 }
290 
291 static void
print_seat_info(void * data)292 print_seat_info(void *data)
293 {
294 	struct seat_info *seat = data;
295 
296 	print_global_info(data);
297 
298 	printf("\tname: %s\n", seat->name);
299 	printf("\tcapabilities:");
300 
301 	if (seat->capabilities & WL_SEAT_CAPABILITY_POINTER)
302 		printf(" pointer");
303 	if (seat->capabilities & WL_SEAT_CAPABILITY_KEYBOARD)
304 		printf(" keyboard");
305 	if (seat->capabilities & WL_SEAT_CAPABILITY_TOUCH)
306 		printf(" touch");
307 
308 	printf("\n");
309 
310 	if (seat->repeat_rate > 0)
311 		printf("\tkeyboard repeat rate: %d\n", seat->repeat_rate);
312 	if (seat->repeat_delay > 0)
313 		printf("\tkeyboard repeat delay: %d\n", seat->repeat_delay);
314 }
315 
316 static void
keyboard_handle_keymap(void * data,struct wl_keyboard * keyboard,uint32_t format,int fd,uint32_t size)317 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
318 		       uint32_t format, int fd, uint32_t size)
319 {
320 }
321 
322 static void
keyboard_handle_enter(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface,struct wl_array * keys)323 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
324 		      uint32_t serial, struct wl_surface *surface,
325 		      struct wl_array *keys)
326 {
327 }
328 
329 static void
keyboard_handle_leave(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface)330 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
331 		      uint32_t serial, struct wl_surface *surface)
332 {
333 }
334 
335 static void
keyboard_handle_key(void * data,struct wl_keyboard * keyboard,uint32_t serial,uint32_t time,uint32_t key,uint32_t state)336 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
337 		    uint32_t serial, uint32_t time, uint32_t key,
338 		    uint32_t state)
339 {
340 }
341 
342 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)343 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
344 			  uint32_t serial, uint32_t mods_depressed,
345 			  uint32_t mods_latched, uint32_t mods_locked,
346 			  uint32_t group)
347 {
348 }
349 
350 static void
keyboard_handle_repeat_info(void * data,struct wl_keyboard * keyboard,int32_t rate,int32_t delay)351 keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
352 			    int32_t rate, int32_t delay)
353 {
354 	struct seat_info *seat = data;
355 
356 	seat->repeat_rate = rate;
357 	seat->repeat_delay = delay;
358 }
359 
360 static const struct wl_keyboard_listener keyboard_listener = {
361 	keyboard_handle_keymap,
362 	keyboard_handle_enter,
363 	keyboard_handle_leave,
364 	keyboard_handle_key,
365 	keyboard_handle_modifiers,
366 	keyboard_handle_repeat_info,
367 };
368 
369 static void
seat_handle_capabilities(void * data,struct wl_seat * wl_seat,enum wl_seat_capability caps)370 seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
371 			 enum wl_seat_capability caps)
372 {
373 	struct seat_info *seat = data;
374 
375 	seat->capabilities = caps;
376 
377 	/* we want listen for repeat_info from wl_keyboard, but only
378 	 * do so if the seat info is >= 4 and if we actually have a
379 	 * keyboard */
380 	if (seat->global.version < 4)
381 		return;
382 
383 	if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
384 		struct wl_keyboard *keyboard;
385 
386 		keyboard = wl_seat_get_keyboard(seat->seat);
387 		wl_keyboard_add_listener(keyboard, &keyboard_listener,
388 					 seat);
389 
390 		seat->info->roundtrip_needed = true;
391 	}
392 }
393 
394 static void
seat_handle_name(void * data,struct wl_seat * wl_seat,const char * name)395 seat_handle_name(void *data, struct wl_seat *wl_seat,
396 		 const char *name)
397 {
398 	struct seat_info *seat = data;
399 	seat->name = xstrdup(name);
400 }
401 
402 static const struct wl_seat_listener seat_listener = {
403 	seat_handle_capabilities,
404 	seat_handle_name,
405 };
406 
407 static void
destroy_seat_info(void * data)408 destroy_seat_info(void *data)
409 {
410 	struct seat_info *seat = data;
411 
412 	wl_seat_destroy(seat->seat);
413 
414 	if (seat->name != NULL)
415 		free(seat->name);
416 }
417 
418 static void
add_seat_info(struct weston_info * info,uint32_t id,uint32_t version)419 add_seat_info(struct weston_info *info, uint32_t id, uint32_t version)
420 {
421 	struct seat_info *seat = xzalloc(sizeof *seat);
422 
423 	/* required to set roundtrip_needed to true in capabilities
424 	 * handler */
425 	seat->info = info;
426 
427 	init_global_info(info, &seat->global, id, "wl_seat", version);
428 	seat->global.print = print_seat_info;
429 	seat->global.destroy = destroy_seat_info;
430 
431 	seat->seat = wl_registry_bind(info->registry,
432 				      id, &wl_seat_interface, MIN(version, 4));
433 	wl_seat_add_listener(seat->seat, &seat_listener, seat);
434 
435 	seat->repeat_rate = seat->repeat_delay = -1;
436 
437 	info->roundtrip_needed = true;
438 }
439 
440 static void
shm_handle_format(void * data,struct wl_shm * wl_shm,uint32_t format)441 shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
442 {
443 	struct shm_info *shm = data;
444 	struct shm_format *shm_format = xzalloc(sizeof *shm_format);
445 
446 	wl_list_insert(&shm->formats, &shm_format->link);
447 	shm_format->format = format;
448 }
449 
450 static const struct wl_shm_listener shm_listener = {
451 	shm_handle_format,
452 };
453 
454 static void
destroy_shm_info(void * data)455 destroy_shm_info(void *data)
456 {
457 	struct shm_info *shm = data;
458 	struct shm_format *format, *tmp;
459 
460 	wl_list_for_each_safe(format, tmp, &shm->formats, link) {
461 		wl_list_remove(&format->link);
462 		free(format);
463 	}
464 
465 	wl_shm_destroy(shm->shm);
466 }
467 
468 static void
add_shm_info(struct weston_info * info,uint32_t id,uint32_t version)469 add_shm_info(struct weston_info *info, uint32_t id, uint32_t version)
470 {
471 	struct shm_info *shm = xzalloc(sizeof *shm);
472 
473 	init_global_info(info, &shm->global, id, "wl_shm", version);
474 	shm->global.print = print_shm_info;
475 	shm->global.destroy = destroy_shm_info;
476 
477 	wl_list_init(&shm->formats);
478 
479 	shm->shm = wl_registry_bind(info->registry,
480 				    id, &wl_shm_interface, 1);
481 	wl_shm_add_listener(shm->shm, &shm_listener, shm);
482 
483 	info->roundtrip_needed = true;
484 }
485 
486 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)487 output_handle_geometry(void *data, struct wl_output *wl_output,
488 		       int32_t x, int32_t y,
489 		       int32_t physical_width, int32_t physical_height,
490 		       int32_t subpixel,
491 		       const char *make, const char *model,
492 		       int32_t output_transform)
493 {
494 	struct output_info *output = data;
495 
496 	output->geometry.x = x;
497 	output->geometry.y = y;
498 	output->geometry.physical_width = physical_width;
499 	output->geometry.physical_height = physical_height;
500 	output->geometry.subpixel = subpixel;
501 	output->geometry.make = xstrdup(make);
502 	output->geometry.model = xstrdup(model);
503 	output->geometry.output_transform = output_transform;
504 }
505 
506 static void
output_handle_mode(void * data,struct wl_output * wl_output,uint32_t flags,int32_t width,int32_t height,int32_t refresh)507 output_handle_mode(void *data, struct wl_output *wl_output,
508 		   uint32_t flags, int32_t width, int32_t height,
509 		   int32_t refresh)
510 {
511 	struct output_info *output = data;
512 	struct output_mode *mode = xmalloc(sizeof *mode);
513 
514 	mode->flags = flags;
515 	mode->width = width;
516 	mode->height = height;
517 	mode->refresh = refresh;
518 
519 	wl_list_insert(output->modes.prev, &mode->link);
520 }
521 
522 static const struct wl_output_listener output_listener = {
523 	output_handle_geometry,
524 	output_handle_mode,
525 };
526 
527 static void
destroy_output_info(void * data)528 destroy_output_info(void *data)
529 {
530 	struct output_info *output = data;
531 	struct output_mode *mode, *tmp;
532 
533 	wl_output_destroy(output->output);
534 
535 	if (output->geometry.make != NULL)
536 		free(output->geometry.make);
537 	if (output->geometry.model != NULL)
538 		free(output->geometry.model);
539 
540 	wl_list_for_each_safe(mode, tmp, &output->modes, link) {
541 		wl_list_remove(&mode->link);
542 		free(mode);
543 	}
544 }
545 
546 static void
add_output_info(struct weston_info * info,uint32_t id,uint32_t version)547 add_output_info(struct weston_info *info, uint32_t id, uint32_t version)
548 {
549 	struct output_info *output = xzalloc(sizeof *output);
550 
551 	init_global_info(info, &output->global, id, "wl_output", version);
552 	output->global.print = print_output_info;
553 	output->global.destroy = destroy_output_info;
554 
555 	wl_list_init(&output->modes);
556 
557 	output->output = wl_registry_bind(info->registry, id,
558 					  &wl_output_interface, 1);
559 	wl_output_add_listener(output->output, &output_listener,
560 			       output);
561 
562 	info->roundtrip_needed = true;
563 }
564 
565 static void
destroy_presentation_info(void * info)566 destroy_presentation_info(void *info)
567 {
568 	struct presentation_info *prinfo = info;
569 
570 	presentation_destroy(prinfo->presentation);
571 }
572 
573 static const char *
clock_name(clockid_t clk_id)574 clock_name(clockid_t clk_id)
575 {
576 	static const char *names[] = {
577 		[CLOCK_REALTIME] =		"CLOCK_REALTIME",
578 		[CLOCK_MONOTONIC] =		"CLOCK_MONOTONIC",
579 #if defined(__FreeBSD__)
580 		[CLOCK_REALTIME_FAST] =	"CLOCK_REALTIME_COARSE",
581 		[CLOCK_MONOTONIC_FAST] =	"CLOCK_MONOTONIC_COARSE",
582 #else
583 		[CLOCK_MONOTONIC_RAW] =		"CLOCK_MONOTONIC_RAW",
584 		[CLOCK_REALTIME_COARSE] =	"CLOCK_REALTIME_COARSE",
585 		[CLOCK_MONOTONIC_COARSE] =	"CLOCK_MONOTONIC_COARSE",
586 		[CLOCK_BOOTTIME] =		"CLOCK_BOOTTIME",
587 #endif
588 	};
589 
590 #if defined(__FreeBSD__)
591 	if ((unsigned)clk_id >= ARRAY_LENGTH(names))
592 #else
593 	if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
594 #endif
595 		return "unknown";
596 
597 	return names[clk_id];
598 }
599 
600 static void
print_presentation_info(void * info)601 print_presentation_info(void *info)
602 {
603 	struct presentation_info *prinfo = info;
604 
605 	print_global_info(info);
606 
607 #if defined(__FreeBSD__)
608 	printf("\tpresentation clock id: %ld (%s)\n",
609 #else
610 	printf("\tpresentation clock id: %d (%s)\n",
611 #endif
612 		prinfo->clk_id, clock_name(prinfo->clk_id));
613 }
614 
615 static void
presentation_handle_clock_id(void * data,struct presentation * presentation,uint32_t clk_id)616 presentation_handle_clock_id(void *data, struct presentation *presentation,
617 			     uint32_t clk_id)
618 {
619 	struct presentation_info *prinfo = data;
620 
621 	prinfo->clk_id = clk_id;
622 }
623 
624 static const struct presentation_listener presentation_listener = {
625 	presentation_handle_clock_id
626 };
627 
628 static void
add_presentation_info(struct weston_info * info,uint32_t id,uint32_t version)629 add_presentation_info(struct weston_info *info, uint32_t id, uint32_t version)
630 {
631 	struct presentation_info *prinfo = xzalloc(sizeof *prinfo);
632 
633 	init_global_info(info, &prinfo->global, id, "presentation", version);
634 	prinfo->global.print = print_presentation_info;
635 	prinfo->global.destroy = destroy_presentation_info;
636 
637 	prinfo->clk_id = -1;
638 	prinfo->presentation = wl_registry_bind(info->registry, id,
639 						&presentation_interface, 1);
640 	presentation_add_listener(prinfo->presentation, &presentation_listener,
641 				  prinfo);
642 
643 	info->roundtrip_needed = true;
644 }
645 
646 static void
destroy_global_info(void * data)647 destroy_global_info(void *data)
648 {
649 }
650 
651 static void
add_global_info(struct weston_info * info,uint32_t id,const char * interface,uint32_t version)652 add_global_info(struct weston_info *info, uint32_t id,
653 		const char *interface, uint32_t version)
654 {
655 	struct global_info *global = xzalloc(sizeof *global);
656 
657 	init_global_info(info, global, id, interface, version);
658 	global->print = print_global_info;
659 	global->destroy = destroy_global_info;
660 }
661 
662 static void
global_handler(void * data,struct wl_registry * registry,uint32_t id,const char * interface,uint32_t version)663 global_handler(void *data, struct wl_registry *registry, uint32_t id,
664 	       const char *interface, uint32_t version)
665 {
666 	struct weston_info *info = data;
667 
668 	if (!strcmp(interface, "wl_seat"))
669 		add_seat_info(info, id, version);
670 	else if (!strcmp(interface, "wl_shm"))
671 		add_shm_info(info, id, version);
672 	else if (!strcmp(interface, "wl_output"))
673 		add_output_info(info, id, version);
674 	else if (!strcmp(interface, "presentation"))
675 		add_presentation_info(info, id, version);
676 	else
677 		add_global_info(info, id, interface, version);
678 }
679 
680 static void
global_remove_handler(void * data,struct wl_registry * registry,uint32_t name)681 global_remove_handler(void *data, struct wl_registry *registry, uint32_t name)
682 {
683 }
684 
685 static const struct wl_registry_listener registry_listener = {
686 	global_handler,
687 	global_remove_handler
688 };
689 
690 static void
print_infos(struct wl_list * infos)691 print_infos(struct wl_list *infos)
692 {
693 	struct global_info *info;
694 
695 	wl_list_for_each(info, infos, link)
696 		info->print(info);
697 }
698 
699 static void
destroy_info(void * data)700 destroy_info(void *data)
701 {
702 	struct global_info *global = data;
703 
704 	global->destroy(data);
705 	wl_list_remove(&global->link);
706 	free(global->interface);
707 	free(data);
708 }
709 
710 static void
destroy_infos(struct wl_list * infos)711 destroy_infos(struct wl_list *infos)
712 {
713 	struct global_info *info, *tmp;
714 	wl_list_for_each_safe(info, tmp, infos, link)
715 		destroy_info(info);
716 }
717 
718 int
main(int argc,char ** argv)719 main(int argc, char **argv)
720 {
721 	struct weston_info info;
722 
723 	info.display = wl_display_connect(NULL);
724 	if (!info.display) {
725 		fprintf(stderr, "failed to create display: %s\n",
726 		    strerror(errno));
727 		return -1;
728 	}
729 
730 	wl_list_init(&info.infos);
731 
732 	info.registry = wl_display_get_registry(info.display);
733 	wl_registry_add_listener(info.registry, &registry_listener, &info);
734 
735 	do {
736 		info.roundtrip_needed = false;
737 		wl_display_roundtrip(info.display);
738 	} while (info.roundtrip_needed);
739 
740 	print_infos(&info.infos);
741 	destroy_infos(&info.infos);
742 
743 	wl_registry_destroy(info.registry);
744 	wl_display_disconnect(info.display);
745 
746 	return 0;
747 }
748