]> git.sur5r.net Git - i3/i3status/blob - src/pulse.c
fix clang errors
[i3/i3status] / src / pulse.c
1 // vim:ts=4:sw=4:expandtab
2 #include <string.h>
3 #include <stdio.h>
4 #include <math.h>
5 #include <signal.h>
6 #include <pulse/pulseaudio.h>
7 #include "i3status.h"
8 #include "queue.h"
9
10 #define APP_NAME "i3status"
11 #define APP_ID "org.i3wm"
12
13 typedef struct indexed_volume_s {
14     char *name;
15     uint32_t idx;
16     int volume;
17     TAILQ_ENTRY(indexed_volume_s)
18     entries;
19 } indexed_volume_t;
20
21 static pa_threaded_mainloop *main_loop = NULL;
22 static pa_context *context = NULL;
23 static pa_mainloop_api *api = NULL;
24 static bool context_ready = false;
25 static bool mainloop_thread_running = false;
26 static uint32_t default_sink_idx = DEFAULT_SINK_INDEX;
27 TAILQ_HEAD(tailhead, indexed_volume_s)
28 cached_volume =
29     TAILQ_HEAD_INITIALIZER(cached_volume);
30 static pthread_mutex_t pulse_mutex = PTHREAD_MUTEX_INITIALIZER;
31
32 static void pulseaudio_error_log(pa_context *c) {
33     fprintf(stderr,
34             "i3status: PulseAudio: %s\n",
35             pa_strerror(pa_context_errno(c)));
36 }
37
38 static bool pulseaudio_free_operation(pa_context *c, pa_operation *o) {
39     if (o)
40         pa_operation_unref(o);
41     else
42         pulseaudio_error_log(c);
43     /* return false if the operation failed */
44     return o;
45 }
46
47 /*
48  * save the volume for the specified sink index
49  * returning true if the value was changed
50  */
51 static bool save_volume(uint32_t sink_idx, int new_volume, const char *name) {
52     pthread_mutex_lock(&pulse_mutex);
53     indexed_volume_t *entry;
54     TAILQ_FOREACH(entry, &cached_volume, entries) {
55         if (name) {
56             if (!entry->name || strcmp(entry->name, name)) {
57                 continue;
58             }
59         } else {
60             if (entry->idx != sink_idx) {
61                 continue;
62             }
63         }
64         const bool changed = (new_volume != entry->volume);
65         entry->volume = new_volume;
66         pthread_mutex_unlock(&pulse_mutex);
67         return changed;
68     }
69     /* index not found, store it */
70     entry = malloc(sizeof(*entry));
71     TAILQ_INSERT_HEAD(&cached_volume, entry, entries);
72     entry->idx = sink_idx;
73     entry->volume = new_volume;
74     if (name) {
75         entry->name = malloc(strlen(name) + 1);
76         strcpy(entry->name, name);
77     } else {
78         entry->name = NULL;
79     }
80     pthread_mutex_unlock(&pulse_mutex);
81     return true;
82 }
83
84 static void store_volume_from_sink_cb(pa_context *c,
85                                       const pa_sink_info *info,
86                                       int eol,
87                                       void *userdata) {
88     if (eol < 0) {
89         if (pa_context_errno(c) == PA_ERR_NOENTITY)
90             return;
91
92         pulseaudio_error_log(c);
93         return;
94     }
95
96     if (eol > 0)
97         return;
98
99     int avg_vol = pa_cvolume_avg(&info->volume);
100     int vol_perc = roundf((float)avg_vol * 100 / PA_VOLUME_NORM);
101     int composed_volume = COMPOSE_VOLUME_MUTE(vol_perc, info->mute);
102
103     /* if this is the default sink we must try to save it twice: once with
104      * DEFAULT_SINK_INDEX as the index, and another with its proper value
105      * (using bitwise OR to avoid early-out logic) */
106     if ((info->index == default_sink_idx &&
107          save_volume(DEFAULT_SINK_INDEX, composed_volume, NULL)) |
108         save_volume(info->index, composed_volume, info->name)) {
109         /* if the volume or mute flag changed, wake the main thread */
110         pthread_kill(main_thread, SIGUSR1);
111     }
112 }
113
114 static void get_sink_info(pa_context *c, uint32_t idx, const char *name) {
115     pa_operation *o;
116
117     if (name || idx == DEFAULT_SINK_INDEX) {
118         o = pa_context_get_sink_info_by_name(
119             c, name ? name : "@DEFAULT_SINK@", store_volume_from_sink_cb, NULL);
120     } else {
121         o = pa_context_get_sink_info_by_index(
122             c, idx, store_volume_from_sink_cb, NULL);
123     }
124     if (o) {
125         pulseaudio_free_operation(c, o);
126     }
127 }
128
129 static void store_default_sink_cb(pa_context *c,
130                                   const pa_sink_info *i,
131                                   int eol,
132                                   void *userdata) {
133     if (i) {
134         if (default_sink_idx != i->index) {
135             /* default sink changed? */
136             default_sink_idx = i->index;
137             store_volume_from_sink_cb(c, i, eol, userdata);
138         }
139     }
140 }
141
142 static void update_default_sink(pa_context *c) {
143     pa_operation *o = pa_context_get_sink_info_by_name(
144         c,
145         "@DEFAULT_SINK@",
146         store_default_sink_cb,
147         NULL);
148     pulseaudio_free_operation(c, o);
149 }
150
151 static void subscribe_cb(pa_context *c, pa_subscription_event_type_t t,
152                          uint32_t idx, void *userdata) {
153     if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
154         return;
155     pa_subscription_event_type_t facility =
156         t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
157     switch (facility) {
158         case PA_SUBSCRIPTION_EVENT_SERVER:
159             /* server change event, see if the default sink changed */
160             update_default_sink(c);
161             break;
162         case PA_SUBSCRIPTION_EVENT_SINK:
163             get_sink_info(c, idx, NULL);
164             break;
165         default:
166             break;
167     }
168 }
169
170 static void context_state_callback(pa_context *c, void *userdata) {
171     switch (pa_context_get_state(c)) {
172         case PA_CONTEXT_UNCONNECTED:
173         case PA_CONTEXT_CONNECTING:
174         case PA_CONTEXT_AUTHORIZING:
175         case PA_CONTEXT_SETTING_NAME:
176         case PA_CONTEXT_TERMINATED:
177         default:
178             context_ready = false;
179             break;
180
181         case PA_CONTEXT_READY: {
182             pa_context_set_subscribe_callback(c, subscribe_cb, NULL);
183             update_default_sink(c);
184
185             pa_operation *o = pa_context_subscribe(
186                 c,
187                 PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SERVER,
188                 NULL,
189                 NULL);
190             if (!pulseaudio_free_operation(c, o))
191                 break;
192             context_ready = true;
193         } break;
194
195         case PA_CONTEXT_FAILED:
196             /* server disconnected us, attempt to reconnect */
197             context_ready = false;
198             pa_context_unref(context);
199             context = NULL;
200             break;
201     }
202 }
203
204 /*
205  * returns the current volume in percent, which, as per PulseAudio,
206  * may be > 100%
207  */
208 int volume_pulseaudio(uint32_t sink_idx, const char *sink_name) {
209     if (!context_ready || default_sink_idx == DEFAULT_SINK_INDEX)
210         return -1;
211
212     pthread_mutex_lock(&pulse_mutex);
213     const indexed_volume_t *entry;
214     TAILQ_FOREACH(entry, &cached_volume, entries) {
215         if (sink_name) {
216             if (!entry->name || strcmp(entry->name, sink_name)) {
217                 continue;
218             }
219         } else {
220             if (entry->idx != sink_idx) {
221                 continue;
222             }
223         }
224         int vol = entry->volume;
225         pthread_mutex_unlock(&pulse_mutex);
226         return vol;
227     }
228     pthread_mutex_unlock(&pulse_mutex);
229     /* first time requires a prime callback call because we only get
230      * updates when the volume actually changes, but we need it to
231      * be correct even if it never changes */
232     pa_threaded_mainloop_lock(main_loop);
233     get_sink_info(context, sink_idx, sink_name);
234     pa_threaded_mainloop_unlock(main_loop);
235     /* show 0 while we don't have this information */
236     return 0;
237 }
238
239 /*
240  *  detect and, if necessary, initialize the PulseAudio API
241  */
242 bool pulse_initialize(void) {
243     if (!main_loop) {
244         main_loop = pa_threaded_mainloop_new();
245         if (!main_loop)
246             return false;
247     }
248     if (!api) {
249         api = pa_threaded_mainloop_get_api(main_loop);
250         if (!api)
251             return false;
252     }
253     if (!context) {
254         pa_proplist *proplist = pa_proplist_new();
255         pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, APP_NAME);
256         pa_proplist_sets(proplist, PA_PROP_APPLICATION_ID, APP_ID);
257         pa_proplist_sets(proplist, PA_PROP_APPLICATION_VERSION, VERSION);
258         context = pa_context_new_with_proplist(api, APP_NAME, proplist);
259         pa_proplist_free(proplist);
260         if (!context)
261             return false;
262         pa_context_set_state_callback(context,
263                                       context_state_callback,
264                                       NULL);
265         if (pa_context_connect(context,
266                                NULL,
267                                PA_CONTEXT_NOFAIL | PA_CONTEXT_NOAUTOSPAWN,
268                                NULL) < 0) {
269             pulseaudio_error_log(context);
270             return false;
271         }
272         if (!mainloop_thread_running &&
273             pa_threaded_mainloop_start(main_loop) < 0) {
274             pulseaudio_error_log(context);
275             pa_threaded_mainloop_free(main_loop);
276             main_loop = NULL;
277             return false;
278         }
279         mainloop_thread_running = true;
280     }
281     return true;
282 }