2020-10-02 06:53:08 -07:00
|
|
|
// This file is part of river, a dynamic tiling wayland compositor.
|
|
|
|
//
|
|
|
|
// Copyright 2020 - 2021 The River Developers
|
|
|
|
//
|
|
|
|
// 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-10-02 06:53:08 -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");
|
2021-07-25 13:17:40 -07:00
|
|
|
const assert = std.debug.assert;
|
2020-10-02 06:53:08 -07:00
|
|
|
const wlr = @import("wlroots");
|
|
|
|
const wayland = @import("wayland");
|
|
|
|
const wl = wayland.server.wl;
|
|
|
|
|
2021-05-13 05:26:27 -07:00
|
|
|
const server = &@import("main.zig").server;
|
2020-10-02 06:53:08 -07:00
|
|
|
const util = @import("util.zig");
|
|
|
|
|
|
|
|
const Layout = @import("Layout.zig");
|
|
|
|
const Server = @import("Server.zig");
|
|
|
|
const Output = @import("Output.zig");
|
|
|
|
const View = @import("View.zig");
|
|
|
|
|
|
|
|
const log = std.log.scoped(.layout);
|
|
|
|
|
|
|
|
const Error = error{ViewDimensionMismatch};
|
|
|
|
|
2021-06-14 12:51:55 -07:00
|
|
|
const timeout_ms = 100;
|
2020-10-02 06:53:08 -07:00
|
|
|
|
|
|
|
serial: u32,
|
|
|
|
/// Number of views for which dimensions have not been pushed.
|
|
|
|
/// This will go negative if the client pushes too many dimensions.
|
|
|
|
views: i32,
|
|
|
|
/// Proposed view dimensions
|
2022-11-11 11:25:21 -08:00
|
|
|
view_boxen: []wlr.Box,
|
2020-10-02 06:53:08 -07:00
|
|
|
timeout_timer: *wl.EventSource,
|
|
|
|
|
|
|
|
pub fn init(layout: *Layout, views: u32) !Self {
|
2021-05-13 05:26:27 -07:00
|
|
|
const event_loop = server.wl_server.getEventLoop();
|
2020-10-02 06:53:08 -07:00
|
|
|
const timeout_timer = try event_loop.addTimer(*Layout, handleTimeout, layout);
|
|
|
|
errdefer timeout_timer.remove();
|
|
|
|
try timeout_timer.timerUpdate(timeout_ms);
|
|
|
|
|
|
|
|
return Self{
|
2021-05-13 05:26:27 -07:00
|
|
|
.serial = server.wl_server.nextSerial(),
|
2023-10-16 07:18:36 -07:00
|
|
|
.views = @intCast(views),
|
2022-11-11 11:25:21 -08:00
|
|
|
.view_boxen = try util.gpa.alloc(wlr.Box, views),
|
2020-10-02 06:53:08 -07:00
|
|
|
.timeout_timer = timeout_timer,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deinit(self: *const Self) void {
|
|
|
|
self.timeout_timer.remove();
|
|
|
|
util.gpa.free(self.view_boxen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Destroy the LayoutDemand on timeout.
|
|
|
|
/// All further responses to the event will simply be ignored.
|
2022-11-04 16:29:51 -07:00
|
|
|
fn handleTimeout(layout: *Layout) c_int {
|
2021-10-11 03:44:46 -07:00
|
|
|
log.info(
|
2021-05-23 04:35:37 -07:00
|
|
|
"layout demand for layout '{s}' on output '{s}' timed out",
|
2021-12-20 19:18:03 -08:00
|
|
|
.{ layout.namespace, layout.output.wlr_output.name },
|
2020-10-02 06:53:08 -07:00
|
|
|
);
|
2023-02-24 10:28:37 -08:00
|
|
|
layout.output.inflight.layout_demand.?.deinit();
|
|
|
|
layout.output.inflight.layout_demand = null;
|
2020-10-02 06:53:08 -07:00
|
|
|
|
2021-05-13 05:35:36 -07:00
|
|
|
server.root.notifyLayoutDemandDone();
|
2020-10-02 06:53:08 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Push a set of proposed view dimensions and position to the list
|
2022-12-31 10:27:18 -08:00
|
|
|
pub fn pushViewDimensions(self: *Self, x: i32, y: i32, width: u31, height: u31) void {
|
2020-10-02 06:53:08 -07:00
|
|
|
// The client pushed too many dimensions
|
2022-07-19 02:35:34 -07:00
|
|
|
if (self.views <= 0) {
|
|
|
|
self.views -= 1;
|
|
|
|
return;
|
|
|
|
}
|
2020-10-02 06:53:08 -07:00
|
|
|
|
2023-10-16 07:18:36 -07:00
|
|
|
self.view_boxen[self.view_boxen.len - @as(usize, @intCast(self.views))] = .{
|
2022-12-31 10:27:18 -08:00
|
|
|
.x = x,
|
|
|
|
.y = y,
|
|
|
|
.width = width,
|
|
|
|
.height = height,
|
2020-10-02 06:53:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
self.views -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply the proposed layout to the output
|
|
|
|
pub fn apply(self: *Self, layout: *Layout) void {
|
2023-06-20 06:26:29 -07:00
|
|
|
// Note: output.layout may not be equal to layout here if the layout
|
|
|
|
// namespace changes while a transactions is inflight.
|
2020-10-02 06:53:08 -07:00
|
|
|
const output = layout.output;
|
|
|
|
|
|
|
|
// Whether the layout demand succeeds or fails, we are done with it and
|
|
|
|
// need to clean up
|
|
|
|
defer {
|
2023-02-24 10:28:37 -08:00
|
|
|
output.inflight.layout_demand.?.deinit();
|
|
|
|
output.inflight.layout_demand = null;
|
2021-05-13 05:35:36 -07:00
|
|
|
server.root.notifyLayoutDemandDone();
|
2020-10-02 06:53:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the number of proposed dimensions is correct.
|
|
|
|
if (self.views != 0) {
|
|
|
|
log.err(
|
|
|
|
"proposed dimension count ({}) does not match view count ({}), aborting layout demand",
|
2023-10-16 07:18:36 -07:00
|
|
|
.{ -self.views + @as(i32, @intCast(self.view_boxen.len)), self.view_boxen.len },
|
2020-10-02 06:53:08 -07:00
|
|
|
);
|
|
|
|
layout.layout.postError(
|
|
|
|
.count_mismatch,
|
|
|
|
"number of proposed view dimensions must match number of views",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-24 10:28:37 -08:00
|
|
|
// Apply proposed layout to the inflight state of the target views
|
|
|
|
var it = output.inflight.wm_stack.iterator(.forward);
|
2020-10-02 06:53:08 -07:00
|
|
|
var i: u32 = 0;
|
2023-02-24 10:28:37 -08:00
|
|
|
while (it.next()) |view| {
|
|
|
|
if (!view.inflight.float and !view.inflight.fullscreen and
|
|
|
|
view.inflight.tags & output.inflight.tags != 0)
|
|
|
|
{
|
|
|
|
const proposed = &self.view_boxen[i];
|
|
|
|
|
|
|
|
// Here we apply the offset to align the coords with the origin of the
|
|
|
|
// usable area and shrink the dimensions to accommodate the border size.
|
2023-03-12 07:40:42 -07:00
|
|
|
const border_width = if (view.inflight.ssd) server.config.border_width else 0;
|
2023-02-24 10:28:37 -08:00
|
|
|
view.inflight.box = .{
|
|
|
|
.x = proposed.x + output.usable_box.x + border_width,
|
|
|
|
.y = proposed.y + output.usable_box.y + border_width,
|
|
|
|
.width = proposed.width - 2 * border_width,
|
|
|
|
.height = proposed.height - 2 * border_width,
|
|
|
|
};
|
|
|
|
|
|
|
|
view.applyConstraints(&view.inflight.box);
|
|
|
|
|
|
|
|
// State flowing "backwards" like this is pretty ugly, but I don't
|
|
|
|
// see a better way to sync this up right now.
|
|
|
|
if (!view.pending.float and !view.pending.fullscreen) {
|
|
|
|
view.pending.box = view.inflight.box;
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
}
|
2020-10-02 06:53:08 -07:00
|
|
|
}
|
2021-07-25 13:17:40 -07:00
|
|
|
assert(i == self.view_boxen.len);
|
2020-10-02 06:53:08 -07:00
|
|
|
}
|