xdg-shell-v5.c 25.9 KB
Newer Older
Quentin Glidic's avatar
Quentin Glidic committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Copyright © 2010-2012 Intel Corporation
 * Copyright © 2011-2012 Collabora, Ltd.
 * Copyright © 2013 Raspberry Pi Foundation
 * Copyright © 2016 Quentin "Sardem FF7" Glidic
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "config.h"

#include <stdbool.h>

#include <wayland-server.h>

#include "compositor.h"
#include "zalloc.h"
35
#include "xdg-shell-unstable-v5-server-protocol.h"
Quentin Glidic's avatar
Quentin Glidic committed
36 37 38 39 40 41 42 43 44 45

#include "libweston-desktop.h"
#include "internal.h"

#define WD_XDG_SHELL_PROTOCOL_VERSION 1

struct weston_desktop_xdg_surface {
	struct wl_resource *resource;
	struct weston_desktop_surface *surface;
	struct weston_desktop *desktop;
46
	bool added;
Quentin Glidic's avatar
Quentin Glidic committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	struct wl_event_source *add_idle;
	struct wl_event_source *configure_idle;
	uint32_t configure_serial;
	struct weston_size requested_size;
	struct {
		bool maximized;
		bool fullscreen;
		bool resizing;
		bool activated;
	} requested_state, next_state, state;
	bool has_next_geometry;
	struct weston_geometry next_geometry;
};

struct weston_desktop_xdg_popup {
	struct wl_resource *resource;
	struct weston_desktop_surface *popup;
	struct weston_desktop *desktop;
	struct weston_desktop_seat *seat;
	struct wl_display *display;
};

69 70 71 72 73 74 75 76 77 78 79 80 81
static void
weston_desktop_xdg_surface_ensure_added(struct weston_desktop_xdg_surface *surface)
{
	if (surface->added)
		return;

	if (surface->add_idle != NULL)
		wl_event_source_remove(surface->add_idle);
	surface->add_idle = NULL;
	weston_desktop_api_surface_added(surface->desktop, surface->surface);
	surface->added = true;
}

Quentin Glidic's avatar
Quentin Glidic committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
static void
weston_desktop_xdg_surface_send_configure(void *data)
{
	struct weston_desktop_xdg_surface *surface = data;
	uint32_t *s;
	struct wl_array states;

	surface->configure_idle = NULL;

	surface->configure_serial =
		wl_display_next_serial(weston_desktop_get_display(surface->desktop));

	wl_array_init(&states);
	if (surface->requested_state.maximized) {
		s = wl_array_add(&states, sizeof(uint32_t));
		*s = XDG_SURFACE_STATE_MAXIMIZED;
	}
	if (surface->requested_state.fullscreen) {
		s = wl_array_add(&states, sizeof(uint32_t));
		*s = XDG_SURFACE_STATE_FULLSCREEN;
	}
	if (surface->requested_state.resizing) {
		s = wl_array_add(&states, sizeof(uint32_t));
		*s = XDG_SURFACE_STATE_RESIZING;
	}
	if (surface->requested_state.activated) {
		s = wl_array_add(&states, sizeof(uint32_t));
		*s = XDG_SURFACE_STATE_ACTIVATED;
	}

	xdg_surface_send_configure(surface->resource,
				   surface->requested_size.width,
				   surface->requested_size.height,
				   &states,
				   surface->configure_serial);

	wl_array_release(&states);
};

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
static bool
weston_desktop_xdg_surface_state_compare(struct weston_desktop_xdg_surface *surface)
{
	struct weston_surface *wsurface =
		weston_desktop_surface_get_surface(surface->surface);

	if (surface->requested_state.activated != surface->state.activated)
		return false;
	if (surface->requested_state.fullscreen != surface->state.fullscreen)
		return false;
	if (surface->requested_state.maximized != surface->state.maximized)
		return false;
	if (surface->requested_state.resizing != surface->state.resizing)
		return false;

	if (wsurface->width == surface->requested_size.width &&
	    wsurface->height == surface->requested_size.height)
		return true;

	if (surface->requested_size.width == 0 &&
	    surface->requested_size.height == 0)
		return true;

	return false;
}

