2021-09-24 21:45:48 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2021-03-02 20:37:23 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2020 the sway authors
|
2021-03-03 20:51:19 +00:00
|
|
|
*
|
|
|
|
|
* This file is only needed in support of
|
2021-08-25 20:45:39 +01:00
|
|
|
* - unconstraining XDG popups
|
2022-02-20 13:14:10 +00:00
|
|
|
* - keeping non-layer-shell xdg-popups outside the layers.c code
|
2021-03-02 20:37:23 +00:00
|
|
|
*/
|
|
|
|
|
|
2022-09-16 18:41:02 -04:00
|
|
|
#include "common/mem.h"
|
2021-03-02 20:37:23 +00:00
|
|
|
#include "labwc.h"
|
2022-03-02 21:07:04 +00:00
|
|
|
#include "node.h"
|
2021-03-02 20:37:23 +00:00
|
|
|
|
2022-02-20 13:14:10 +00:00
|
|
|
struct xdg_popup {
|
2022-09-13 12:51:23 -04:00
|
|
|
struct view *parent_view;
|
2022-02-20 13:14:10 +00:00
|
|
|
struct wlr_xdg_popup *wlr_popup;
|
|
|
|
|
|
|
|
|
|
struct wl_listener destroy;
|
|
|
|
|
struct wl_listener new_popup;
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-02 20:37:23 +00:00
|
|
|
static void
|
2022-02-11 23:12:45 +00:00
|
|
|
popup_unconstrain(struct view *view, struct wlr_xdg_popup *popup)
|
2021-03-02 20:37:23 +00:00
|
|
|
{
|
2021-03-02 20:51:32 +00:00
|
|
|
struct server *server = view->server;
|
2022-05-30 16:34:50 +02:00
|
|
|
struct wlr_box *popup_box = &popup->current.geometry;
|
2021-03-02 20:51:32 +00:00
|
|
|
struct wlr_output_layout *output_layout = server->output_layout;
|
|
|
|
|
struct wlr_output *wlr_output = wlr_output_layout_output_at(
|
|
|
|
|
output_layout, view->x + popup_box->x, view->y + popup_box->y);
|
2022-02-14 20:20:16 +00:00
|
|
|
|
|
|
|
|
struct wlr_box output_box;
|
|
|
|
|
wlr_output_layout_get_box(output_layout, wlr_output, &output_box);
|
2021-03-02 20:51:32 +00:00
|
|
|
|
|
|
|
|
struct wlr_box output_toplevel_box = {
|
2022-02-14 20:20:16 +00:00
|
|
|
.x = output_box.x - view->x,
|
|
|
|
|
.y = output_box.y - view->y,
|
|
|
|
|
.width = output_box.width,
|
|
|
|
|
.height = output_box.height,
|
2021-03-02 20:51:32 +00:00
|
|
|
};
|
2022-02-11 23:12:45 +00:00
|
|
|
wlr_xdg_popup_unconstrain_from_box(popup, &output_toplevel_box);
|
2021-03-02 20:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-20 13:14:10 +00:00
|
|
|
static void
|
|
|
|
|
handle_xdg_popup_destroy(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct xdg_popup *popup = wl_container_of(listener, popup, destroy);
|
|
|
|
|
wl_list_remove(&popup->destroy.link);
|
|
|
|
|
wl_list_remove(&popup->new_popup.link);
|
|
|
|
|
free(popup);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
popup_handle_new_xdg_popup(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct xdg_popup *popup = wl_container_of(listener, popup, new_popup);
|
|
|
|
|
struct wlr_xdg_popup *wlr_popup = data;
|
2022-09-13 12:51:23 -04:00
|
|
|
xdg_popup_create(popup->parent_view, wlr_popup);
|
2022-02-20 13:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-02 20:37:23 +00:00
|
|
|
void
|
|
|
|
|
xdg_popup_create(struct view *view, struct wlr_xdg_popup *wlr_popup)
|
|
|
|
|
{
|
2022-09-13 12:51:23 -04:00
|
|
|
struct wlr_xdg_surface *parent =
|
|
|
|
|
wlr_surface_is_xdg_surface(wlr_popup->parent) ?
|
|
|
|
|
wlr_xdg_surface_from_wlr_surface(wlr_popup->parent) : NULL;
|
|
|
|
|
if (!parent) {
|
|
|
|
|
wlr_log(WLR_ERROR, "parent is not a valid XDG surface");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-16 18:41:02 -04:00
|
|
|
struct xdg_popup *popup = xzalloc(sizeof(struct xdg_popup));
|
2022-02-20 13:14:10 +00:00
|
|
|
|
2022-09-13 12:51:23 -04:00
|
|
|
popup->parent_view = view;
|
2022-02-20 13:14:10 +00:00
|
|
|
popup->wlr_popup = wlr_popup;
|
|
|
|
|
|
|
|
|
|
popup->destroy.notify = handle_xdg_popup_destroy;
|
|
|
|
|
wl_signal_add(&wlr_popup->base->events.destroy, &popup->destroy);
|
|
|
|
|
popup->new_popup.notify = popup_handle_new_xdg_popup;
|
|
|
|
|
wl_signal_add(&wlr_popup->base->events.new_popup, &popup->new_popup);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We must add xdg popups to the scene graph so they get rendered. The
|
|
|
|
|
* wlroots scene graph provides a helper for this, but to use it we must
|
|
|
|
|
* provide the proper parent scene node of the xdg popup. To enable
|
|
|
|
|
* this, we always set the user data field of xdg_surfaces to the
|
|
|
|
|
* corresponding scene node.
|
|
|
|
|
*/
|
2022-06-05 15:17:35 +02:00
|
|
|
struct wlr_scene_tree *parent_tree = parent->surface->data;
|
2022-02-20 13:14:10 +00:00
|
|
|
wlr_popup->base->surface->data =
|
2022-06-05 15:17:35 +02:00
|
|
|
wlr_scene_xdg_surface_create(parent_tree, wlr_popup->base);
|
2022-02-25 22:31:24 +00:00
|
|
|
node_descriptor_create(wlr_popup->base->surface->data,
|
|
|
|
|
LAB_NODE_DESC_XDG_POPUP, view);
|
2022-02-20 13:14:10 +00:00
|
|
|
|
2022-02-11 23:12:45 +00:00
|
|
|
popup_unconstrain(view, wlr_popup);
|
2021-03-02 20:37:23 +00:00
|
|
|
}
|