aboutsummaryrefslogtreecommitdiffstats
path: root/src/control.zig
blob: cec6f8093420b22dab287b73fea84c08a10ba0aa (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
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
const c = @import("c.zig");
const gl = @import("gl.zig");
const std = @import("std");
const cfg = @import("config.zig");

const param_prefix = "/param/";

pub const ControlServer = struct {
    server: c.lo_server,
    shader: gl.ShaderProgram,

    pub fn init(allocator: *std.mem.Allocator, config: cfg.OSCConfig) !*ControlServer {
        var self: *ControlServer = try allocator.create(ControlServer);

        switch (config) {
            .Manual => |conf| {
                var port = [_]u8{0} ** 6;
                _ = std.fmt.formatIntBuf(port[0..], conf.port, 10, false, std.fmt.FormatOptions{});
                const proto: c_int = switch (conf.protocol) {
                    .udp => c.LO_UDP,
                    .tcp => c.LO_TCP,
                    .unix => c.LO_UNIX,
                    else => unreachable,
                };

                std.debug.warn("listening for OSC messages on {} port {}\n", .{ conf.protocol, conf.port });
                self.server = c.lo_server_new_with_proto(port[0..].ptr, proto, handle_error);
            },
            .URL => |url| {
                std.debug.warn("listening for OSC messages at {s}\n", .{url});
                self.server = c.lo_server_new_from_url(url[0..].ptr, handle_error);
            },
            else => unreachable,
        }

        if (self.server == null)
            return error.serverInitializationError;
        _ = c.lo_server_add_method(self.server, null, null, handle_method, @ptrCast(*c_void, self));

        return self;
    }

    pub fn update(self: *ControlServer, shader: gl.ShaderProgram) void {
        self.shader = shader;
        while (c.lo_server_recv_noblock(self.server, 0) > 0) {}
    }

    pub fn destroy(self: ControlServer) void {
        c.lo_server_free(self.server);
    }

    fn handle_error(num: c_int, msg: [*c]const u8, where: [*c]const u8) callconv(.C) void {
        std.debug.warn("OSC error {} @ {s}: {s}\n", .{ num, @ptrCast([*:0]const u8, where), @ptrCast([*:0]const u8, msg) });
    }

    fn handle_method(_path: [*c]const u8, _types: [*c]const u8, argv: [*c][*c]c.lo_arg, argc: c_int, msg: c.lo_message, userdata: ?*c_void) callconv(.C) c_int {
        const self = @ptrCast(*ControlServer, @alignCast(@alignOf(ControlServer), userdata.?));
        const path = _path[0..c.strlen(_path)];
        const types = _types[0..@intCast(u32, argc)];

        if (std.mem.startsWith(u8, path, "/param/")) {
            if (!std.mem.endsWith(u8, path, "/set")) {
                std.debug.warn("unhandled OSC message {s} ({s})\n", .{ path, types });
                return 1;
            }

            const name_slice = path["/param/".len .. path.len - "/set".len];
            // const uniform = self.shader.uniformLocation(name) catch {
            //     std.debug.warn("param {s} doesn't exist\n", .{uniform_name});
            //     return 0;
            // };

            // if (std.mem.eql(u8, types, "f")) {
            //     self.shader.setUniform1f(uniform, argv[0].*.f);
            // } else if (std.mem.eql(u8, types, "ff")) {
            //     self.shader.setUniform2f(uniform, argv[0].*.f, argv[1].*.f);
            // } else if (std.mem.eql(u8, types, "fff")) {
            //     self.shader.setUniform3f(uniform, argv[0].*.f, argv[1].*.f, argv[2].*.f);
            // } else if (std.mem.eql(u8, types, "ffff")) {
            //     self.shader.setUniform4f(uniform, argv[0].*.f, argv[1].*.f, argv[2].*.f, argv[3].*.f);
            // } else if (std.mem.eql(u8, types, "i")) {
            //     self.shader.setUniform1i(uniform, argv[0].*.i);
            // } else if (std.mem.eql(u8, types, "ii")) {
            //     self.shader.setUniform2i(uniform, argv[0].*.i, argv[1].*.i);
            // } else if (std.mem.eql(u8, types, "iii")) {
            //     self.shader.setUniform3i(uniform, argv[0].*.i, argv[1].*.i, argv[2].*.i);
            // } else if (std.mem.eql(u8, types, "iiii")) {
            //     self.shader.setUniform4i(uniform, argv[0].*.i, argv[1].*.i, argv[2].*.i, argv[3].*.i);
            // } else {
            //     std.debug.warn("unsupported types: {s}\n", .{types});
            // }
            return 0;
        }

        std.debug.warn("unhandled OSC message {s} ({s})\n", .{ path, types });
        return 1;
    }

    fn handle_bundle_start(time: c.lo_timetag, userdata: ?*c_void) callconv(.C) c_int {
        return 1;
    }
    fn handle_bundle_end(userdata: ?*c_void) callconv(.C) c_int {
        return 1;
    }
};