weston-info.c 47.2 KB
Newer Older
1 2 3
/*
 * Copyright © 2012 Philipp Brüschweiler
 *
4 5 6 7 8 9
 * 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:
10
 *
11 12 13 14 15 16 17 18 19 20 21
 * 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.
22 23
 */

24 25 26
#include "config.h"

#include <errno.h>
27
#include <stdbool.h>
28
#include <stdint.h>
29 30 31
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
32
#include <time.h>
33 34
#include <assert.h>
#include <ctype.h>
35 36 37

#include <wayland-client.h>

38
#include "shared/helpers.h"
39
#include "shared/os-compatibility.h"
40
#include "shared/xalloc.h"
Bryce Harrington's avatar
Bryce Harrington committed
41
#include "shared/zalloc.h"
42
#include "presentation-time-client-protocol.h"
43
#include "linux-dmabuf-unstable-v1-client-protocol.h"
44
#include "tablet-unstable-v2-client-protocol.h"
45
#include "xdg-output-unstable-v1-client-protocol.h"
46

47
typedef void (*print_info_t)(void *info);
48
typedef void (*destroy_info_t)(void *info);
49 50 51 52 53 54 55 56 57

struct global_info {
	struct wl_list link;

	uint32_t id;
	uint32_t version;
	char *interface;

	print_info_t print;
58
	destroy_info_t destroy;
59 60
};

61 62 63 64 65 66 67 68
struct output_mode {
	struct wl_list link;

	uint32_t flags;
	int32_t width, height;
	int32_t refresh;
};

69 70
struct output_info {
	struct global_info global;
71
	struct wl_list global_link;
72 73 74

	struct wl_output *output;

75 76
	int32_t version;

77 78
	struct {
		int32_t x, y;
79
		int32_t scale;
80 81 82 83 84 85 86
		int32_t physical_width, physical_height;
		enum wl_output_subpixel subpixel;
		enum wl_output_transform output_transform;
		char *make;
		char *model;
	} geometry;

87
	struct wl_list modes;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
};

struct shm_format {
	struct wl_list link;

	uint32_t format;
};

struct shm_info {
	struct global_info global;
	struct wl_shm *shm;

	struct wl_list formats;
};

103 104 105 106 107 108 109 110 111 112 113 114 115 116
struct linux_dmabuf_modifier {
	struct wl_list link;

	uint32_t format;
	uint64_t modifier;
};

struct linux_dmabuf_info {
	struct global_info global;
	struct zwp_linux_dmabuf_v1 *dmabuf;

	struct wl_list modifiers;
};

117 118
struct seat_info {
	struct global_info global;
119
	struct wl_list global_link;
120
	struct wl_seat *seat;
121
	struct weston_info *info;
122

Ongy's avatar
Ongy committed
123
	struct wl_keyboard *keyboard;
124
	uint32_t capabilities;
125
	char *name;
126 127 128

	int32_t repeat_rate;
	int32_t repeat_delay;
129 130
};

131 132 133 134 135 136 137 138 139 140 141 142
struct tablet_v2_path {
	struct wl_list link;
	char *path;
};

struct tablet_tool_info {
	struct wl_list link;
	struct zwp_tablet_tool_v2 *tool;

	uint64_t hardware_serial;
	uint64_t hardware_id_wacom;
	enum zwp_tablet_tool_v2_type type;
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
	bool has_tilt;
	bool has_pressure;
	bool has_distance;
	bool has_rotation;
	bool has_slider;
	bool has_wheel;
};

struct tablet_pad_group_info {
	struct wl_list link;
	struct zwp_tablet_pad_group_v2 *group;

	uint32_t modes;
	size_t button_count;
	int *buttons;
	size_t strips;
	size_t rings;
};

struct tablet_pad_info {
	struct wl_list link;
	struct zwp_tablet_pad_v2 *pad;

	uint32_t buttons;
	struct wl_list paths;
	struct wl_list groups;
};

struct tablet_info {
	struct wl_list link;
	struct zwp_tablet_v2 *tablet;

	char *name;
	uint32_t vid, pid;
	struct wl_list paths;
};

struct tablet_seat_info {
	struct wl_list link;

	struct zwp_tablet_seat_v2 *seat;
	struct seat_info *seat_info;

	struct wl_list tablets;
	struct wl_list tools;
	struct wl_list pads;
};

struct tablet_v2_info {
	struct global_info global;
	struct zwp_tablet_manager_v2 *manager;
	struct weston_info *info;

	struct wl_list seats;
};

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
struct xdg_output_v1_info {
	struct wl_list link;

	struct zxdg_output_v1 *xdg_output;
	struct output_info *output;

	struct {
		int32_t x, y;
		int32_t width, height;
	} logical;

	char *name, *description;
};

struct xdg_output_manager_v1_info {
	struct global_info global;
	struct zxdg_output_manager_v1 *manager;
	struct weston_info *info;

	struct wl_list outputs;
};

222 223
struct presentation_info {
	struct global_info global;
224
	struct wp_presentation *presentation;
225 226 227 228

	clockid_t clk_id;
};

229 230
struct weston_info {
	struct wl_display *display;
231
	struct wl_registry *registry;
232 233

	struct wl_list infos;
234
	bool roundtrip_needed;
235 236 237 238

	/* required for tablet-unstable-v2 */
	struct wl_list seats;
	struct tablet_v2_info *tablet_info;
239 240 241 242

	/* required for xdg-output-unstable-v1 */
	struct wl_list outputs;
	struct xdg_output_manager_v1_info *xdg_output_manager_v1_info;
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
};

static void
print_global_info(void *data)
{
	struct global_info *global = data;

	printf("interface: '%s', version: %u, name: %u\n",
	       global->interface, global->version, global->id);
}

static void
init_global_info(struct weston_info *info,
		 struct global_info *global, uint32_t id,
		 const char *interface, uint32_t version)
{
	global->id = id;
	global->version = version;
261
	global->interface = xstrdup(interface);
262 263 264 265 266 267 268 269

	wl_list_insert(info->infos.prev, &global->link);
}