Quentin Glidic's avatar
Quentin Glidic committed
147
static void
148 149
weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface,
					      bool force)
Quentin Glidic's avatar
Quentin Glidic committed
150 151 152
{
	struct wl_display *display = weston_desktop_get_display(surface->desktop);
	struct wl_event_loop *loop = wl_display_get_event_loop(display);
153 154
	bool requested_same =
		!force && weston_desktop_xdg_surface_state_compare(surface);
Quentin Glidic's avatar
Quentin Glidic committed
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	if (surface->configure_idle != NULL) {
		if (!requested_same)
			return;

		wl_event_source_remove(surface->configure_idle);
		surface->configure_idle = NULL;
	} else {
		if (requested_same)
			return;

		surface->configure_idle =
			wl_event_loop_add_idle(loop,
					       weston_desktop_xdg_surface_send_configure,
					       surface);
	}
Quentin Glidic's avatar
Quentin Glidic committed
171 172 173 174 175 176 177 178 179
}

static void
weston_desktop_xdg_surface_set_maximized(struct weston_desktop_surface *dsurface,
					 void *user_data, bool maximized)
{
	struct weston_desktop_xdg_surface *surface = user_data;

	surface->requested_state.maximized = maximized;
180
	weston_desktop_xdg_surface_schedule_configure(surface, false);
Quentin Glidic's avatar
Quentin Glidic committed
181 182 183 184 185 186 187 188 189
}

static void
weston_desktop_xdg_surface_set_fullscreen(struct weston_desktop_surface *dsurface,
					  void *user_data, bool fullscreen)
{
	struct weston_desktop_xdg_surface *surface = user_data;

	surface->requested_state.fullscreen = fullscreen;
190
	weston_desktop_xdg_surface_schedule_configure(surface, false);
Quentin Glidic's avatar
Quentin Glidic committed
191 192 193 194 195 196 197 198 199
}

static void
weston_desktop_xdg_surface_set_resizing(struct weston_desktop_surface *dsurface,
					void *user_data, bool resizing)
{
	struct weston_desktop_xdg_surface *surface = user_data;

	surface->requested_state.resizing = resizing;
200
	weston_desktop_xdg_surface_schedule_configure(surface, false);
Quentin Glidic's avatar
Quentin Glidic committed
201 202 203 204 205 206 207 208 209
}

static void
weston_desktop_xdg_surface_set_activated(struct weston_desktop_surface *dsurface,
					 void *user_data, bool activated)
{
	struct weston_desktop_xdg_surface *surface = user_data;

	surface->requested_state.activated = activated;
210
	weston_desktop_xdg_surface_schedule_configure(surface, false);
Quentin Glidic's avatar
Quentin Glidic committed
211 212 213 214 215 216 217 218 219 220 221
}

static void
weston_desktop_xdg_surface_set_size(struct weston_desktop_surface *dsurface,
				    void *user_data,
				    int32_t width, int32_t height)
{
	struct weston_desktop_xdg_surface *surface = user_data;

	surface->requested_size.width = width;
	surface->requested_size.height = height;
222

223
	weston_desktop_xdg_surface_schedule_configure(surface, false);
Quentin Glidic's avatar
Quentin Glidic committed
224 225 226 227
}

static void
weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface,
228
				     void *user_data,
Quentin Glidic's avatar
Quentin Glidic committed
229 230 231 232 233 234 235 236 237 238 239 240
				     int32_t sx, int32_t sy)
{
	struct weston_desktop_xdg_surface *surface = user_data;
	struct weston_surface *wsurface =
		weston_desktop_surface_get_surface(surface->surface);
	bool reconfigure = false;

	if (surface->next_state.maximized || surface->next_state.fullscreen)
		reconfigure = surface->requested_size.width != wsurface->width ||
			      surface->requested_size.height != wsurface->height;

	if (reconfigure) {
241
		weston_desktop_xdg_surface_schedule_configure(surface, true);
Quentin Glidic's avatar
Quentin Glidic committed
242 243 244 245 246 247 248 249
	} else {
		surface->state = surface->next_state;
		if (surface->has_next_geometry) {
			surface->has_next_geometry = false;
			weston_desktop_surface_set_geometry(surface->surface,
							    surface->next_geometry);
		}

250
		weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic's avatar
Quentin Glidic committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
		weston_desktop_api_committed(surface->desktop, surface->surface,
					     sx, sy);
	}
}

