git.s-ol.nu glsl-view / master src / config.zig
master

Tree @master (Download .tar.gz)

config.zig @masterraw · history · blame

  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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
const c = @import("c.zig");
const fs = @import("std").fs;
const std = @import("std");
const debug = @import("std").debug;

pub const OutputConfig = struct {
    const Type = enum {
        window,
    };

    type: Type,
    width: i32,
    height: i32,

    // only for 'window'
    monitor: []const u8,
    fullscreen: bool,
};

pub const ParameterConfig = struct {
    name: []const u8,
    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 = .window,
    .width = 800,
    .height = 600,
    .monitor = "",
    .fullscreen = false,
};

pub const Config = struct {
    width: i32,
    height: i32,
    outputs: []const OutputConfig,
    fragment: []const u8,
    parameters: []const ParameterConfig,
    project_root: fs.Dir,
    osc: OSCConfig,

    pub fn parse(allocator: *const 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 "";
        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" },
        };

        try expectEvent(&parser, c.YAML_STREAM_START_EVENT);
        try expectEvent(&parser, c.YAML_DOCUMENT_START_EVENT);
        try expectEvent(&parser, c.YAML_MAPPING_START_EVENT);

        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, "width")) {
                        config.width = try parseInt(&parser, i32);
                    } 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, 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 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 skipAny(&parser);
                    }
                },
                else => {
                    debug.print("unexpected event: {}\n", .{event.type});
                    return error.InvalidConfiguration;
                },
            }
        }

        try expectEvent(&parser, c.YAML_DOCUMENT_END_EVENT);
        try expectEvent(&parser, c.YAML_STREAM_END_EVENT);

        return config;
    }
};

fn expectEvent(parser: *c.yaml_parser_t, expectedType: c.yaml_event_type_t) !void {
    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 != expectedType) {
        debug.print("unexpected event: {}\n", .{event.type});
        return error.InvalidConfiguration;
    }
}

fn parseStringEvent(event: *c.yaml_event_t, allocator: *const std.mem.Allocator) ![]const u8 {
    if (event.type != c.YAML_SCALAR_EVENT)
        return error.InvalidType;

    const data = event.data.scalar;
    const value: []const u8 = data.value[0..data.length];
    return try allocator.dupe(u8, value);
}

fn parseString(parser: *c.yaml_parser_t, allocator: *const 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)
        return error.YAMLParserError;
    defer c.yaml_event_delete(&event);

    if (event.type != c.YAML_SCALAR_EVENT)
        return error.InvalidType;

    const data = event.data.scalar;
    const value: []const u8 = data.value[0..data.length];
    return try std.fmt.parseInt(T, value, 10);
}

fn parseEnum(parser: *c.yaml_parser_t, comptime T: type) !T {
    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)
        return error.InvalidType;

    const data = event.data.scalar;
    const value: []const u8 = data.value[0..data.length];

    inline for (@typeInfo(T).Enum.fields) |field| {
        if (std.mem.eql(u8, value, field.name))
            return @field(T, field.name);
    }

    return error.InvalidValue;
}

fn parseBool(parser: *c.yaml_parser_t) !bool {
    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)
        return error.InvalidType;

    const data = event.data.scalar;
    const value: []const u8 = data.value[0..data.length];

    if (data.style != c.YAML_PLAIN_SCALAR_STYLE)
        return false;
    return std.mem.eql(u8, value, "true") or std.mem.eql(u8, value, "1");
}

fn parseOutputs(parser: *c.yaml_parser_t, allocator: *const std.mem.Allocator) ![]OutputConfig {
    try expectEvent(parser, c.YAML_SEQUENCE_START_EVENT);

    var outputs = try allocator.alloc(OutputConfig, 1024);
    var output_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 => {
                var 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")) {
                                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")) {
                                output.*.height = try parseInt(parser, i32);
                            } else if (std.mem.eql(u8, key, "monitor")) {
                                output.*.monitor = try parseString(parser, allocator);
                            } else if (std.mem.eql(u8, key, "fullscreen")) {
                                output.*.fullscreen = try parseBool(parser);
                            } else {
                                debug.print("unknown key: '{s}'\n", .{key});
                                try 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: *const std.mem.Allocator) ![]ParameterConfig {
    try 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 => {
                var param = &parameters[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 parseString(parser, allocator);
                                have_name = true;
                            } else if (std.mem.eql(u8, key, "type")) {
                                param.*.type = try parseString(parser, allocator);
                                have_type = true;
                            } else {
                                debug.print("unknown key: '{s}'\n", .{key});
                                try 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: *const 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 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 parseEnum(parser, OSCConfig.Protocol);
                } else if (std.mem.eql(u8, key, "port")) {
                    config.Manual.port = try parseInt(parser, u16);
                } else {
                    debug.print("unknown key: '{s}'\n", .{key});
                    try skipAny(parser);
                }
            },
            else => {
                debug.print("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;
    while (true) {
        if (c.yaml_parser_parse(parser, &event) != 1)
            return error.YAMLParserError;
        defer c.yaml_event_delete(&event);

        switch (event.type) {
            c.YAML_SCALAR_EVENT => {
                if (depth == 0)
                    break;
            },

            c.YAML_SEQUENCE_START_EVENT,
            c.YAML_MAPPING_START_EVENT,
            => depth += 1,

            c.YAML_SEQUENCE_END_EVENT,
            c.YAML_MAPPING_END_EVENT,
            => {
                depth -= 1;

                if (depth == 0)
                    break;
            },

            else => {
                debug.print("unexpected event: {}\n", .{event.type});
                return error.InvalidConfiguration;
            },
        }
    }
}