aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.zig')
-rw-r--r--src/config.zig105
1 files changed, 85 insertions, 20 deletions
diff --git a/src/config.zig b/src/config.zig
index 3ef8a9e..14f0402 100644
--- a/src/config.zig
+++ b/src/config.zig
@@ -3,12 +3,12 @@ const fs = @import("std").fs;
const std = @import("std");
const debug = @import("std").debug;
-pub const OutputType = enum {
- window,
-};
-
pub const OutputConfig = struct {
- type: OutputType,
+ const Type = enum {
+ window,
+ };
+
+ type: Type,
width: i32,
height: i32,
@@ -22,8 +22,22 @@ pub const ParameterConfig = struct {
type: []const u8,
};
+pub const OSCConfig = union(enum) {
+ const Protocol = enum {
+ udp,
+ tcp,
+ unix,
+ };
+
+ Manual: struct {
+ protocol: Protocol = Protocol.udp,
+ port: u16 = 9000,
+ },
+ URL: []const u8,
+};
+
const defaultOutput = OutputConfig{
- .type = OutputType.window,
+ .type = .window,
.width = 800,
.height = 600,
.monitor = "",
@@ -36,15 +50,14 @@ pub const Config = struct {
outputs: []const OutputConfig,
fragment: []const u8,
parameters: []const ParameterConfig,
- arena: std.heap.ArenaAllocator,
+ osc: OSCConfig,
- pub fn parse(filename: []const u8) !Config {
+ 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, .{});
- // c.yaml_parser_set_input_file(&parser, @as([*c]c.FILE, file.handle));
var buffer: [1024]u8 = undefined;
const len: usize = try file.read(buffer[0..]);
c.yaml_parser_set_input_string(&parser, &buffer[0], len);
@@ -55,7 +68,7 @@ pub const Config = struct {
.outputs = ([_]OutputConfig{defaultOutput})[0..],
.fragment = "",
.parameters = ([0]ParameterConfig{})[0..],
- .arena = std.heap.ArenaAllocator.init(std.heap.page_allocator),
+ .osc = .{ .URL = "osc.udp://localhost:9000" },
};
try expectEvent(&parser, c.yaml_event_type_t.YAML_STREAM_START_EVENT);
@@ -79,11 +92,13 @@ pub const Config = struct {
} else if (std.mem.eql(u8, key, "height")) {
config.height = try parseInt(&parser, i32);
} else if (std.mem.eql(u8, key, "fragment")) {
- config.fragment = try parseString(&parser, &config.arena.allocator);
+ config.fragment = try parseString(&parser, allocator);
} else if (std.mem.eql(u8, key, "outputs")) {
- config.outputs = try parseOutputs(&parser, &config.arena.allocator);
+ config.outputs = try parseOutputs(&parser, allocator);
} else if (std.mem.eql(u8, key, "parameters")) {
- config.parameters = try parseParameters(&parser, &config.arena.allocator);
+ config.parameters = try parseParameters(&parser, allocator);
+ } else if (std.mem.eql(u8, key, "osc")) {
+ config.osc = try parseOSC(&parser, allocator);
} else {
debug.warn("unknown key: '{}'\n", .{key});
try skipAny(&parser);
@@ -116,12 +131,7 @@ fn expectEvent(parser: *c.yaml_parser_t, expectedType: c.yaml_event_type_t) !voi
}
}
-fn parseString(parser: *c.yaml_parser_t, allocator: *std.mem.Allocator) ![]const u8 {
- var event: c.yaml_event_t = undefined;
- if (c.yaml_parser_parse(parser, &event) != 1)
- return error.YAMLParserError;
- defer c.yaml_event_delete(&event);
-
+fn parseStringEvent(event: *c.yaml_event_t, allocator: *std.mem.Allocator) ![]const u8 {
if (event.type != c.yaml_event_type_t.YAML_SCALAR_EVENT)
return error.InvalidType;
@@ -130,6 +140,15 @@ fn parseString(parser: *c.yaml_parser_t, allocator: *std.mem.Allocator) ![]const
return try std.mem.dupe(allocator, u8, value);
}
+fn parseString(parser: *c.yaml_parser_t, allocator: *std.mem.Allocator) ![]const u8 {
+ var event: c.yaml_event_t = undefined;
+ if (c.yaml_parser_parse(parser, &event) != 1)
+ return error.YAMLParserError;
+ defer c.yaml_event_delete(&event);
+
+ return parseStringEvent(&event, allocator);
+}
+
fn parseInt(parser: *c.yaml_parser_t, comptime T: type) !T {
var event: c.yaml_event_t = undefined;
if (c.yaml_parser_parse(parser, &event) != 1)
@@ -212,7 +231,7 @@ fn parseOutputs(parser: *c.yaml_parser_t, allocator: *std.mem.Allocator) ![]Outp
const key: []const u8 = data.value[0..data.length];
if (std.mem.eql(u8, key, "type")) {
- output.*.type = try parseEnum(parser, OutputType);
+ output.*.type = try parseEnum(parser, OutputConfig.Type);
} else if (std.mem.eql(u8, key, "width")) {
output.*.width = try parseInt(parser, i32);
} else if (std.mem.eql(u8, key, "height")) {
@@ -313,6 +332,52 @@ fn parseParameters(parser: *c.yaml_parser_t, allocator: *std.mem.Allocator) ![]P
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_event_type_t.YAML_SCALAR_EVENT) {
+ const url = try parseStringEvent(&event, allocator);
+ return OSCConfig{ .URL = url };
+ } else if (event.type != c.yaml_event_type_t.YAML_MAPPING_START_EVENT) {
+ debug.warn("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_event_type_t.YAML_MAPPING_END_EVENT => break,
+ c.yaml_event_type_t.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 parseEnum(parser, OSCConfig.Protocol);
+ } else if (std.mem.eql(u8, key, "port")) {
+ config.Manual.port = try parseInt(parser, u16);
+ } else {
+ debug.warn("unknown key: '{}'\n", .{key});
+ try skipAny(parser);
+ }
+ },
+ else => {
+ debug.warn("unexpected event: {}\n", .{event.type});
+ return error.InvalidConfiguration;
+ },
+ }
+ }
+
+ return config;
+}
+
fn skipAny(parser: *c.yaml_parser_t) !void {
var event: c.yaml_event_t = undefined;
var depth: u32 = 0;