static void
weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface,
				uint32_t serial, void *user_data)
{
	struct weston_desktop_client *client =
		weston_desktop_surface_get_client(dsurface);

	xdg_shell_send_ping(weston_desktop_client_get_resource(client),
			    serial);
}

static void
weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface,
				 void *user_data)
{
	struct weston_desktop_xdg_surface *surface = user_data;

	xdg_surface_send_close(surface->resource);
}

static bool
weston_desktop_xdg_surface_get_maximized(struct weston_desktop_surface *dsurface,
					 void *user_data)
{
	struct weston_desktop_xdg_surface *surface = user_data;

	return surface->state.maximized;
}

static bool
weston_desktop_xdg_surface_get_fullscreen(struct weston_desktop_surface *dsurface,
					  void *user_data)
{
	struct weston_desktop_xdg_surface *surface = user_data;

	return surface->state.fullscreen;
}

static bool
weston_desktop_xdg_surface_get_resizing(struct weston_desktop_surface *dsurface,
					void *user_data)
{
	struct weston_desktop_xdg_surface *surface = user_data;

	return surface->state.resizing;
}

static bool
weston_desktop_xdg_surface_get_activated(struct weston_desktop_surface *dsurface,
					 void *user_data)
{
	struct weston_desktop_xdg_surface *surface = user_data;

	return surface->state.activated;
}

static void
weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface,
				   void *user_data)
{
	struct weston_desktop_xdg_surface *surface = user_data;

318 319 320
	if (surface->added)
		weston_desktop_api_surface_removed(surface->desktop,
						   surface->surface);
Quentin Glidic's avatar
Quentin Glidic committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

	if (surface->add_idle != NULL)
		wl_event_source_remove(surface->add_idle);

	if (surface->configure_idle != NULL)
		wl_event_source_remove(surface->configure_idle);

	free(surface);
}

static void
weston_desktop_xdg_surface_protocol_set_parent(struct wl_client *wl_client,
					       struct wl_resource *resource,
					       struct wl_resource *parent_resource)
{
	struct weston_desktop_surface *dsurface =
		wl_resource_get_user_data(resource);
	struct weston_desktop_xdg_surface *surface =
		weston_desktop_surface_get_implementation_data(dsurface);
	struct weston_desktop_surface *parent = NULL;

	if (parent_resource != NULL)
		parent = wl_resource_get_user_data(parent_resource);
344 345

	weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic's avatar
Quentin Glidic committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	weston_desktop_api_set_parent(surface->desktop, dsurface, parent);
}

static void
weston_desktop_xdg_surface_protocol_set_title(struct wl_client *wl_client,
					      struct wl_resource *resource,
					      const char *title)
{
	struct weston_desktop_surface *surface =
		wl_resource_get_user_data(resource);

	weston_desktop_surface_set_title(surface, title);
}

static void
weston_desktop_xdg_surface_protocol_set_app_id(struct wl_client *wl_client,
					       struct wl_resource *resource,
					       const char *app_id)
{
	struct weston_desktop_surface *surface =
		wl_resource_get_user_data(resource);

	weston_desktop_surface_set_app_id(surface, app_id);
}

static void
weston_desktop_xdg_surface_protocol_show_window_menu(struct wl_client *wl_client,
						     struct wl_resource *resource,
						     struct wl_resource *seat_resource,
						     uint32_t serial,
						     int32_t x, int32_t y)
{
	struct weston_desktop_surface *dsurface =
		wl_resource_get_user_data(resource);
	struct weston_seat *seat =
		wl_resource_get_user_data(seat_resource);
	struct weston_desktop_xdg_surface *surface =
		weston_desktop_surface_get_implementation_data(dsurface);

385
	weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic's avatar
Quentin Glidic committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	weston_desktop_api_show_window_menu(surface->desktop, dsurface, seat, x, y);
}

