2021-09-24 21:45:48 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2022-10-27 13:53:50 -04:00
|
|
|
#include <assert.h>
|
2023-01-07 17:50:33 -05:00
|
|
|
#include <wlr/xwayland.h>
|
2022-10-05 08:43:56 +02:00
|
|
|
#include "common/list.h"
|
2023-10-16 02:01:35 -04:00
|
|
|
#include "common/macros.h"
|
2022-09-16 18:41:02 -04:00
|
|
|
#include "common/mem.h"
|
2020-09-04 20:25:20 +01:00
|
|
|
#include "labwc.h"
|
2023-01-07 17:50:33 -05:00
|
|
|
#include "xwayland.h"
|
2020-09-04 20:25:20 +01:00
|
|
|
|
2024-02-13 22:25:20 -05:00
|
|
|
static void
|
|
|
|
|
handle_grab_focus(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct xwayland_unmanaged *unmanaged =
|
|
|
|
|
wl_container_of(listener, unmanaged, grab_focus);
|
|
|
|
|
|
|
|
|
|
unmanaged->ever_grabbed_focus = true;
|
|
|
|
|
if (unmanaged->node) {
|
|
|
|
|
assert(unmanaged->xwayland_surface->surface);
|
|
|
|
|
seat_focus_surface(&unmanaged->server->seat,
|
|
|
|
|
unmanaged->xwayland_surface->surface);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
2023-10-16 02:01:35 -04:00
|
|
|
handle_request_configure(struct wl_listener *listener, void *data)
|
2020-09-04 20:25:20 +01:00
|
|
|
{
|
|
|
|
|
struct xwayland_unmanaged *unmanaged =
|
|
|
|
|
wl_container_of(listener, unmanaged, request_configure);
|
|
|
|
|
struct wlr_xwayland_surface *xsurface = unmanaged->xwayland_surface;
|
|
|
|
|
struct wlr_xwayland_surface_configure_event *ev = data;
|
2022-08-10 06:14:55 +02:00
|
|
|
wlr_xwayland_surface_configure(xsurface, ev->x, ev->y, ev->width, ev->height);
|
|
|
|
|
if (unmanaged->node) {
|
|
|
|
|
wlr_scene_node_set_position(unmanaged->node, ev->x, ev->y);
|
2022-09-12 13:14:18 -04:00
|
|
|
cursor_update_focus(unmanaged->server);
|
2022-08-10 06:14:55 +02:00
|
|
|
}
|
2020-09-04 20:25:20 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-04 13:20:40 +01:00
|
|
|
static void
|
2023-10-16 02:01:35 -04:00
|
|
|
handle_set_geometry(struct wl_listener *listener, void *data)
|
2022-06-04 13:20:40 +01:00
|
|
|
{
|
|
|
|
|
struct xwayland_unmanaged *unmanaged =
|
2022-06-05 20:42:02 +00:00
|
|
|
wl_container_of(listener, unmanaged, set_geometry);
|
2022-06-04 13:20:40 +01:00
|
|
|
struct wlr_xwayland_surface *xsurface = unmanaged->xwayland_surface;
|
2022-08-10 06:14:55 +02:00
|
|
|
if (unmanaged->node) {
|
|
|
|
|
wlr_scene_node_set_position(unmanaged->node, xsurface->x, xsurface->y);
|
2022-09-12 13:14:18 -04:00
|
|
|
cursor_update_focus(unmanaged->server);
|
2022-08-10 06:14:55 +02:00
|
|
|
}
|
2022-06-04 13:20:40 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-07 17:50:33 -05:00
|
|
|
static void
|
2023-10-16 02:01:35 -04:00
|
|
|
handle_map(struct wl_listener *listener, void *data)
|
2020-09-04 20:25:20 +01:00
|
|
|
{
|
|
|
|
|
struct xwayland_unmanaged *unmanaged =
|
2023-06-15 02:35:43 -07:00
|
|
|
wl_container_of(listener, unmanaged, mappable.map);
|
2020-09-04 20:25:20 +01:00
|
|
|
struct wlr_xwayland_surface *xsurface = unmanaged->xwayland_surface;
|
2022-10-27 13:53:50 -04:00
|
|
|
assert(!unmanaged->node);
|
2020-09-04 20:25:20 +01:00
|
|
|
|
2022-09-01 14:33:42 -04:00
|
|
|
/* Stack new surface on top */
|
2022-10-05 08:43:56 +02:00
|
|
|
wl_list_append(&unmanaged->server->unmanaged_surfaces, &unmanaged->link);
|
2020-09-04 20:25:20 +01:00
|
|
|
|
2023-10-16 02:01:35 -04:00
|
|
|
CONNECT_SIGNAL(xsurface, unmanaged, set_geometry);
|
2022-06-04 13:20:40 +01:00
|
|
|
|
2024-02-13 22:25:20 -05:00
|
|
|
if (wlr_xwayland_surface_override_redirect_wants_focus(xsurface)
|
|
|
|
|
|| unmanaged->ever_grabbed_focus) {
|
2020-10-08 20:22:52 +01:00
|
|
|
seat_focus_surface(&unmanaged->server->seat, xsurface->surface);
|
2020-09-28 20:41:41 +01:00
|
|
|
}
|
2022-02-11 23:12:45 +00:00
|
|
|
|
2022-02-22 07:57:17 +01:00
|
|
|
/* node will be destroyed automatically once surface is destroyed */
|
2022-07-02 13:18:31 -04:00
|
|
|
unmanaged->node = &wlr_scene_surface_create(
|
2022-06-05 15:17:35 +02:00
|
|
|
unmanaged->server->unmanaged_tree,
|
2022-06-04 14:08:46 +01:00
|
|
|
xsurface->surface)->buffer->node;
|
2022-07-23 11:20:34 -04:00
|
|
|
wlr_scene_node_set_position(unmanaged->node, xsurface->x, xsurface->y);
|
2022-09-12 13:14:18 -04:00
|
|
|
cursor_update_focus(unmanaged->server);
|
2020-09-04 20:25:20 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-03 13:10:33 -04:00
|
|
|
static void
|
|
|
|
|
focus_next_surface(struct server *server, struct wlr_xwayland_surface *xsurface)
|
|
|
|
|
{
|
|
|
|
|
/* Try to focus on last created unmanaged xwayland surface */
|
|
|
|
|
struct xwayland_unmanaged *u;
|
|
|
|
|
struct wl_list *list = &server->unmanaged_surfaces;
|
2022-11-03 19:58:21 +00:00
|
|
|
wl_list_for_each_reverse(u, list, link) {
|
2022-09-03 13:10:33 -04:00
|
|
|
struct wlr_xwayland_surface *prev = u->xwayland_surface;
|
2024-02-13 22:25:20 -05:00
|
|
|
if (wlr_xwayland_surface_override_redirect_wants_focus(prev)
|
|
|
|
|
|| u->ever_grabbed_focus) {
|
2022-09-03 13:10:33 -04:00
|
|
|
seat_focus_surface(&server->seat, prev->surface);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If we don't find a surface to focus fall back
|
|
|
|
|
* to the topmost mapped view. This fixes dmenu
|
|
|
|
|
* not giving focus back when closed with ESC.
|
|
|
|
|
*/
|
2023-09-23 11:51:47 -04:00
|
|
|
desktop_focus_topmost_view(server);
|
2022-09-03 13:10:33 -04:00
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
2023-10-16 02:01:35 -04:00
|
|
|
handle_unmap(struct wl_listener *listener, void *data)
|
2020-09-04 20:25:20 +01:00
|
|
|
{
|
|
|
|
|
struct xwayland_unmanaged *unmanaged =
|
2023-06-15 02:35:43 -07:00
|
|
|
wl_container_of(listener, unmanaged, mappable.unmap);
|
2020-09-04 20:25:20 +01:00
|
|
|
struct wlr_xwayland_surface *xsurface = unmanaged->xwayland_surface;
|
2022-09-01 17:50:28 -04:00
|
|
|
struct seat *seat = &unmanaged->server->seat;
|
2022-10-27 13:53:50 -04:00
|
|
|
assert(unmanaged->node);
|
2022-09-01 17:50:28 -04:00
|
|
|
|
2020-09-04 20:25:20 +01:00
|
|
|
wl_list_remove(&unmanaged->link);
|
2022-06-04 13:20:40 +01:00
|
|
|
wl_list_remove(&unmanaged->set_geometry.link);
|
2022-10-27 13:53:50 -04:00
|
|
|
wlr_scene_node_set_enabled(unmanaged->node, false);
|
2020-09-04 20:25:20 +01:00
|
|
|
|
2022-08-10 06:14:55 +02:00
|
|
|
/*
|
|
|
|
|
* Mark the node as gone so a racing configure event
|
|
|
|
|
* won't try to reposition the node while unmapped.
|
|
|
|
|
*/
|
|
|
|
|
unmanaged->node = NULL;
|
2022-09-12 13:14:18 -04:00
|
|
|
cursor_update_focus(unmanaged->server);
|
2022-08-10 06:14:55 +02:00
|
|
|
|
2020-10-08 20:22:52 +01:00
|
|
|
if (seat->seat->keyboard_state.focused_surface == xsurface->surface) {
|
2022-09-03 13:10:33 -04:00
|
|
|
focus_next_surface(unmanaged->server, xsurface);
|
2020-09-04 20:25:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-15 02:35:43 -07:00
|
|
|
static void
|
|
|
|
|
handle_associate(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct xwayland_unmanaged *unmanaged =
|
|
|
|
|
wl_container_of(listener, unmanaged, associate);
|
|
|
|
|
assert(unmanaged->xwayland_surface &&
|
|
|
|
|
unmanaged->xwayland_surface->surface);
|
|
|
|
|
|
|
|
|
|
mappable_connect(&unmanaged->mappable,
|
|
|
|
|
unmanaged->xwayland_surface->surface,
|
|
|
|
|
handle_map, handle_unmap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
handle_dissociate(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct xwayland_unmanaged *unmanaged =
|
|
|
|
|
wl_container_of(listener, unmanaged, dissociate);
|
|
|
|
|
|
2024-01-23 18:22:56 -05:00
|
|
|
if (!unmanaged->mappable.connected) {
|
|
|
|
|
/*
|
|
|
|
|
* In some cases wlroots fails to emit the associate event
|
|
|
|
|
* due to an early return in xwayland_surface_associate().
|
|
|
|
|
* This is arguably a wlroots bug, but nevertheless it
|
|
|
|
|
* should not bring down labwc.
|
|
|
|
|
*
|
|
|
|
|
* TODO: Potentially remove when starting to track
|
|
|
|
|
* wlroots 0.18 and it got fixed upstream.
|
|
|
|
|
*/
|
|
|
|
|
wlr_log(WLR_ERROR, "dissociate received before associate");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-06-15 02:35:43 -07:00
|
|
|
mappable_disconnect(&unmanaged->mappable);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 20:41:41 +01:00
|
|
|
static void
|
2023-10-16 02:01:35 -04:00
|
|
|
handle_destroy(struct wl_listener *listener, void *data)
|
2020-09-04 20:25:20 +01:00
|
|
|
{
|
|
|
|
|
struct xwayland_unmanaged *unmanaged =
|
|
|
|
|
wl_container_of(listener, unmanaged, destroy);
|
2023-06-15 02:35:43 -07:00
|
|
|
|
|
|
|
|
if (unmanaged->mappable.connected) {
|
|
|
|
|
mappable_disconnect(&unmanaged->mappable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wl_list_remove(&unmanaged->associate.link);
|
|
|
|
|
wl_list_remove(&unmanaged->dissociate.link);
|
2024-02-13 22:25:20 -05:00
|
|
|
wl_list_remove(&unmanaged->grab_focus.link);
|
|
|
|
|
wl_list_remove(&unmanaged->request_activate.link);
|
2022-06-05 22:10:15 +01:00
|
|
|
wl_list_remove(&unmanaged->request_configure.link);
|
2023-10-16 02:01:35 -04:00
|
|
|
wl_list_remove(&unmanaged->set_override_redirect.link);
|
2020-09-04 20:25:20 +01:00
|
|
|
wl_list_remove(&unmanaged->destroy.link);
|
|
|
|
|
free(unmanaged);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-04 13:20:40 +01:00
|
|
|
static void
|
2023-10-16 02:01:35 -04:00
|
|
|
handle_set_override_redirect(struct wl_listener *listener, void *data)
|
2022-06-04 13:20:40 +01:00
|
|
|
{
|
2023-04-17 17:05:13 +01:00
|
|
|
wlr_log(WLR_DEBUG, "handle unmanaged override_redirect");
|
|
|
|
|
struct xwayland_unmanaged *unmanaged =
|
2023-10-16 02:01:35 -04:00
|
|
|
wl_container_of(listener, unmanaged, set_override_redirect);
|
2023-04-17 17:05:13 +01:00
|
|
|
struct wlr_xwayland_surface *xsurface = unmanaged->xwayland_surface;
|
|
|
|
|
struct server *server = unmanaged->server;
|
|
|
|
|
|
2023-06-15 02:35:43 -07:00
|
|
|
bool mapped = xsurface->surface && xsurface->surface->mapped;
|
2023-04-17 17:05:13 +01:00
|
|
|
if (mapped) {
|
2023-06-15 02:35:43 -07:00
|
|
|
handle_unmap(&unmanaged->mappable.unmap, NULL);
|
2023-04-17 17:05:13 +01:00
|
|
|
}
|
2023-10-16 02:01:35 -04:00
|
|
|
handle_destroy(&unmanaged->destroy, NULL);
|
2023-04-17 17:05:13 +01:00
|
|
|
|
2023-04-19 18:10:07 -04:00
|
|
|
xwayland_view_create(server, xsurface, mapped);
|
2022-06-04 13:20:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2023-10-16 02:01:35 -04:00
|
|
|
handle_request_activate(struct wl_listener *listener, void *data)
|
2022-06-04 13:20:40 +01:00
|
|
|
{
|
2023-04-19 10:14:51 +01:00
|
|
|
wlr_log(WLR_DEBUG, "handle unmanaged request_activate");
|
2023-09-29 04:50:47 +02:00
|
|
|
struct xwayland_unmanaged *unmanaged =
|
|
|
|
|
wl_container_of(listener, unmanaged, request_activate);
|
|
|
|
|
struct wlr_xwayland_surface *xsurface = unmanaged->xwayland_surface;
|
2023-06-15 02:35:43 -07:00
|
|
|
if (!xsurface->surface || !xsurface->surface->mapped) {
|
2023-04-19 10:14:51 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
struct server *server = unmanaged->server;
|
|
|
|
|
struct seat *seat = &server->seat;
|
2023-04-19 20:40:40 +01:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Validate that the unmanaged surface trying to grab focus is actually
|
|
|
|
|
* a child of the topmost mapped view before granting the request.
|
|
|
|
|
*/
|
2023-09-23 11:51:47 -04:00
|
|
|
struct view *view = desktop_topmost_focusable_view(server);
|
2023-04-19 20:40:40 +01:00
|
|
|
if (view && view->type == LAB_XWAYLAND_VIEW) {
|
|
|
|
|
struct wlr_xwayland_surface *surf =
|
2023-02-03 14:53:26 -05:00
|
|
|
wlr_xwayland_surface_try_from_wlr_surface(view->surface);
|
2023-04-19 20:40:40 +01:00
|
|
|
if (surf && surf->pid != xsurface->pid) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 10:14:51 +01:00
|
|
|
seat_focus_surface(seat, xsurface->surface);
|
2022-06-04 13:20:40 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-07 17:50:33 -05:00
|
|
|
void
|
2020-09-28 20:41:41 +01:00
|
|
|
xwayland_unmanaged_create(struct server *server,
|
2023-01-07 17:50:33 -05:00
|
|
|
struct wlr_xwayland_surface *xsurface, bool mapped)
|
2020-09-04 20:25:20 +01:00
|
|
|
{
|
2022-09-18 15:22:26 -04:00
|
|
|
struct xwayland_unmanaged *unmanaged = znew(*unmanaged);
|
2020-09-08 20:18:12 +01:00
|
|
|
unmanaged->server = server;
|
2020-09-04 20:25:20 +01:00
|
|
|
unmanaged->xwayland_surface = xsurface;
|
2023-09-30 01:31:37 -04:00
|
|
|
/*
|
|
|
|
|
* xsurface->data is presumed to be a (struct view *) if set,
|
|
|
|
|
* so it must be left NULL for an unmanaged surface (it should
|
|
|
|
|
* be NULL already at this point).
|
|
|
|
|
*/
|
|
|
|
|
assert(!xsurface->data);
|
2022-06-05 22:10:15 +01:00
|
|
|
|
2023-06-15 02:35:43 -07:00
|
|
|
CONNECT_SIGNAL(xsurface, unmanaged, associate);
|
|
|
|
|
CONNECT_SIGNAL(xsurface, unmanaged, dissociate);
|
2023-10-16 02:01:35 -04:00
|
|
|
CONNECT_SIGNAL(xsurface, unmanaged, destroy);
|
2024-02-13 22:25:20 -05:00
|
|
|
CONNECT_SIGNAL(xsurface, unmanaged, grab_focus);
|
2023-10-16 02:01:35 -04:00
|
|
|
CONNECT_SIGNAL(xsurface, unmanaged, request_activate);
|
|
|
|
|
CONNECT_SIGNAL(xsurface, unmanaged, request_configure);
|
|
|
|
|
CONNECT_SIGNAL(xsurface, unmanaged, set_override_redirect);
|
2022-06-04 13:20:40 +01:00
|
|
|
|
2024-01-23 18:22:56 -05:00
|
|
|
if (xsurface->surface) {
|
|
|
|
|
handle_associate(&unmanaged->associate, NULL);
|
|
|
|
|
}
|
2023-01-07 17:50:33 -05:00
|
|
|
if (mapped) {
|
2023-06-15 02:35:43 -07:00
|
|
|
handle_map(&unmanaged->mappable.map, NULL);
|
2023-01-07 17:50:33 -05:00
|
|
|
}
|
2020-09-04 20:25:20 +01:00
|
|
|
}
|