aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.zig
blob: 795c564fc7335b8630f387af66026f25d5a5647e (plain)
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
const std = @import("std");
const debug = @import("std").debug;
const out = @import("output.zig");
const build_config = @import("build_config");

pub const OutputConfig = union(enum) {
    window: out.WindowOutput.Config,
    stdout: out.StdoutOutput.Config,
    texture_share_vk: if (build_config.have_tsv) @import("tsv.zig").TSVOutput.Config else void,

    const default: OutputConfig = .{ .window = .default };
};

fn parseInt(it: *std.process.ArgIterator, comptime T: type) !T {
    const value = it.next() orelse return error.missingArgument;
    return try std.fmt.parseInt(T, value, 10);
}

fn parseString(it: *std.process.ArgIterator, allocator: std.mem.Allocator) ![]u8 {
    const value = it.next() orelse return error.missingArgument;
    return allocator.dupe(u8, value);
}

pub const Config = struct {
    width: i32,
    height: i32,
    outputs: []const OutputConfig,
    osc: []const u8,

    pub const default: Config = .{
        .width = 1920,
        .height = 1080,
        .outputs = &.{},
        .osc = "osc.udp://:9000",
    };

    pub fn init(allocator: std.mem.Allocator) !Config {
        var config: Config = .default;

        var it = try std.process.argsWithAllocator(allocator);
        defer it.deinit();

        _ = it.skip();

        var outputs = std.ArrayList(OutputConfig).empty;

        while (it.next()) |arg| {
            if (std.mem.eql(u8, arg, "--width")) {
                config.width = try parseInt(&it, i32);
            } else if (std.mem.eql(u8, arg, "--height")) {
                config.height = try parseInt(&it, i32);
            } else if (std.mem.eql(u8, arg, "--osc")) {
                config.osc = try parseString(&it, allocator);
            } else if (std.mem.eql(u8, arg, "--window")) {
                var window: OutputConfig = .{ .window = .default };
                window.window.width = config.width;
                window.window.height = config.height;
                try outputs.append(allocator, window);
            } else if (std.mem.eql(u8, arg, "--stdout")) {
                try outputs.append(allocator, .{ .stdout = .default });
            } else {
                return error.invalidArgument;
            }
        }

        if (outputs.items.len == 0) {
            var window: OutputConfig = .{ .window = .default };
            window.window.width = config.width;
            window.window.height = config.height;
            try outputs.append(allocator, window);
        }

        config.outputs = try outputs.toOwnedSlice(allocator);

        return config;
    }
};