1 // vim:ts=4:sw=4:expandtab
6 #include <pulse/pulseaudio.h>
10 #define APP_NAME "i3status"
11 #define APP_ID "org.i3wm"
13 typedef struct indexed_volume_s {
17 TAILQ_ENTRY(indexed_volume_s)
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)
29 TAILQ_HEAD_INITIALIZER(cached_volume);
30 static pthread_mutex_t pulse_mutex = PTHREAD_MUTEX_INITIALIZER;
32 static void pulseaudio_error_log(pa_context *c) {
34 "i3status: PulseAudio: %s\n",
35 pa_strerror(pa_context_errno(c)));
38 static bool pulseaudio_free_operation(pa_context *c, pa_operation *o) {
40 pa_operation_unref(o);
42 pulseaudio_error_log(c);
43 /* return false if the operation failed */
48 * save the volume for the specified sink index
49 * returning true if the value was changed
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) {
56 if (!entry->name || strcmp(entry->name, name)) {
60 if (entry->idx != sink_idx) {
64 const bool changed = (new_volume != entry->volume);
65 entry->volume = new_volume;
66 pthread_mutex_unlock(&pulse_mutex);
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;
75 entry->name = malloc(strlen(name) + 1);
76 strcpy(entry->name, name);
80 pthread_mutex_unlock(&pulse_mutex);
84 static void store_volume_from_sink_cb(pa_context *c,
85 const pa_sink_info *info,
89 if (pa_context_errno(c) == PA_ERR_NOENTITY)
92 pulseaudio_error_log(c);
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);
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);
114 static void get_sink_info(pa_context *c, uint32_t idx, const char *name) {
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);
121 o = pa_context_get_sink_info_by_index(
122 c, idx, store_volume_from_sink_cb, NULL);
125 pulseaudio_free_operation(c, o);
129 static void store_default_sink_cb(pa_context *c,
130 const pa_sink_info *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);
142 static void update_default_sink(pa_context *c) {
143 pa_operation *o = pa_context_get_sink_info_by_name(
146 store_default_sink_cb,
148 pulseaudio_free_operation(c, o);
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)
155 pa_subscription_event_type_t facility =
156 t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
158 case PA_SUBSCRIPTION_EVENT_SERVER:
159 /* server change event, see if the default sink changed */
160 update_default_sink(c);
162 case PA_SUBSCRIPTION_EVENT_SINK:
163 get_sink_info(c, idx, NULL);
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:
178 context_ready = false;
181 case PA_CONTEXT_READY: {
182 pa_context_set_subscribe_callback(c, subscribe_cb, NULL);
183 update_default_sink(c);
185 pa_operation *o = pa_context_subscribe(
187 PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SERVER,
190 if (!pulseaudio_free_operation(c, o))
192 context_ready = true;
195 case PA_CONTEXT_FAILED:
196 /* server disconnected us, attempt to reconnect */
197 context_ready = false;
198 pa_context_unref(context);
205 * returns the current volume in percent, which, as per PulseAudio,
208 int volume_pulseaudio(uint32_t sink_idx, const char *sink_name) {
209 if (!context_ready || default_sink_idx == DEFAULT_SINK_INDEX)
212 pthread_mutex_lock(&pulse_mutex);
213 const indexed_volume_t *entry;
214 TAILQ_FOREACH(entry, &cached_volume, entries) {
216 if (!entry->name || strcmp(entry->name, sink_name)) {
220 if (entry->idx != sink_idx) {
224 int vol = entry->volume;
225 pthread_mutex_unlock(&pulse_mutex);
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 */
240 * detect and, if necessary, initialize the PulseAudio API
242 bool pulse_initialize(void) {
244 main_loop = pa_threaded_mainloop_new();
249 api = pa_threaded_mainloop_get_api(main_loop);
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);
262 pa_context_set_state_callback(context,
263 context_state_callback,
265 if (pa_context_connect(context,
267 PA_CONTEXT_NOFAIL | PA_CONTEXT_NOAUTOSPAWN,
269 pulseaudio_error_log(context);
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);
279 mainloop_thread_running = true;