diff options
Diffstat (limited to 'src/config.zig')
| -rw-r--r-- | src/config.zig | 309 |
1 files changed, 31 insertions, 278 deletions
diff --git a/src/config.zig b/src/config.zig index 97985c9..529b3bd 100644 --- a/src/config.zig +++ b/src/config.zig @@ -1,305 +1,58 @@ -const c = @import("c.zig"); -const fs = @import("std").fs; const std = @import("std"); const debug = @import("std").debug; -const yaml = @import("yaml.zig"); const out = @import("output.zig"); pub const OutputConfig = union(enum) { window: out.WindowOutput.Config, texture_share_vk: out.TSVOutput.Config, -}; -pub const ParameterConfig = struct { - name: []const u8, - type: []const u8, + const default: OutputConfig = .{ .window = .default }; }; -pub const OSCConfig = union(enum) { - const Protocol = enum { - udp, - tcp, - unix, - }; - - Manual: struct { - protocol: Protocol = Protocol.udp, - port: u16 = 9000, - }, - URL: []const u8, -}; +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); +} -const defaultOutput: OutputConfig = .{ .window = .default }; +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, - fragment: []const u8, - parameters: []const ParameterConfig, - project_root: fs.Dir, - osc: OSCConfig, + osc: []const u8, - pub fn parse(allocator: std.mem.Allocator, filename: []const u8) !Config { - var parser: c.yaml_parser_t = undefined; - _ = c.yaml_parser_initialize(&parser); - defer c.yaml_parser_delete(&parser); - - const file = try fs.cwd().openFile(filename, .{}); - var buffer: [1024]u8 = undefined; - const len: usize = try file.read(buffer[0..]); - c.yaml_parser_set_input_string(&parser, buffer[0..], len); - - const dirname = fs.path.dirname(filename) orelse "."; - debug.print("file/dirname is {s} / {s}\n", .{ filename, dirname }); - var config: Config = .{ - .width = 1920, - .height = 1080, - .outputs = ([_]OutputConfig{defaultOutput})[0..], - .fragment = "", - .parameters = ([0]ParameterConfig{})[0..], - .project_root = try fs.cwd().openDir(dirname, .{}), - .osc = .{ .URL = "osc.udp://:9000" }, - }; + pub const default: Config = .{ + .width = 1920, + .height = 1080, + .outputs = ([_]OutputConfig{.default})[0..], + .osc = "osc.udp://:9000", + }; - try yaml.expectEvent(&parser, c.YAML_STREAM_START_EVENT); - try yaml.expectEvent(&parser, c.YAML_DOCUMENT_START_EVENT); - try yaml.expectEvent(&parser, c.YAML_MAPPING_START_EVENT); + pub fn init(allocator: std.mem.Allocator) !Config { + var config: Config = .default; - while (true) { - var event: c.yaml_event_t = undefined; - if (c.yaml_parser_parse(&parser, &event) != 1) - return error.YAMLParserError; - defer c.yaml_event_delete(&event); + var it = try std.process.argsWithAllocator(allocator); + defer it.deinit(); - switch (event.type) { - c.YAML_MAPPING_END_EVENT => break, - c.YAML_SCALAR_EVENT => { - const data = event.data.scalar; - const key: []const u8 = data.value[0..data.length]; + _ = it.skip(); - if (std.mem.eql(u8, key, "width")) { - config.width = try yaml.parseInt(&parser, i32); - } else if (std.mem.eql(u8, key, "height")) { - config.height = try yaml.parseInt(&parser, i32); - } else if (std.mem.eql(u8, key, "fragment")) { - config.fragment = try yaml.parseString(&parser, allocator); - } else if (std.mem.eql(u8, key, "outputs")) { - config.outputs = try parseOutputs(&parser, allocator); - } else if (std.mem.eql(u8, key, "parameters")) { - config.parameters = try parseParameters(&parser, allocator); - } else if (std.mem.eql(u8, key, "project_root")) { - config.project_root = try fs.cwd().openDir(try yaml.parseString(&parser, allocator), .{}); - } else if (std.mem.eql(u8, key, "osc")) { - config.osc = try parseOSC(&parser, allocator); - } else { - debug.print("unknown key: '{s}'\n", .{key}); - try yaml.skipAny(&parser); - } - }, - else => { - debug.print("unexpected event: {}\n", .{event.type}); - return error.InvalidConfiguration; - }, + 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 } - try yaml.expectEvent(&parser, c.YAML_DOCUMENT_END_EVENT); - try yaml.expectEvent(&parser, c.YAML_STREAM_END_EVENT); - return config; } }; - -fn parseOutputs(parser: *c.yaml_parser_t, allocator: std.mem.Allocator) ![]OutputConfig { - try yaml.expectEvent(parser, c.YAML_SEQUENCE_START_EVENT); - - var outputs = try allocator.alloc(OutputConfig, 1024); - var output_count: usize = 0; - - while (true) { - outputs[output_count] = yaml.parseUnion(parser, OutputConfig, allocator) catch |err| { - if (err == error.InvalidType) break; - return err; - }; - output_count += 1; - - // var seqEvent: c.yaml_event_t = undefined; - // if (c.yaml_parser_parse(parser, &seqEvent) != 1) - // return error.YAMLParserError; - // defer c.yaml_event_delete(&seqEvent); - - // switch (seqEvent.type) { - // c.YAML_SEQUENCE_END_EVENT => break, - // c.YAML_MAPPING_START_EVENT => { - // const output = &outputs[output_count]; - // output.* = defaultOutput; - // - // while (true) { - // var event: c.yaml_event_t = undefined; - // if (c.yaml_parser_parse(parser, &event) != 1) - // return error.YAMLParserError; - // defer c.yaml_event_delete(&event); - // - // switch (event.type) { - // c.YAML_MAPPING_END_EVENT => break, - // c.YAML_SCALAR_EVENT => { - // const data = event.data.scalar; - // const key: []const u8 = data.value[0..data.length]; - // - // if (!std.mem.eql(u8, key, "type")) { - // const ouput_type = try yaml.parseEnum(parser, OutputConfig.Type); - // output.* = switch (ouput_type) { - // inline else => |t| { - // - // } - // } - // } - // - // if (std.mem.eql(u8, key, "width")) { - // output.*.width = try yaml.parseInt(parser, i32); - // } else if (std.mem.eql(u8, key, "height")) { - // output.*.height = try yaml.parseInt(parser, i32); - // } else if (std.mem.eql(u8, key, "filter")) { - // output.*.filter = try yaml.parseEnum(parser, OutputConfig.FilterMode); - // } else if (std.mem.eql(u8, key, "monitor")) { - // output.*.monitor = try yaml.parseString(parser, allocator); - // } else if (std.mem.eql(u8, key, "fullscreen")) { - // output.*.fullscreen = try yaml.parseBool(parser); - // } else { - // debug.print("unknown key: '{s}'\n", .{key}); - // try yaml.skipAny(parser); - // } - // }, - // else => { - // debug.print("unexpected event: {}\n", .{event.type}); - // return error.InvalidConfiguration; - // }, - // } - // } - // - // output_count += 1; - // }, - // else => { - // debug.print("unexpected event: {}\n", .{seqEvent.type}); - // return error.InvalidConfiguration; - // }, - // } - } - - _ = allocator.realloc(outputs, output_count) catch 0; - return outputs[0..output_count]; -} - -fn parseParameters(parser: *c.yaml_parser_t, allocator: std.mem.Allocator) ![]ParameterConfig { - try yaml.expectEvent(parser, c.YAML_SEQUENCE_START_EVENT); - - var parameters = try allocator.alloc(ParameterConfig, 1024); - var param_count: usize = 0; - - while (true) { - var seqEvent: c.yaml_event_t = undefined; - if (c.yaml_parser_parse(parser, &seqEvent) != 1) - return error.YAMLParserError; - defer c.yaml_event_delete(&seqEvent); - - switch (seqEvent.type) { - c.YAML_SEQUENCE_END_EVENT => break, - c.YAML_MAPPING_START_EVENT => { - const param = ¶meters[param_count]; - var have_name = false; - var have_type = false; - - while (true) { - var event: c.yaml_event_t = undefined; - if (c.yaml_parser_parse(parser, &event) != 1) - return error.YAMLParserError; - defer c.yaml_event_delete(&event); - - switch (event.type) { - c.YAML_MAPPING_END_EVENT => break, - c.YAML_SCALAR_EVENT => { - const data = event.data.scalar; - const key: []const u8 = data.value[0..data.length]; - - if (std.mem.eql(u8, key, "name")) { - param.*.name = try yaml.parseString(parser, allocator); - have_name = true; - } else if (std.mem.eql(u8, key, "type")) { - param.*.type = try yaml.parseString(parser, allocator); - have_type = true; - } else { - debug.print("unknown key: '{s}'\n", .{key}); - try yaml.skipAny(parser); - } - }, - else => { - debug.print("unexpected event: {}\n", .{event.type}); - return error.InvalidConfiguration; - }, - } - } - - if (!(have_name and have_type)) { - debug.print("name and type are mandatory for parameters.\n", .{}); - return error.InvalidConfiguration; - } - - param_count += 1; - }, - else => { - debug.print("unexpected event: {}\n", .{seqEvent.type}); - return error.InvalidConfiguration; - }, - } - } - - _ = allocator.realloc(parameters, param_count) catch 0; - return parameters[0..param_count]; -} - -fn parseOSC(parser: *c.yaml_parser_t, allocator: std.mem.Allocator) !OSCConfig { - var event: c.yaml_event_t = undefined; - if (c.yaml_parser_parse(parser, &event) != 1) - return error.YAMLParserError; - defer c.yaml_event_delete(&event); - - if (event.type == c.YAML_SCALAR_EVENT) { - const url = try yaml.parseStringEvent(&event, allocator); - return OSCConfig{ .URL = url }; - } else if (event.type != c.YAML_MAPPING_START_EVENT) { - debug.print("unexpected event: {}\n", .{event.type}); - return error.InvalidConfiguration; - } - - var config: OSCConfig = .{ .Manual = .{} }; - - while (true) { - if (c.yaml_parser_parse(parser, &event) != 1) - return error.YAMLParserError; - defer c.yaml_event_delete(&event); - - switch (event.type) { - c.YAML_MAPPING_END_EVENT => break, - c.YAML_SCALAR_EVENT => { - const data = event.data.scalar; - const key: []const u8 = data.value[0..data.length]; - - if (std.mem.eql(u8, key, "protocol")) { - config.Manual.protocol = try yaml.parseEnum(parser, OSCConfig.Protocol); - } else if (std.mem.eql(u8, key, "port")) { - config.Manual.port = try yaml.parseInt(parser, u16); - } else { - debug.print("unknown key: '{s}'\n", .{key}); - try yaml.skipAny(parser); - } - }, - else => { - debug.print("unexpected event: {}\n", .{event.type}); - return error.InvalidConfiguration; - }, - } - } - - return config; -} |
