2020-05-02 07:43:16 -07:00
|
|
|
const Self = @This();
|
|
|
|
|
2020-03-22 14:42:55 -07:00
|
|
|
const std = @import("std");
|
2020-05-02 07:43:16 -07:00
|
|
|
|
2020-03-29 10:36:15 -07:00
|
|
|
const c = @import("c.zig");
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-04-26 07:49:50 -07:00
|
|
|
const Box = @import("box.zig");
|
2020-04-22 14:35:16 -07:00
|
|
|
const Log = @import("log.zig").Log;
|
2020-05-02 07:40:05 -07:00
|
|
|
const Output = @import("output.zig");
|
2020-03-25 07:59:24 -07:00
|
|
|
const Root = @import("root.zig").Root;
|
2020-04-03 09:53:36 -07:00
|
|
|
const ViewStack = @import("view_stack.zig").ViewStack;
|
2020-04-25 12:40:26 -07:00
|
|
|
const XdgToplevel = @import("xdg_toplevel.zig");
|
2020-03-23 08:50:20 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
const ViewImpl = union(enum) {
|
|
|
|
xdg_toplevel: XdgToplevel,
|
|
|
|
};
|
2020-03-24 12:48:38 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
/// The implementation of this view
|
|
|
|
impl: ViewImpl,
|
2020-04-25 12:40:26 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
/// The output this view is currently associated with
|
|
|
|
output: *Output,
|
2020-04-25 12:40:26 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
/// This is non-null exactly when the view is mapped
|
|
|
|
wlr_surface: ?*c.wlr_surface,
|
2020-03-23 06:04:54 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
/// If the view is floating or not
|
|
|
|
floating: bool,
|
2020-03-26 13:32:30 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
/// True if the view is currentlt focused by at lease one seat
|
|
|
|
focused: bool,
|
2020-04-22 13:50:03 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
/// The current output-relative coordinates and dimensions of the view
|
|
|
|
current_box: Box,
|
|
|
|
pending_box: ?Box,
|
2020-04-23 08:27:18 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
/// The dimensions the view would have taken if we didn't force it to tile
|
|
|
|
natural_width: u32,
|
|
|
|
natural_height: u32,
|
2020-04-02 04:44:24 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
current_tags: u32,
|
|
|
|
pending_tags: ?u32,
|
2020-04-22 13:50:03 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
pending_serial: ?u32,
|
2020-03-26 13:32:30 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
// This is what we render while a transaction is in progress
|
|
|
|
stashed_buffer: ?*c.wlr_buffer,
|
2020-03-26 13:32:30 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
pub fn init_xdg_toplevel(
|
|
|
|
self: *Self,
|
|
|
|
output: *Output,
|
|
|
|
tags: u32,
|
|
|
|
wlr_xdg_surface: *c.wlr_xdg_surface,
|
|
|
|
) void {
|
|
|
|
self.output = output;
|
2020-03-23 13:51:46 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
self.wlr_surface = null;
|
2020-03-23 13:51:46 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
self.focused = false;
|
2020-04-01 08:50:49 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
self.current_box = Box{
|
|
|
|
.x = 0,
|
|
|
|
.y = 0,
|
|
|
|
.height = 0,
|
|
|
|
.width = 0,
|
|
|
|
};
|
|
|
|
self.pending_box = null;
|
2020-04-23 08:27:18 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
self.current_tags = tags;
|
|
|
|
self.pending_tags = null;
|
2020-04-02 04:44:24 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
self.pending_serial = null;
|
2020-04-01 08:50:49 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
self.stashed_buffer = null;
|
2020-04-01 08:50:49 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
self.impl = .{ .xdg_toplevel = undefined };
|
|
|
|
self.impl.xdg_toplevel.init(self, wlr_xdg_surface);
|
|
|
|
}
|
2020-03-22 14:42:55 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
pub fn deinit(self: *Self) void {
|
|
|
|
if (self.stashed_buffer) |buffer| {
|
|
|
|
c.wlr_buffer_unref(buffer);
|
2020-03-23 06:04:54 -07:00
|
|
|
}
|
2020-05-02 07:43:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn needsConfigure(self: Self) bool {
|
|
|
|
if (self.pending_box) |pending_box| {
|
|
|
|
return pending_box.width != self.current_box.width or
|
|
|
|
pending_box.height != self.current_box.height;
|
|
|
|
} else {
|
|
|
|
return false;
|
2020-03-26 13:32:30 -07:00
|
|
|
}
|
2020-05-02 07:43:16 -07:00
|
|
|
}
|
2020-03-26 13:32:30 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
pub fn configure(self: Self) void {
|
|
|
|
if (self.pending_box) |pending_box| {
|
|
|
|
switch (self.impl) {
|
|
|
|
.xdg_toplevel => |xdg_toplevel| xdg_toplevel.configure(pending_box),
|
2020-04-01 08:50:49 -07:00
|
|
|
}
|
2020-05-02 07:43:16 -07:00
|
|
|
} else {
|
|
|
|
Log.Error.log("Configure called on a View with no pending box", .{});
|
2020-03-26 13:32:30 -07:00
|
|
|
}
|
2020-05-02 07:43:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sendFrameDone(self: Self) void {
|
|
|
|
var now: c.timespec = undefined;
|
|
|
|
_ = c.clock_gettime(c.CLOCK_MONOTONIC, &now);
|
|
|
|
c.wlr_surface_send_frame_done(self.wlr_surface.?, &now);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dropStashedBuffer(self: *Self) void {
|
|
|
|
// TODO: log debug error
|
|
|
|
if (self.stashed_buffer) |buffer| {
|
|
|
|
c.wlr_buffer_unref(buffer);
|
|
|
|
self.stashed_buffer = null;
|
2020-03-26 13:32:30 -07:00
|
|
|
}
|
2020-05-02 07:43:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stashBuffer(self: *Self) void {
|
|
|
|
// TODO: log debug error if there is already a saved buffer
|
|
|
|
if (self.wlr_surface) |wlr_surface| {
|
|
|
|
if (c.wlr_surface_has_buffer(wlr_surface)) {
|
|
|
|
_ = c.wlr_buffer_ref(wlr_surface.buffer);
|
|
|
|
self.stashed_buffer = wlr_surface.buffer;
|
2020-03-26 13:32:30 -07:00
|
|
|
}
|
|
|
|
}
|
2020-05-02 07:43:16 -07:00
|
|
|
}
|
2020-03-26 13:32:30 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
/// Set the focued bool and the active state of the view if it is a toplevel
|
|
|
|
pub fn setFocused(self: *Self, focused: bool) void {
|
|
|
|
self.focused = focused;
|
|
|
|
switch (self.impl) {
|
|
|
|
.xdg_toplevel => |xdg_toplevel| xdg_toplevel.setActivated(focused),
|
2020-04-23 08:27:18 -07:00
|
|
|
}
|
2020-05-02 07:43:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If true is passsed, make the view float. If false, return it to the tiled
|
|
|
|
/// layout.
|
|
|
|
pub fn setFloating(self: *Self, float: bool) void {
|
|
|
|
if (float and !self.floating) {
|
|
|
|
self.floating = true;
|
|
|
|
self.pending_box = Box{
|
|
|
|
.x = std.math.max(0, @divTrunc(@intCast(i32, self.output.usable_box.width) -
|
|
|
|
@intCast(i32, self.natural_width), 2)),
|
|
|
|
.y = std.math.max(0, @divTrunc(@intCast(i32, self.output.usable_box.height) -
|
|
|
|
@intCast(i32, self.natural_height), 2)),
|
|
|
|
.width = self.natural_width,
|
|
|
|
.height = self.natural_height,
|
|
|
|
};
|
|
|
|
} else if (!float and self.floating) {
|
|
|
|
self.floating = false;
|
2020-04-22 13:50:03 -07:00
|
|
|
}
|
2020-05-02 07:43:16 -07:00
|
|
|
}
|
2020-04-22 13:50:03 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
/// Move a view from one output to another, sending the required enter/leave
|
|
|
|
/// events.
|
|
|
|
pub fn sendToOutput(self: *Self, destination_output: *Output) void {
|
|
|
|
const node = @fieldParentPtr(ViewStack(Self).Node, "view", self);
|
2020-04-18 07:21:26 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
self.output.views.remove(node);
|
|
|
|
destination_output.views.push(node);
|
2020-04-18 07:21:26 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
c.wlr_surface_send_leave(self.wlr_surface, self.output.wlr_output);
|
|
|
|
c.wlr_surface_send_enter(self.wlr_surface, destination_output.wlr_output);
|
2020-04-18 07:21:26 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
self.output = destination_output;
|
|
|
|
}
|
2020-04-18 07:21:26 -07:00
|
|
|
|
2020-05-02 07:43:16 -07:00
|
|
|
pub fn close(self: Self) void {
|
|
|
|
switch (self.impl) {
|
|
|
|
.xdg_toplevel => |xdg_toplevel| xdg_toplevel.close(),
|
2020-03-22 14:42:55 -07:00
|
|
|
}
|
2020-05-02 07:43:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn forEachSurface(
|
|
|
|
self: Self,
|
|
|
|
iterator: c.wlr_surface_iterator_func_t,
|
|
|
|
user_data: ?*c_void,
|
|
|
|
) void {
|
|
|
|
switch (self.impl) {
|
|
|
|
.xdg_toplevel => |xdg_toplevel| xdg_toplevel.forEachSurface(iterator, user_data),
|
2020-03-26 13:32:30 -07:00
|
|
|
}
|
2020-05-02 07:43:16 -07:00
|
|
|
}
|