~andreafeletto/levee

ref: 1ecf272596fd8f50d28073181b960f952abb4419 levee/src/modules/Pulse.zig -rw-r--r-- 5.8 KiB
1ecf2725Andrea Feletto pulse: read fd after poll 2 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
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
const std = @import("std");
const mem = std.mem;
const os = std.os;

const pulse = @cImport(@cInclude("pulse/pulseaudio.h"));

const Module = @import("../Modules.zig").Module;
const Event = @import("../Loop.zig").Event;
const render = @import("../render.zig");
const State = @import("../main.zig").State;
const utils = @import("../utils.zig");
const Pulse = @This();

state: *State,
mainloop: *pulse.pa_threaded_mainloop,
api: *pulse.pa_mainloop_api,
context: *pulse.pa_context,
fd: os.fd_t,
sink_name: []const u8,
volume: u8,
muted: bool,

pub fn create(state: *State) !*Pulse {
    const self = try state.gpa.create(Pulse);
    self.state = state;

    self.mainloop = pulse.pa_threaded_mainloop_new() orelse {
        return error.InitFailed;
    };
    self.api = pulse.pa_threaded_mainloop_get_api(self.mainloop);
    self.context = pulse.pa_context_new(self.api, "levee") orelse {
        return error.InitFailed;
    };
    const connected = pulse.pa_context_connect(
        self.context,
        null,
        pulse.PA_CONTEXT_NOFAIL,
        null,
    );
    if (connected < 0) return error.InitFailed;
    pulse.pa_context_set_state_callback(
        self.context,
        contextStateCallback,
        @ptrCast(*anyopaque, self),
    );
    const started = pulse.pa_threaded_mainloop_start(self.mainloop);
    if (started < 0) return error.InitFailed;

    const fd = try os.eventfd(0, os.linux.EFD.NONBLOCK);
    self.fd = @intCast(os.fd_t, fd);
    return self;
}

pub fn module(self: *Pulse) !Module {
    return Module{
        .impl = @ptrCast(*anyopaque, self),
        .funcs = .{
            .getEvent = getEvent,
            .print = print,
            .destroy = destroy,
        },
    };
}

fn getEvent(self_opaque: *anyopaque) !Event {
    const self = utils.cast(Pulse)(self_opaque);

    return Event{
        .fd = .{ .fd = self.fd, .events = os.POLL.IN, .revents = undefined },
        .data = self_opaque,
        .callbackIn = callbackIn,
        .callbackOut = Event.noop,
    };
}

fn print(self_opaque: *anyopaque, writer: Module.StringWriter) !void {
    const self = utils.cast(Pulse)(self_opaque);

    if (self.muted) {
        try writer.print("   🔇   ", .{});
    } else {
        try writer.print("🔊   {d}%", .{ self.volume });
    }
}

fn callbackIn(self_opaque: *anyopaque) error{Terminate}!void {
    const self = utils.cast(Pulse)(self_opaque);

    var data = mem.zeroes([8]u8);
    _ = os.read(self.fd, &data) catch return;

    for (self.state.wayland.monitors.items) |monitor| {
        if (monitor.bar) |bar| {
            if (bar.configured) {
                render.renderClock(bar) catch continue;
                render.renderModules(bar) catch continue;
                bar.clock.surface.commit();
                bar.modules.surface.commit();
                bar.background.surface.commit();
            }
        }
    }
}

fn destroy(self_opaque: *anyopaque) void {
    const self = utils.cast(Pulse)(self_opaque);

    self.api.quit.?(self.api, 0);
    pulse.pa_threaded_mainloop_stop(self.mainloop);
    pulse.pa_threaded_mainloop_free(self.mainloop);
    self.state.gpa.destroy(self);
}

export fn contextStateCallback(
    ctx: ?*pulse.pa_context,
    self_opaque: ?*anyopaque,
) void {
    const self = utils.cast(Pulse)(self_opaque.?);

    const ctx_state = pulse.pa_context_get_state(ctx);
    switch (ctx_state) {
        pulse.PA_CONTEXT_READY => {
            _ = pulse.pa_context_get_server_info(
                ctx,
                serverInfoCallback,
                self_opaque,
            );
            pulse.pa_context_set_subscribe_callback(
                ctx,
                subscribeCallback,
                self_opaque,
            );
            const mask = pulse.PA_SUBSCRIPTION_MASK_SERVER |
                pulse.PA_SUBSCRIPTION_MASK_SINK |
                pulse.PA_SUBSCRIPTION_MASK_SINK_INPUT |
                pulse.PA_SUBSCRIPTION_MASK_SOURCE |
                pulse.PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT;
            _ = pulse.pa_context_subscribe(ctx, mask, null, null);
        },
        pulse.PA_CONTEXT_TERMINATED => self.api.quit.?(self.api, 0),
        pulse.PA_CONTEXT_FAILED => self.api.quit.?(self.api, 0),
        else => {},
    }
}

export fn serverInfoCallback(
    ctx: ?*pulse.pa_context,
    info: ?*const pulse.pa_server_info,
    self_opaque: ?*anyopaque,
) void {
    const self = utils.cast(Pulse)(self_opaque.?);

    self.sink_name = mem.span(info.?.default_sink_name);
    _ = pulse.pa_context_get_sink_info_list(ctx, sinkInfoCallback, self_opaque);
}

export fn subscribeCallback(
    ctx: ?*pulse.pa_context,
    event_type: pulse.pa_subscription_event_type_t,
    index: u32,
    self_opaque: ?*anyopaque,
) void {
    const operation = event_type & pulse.PA_SUBSCRIPTION_EVENT_TYPE_MASK;
    if (operation != pulse.PA_SUBSCRIPTION_EVENT_CHANGE) return;

    const facility = event_type & pulse.PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
    if (facility == pulse.PA_SUBSCRIPTION_EVENT_SINK) {
        _ = pulse.pa_context_get_sink_info_by_index(
            ctx,
            index,
            sinkInfoCallback,
            self_opaque,
        );
    }
}

export fn sinkInfoCallback(
    _: ?*pulse.pa_context,
    maybe_info: ?*const pulse.pa_sink_info,
    _: c_int,
    self_opaque: ?*anyopaque,
) void {
    const self = utils.cast(Pulse)(self_opaque.?);
    const info = maybe_info orelse return;

    const sink_name = mem.span(info.name);
    if (!mem.eql(u8, self.sink_name, sink_name)) return;

    self.volume = volume: {
        const avg = pulse.pa_cvolume_avg(&info.volume);
        const norm = @intToFloat(f64, pulse.PA_VOLUME_NORM);
        const ratio = 100 * @intToFloat(f64, avg) / norm;
        break :volume @floatToInt(u8, @round(ratio));
    };
    self.muted = info.mute != 0;

    const increment = mem.asBytes(&@as(u64, 1));
    _ = os.write(self.fd, increment) catch return;
}