static void
print_output_info(void *data)
{
	struct output_info *output = data;
270
	struct output_mode *mode;
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
	const char *subpixel_orientation;
	const char *transform;

	print_global_info(data);

	switch (output->geometry.subpixel) {
	case WL_OUTPUT_SUBPIXEL_UNKNOWN:
		subpixel_orientation = "unknown";
		break;
	case WL_OUTPUT_SUBPIXEL_NONE:
		subpixel_orientation = "none";
		break;
	case WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB:
		subpixel_orientation = "horizontal rgb";
		break;
	case WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR:
		subpixel_orientation = "horizontal bgr";
		break;
	case WL_OUTPUT_SUBPIXEL_VERTICAL_RGB:
		subpixel_orientation = "vertical rgb";
		break;
	case WL_OUTPUT_SUBPIXEL_VERTICAL_BGR:
		subpixel_orientation = "vertical bgr";
		break;
	default:
		fprintf(stderr, "unknown subpixel orientation %u\n",
			output->geometry.subpixel);
		subpixel_orientation = "unexpected value";
		break;
	}

	switch (output->geometry.output_transform) {
	case WL_OUTPUT_TRANSFORM_NORMAL:
		transform = "normal";
		break;
	case WL_OUTPUT_TRANSFORM_90:
		transform = "90°";
		break;
	case WL_OUTPUT_TRANSFORM_180:
		transform = "180°";
		break;
	case WL_OUTPUT_TRANSFORM_270:
		transform = "270°";
		break;
	case WL_OUTPUT_TRANSFORM_FLIPPED:
		transform = "flipped";
		break;
	case WL_OUTPUT_TRANSFORM_FLIPPED_90:
		transform = "flipped 90°";
		break;
	case WL_OUTPUT_TRANSFORM_FLIPPED_180:
		transform = "flipped 180°";
		break;
	case WL_OUTPUT_TRANSFORM_FLIPPED_270:
		transform = "flipped 270°";
		break;
	default:
		fprintf(stderr, "unknown output transform %u\n",
			output->geometry.output_transform);
		transform = "unexpected value";
		break;
	}

334
	printf("\tx: %d, y: %d,",
335
	       output->geometry.x, output->geometry.y);
336 337 338 339
	if (output->version >= 2)
		printf(" scale: %d,", output->geometry.scale);
	printf("\n");

340
	printf("\tphysical_width: %d mm, physical_height: %d mm,\n",
341
	       output->geometry.physical_width,
342
	       output->geometry.physical_height);
343 344
	printf("\tmake: '%s', model: '%s',\n",
	       output->geometry.make, output->geometry.model);
345
	printf("\tsubpixel_orientation: %s, output_transform: %s,\n",
346
	       subpixel_orientation, transform);
347 348 349 350

	wl_list_for_each(mode, &output->modes, link) {
		printf("\tmode:\n");

351
		printf("\t\twidth: %d px, height: %d px, refresh: %.3f Hz,\n",
352 353 354 355 356 357 358 359 360 361
		       mode->width, mode->height,
		       (float) mode->refresh / 1000);

		printf("\t\tflags:");
		if (mode->flags & WL_OUTPUT_MODE_CURRENT)
			printf(" current");
		if (mode->flags & WL_OUTPUT_MODE_PREFERRED)
			printf(" preferred");
		printf("\n");
	}
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
static char
bits2graph(uint32_t value, unsigned bitoffset)
{
	int c = (value >> bitoffset) & 0xff;

	if (isgraph(c) || isspace(c))
		return c;

	return '?';
}

static void
fourcc2str(uint32_t format, char *str, int len)
{
	int i;

	assert(len >= 5);

	for (i = 0; i < 4; i++)
		str[i] = bits2graph(format, i * 8);
	str[i] = '\0';
}

387 388 389
static void
print_shm_info(void *data)
{
390
	char str[5];
391 392 393 394 395 396 397 398
	struct shm_info *shm = data;
	struct shm_format *format;

	print_global_info(data);

	printf("\tformats:");

	wl_list_for_each(format, &shm->formats, link)
399 400 401 402 403 404 405 406 407 408 409
		switch (format->format) {
		case WL_SHM_FORMAT_ARGB8888:
			printf(" ARGB8888");
			break;
		case WL_SHM_FORMAT_XRGB8888:
			printf(" XRGB8888");
			break;
		case WL_SHM_FORMAT_RGB565:
			printf(" RGB565");
			break;
		default:
410 411
			fourcc2str(format->format, str, sizeof(str));
			printf(" '%s'(0x%08x)", str, format->format);
412 413
			break;
		}
414 415 416 417

	printf("\n");
}

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
static void
print_linux_dmabuf_info(void *data)
{
	char str[5];
	struct linux_dmabuf_info *dmabuf = data;
	struct linux_dmabuf_modifier *modifier;

	print_global_info(data);

	printf("\tformats:");

	wl_list_for_each(modifier, &dmabuf->modifiers, link) {
		fourcc2str(modifier->format, str, sizeof(str));
		printf("\n\t'%s'(0x%08x), modifier: 0x%016"PRIx64, str, modifier->format, modifier->modifier);
	}

	printf("\n");
}

437 438 439 440 441 442 443
static void
print_seat_info(void *data)
{
	struct seat_info *seat = data;

	print_global_info(data);

444
	printf("\tname: %s\n", seat->name);
445 446 447 448 449 450 451 452 453 454
	printf("\tcapabilities:");

	if (seat->capabilities & WL_SEAT_CAPABILITY_POINTER)
		printf(" pointer");
	if (seat->capabilities & WL_SEAT_CAPABILITY_KEYBOARD)
		printf(" keyboard");
	if (seat->capabilities & WL_SEAT_CAPABILITY_TOUCH)
		printf(" touch");

	printf("\n");
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

	if (seat->repeat_rate > 0)
		printf("\tkeyboard repeat rate: %d\n", seat->repeat_rate);
	if (seat->repeat_delay > 0)
		printf("\tkeyboard repeat delay: %d\n", seat->repeat_delay);
}

static void
keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
		       uint32_t format, int fd, uint32_t size)
{
}

static void
keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
		      uint32_t serial, struct wl_surface *surface,
		      struct wl_array *keys)
{
}

static void
keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
		      uint32_t serial, struct wl_surface *surface)
{
479 480
}

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
static void
keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
		    uint32_t serial, uint32_t time, uint32_t key,
		    uint32_t state)
{
}

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)
{
}

static void
keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
			    int32_t rate, int32_t delay)
{
	struct seat_info *seat = data;

	seat->repeat_rate = rate;
	seat->repeat_delay = delay;
}

static const struct wl_keyboard_listener keyboard_listener = {
	keyboard_handle_keymap,
	keyboard_handle_enter,
	keyboard_handle_leave,
	keyboard_handle_key,
	keyboard_handle_modifiers,
	keyboard_handle_repeat_info,
};

515 516 517 518 519
static void
seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
			 enum wl_seat_capability caps)
{
	struct seat_info *seat = data;
520

521
	seat->capabilities = caps;
522 523 524 525 526 527 528 529

	/* we want listen for repeat_info from wl_keyboard, but only
	 * do so if the seat info is >= 4 and if we actually have a
	 * keyboard */
	if (seat->global.version < 4)
		return;

