aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.zig
blob: 5d3ef6ac511bc498f47d6e8b9a82d4ce1c5807d3 (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
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,
    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();

        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 {
                return error.invalidArgument;
            }
            // @TODO: output config
        }

        if (config.outputs.len == 0) {
            const outputs = try allocator.alloc(OutputConfig, 1);
            // const outputs = try allocator.alloc(OutputConfig, 2);
            outputs[0] = .{ .window = .default };
            outputs[0].window.width = config.width;
            outputs[0].window.height = config.height;
            // outputs[1] = .{ .window = .default };
            // outputs[1].window.width = config.width;
            // outputs[1].window.height = config.height;
            config.outputs = outputs;
        }

        return config;
    }
};