2018-03-17 23:03:52 +00:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2018-03-17 19:07:48 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2018-03-17 23:03:52 +00:00
|
|
|
#include <string.h>
|
2019-07-27 08:53:54 +00:00
|
|
|
#include <wayland-server-core.h>
|
2018-09-14 17:32:33 +00:00
|
|
|
#include <wlr/types/wlr_layer_shell_v1.h>
|
2018-03-20 03:11:37 +00:00
|
|
|
#include <wlr/types/wlr_output.h>
|
2018-03-17 23:03:52 +00:00
|
|
|
#include <wlr/types/wlr_surface.h>
|
2018-04-14 19:47:51 +00:00
|
|
|
#include <wlr/types/wlr_xdg_shell.h>
|
2018-03-17 23:03:52 +00:00
|
|
|
#include <wlr/util/log.h>
|
|
|
|
#include "util/signal.h"
|
2018-03-17 19:07:48 +00:00
|
|
|
#include "wlr-layer-shell-unstable-v1-protocol.h"
|
|
|
|
|
2018-03-17 23:03:52 +00:00
|
|
|
static void resource_handle_destroy(struct wl_client *client,
|
|
|
|
struct wl_resource *resource) {
|
|
|
|
wl_resource_destroy(resource);
|
2018-03-17 19:07:48 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 00:24:16 +00:00
|
|
|
static const struct zwlr_layer_shell_v1_interface layer_shell_implementation;
|
|
|
|
static const struct zwlr_layer_surface_v1_interface layer_surface_implementation;
|
2018-03-17 19:07:48 +00:00
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
static struct wlr_layer_shell_v1 *layer_shell_from_resource(
|
2018-03-17 19:07:48 +00:00
|
|
|
struct wl_resource *resource) {
|
|
|
|
assert(wl_resource_instance_of(resource, &zwlr_layer_shell_v1_interface,
|
2018-03-20 00:24:16 +00:00
|
|
|
&layer_shell_implementation));
|
2018-03-17 19:07:48 +00:00
|
|
|
return wl_resource_get_user_data(resource);
|
|
|
|
}
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
static struct wlr_layer_surface_v1 *layer_surface_from_resource(
|
2018-03-17 23:03:52 +00:00
|
|
|
struct wl_resource *resource) {
|
|
|
|
assert(wl_resource_instance_of(resource, &zwlr_layer_surface_v1_interface,
|
2018-03-20 00:24:16 +00:00
|
|
|
&layer_surface_implementation));
|
2018-03-17 23:03:52 +00:00
|
|
|
return wl_resource_get_user_data(resource);
|
|
|
|
}
|
|
|
|
|
2018-07-07 21:45:16 +00:00
|
|
|
static const struct wlr_surface_role layer_surface_role;
|
|
|
|
|
2018-04-03 00:39:33 +00:00
|
|
|
bool wlr_surface_is_layer_surface(struct wlr_surface *surface) {
|
2018-07-07 21:45:16 +00:00
|
|
|
return surface->role == &layer_surface_role;
|
2018-04-03 00:39:33 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1 *wlr_layer_surface_v1_from_wlr_surface(
|
2018-04-03 00:39:33 +00:00
|
|
|
struct wlr_surface *surface) {
|
|
|
|
assert(wlr_surface_is_layer_surface(surface));
|
2018-09-14 17:32:33 +00:00
|
|
|
return (struct wlr_layer_surface_v1 *)surface->role_data;
|
2018-04-03 00:39:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 23:11:14 +00:00
|
|
|
static void layer_surface_configure_destroy(
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1_configure *configure) {
|
2018-03-17 23:11:14 +00:00
|
|
|
if (configure == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wl_list_remove(&configure->link);
|
|
|
|
free(configure);
|
|
|
|
}
|
|
|
|
|
2018-03-17 23:03:52 +00:00
|
|
|
static void layer_surface_handle_ack_configure(struct wl_client *client,
|
|
|
|
struct wl_resource *resource, uint32_t serial) {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1 *surface = layer_surface_from_resource(resource);
|
2018-03-17 23:11:14 +00:00
|
|
|
|
|
|
|
bool found = false;
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1_configure *configure, *tmp;
|
2019-07-16 12:44:50 +00:00
|
|
|
|
2019-09-13 10:51:53 +00:00
|
|
|
if (!surface || surface->closed) {
|
2019-07-16 12:44:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-17 23:11:14 +00:00
|
|
|
wl_list_for_each_safe(configure, tmp, &surface->configure_list, link) {
|
|
|
|
if (configure->serial < serial) {
|
|
|
|
layer_surface_configure_destroy(configure);
|
|
|
|
} else if (configure->serial == serial) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2018-03-27 21:18:16 +00:00
|
|
|
wl_resource_post_error(resource,
|
2018-03-17 23:11:14 +00:00
|
|
|
ZWLR_LAYER_SURFACE_V1_ERROR_INVALID_SURFACE_STATE,
|
|
|
|
"wrong configure serial: %u", serial);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-28 02:50:55 +00:00
|
|
|
if (surface->acked_configure) {
|
|
|
|
layer_surface_configure_destroy(surface->acked_configure);
|
|
|
|
}
|
|
|
|
surface->acked_configure = configure;
|
|
|
|
wl_list_remove(&configure->link);
|
|
|
|
wl_list_init(&configure->link);
|
2018-03-17 23:03:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 03:11:37 +00:00
|
|
|
static void layer_surface_handle_set_size(struct wl_client *client,
|
2018-03-26 22:12:31 +00:00
|
|
|
struct wl_resource *resource, uint32_t width, uint32_t height) {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1 *surface = layer_surface_from_resource(resource);
|
2019-07-16 12:44:50 +00:00
|
|
|
|
|
|
|
if (!surface) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-26 22:12:31 +00:00
|
|
|
surface->client_pending.desired_width = width;
|
|
|
|
surface->client_pending.desired_height = height;
|
2018-03-20 03:11:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 23:03:52 +00:00
|
|
|
static void layer_surface_handle_set_anchor(struct wl_client *client,
|
|
|
|
struct wl_resource *resource, uint32_t anchor) {
|
2018-03-20 03:11:37 +00:00
|
|
|
const uint32_t max_anchor =
|
|
|
|
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
|
|
|
|
ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
|
|
|
|
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
|
|
|
|
ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
|
|
|
|
if (anchor > max_anchor) {
|
|
|
|
wl_resource_post_error(resource,
|
|
|
|
ZWLR_LAYER_SURFACE_V1_ERROR_INVALID_ANCHOR,
|
|
|
|
"invalid anchor %d", anchor);
|
|
|
|
}
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1 *surface = layer_surface_from_resource(resource);
|
2019-07-16 12:44:50 +00:00
|
|
|
|
|
|
|
if (!surface) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-18 17:58:01 +00:00
|
|
|
surface->client_pending.anchor = anchor;
|
2018-03-17 23:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void layer_surface_handle_set_exclusive_zone(struct wl_client *client,
|
2018-03-26 22:12:31 +00:00
|
|
|
struct wl_resource *resource, int32_t zone) {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1 *surface = layer_surface_from_resource(resource);
|
2019-07-16 12:44:50 +00:00
|
|
|
|
|
|
|
if (!surface) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-18 17:58:01 +00:00
|
|
|
surface->client_pending.exclusive_zone = zone;
|
2018-03-17 23:03:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 17:58:01 +00:00
|
|
|
static void layer_surface_handle_set_margin(
|
|
|
|
struct wl_client *client, struct wl_resource *resource,
|
|
|
|
int32_t top, int32_t right, int32_t bottom, int32_t left) {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1 *surface = layer_surface_from_resource(resource);
|
2019-07-16 12:44:50 +00:00
|
|
|
|
|
|
|
if (!surface) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-18 17:58:01 +00:00
|
|
|
surface->client_pending.margin.top = top;
|
|
|
|
surface->client_pending.margin.right = right;
|
|
|
|
surface->client_pending.margin.bottom = bottom;
|
|
|
|
surface->client_pending.margin.left = left;
|
2018-03-17 23:03:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 17:58:01 +00:00
|
|
|
static void layer_surface_handle_set_keyboard_interactivity(
|
|
|
|
struct wl_client *client, struct wl_resource *resource,
|
|
|
|
uint32_t interactive) {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1 *surface = layer_surface_from_resource(resource);
|
2019-07-16 12:44:50 +00:00
|
|
|
|
|
|
|
if (!surface) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-27 02:08:34 +00:00
|
|
|
surface->client_pending.keyboard_interactive = !!interactive;
|
2018-03-17 23:03:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 17:58:01 +00:00
|
|
|
static void layer_surface_handle_get_popup(struct wl_client *client,
|
2018-04-14 19:47:51 +00:00
|
|
|
struct wl_resource *layer_resource,
|
|
|
|
struct wl_resource *popup_resource) {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1 *parent =
|
2018-04-14 19:47:51 +00:00
|
|
|
layer_surface_from_resource(layer_resource);
|
|
|
|
struct wlr_xdg_surface *popup_surface =
|
2018-04-14 20:50:05 +00:00
|
|
|
wlr_xdg_surface_from_popup_resource(popup_resource);
|
2018-04-14 19:47:51 +00:00
|
|
|
|
2019-07-16 12:44:50 +00:00
|
|
|
if (!parent) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-14 19:47:51 +00:00
|
|
|
assert(popup_surface->role == WLR_XDG_SURFACE_ROLE_POPUP);
|
|
|
|
struct wlr_xdg_popup *popup = popup_surface->popup;
|
|
|
|
popup->parent = parent->surface;
|
|
|
|
wl_list_insert(&parent->popups, &popup->link);
|
|
|
|
wlr_signal_emit_safe(&parent->events.new_popup, popup);
|
2018-03-17 23:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct zwlr_layer_surface_v1_interface layer_surface_implementation = {
|
|
|
|
.destroy = resource_handle_destroy,
|
|
|
|
.ack_configure = layer_surface_handle_ack_configure,
|
2018-03-20 03:11:37 +00:00
|
|
|
.set_size = layer_surface_handle_set_size,
|
2018-03-17 23:03:52 +00:00
|
|
|
.set_anchor = layer_surface_handle_set_anchor,
|
|
|
|
.set_exclusive_zone = layer_surface_handle_set_exclusive_zone,
|
|
|
|
.set_margin = layer_surface_handle_set_margin,
|
2018-03-18 17:58:01 +00:00
|
|
|
.set_keyboard_interactivity = layer_surface_handle_set_keyboard_interactivity,
|
2018-03-17 23:03:52 +00:00
|
|
|
.get_popup = layer_surface_handle_get_popup,
|
|
|
|
};
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
static void layer_surface_unmap(struct wlr_layer_surface_v1 *surface) {
|
2018-03-17 23:03:52 +00:00
|
|
|
// TODO: probably need to ungrab before this event
|
|
|
|
wlr_signal_emit_safe(&surface->events.unmap, surface);
|
|
|
|
|
2019-08-24 04:14:18 +00:00
|
|
|
struct wlr_xdg_popup *popup, *popup_tmp;
|
|
|
|
wl_list_for_each_safe(popup, popup_tmp, &surface->popups, link) {
|
|
|
|
wlr_xdg_popup_destroy(popup->base);
|
|
|
|
}
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1_configure *configure, *tmp;
|
2018-03-17 23:03:52 +00:00
|
|
|
wl_list_for_each_safe(configure, tmp, &surface->configure_list, link) {
|
|
|
|
layer_surface_configure_destroy(configure);
|
|
|
|
}
|
|
|
|
|
2018-03-27 02:08:34 +00:00
|
|
|
surface->configured = surface->mapped = false;
|
2018-03-17 23:03:52 +00:00
|
|
|
surface->configure_serial = 0;
|
|
|
|
surface->configure_next_serial = 0;
|
|
|
|
}
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
static void layer_surface_destroy(struct wlr_layer_surface_v1 *surface) {
|
2018-06-16 14:29:26 +00:00
|
|
|
if (surface->configured && surface->mapped) {
|
|
|
|
layer_surface_unmap(surface);
|
|
|
|
}
|
2018-03-17 23:03:52 +00:00
|
|
|
wlr_signal_emit_safe(&surface->events.destroy, surface);
|
|
|
|
wl_resource_set_user_data(surface->resource, NULL);
|
2018-07-07 21:45:16 +00:00
|
|
|
surface->surface->role_data = NULL;
|
|
|
|
wl_list_remove(&surface->surface_destroy.link);
|
2018-03-29 20:30:09 +00:00
|
|
|
wl_list_remove(&surface->link);
|
2018-04-24 21:40:48 +00:00
|
|
|
free(surface->namespace);
|
2018-03-17 23:03:52 +00:00
|
|
|
free(surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void layer_surface_resource_destroy(struct wl_resource *resource) {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1 *surface =
|
2018-03-17 23:03:52 +00:00
|
|
|
layer_surface_from_resource(resource);
|
|
|
|
if (surface != NULL) {
|
|
|
|
layer_surface_destroy(surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
static bool layer_surface_state_changed(struct wlr_layer_surface_v1 *surface) {
|
|
|
|
struct wlr_layer_surface_v1_state *state;
|
2018-03-18 01:06:54 +00:00
|
|
|
if (wl_list_empty(&surface->configure_list)) {
|
2018-03-28 02:50:55 +00:00
|
|
|
if (surface->acked_configure) {
|
|
|
|
state = &surface->acked_configure->state;
|
|
|
|
} else if (!surface->configured) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
state = &surface->current;
|
|
|
|
}
|
2018-03-18 01:06:54 +00:00
|
|
|
} else {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1_configure *configure =
|
2018-03-18 01:06:54 +00:00
|
|
|
wl_container_of(surface->configure_list.prev, configure, link);
|
|
|
|
state = &configure->state;
|
|
|
|
}
|
|
|
|
|
2018-03-26 22:12:31 +00:00
|
|
|
bool changed = state->actual_width != surface->server_pending.actual_width
|
|
|
|
|| state->actual_height != surface->server_pending.actual_height;
|
|
|
|
return changed;
|
2018-03-18 01:06:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
void wlr_layer_surface_v1_configure(struct wlr_layer_surface_v1 *surface,
|
2018-03-18 01:06:54 +00:00
|
|
|
uint32_t width, uint32_t height) {
|
2018-03-26 22:12:31 +00:00
|
|
|
surface->server_pending.actual_width = width;
|
|
|
|
surface->server_pending.actual_height = height;
|
2018-04-25 22:51:00 +00:00
|
|
|
if (layer_surface_state_changed(surface)) {
|
2018-03-27 02:08:34 +00:00
|
|
|
struct wl_display *display =
|
2018-03-27 21:18:16 +00:00
|
|
|
wl_client_get_display(wl_resource_get_client(surface->resource));
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1_configure *configure =
|
|
|
|
calloc(1, sizeof(struct wlr_layer_surface_v1_configure));
|
2018-03-27 02:08:34 +00:00
|
|
|
if (configure == NULL) {
|
2018-03-27 21:18:16 +00:00
|
|
|
wl_client_post_no_memory(wl_resource_get_client(surface->resource));
|
2018-03-27 02:08:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
surface->configure_next_serial = wl_display_next_serial(display);
|
|
|
|
wl_list_insert(surface->configure_list.prev, &configure->link);
|
|
|
|
configure->state.actual_width = width;
|
|
|
|
configure->state.actual_height = height;
|
|
|
|
configure->serial = surface->configure_next_serial;
|
|
|
|
zwlr_layer_surface_v1_send_configure(surface->resource,
|
|
|
|
configure->serial, configure->state.actual_width,
|
|
|
|
configure->state.actual_height);
|
|
|
|
}
|
2018-03-18 01:06:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
void wlr_layer_surface_v1_close(struct wlr_layer_surface_v1 *surface) {
|
2018-03-27 02:08:34 +00:00
|
|
|
if (surface->closed) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-24 22:00:01 +00:00
|
|
|
surface->closed = true;
|
|
|
|
layer_surface_unmap(surface);
|
|
|
|
zwlr_layer_surface_v1_send_closed(surface->resource);
|
|
|
|
}
|
|
|
|
|
2018-07-07 21:45:16 +00:00
|
|
|
static void layer_surface_role_commit(struct wlr_surface *wlr_surface) {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1 *surface =
|
|
|
|
wlr_layer_surface_v1_from_wlr_surface(wlr_surface);
|
2018-07-07 21:45:16 +00:00
|
|
|
if (surface == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-17 23:03:52 +00:00
|
|
|
|
2018-03-28 02:50:55 +00:00
|
|
|
if (surface->closed) {
|
|
|
|
// Ignore commits after the compositor has closed it
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (surface->acked_configure) {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1_configure *configure =
|
2018-03-28 02:50:55 +00:00
|
|
|
surface->acked_configure;
|
|
|
|
surface->configured = true;
|
|
|
|
surface->configure_serial = configure->serial;
|
|
|
|
surface->current.actual_width = configure->state.actual_width;
|
|
|
|
surface->current.actual_height = configure->state.actual_height;
|
|
|
|
layer_surface_configure_destroy(configure);
|
|
|
|
surface->acked_configure = NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-17 23:03:52 +00:00
|
|
|
if (wlr_surface_has_buffer(surface->surface) && !surface->configured) {
|
|
|
|
wl_resource_post_error(surface->resource,
|
|
|
|
ZWLR_LAYER_SHELL_V1_ERROR_ALREADY_CONSTRUCTED,
|
|
|
|
"layer_surface has never been configured");
|
|
|
|
return;
|
|
|
|
}
|
2018-03-18 17:58:01 +00:00
|
|
|
|
|
|
|
surface->current.anchor = surface->client_pending.anchor;
|
|
|
|
surface->current.exclusive_zone = surface->client_pending.exclusive_zone;
|
|
|
|
surface->current.margin = surface->client_pending.margin;
|
|
|
|
surface->current.keyboard_interactive =
|
|
|
|
surface->client_pending.keyboard_interactive;
|
2018-03-26 22:12:31 +00:00
|
|
|
surface->current.desired_width = surface->client_pending.desired_width;
|
|
|
|
surface->current.desired_height = surface->client_pending.desired_height;
|
2018-03-18 17:58:01 +00:00
|
|
|
|
2018-03-17 23:03:52 +00:00
|
|
|
if (!surface->added) {
|
|
|
|
surface->added = true;
|
2018-03-27 21:18:16 +00:00
|
|
|
wlr_signal_emit_safe(&surface->shell->events.new_surface,
|
2018-03-18 01:06:54 +00:00
|
|
|
surface);
|
2018-04-26 12:04:24 +00:00
|
|
|
// either the compositor found a suitable output or it must
|
|
|
|
// have closed the surface
|
|
|
|
assert(surface->output || surface->closed);
|
2018-03-17 23:03:52 +00:00
|
|
|
}
|
|
|
|
if (surface->configured && wlr_surface_has_buffer(surface->surface) &&
|
|
|
|
!surface->mapped) {
|
|
|
|
surface->mapped = true;
|
|
|
|
wlr_signal_emit_safe(&surface->events.map, surface);
|
|
|
|
}
|
|
|
|
if (surface->configured && !wlr_surface_has_buffer(surface->surface) &&
|
|
|
|
surface->mapped) {
|
|
|
|
layer_surface_unmap(surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-07 21:45:16 +00:00
|
|
|
static const struct wlr_surface_role layer_surface_role = {
|
|
|
|
.name = "zwlr_layer_surface_v1",
|
|
|
|
.commit = layer_surface_role_commit,
|
|
|
|
};
|
|
|
|
|
2018-04-24 21:40:48 +00:00
|
|
|
static void handle_surface_destroyed(struct wl_listener *listener,
|
2018-03-17 23:03:52 +00:00
|
|
|
void *data) {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1 *layer_surface =
|
2018-07-07 21:45:16 +00:00
|
|
|
wl_container_of(listener, layer_surface, surface_destroy);
|
2018-03-17 23:03:52 +00:00
|
|
|
layer_surface_destroy(layer_surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void layer_shell_handle_get_layer_surface(struct wl_client *wl_client,
|
|
|
|
struct wl_resource *client_resource, uint32_t id,
|
|
|
|
struct wl_resource *surface_resource,
|
|
|
|
struct wl_resource *output_resource,
|
|
|
|
uint32_t layer, const char *namespace) {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_shell_v1 *shell =
|
2018-03-27 21:18:16 +00:00
|
|
|
layer_shell_from_resource(client_resource);
|
2018-03-28 03:08:41 +00:00
|
|
|
struct wlr_surface *wlr_surface =
|
|
|
|
wlr_surface_from_resource(surface_resource);
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_surface_v1 *surface =
|
|
|
|
calloc(1, sizeof(struct wlr_layer_surface_v1));
|
2018-03-17 23:03:52 +00:00
|
|
|
if (surface == NULL) {
|
|
|
|
wl_client_post_no_memory(wl_client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-07 21:45:16 +00:00
|
|
|
if (!wlr_surface_set_role(wlr_surface, &layer_surface_role, surface,
|
|
|
|
client_resource, ZWLR_LAYER_SHELL_V1_ERROR_ROLE)) {
|
|
|
|
free(surface);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-27 21:18:16 +00:00
|
|
|
surface->shell = shell;
|
2018-03-28 03:08:41 +00:00
|
|
|
surface->surface = wlr_surface;
|
2018-04-22 16:57:45 +00:00
|
|
|
if (output_resource) {
|
|
|
|
surface->output = wlr_output_from_resource(output_resource);
|
|
|
|
}
|
2018-03-17 23:03:52 +00:00
|
|
|
surface->layer = layer;
|
2018-03-20 00:24:16 +00:00
|
|
|
if (layer > ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY) {
|
2018-04-24 21:40:48 +00:00
|
|
|
free(surface);
|
2018-04-26 14:58:41 +00:00
|
|
|
wl_resource_post_error(client_resource,
|
2018-03-20 00:24:16 +00:00
|
|
|
ZWLR_LAYER_SHELL_V1_ERROR_INVALID_LAYER,
|
|
|
|
"Invalid layer %d", layer);
|
2018-04-24 21:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
surface->namespace = strdup(namespace);
|
|
|
|
if (surface->namespace == NULL) {
|
2018-03-20 00:24:16 +00:00
|
|
|
free(surface);
|
2018-04-24 21:40:48 +00:00
|
|
|
wl_client_post_no_memory(wl_client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
surface->resource = wl_resource_create(wl_client,
|
|
|
|
&zwlr_layer_surface_v1_interface,
|
|
|
|
wl_resource_get_version(client_resource),
|
|
|
|
id);
|
|
|
|
if (surface->resource == NULL) {
|
|
|
|
free(surface->namespace);
|
|
|
|
free(surface);
|
|
|
|
wl_client_post_no_memory(wl_client);
|
2018-03-20 00:24:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_list_init(&surface->configure_list);
|
2018-04-14 19:47:51 +00:00
|
|
|
wl_list_init(&surface->popups);
|
2018-03-17 23:03:52 +00:00
|
|
|
|
|
|
|
wl_signal_init(&surface->events.destroy);
|
2018-03-20 00:24:16 +00:00
|
|
|
wl_signal_init(&surface->events.map);
|
|
|
|
wl_signal_init(&surface->events.unmap);
|
2018-04-14 19:47:51 +00:00
|
|
|
wl_signal_init(&surface->events.new_popup);
|
2018-03-17 23:03:52 +00:00
|
|
|
|
2018-07-07 21:45:16 +00:00
|
|
|
wl_signal_add(&surface->surface->events.destroy,
|
|
|
|
&surface->surface_destroy);
|
|
|
|
surface->surface_destroy.notify = handle_surface_destroyed;
|
2018-03-17 23:03:52 +00:00
|
|
|
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log(WLR_DEBUG, "new layer_surface %p (res %p)",
|
2018-03-17 23:03:52 +00:00
|
|
|
surface, surface->resource);
|
|
|
|
wl_resource_set_implementation(surface->resource,
|
|
|
|
&layer_surface_implementation, surface, layer_surface_resource_destroy);
|
2018-03-29 20:30:09 +00:00
|
|
|
wl_list_insert(&shell->surfaces, &surface->link);
|
2018-03-17 23:03:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 00:24:16 +00:00
|
|
|
static const struct zwlr_layer_shell_v1_interface layer_shell_implementation = {
|
2018-03-17 23:03:52 +00:00
|
|
|
.get_layer_surface = layer_shell_handle_get_layer_surface,
|
|
|
|
};
|
|
|
|
|
2018-03-27 21:18:16 +00:00
|
|
|
static void client_handle_destroy(struct wl_resource *resource) {
|
2018-03-31 04:22:09 +00:00
|
|
|
struct wl_client *client = wl_resource_get_client(resource);
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_shell_v1 *shell = layer_shell_from_resource(resource);
|
|
|
|
struct wlr_layer_surface_v1 *surface, *tmp = NULL;
|
2018-03-29 20:30:09 +00:00
|
|
|
wl_list_for_each_safe(surface, tmp, &shell->surfaces, link) {
|
2018-03-31 04:22:09 +00:00
|
|
|
if (wl_resource_get_client(surface->resource) == client) {
|
|
|
|
layer_surface_destroy(surface);
|
|
|
|
}
|
2018-03-29 20:30:09 +00:00
|
|
|
}
|
2018-03-27 21:18:16 +00:00
|
|
|
wl_list_remove(wl_resource_get_link(resource));
|
2018-03-17 19:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void layer_shell_bind(struct wl_client *wl_client, void *data,
|
|
|
|
uint32_t version, uint32_t id) {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_shell_v1 *layer_shell = data;
|
2018-03-17 19:07:48 +00:00
|
|
|
assert(wl_client && layer_shell);
|
|
|
|
|
2018-03-27 21:18:16 +00:00
|
|
|
struct wl_resource *resource = wl_resource_create(
|
2018-03-17 19:07:48 +00:00
|
|
|
wl_client, &zwlr_layer_shell_v1_interface, version, id);
|
2018-03-27 21:18:16 +00:00
|
|
|
if (resource == NULL) {
|
2018-03-17 19:07:48 +00:00
|
|
|
wl_client_post_no_memory(wl_client);
|
|
|
|
return;
|
|
|
|
}
|
2018-03-27 21:18:16 +00:00
|
|
|
wl_resource_set_implementation(resource,
|
|
|
|
&layer_shell_implementation, layer_shell, client_handle_destroy);
|
2018-09-14 17:32:33 +00:00
|
|
|
wl_list_insert(&layer_shell->resources, wl_resource_get_link(resource));
|
2018-03-17 19:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_display_destroy(struct wl_listener *listener, void *data) {
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_shell_v1 *layer_shell =
|
2018-03-17 19:07:48 +00:00
|
|
|
wl_container_of(listener, layer_shell, display_destroy);
|
2018-09-14 17:32:33 +00:00
|
|
|
wlr_layer_shell_v1_destroy(layer_shell);
|
2018-03-17 19:07:48 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_layer_shell_v1 *wlr_layer_shell_v1_create(struct wl_display *display) {
|
|
|
|
struct wlr_layer_shell_v1 *layer_shell =
|
|
|
|
calloc(1, sizeof(struct wlr_layer_shell_v1));
|
2018-03-17 19:07:48 +00:00
|
|
|
if (!layer_shell) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
wl_list_init(&layer_shell->resources);
|
2018-03-29 20:30:09 +00:00
|
|
|
wl_list_init(&layer_shell->surfaces);
|
2018-03-17 19:07:48 +00:00
|
|
|
|
2018-07-08 18:21:31 +00:00
|
|
|
struct wl_global *global = wl_global_create(display,
|
2018-03-17 19:07:48 +00:00
|
|
|
&zwlr_layer_shell_v1_interface, 1, layer_shell, layer_shell_bind);
|
2018-07-08 18:21:31 +00:00
|
|
|
if (!global) {
|
2018-03-17 19:07:48 +00:00
|
|
|
free(layer_shell);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-07-08 18:21:31 +00:00
|
|
|
layer_shell->global = global;
|
2018-03-17 19:07:48 +00:00
|
|
|
|
|
|
|
wl_signal_init(&layer_shell->events.new_surface);
|
2018-08-27 16:21:36 +00:00
|
|
|
wl_signal_init(&layer_shell->events.destroy);
|
2018-03-17 19:07:48 +00:00
|
|
|
|
|
|
|
layer_shell->display_destroy.notify = handle_display_destroy;
|
|
|
|
wl_display_add_destroy_listener(display, &layer_shell->display_destroy);
|
|
|
|
|
|
|
|
return layer_shell;
|
|
|
|
}
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
void wlr_layer_shell_v1_destroy(struct wlr_layer_shell_v1 *layer_shell) {
|
2018-03-17 19:07:48 +00:00
|
|
|
if (!layer_shell) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wl_resource *resource, *tmp;
|
|
|
|
wl_resource_for_each_safe(resource, tmp, &layer_shell->resources) {
|
|
|
|
wl_resource_destroy(resource);
|
2018-03-27 21:18:16 +00:00
|
|
|
}
|
2018-08-26 21:23:12 +00:00
|
|
|
wlr_signal_emit_safe(&layer_shell->events.destroy, layer_shell);
|
2018-03-17 19:07:48 +00:00
|
|
|
wl_list_remove(&layer_shell->display_destroy.link);
|
2018-07-08 18:21:31 +00:00
|
|
|
wl_global_destroy(layer_shell->global);
|
2018-03-17 19:07:48 +00:00
|
|
|
free(layer_shell);
|
|
|
|
}
|
2018-04-15 00:36:01 +00:00
|
|
|
|
|
|
|
struct layer_surface_iterator_data {
|
|
|
|
wlr_surface_iterator_func_t user_iterator;
|
|
|
|
void *user_data;
|
|
|
|
int x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void layer_surface_iterator(struct wlr_surface *surface,
|
|
|
|
int sx, int sy, void *data) {
|
|
|
|
struct layer_surface_iterator_data *iter_data = data;
|
|
|
|
iter_data->user_iterator(surface, iter_data->x + sx, iter_data->y + sy,
|
|
|
|
iter_data->user_data);
|
|
|
|
}
|
|
|
|
|
2018-04-15 02:21:05 +00:00
|
|
|
static void xdg_surface_for_each_surface(struct wlr_xdg_surface *surface,
|
|
|
|
int x, int y, wlr_surface_iterator_func_t iterator, void *user_data) {
|
|
|
|
struct layer_surface_iterator_data data = {
|
|
|
|
.user_iterator = iterator,
|
|
|
|
.user_data = user_data,
|
|
|
|
.x = x, .y = y,
|
|
|
|
};
|
|
|
|
wlr_surface_for_each_surface(
|
|
|
|
surface->surface, layer_surface_iterator, &data);
|
|
|
|
|
|
|
|
struct wlr_xdg_popup *popup_state;
|
|
|
|
wl_list_for_each(popup_state, &surface->popups, link) {
|
|
|
|
struct wlr_xdg_surface *popup = popup_state->base;
|
|
|
|
if (!popup->configured) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-14 21:28:45 +00:00
|
|
|
double popup_sx = popup_state->geometry.x - popup_state->base->geometry.x;
|
|
|
|
double popup_sy = popup_state->geometry.y - popup_state->base->geometry.y;
|
2018-04-15 02:21:05 +00:00
|
|
|
|
|
|
|
xdg_surface_for_each_surface(popup,
|
|
|
|
x + popup_sx,
|
|
|
|
y + popup_sy,
|
|
|
|
iterator, user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
static void layer_surface_for_each_surface(struct wlr_layer_surface_v1 *surface,
|
2018-04-15 00:36:01 +00:00
|
|
|
int x, int y, wlr_surface_iterator_func_t iterator, void *user_data) {
|
|
|
|
struct layer_surface_iterator_data data = {
|
|
|
|
.user_iterator = iterator,
|
|
|
|
.user_data = user_data,
|
|
|
|
.x = x, .y = y,
|
|
|
|
};
|
|
|
|
wlr_surface_for_each_surface(surface->surface,
|
|
|
|
layer_surface_iterator, &data);
|
|
|
|
|
|
|
|
struct wlr_xdg_popup *popup_state;
|
|
|
|
wl_list_for_each(popup_state, &surface->popups, link) {
|
|
|
|
struct wlr_xdg_surface *popup = popup_state->base;
|
|
|
|
if (!popup->configured) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
double popup_sx, popup_sy;
|
2018-04-15 02:21:05 +00:00
|
|
|
popup_sx = popup->popup->geometry.x - popup->geometry.x;
|
|
|
|
popup_sy = popup->popup->geometry.y - popup->geometry.y;
|
2018-04-15 00:36:01 +00:00
|
|
|
|
2018-04-15 02:21:05 +00:00
|
|
|
xdg_surface_for_each_surface(popup,
|
|
|
|
popup_sx, popup_sy, iterator, user_data);
|
2018-04-15 00:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
void wlr_layer_surface_v1_for_each_surface(struct wlr_layer_surface_v1 *surface,
|
2018-04-15 00:36:01 +00:00
|
|
|
wlr_surface_iterator_func_t iterator, void *user_data) {
|
|
|
|
layer_surface_for_each_surface(surface, 0, 0, iterator, user_data);
|
|
|
|
}
|
2018-05-08 13:19:31 +00:00
|
|
|
|
2018-09-14 17:32:33 +00:00
|
|
|
struct wlr_surface *wlr_layer_surface_v1_surface_at(
|
|
|
|
struct wlr_layer_surface_v1 *surface, double sx, double sy,
|
2018-05-08 13:19:31 +00:00
|
|
|
double *sub_x, double *sub_y) {
|
|
|
|
struct wlr_xdg_popup *popup_state;
|
|
|
|
wl_list_for_each(popup_state, &surface->popups, link) {
|
|
|
|
struct wlr_xdg_surface *popup = popup_state->base;
|
|
|
|
|
|
|
|
double popup_sx = popup_state->geometry.x - popup->geometry.x;
|
|
|
|
double popup_sy = popup_state->geometry.y - popup->geometry.y;
|
|
|
|
|
|
|
|
struct wlr_surface *sub = wlr_xdg_surface_surface_at(popup,
|
|
|
|
sx - popup_sx,
|
|
|
|
sy - popup_sy,
|
|
|
|
sub_x, sub_y);
|
|
|
|
if (sub != NULL) {
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return wlr_surface_surface_at(surface->surface, sx, sy, sub_x, sub_y);
|
|
|
|
}
|