~andreafeletto/levee

ref: a773bcb6a7f6170279720581150b2eb8eabf762c levee/src/main.zig -rw-r--r-- 2.1 KiB
a773bcb6Andrea Feletto add README.md 5 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
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;

pub const State = struct {
    wayland: *Wayland,
    tags: *Tags,
    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 wayland = try Wayland.init();
    var tags = try Tags.init(&wayland);

    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();

    var font_names = [_][*:0]const u8{ "monospace:size=18" };
    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,
        .allocator = &arena.allocator,
        .font = fcft.Font.fromName(mem.span(&font_names), null).?,
        .foreground = &foreground,
        .background = &background,
        .draw = draw,
    };

    try wayland.setup(30);
    try tags.listen(&state);
    draw(&state);
    wayland.loop();
}

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

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

    const bg = pixman.Color{ .red = 0, .green = 0, .blue = 0, .alpha = 0xffff };
    const rects = [_]pixman.Rectangle16{
        .{ .x = 0, .y = 0, .width = @intCast(u16, width), .height = @intCast(u16, height) },
    };
    _ = pixman.Image.fillRectangles(.src, buffer.pix, &bg, 1, &rects);

    state.tags.render(buffer.pix, state);

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

    buffer.destroy();
}