~andreafeletto/levee

ref: ae55230ec4895a224104a266195278ff23a8d44b levee/src/main.zig -rw-r--r-- 2.3 KiB
ae55230eAndrea Feletto correct rendering of clock 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
const std = @import("std");
const mem = std.mem;
const os = std.os;

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

const Wayland = @import("wayland.zig").Wayland;
const Buffer = @import("shm.zig").Buffer;
const Tags = @import("tags.zig").Tags;
const Clock = @import("clock.zig").Clock;

pub const State = struct {
    wayland: *Wayland,
    tags: *Tags,
    clock: *Clock,
    allocator: *mem.Allocator,
    font: *fcft.Font,
    foreground: *const pixman.Color,
    background: *const pixman.Color,
    draw: fn (state: *State) void,
};

pub fn main() anyerror!void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    const allocator = &arena.allocator;

    const wayland = try Wayland.create(allocator);
    defer wayland.destroy();
    try wayland.setup(34);

    var tags = try Tags.init(wayland);
    var clock = Clock{};

    var font_names = [_][*:0]const u8{"monospace:size=14"};
    const foreground = pixman.Color{
        .red = 0xffff,
        .green = 0xffff,
        .blue = 0xffff,
        .alpha = 0xffff,
    };
    const background = pixman.Color{
        .red = 0,
        .green = 0,
        .blue = 0,
        .alpha = 0xffff,
    };

    var state = State{
        .wayland = wayland,
        .tags = &tags,
        .clock = &clock,
        .allocator = allocator,
        .font = try fcft.Font.fromName(font_names[0..], null),
        .foreground = &foreground,
        .background = &background,
        .draw = draw,
    };

    try tags.listen(&state);
    try clock.start(&state);
    // draw(&state);
    wayland.loop();
}

fn draw(state: *State) void {
    const width = @intCast(u16, state.wayland.width);
    const height = @intCast(u16, state.wayland.height);
    const surface = state.wayland.wlSurface;

    const shm = state.wayland.shm.?;
    const buffer = Buffer.create(state.allocator, shm, width, height) catch return;
    defer buffer.destroy();

    const area = [_]pixman.Rectangle16{
        .{ .x = 0, .y = 0, .width = width, .height = height },
    };
    _ = pixman.Image.fillRectangles(.src, buffer.pix, state.background, 1, &area);

    state.tags.render(buffer.pix, state) catch return;
    state.clock.render(buffer.pix, state) catch return;

    surface.attach(buffer.wlBuffer, 0, 0);
    surface.damageBuffer(0, 0, width, height);
    surface.commit();
}