	if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
Ongy's avatar
Ongy committed
530 531
		seat->keyboard = wl_seat_get_keyboard(seat->seat);
		wl_keyboard_add_listener(seat->keyboard, &keyboard_listener,
532 533 534 535
					 seat);

		seat->info->roundtrip_needed = true;
	}
536 537
}

538 539 540 541 542
static void
seat_handle_name(void *data, struct wl_seat *wl_seat,
		 const char *name)
{
	struct seat_info *seat = data;
543
	seat->name = xstrdup(name);
544 545
}

546 547
static const struct wl_seat_listener seat_listener = {
	seat_handle_capabilities,
548
	seat_handle_name,
549 550
};

551 552 553 554 555 556 557 558 559
static void
destroy_seat_info(void *data)
{
	struct seat_info *seat = data;

	wl_seat_destroy(seat->seat);

	if (seat->name != NULL)
		free(seat->name);
560

Ongy's avatar
Ongy committed
561 562 563
	if (seat->keyboard)
		wl_keyboard_destroy(seat->keyboard);

564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	wl_list_remove(&seat->global_link);
}

static const char *
tablet_tool_type_to_str(enum zwp_tablet_tool_v2_type type)
{
	switch (type) {
	case ZWP_TABLET_TOOL_V2_TYPE_PEN:
		return "pen";
	case ZWP_TABLET_TOOL_V2_TYPE_ERASER:
		return "eraser";
	case ZWP_TABLET_TOOL_V2_TYPE_BRUSH:
		return "brush";
	case ZWP_TABLET_TOOL_V2_TYPE_PENCIL:
		return "pencil";
	case ZWP_TABLET_TOOL_V2_TYPE_AIRBRUSH:
		return "airbrush";
	case ZWP_TABLET_TOOL_V2_TYPE_FINGER:
		return "finger";
	case ZWP_TABLET_TOOL_V2_TYPE_MOUSE:
		return "mouse";
	case ZWP_TABLET_TOOL_V2_TYPE_LENS:
		return "lens";
	}

	return "Unknown type";
}

static void
print_tablet_tool_info(const struct tablet_tool_info *info)
{
	printf("\t\ttablet_tool: %s\n", tablet_tool_type_to_str(info->type));
	if (info->hardware_serial) {
597
		printf("\t\t\thardware serial: %" PRIx64 "\n", info->hardware_serial);
598 599
	}
	if (info->hardware_id_wacom) {
600
		printf("\t\t\thardware wacom: %" PRIx64 "\n", info->hardware_id_wacom);
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
	}

	printf("\t\t\tcapabilities:");

	if (info->has_tilt) {
		printf(" tilt");
	}
	if (info->has_pressure) {
		printf(" pressure");
	}
	if (info->has_distance) {
		printf(" distance");
	}
	if (info->has_rotation) {
		printf(" rotation");
	}
	if (info->has_slider) {
		printf(" slider");
	}
	if (info->has_wheel) {
		printf(" wheel");
	}
	printf("\n");
}

static void
destroy_tablet_tool_info(struct tablet_tool_info *info)
{
	wl_list_remove(&info->link);
	zwp_tablet_tool_v2_destroy(info->tool);
	free(info);
}

static void
print_tablet_pad_group_info(const struct tablet_pad_group_info *info)
{
	size_t i;
	printf("\t\t\tgroup:\n");
	printf("\t\t\t\tmodes: %u\n", info->modes);
	printf("\t\t\t\tstrips: %zu\n", info->strips);
	printf("\t\t\t\trings: %zu\n", info->rings);
	printf("\t\t\t\tbuttons:");

	for (i = 0; i < info->button_count; ++i) {
		printf(" %d", info->buttons[i]);
	}

	printf("\n");
}

static void
destroy_tablet_pad_group_info(struct tablet_pad_group_info *info)
{
	wl_list_remove(&info->link);
	zwp_tablet_pad_group_v2_destroy(info->group);

	if (info->buttons) {
		free(info->buttons);
	}
	free(info);
}

static void
print_tablet_pad_info(const struct tablet_pad_info *info)
{
	const struct tablet_v2_path *path;
	const struct tablet_pad_group_info *group;

	printf("\t\tpad:\n");
	printf("\t\t\tbuttons: %u\n", info->buttons);

	wl_list_for_each(path, &info->paths, link) {
		printf("\t\t\tpath: %s\n", path->path);
	}

	wl_list_for_each(group, &info->groups, link) {
		print_tablet_pad_group_info(group);
	}
}

static void
destroy_tablet_pad_info(struct tablet_pad_info *info)
{
	struct tablet_v2_path *path;
	struct tablet_v2_path *tmp_path;
	struct tablet_pad_group_info *group;
	struct tablet_pad_group_info *tmp_group;

	wl_list_remove(&info->link);
	zwp_tablet_pad_v2_destroy(info->pad);

	wl_list_for_each_safe(path, tmp_path, &info->paths, link) {
		wl_list_remove(&path->link);
		free(path->path);
		free(path);
	}

	wl_list_for_each_safe(group, tmp_group, &info->groups, link) {
		destroy_tablet_pad_group_info(group);
	}

	free(info);
}

static void
print_tablet_info(const struct tablet_info *info)
{
	const struct tablet_v2_path *path;

	printf("\t\ttablet: %s\n", info->name);
	printf("\t\t\tvendor: %u\n", info->vid);
	printf("\t\t\tproduct: %u\n", info->pid);

	wl_list_for_each(path, &info->paths, link) {
		printf("\t\t\tpath: %s\n", path->path);
	}
}

static void
destroy_tablet_info(struct tablet_info *info)
{
	struct tablet_v2_path *path;
	struct tablet_v2_path *tmp;

	wl_list_remove(&info->link);
	zwp_tablet_v2_destroy(info->tablet);

	if (info->name) {
		free(info->name);
	}

	wl_list_for_each_safe(path, tmp, &info->paths, link) {
		wl_list_remove(&path->link);
		free(path->path);
		free(path);
	}

	free(info);
}

static void
print_tablet_seat_info(const struct tablet_seat_info *info)
{
	const struct tablet_info *tablet;
	const struct tablet_pad_info *pad;
	const struct tablet_tool_info *tool;

	printf("\ttablet_seat: %s\n", info->seat_info->name);

	wl_list_for_each(tablet, &info->tablets, link) {
		print_tablet_info(tablet);
	}

	wl_list_for_each(pad, &info->pads, link) {
		print_tablet_pad_info(pad);
	}

	wl_list_for_each(tool, &info->tools, link) {
		print_tablet_tool_info(tool);
	}
}