static void
weston_desktop_xdg_surface_protocol_move(struct wl_client *wl_client,
					 struct wl_resource *resource,
					 struct wl_resource *seat_resource,
					 uint32_t serial)
{
	struct weston_desktop_surface *dsurface =
		wl_resource_get_user_data(resource);
	struct weston_seat *seat =
		wl_resource_get_user_data(seat_resource);
	struct weston_desktop_xdg_surface *surface =
		weston_desktop_surface_get_implementation_data(dsurface);

402
	weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic's avatar
Quentin Glidic committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
	weston_desktop_api_move(surface->desktop, dsurface, seat, serial);
}

static void
weston_desktop_xdg_surface_protocol_resize(struct wl_client *wl_client,
					   struct wl_resource *resource,
					   struct wl_resource *seat_resource,
					   uint32_t serial,
					   enum xdg_surface_resize_edge edges)
{
	struct weston_desktop_surface *dsurface =
		wl_resource_get_user_data(resource);
	struct weston_seat *seat =
		wl_resource_get_user_data(seat_resource);
	struct weston_desktop_xdg_surface *surface =
		weston_desktop_surface_get_implementation_data(dsurface);
419 420
	enum weston_desktop_surface_edge surf_edges =
		(enum weston_desktop_surface_edge) edges;
Quentin Glidic's avatar
Quentin Glidic committed
421

422
	weston_desktop_xdg_surface_ensure_added(surface);
423
	weston_desktop_api_resize(surface->desktop, dsurface, seat, serial, surf_edges);
Quentin Glidic's avatar
Quentin Glidic committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
}

static void
weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client,
						  struct wl_resource *resource,
						  uint32_t serial)
{
	struct weston_desktop_surface *dsurface =
		wl_resource_get_user_data(resource);
	struct weston_desktop_xdg_surface *surface =
		weston_desktop_surface_get_implementation_data(dsurface);

	if (surface->configure_serial != serial)
		return;

	surface->next_state = surface->requested_state;
}

static void
weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client,
							struct wl_resource *resource,
							int32_t x, int32_t y,
							int32_t width, int32_t height)
{
	struct weston_desktop_surface *dsurface =
		wl_resource_get_user_data(resource);
	struct weston_desktop_xdg_surface *surface =
		weston_desktop_surface_get_implementation_data(dsurface);

	surface->has_next_geometry = true;
	surface->next_geometry.x = x;
	surface->next_geometry.y = y;
	surface->next_geometry.width = width;
	surface->next_geometry.height = height;
}

static void
weston_desktop_xdg_surface_protocol_set_maximized(struct wl_client *wl_client,
						  struct wl_resource *resource)
{
	struct weston_desktop_surface *dsurface =
		wl_resource_get_user_data(resource);
	struct weston_desktop_xdg_surface *surface =
		weston_desktop_surface_get_implementation_data(dsurface);

469
	weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic's avatar
Quentin Glidic committed
470 471 472 473 474 475 476 477 478 479 480 481
	weston_desktop_api_maximized_requested(surface->desktop, dsurface, true);
}

static void
weston_desktop_xdg_surface_protocol_unset_maximized(struct wl_client *wl_client,
						    struct wl_resource *resource)
{
	struct weston_desktop_surface *dsurface =
		wl_resource_get_user_data(resource);
	struct weston_desktop_xdg_surface *surface =
		weston_desktop_surface_get_implementation_data(dsurface);

482
	weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic's avatar
Quentin Glidic committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
	weston_desktop_api_maximized_requested(surface->desktop, dsurface, false);
}

static void
weston_desktop_xdg_surface_protocol_set_fullscreen(struct wl_client *wl_client,
						   struct wl_resource *resource,
						   struct wl_resource *output_resource)
{
	struct weston_desktop_surface *dsurface =
		wl_resource_get_user_data(resource);
	struct weston_desktop_xdg_surface *surface =
		weston_desktop_surface_get_implementation_data(dsurface);
	struct weston_output *output = NULL;

