2020-05-02 10:21:10 -07:00
|
|
|
// This file is part of river, a dynamic tiling wayland compositor.
|
|
|
|
//
|
|
|
|
// Copyright 2020 Isaac Freund
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
2020-04-25 12:40:26 -07:00
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
const std = @import("std");
|
|
|
|
|
2020-05-02 10:21:10 -07:00
|
|
|
const c = @import("c.zig");
|
2020-06-16 16:25:11 -07:00
|
|
|
const log = @import("log.zig");
|
2020-06-16 11:54:05 -07:00
|
|
|
const util = @import("util.zig");
|
2020-05-02 10:21:10 -07:00
|
|
|
|
2020-05-02 14:11:56 -07:00
|
|
|
const Box = @import("Box.zig");
|
|
|
|
const View = @import("View.zig");
|
2020-04-25 12:40:26 -07:00
|
|
|
const ViewStack = @import("view_stack.zig").ViewStack;
|
2020-05-02 14:11:56 -07:00
|
|
|
const XdgPopup = @import("XdgPopup.zig");
|
2020-04-25 12:40:26 -07:00
|
|
|
|
|
|
|
/// The view this xdg toplevel implements
|
|
|
|
view: *View,
|
|
|
|
|
|
|
|
/// The corresponding wlroots object
|
|
|
|
wlr_xdg_surface: *c.wlr_xdg_surface,
|
|
|
|
|
|
|
|
// Listeners that are always active over the view's lifetime
|
2020-08-21 07:45:27 -07:00
|
|
|
listen_destroy: c.wl_listener = undefined,
|
|
|
|
listen_map: c.wl_listener = undefined,
|
|
|
|
listen_unmap: c.wl_listener = undefined,
|
2020-04-25 12:40:26 -07:00
|
|
|
|
|
|
|
// Listeners that are only active while the view is mapped
|
2020-08-21 07:45:27 -07:00
|
|
|
listen_commit: c.wl_listener = undefined,
|
|
|
|
listen_new_popup: c.wl_listener = undefined,
|
|
|
|
listen_request_fullscreen: c.wl_listener = undefined,
|
2020-04-25 12:40:26 -07:00
|
|
|
|
|
|
|
pub fn init(self: *Self, view: *View, wlr_xdg_surface: *c.wlr_xdg_surface) void {
|
2020-08-21 07:45:27 -07:00
|
|
|
self.* = .{ .view = view, .wlr_xdg_surface = wlr_xdg_surface };
|
2020-04-25 12:40:26 -07:00
|
|
|
wlr_xdg_surface.data = self;
|
|
|
|
|
|
|
|
// Add listeners that are active over the view's entire lifetime
|
|
|
|
self.listen_destroy.notify = handleDestroy;
|
|
|
|
c.wl_signal_add(&self.wlr_xdg_surface.events.destroy, &self.listen_destroy);
|
|
|
|
|
|
|
|
self.listen_map.notify = handleMap;
|
|
|
|
c.wl_signal_add(&self.wlr_xdg_surface.events.map, &self.listen_map);
|
|
|
|
|
|
|
|
self.listen_unmap.notify = handleUnmap;
|
|
|
|
c.wl_signal_add(&self.wlr_xdg_surface.events.unmap, &self.listen_unmap);
|
|
|
|
}
|
|
|
|
|
2020-08-20 05:35:19 -07:00
|
|
|
pub fn deinit(self: *Self) void {
|
|
|
|
if (self.view.wlr_surface != null) {
|
|
|
|
// Remove listeners that are active for the entire lifetime of the view
|
|
|
|
c.wl_list_remove(&self.listen_destroy.link);
|
|
|
|
c.wl_list_remove(&self.listen_map.link);
|
|
|
|
c.wl_list_remove(&self.listen_unmap.link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:13:28 -07:00
|
|
|
/// Returns true if a configure must be sent to ensure the dimensions of the
|
|
|
|
/// pending_box are applied.
|
|
|
|
pub fn needsConfigure(self: Self) bool {
|
2020-08-03 06:00:04 -07:00
|
|
|
const server_pending = &@field(
|
2020-06-09 10:13:28 -07:00
|
|
|
self.wlr_xdg_surface,
|
|
|
|
c.wlr_xdg_surface_union,
|
2020-08-03 06:00:04 -07:00
|
|
|
).toplevel.*.server_pending;
|
|
|
|
const state = &self.view.pending;
|
2020-06-09 10:13:28 -07:00
|
|
|
|
|
|
|
// Checking server_pending is sufficient here since it will be either in
|
|
|
|
// sync with the current dimensions or be the dimensions sent with the
|
|
|
|
// most recent configure. In both cases server_pending has the values we
|
|
|
|
// want to check against.
|
2020-08-03 06:00:04 -07:00
|
|
|
return (state.focus != 0) != server_pending.activated or
|
|
|
|
state.box.width != server_pending.width or
|
|
|
|
state.box.height != server_pending.height;
|
2020-06-09 10:13:28 -07:00
|
|
|
}
|
|
|
|
|
2020-08-03 06:00:04 -07:00
|
|
|
/// Send a configure event, applying the pending state of the view.
|
|
|
|
pub fn configure(self: Self) void {
|
|
|
|
const state = &self.view.pending;
|
|
|
|
_ = c.wlr_xdg_toplevel_set_activated(self.wlr_xdg_surface, state.focus != 0);
|
2020-08-11 13:32:32 -07:00
|
|
|
_ = c.wlr_xdg_toplevel_set_fullscreen(self.wlr_xdg_surface, state.fullscreen);
|
2020-04-25 12:40:26 -07:00
|
|
|
self.view.pending_serial = c.wlr_xdg_toplevel_set_size(
|
|
|
|
self.wlr_xdg_surface,
|
2020-08-03 06:00:04 -07:00
|
|
|
state.box.width,
|
|
|
|
state.box.height,
|
2020-04-25 12:40:26 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Close the view. This will lead to the unmap and destroy events being sent
|
|
|
|
pub fn close(self: Self) void {
|
|
|
|
c.wlr_xdg_toplevel_send_close(self.wlr_xdg_surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn forEachSurface(
|
|
|
|
self: Self,
|
|
|
|
iterator: c.wlr_surface_iterator_func_t,
|
|
|
|
user_data: ?*c_void,
|
|
|
|
) void {
|
|
|
|
c.wlr_xdg_surface_for_each_surface(self.wlr_xdg_surface, iterator, user_data);
|
|
|
|
}
|
2020-04-27 07:25:49 -07:00
|
|
|
|
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.
|
|
|
|
pub fn surfaceAt(self: Self, ox: f64, oy: f64, sx: *f64, sy: *f64) ?*c.wlr_surface {
|
2020-06-13 05:42:31 -07:00
|
|
|
const view = self.view;
|
2020-05-08 05:51:10 -07:00
|
|
|
return c.wlr_xdg_surface_surface_at(
|
|
|
|
self.wlr_xdg_surface,
|
2020-06-27 13:43:15 -07:00
|
|
|
ox - @intToFloat(f64, view.current.box.x - view.surface_box.x),
|
|
|
|
oy - @intToFloat(f64, view.current.box.y - view.surface_box.y),
|
2020-05-08 05:51:10 -07:00
|
|
|
sx,
|
|
|
|
sy,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-04 07:56:58 -07:00
|
|
|
/// Return the current title of the toplevel. May be an empty string.
|
|
|
|
pub fn getTitle(self: Self) [*:0]const u8 {
|
|
|
|
const wlr_xdg_toplevel: *c.wlr_xdg_toplevel = @field(
|
|
|
|
self.wlr_xdg_surface,
|
|
|
|
c.wlr_xdg_surface_union,
|
|
|
|
).toplevel;
|
2020-06-09 10:13:28 -07:00
|
|
|
return wlr_xdg_toplevel.title orelse "NULL";
|
2020-06-04 07:56:58 -07:00
|
|
|
}
|
|
|
|
|
2020-07-29 07:36:46 -07:00
|
|
|
/// Return bounds on the dimensions of the toplevel.
|
|
|
|
pub fn getConstraints(self: Self) View.Constraints {
|
|
|
|
const state = @field(self.wlr_xdg_surface, c.wlr_xdg_surface_union).toplevel.*.current;
|
|
|
|
return .{
|
2020-08-10 12:26:48 -07:00
|
|
|
.min_width = std.math.max(state.min_width, View.min_size),
|
2020-07-29 07:36:46 -07:00
|
|
|
.max_width = if (state.max_width > 0) state.max_width else std.math.maxInt(u32),
|
2020-08-10 12:26:48 -07:00
|
|
|
.min_height = std.math.max(state.min_height, View.min_size),
|
2020-07-29 07:36:46 -07:00
|
|
|
.max_height = if (state.max_height > 0) state.max_height else std.math.maxInt(u32),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-25 12:40:26 -07:00
|
|
|
/// Called when the xdg surface is destroyed
|
|
|
|
fn handleDestroy(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
|
|
|
const self = @fieldParentPtr(Self, "listen_destroy", listener.?);
|
2020-08-20 05:35:19 -07:00
|
|
|
self.deinit();
|
|
|
|
self.view.wlr_surface = null;
|
2020-04-25 12:40:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when the xdg surface is mapped, or ready to display on-screen.
|
|
|
|
fn handleMap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
|
|
|
const self = @fieldParentPtr(Self, "listen_map", listener.?);
|
|
|
|
const view = self.view;
|
|
|
|
const root = view.output.root;
|
2020-06-29 02:52:53 -07:00
|
|
|
const wlr_xdg_toplevel: *c.wlr_xdg_toplevel = @field(self.wlr_xdg_surface, c.wlr_xdg_surface_union).toplevel;
|
2020-04-25 12:40:26 -07:00
|
|
|
|
|
|
|
// Add listeners that are only active while mapped
|
|
|
|
self.listen_commit.notify = handleCommit;
|
|
|
|
c.wl_signal_add(&self.wlr_xdg_surface.surface.*.events.commit, &self.listen_commit);
|
|
|
|
|
2020-04-27 06:19:16 -07:00
|
|
|
self.listen_new_popup.notify = handleNewPopup;
|
|
|
|
c.wl_signal_add(&self.wlr_xdg_surface.events.new_popup, &self.listen_new_popup);
|
|
|
|
|
2020-06-29 02:52:53 -07:00
|
|
|
self.listen_request_fullscreen.notify = handleRequestFullscreen;
|
|
|
|
c.wl_signal_add(&wlr_xdg_toplevel.events.request_fullscreen, &self.listen_request_fullscreen);
|
|
|
|
|
2020-04-25 12:40:26 -07:00
|
|
|
view.wlr_surface = self.wlr_xdg_surface.surface;
|
|
|
|
|
2020-06-29 02:15:55 -07:00
|
|
|
// Use the view's "natural" size centered on the output as the default
|
|
|
|
// floating dimensions
|
|
|
|
view.float_box.width = @intCast(u32, self.wlr_xdg_surface.geometry.width);
|
|
|
|
view.float_box.height = @intCast(u32, self.wlr_xdg_surface.geometry.height);
|
|
|
|
view.float_box.x = std.math.max(0, @divTrunc(@intCast(i32, view.output.usable_box.width) -
|
|
|
|
@intCast(i32, view.float_box.width), 2));
|
|
|
|
view.float_box.y = std.math.max(0, @divTrunc(@intCast(i32, view.output.usable_box.height) -
|
|
|
|
@intCast(i32, view.float_box.height), 2));
|
2020-04-25 12:40:26 -07:00
|
|
|
|
2020-04-27 07:25:49 -07:00
|
|
|
const state = &wlr_xdg_toplevel.current;
|
2020-06-27 14:48:04 -07:00
|
|
|
const has_fixed_size = state.min_width != 0 and state.min_height != 0 and
|
|
|
|
(state.min_width == state.max_width or state.min_height == state.max_height);
|
2020-04-27 07:25:49 -07:00
|
|
|
const app_id: [*:0]const u8 = if (wlr_xdg_toplevel.app_id) |id| id else "NULL";
|
|
|
|
|
2020-06-27 14:48:04 -07:00
|
|
|
if (wlr_xdg_toplevel.parent != null or has_fixed_size) {
|
|
|
|
// If the toplevel has a parent or has a fixed size make it float
|
2020-06-28 11:30:12 -07:00
|
|
|
view.pending.float = true;
|
2020-06-29 02:15:55 -07:00
|
|
|
view.pending.box = view.float_box;
|
2020-06-27 14:48:04 -07:00
|
|
|
} else {
|
2020-04-27 07:25:49 -07:00
|
|
|
// Make views with app_ids listed in the float filter float
|
2020-06-27 14:48:04 -07:00
|
|
|
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))) {
|
2020-08-11 10:04:37 -07:00
|
|
|
view.current.float = true;
|
2020-06-28 11:30:12 -07:00
|
|
|
view.pending.float = true;
|
2020-06-29 02:15:55 -07:00
|
|
|
view.pending.box = view.float_box;
|
2020-06-27 14:48:04 -07:00
|
|
|
break;
|
|
|
|
}
|
2020-04-25 12:40:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 10:48:13 -07:00
|
|
|
// If the toplevel has an app_id which is not configured to use client side
|
|
|
|
// decorations, inform it that it is tiled.
|
|
|
|
for (root.server.config.csd_filter.items) |filter_app_id| {
|
|
|
|
if (std.mem.eql(u8, std.mem.span(app_id), filter_app_id)) {
|
2020-07-15 16:21:22 -07:00
|
|
|
view.draw_borders = false;
|
2020-07-16 10:48:13 -07:00
|
|
|
break;
|
2020-07-15 16:21:22 -07:00
|
|
|
}
|
2020-07-16 10:48:13 -07:00
|
|
|
} else {
|
|
|
|
_ = c.wlr_xdg_toplevel_set_tiled(
|
|
|
|
self.wlr_xdg_surface,
|
|
|
|
c.WLR_EDGE_LEFT | c.WLR_EDGE_RIGHT | c.WLR_EDGE_TOP | c.WLR_EDGE_BOTTOM,
|
|
|
|
);
|
2020-07-15 16:21:22 -07:00
|
|
|
}
|
2020-06-13 05:42:31 -07:00
|
|
|
|
2020-05-08 05:51:10 -07:00
|
|
|
view.map();
|
2020-04-25 12:40:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when the surface is unmapped and will no longer be displayed.
|
|
|
|
fn handleUnmap(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
|
|
|
const self = @fieldParentPtr(Self, "listen_unmap", listener.?);
|
|
|
|
const root = self.view.output.root;
|
|
|
|
|
2020-05-08 05:51:10 -07:00
|
|
|
self.view.unmap();
|
2020-04-25 12:40:26 -07:00
|
|
|
|
|
|
|
// Remove listeners that are only active while mapped
|
|
|
|
c.wl_list_remove(&self.listen_commit.link);
|
2020-04-27 06:19:16 -07:00
|
|
|
c.wl_list_remove(&self.listen_new_popup.link);
|
2020-06-29 02:52:53 -07:00
|
|
|
c.wl_list_remove(&self.listen_request_fullscreen.link);
|
2020-04-25 12:40:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when the surface is comitted
|
|
|
|
fn handleCommit(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
|
|
|
const self = @fieldParentPtr(Self, "listen_commit", listener.?);
|
|
|
|
const view = self.view;
|
|
|
|
|
2020-06-13 05:42:31 -07:00
|
|
|
var wlr_box: c.wlr_box = undefined;
|
|
|
|
c.wlr_xdg_surface_get_geometry(self.wlr_xdg_surface, &wlr_box);
|
|
|
|
const new_box = Box.fromWlrBox(wlr_box);
|
|
|
|
|
|
|
|
// If we have sent a configure changing the size
|
2020-04-25 12:40:26 -07:00
|
|
|
if (view.pending_serial) |s| {
|
2020-06-13 05:42:31 -07:00
|
|
|
// Update the stored dimensions of the surface
|
|
|
|
view.surface_box = new_box;
|
|
|
|
|
2020-04-25 12:40:26 -07:00
|
|
|
if (s == self.wlr_xdg_surface.configure_serial) {
|
2020-08-11 10:04:37 -07:00
|
|
|
// If this commit is in response to our configure and the
|
|
|
|
// transaction code is tracking this configure, notify it.
|
|
|
|
// Otherwise, apply the pending state immediately.
|
2020-04-25 12:40:26 -07:00
|
|
|
view.pending_serial = null;
|
2020-08-11 10:04:37 -07:00
|
|
|
if (view.shouldTrackConfigure())
|
2020-07-31 03:16:11 -07:00
|
|
|
view.output.root.notifyConfigured()
|
2020-10-03 13:09:15 -07:00
|
|
|
else {
|
2020-07-31 03:16:11 -07:00
|
|
|
view.current = view.pending;
|
2020-10-03 13:09:15 -07:00
|
|
|
view.commitOpacityTransition();
|
|
|
|
}
|
2020-06-09 10:13:28 -07:00
|
|
|
} else {
|
2020-06-13 05:42:31 -07:00
|
|
|
// If the client has not yet acked our configure, we need to send a
|
|
|
|
// frame done event so that it commits another buffer. These
|
2020-06-09 10:13:28 -07:00
|
|
|
// buffers won't be rendered since we are still rendering our
|
|
|
|
// stashed buffer from when the transaction started.
|
|
|
|
view.sendFrameDone();
|
2020-04-25 12:40:26 -07:00
|
|
|
}
|
2020-06-13 05:42:31 -07:00
|
|
|
} else {
|
|
|
|
// TODO: handle unexpected change in dimensions
|
|
|
|
if (!std.meta.eql(view.surface_box, new_box))
|
2020-06-16 16:25:11 -07:00
|
|
|
log.err(.xdg_shell, "view changed size unexpectedly", .{});
|
2020-06-13 05:42:31 -07:00
|
|
|
view.surface_box = new_box;
|
2020-04-25 12:40:26 -07:00
|
|
|
}
|
|
|
|
}
|
2020-04-27 06:19:16 -07:00
|
|
|
|
|
|
|
/// Called when a new xdg popup is requested by the client
|
|
|
|
fn handleNewPopup(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
|
|
|
const self = @fieldParentPtr(Self, "listen_new_popup", listener.?);
|
2020-06-16 11:54:05 -07:00
|
|
|
const wlr_xdg_popup = util.voidCast(c.wlr_xdg_popup, data.?);
|
2020-04-27 06:19:16 -07:00
|
|
|
|
|
|
|
// This will free itself on destroy
|
2020-07-05 13:49:17 -07:00
|
|
|
var xdg_popup = util.gpa.create(XdgPopup) catch {
|
|
|
|
c.wl_resource_post_no_memory(wlr_xdg_popup.resource);
|
|
|
|
return;
|
|
|
|
};
|
2020-06-27 13:43:15 -07:00
|
|
|
xdg_popup.init(self.view.output, &self.view.current.box, wlr_xdg_popup);
|
2020-04-27 06:19:16 -07:00
|
|
|
}
|
2020-06-29 02:52:53 -07:00
|
|
|
|
|
|
|
/// Called when the client asks to be fullscreened. We always honor the request
|
|
|
|
/// for now, perhaps it should be denied in some cases in the future.
|
|
|
|
fn handleRequestFullscreen(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
|
|
|
|
const self = @fieldParentPtr(Self, "listen_request_fullscreen", listener.?);
|
|
|
|
const event = util.voidCast(c.wlr_xdg_toplevel_set_fullscreen_event, data.?);
|
2020-08-11 10:04:37 -07:00
|
|
|
self.view.pending.fullscreen = event.fullscreen;
|
|
|
|
self.view.applyPending();
|
2020-06-29 02:52:53 -07:00
|
|
|
}
|