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