2020-05-11 04:46:29 -07:00
|
|
|
// This file is part of river, a dynamic tiling wayland compositor.
|
|
|
|
//
|
2020-11-11 11:30:21 -08:00
|
|
|
// Copyright 2020 The River Developers
|
2020-05-11 04:46:29 -07:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2022-01-31 10:33:22 -08:00
|
|
|
// the Free Software Foundation, version 3.
|
2020-05-11 04:46:29 -07:00
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
const std = @import("std");
|
2022-05-29 06:16:48 -07:00
|
|
|
const assert = std.debug.assert;
|
|
|
|
|
2020-11-03 15:23:21 -08:00
|
|
|
const wlr = @import("wlroots");
|
|
|
|
const wl = @import("wayland").server.wl;
|
2020-05-11 04:46:29 -07:00
|
|
|
|
2021-05-13 06:08:53 -07:00
|
|
|
const server = &@import("main.zig").server;
|
2020-06-16 11:54:05 -07:00
|
|
|
const util = @import("util.zig");
|
2020-05-11 04:46:29 -07:00
|
|
|
|
2023-02-20 09:01:24 -08:00
|
|
|
const SceneNodeData = @import("SceneNodeData.zig");
|
2022-05-29 06:16:48 -07:00
|
|
|
const View = @import("View.zig");
|
2023-02-20 09:01:24 -08:00
|
|
|
const XwaylandView = @import("XwaylandView.zig");
|
2022-05-29 06:16:48 -07:00
|
|
|
|
|
|
|
const log = std.log.scoped(.xwayland);
|
2020-05-11 04:46:29 -07:00
|
|
|
|
2020-11-03 15:23:21 -08:00
|
|
|
xwayland_surface: *wlr.XwaylandSurface,
|
2023-02-20 09:01:24 -08:00
|
|
|
surface_tree: ?*wlr.SceneTree = null,
|
2020-05-11 04:46:29 -07:00
|
|
|
|
2020-12-31 06:35:35 -08:00
|
|
|
request_configure: wl.Listener(*wlr.XwaylandSurface.event.Configure) =
|
|
|
|
wl.Listener(*wlr.XwaylandSurface.event.Configure).init(handleRequestConfigure),
|
|
|
|
destroy: wl.Listener(*wlr.XwaylandSurface) = wl.Listener(*wlr.XwaylandSurface).init(handleDestroy),
|
|
|
|
map: wl.Listener(*wlr.XwaylandSurface) = wl.Listener(*wlr.XwaylandSurface).init(handleMap),
|
|
|
|
unmap: wl.Listener(*wlr.XwaylandSurface) = wl.Listener(*wlr.XwaylandSurface).init(handleUnmap),
|
2023-02-20 09:01:24 -08:00
|
|
|
set_geometry: wl.Listener(*wlr.XwaylandSurface) = wl.Listener(*wlr.XwaylandSurface).init(handleSetGeometry),
|
2022-05-29 06:16:48 -07:00
|
|
|
set_override_redirect: wl.Listener(*wlr.XwaylandSurface) =
|
|
|
|
wl.Listener(*wlr.XwaylandSurface).init(handleSetOverrideRedirect),
|
|
|
|
|
2023-02-20 09:01:24 -08:00
|
|
|
pub fn create(xwayland_surface: *wlr.XwaylandSurface) error{OutOfMemory}!void {
|
|
|
|
const self = try util.gpa.create(Self);
|
|
|
|
errdefer util.gpa.destroy(self);
|
2022-05-29 06:59:46 -07:00
|
|
|
|
2021-05-13 06:08:53 -07:00
|
|
|
self.* = .{ .xwayland_surface = xwayland_surface };
|
2022-06-24 03:05:54 -07:00
|
|
|
// This must be set to 0 for usage in View.fromWlrSurface()
|
|
|
|
xwayland_surface.data = 0;
|
2020-05-11 04:46:29 -07:00
|
|
|
|
2020-11-03 15:23:21 -08:00
|
|
|
xwayland_surface.events.request_configure.add(&self.request_configure);
|
|
|
|
xwayland_surface.events.destroy.add(&self.destroy);
|
|
|
|
xwayland_surface.events.map.add(&self.map);
|
|
|
|
xwayland_surface.events.unmap.add(&self.unmap);
|
2022-05-29 08:01:49 -07:00
|
|
|
xwayland_surface.events.set_override_redirect.add(&self.set_override_redirect);
|
2022-05-29 06:59:46 -07:00
|
|
|
|
2023-02-20 09:01:24 -08:00
|
|
|
if (xwayland_surface.mapped) {
|
|
|
|
handleMap(&self.map, xwayland_surface);
|
|
|
|
}
|
2020-05-11 04:46:29 -07:00
|
|
|
}
|
|
|
|
|
2020-11-03 15:23:21 -08:00
|
|
|
fn handleRequestConfigure(
|
2021-10-11 03:44:46 -07:00
|
|
|
_: *wl.Listener(*wlr.XwaylandSurface.event.Configure),
|
2020-11-03 15:23:21 -08:00
|
|
|
event: *wlr.XwaylandSurface.event.Configure,
|
|
|
|
) void {
|
2021-01-07 12:48:55 -08:00
|
|
|
event.surface.configure(event.x, event.y, event.width, event.height);
|
2020-05-11 04:46:29 -07:00
|
|
|
}
|
|
|
|
|
2021-10-11 03:44:46 -07:00
|
|
|
fn handleDestroy(listener: *wl.Listener(*wlr.XwaylandSurface), _: *wlr.XwaylandSurface) void {
|
2020-11-03 15:23:21 -08:00
|
|
|
const self = @fieldParentPtr(Self, "destroy", listener);
|
2020-05-11 04:46:29 -07:00
|
|
|
|
2021-01-07 12:48:55 -08:00
|
|
|
self.request_configure.link.remove();
|
2020-11-03 15:23:21 -08:00
|
|
|
self.destroy.link.remove();
|
|
|
|
self.map.link.remove();
|
|
|
|
self.unmap.link.remove();
|
2022-05-29 08:01:49 -07:00
|
|
|
self.set_override_redirect.link.remove();
|
2020-05-11 04:46:29 -07:00
|
|
|
|
2023-02-20 09:01:24 -08:00
|
|
|
util.gpa.destroy(self);
|
2020-05-11 04:46:29 -07:00
|
|
|
}
|
|
|
|
|
2023-01-27 10:51:56 -08:00
|
|
|
pub fn handleMap(listener: *wl.Listener(*wlr.XwaylandSurface), _: *wlr.XwaylandSurface) void {
|
2020-11-03 15:23:21 -08:00
|
|
|
const self = @fieldParentPtr(Self, "map", listener);
|
2020-05-11 04:46:29 -07:00
|
|
|
|
2023-02-20 09:01:24 -08:00
|
|
|
self.mapImpl() catch {
|
|
|
|
log.err("out of memory", .{});
|
|
|
|
self.xwayland_surface.surface.?.resource.getClient().postNoMemory();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mapImpl(self: *Self) error{OutOfMemory}!void {
|
|
|
|
self.surface_tree = try server.root.layers.xwayland_override_redirect.createSceneSubsurfaceTree(
|
|
|
|
self.xwayland_surface.surface.?,
|
|
|
|
);
|
|
|
|
try SceneNodeData.attach(&self.surface_tree.?.node, .{ .xwayland_override_redirect = self });
|
|
|
|
|
|
|
|
self.surface_tree.?.node.setPosition(self.xwayland_surface.x, self.xwayland_surface.y);
|
|
|
|
|
|
|
|
self.xwayland_surface.events.set_geometry.add(&self.set_geometry);
|
2020-05-11 04:46:29 -07:00
|
|
|
|
2023-01-12 02:57:56 -08:00
|
|
|
self.focusIfDesired();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn focusIfDesired(self: *Self) void {
|
2023-02-10 04:12:11 -08:00
|
|
|
if (server.lock_manager.state != .unlocked) return;
|
|
|
|
|
2022-06-21 15:56:21 -07:00
|
|
|
if (self.xwayland_surface.overrideRedirectWantsFocus() and
|
|
|
|
self.xwayland_surface.icccmInputModel() != .none)
|
|
|
|
{
|
2023-01-12 02:57:56 -08:00
|
|
|
const seat = server.input_manager.defaultSeat();
|
|
|
|
// Keep the parent top-level Xwayland view of any override redirect surface
|
|
|
|
// activated while that override redirect surface is focused. This ensures
|
|
|
|
// override redirect menus do not disappear as a result of deactivating
|
|
|
|
// their parent window.
|
|
|
|
if (seat.focused == .view and
|
|
|
|
seat.focused.view.impl == .xwayland_view and
|
|
|
|
seat.focused.view.impl.xwayland_view.xwayland_surface.pid == self.xwayland_surface.pid)
|
|
|
|
{
|
|
|
|
seat.keyboardEnterOrLeave(self.xwayland_surface.surface);
|
|
|
|
} else {
|
|
|
|
seat.setFocusRaw(.{ .xwayland_override_redirect = self });
|
|
|
|
}
|
2022-05-28 05:19:43 -07:00
|
|
|
}
|
2020-05-11 04:46:29 -07:00
|
|
|
}
|
|
|
|
|
2021-10-11 03:44:46 -07:00
|
|
|
fn handleUnmap(listener: *wl.Listener(*wlr.XwaylandSurface), _: *wlr.XwaylandSurface) void {
|
2020-11-03 15:23:21 -08:00
|
|
|
const self = @fieldParentPtr(Self, "unmap", listener);
|
2020-05-11 04:46:29 -07:00
|
|
|
|
2023-02-20 09:01:24 -08:00
|
|
|
self.set_geometry.link.remove();
|
|
|
|
|
|
|
|
self.surface_tree.?.node.destroy();
|
|
|
|
self.surface_tree = null;
|
2021-05-23 08:10:26 -07:00
|
|
|
|
2023-01-12 02:57:56 -08:00
|
|
|
// If the unmapped surface is currently focused, pass keyboard focus
|
|
|
|
// to the most appropriate surface.
|
2022-05-28 05:19:43 -07:00
|
|
|
var seat_it = server.input_manager.seats.first;
|
|
|
|
while (seat_it) |seat_node| : (seat_it = seat_node.next) {
|
|
|
|
const seat = &seat_node.data;
|
2023-03-01 01:49:44 -08:00
|
|
|
if (seat.focused == .view and seat.focused.view.impl == .xwayland_view and
|
|
|
|
seat.focused.view.impl.xwayland_view.xwayland_surface.pid == self.xwayland_surface.pid and
|
|
|
|
seat.wlr_seat.keyboard_state.focused_surface == self.xwayland_surface.surface)
|
|
|
|
{
|
|
|
|
seat.keyboardEnterOrLeave(seat.focused.view.rootSurface());
|
2022-05-28 05:19:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 10:28:37 -08:00
|
|
|
server.root.applyPending();
|
2021-05-23 08:10:26 -07:00
|
|
|
}
|
|
|
|
|
2023-02-20 09:01:24 -08:00
|
|
|
fn handleSetGeometry(listener: *wl.Listener(*wlr.XwaylandSurface), _: *wlr.XwaylandSurface) void {
|
|
|
|
const self = @fieldParentPtr(Self, "set_geometry", listener);
|
|
|
|
|
|
|
|
self.surface_tree.?.node.setPosition(self.xwayland_surface.x, self.xwayland_surface.y);
|
|
|
|
}
|
|
|
|
|
2022-05-29 06:16:48 -07:00
|
|
|
fn handleSetOverrideRedirect(
|
|
|
|
listener: *wl.Listener(*wlr.XwaylandSurface),
|
|
|
|
xwayland_surface: *wlr.XwaylandSurface,
|
|
|
|
) void {
|
|
|
|
const self = @fieldParentPtr(Self, "set_override_redirect", listener);
|
|
|
|
|
2022-05-29 16:08:09 -07:00
|
|
|
log.debug("xwayland surface unset override redirect", .{});
|
2022-05-29 06:16:48 -07:00
|
|
|
|
|
|
|
assert(!xwayland_surface.override_redirect);
|
|
|
|
|
|
|
|
if (xwayland_surface.mapped) handleUnmap(&self.unmap, xwayland_surface);
|
|
|
|
handleDestroy(&self.destroy, xwayland_surface);
|
|
|
|
|
2023-02-24 10:28:37 -08:00
|
|
|
XwaylandView.create(xwayland_surface) catch {
|
2022-05-29 06:16:48 -07:00
|
|
|
log.err("out of memory", .{});
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
}
|