	if (output_resource != NULL)
		output = wl_resource_get_user_data(output_resource);

500
	weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic's avatar
Quentin Glidic committed
501 502 503 504 505 506 507 508 509 510 511 512 513
	weston_desktop_api_fullscreen_requested(surface->desktop, dsurface,
						true, output);
}

static void
weston_desktop_xdg_surface_protocol_unset_fullscreen(struct wl_client *wl_client,
					       struct wl_resource *resource)
{
	struct weston_desktop_surface *dsurface =
		wl_resource_get_user_data(resource);
	struct weston_desktop_xdg_surface *surface =
		weston_desktop_surface_get_implementation_data(dsurface);

514
	weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic's avatar
Quentin Glidic committed
515 516 517 518 519 520 521 522 523 524 525 526 527
	weston_desktop_api_fullscreen_requested(surface->desktop, dsurface,
						false, NULL);
}

static void
weston_desktop_xdg_surface_protocol_set_minimized(struct wl_client *wl_client,
						  struct wl_resource *resource)
{
	struct weston_desktop_surface *dsurface =
		wl_resource_get_user_data(resource);
	struct weston_desktop_xdg_surface *surface =
		weston_desktop_surface_get_implementation_data(dsurface);

528
	weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic's avatar
Quentin Glidic committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 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 597 598 599 600 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
	weston_desktop_api_minimized_requested(surface->desktop, dsurface);
}

static const struct xdg_surface_interface weston_desktop_xdg_surface_implementation = {
	.destroy             = weston_desktop_destroy_request,
	.set_parent          = weston_desktop_xdg_surface_protocol_set_parent,
	.set_title           = weston_desktop_xdg_surface_protocol_set_title,
	.set_app_id          = weston_desktop_xdg_surface_protocol_set_app_id,
	.show_window_menu    = weston_desktop_xdg_surface_protocol_show_window_menu,
	.move                = weston_desktop_xdg_surface_protocol_move,
	.resize              = weston_desktop_xdg_surface_protocol_resize,
	.ack_configure       = weston_desktop_xdg_surface_protocol_ack_configure,
	.set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry,
	.set_maximized       = weston_desktop_xdg_surface_protocol_set_maximized,
	.unset_maximized     = weston_desktop_xdg_surface_protocol_unset_maximized,
	.set_fullscreen      = weston_desktop_xdg_surface_protocol_set_fullscreen,
	.unset_fullscreen    = weston_desktop_xdg_surface_protocol_unset_fullscreen,
	.set_minimized       = weston_desktop_xdg_surface_protocol_set_minimized,
};

static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = {
	.set_maximized = weston_desktop_xdg_surface_set_maximized,
	.set_fullscreen = weston_desktop_xdg_surface_set_fullscreen,
	.set_resizing = weston_desktop_xdg_surface_set_resizing,
	.set_activated = weston_desktop_xdg_surface_set_activated,
	.set_size = weston_desktop_xdg_surface_set_size,
	.committed = weston_desktop_xdg_surface_committed,
	.ping = weston_desktop_xdg_surface_ping,
	.close = weston_desktop_xdg_surface_close,

	.get_maximized = weston_desktop_xdg_surface_get_maximized,
	.get_fullscreen = weston_desktop_xdg_surface_get_fullscreen,
	.get_resizing = weston_desktop_xdg_surface_get_resizing,
	.get_activated = weston_desktop_xdg_surface_get_activated,

	.destroy = weston_desktop_xdg_surface_destroy,
};

static void
weston_desktop_xdg_popup_close(struct weston_desktop_surface *dsurface,
			       void *user_data)
{
	struct weston_desktop_xdg_popup *popup = user_data;

	xdg_popup_send_popup_done(popup->resource);
}

static void
weston_desktop_xdg_popup_destroy(struct weston_desktop_surface *dsurface,
				 void *user_data)
{
	struct weston_desktop_xdg_popup *popup = user_data;
	struct weston_desktop_surface *topmost;
	struct weston_desktop_client *client =
		weston_desktop_surface_get_client(popup->popup);

	if (!weston_desktop_surface_get_grab(popup->popup))
		goto end;

	topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat);
	if (topmost != popup->popup) {
		struct wl_resource *client_resource =
			weston_desktop_client_get_resource(client);

		wl_resource_post_error(client_resource,
				       XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
				       "xdg_popup was destroyed while it was not the topmost popup.");
	}

	weston_desktop_surface_popup_ungrab(popup->popup, popup->seat);