static void
destroy_tablet_seat_info(struct tablet_seat_info *info)
{
	struct tablet_info *tablet;
	struct tablet_info *tmp_tablet;
	struct tablet_pad_info *pad;
	struct tablet_pad_info *tmp_pad;
	struct tablet_tool_info *tool;
	struct tablet_tool_info *tmp_tool;

	wl_list_remove(&info->link);
	zwp_tablet_seat_v2_destroy(info->seat);

	wl_list_for_each_safe(tablet, tmp_tablet, &info->tablets, link) {
		destroy_tablet_info(tablet);
	}

	wl_list_for_each_safe(pad, tmp_pad, &info->pads, link) {
		destroy_tablet_pad_info(pad);
	}

	wl_list_for_each_safe(tool, tmp_tool, &info->tools, link) {
		destroy_tablet_tool_info(tool);
	}

	free(info);
}

static void
print_tablet_v2_info(void *data)
{
	struct tablet_v2_info *info = data;
	struct tablet_seat_info *seat;
	print_global_info(data);

	wl_list_for_each(seat, &info->seats, link) {
		/* Skip tablet_seats without a tablet, they are irrelevant */
		if (wl_list_empty(&seat->pads) &&
		    wl_list_empty(&seat->tablets) &&
		    wl_list_empty(&seat->tools)) {
			continue;
		}

		print_tablet_seat_info(seat);
	}
}

static void
destroy_tablet_v2_info(void *data)
{
	struct tablet_v2_info *info = data;
	struct tablet_seat_info *seat;
	struct tablet_seat_info *tmp;

	zwp_tablet_manager_v2_destroy(info->manager);

	wl_list_for_each_safe(seat, tmp, &info->seats, link) {
		destroy_tablet_seat_info(seat);
	}
}

static void
handle_tablet_v2_tablet_tool_done(void *data, struct zwp_tablet_tool_v2 *tool)
{
	/* don't bother waiting for this; there's no good reason a
	 * compositor will wait more than one roundtrip before sending
	 * these initial events. */
}

static void
handle_tablet_v2_tablet_tool_removed(void *data, struct zwp_tablet_tool_v2 *tool)
{
	/* don't bother waiting for this; we never make any request either way. */
}

static void
handle_tablet_v2_tablet_tool_type(void *data, struct zwp_tablet_tool_v2 *tool,
                                  uint32_t tool_type)
{
	struct tablet_tool_info *info = data;
	info->type = tool_type;
}

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)
{
	struct tablet_tool_info *info = data;

	info->hardware_serial = ((uint64_t) serial_hi) << 32 |
		(uint64_t) serial_lo;
}

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)
{
	struct tablet_tool_info *info = data;

	info->hardware_id_wacom = ((uint64_t) id_hi) << 32 | (uint64_t) id_lo;
}

static void
handle_tablet_v2_tablet_tool_capability(void *data,
                                        struct zwp_tablet_tool_v2 *tool,
                                        uint32_t capability)
{
	struct tablet_tool_info *info = data;
	enum zwp_tablet_tool_v2_capability cap = capability;
875

876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
	switch(cap) {
	case ZWP_TABLET_TOOL_V2_CAPABILITY_TILT:
		info->has_tilt = true;
		break;
	case ZWP_TABLET_TOOL_V2_CAPABILITY_PRESSURE:
		info->has_pressure = true;
		break;
	case ZWP_TABLET_TOOL_V2_CAPABILITY_DISTANCE:
		info->has_distance = true;
		break;
	case ZWP_TABLET_TOOL_V2_CAPABILITY_ROTATION:
		info->has_rotation = true;
		break;
	case ZWP_TABLET_TOOL_V2_CAPABILITY_SLIDER:
		info->has_slider = true;
		break;
	case ZWP_TABLET_TOOL_V2_CAPABILITY_WHEEL:
		info->has_wheel = true;
		break;
	}
}

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)
{

}

static void
handle_tablet_v2_tablet_tool_proximity_out(void *data,
                                 struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2)
{

}

static void
handle_tablet_v2_tablet_tool_down(void *data,
                                 struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
                                 uint32_t serial)
{

}

static void
handle_tablet_v2_tablet_tool_up(void *data,
                                struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2)
{

}


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)
{

}

static void
handle_tablet_v2_tablet_tool_pressure(void *data,
                                 struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
                                 uint32_t pressure)
{

}

static void
handle_tablet_v2_tablet_tool_distance(void *data,
                                 struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
                                 uint32_t distance)
{

}

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)
{

}

static void
handle_tablet_v2_tablet_tool_rotation(void *data,
                                 struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
                                 wl_fixed_t degrees)
{

}

static void
handle_tablet_v2_tablet_tool_slider(void *data,
                                 struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
                                 int32_t position)
{

}

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)
{

}

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)
{

}

static void
handle_tablet_v2_tablet_tool_frame(void *data,
                                 struct zwp_tablet_tool_v2 *zwp_tablet_tool_v2,
                                 uint32_t time)
{

}

static const struct zwp_tablet_tool_v2_listener tablet_tool_listener = {
	.removed = handle_tablet_v2_tablet_tool_removed,
	.done = handle_tablet_v2_tablet_tool_done,
	.type = handle_tablet_v2_tablet_tool_type,
	.hardware_serial = handle_tablet_v2_tablet_tool_hardware_serial,
	.hardware_id_wacom = handle_tablet_v2_tablet_tool_hardware_id_wacom,
	.capability = handle_tablet_v2_tablet_tool_capability,

	.proximity_in = handle_tablet_v2_tablet_tool_proximity_in,
	.proximity_out = handle_tablet_v2_tablet_tool_proximity_out,
	.down = handle_tablet_v2_tablet_tool_down,
	.up = handle_tablet_v2_tablet_tool_up,

	.motion = handle_tablet_v2_tablet_tool_motion,
	.pressure = handle_tablet_v2_tablet_tool_pressure,
	.distance = handle_tablet_v2_tablet_tool_distance,
	.tilt = handle_tablet_v2_tablet_tool_tilt,
	.rotation = handle_tablet_v2_tablet_tool_rotation,
	.slider = handle_tablet_v2_tablet_tool_slider,
	.wheel = handle_tablet_v2_tablet_tool_wheel,
	.button = handle_tablet_v2_tablet_tool_button,
	.frame = handle_tablet_v2_tablet_tool_frame,
};

static void add_tablet_v2_tablet_tool_info(void *data,
                                     struct zwp_tablet_seat_v2 *tablet_seat_v2,
                                     struct zwp_tablet_tool_v2 *tool)
{
	struct tablet_seat_info *tablet_seat = data;
	struct tablet_tool_info *tool_info = xzalloc(sizeof *tool_info);

	tool_info->tool = tool;
	wl_list_insert(&tablet_seat->tools, &tool_info->link);

	zwp_tablet_tool_v2_add_listener(tool, &tablet_tool_listener, tool_info);
}

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)
{
	/* This shouldn't ever happen  */
}

