]> git.sur5r.net Git - i3/i3status/blob - src/pulse.c
Volume percentage should be obtained by float division and rounded to int.
[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 <pulse/pulseaudio.h>
6 #include "i3status.h"
7 #include "queue.h"
8
9 #define APP_NAME "i3status"
10 #define APP_ID "org.i3wm"
11
12 typedef struct indexed_volume_s {
13     uint32_t idx;
14     int volume;
15     TAILQ_ENTRY(indexed_volume_s) entries;
16 } indexed_volume_t;
17
18 static pa_threaded_mainloop *main_loop = NULL;
19 static pa_context *context = NULL;
20 static pa_mainloop_api *api = NULL;
21 static bool context_ready = false;
22 static uint32_t default_sink_idx = DEFAULT_SINK_INDEX;
23 TAILQ_HEAD(tailhead, indexed_volume_s) cached_volume =
24     TAILQ_HEAD_INITIALIZER(cached_volume);
25 static pthread_mutex_t pulse_mutex = PTHREAD_MUTEX_INITIALIZER;
26
27 static void pulseaudio_error_log(pa_context *c) {
28     fprintf(stderr,
29             "i3status: PulseAudio: %s\n",
30             pa_strerror(pa_context_errno(c)));
31 }
32
33 static bool pulseaudio_free_operation(pa_context *c, pa_operation *o) {
34     if (o)
35         pa_operation_unref(o);
36     else
37         pulseaudio_error_log(c);
38     /* return false if the operation failed */
39     return o;
40 }
41
42 /*
43  * save the volume for the specified sink index
44  * returning true if the value was changed
45  */
46 static bool save_volume(uint32_t sink_idx, int new_volume) {
47     pthread_mutex_lock(&pulse_mutex);
48     indexed_volume_t *entry;
49     TAILQ_FOREACH(entry, &cached_volume, entries) {
50         if (entry->idx == sink_idx) {
51             const bool changed = (new_volume != entry->volume);
52             entry->volume = new_volume;
53             pthread_mutex_unlock(&pulse_mutex);
54             return changed;
55         }
56     }
57     /* index not found, store it */
58     entry = malloc(sizeof(*entry));
59     TAILQ_INSERT_HEAD(&cached_volume, entry, entries);
60     entry->idx = sink_idx;
61     entry->volume = new_volume;
62     pthread_mutex_unlock(&pulse_mutex);
63     return true;
64 }
65
66 static void store_volume_from_sink_cb(pa_context *c,
67                                       const pa_sink_info *info,
68                                       int eol,
69                                       void *userdata) {
70     if (eol < 0) {
71         if (pa_context_errno(c) == PA_ERR_NOENTITY)
72             return;
73
74         pulseaudio_error_log(c);
75         return;
76     }
77
78     if (eol > 0)
79         return;
80
81     int avg_vol = pa_cvolume_avg(&info->volume);
82     int vol_perc = roundf((float)avg_vol * 100 / PA_VOLUME_NORM);
83     int composed_volume = COMPOSE_VOLUME_MUTE(vol_perc, info->mute);
84
85     /* if this is the default sink we must try to save it twice: once with
86      * DEFAULT_SINK_INDEX as the index, and another with its proper value
87      * (using bitwise OR to avoid early-out logic) */
88     if ((info->index == default_sink_idx &&
89          save_volume(DEFAULT_SINK_INDEX, composed_volume)) |
90         save_volume(info->index, composed_volume)) {
91         /* if the volume or mute flag changed, wake the main thread */
92         pthread_mutex_lock(&i3status_sleep_mutex);
93         pthread_cond_broadcast(&i3status_sleep_cond);
94         pthread_mutex_unlock(&i3status_sleep_mutex);
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             break;
157
158         case PA_CONTEXT_READY: {
159             pa_context_set_subscribe_callback(c, subscribe_cb, NULL);
160             update_default_sink(c);
161
162             pa_operation *o = pa_context_subscribe(
163                 c,
164                 PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SERVER,
165                 NULL,
166                 NULL);
167             if (!pulseaudio_free_operation(c, o))
168                 break;
169             context_ready = true;
170         } break;
171
172         case PA_CONTEXT_FAILED:
173             pulseaudio_error_log(c);
174             break;
175     }
176 }
177
178 /*
179  * returns the current volume in percent, which, as per PulseAudio,
180  * may be > 100%
181  */
182 int volume_pulseaudio(uint32_t sink_idx) {
183     if (!context_ready || default_sink_idx == DEFAULT_SINK_INDEX)
184         return -1;
185
186     pthread_mutex_lock(&pulse_mutex);
187     const indexed_volume_t *entry;
188     TAILQ_FOREACH(entry, &cached_volume, entries) {
189         if (entry->idx == sink_idx) {
190             int vol = entry->volume;
191             pthread_mutex_unlock(&pulse_mutex);
192             return vol;
193         }
194     }
195     pthread_mutex_unlock(&pulse_mutex);
196     /* first time requires a prime callback call because we only get
197      * updates when the volume actually changes, but we need it to
198      * be correct even if it never changes */
199     pa_threaded_mainloop_lock(main_loop);
200     get_sink_info(context, sink_idx);
201     pa_threaded_mainloop_unlock(main_loop);
202     /* show 0 while we don't have this information */
203     return 0;
204 }
205
206 /*
207  *  detect and, if necessary, initialize the PulseAudio API
208  */
209 bool pulse_initialize(void) {
210     if (!main_loop) {
211         main_loop = pa_threaded_mainloop_new();
212         if (!main_loop)
213             return false;
214     }
215     if (!api) {
216         api = pa_threaded_mainloop_get_api(main_loop);
217         if (!api)
218             return false;
219     }
220     if (!context) {
221         pa_proplist *proplist = pa_proplist_new();
222         pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, APP_NAME);
223         pa_proplist_sets(proplist, PA_PROP_APPLICATION_ID, APP_ID);
224         pa_proplist_sets(proplist, PA_PROP_APPLICATION_VERSION, VERSION);
225         context = pa_context_new_with_proplist(api, APP_NAME, proplist);
226         pa_proplist_free(proplist);
227         if (!context)
228             return false;
229         pa_context_set_state_callback(context,
230                                       context_state_callback,
231                                       NULL);
232         if (pa_context_connect(context,
233                                NULL,
234                                PA_CONTEXT_NOFAIL | PA_CONTEXT_NOAUTOSPAWN,
235                                NULL) < 0) {
236             pulseaudio_error_log(context);
237             return false;
238         }
239         if (pa_threaded_mainloop_start(main_loop) < 0) {
240             pulseaudio_error_log(context);
241             pa_threaded_mainloop_free(main_loop);
242             main_loop = NULL;
243             return false;
244         }
245     }
246     return true;
247 }