~andreafeletto/levee

ref: f2b74950b7431f6024814ff54cbc0949e29e9a5d levee/src/main.zig -rw-r--r-- 1.9 KiB
f2b74950Andrea Feletto add args for help and modules 2 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
const std = @import("std");
const heap = std.heap;
const io = std.io;
const mem = std.mem;
const os = std.os;

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

const Config = @import("Config.zig");
const Loop = @import("Loop.zig");
const Modules = @import("Modules.zig");
const Wayland = @import("wayland.zig").Wayland;

pub const State = struct {
    gpa: mem.Allocator,
    config: Config,
    wayland: Wayland,
    modules: Modules,
    loop: Loop,
};

pub fn main() anyerror!void {
    var gpa: heap.GeneralPurposeAllocator(.{}) = .{};
    defer _ = gpa.deinit();

    fcft.init(.auto, false, .info);

    // cli arguments
    const params = comptime [_]clap.Param(clap.Help){
        try clap.parseParam("-h, --help  Display this help and exit."),
        try clap.parseParam("-m, --module <str>...  Add module."),
    };
    var args = try clap.parse(clap.Help, &params, .{});
    defer args.deinit();
    if (args.flag("--help")) {
        return clap.help(io.getStdErr().writer(), &params);
    }

    // initialization
    var state: State = undefined;
    state.gpa = gpa.allocator();
    state.config = try Config.init();
    state.wayland = try Wayland.init(&state);
    defer state.wayland.deinit();
    state.modules = Modules.init(&state);
    defer state.modules.deinit();
    state.loop = try Loop.init(&state);

    // modules
    for (args.options("--module")) |module_name| {
        if (mem.eql(u8, module_name, "alsa")) {
            try state.modules.register(Modules.Alsa);
        } else if (mem.eql(u8, module_name, "backlight")) {
            try state.modules.register(Modules.Backlight);
        } else if (mem.eql(u8, module_name, "battery")) {
            try state.modules.register(Modules.Battery);
        } else {
            std.log.err("unknown module: {s}", .{ module_name });
            os.exit(1);
        }
    }

    // event loop
    try state.wayland.registerGlobals();
    try state.loop.run();
}