static void
handle_tablet_v2_tablet_pad_group_done(void *data,
                                       struct zwp_tablet_pad_group_v2 *group)
{
	/* don't bother waiting for this; there's no good reason a
	 * compositor will wait more than one roundtrip before sending
	 * these initial events. */
}

static void
handle_tablet_v2_tablet_pad_group_modes(void *data,
                                        struct zwp_tablet_pad_group_v2 *group,
                                        uint32_t modes)
{
	struct tablet_pad_group_info *info = data;
	info->modes = modes;
}

static void
handle_tablet_v2_tablet_pad_group_buttons(void *data,
                                          struct zwp_tablet_pad_group_v2 *group,
                                          struct wl_array *buttons)
{
	struct tablet_pad_group_info *info = data;

	info->button_count = buttons->size / sizeof(int);
	info->buttons = xzalloc(buttons->size);
	memcpy(info->buttons, buttons->data, buttons->size);
}

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)
{
	struct tablet_pad_group_info *info = data;
	++info->rings;

	zwp_tablet_pad_ring_v2_destroy(ring);
}

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)
{
	struct tablet_pad_group_info *info = data;
	++info->strips;

	zwp_tablet_pad_strip_v2_destroy(strip);
}

static const struct zwp_tablet_pad_group_v2_listener tablet_pad_group_listener = {
	.buttons = handle_tablet_v2_tablet_pad_group_buttons,
	.modes = handle_tablet_v2_tablet_pad_group_modes,
	.ring = handle_tablet_v2_tablet_pad_group_ring,
	.strip = handle_tablet_v2_tablet_pad_group_strip,
	.done = handle_tablet_v2_tablet_pad_group_done,
	.mode_switch = handle_tablet_v2_tablet_pad_group_mode_switch,
};

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)
{
	struct tablet_pad_info *pad_info = data;
	struct tablet_pad_group_info *group = xzalloc(sizeof *group);

	wl_list_insert(&pad_info->groups, &group->link);
	group->group = pad_group;
	zwp_tablet_pad_group_v2_add_listener(pad_group,
	                                     &tablet_pad_group_listener, group);
}

static void
handle_tablet_v2_tablet_pad_path(void *data, struct zwp_tablet_pad_v2 *pad,
                                 const char *path)
{
	struct tablet_pad_info *pad_info = data;
	struct tablet_v2_path *path_elem = xzalloc(sizeof *path_elem);
	path_elem->path = xstrdup(path);

	wl_list_insert(&pad_info->paths, &path_elem->link);
}

static void
handle_tablet_v2_tablet_pad_buttons(void *data, struct zwp_tablet_pad_v2 *pad,
                                    uint32_t buttons)
{
	struct tablet_pad_info *pad_info = data;

	pad_info->buttons = buttons;
}

static void
handle_tablet_v2_tablet_pad_done(void *data, struct zwp_tablet_pad_v2 *pad)
{
	/* don't bother waiting for this; there's no good reason a
	 * compositor will wait more than one roundtrip before sending
	 * these initial events. */
}

static void
handle_tablet_v2_tablet_pad_removed(void *data, struct zwp_tablet_pad_v2 *pad)
{
	/* don't bother waiting for this; We never make any request that's not
	 * allowed to be issued either way. */
}

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)
{
	/* we don't have a surface, so this can't ever happen */
}

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)
{
	/* we don't have a surface, so this can't ever happen */
}

static void
handle_tablet_v2_tablet_pad_leave(void *data, struct zwp_tablet_pad_v2 *pad,
		uint32_t serial, struct wl_surface *surface)
{
	/* we don't have a surface, so this can't ever happen */
}

static const struct zwp_tablet_pad_v2_listener tablet_pad_listener = {
	.group = handle_tablet_v2_tablet_pad_group,
	.path = handle_tablet_v2_tablet_pad_path,
	.buttons = handle_tablet_v2_tablet_pad_buttons,
	.done = handle_tablet_v2_tablet_pad_done,
	.removed = handle_tablet_v2_tablet_pad_removed,
	.button = handle_tablet_v2_tablet_pad_button,
	.enter = handle_tablet_v2_tablet_pad_enter,
	.leave = handle_tablet_v2_tablet_pad_leave,
};

static void add_tablet_v2_tablet_pad_info(void *data,
                                     struct zwp_tablet_seat_v2 *tablet_seat_v2,
                                     struct zwp_tablet_pad_v2 *pad)
{
	struct tablet_seat_info *tablet_seat = data;
	struct tablet_pad_info *pad_info = xzalloc(sizeof *pad_info);

	wl_list_init(&pad_info->paths);
	wl_list_init(&pad_info->groups);
	pad_info->pad = pad;
	wl_list_insert(&tablet_seat->pads, &pad_info->link);

	zwp_tablet_pad_v2_add_listener(pad, &tablet_pad_listener, pad_info);
}

static void
handle_tablet_v2_tablet_name(void *data, struct zwp_tablet_v2 *zwp_tablet_v2,
                             const char *name)
{
	struct tablet_info *tablet_info = data;
	tablet_info->name = xstrdup(name);
}

static void
handle_tablet_v2_tablet_path(void *data, struct zwp_tablet_v2 *zwp_tablet_v2,
                             const char *path)
{
	struct tablet_info *tablet_info = data;
	struct tablet_v2_path *path_elem = xzalloc(sizeof *path_elem);
	path_elem->path = xstrdup(path);

	wl_list_insert(&tablet_info->paths, &path_elem->link);
}

static void
handle_tablet_v2_tablet_id(void *data, struct zwp_tablet_v2 *zwp_tablet_v2,
                           uint32_t vid, uint32_t pid)
{
	struct tablet_info *tablet_info = data;

	tablet_info->vid = vid;
	tablet_info->pid = pid;
}

static void
handle_tablet_v2_tablet_done(void *data, struct zwp_tablet_v2 *zwp_tablet_v2)
{
	/* don't bother waiting for this; there's no good reason a
	 * compositor will wait more than one roundtrip before sending
	 * these initial events. */
}

static void
handle_tablet_v2_tablet_removed(void *data, struct zwp_tablet_v2 *zwp_tablet_v2)
{
	/* don't bother waiting for this; We never make any request that's not
	 * allowed to be issued either way. */
}

static const struct zwp_tablet_v2_listener tablet_listener = {
	.name = handle_tablet_v2_tablet_name,
	.id = handle_tablet_v2_tablet_id,
	.path = handle_tablet_v2_tablet_path,
	.done = handle_tablet_v2_tablet_done,
	.removed = handle_tablet_v2_tablet_removed
};

