aboutsummaryrefslogtreecommitdiff
path: root/src/root.zig
blob: e31d2e10d78abdb66a00d23edc8e224ebca4539f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const std = @import("std");
const c = @import("c.zig").c;

const Output = @import("output.zig").Output;
const Server = @import("server.zig").Server;
const Seat = @import("seat.zig").Seat;
const View = @import("view.zig").View;

/// Responsible for all windowing operations
pub const Root = struct {
    const Self = @This();

    server: *Server,

    wlr_output_layout: *c.wlr_output_layout,
    outputs: std.TailQueue(Output),

    // Must stay ordered, first N views in list are the masters
    views: std.TailQueue(View),
    unmapped_views: std.TailQueue(View),

    // Number of pending configures sent in the current transaction.
    // A value of 0 means there is no current transaction.
    pending_count: u32,

    pub fn init(self: *Self, server: *Server) !void {
        self.server = server;
        self.pending_count = 0;

        // Create an output layout, which a wlroots utility for working with an
        // arrangement of screens in a physical layout.
        self.wlr_output_layout = c.wlr_output_layout_create() orelse
            return error.CantCreateWlrOutputLayout;
        errdefer c.wlr_output_layout_destroy(self.wlr_output_layout);

        self.outputs = std.TailQueue(Output).init();
        self.views = std.TailQueue(View).init();
        self.unmapped_views = std.TailQueue(View).init();
    }

    pub fn destroy(self: *Self) void {
        c.wlr_output_layout_destroy(self.wlr_output_layout);
    }

    pub fn addOutput(self: *Self, wlr_output: *c.wlr_output) void {
        // TODO: Handle failure
        const node = self.outputs.allocateNode(self.server.allocator) catch unreachable;
        node.data.init(self, wlr_output) catch unreachable;
        self.outputs.append(node);
    }

    pub fn addView(self: *Self, wlr_xdg_surface: *c.wlr_xdg_surface) void {
        const node = self.views.allocateNode(self.server.allocator) catch unreachable;
        node.data.init(self, wlr_xdg_surface);
        self.unmapped_views.append(node);
    }

    /// Finds the top most view under the output layout coordinates lx, ly
    /// returns the view if found, and a pointer to the wlr_surface as well as the surface coordinates
    pub fn viewAt(self: *Self, lx: f64, ly: f64, surface: *?*c.wlr_surface, sx: *f64, sy: *f64) ?*View {
        var it = self.views.last;
        while (it) |node| : (it = node.prev) {
            if (node.data.isAt(lx, ly, surface, sx, sy)) {
                return &node.data;
            }
        }
        return null;
    }

    pub fn arrange(self: *Self) void {
        if (self.views.len == 0) {
            return;
        }
        // Super basic vertical layout for now, no master/slave stuff
        // This can't return null if pass null as the reference
        const output_box: *c.wlr_box = c.wlr_output_layout_get_box(self.wlr_output_layout, null);
        const new_height = output_box.height;
        // Allow for a 10px gap
        const num_views = @intCast(c_int, self.views.len);
        const new_width = @divTrunc(output_box.width, num_views) - (num_views - 1) * 10;

        var x: c_int = 0;
        var y: c_int = 0;

        var it = self.views.first;
        while (it) |node| : (it = node.next) {
            const view = &node.data;
            view.pending_state.x = x;
            view.pending_state.y = y;
            view.pending_state.width = @intCast(u32, new_width);
            view.pending_state.height = @intCast(u32, new_height);

            x += new_width + 10;
        }

        self.startTransaction();
    }

    /// Initiate an atomic change to the layout. This change will not be
    /// applied until all affected clients ack a configure and commit a buffer.
    fn startTransaction(self: *Self) void {
        std.debug.assert(self.pending_count == 0);

        var it = self.views.first;
        while (it) |node| : (it = node.next) {
            const view = &node.data;
            if (view.needsConfigure()) {
                view.configurePending();
                self.pending_count += 1;

                // We save the current buffer, so we can send an early
                // frame done event to give the client a head start on
                // redrawing.
                view.sendFrameDone();
            }
            view.stashBuffer();
        }

        // TODO: start a timer and handle timeout waiting for all clients to ack
    }

    pub fn notifyConfigured(self: *Self) void {
        self.pending_count -= 1;
        if (self.pending_count == 0) {
            self.commitTransaction();
        }
    }

    /// Apply the pending state and drop stashed buffers. This means that
    /// the next frame drawn will be the post-transaction state of the
    /// layout. Must only be called after all clients have configured for
    /// the new layout.
    fn commitTransaction(self: *Self) void {
        // TODO: apply damage properly
        var it = self.views.first;
        while (it) |node| : (it = node.next) {
            const view = &node.data;

            // TODO: handle views that timed out
            view.current_state = view.pending_state;
            view.dropStashedBuffer();
        }
    }
};