~schnouki/pustule

ref: 2e90522e70214bb6d1bb88aef6f7d8c06152a99f pustule/pulse.c -rw-r--r-- 8.0 KiB
2e90522e — Thomas Jost Expose log functions in Lua 10 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
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
/**
 * pustule
 * Copyright 2014-2020 Thomas Jost <schnouki@schnouki.net>
 *
 * This file is part of pustule.
 *
 * pustule is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * pustule is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * pustule. If not, see <http://www.gnu.org/licenses/>.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pulse/pulseaudio.h>

#include "log.h"
#include "pustule.h"

/* Global PulseAudio context and main loop */
pa_context* pactx = NULL;
pa_mainloop* paloop = NULL;

/* PulseAudio state changed -- query for infos and subscribe to new events */
void state_callback(pa_context* ctx, void* userdata) {
    pa_context_state_t state = pa_context_get_state(ctx);

    char* st = "unknown";
    if (state == PA_CONTEXT_UNCONNECTED)
        st = "unconnected";
    else if (state == PA_CONTEXT_CONNECTING)
        st = "connecting";
    else if (state == PA_CONTEXT_AUTHORIZING)
        st = "authorizing";
    else if (state == PA_CONTEXT_SETTING_NAME)
        st = "setting_name";
    else if (state == PA_CONTEXT_READY)
        st = "ready";
    else if (state == PA_CONTEXT_FAILED)
        st = "failed";
    else if (state == PA_CONTEXT_TERMINATED)
        st = "terminated";
    log_debug("State is now %s", st);

    if (state == PA_CONTEXT_READY) {
        // Get infos about all current inputs
        pa_context_get_sink_input_info_list(ctx, input_info_callback, NULL);

        // Get infos about all current devices
        pa_context_get_sink_info_list(ctx, device_info_callback, NULL);

        // Subscribe to new events
        pa_context_set_subscribe_callback(ctx, event_callback, NULL);
        pa_context_subscribe(ctx, PA_SUBSCRIPTION_MASK_SINK_INPUT | PA_SUBSCRIPTION_MASK_SINK, NULL, NULL);
    }
}

/* Event handler -- dispatch Lua events */
void event_callback(pa_context* ctx, pa_subscription_event_type_t t, uint32_t idx, void* userdata) {
    if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
        // This event is about an input!

        if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
            // Input added: query info about it
            pa_context_get_sink_input_info(ctx, idx, input_info_callback, NULL);
        }
        else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
            // Input removed: directly call the corresponding callback
            pustule_event_t* event = malloc(sizeof(pustule_event_t));
            if (!event) abort();

            event->type = INPUT_REMOVED;
            event->input_event.index = idx;
            pustule_handle_event(event);
        }

    }
    else if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
        // This event is about a device!

        if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
            // Device added: query info about it
            pa_context_get_sink_info_by_index(ctx, idx, device_info_callback, NULL);
        }
        else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
            // Device removed: directly call the corresponding callback
            pustule_event_t* event = malloc(sizeof(pustule_event_t));
            if (!event) abort();

            event->type = DEVICE_REMOVED;
            event->device_event.index = idx;
            pustule_handle_event(event);
        }
    }
}

/* New input available (or infos about an existing input): defer a call to the "input added" callback */
void input_info_callback(pa_context* ctx, const pa_sink_input_info* i, int eol, void* userdata) {
    if (!i) return;

    pustule_event_t* event = malloc(sizeof(pustule_event_t));
    if (!event) abort();
    pustule_input_event_t* iev = &event->input_event;

    event->type = INPUT_ADDED;
    iev->index = i->index;
    iev->name = strdup(i->name);
    iev->driver = strdup(i->name);
    iev->mute = i->mute;
    iev->volume_writable = i->volume_writable;
    iev->has_volume = i->has_volume;
    iev->volume = i->volume;

    iev->proplist = NULL;
    if (i->proplist)
        iev->proplist = pa_proplist_copy(i->proplist);

    /* Event callback */
    pustule_handle_event(event);
}

/* New device available (or infos about an existing device): defer a call to the "device added" callback */
void device_info_callback(pa_context* ctx, const pa_sink_info* i, int eol, void* userdata) {
    if (!i) return;

    pustule_event_t* event = malloc(sizeof(pustule_event_t));
    if (!event) abort();
    pustule_device_event_t* dev = &event->device_event;

    event->type = DEVICE_ADDED;
    dev->index = i->index;
    dev->name = strdup(i->name);
    dev->description = strdup(i->description);
    dev->mute = i->mute;
    dev->volume = i->volume;

    dev->proplist = NULL;
    if (i->proplist)
        dev->proplist = pa_proplist_copy(i->proplist);

    /* Event callback */
    pustule_handle_event(event);
}

/* Query info about an input or device (for its volume) */
typedef struct {
    pa_operation* op;
    pustule_volume_callback_t callback;
    void* userdata;
} _volume_data_cb_t;

static void input_volume_info_callback(pa_context* ctx, const pa_sink_input_info* i, int eol, void* userdata) {
    if (!i) return;
    _volume_data_cb_t* data = (_volume_data_cb_t*) userdata;
    double volume = ((double) pa_cvolume_avg(&(i->volume))) / PA_VOLUME_NORM;

    data->callback(volume, data->userdata);
    pa_operation_unref(data->op);
    free(data);
}
void input_get_volume(uint32_t idx, pustule_volume_callback_t callback, void* userdata) {
    _volume_data_cb_t* data = malloc(sizeof(_volume_data_cb_t));
    if (!data)
        abort();
    data->callback = callback;
    data->userdata = userdata;
    data->op = pa_context_get_sink_input_info(pactx, idx, input_volume_info_callback, (void*) data);
}

static void device_volume_info_callback(pa_context* ctx, const pa_sink_info* i, int eol, void* userdata) {
    if (!i) return;
    _volume_data_cb_t* data = (_volume_data_cb_t*) userdata;
    double volume = ((double) pa_cvolume_avg(&(i->volume))) / PA_VOLUME_NORM;

    data->callback(volume, data->userdata);
    pa_operation_unref(data->op);
    free(data);
}
void device_get_volume(uint32_t idx, pustule_volume_callback_t callback, void* userdata) {
    _volume_data_cb_t* data = malloc(sizeof(_volume_data_cb_t));
    if (!data)
        abort();
    data->callback = callback;
    data->userdata = userdata;
    data->op = pa_context_get_sink_info_by_index(pactx, idx, device_volume_info_callback, (void*) data);
}

/* Query info about the server */
typedef struct {
    pa_operation* op;
    pustule_server_info_callback_t callback;
    void* userdata;
} _server_info_data_cb_t;

static void server_info_callback(pa_context* ctx, const pa_server_info *i, void* userdata) {
    if (!i) return;
    _server_info_data_cb_t* data = (_server_info_data_cb_t*) userdata;

    pustule_server_info_t* info = malloc(sizeof(pustule_server_info_t));
    if (!info)
        abort();

    info->user_name = i->user_name;
    info->host_name = i->host_name;
    info->server_version = i->server_version;
    info->server_name = i->server_name;
    info->default_sink_name = i->default_sink_name;

    data->callback(info, data->userdata);
    pa_operation_unref(data->op);
    free(info);
}
void server_get_info(pustule_server_info_callback_t callback, void* userdata) {
    _server_info_data_cb_t* data = malloc(sizeof(_server_info_data_cb_t));
    if (!data)
        abort();
    data->callback = callback;
    data->userdata = userdata;
    data->op = pa_context_get_server_info(pactx, server_info_callback, (void*) data);
}