static void
add_tablet_v2_tablet_info(void *data, struct zwp_tablet_seat_v2 *tablet_seat_v2,
                          struct zwp_tablet_v2 *tablet)
{
	struct tablet_seat_info *tablet_seat = data;
	struct tablet_info *tablet_info = xzalloc(sizeof *tablet_info);

	wl_list_init(&tablet_info->paths);
	tablet_info->tablet = tablet;
	wl_list_insert(&tablet_seat->tablets, &tablet_info->link);

	zwp_tablet_v2_add_listener(tablet, &tablet_listener, tablet_info);
}

static const struct zwp_tablet_seat_v2_listener tablet_seat_listener =  {
	.tablet_added = add_tablet_v2_tablet_info,
	.pad_added = add_tablet_v2_tablet_pad_info,
	.tool_added = add_tablet_v2_tablet_tool_info,
};

static void
add_tablet_seat_info(struct tablet_v2_info *tablet_info, struct seat_info *seat)
{
	struct tablet_seat_info *tablet_seat = xzalloc(sizeof *tablet_seat);

	wl_list_insert(&tablet_info->seats, &tablet_seat->link);
	tablet_seat->seat = zwp_tablet_manager_v2_get_tablet_seat(
		tablet_info->manager, seat->seat);
	zwp_tablet_seat_v2_add_listener(tablet_seat->seat,
		&tablet_seat_listener, tablet_seat);

	wl_list_init(&tablet_seat->pads);
	wl_list_init(&tablet_seat->tablets);
	wl_list_init(&tablet_seat->tools);
	tablet_seat->seat_info = seat;

	tablet_info->info->roundtrip_needed = true;
}

static void
add_tablet_v2_info(struct weston_info *info, uint32_t id, uint32_t version)
{
	struct seat_info *seat;
	struct tablet_v2_info *tablet = xzalloc(sizeof *tablet);

	wl_list_init(&tablet->seats);
	tablet->info = info;

	init_global_info(info, &tablet->global, id,
		zwp_tablet_manager_v2_interface.name, version);
	tablet->global.print = print_tablet_v2_info;
	tablet->global.destroy = destroy_tablet_v2_info;

	tablet->manager = wl_registry_bind(info->registry,
		id, &zwp_tablet_manager_v2_interface, 1);

	wl_list_for_each(seat, &info->seats, global_link) {
		add_tablet_seat_info(tablet, seat);
	}

	info->tablet_info = tablet;
1324 1325
}

1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
static void
destroy_xdg_output_v1_info(struct xdg_output_v1_info *info)
{
	wl_list_remove(&info->link);
	zxdg_output_v1_destroy(info->xdg_output);
	free(info->name);
	free(info->description);
	free(info);
}

static void
print_xdg_output_v1_info(const struct xdg_output_v1_info *info)
{
	printf("\txdg_output_v1\n");
	printf("\t\toutput: %d\n", info->output->global.id);
	if (info->name)
		printf("\t\tname: '%s'\n", info->name);
	if (info->description)
		printf("\t\tdescription: '%s'\n", info->description);
	printf("\t\tlogical_x: %d, logical_y: %d\n",
		info->logical.x, info->logical.y);
	printf("\t\tlogical_width: %d, logical_height: %d\n",
		info->logical.width, info->logical.height);
}

static void
print_xdg_output_manager_v1_info(void *data)
{
	struct xdg_output_manager_v1_info *info = data;
	struct xdg_output_v1_info *output;

	print_global_info(data);

	wl_list_for_each(output, &info->outputs, link)
		print_xdg_output_v1_info(output);
}

static void
destroy_xdg_output_manager_v1_info(void *data)
{
	struct xdg_output_manager_v1_info *info = data;
	struct xdg_output_v1_info *output, *tmp;

	zxdg_output_manager_v1_destroy(info->manager);

	wl_list_for_each_safe(output, tmp, &info->outputs, link)
		destroy_xdg_output_v1_info(output);
}

static void
handle_xdg_output_v1_logical_position(void *data, struct zxdg_output_v1 *output,
                                      int32_t x, int32_t y)
{
	struct xdg_output_v1_info *xdg_output = data;
	xdg_output->logical.x = x;
	xdg_output->logical.y = y;
}

static void
handle_xdg_output_v1_logical_size(void *data, struct zxdg_output_v1 *output,
                                      int32_t width, int32_t height)
{
	struct xdg_output_v1_info *xdg_output = data;
	xdg_output->logical.width = width;
	xdg_output->logical.height = height;
}

static void
handle_xdg_output_v1_done(void *data, struct zxdg_output_v1 *output)
{
	/* Don't bother waiting for this; there's no good reason a
	 * compositor will wait more than one roundtrip before sending
	 * these initial events. */
}

static void
handle_xdg_output_v1_name(void *data, struct zxdg_output_v1 *output,
                          const char *name)
{
	struct xdg_output_v1_info *xdg_output = data;
	xdg_output->name = strdup(name);
}

static void
handle_xdg_output_v1_description(void *data, struct zxdg_output_v1 *output,
                          const char *description)
{
	struct xdg_output_v1_info *xdg_output = data;
	xdg_output->description = strdup(description);
}

static const struct zxdg_output_v1_listener xdg_output_v1_listener = {
	.logical_position = handle_xdg_output_v1_logical_position,
	.logical_size = handle_xdg_output_v1_logical_size,
	.done = handle_xdg_output_v1_done,
	.name = handle_xdg_output_v1_name,
	.description = handle_xdg_output_v1_description,
};

static void
add_xdg_output_v1_info(struct xdg_output_manager_v1_info *manager_info,
                       struct output_info *output)
{
	struct xdg_output_v1_info *xdg_output = xzalloc(sizeof *xdg_output);

	wl_list_insert(&manager_info->outputs, &xdg_output->link);
	xdg_output->xdg_output = zxdg_output_manager_v1_get_xdg_output(
		manager_info->manager, output->output);
	zxdg_output_v1_add_listener(xdg_output->xdg_output,
		&xdg_output_v1_listener, xdg_output);

	xdg_output->output = output;

	manager_info->info->roundtrip_needed = true;
}

static void
add_xdg_output_manager_v1_info(struct weston_info *info, uint32_t id,
                               uint32_t version)
{
	struct output_info *output;
	struct xdg_output_manager_v1_info *manager = xzalloc(sizeof *manager);

	wl_list_init(&manager->outputs);
	manager->info = info;

	init_global_info(info, &manager->global, id,
		zxdg_output_manager_v1_interface.name, version);
	manager->global.print = print_xdg_output_manager_v1_info;
	manager->global.destroy = destroy_xdg_output_manager_v1_info;

	manager->manager = wl_registry_bind(info->registry, id,
		&zxdg_output_manager_v1_interface, version > 2 ? 2 : version);

	wl_list_for_each(output, &info->outputs, global_link)
		add_xdg_output_v1_info(manager, output);

	info->xdg_output_manager_v1_info = manager;
}

