neuswc

patched neuswc library (tracked)
git clone https://git.daat.foo/neuswc.git
Log | Files | Refs | README | LICENSE

layer_shell.c (21822B)


      1 #include "layer_shell.h"
      2 
      3 #include "compositor.h"
      4 #include "internal.h"
      5 #include "keyboard.h"
      6 #include "output.h"
      7 #include "screen.h"
      8 #include "seat.h"
      9 #include "surface.h"
     10 #include "util.h"
     11 #include "view.h"
     12 #include "xdg_shell.h"
     13 
     14 #include "wlr-layer-shell-unstable-v1-server-protocol.h"
     15 #include <assert.h>
     16 #include <inttypes.h>
     17 #include <stdlib.h>
     18 
     19 struct layer_surface {
     20 	struct wl_resource *resource;
     21 	struct wl_listener surface_destroy_listener;
     22 	struct wl_listener surface_commit_listener;
     23 	struct compositor_view *view;
     24 	struct view_handler view_handler;
     25 	struct screen *screen;
     26 	struct screen_modifier modifier;
     27 	struct layer_surface_state {
     28 		uint32_t layer;
     29 		uint32_t anchor;
     30 		int32_t exclusive_zone;
     31 		uint32_t exclusive_edge;
     32 		uint32_t keyboard_interactivity;
     33 		struct {
     34 			int32_t top, right, bottom, left;
     35 		} margin;
     36 		uint32_t desired_width, desired_height;
     37 	} current, pending;
     38 	uint32_t configure_serial;
     39 	/* screen size the last configure was computed from */
     40 	uint32_t screen_width, screen_height;
     41 	bool configured;
     42 	bool mapped;
     43 };
     44 
     45 static bool
     46 state_equal(const struct layer_surface_state *a,
     47             const struct layer_surface_state *b)
     48 {
     49 	return a->layer == b->layer && a->anchor == b->anchor &&
     50 	       a->exclusive_zone == b->exclusive_zone &&
     51 	       a->exclusive_edge == b->exclusive_edge &&
     52 	       a->keyboard_interactivity == b->keyboard_interactivity &&
     53 	       a->margin.top == b->margin.top &&
     54 	       a->margin.right == b->margin.right &&
     55 	       a->margin.bottom == b->margin.bottom &&
     56 	       a->margin.left == b->margin.left &&
     57 	       a->desired_width == b->desired_width &&
     58 	       a->desired_height == b->desired_height;
     59 }
     60 
     61 static int32_t
     62 available_size(uint32_t size, int32_t start_margin, int32_t end_margin)
     63 {
     64 	int32_t available = (int32_t)size - start_margin - end_margin;
     65 
     66 	return MAX(available, 0);
     67 }
     68 
     69 static uint32_t
     70 configure_size(uint32_t desired, uint32_t anchor, uint32_t first_anchor,
     71                uint32_t second_anchor, uint32_t total_size, int32_t start_margin,
     72                int32_t end_margin)
     73 {
     74 	if (desired != 0) {
     75 		return desired;
     76 	}
     77 
     78 	if ((anchor & first_anchor) && (anchor & second_anchor)) {
     79 		return available_size(total_size, start_margin, end_margin);
     80 	}
     81 
     82 	return 0;
     83 }
     84 
     85 static uint32_t
     86 exclusive_edge(struct layer_surface *surface)
     87 {
     88 	if (surface->current.exclusive_edge != 0) {
     89 		return surface->current.exclusive_edge;
     90 	}
     91 
     92 	switch (surface->current.anchor) {
     93 	case ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
     94 	    ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
     95 	    ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT:
     96 	case ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP:
     97 		return ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP;
     98 	case ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
     99 	    ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
    100 	    ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT:
    101 	case ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM:
    102 		return ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
    103 	case ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
    104 	    ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
    105 	    ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM:
    106 	case ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT:
    107 		return ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT;
    108 	case ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT |
    109 	    ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
    110 	    ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM:
    111 	case ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT:
    112 		return ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
    113 	default:
    114 		return 0;
    115 	}
    116 }
    117 
    118 static int32_t
    119 exclusive_size(struct layer_surface *surface)
    120 {
    121 	switch (surface->current.exclusive_zone) {
    122 	case 0:
    123 		return 0;
    124 	case -1:
    125 		switch (exclusive_edge(surface)) {
    126 		case ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP:
    127 			return (int32_t)surface->view->base.geometry.height +
    128 			       surface->current.margin.top;
    129 		case ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM:
    130 			return (int32_t)surface->view->base.geometry.height +
    131 			       surface->current.margin.bottom;
    132 		case ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT:
    133 			return (int32_t)surface->view->base.geometry.width +
    134 			       surface->current.margin.left;
    135 		case ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT:
    136 			return (int32_t)surface->view->base.geometry.width +
    137 			       surface->current.margin.right;
    138 		default:
    139 			return 0;
    140 		}
    141 	default:
    142 			return MAX(surface->current.exclusive_zone, 0);
    143 	}
    144 }
    145 
    146 static void
    147 update_usable_geometry(struct layer_surface *surface)
    148 {
    149 	if (!surface->screen) {
    150 		return;
    151 	}
    152 
    153 	screen_update_usable_geometry(surface->screen);
    154 }
    155 
    156 static void
    157 restack_layer(struct layer_surface *surface)
    158 {
    159 	int32_t z = STACK_LAYER_NORMAL;
    160 	bool always_top = false;
    161 
    162 	switch (surface->current.layer) {
    163 	case ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND:
    164 		z = STACK_LAYER_BACKGROUND;
    165 		break;
    166 	case ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM:
    167 		z = STACK_LAYER_BOTTOM;
    168 		break;
    169 	case ZWLR_LAYER_SHELL_V1_LAYER_TOP:
    170 		z = STACK_LAYER_TOP;
    171 		always_top = true;
    172 		break;
    173 	case ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY:
    174 		z = STACK_LAYER_OVERLAY;
    175 		always_top = true;
    176 		break;
    177 	default:
    178 		break;
    179 	}
    180 
    181 	surface->view->always_top = always_top;
    182 	compositor_view_set_z(surface->view, z, true);
    183 }
    184 
    185 /* zone 0 surfaces keep clear of reserved zones, others span the screen */
    186 static const struct swc_rectangle *
    187 area_of(const struct layer_surface *surface)
    188 {
    189 	const struct swc_rectangle *usable = &surface->screen->base.usable_geometry;
    190 
    191 	if (surface->current.exclusive_zone == 0 && usable->width && usable->height) {
    192 		return usable;
    193 	}
    194 	return &surface->screen->base.geometry;
    195 }
    196 
    197 static void
    198 update_position(struct layer_surface *surface)
    199 {
    200 	const struct swc_rectangle *screen = area_of(surface);
    201 	struct swc_rectangle *view = &surface->view->base.geometry;
    202 	int32_t x, y;
    203 
    204 	if ((surface->current.anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) &&
    205 	    !(surface->current.anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT)) {
    206 		x = screen->x + surface->current.margin.left;
    207 	} else if ((surface->current.anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT) &&
    208 	           !(surface->current.anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT)) {
    209 		x = screen->x + (int32_t)screen->width - (int32_t)view->width -
    210 		    surface->current.margin.right;
    211 	} else {
    212 		x = screen->x + ((int32_t)screen->width - (int32_t)view->width +
    213 		                 surface->current.margin.left -
    214 		                 surface->current.margin.right) /
    215 		                2;
    216 	}
    217 
    218 	if ((surface->current.anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) &&
    219 	    !(surface->current.anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM)) {
    220 		y = screen->y + surface->current.margin.top;
    221 	} else if ((surface->current.anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM) &&
    222 	           !(surface->current.anchor & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP)) {
    223 		y = screen->y + (int32_t)screen->height - (int32_t)view->height -
    224 		    surface->current.margin.bottom;
    225 	} else {
    226 		y = screen->y + ((int32_t)screen->height - (int32_t)view->height +
    227 		                 surface->current.margin.top -
    228 		                 surface->current.margin.bottom) /
    229 		                2;
    230 	}
    231 
    232 	view_move(&surface->view->base, x, y);
    233 }
    234 
    235 static void
    236 send_configure(struct layer_surface *surface)
    237 {
    238 	uint32_t width, height;
    239 
    240 	if (!surface->screen) {
    241 		return;
    242 	}
    243 
    244 	width = configure_size(surface->current.desired_width, surface->current.anchor,
    245 	                       ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT,
    246 	                       ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT,
    247 	                       area_of(surface)->width,
    248 	                       surface->current.margin.left,
    249 	                       surface->current.margin.right);
    250 	height =
    251 	    configure_size(surface->current.desired_height, surface->current.anchor,
    252 	                   ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP,
    253 	                   ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM,
    254 	                   area_of(surface)->height,
    255 	                   surface->current.margin.top,
    256 	                   surface->current.margin.bottom);
    257 	surface->screen_width = surface->screen->base.geometry.width;
    258 	surface->screen_height = surface->screen->base.geometry.height;
    259 	surface->configure_serial = wl_display_next_serial(swc.display);
    260 	zwlr_layer_surface_v1_send_configure(surface->resource,
    261 	                                     surface->configure_serial, width,
    262 	                                     height);
    263 }
    264 
    265 static void
    266 set_size(struct wl_client *client, struct wl_resource *resource, uint32_t width,
    267          uint32_t height)
    268 {
    269 	struct layer_surface *surface = wl_resource_get_user_data(resource);
    270 
    271 	(void)client;
    272 
    273 	surface->pending.desired_width = width;
    274 	surface->pending.desired_height = height;
    275 }
    276 
    277 static void
    278 set_anchor(struct wl_client *client, struct wl_resource *resource,
    279            uint32_t anchor)
    280 {
    281 	struct layer_surface *surface = wl_resource_get_user_data(resource);
    282 
    283 	(void)client;
    284 
    285 	surface->pending.anchor = anchor;
    286 }
    287 
    288 static void
    289 set_exclusive_zone(struct wl_client *client, struct wl_resource *resource,
    290                    int32_t zone)
    291 {
    292 	struct layer_surface *surface = wl_resource_get_user_data(resource);
    293 
    294 	(void)client;
    295 
    296 	surface->pending.exclusive_zone = zone;
    297 }
    298 
    299 static void
    300 set_margin(struct wl_client *client, struct wl_resource *resource, int32_t top,
    301            int32_t right, int32_t bottom, int32_t left)
    302 {
    303 	struct layer_surface *surface = wl_resource_get_user_data(resource);
    304 
    305 	(void)client;
    306 
    307 	surface->pending.margin.top = top;
    308 	surface->pending.margin.right = right;
    309 	surface->pending.margin.bottom = bottom;
    310 	surface->pending.margin.left = left;
    311 }
    312 
    313 static void
    314 set_keyboard_interactivity(struct wl_client *client, struct wl_resource *resource,
    315                            uint32_t keyboard_interactivity)
    316 {
    317 	struct layer_surface *surface = wl_resource_get_user_data(resource);
    318 
    319 	(void)client;
    320 
    321 	surface->pending.keyboard_interactivity = keyboard_interactivity;
    322 }
    323 
    324 static void
    325 get_popup(struct wl_client *client, struct wl_resource *resource,
    326           struct wl_resource *popup)
    327 {
    328 	struct layer_surface *surface = wl_resource_get_user_data(resource);
    329 
    330 	(void)client;
    331 	if (surface->view) {
    332 		xdg_popup_set_parent(popup, surface->view->surface);
    333 	}
    334 }
    335 
    336 static void
    337 ack_configure(struct wl_client *client, struct wl_resource *resource,
    338               uint32_t serial)
    339 {
    340 	struct layer_surface *surface = wl_resource_get_user_data(resource);
    341 
    342 	(void)client;
    343 
    344 	if (serial == surface->configure_serial) {
    345 		update_position(surface);
    346 	}
    347 }
    348 
    349 static void
    350 set_layer(struct wl_client *client, struct wl_resource *resource, uint32_t layer)
    351 {
    352 	struct layer_surface *surface = wl_resource_get_user_data(resource);
    353 
    354 	(void)client;
    355 
    356 	if (layer > ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY) {
    357 		wl_resource_post_error(resource,
    358 		                       ZWLR_LAYER_SHELL_V1_ERROR_INVALID_LAYER,
    359 		                       "invalid layer %" PRIu32, layer);
    360 		return;
    361 	}
    362 
    363 	surface->pending.layer = layer;
    364 }
    365 
    366 static void
    367 set_exclusive_edge(struct wl_client *client, struct wl_resource *resource,
    368                    uint32_t edge)
    369 {
    370 	struct layer_surface *surface = wl_resource_get_user_data(resource);
    371 
    372 	(void)client;
    373 
    374 	surface->pending.exclusive_edge = edge;
    375 }
    376 
    377 static const struct zwlr_layer_surface_v1_interface layer_surface_impl = {
    378     .set_size = set_size,
    379     .set_anchor = set_anchor,
    380     .set_exclusive_zone = set_exclusive_zone,
    381     .set_margin = set_margin,
    382     .set_keyboard_interactivity = set_keyboard_interactivity,
    383     .get_popup = get_popup,
    384     .ack_configure = ack_configure,
    385     .destroy = destroy_resource,
    386     .set_layer = set_layer,
    387     .set_exclusive_edge = set_exclusive_edge,
    388 };
    389 
    390 /* an exclusive surface has every key while it is mapped; the view a
    391  * window asked for meanwhile gets them when it goes */
    392 static struct {
    393 	struct layer_surface *holder;
    394 	struct compositor_view *wanted;
    395 	struct wl_listener wanted_destroy;
    396 } keys;
    397 
    398 static void
    399 handle_wanted_destroy(struct wl_listener *listener, void *data)
    400 {
    401 	(void)data;
    402 	wl_list_remove(&listener->link);
    403 	keys.wanted = NULL;
    404 }
    405 
    406 static void
    407 want_keys(struct compositor_view *view)
    408 {
    409 	if (keys.wanted) {
    410 		wl_list_remove(&keys.wanted_destroy.link);
    411 	}
    412 	keys.wanted = view;
    413 	if (view) {
    414 		keys.wanted_destroy.notify = handle_wanted_destroy;
    415 		wl_signal_add(&view->destroy_signal, &keys.wanted_destroy);
    416 	}
    417 }
    418 
    419 static void
    420 hold_keys(struct layer_surface *surface)
    421 {
    422 	struct compositor_view *focus = swc.seat->keyboard->focus.view;
    423 
    424 	/* a second exclusive surface over the first gives back what the first held */
    425 	if (!keys.holder) {
    426 		want_keys(focus);
    427 	}
    428 	keys.holder = surface;
    429 	keyboard_set_focus(swc.seat->keyboard, surface->view);
    430 }
    431 
    432 static void
    433 release_keys(struct layer_surface *surface)
    434 {
    435 	struct compositor_view *view = keys.wanted;
    436 
    437 	if (keys.holder != surface) {
    438 		return;
    439 	}
    440 	keys.holder = NULL;
    441 	want_keys(NULL);
    442 	if (swc.seat->keyboard->focus.view == surface->view) {
    443 		keyboard_set_focus(swc.seat->keyboard, view);
    444 	}
    445 }
    446 
    447 bool
    448 layer_shell_hold_keys(struct compositor_view *view)
    449 {
    450 	if (!keys.holder) {
    451 		return false;
    452 	}
    453 	want_keys(view);
    454 	return true;
    455 }
    456 
    457 static void
    458 handle_attach(struct view_handler *handler)
    459 {
    460 	struct layer_surface *surface = wl_container_of(handler, surface, view_handler);
    461 	bool mapped = surface->view->base.buffer != NULL;
    462 
    463 	if (mapped) {
    464 		update_position(surface);
    465 		/* raise on map only, or it covers its own popups */
    466 		if (!surface->mapped) {
    467 			restack_layer(surface);
    468 			compositor_view_show(surface->view);
    469 			/* on demand waits for a press, see layer_shell_pressed */
    470 			if (surface->current.keyboard_interactivity ==
    471 			    ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE) {
    472 				hold_keys(surface);
    473 			}
    474 		}
    475 	} else if (surface->mapped) {
    476 		release_keys(surface);
    477 		compositor_view_hide(surface->view);
    478 	}
    479 
    480 	surface->mapped = mapped;
    481 	update_usable_geometry(surface);
    482 }
    483 
    484 static void
    485 handle_resize(struct view_handler *handler, uint32_t old_width,
    486               uint32_t old_height)
    487 {
    488 	struct layer_surface *surface = wl_container_of(handler, surface, view_handler);
    489 
    490 	(void)old_width;
    491 	(void)old_height;
    492 
    493 	update_position(surface);
    494 	update_usable_geometry(surface);
    495 }
    496 
    497 static const struct view_handler_impl view_handler_impl = {
    498     .attach = handle_attach,
    499     .resize = handle_resize,
    500 };
    501 
    502 static void
    503 handle_surface_commit(struct wl_listener *listener, void *data)
    504 {
    505 	struct layer_surface *surface =
    506 	    wl_container_of(listener, surface, surface_commit_listener);
    507 	bool state_changed = !state_equal(&surface->current, &surface->pending);
    508 
    509 	(void)data;
    510 
    511 	if (state_changed) {
    512 		surface->current = surface->pending;
    513 		restack_layer(surface);
    514 		update_usable_geometry(surface);
    515 	}
    516 
    517 	/* Make sure that the initial commit and also any later state change gets a fresh
    518 	 * configure  */
    519 	if (!surface->configured || state_changed) {
    520 		send_configure(surface);
    521 		surface->configured = true;
    522 	}
    523 }
    524 
    525 static void modify(struct screen_modifier *modifier, const struct swc_rectangle *geom,
    526                    pixman_region32_t *usable);
    527 
    528 void
    529 layer_shell_usable_changed(struct screen *screen)
    530 {
    531 	struct screen_modifier *modifier;
    532 	struct layer_surface *surface;
    533 
    534 	wl_list_for_each(modifier, &screen->modifiers, link)
    535 	{
    536 		if (modifier->modify != modify) {
    537 			continue;
    538 		}
    539 		surface = wl_container_of(modifier, surface, modifier);
    540 		if (surface->configured && surface->current.exclusive_zone == 0) {
    541 			send_configure(surface);
    542 			update_position(surface);
    543 		}
    544 	}
    545 }
    546 
    547 static void
    548 modify(struct screen_modifier *modifier, const struct swc_rectangle *geom,
    549        pixman_region32_t *usable)
    550 {
    551 	struct layer_surface *surface = wl_container_of(modifier, surface, modifier);
    552 	pixman_box32_t box = {.x1 = geom->x,
    553 	                      .y1 = geom->y,
    554 	                      .x2 = geom->x + geom->width,
    555 	                      .y2 = geom->y + geom->height};
    556 	int32_t size;
    557 
    558 	/* every usable-area pass visits us, so notice a size change here */
    559 	if (surface->configured && (geom->width != surface->screen_width ||
    560 	                            geom->height != surface->screen_height)) {
    561 		send_configure(surface);
    562 		update_position(surface);
    563 	}
    564 
    565 	if (!surface->mapped) {
    566 		pixman_region32_reset(usable, &box);
    567 		return;
    568 	}
    569 
    570 	size = exclusive_size(surface);
    571 	if (size <= 0) {
    572 		pixman_region32_reset(usable, &box);
    573 		return;
    574 	}
    575 
    576 	/* shrink usable area */
    577 	switch (exclusive_edge(surface)) {
    578 	case ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP:
    579 		box.y1 = MAX(box.y1, geom->y + size);
    580 		break;
    581 	case ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM:
    582 		box.y2 = MIN(box.y2, geom->y + geom->height - size);
    583 		break;
    584 	case ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT:
    585 		box.x1 = MAX(box.x1, geom->x + size);
    586 		break;
    587 	case ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT:
    588 		box.x2 = MIN(box.x2, geom->x + geom->width - size);
    589 		break;
    590 	default:
    591 		break;
    592 	}
    593 
    594 	pixman_region32_reset(usable, &box);
    595 }
    596 
    597 static void
    598 destroy_layer_surface(struct wl_resource *resource)
    599 {
    600 	struct layer_surface *surface = wl_resource_get_user_data(resource);
    601 	bool had_screen = surface->screen != NULL;
    602 
    603 	wl_list_remove(&surface->surface_destroy_listener.link);
    604 	wl_list_remove(&surface->surface_commit_listener.link);
    605 	wl_list_remove(&surface->modifier.link);
    606 	release_keys(surface);
    607 	compositor_view_destroy(surface->view);
    608 	if (had_screen) {
    609 		screen_update_usable_geometry(surface->screen);
    610 	}
    611 	free(surface);
    612 }
    613 
    614 static void
    615 handle_surface_destroy(struct wl_listener *listener, void *data)
    616 {
    617 	struct layer_surface *surface =
    618 	    wl_container_of(listener, surface, surface_destroy_listener);
    619 
    620 	(void)data;
    621 
    622 	wl_resource_destroy(surface->resource);
    623 }
    624 
    625 static struct layer_surface *
    626 layer_surface_new(struct wl_client *client, uint32_t version, uint32_t id,
    627                   struct surface *surface, struct screen *screen, uint32_t layer)
    628 {
    629 	struct layer_surface *layer_surface;
    630 
    631 	layer_surface = calloc(1, sizeof(*layer_surface));
    632 	if (!layer_surface) {
    633 		goto error0;
    634 	}
    635 
    636 	layer_surface->resource =
    637 	    wl_resource_create(client, &zwlr_layer_surface_v1_interface, version, id);
    638 	if (!layer_surface->resource) {
    639 		goto error1;
    640 	}
    641 
    642 	if (!(layer_surface->view = compositor_create_view(surface))) {
    643 		goto error2;
    644 	}
    645 
    646 	if (!surface_set_role(surface, layer_surface->resource)) {
    647 		goto error3;
    648 	}
    649 
    650 	layer_surface->screen = screen;
    651 	layer_surface->current.layer = layer;
    652 	layer_surface->current.exclusive_zone = 0;
    653 	layer_surface->current.exclusive_edge = 0;
    654 	layer_surface->current.keyboard_interactivity =
    655 	    ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE;
    656 	layer_surface->pending = layer_surface->current;
    657 	layer_surface->surface_destroy_listener.notify = handle_surface_destroy;
    658 	layer_surface->surface_commit_listener.notify = handle_surface_commit;
    659 	layer_surface->view_handler.impl = &view_handler_impl;
    660 	layer_surface->modifier.modify = modify;
    661 	wl_list_init(&layer_surface->modifier.link);
    662 	wl_resource_set_implementation(layer_surface->resource, &layer_surface_impl,
    663 	                               layer_surface, destroy_layer_surface);
    664 	wl_resource_add_destroy_listener(surface->resource,
    665 	                                 &layer_surface->surface_destroy_listener);
    666 	wl_signal_add(&surface->signal.commit, &layer_surface->surface_commit_listener);
    667 	wl_list_insert(&layer_surface->view->base.handlers,
    668 	               &layer_surface->view_handler.link);
    669 	wl_list_insert(&screen->modifiers, &layer_surface->modifier.link);
    670 	restack_layer(layer_surface);
    671 
    672 	return layer_surface;
    673 
    674 error3:
    675 	compositor_view_destroy(layer_surface->view);
    676 error2:
    677 	wl_resource_destroy(layer_surface->resource);
    678 error1:
    679 	free(layer_surface);
    680 error0:
    681 	return NULL;
    682 }
    683 
    684 static void
    685 get_layer_surface(struct wl_client *client, struct wl_resource *resource,
    686                   uint32_t id, struct wl_resource *surface_resource,
    687                   struct wl_resource *output_resource, uint32_t layer,
    688                   const char *namespace_)
    689 {
    690 	struct surface *surface = wl_resource_get_user_data(surface_resource);
    691 	struct output *output;
    692 	struct screen *screen;
    693 
    694 	(void)namespace_;
    695 
    696 	if (layer > ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY) {
    697 		wl_resource_post_error(resource,
    698 		                       ZWLR_LAYER_SHELL_V1_ERROR_INVALID_LAYER,
    699 		                       "invalid layer %" PRIu32, layer);
    700 		return;
    701 	}
    702 
    703 	if (surface->role) {
    704 		wl_resource_post_error(resource, ZWLR_LAYER_SHELL_V1_ERROR_ROLE,
    705 		                       "surface already has a role");
    706 		return;
    707 	}
    708 
    709 	if (surface_has_buffer(surface)) {
    710 		wl_resource_post_error(resource,
    711 		                       ZWLR_LAYER_SHELL_V1_ERROR_ALREADY_CONSTRUCTED,
    712 		                       "surface already has a buffer");
    713 		return;
    714 	}
    715 
    716 	if (output_resource) {
    717 		output = wl_resource_get_user_data(output_resource);
    718 		screen = output ? output->screen : NULL;
    719 	} else if (!wl_list_empty(&swc.screens)) {
    720 		screen = wl_container_of(swc.screens.next, screen, link);
    721 	} else {
    722 		screen = NULL;
    723 	}
    724 
    725 	if (!screen ||
    726 	    !layer_surface_new(client, wl_resource_get_version(resource), id, surface,
    727 	                       screen, layer)) {
    728 		wl_client_post_no_memory(client);
    729 	}
    730 }
    731 
    732 static const struct zwlr_layer_shell_v1_interface layer_shell_impl = {
    733     .get_layer_surface = get_layer_surface,
    734     .destroy = destroy_resource,
    735 };
    736 
    737 static void
    738 bind_layer_shell(struct wl_client *client, void *data, uint32_t version,
    739                  uint32_t id)
    740 {
    741 	struct wl_resource *resource;
    742 
    743 	(void)data;
    744 
    745 	resource =
    746 	    wl_resource_create(client, &zwlr_layer_shell_v1_interface, version, id);
    747 	if (!resource) {
    748 		wl_client_post_no_memory(client);
    749 		return;
    750 	}
    751 
    752 	wl_resource_set_implementation(resource, &layer_shell_impl, NULL, NULL);
    753 }
    754 
    755 void
    756 layer_shell_pressed(struct compositor_view *view)
    757 {
    758 	struct view_handler *handler;
    759 	struct layer_surface *surface;
    760 
    761 	if (!view) {
    762 		return;
    763 	}
    764 	wl_list_for_each(handler, &view->base.handlers, link) {
    765 		if (handler->impl != &view_handler_impl) {
    766 			continue;
    767 		}
    768 		surface = wl_container_of(handler, surface, view_handler);
    769 		if (surface->current.keyboard_interactivity ==
    770 		    ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_ON_DEMAND) {
    771 			keyboard_set_focus(swc.seat->keyboard, view);
    772 		}
    773 		return;
    774 	}
    775 }
    776 
    777 struct wl_global *
    778 layer_shell_create(struct wl_display *display)
    779 {
    780 	return wl_global_create(display, &zwlr_layer_shell_v1_interface, 5, NULL,
    781 	                        bind_layer_shell);
    782 }