2020-09-28 20:53:59 +01:00
|
|
|
#include <assert.h>
|
2019-12-26 21:37:31 +00:00
|
|
|
#include "labwc.h"
|
|
|
|
|
|
2020-05-13 20:51:13 +01:00
|
|
|
struct xdg_deco {
|
|
|
|
|
struct wlr_xdg_toplevel_decoration_v1 *wlr_decoration;
|
|
|
|
|
struct server *server;
|
|
|
|
|
struct wl_listener destroy;
|
|
|
|
|
struct wl_listener request_mode;
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
xdg_deco_destroy(struct wl_listener *listener, void *data)
|
2020-05-13 20:51:13 +01:00
|
|
|
{
|
|
|
|
|
struct xdg_deco *xdg_deco =
|
|
|
|
|
wl_container_of(listener, xdg_deco, destroy);
|
|
|
|
|
wl_list_remove(&xdg_deco->destroy.link);
|
|
|
|
|
wl_list_remove(&xdg_deco->request_mode.link);
|
|
|
|
|
free(xdg_deco);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
xdg_deco_request_mode(struct wl_listener *listener, void *data)
|
2020-05-13 20:51:13 +01:00
|
|
|
{
|
|
|
|
|
struct xdg_deco *xdg_deco;
|
|
|
|
|
xdg_deco = wl_container_of(listener, xdg_deco, request_mode);
|
|
|
|
|
enum wlr_xdg_toplevel_decoration_v1_mode mode;
|
2020-09-28 20:41:41 +01:00
|
|
|
if (rc.xdg_shell_server_side_deco) {
|
2020-05-13 20:51:13 +01:00
|
|
|
mode = WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE;
|
2020-09-28 20:41:41 +01:00
|
|
|
} else {
|
2020-05-13 20:51:13 +01:00
|
|
|
mode = WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-05-13 20:51:13 +01:00
|
|
|
wlr_xdg_toplevel_decoration_v1_set_mode(xdg_deco->wlr_decoration, mode);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
void
|
|
|
|
|
xdg_toplevel_decoration(struct wl_listener *listener, void *data)
|
2020-05-13 20:51:13 +01:00
|
|
|
{
|
|
|
|
|
struct server *server =
|
|
|
|
|
wl_container_of(listener, server, xdg_toplevel_decoration);
|
|
|
|
|
struct wlr_xdg_toplevel_decoration_v1 *wlr_decoration = data;
|
|
|
|
|
struct xdg_deco *xdg_deco = calloc(1, sizeof(struct xdg_deco));
|
2020-09-28 20:41:41 +01:00
|
|
|
if (!xdg_deco) {
|
2020-05-13 20:51:13 +01:00
|
|
|
return;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-05-13 20:51:13 +01:00
|
|
|
xdg_deco->wlr_decoration = wlr_decoration;
|
|
|
|
|
xdg_deco->server = server;
|
|
|
|
|
xdg_deco->destroy.notify = xdg_deco_destroy;
|
|
|
|
|
wl_signal_add(&wlr_decoration->events.destroy, &xdg_deco->destroy);
|
|
|
|
|
xdg_deco->request_mode.notify = xdg_deco_request_mode;
|
|
|
|
|
wl_signal_add(&wlr_decoration->events.request_mode,
|
|
|
|
|
&xdg_deco->request_mode);
|
|
|
|
|
xdg_deco_request_mode(&xdg_deco->request_mode, wlr_decoration);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static bool
|
|
|
|
|
has_ssd(struct view *view)
|
2020-08-06 14:51:45 +01:00
|
|
|
{
|
2020-09-28 20:41:41 +01:00
|
|
|
if (!rc.xdg_shell_server_side_deco) {
|
2020-08-06 14:51:45 +01:00
|
|
|
return false;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-08-06 14:51:45 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Some XDG shells refuse to disable CSD in which case their
|
|
|
|
|
* geometry.{x,y} seems to be greater. We filter on that on the
|
|
|
|
|
* assumption that this will remain true.
|
|
|
|
|
*/
|
2020-09-28 20:41:41 +01:00
|
|
|
if (view->xdg_surface->geometry.x || view->xdg_surface->geometry.y) {
|
2020-08-06 14:51:45 +01:00
|
|
|
return false;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-08-06 14:51:45 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
handle_commit(struct wl_listener *listener, void *data)
|
2020-08-31 08:12:44 +01:00
|
|
|
{
|
|
|
|
|
struct view *view = wl_container_of(listener, view, commit);
|
2020-09-28 20:53:59 +01:00
|
|
|
assert(view->surface);
|
2020-12-22 21:08:17 +00:00
|
|
|
struct wlr_box size;
|
|
|
|
|
wlr_xdg_surface_get_geometry(view->xdg_surface, &size);
|
|
|
|
|
|
|
|
|
|
view->w = size.width;
|
|
|
|
|
view->h = size.height;
|
|
|
|
|
|
|
|
|
|
uint32_t serial = view->pending_move_resize.configure_serial;
|
|
|
|
|
if (serial > 0 && serial >= view->xdg_surface->configure_serial) {
|
|
|
|
|
if (view->pending_move_resize.update_x) {
|
|
|
|
|
view->x = view->pending_move_resize.x +
|
|
|
|
|
view->pending_move_resize.width - size.width;
|
|
|
|
|
}
|
|
|
|
|
if (view->pending_move_resize.update_y) {
|
|
|
|
|
view->y = view->pending_move_resize.y +
|
|
|
|
|
view->pending_move_resize.height - size.height;
|
|
|
|
|
}
|
|
|
|
|
if (serial == view->xdg_surface->configure_serial) {
|
|
|
|
|
view->pending_move_resize.configure_serial = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-31 08:12:44 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
handle_map(struct wl_listener *listener, void *data)
|
2019-12-26 21:37:31 +00:00
|
|
|
{
|
2019-12-27 20:48:58 +00:00
|
|
|
struct view *view = wl_container_of(listener, view, map);
|
2020-09-03 20:50:35 +01:00
|
|
|
view->impl->map(view);
|
2019-12-26 21:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
handle_unmap(struct wl_listener *listener, void *data)
|
2019-12-26 21:37:31 +00:00
|
|
|
{
|
2019-12-27 20:48:58 +00:00
|
|
|
struct view *view = wl_container_of(listener, view, unmap);
|
2020-09-03 20:50:35 +01:00
|
|
|
view->impl->unmap(view);
|
2019-12-26 21:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
handle_destroy(struct wl_listener *listener, void *data)
|
2019-12-26 21:37:31 +00:00
|
|
|
{
|
2019-12-27 20:48:58 +00:00
|
|
|
struct view *view = wl_container_of(listener, view, destroy);
|
2019-12-26 21:37:31 +00:00
|
|
|
wl_list_remove(&view->link);
|
|
|
|
|
free(view);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
handle_request_move(struct wl_listener *listener, void *data)
|
2019-12-26 21:37:31 +00:00
|
|
|
{
|
|
|
|
|
/* This event is raised when a client would like to begin an interactive
|
|
|
|
|
* move, typically because the user clicked on their client-side
|
2019-12-27 21:22:45 +00:00
|
|
|
* decorations. Note that a more sophisticated compositor should check
|
|
|
|
|
* the provied serial against a list of button press serials sent to
|
|
|
|
|
* this
|
|
|
|
|
* client, to prevent the client from requesting this whenever they
|
|
|
|
|
* want. */
|
2019-12-27 20:48:58 +00:00
|
|
|
struct view *view = wl_container_of(listener, view, request_move);
|
2020-10-21 20:30:06 +01:00
|
|
|
interactive_begin(view, LAB_INPUT_STATE_MOVE, 0);
|
2019-12-26 21:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
handle_request_resize(struct wl_listener *listener, void *data)
|
2019-12-26 21:37:31 +00:00
|
|
|
{
|
|
|
|
|
/* This event is raised when a client would like to begin an interactive
|
|
|
|
|
* resize, typically because the user clicked on their client-side
|
2019-12-27 21:22:45 +00:00
|
|
|
* decorations. Note that a more sophisticated compositor should check
|
|
|
|
|
* the provied serial against a list of button press serials sent to
|
|
|
|
|
* this
|
|
|
|
|
* client, to prevent the client from requesting this whenever they
|
|
|
|
|
* want. */
|
2019-12-26 21:37:31 +00:00
|
|
|
struct wlr_xdg_toplevel_resize_event *event = data;
|
2019-12-27 20:48:58 +00:00
|
|
|
struct view *view = wl_container_of(listener, view, request_resize);
|
2020-10-21 20:30:06 +01:00
|
|
|
interactive_begin(view, LAB_INPUT_STATE_RESIZE, event->edges);
|
2019-12-26 21:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
xdg_toplevel_view_configure(struct view *view, struct wlr_box geo)
|
2020-09-02 20:20:52 +01:00
|
|
|
{
|
2020-12-22 21:08:17 +00:00
|
|
|
view->pending_move_resize.update_x = geo.x != view->x;
|
|
|
|
|
view->pending_move_resize.update_y = geo.y != view->y;
|
|
|
|
|
view->pending_move_resize.x = geo.x;
|
|
|
|
|
view->pending_move_resize.y = geo.y;
|
|
|
|
|
view->pending_move_resize.width = geo.width;
|
|
|
|
|
view->pending_move_resize.height = geo.height;
|
|
|
|
|
|
|
|
|
|
uint32_t serial = wlr_xdg_toplevel_set_size(view->xdg_surface,
|
|
|
|
|
(uint32_t)geo.width, (uint32_t)geo.height);
|
|
|
|
|
if (serial > 0) {
|
|
|
|
|
view->pending_move_resize.configure_serial = serial;
|
|
|
|
|
} else if (view->pending_move_resize.configure_serial == 0) {
|
|
|
|
|
view->x = geo.x;
|
|
|
|
|
view->y = geo.y;
|
|
|
|
|
}
|
2020-09-02 20:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-23 18:52:46 +00:00
|
|
|
static void
|
|
|
|
|
xdg_toplevel_view_move(struct view *view, double x, double y)
|
|
|
|
|
{
|
|
|
|
|
view->x = x;
|
|
|
|
|
view->y = y;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
xdg_toplevel_view_close(struct view *view)
|
2020-09-02 21:00:28 +01:00
|
|
|
{
|
|
|
|
|
wlr_xdg_toplevel_send_close(view->xdg_surface);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 20:48:50 +01:00
|
|
|
static void
|
|
|
|
|
xdg_toplevel_view_for_each_surface(struct view *view,
|
|
|
|
|
wlr_surface_iterator_func_t iterator, void *data)
|
|
|
|
|
{
|
|
|
|
|
wlr_xdg_surface_for_each_surface(view->xdg_surface, iterator, data);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static struct border
|
|
|
|
|
xdg_shell_border(struct view *view)
|
2020-09-15 20:41:01 +01:00
|
|
|
{
|
|
|
|
|
struct wlr_box box;
|
|
|
|
|
wlr_xdg_surface_get_geometry(view->xdg_surface, &box);
|
|
|
|
|
struct border border = {
|
|
|
|
|
.top = -box.y,
|
|
|
|
|
.bottom = -box.y,
|
|
|
|
|
.left = -box.x,
|
|
|
|
|
.right = -box.x,
|
|
|
|
|
};
|
|
|
|
|
return border;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static bool
|
|
|
|
|
istopmost(struct view *view)
|
2020-09-25 20:22:18 +01:00
|
|
|
{
|
|
|
|
|
return view->xdg_surface->toplevel->parent == NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
xdg_toplevel_view_map(struct view *view)
|
2020-09-03 20:50:35 +01:00
|
|
|
{
|
|
|
|
|
view->mapped = true;
|
|
|
|
|
view->surface = view->xdg_surface->surface;
|
|
|
|
|
if (!view->been_mapped) {
|
2020-09-15 21:10:02 +01:00
|
|
|
view->server_side_deco = has_ssd(view);
|
|
|
|
|
if (view->server_side_deco) {
|
2020-09-17 21:11:54 +01:00
|
|
|
view->margin = deco_thickness(view);
|
2020-09-15 20:41:01 +01:00
|
|
|
} else {
|
|
|
|
|
view->margin = xdg_shell_border(view);
|
|
|
|
|
view->xdg_grab_offset = -view->margin.left;
|
|
|
|
|
}
|
2020-09-25 20:22:18 +01:00
|
|
|
if (istopmost(view)) {
|
|
|
|
|
/* align to edge of screen */
|
|
|
|
|
view->x += view->margin.left;
|
|
|
|
|
view->y += view->margin.top;
|
|
|
|
|
}
|
2020-09-03 20:50:35 +01:00
|
|
|
}
|
|
|
|
|
view->been_mapped = true;
|
2020-09-15 20:41:01 +01:00
|
|
|
|
2020-09-03 20:50:35 +01:00
|
|
|
wl_signal_add(&view->xdg_surface->surface->events.commit,
|
|
|
|
|
&view->commit);
|
|
|
|
|
view->commit.notify = handle_commit;
|
2020-09-15 20:41:01 +01:00
|
|
|
|
2020-10-08 20:08:41 +01:00
|
|
|
desktop_focus_view(&view->server->seat, view);
|
2020-09-03 20:50:35 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
|
|
|
|
xdg_toplevel_view_unmap(struct view *view)
|
2020-09-03 20:50:35 +01:00
|
|
|
{
|
|
|
|
|
view->mapped = false;
|
|
|
|
|
wl_list_remove(&view->commit.link);
|
2020-10-31 14:32:31 +00:00
|
|
|
desktop_focus_topmost_mapped_view(view->server);
|
2020-09-03 20:50:35 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-02 20:20:52 +01:00
|
|
|
static const struct view_impl xdg_toplevel_view_impl = {
|
|
|
|
|
.configure = xdg_toplevel_view_configure,
|
2020-09-03 20:50:35 +01:00
|
|
|
.close = xdg_toplevel_view_close,
|
2020-09-29 20:48:50 +01:00
|
|
|
.for_each_surface = xdg_toplevel_view_for_each_surface,
|
2020-09-03 20:50:35 +01:00
|
|
|
.map = xdg_toplevel_view_map,
|
2020-12-23 18:52:46 +00:00
|
|
|
.move = xdg_toplevel_view_move,
|
2020-09-03 20:50:35 +01:00
|
|
|
.unmap = xdg_toplevel_view_unmap,
|
2020-09-02 20:20:52 +01:00
|
|
|
};
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
void
|
|
|
|
|
xdg_surface_new(struct wl_listener *listener, void *data)
|
2019-12-26 21:37:31 +00:00
|
|
|
{
|
2019-12-27 20:48:58 +00:00
|
|
|
struct server *server =
|
2019-12-26 21:37:31 +00:00
|
|
|
wl_container_of(listener, server, new_xdg_surface);
|
|
|
|
|
struct wlr_xdg_surface *xdg_surface = data;
|
2020-09-28 20:41:41 +01:00
|
|
|
if (xdg_surface->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
|
2019-12-26 21:37:31 +00:00
|
|
|
return;
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2020-10-08 19:58:47 +01:00
|
|
|
wlr_xdg_surface_ping(xdg_surface);
|
2019-12-26 21:37:31 +00:00
|
|
|
|
2019-12-27 21:22:45 +00:00
|
|
|
struct view *view = calloc(1, sizeof(struct view));
|
2019-12-26 21:37:31 +00:00
|
|
|
view->server = server;
|
|
|
|
|
view->type = LAB_XDG_SHELL_VIEW;
|
2020-09-02 20:20:52 +01:00
|
|
|
view->impl = &xdg_toplevel_view_impl;
|
2019-12-26 21:37:31 +00:00
|
|
|
view->xdg_surface = xdg_surface;
|
|
|
|
|
|
2020-09-03 21:40:27 +01:00
|
|
|
view->map.notify = handle_map;
|
2019-12-26 21:37:31 +00:00
|
|
|
wl_signal_add(&xdg_surface->events.map, &view->map);
|
2020-09-03 21:40:27 +01:00
|
|
|
view->unmap.notify = handle_unmap;
|
2019-12-26 21:37:31 +00:00
|
|
|
wl_signal_add(&xdg_surface->events.unmap, &view->unmap);
|
2020-09-03 21:40:27 +01:00
|
|
|
view->destroy.notify = handle_destroy;
|
2019-12-26 21:37:31 +00:00
|
|
|
wl_signal_add(&xdg_surface->events.destroy, &view->destroy);
|
|
|
|
|
|
|
|
|
|
struct wlr_xdg_toplevel *toplevel = xdg_surface->toplevel;
|
2020-09-03 21:40:27 +01:00
|
|
|
view->request_move.notify = handle_request_move;
|
2019-12-26 21:37:31 +00:00
|
|
|
wl_signal_add(&toplevel->events.request_move, &view->request_move);
|
2020-09-03 21:40:27 +01:00
|
|
|
view->request_resize.notify = handle_request_resize;
|
2019-12-26 21:37:31 +00:00
|
|
|
wl_signal_add(&toplevel->events.request_resize, &view->request_resize);
|
|
|
|
|
|
|
|
|
|
wl_list_insert(&server->views, &view->link);
|
|
|
|
|
}
|