2020-05-08 05:51:10 -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-08 05:51:10 -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
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// 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");
|
2020-12-18 09:59:13 -08:00
|
|
|
const math = std.math;
|
2020-11-03 15:23:21 -08:00
|
|
|
const wlr = @import("wlroots");
|
|
|
|
const wl = @import("wayland").server.wl;
|
2020-05-08 05:51:10 -07:00
|
|
|
|
|
|
|
const Box = @import("Box.zig");
|
|
|
|
const View = @import("View.zig");
|
|
|
|
const ViewStack = @import("view_stack.zig").ViewStack;
|
|
|
|
const XdgPopup = @import("XdgPopup.zig");
|
|
|
|
|
|
|
|
/// The view this xwayland view implements
|
|
|
|
view: *View,
|
|
|
|
|
|
|
|
/// The corresponding wlroots object
|
2020-11-03 15:23:21 -08:00
|
|
|
xwayland_surface: *wlr.XwaylandSurface,
|
2020-05-08 05:51:10 -07:00
|
|
|
|
|
|
|
// Listeners that are always active over the view's lifetime
|
2020-12-31 06:35:35 -08:00
|
|
|
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),
|
2021-01-31 09:24:21 -08:00
|
|
|
// zig fmt: off
|
|
|
|
request_configure: wl.Listener(*wlr.XwaylandSurface.event.Configure) =
|
|
|
|
wl.Listener(*wlr.XwaylandSurface.event.Configure).init(handleRequestConfigure),
|
|
|
|
// zig fmt: on
|
2020-05-08 05:51:10 -07:00
|
|
|
|
|
|
|
// Listeners that are only active while the view is mapped
|
2020-12-31 06:35:35 -08:00
|
|
|
commit: wl.Listener(*wlr.Surface) = wl.Listener(*wlr.Surface).init(handleCommit),
|
2021-01-31 09:24:21 -08:00
|
|
|
set_title: wl.Listener(*wlr.XwaylandSurface) = wl.Listener(*wlr.XwaylandSurface).init(handleSetTitle),
|
|
|
|
set_class: wl.Listener(*wlr.XwaylandSurface) = wl.Listener(*wlr.XwaylandSurface).init(handleSetClass),
|
2020-05-08 05:51:10 -07:00
|
|
|
|
2020-11-03 15:23:21 -08:00
|
|
|
pub fn init(self: *Self, view: *View, xwayland_surface: *wlr.XwaylandSurface) void {
|
|
|
|
self.* = .{ .view = view, .xwayland_surface = xwayland_surface };
|
|
|
|
xwayland_surface.data = @ptrToInt(self);
|
2020-05-08 05:51:10 -07:00
|
|
|
|
|
|
|
// Add listeners that are active over the view's entire lifetime
|
2020-12-31 06:35:35 -08:00
|
|
|
xwayland_surface.events.destroy.add(&self.destroy);
|
|
|
|
xwayland_surface.events.map.add(&self.map);
|
|
|
|
xwayland_surface.events.unmap.add(&self.unmap);
|
2021-01-31 09:24:21 -08:00
|
|
|
xwayland_surface.events.request_configure.add(&self.request_configure);
|
2020-05-08 05:51:10 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 05:35:19 -07:00
|
|
|
pub fn deinit(self: *Self) void {
|
2020-11-03 15:23:21 -08:00
|
|
|
if (self.view.surface != null) {
|
2020-08-20 05:35:19 -07:00
|
|
|
// Remove listeners that are active for the entire lifetime of the view
|
2020-11-03 15:23:21 -08:00
|
|
|
self.destroy.link.remove();
|
|
|
|
self.map.link.remove();
|
|
|
|
self.unmap.link.remove();
|
2021-01-31 09:24:21 -08:00
|
|
|
self.request_configure.link.remove();
|
2020-08-20 05:35:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:13:28 -07:00
|
|
|
pub fn needsConfigure(self: Self) bool {
|
2020-12-29 10:18:00 -08:00
|
|
|
const output = self.view.output;
|
|
|
|
const output_box = output.root.output_layout.getBox(output.wlr_output).?;
|
|
|
|
return self.xwayland_surface.x != self.view.pending.box.x + output_box.x or
|
|
|
|
self.xwayland_surface.y != self.view.pending.box.y + output_box.y or
|
2020-11-03 15:23:21 -08:00
|
|
|
self.xwayland_surface.width != self.view.pending.box.width or
|
|
|
|
self.xwayland_surface.height != self.view.pending.box.height;
|
2020-06-09 10:13:28 -07:00
|
|
|
}
|
|
|
|
|
2020-12-29 11:38:30 -08:00
|
|
|
/// Apply pending state. Note: we don't set View.serial as
|
|
|
|
/// shouldTrackConfigure() is always false for xwayland views.
|
2020-08-03 06:00:04 -07:00
|
|
|
pub fn configure(self: Self) void {
|
2020-12-29 10:18:00 -08:00
|
|
|
const output = self.view.output;
|
|
|
|
const output_box = output.root.output_layout.getBox(output.wlr_output).?;
|
|
|
|
|
2020-08-03 06:00:04 -07:00
|
|
|
const state = &self.view.pending;
|
2020-11-03 15:23:21 -08:00
|
|
|
self.xwayland_surface.setFullscreen(state.fullscreen);
|
|
|
|
self.xwayland_surface.configure(
|
2020-12-29 10:18:00 -08:00
|
|
|
@intCast(i16, state.box.x + output_box.x),
|
|
|
|
@intCast(i16, state.box.y + output_box.y),
|
2020-08-03 06:00:04 -07:00
|
|
|
@intCast(u16, state.box.width),
|
|
|
|
@intCast(u16, state.box.height),
|
2020-05-08 05:51:10 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Close the view. This will lead to the unmap and destroy events being sent
|
|
|
|
pub fn close(self: Self) void {
|
2020-11-03 15:23:21 -08:00
|
|
|
self.xwayland_surface.close();
|
2020-05-08 05:51:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the surface at output coordinates ox, oy and set sx, sy to the
|
|
|
|
/// corresponding surface-relative coordinates, if there is a surface.
|
2020-11-03 15:23:21 -08:00
|
|
|
pub fn surfaceAt(self: Self, ox: f64, oy: f64, sx: *f64, sy: *f64) ?*wlr.Surface {
|
|
|
|
return self.xwayland_surface.surface.?.surfaceAt(
|
2020-06-27 13:43:15 -07:00
|
|
|
ox - @intToFloat(f64, self.view.current.box.x),
|
|
|
|
oy - @intToFloat(f64, self.view.current.box.y),
|
2020-05-08 05:51:10 -07:00
|
|
|
sx,
|
|
|
|
sy,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:59:30 -08:00
|
|
|
/// Get the current title of the xwayland surface if any.
|
|
|
|
pub fn getTitle(self: Self) ?[*:0]const u8 {
|
|
|
|
return self.xwayland_surface.title;
|
|
|
|
}
|
2021-01-01 04:29:57 -08:00
|
|
|
/// X11 clients don't have an app_id but the class serves a similar role.
|
2020-12-23 16:59:30 -08:00
|
|
|
/// Get the current class of the xwayland surface if any.
|
2021-01-01 04:29:57 -08:00
|
|
|
pub fn getAppId(self: Self) ?[*:0]const u8 {
|
2020-12-23 16:59:30 -08:00
|
|
|
return self.xwayland_surface.class;
|
2020-06-04 07:56:58 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 07:36:46 -07:00
|
|
|
/// Return bounds on the dimensions of the view
|
|
|
|
pub fn getConstraints(self: Self) View.Constraints {
|
2020-11-03 15:23:21 -08:00
|
|
|
const hints = self.xwayland_surface.size_hints orelse return .{
|
2020-07-29 07:36:46 -07:00
|
|
|
.min_width = View.min_size,
|
|
|
|
.min_height = View.min_size,
|
2020-12-18 09:59:13 -08:00
|
|
|
.max_width = math.maxInt(u32),
|
|
|
|
.max_height = math.maxInt(u32),
|
2020-07-29 07:36:46 -07:00
|
|
|
};
|
|
|
|
return .{
|
2020-12-18 09:59:13 -08:00
|
|
|
.min_width = @intCast(u32, math.max(hints.min_width, View.min_size)),
|
|
|
|
.min_height = @intCast(u32, math.max(hints.min_height, View.min_size)),
|
|
|
|
|
|
|
|
.max_width = if (hints.max_width > 0)
|
|
|
|
math.max(@intCast(u32, hints.max_width), View.min_size)
|
|
|
|
else
|
|
|
|
math.maxInt(u32),
|
|
|
|
|
|
|
|
.max_height = if (hints.max_height > 0)
|
|
|
|
math.max(@intCast(u32, hints.max_height), View.min_size)
|
|
|
|
else
|
|
|
|
math.maxInt(u32),
|
2020-07-29 07:36:46 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-08 05:51:10 -07:00
|
|
|
/// Called when the xwayland surface is destroyed
|
2020-11-03 15:23:21 -08:00
|
|
|
fn handleDestroy(listener: *wl.Listener(*wlr.XwaylandSurface), xwayland_surface: *wlr.XwaylandSurface) void {
|
|
|
|
const self = @fieldParentPtr(Self, "destroy", listener);
|
2020-08-20 05:35:19 -07:00
|
|
|
self.deinit();
|
2020-11-03 15:23:21 -08:00
|
|
|
self.view.surface = null;
|
2020-05-08 05:51:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when the xwayland surface is mapped, or ready to display on-screen.
|
2020-11-03 15:23:21 -08:00
|
|
|
fn handleMap(listener: *wl.Listener(*wlr.XwaylandSurface), xwayland_surface: *wlr.XwaylandSurface) void {
|
|
|
|
const self = @fieldParentPtr(Self, "map", listener);
|
2020-05-08 05:51:10 -07:00
|
|
|
const view = self.view;
|
|
|
|
const root = view.output.root;
|
|
|
|
|
|
|
|
// Add listeners that are only active while mapped
|
2020-12-31 06:35:35 -08:00
|
|
|
xwayland_surface.surface.?.events.commit.add(&self.commit);
|
2021-01-01 04:29:57 -08:00
|
|
|
xwayland_surface.events.set_title.add(&self.set_title);
|
|
|
|
xwayland_surface.events.set_class.add(&self.set_class);
|
2020-05-08 05:51:10 -07:00
|
|
|
|
2020-11-03 15:23:21 -08:00
|
|
|
view.surface = self.xwayland_surface.surface;
|
2020-05-08 05:51:10 -07:00
|
|
|
|
2020-06-29 02:15:55 -07:00
|
|
|
// Use the view's "natural" size centered on the output as the default
|
|
|
|
// floating dimensions
|
2020-11-03 15:23:21 -08:00
|
|
|
view.float_box.width = self.xwayland_surface.width;
|
|
|
|
view.float_box.height = self.xwayland_surface.height;
|
2020-12-18 09:59:13 -08:00
|
|
|
view.float_box.x = math.max(0, @divTrunc(@intCast(i32, view.output.usable_box.width) -
|
2020-06-29 02:15:55 -07:00
|
|
|
@intCast(i32, view.float_box.width), 2));
|
2020-12-18 09:59:13 -08:00
|
|
|
view.float_box.y = math.max(0, @divTrunc(@intCast(i32, view.output.usable_box.height) -
|
2020-06-29 02:15:55 -07:00
|
|
|
@intCast(i32, view.float_box.height), 2));
|
2020-05-08 05:51:10 -07:00
|
|
|
|
2020-11-03 15:23:21 -08:00
|
|
|
const has_fixed_size = if (self.xwayland_surface.size_hints) |size_hints|
|
|
|
|
size_hints.min_width != 0 and size_hints.min_height != 0 and
|
|
|
|
(size_hints.min_width == size_hints.max_width or size_hints.min_height == size_hints.max_height)
|
|
|
|
else
|
|
|
|
false;
|
|
|
|
|
|
|
|
const app_id: [*:0]const u8 = if (self.xwayland_surface.class) |id| id else "NULL";
|
2020-10-22 10:22:54 -07:00
|
|
|
|
2020-11-03 15:23:21 -08:00
|
|
|
if (self.xwayland_surface.parent != null or has_fixed_size) {
|
2020-10-22 10:22:54 -07:00
|
|
|
// If the toplevel has a parent or has a fixed size make it float
|
|
|
|
view.current.float = true;
|
|
|
|
view.pending.float = true;
|
|
|
|
view.pending.box = view.float_box;
|
|
|
|
} else {
|
|
|
|
// Make views with app_ids listed in the float filter float
|
|
|
|
for (root.server.config.float_filter.items) |filter_app_id| {
|
|
|
|
if (std.mem.eql(u8, std.mem.span(app_id), std.mem.span(filter_app_id))) {
|
|
|
|
view.current.float = true;
|
|
|
|
view.pending.float = true;
|
|
|
|
view.pending.box = view.float_box;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 05:51:10 -07:00
|
|
|
view.map();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when the surface is unmapped and will no longer be displayed.
|
2020-11-03 15:23:21 -08:00
|
|
|
fn handleUnmap(listener: *wl.Listener(*wlr.XwaylandSurface), xwayland_surface: *wlr.XwaylandSurface) void {
|
|
|
|
const self = @fieldParentPtr(Self, "unmap", listener);
|
2020-05-08 05:51:10 -07:00
|
|
|
|
|
|
|
self.view.unmap();
|
|
|
|
|
|
|
|
// Remove listeners that are only active while mapped
|
2020-11-03 15:23:21 -08:00
|
|
|
self.commit.link.remove();
|
2021-01-01 04:29:57 -08:00
|
|
|
self.set_title.link.remove();
|
|
|
|
self.set_class.link.remove();
|
2020-05-08 05:51:10 -07:00
|
|
|
}
|
|
|
|
|
2021-01-31 09:24:21 -08:00
|
|
|
fn handleRequestConfigure(
|
|
|
|
listener: *wl.Listener(*wlr.XwaylandSurface.event.Configure),
|
|
|
|
event: *wlr.XwaylandSurface.event.Configure,
|
|
|
|
) void {
|
|
|
|
const self = @fieldParentPtr(Self, "request_configure", listener);
|
|
|
|
|
|
|
|
// Allow xwayland views to set their own dimensions (but not position)
|
|
|
|
// if floating or unmapped
|
|
|
|
if (self.view.surface == null or self.view.pending.float) {
|
|
|
|
self.view.pending.box.width = event.width;
|
|
|
|
self.view.pending.box.height = event.height;
|
|
|
|
self.configure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 05:51:10 -07:00
|
|
|
/// Called when the surface is comitted
|
2020-05-08 08:06:15 -07:00
|
|
|
/// TODO: check for unexpected change in size and react as needed
|
2020-11-03 15:23:21 -08:00
|
|
|
fn handleCommit(listener: *wl.Listener(*wlr.Surface), surface: *wlr.Surface) void {
|
|
|
|
const self = @fieldParentPtr(Self, "commit", listener);
|
2020-12-29 11:38:30 -08:00
|
|
|
self.view.surface_box = Box{
|
2020-06-13 05:42:31 -07:00
|
|
|
.x = 0,
|
|
|
|
.y = 0,
|
2020-11-03 15:23:21 -08:00
|
|
|
.width = @intCast(u32, surface.current.width),
|
|
|
|
.height = @intCast(u32, surface.current.height),
|
2020-06-13 05:42:31 -07:00
|
|
|
};
|
2020-05-08 05:51:10 -07:00
|
|
|
}
|
2020-10-25 06:54:31 -07:00
|
|
|
|
|
|
|
/// Called then the window updates its title
|
2021-01-01 04:29:57 -08:00
|
|
|
fn handleSetTitle(listener: *wl.Listener(*wlr.XwaylandSurface), xwayland_surface: *wlr.XwaylandSurface) void {
|
|
|
|
const self = @fieldParentPtr(Self, "set_title", listener);
|
|
|
|
self.view.notifyTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called then the window updates its class
|
|
|
|
fn handleSetClass(listener: *wl.Listener(*wlr.XwaylandSurface), xwayland_surface: *wlr.XwaylandSurface) void {
|
|
|
|
const self = @fieldParentPtr(Self, "set_class", listener);
|
|
|
|
self.view.notifyAppId();
|
2020-10-25 06:54:31 -07:00
|
|
|
}
|