end:
	free(popup);
}

static const struct xdg_popup_interface weston_desktop_xdg_popup_implementation = {
	.destroy             = weston_desktop_destroy_request,
};

static const struct weston_desktop_surface_implementation weston_desktop_xdg_popup_internal_implementation = {
	.close = weston_desktop_xdg_popup_close,

	.destroy = weston_desktop_xdg_popup_destroy,
};

static void
weston_desktop_xdg_shell_protocol_use_unstable_version(struct wl_client *wl_client,
						       struct wl_resource *resource,
						       int32_t version)
{
	if (version > 1) {
		wl_resource_post_error(resource,
				       1, "xdg_shell version not supported");
		return;
	}
}

static void
627
weston_desktop_xdg_surface_add_idle_callback(void *user_data)
Quentin Glidic's avatar
Quentin Glidic committed
628 629
{
	struct weston_desktop_xdg_surface *surface = user_data;
630

Quentin Glidic's avatar
Quentin Glidic committed
631
	surface->add_idle = NULL;
632
	weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic's avatar
Quentin Glidic committed
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
}

static void
weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client,
						  struct wl_resource *resource,
						  uint32_t id,
						  struct wl_resource *surface_resource)
{
	struct weston_desktop_client *client =
		wl_resource_get_user_data(resource);
	struct weston_desktop *desktop =
		weston_desktop_client_get_desktop(client);
	struct weston_surface *wsurface =
		wl_resource_get_user_data(surface_resource);
	struct weston_desktop_xdg_surface *surface;
	struct wl_display *display = weston_desktop_get_display(desktop);
	struct wl_event_loop *loop = wl_display_get_event_loop(display);

	if (weston_surface_set_role(wsurface, "xdg_surface", resource, XDG_SHELL_ERROR_ROLE) < 0)
		return;

	surface = zalloc(sizeof(struct weston_desktop_xdg_surface));
	if (surface == NULL) {
		wl_client_post_no_memory(wl_client);
		return;
	}

	surface->desktop = desktop;

	surface->surface =
		weston_desktop_surface_create(surface->desktop, client,
					      wsurface,
					      &weston_desktop_xdg_surface_internal_implementation,
					      surface);
	if (surface->surface == NULL) {
		free(surface);
		return;
	}

	surface->resource =
		weston_desktop_surface_add_resource(surface->surface,
						    &xdg_surface_interface,
						    &weston_desktop_xdg_surface_implementation,
						    id, NULL);
	if (surface->resource == NULL)
		return;

	surface->add_idle =
		wl_event_loop_add_idle(loop,
682
				       weston_desktop_xdg_surface_add_idle_callback,
Quentin Glidic's avatar
Quentin Glidic committed
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
				       surface);
}