1466 1467 1468
static void
add_seat_info(struct weston_info *info, uint32_t id, uint32_t version)
{
1469
	struct seat_info *seat = xzalloc(sizeof *seat);
1470

1471 1472 1473 1474
	/* required to set roundtrip_needed to true in capabilities
	 * handler */
	seat->info = info;

1475 1476
	init_global_info(info, &seat->global, id, "wl_seat", version);
	seat->global.print = print_seat_info;
1477
	seat->global.destroy = destroy_seat_info;
1478

1479
	seat->seat = wl_registry_bind(info->registry,
1480
				      id, &wl_seat_interface, MIN(version, 4));
1481
	wl_seat_add_listener(seat->seat, &seat_listener, seat);
1482

1483 1484
	seat->repeat_rate = seat->repeat_delay = -1;

1485
	info->roundtrip_needed = true;
1486 1487 1488 1489 1490
	wl_list_insert(&info->seats, &seat->global_link);

	if (info->tablet_info) {
		add_tablet_seat_info(info->tablet_info, seat);
	}
1491 1492 1493 1494 1495 1496
}

static void
shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
{
	struct shm_info *shm = data;
1497
	struct shm_format *shm_format = xzalloc(sizeof *shm_format);
1498 1499 1500 1501 1502 1503 1504 1505 1506

	wl_list_insert(&shm->formats, &shm_format->link);
	shm_format->format = format;
}

static const struct wl_shm_listener shm_listener = {
	shm_handle_format,
};

1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
static void
destroy_shm_info(void *data)
{
	struct shm_info *shm = data;
	struct shm_format *format, *tmp;

	wl_list_for_each_safe(format, tmp, &shm->formats, link) {
		wl_list_remove(&format->link);
		free(format);
	}

	wl_shm_destroy(shm->shm);
}

1521 1522 1523
static void
add_shm_info(struct weston_info *info, uint32_t id, uint32_t version)
{
1524
	struct shm_info *shm = xzalloc(sizeof *shm);
1525 1526 1527

	init_global_info(info, &shm->global, id, "wl_shm", version);
	shm->global.print = print_shm_info;
1528 1529
	shm->global.destroy = destroy_shm_info;

1530 1531
	wl_list_init(&shm->formats);

1532 1533
	shm->shm = wl_registry_bind(info->registry,
				    id, &wl_shm_interface, 1);
1534
	wl_shm_add_listener(shm->shm, &shm_listener, shm);
1535 1536

	info->roundtrip_needed = true;
1537 1538
}

1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
static void
linux_dmabuf_handle_format(void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf_v1, uint32_t format)
{
	/* This is a deprecated event, don’t use it. */
}

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)
{
	struct linux_dmabuf_info *dmabuf = data;
	struct linux_dmabuf_modifier *linux_dmabuf_modifier = xzalloc(sizeof *linux_dmabuf_modifier);

	wl_list_insert(&dmabuf->modifiers, &linux_dmabuf_modifier->link);
	linux_dmabuf_modifier->format = format;
	linux_dmabuf_modifier->modifier = ((uint64_t)modifier_hi) << 32 | modifier_lo;
}

static const struct zwp_linux_dmabuf_v1_listener linux_dmabuf_listener = {
	linux_dmabuf_handle_format,
	linux_dmabuf_handle_modifier,
};

static void
destroy_linux_dmabuf_info(void *data)
{
	struct linux_dmabuf_info *dmabuf = data;
	struct linux_dmabuf_modifier *modifier, *tmp;

	wl_list_for_each_safe(modifier, tmp, &dmabuf->modifiers, link) {
		wl_list_remove(&modifier->link);
		free(modifier);
	}

	zwp_linux_dmabuf_v1_destroy(dmabuf->dmabuf);
}

static void
add_linux_dmabuf_info(struct weston_info *info, uint32_t id, uint32_t version)
{
	struct linux_dmabuf_info *dmabuf = xzalloc(sizeof *dmabuf);

	init_global_info(info, &dmabuf->global, id, "zwp_linux_dmabuf_v1", version);
	dmabuf->global.print = print_linux_dmabuf_info;
	dmabuf->global.destroy = destroy_linux_dmabuf_info;

	wl_list_init(&dmabuf->modifiers);

	if (version >= 3) {
		dmabuf->dmabuf = wl_registry_bind(info->registry,
		                                  id, &zwp_linux_dmabuf_v1_interface, 3);
		zwp_linux_dmabuf_v1_add_listener(dmabuf->dmabuf, &linux_dmabuf_listener, dmabuf);

		info->roundtrip_needed = true;
	}
}

1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
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)
{
	struct output_info *output = data;

	output->geometry.x = x;
	output->geometry.y = y;
	output->geometry.physical_width = physical_width;
	output->geometry.physical_height = physical_height;
	output->geometry.subpixel = subpixel;
1610 1611
	output->geometry.make = xstrdup(make);
	output->geometry.model = xstrdup(model);
1612 1613 1614 1615 1616 1617 1618 1619 1620
	output->geometry.output_transform = output_transform;
}

static void
output_handle_mode(void *data, struct wl_output *wl_output,
		   uint32_t flags, int32_t width, int32_t height,
		   int32_t refresh)
{
	struct output_info *output = data;
1621
	struct output_mode *mode = xmalloc(sizeof *mode);
1622 1623 1624 1625 1626

	mode->flags = flags;
	mode->width = width;
	mode->height = height;
	mode->refresh = refresh;
1627

1628
	wl_list_insert(output->modes.prev, &mode->link);
1629 1630
}

1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
static void
output_handle_done(void *data, struct wl_output *wl_output)
{
	/* don't bother waiting for this; there's no good reason a
	 * compositor will wait more than one roundtrip before sending
	 * these initial events. */
}

static void
output_handle_scale(void *data, struct wl_output *wl_output,
		    int32_t scale)
{
	struct output_info *output = data;

	output->geometry.scale = scale;
}

1648 1649 1650
static const struct wl_output_listener output_listener = {
	output_handle_geometry,
	output_handle_mode,
1651 1652
	output_handle_done,
	output_handle_scale,
1653 1654
};

1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
static void
destroy_output_info(void *data)
{
	struct output_info *output = data;
	struct output_mode *mode, *tmp;

	wl_output_destroy(output->output);

	if (output->geometry.make != NULL)
		free(output->geometry.make);
	if (output->geometry.model != NULL)
		free(output->geometry.model);

	wl_list_for_each_safe(mode, tmp, &output->modes, link) {
		wl_list_remove(&mode->link);
		free(mode);
	}
}

