~andreafeletto/levee

ref: a40cd1f35e815d58ac2b5bdf3968a611a8a0972e levee/src/Modules.zig -rw-r--r-- 1.4 KiB
a40cd1f3Andrea Feletto pulse: respect muted flag 5 months ago
                                                                                
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
const std = @import("std");
const ArrayList = std.ArrayList;

const Event = @import("Loop.zig").Event;
const State = @import("main.zig").State;
const Modules = @This();

state: *State,
modules: ArrayList(Module),

pub const Backlight = @import("modules/Backlight.zig");
pub const Battery = @import("modules/Battery.zig");
pub const Pulse = @import("modules/Pulse.zig");

pub const Module = struct {
    impl: *anyopaque,
    funcs: struct {
        getEvent: fn (*anyopaque) anyerror!Event,
        print: fn (*anyopaque, StringWriter) anyerror!void,
        destroy: fn (*anyopaque) void,
    },

    pub const StringWriter = std.ArrayList(u8).Writer;

    pub fn getEvent(self: *Module) !Event {
        return self.funcs.getEvent(self.impl);
    }

    pub fn print(self: *Module, writer: StringWriter) !void {
        return self.funcs.print(self.impl, writer);
    }

    pub fn destroyInstance(self: *Module) void {
        return self.funcs.destroy(self.impl);
    }
};

pub fn init(state: *State) Modules {
    return Modules{
        .state = state,
        .modules = ArrayList(Module).init(state.gpa),
    };
}

pub fn deinit(self: *Modules) void {
    for (self.modules.items) |*module| module.destroyInstance();
    self.modules.deinit();
}

pub fn register(self: *Modules, comptime ModuleType: type) !void {
    const instance = try ModuleType.create(self.state);
    try self.modules.append(try instance.module());
}