static void
weston_desktop_xdg_shell_protocol_get_xdg_popup(struct wl_client *wl_client,
						struct wl_resource *resource,
						uint32_t id,
						struct wl_resource *surface_resource,
						struct wl_resource *parent_resource,
						struct wl_resource *seat_resource,
						uint32_t serial,
						int32_t x, int32_t y)
{
	struct weston_desktop_client *client =
		wl_resource_get_user_data(resource);
	struct weston_surface *wsurface =
		wl_resource_get_user_data(surface_resource);
	struct weston_surface *wparent =
		wl_resource_get_user_data(parent_resource);
	struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
	struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
	struct weston_desktop_surface *parent, *topmost;
	bool parent_is_popup, parent_is_xdg;
	struct weston_desktop_xdg_popup *popup;

	if (weston_surface_set_role(wsurface, "xdg_popup", resource, XDG_SHELL_ERROR_ROLE) < 0)
		return;

	if (!weston_surface_is_desktop_surface(wparent)) {
		wl_resource_post_error(resource,
				       XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
				       "xdg_popup parent was invalid");
		return;
	}

	parent = weston_surface_get_desktop_surface(wparent);
	parent_is_xdg =
		weston_desktop_surface_has_implementation(parent,
							  &weston_desktop_xdg_surface_internal_implementation);
	parent_is_popup =
		weston_desktop_surface_has_implementation(parent,
							  &weston_desktop_xdg_popup_internal_implementation);

	if (!parent_is_xdg && !parent_is_popup) {
		wl_resource_post_error(resource,
				       XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
				       "xdg_popup parent was invalid");
		return;
	}

	topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat);
	if ((topmost == NULL && parent_is_popup) ||
	    (topmost != NULL && topmost != parent)) {
		wl_resource_post_error(resource,
				       XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
				       "xdg_popup was not created on the topmost popup");
		return;
	}

	popup = zalloc(sizeof(struct weston_desktop_xdg_popup));
	if (popup == NULL) {
		wl_client_post_no_memory(wl_client);
		return;
	}

	popup->desktop = weston_desktop_client_get_desktop(client);
	popup->display = weston_desktop_get_display(popup->desktop);
	popup->seat = seat;

	popup->popup =
		weston_desktop_surface_create(popup->desktop, client, wsurface,
					      &weston_desktop_xdg_popup_internal_implementation,
					      popup);
	if (popup->popup == NULL) {
		free(popup);
		return;
	}

	popup->resource =
		weston_desktop_surface_add_resource(popup->popup,
						    &xdg_popup_interface,
						    &weston_desktop_xdg_popup_implementation,
						    id, NULL);
	if (popup->resource == NULL)
		return;

	weston_desktop_surface_set_relative_to(popup->popup, parent, x, y, false);
	weston_desktop_surface_popup_grab(popup->popup, popup->seat, serial);
}

static void
weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client,
				       struct wl_resource *resource,
				       uint32_t serial)
{
	struct weston_desktop_client *client =
		wl_resource_get_user_data(resource);

	weston_desktop_client_pong(client, serial);
}

static const struct xdg_shell_interface weston_desktop_xdg_shell_implementation = {
	.destroy = weston_desktop_destroy_request,
	.use_unstable_version = weston_desktop_xdg_shell_protocol_use_unstable_version,
	.get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface,
	.get_xdg_popup = weston_desktop_xdg_shell_protocol_get_xdg_popup,
	.pong = weston_desktop_xdg_shell_protocol_pong,
};

static int
xdg_shell_unversioned_dispatch(const void *implementation,
			       void *_target, uint32_t opcode,
			       const struct wl_message *message,
			       union wl_argument *args)
{
	struct wl_resource *resource = _target;
	struct weston_desktop_client *client =
		wl_resource_get_user_data(resource);

	if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
		wl_resource_post_error(resource,
				       WL_DISPLAY_ERROR_INVALID_OBJECT,
				       "must call use_unstable_version first");
		return 0;
	}

#define XDG_SERVER_VERSION 5

	if (args[0].i != XDG_SERVER_VERSION) {
		wl_resource_post_error(resource,
				       WL_DISPLAY_ERROR_INVALID_OBJECT,
				       "incompatible version, server is %d " "client wants %d",
				       XDG_SERVER_VERSION, args[0].i);
		return 0;
	}

	wl_resource_set_implementation(resource,
				       &weston_desktop_xdg_shell_implementation,
				       client, implementation);

	return 1;
}

static void
weston_desktop_xdg_shell_bind(struct wl_client *client, void *data,
			      uint32_t version, uint32_t id)
{
	struct weston_desktop *desktop = data;

	weston_desktop_client_create(desktop, client,
				     xdg_shell_unversioned_dispatch,
				     &xdg_shell_interface, NULL, version, id);
}

struct wl_global *
weston_desktop_xdg_shell_v5_create(struct weston_desktop *desktop,
				   struct wl_display *display)
{
	return wl_global_create(display,
				&xdg_shell_interface,
				WD_XDG_SHELL_PROTOCOL_VERSION,
				desktop, weston_desktop_xdg_shell_bind);
}