1674 1675 1676
static void
add_output_info(struct weston_info *info, uint32_t id, uint32_t version)
{
1677
	struct output_info *output = xzalloc(sizeof *output);
1678 1679 1680

	init_global_info(info, &output->global, id, "wl_output", version);
	output->global.print = print_output_info;
1681
	output->global.destroy = destroy_output_info;
1682

1683 1684
	output->version = MIN(version, 2);
	output->geometry.scale = 1;
1685 1686
	wl_list_init(&output->modes);

1687
	output->output = wl_registry_bind(info->registry, id,
1688
					  &wl_output_interface, output->version);
1689 1690
	wl_output_add_listener(output->output, &output_listener,
			       output);
1691 1692

	info->roundtrip_needed = true;
1693 1694 1695 1696 1697
	wl_list_insert(&info->outputs, &output->global_link);

	if (info->xdg_output_manager_v1_info)
		add_xdg_output_v1_info(info->xdg_output_manager_v1_info,
				       output);
1698 1699
}

1700 1701 1702 1703 1704
static void
destroy_presentation_info(void *info)
{
	struct presentation_info *prinfo = info;

1705
	wp_presentation_destroy(prinfo->presentation);
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
}

static const char *
clock_name(clockid_t clk_id)
{
	static const char *names[] = {
		[CLOCK_REALTIME] =		"CLOCK_REALTIME",
		[CLOCK_MONOTONIC] =		"CLOCK_MONOTONIC",
		[CLOCK_MONOTONIC_RAW] =		"CLOCK_MONOTONIC_RAW",
		[CLOCK_REALTIME_COARSE] =	"CLOCK_REALTIME_COARSE",
		[CLOCK_MONOTONIC_COARSE] =	"CLOCK_MONOTONIC_COARSE",
1717
#ifdef CLOCK_BOOTTIME
1718
		[CLOCK_BOOTTIME] =		"CLOCK_BOOTTIME",
1719
#endif
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
	};

	if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
		return "unknown";

	return names[clk_id];
}

static void
print_presentation_info(void *info)
{
	struct presentation_info *prinfo = info;

	print_global_info(info);

	printf("\tpresentation clock id: %d (%s)\n",
		prinfo->clk_id, clock_name(prinfo->clk_id));
}

static void
1740
presentation_handle_clock_id(void *data, struct wp_presentation *presentation,
1741 1742 1743 1744 1745 1746 1747
			     uint32_t clk_id)
{
	struct presentation_info *prinfo = data;

	prinfo->clk_id = clk_id;
}

1748
static const struct wp_presentation_listener presentation_listener = {
1749 1750 1751 1752 1753 1754 1755 1756
	presentation_handle_clock_id
};

static void
add_presentation_info(struct weston_info *info, uint32_t id, uint32_t version)
{
	struct presentation_info *prinfo = xzalloc(sizeof *prinfo);

1757 1758
	init_global_info(info, &prinfo->global, id,
			 wp_presentation_interface.name, version);
1759 1760 1761 1762 1763
	prinfo->global.print = print_presentation_info;
	prinfo->global.destroy = destroy_presentation_info;

	prinfo->clk_id = -1;
	prinfo->presentation = wl_registry_bind(info->registry, id,
1764 1765 1766
						&wp_presentation_interface, 1);
	wp_presentation_add_listener(prinfo->presentation,
				     &presentation_listener, prinfo);
1767 1768 1769 1770

	info->roundtrip_needed = true;
}

1771 1772 1773 1774 1775
static void
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
	struct global_info *global = xzalloc(sizeof *global);
1781 1782 1783

	init_global_info(info, global, id, interface, version);
	global->print = print_global_info;
1784
	global->destroy = destroy_global_info;
1785 1786 1787
}

static void
1788 1789
global_handler(void *data, struct wl_registry *registry, uint32_t id,
	       const char *interface, uint32_t version)
1790 1791 1792 1793 1794 1795 1796
{
	struct weston_info *info = data;

	if (!strcmp(interface, "wl_seat"))
		add_seat_info(info, id, version);
	else if (!strcmp(interface, "wl_shm"))
		add_shm_info(info, id, version);
1797 1798
	else if (!strcmp(interface, "zwp_linux_dmabuf_v1"))
		add_linux_dmabuf_info(info, id, version);
1799 1800
	else if (!strcmp(interface, "wl_output"))
		add_output_info(info, id, version);
1801
	else if (!strcmp(interface, wp_presentation_interface.name))
1802
		add_presentation_info(info, id, version);
1803 1804
	else if (!strcmp(interface, zwp_tablet_manager_v2_interface.name))
		add_tablet_v2_info(info, id, version);
1805 1806
	else if (!strcmp(interface, zxdg_output_manager_v1_interface.name))
		add_xdg_output_manager_v1_info(info, id, version);
1807 1808 1809 1810
	else
		add_global_info(info, id, interface, version);
}

1811 1812 1813 1814 1815
static void
global_remove_handler(void *data, struct wl_registry *registry, uint32_t name)
{
}

1816
static const struct wl_registry_listener registry_listener = {
1817 1818
	global_handler,
	global_remove_handler
1819 1820
};

1821 1822 1823 1824 1825 1826 1827 1828 1829
static void
print_infos(struct wl_list *infos)
{
	struct global_info *info;

	wl_list_for_each(info, infos, link)
		info->print(info);
}

1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
static void
destroy_info(void *data)
{
	struct global_info *global = data;

	global->destroy(data);
	wl_list_remove(&global->link);
	free(global->interface);
	free(data);
}

static void
destroy_infos(struct wl_list *infos)
{
	struct global_info *info, *tmp;
	wl_list_for_each_safe(info, tmp, infos, link)
		destroy_info(info);
}

1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
int
main(int argc, char **argv)
{
	struct weston_info info;

	info.display = wl_display_connect(NULL);
	if (!info.display) {
		fprintf(stderr, "failed to create display: %m\n");
		return -1;
	}

1860
	info.tablet_info = NULL;
1861
	info.xdg_output_manager_v1_info = NULL;
1862
	wl_list_init(&info.infos);
1863
	wl_list_init(&info.seats);
1864
	wl_list_init(&info.outputs);
1865

1866 1867
	info.registry = wl_display_get_registry(info.display);
	wl_registry_add_listener(info.registry, &registry_listener, &info);
1868

1869 1870 1871 1872
	do {
		info.roundtrip_needed = false;
		wl_display_roundtrip(info.display);
	} while (info.roundtrip_needed);
1873 1874

	print_infos(&info.infos);
1875 1876 1877 1878
	destroy_infos(&info.infos);

	wl_registry_destroy(info.registry);
	wl_display_disconnect(info.display);
1879 1880 1881

	return 0;
}