~andreafeletto/levee

ref: 68ee28d8b66dde9d8af13495c7b23c548c5191af levee/src/tags.zig -rw-r--r-- 4.3 KiB
68ee28d8Andrea Feletto remove 10th tag 4 months ago
                                                                                
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
const std = @import("std");
const os = std.os;

const wayland = @import("wayland").client;
const wl = wayland.wl;
const zriver = wayland.zriver;

const pixman = @import("pixman");
const fcft = @import("fcft");

const Wayland = @import("wayland.zig").Wayland;
const State = @import("main.zig").State;

pub const Tag = struct {
    label: u8,
    focused: bool = false,
    occupied: bool = false,

    pub fn render(self: *const Tag, pix: *pixman.Image, offset: i16, state: *State) !void {
        const size = @intCast(u16, state.wayland.height);

        const outer = [_]pixman.Rectangle16{
            .{ .x = offset, .y = 0, .width = size, .height = size },
        };
        const outer_color = if (self.focused or self.occupied)
            state.foreground
        else
            state.background;
        _ = pixman.Image.fillRectangles(.over, pix, outer_color, 1, &outer);

        const inner = [_]pixman.Rectangle16{
            .{ .x = offset + 1, .y = 1, .width = size - 2, .height = size - 2 },
        };
        if (!self.focused and self.occupied) {
            _ = pixman.Image.fillRectangles(.over, pix, state.background, 1, &inner);
        }

        const glyph_color = if (self.focused)
            state.background
        else
            state.foreground;
        var char = pixman.Image.createSolidFill(glyph_color).?;
        const glyph = try fcft.Glyph.rasterize(state.font, self.label, .default);
        const x = offset + @divFloor(size - glyph.width, 2);
        const y = @divFloor(size - glyph.height, 2);
        pixman.Image.composite32(.over, char, glyph.pix, pix, 0, 0, 0, 0, x, y, glyph.width, glyph.height);
    }
};

pub const Tags = struct {
    statusManager: ?*zriver.StatusManagerV1 = null,
    outputStatus: *zriver.OutputStatusV1,
    tags: [9]Tag,

    pub fn init(state: *Wayland) !Tags {
        var result = Tags{
            .outputStatus = undefined,
            .tags = std.mem.zeroes([9]Tag),
        };

        for (result.tags) |*tag, i| {
            tag.label = '1' + @intCast(u8, i);
        }

        const registry = try state.display.getRegistry();
        registry.setListener(*Tags, globalListener, &result);
        _ = try state.display.roundtrip();

        if (result.statusManager == null) {
            std.log.err("globals: river status manager not available", .{});
            os.exit(1);
        }

        result.outputStatus = try result.statusManager.?.getRiverOutputStatus(
            state.output.?,
        );

        return result;
    }

    pub fn listen(self: *Tags, state: *State) !void {
        self.outputStatus.setListener(*State, outputStatusListener, state);
        _ = try state.wayland.display.roundtrip();
    }

    pub fn render(self: *const Tags, pix: *pixman.Image, state: *State) !void {
        for (self.tags) |tag, i| {
            const offset = @intCast(i16, state.wayland.height * i);
            try tag.render(pix, offset, state);
        }
    }

    pub fn resetOccupied(self: *Tags) void {
        for (self.tags) |*tag| {
            tag.occupied = false;
        }
    }

    pub fn deinit(self: *Tags) void {
        self.outputStatus.destroy();
        self.statusManager.?.destroy();
    }
};

fn globalListener(registry: *wl.Registry, event: wl.Registry.Event, state: *Tags) void {
    switch (event) {
        .global => |global| {
            const inter = zriver.StatusManagerV1;
            const name = inter.getInterface().name;
            if (std.cstr.cmp(global.interface, name) == 0) {
                state.statusManager = registry.bind(global.name, inter, 1) catch return;
            }
        },
        .global_remove => {},
    }
}

fn outputStatusListener(status: *zriver.OutputStatusV1, event: zriver.OutputStatusV1.Event, state: *State) void {
    switch (event) {
        .focused_tags => |data| {
            for (state.tags.tags) |*tag, i| {
                const mask = @as(u32, 1) << @intCast(u5, i);
                tag.focused = data.tags & mask != 0;
            }
            state.draw(state);
        },
        .view_tags => |data| {
            state.tags.resetOccupied();
            for (data.tags.slice(u32)) |view| {
                for (state.tags.tags) |*tag, i| {
                    const mask = @as(u32, 1) << @intCast(u5, i);
                    if (view & mask != 0) tag.occupied = true;
                }
            }
        },
    }
    // state.draw(state);
}