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) entries;
20 static pa_threaded_mainloop *main_loop = NULL;
21 static pa_context *context = NULL;
22 static pa_mainloop_api *api = NULL;
23 static bool context_ready = false;
24 static bool mainloop_thread_running = false;
25 static uint32_t default_sink_idx = DEFAULT_SINK_INDEX;
26 TAILQ_HEAD(tailhead, indexed_volume_s) cached_volume =
27 TAILQ_HEAD_INITIALIZER(cached_volume);
28 static pthread_mutex_t pulse_mutex = PTHREAD_MUTEX_INITIALIZER;
30 static void pulseaudio_error_log(pa_context *c) {
32 "i3status: PulseAudio: %s\n",
33 pa_strerror(pa_context_errno(c)));
36 static bool pulseaudio_free_operation(pa_context *c, pa_operation *o) {
38 pa_operation_unref(o);
40 pulseaudio_error_log(c);
41 /* return false if the operation failed */
46 * save the volume for the specified sink index
47 * returning true if the value was changed
49 static bool save_volume(uint32_t sink_idx, int new_volume, const char *name) {
50 pthread_mutex_lock(&pulse_mutex);
51 indexed_volume_t *entry;
52 TAILQ_FOREACH(entry, &cached_volume, entries) {
54 if (!entry->name || strcmp(entry->name, name)) {
58 if (entry->idx != sink_idx) {
62 const bool changed = (new_volume != entry->volume);
63 entry->volume = new_volume;
64 pthread_mutex_unlock(&pulse_mutex);
67 /* index not found, store it */
68 entry = malloc(sizeof(*entry));
69 TAILQ_INSERT_HEAD(&cached_volume, entry, entries);
70 entry->idx = sink_idx;
71 entry->volume = new_volume;
73 entry->name = malloc(strlen(name) + 1);
74 strcpy(entry->name, name);
78 pthread_mutex_unlock(&pulse_mutex);
82 static void store_volume_from_sink_cb(pa_context *c,
83 const pa_sink_info *info,
87 if (pa_context_errno(c) == PA_ERR_NOENTITY)
90 pulseaudio_error_log(c);
97 int avg_vol = pa_cvolume_avg(&info->volume);
98 int vol_perc = roundf((float)avg_vol * 100 / PA_VOLUME_NORM);
99 int composed_volume = COMPOSE_VOLUME_MUTE(vol_perc, info->mute);
101 /* if this is the default sink we must try to save it twice: once with
102 * DEFAULT_SINK_INDEX as the index, and another with its proper value
103 * (using bitwise OR to avoid early-out logic) */
104 if ((info->index == default_sink_idx &&
105 save_volume(DEFAULT_SINK_INDEX, composed_volume, NULL)) |
106 save_volume(info->index, composed_volume, info->name)) {
107 /* if the volume or mute flag changed, wake the main thread */
108 pthread_kill(main_thread, SIGUSR1);
112 static void get_sink_info(pa_context *c, uint32_t idx, const char *name) {
115 if (name || idx == DEFAULT_SINK_INDEX) {
116 o = pa_context_get_sink_info_by_name(
117 c, name ? name : "@DEFAULT_SINK@", store_volume_from_sink_cb, NULL);
119 o = pa_context_get_sink_info_by_index(
120 c, idx, store_volume_from_sink_cb, NULL);
123 pulseaudio_free_operation(c, o);
127 static void store_default_sink_cb(pa_context *c,
128 const pa_sink_info *i,
132 if (default_sink_idx != i->index) {
133 /* default sink changed? */
134 default_sink_idx = i->index;
135 store_volume_from_sink_cb(c, i, eol, userdata);
140 static void update_default_sink(pa_context *c) {
141 pa_operation *o = pa_context_get_sink_info_by_name(
144 store_default_sink_cb,
146 pulseaudio_free_operation(c, o);
149 static void subscribe_cb(pa_context *c, pa_subscription_event_type_t t,
150 uint32_t idx, void *userdata) {
151 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
153 pa_subscription_event_type_t facility =
154 t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
156 case PA_SUBSCRIPTION_EVENT_SERVER:
157 /* server change event, see if the default sink changed */
158 update_default_sink(c);
160 case PA_SUBSCRIPTION_EVENT_SINK:
161 get_sink_info(c, idx, NULL);
168 static void context_state_callback(pa_context *c, void *userdata) {
169 switch (pa_context_get_state(c)) {
170 case PA_CONTEXT_UNCONNECTED:
171 case PA_CONTEXT_CONNECTING:
172 case PA_CONTEXT_AUTHORIZING:
173 case PA_CONTEXT_SETTING_NAME:
174 case PA_CONTEXT_TERMINATED:
176 context_ready = false;
179 case PA_CONTEXT_READY: {
180 pa_context_set_subscribe_callback(c, subscribe_cb, NULL);
181 update_default_sink(c);
183 pa_operation *o = pa_context_subscribe(
185 PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SERVER,
188 if (!pulseaudio_free_operation(c, o))
190 context_ready = true;
193 case PA_CONTEXT_FAILED:
194 /* server disconnected us, attempt to reconnect */
195 context_ready = false;
196 pa_context_unref(context);
203 * returns the current volume in percent, which, as per PulseAudio,
206 int volume_pulseaudio(uint32_t sink_idx, const char *sink_name) {
207 if (!context_ready || default_sink_idx == DEFAULT_SINK_INDEX)
210 pthread_mutex_lock(&pulse_mutex);
211 const indexed_volume_t *entry;
212 TAILQ_FOREACH(entry, &cached_volume, entries) {
214 if (!entry->name || strcmp(entry->name, sink_name)) {
218 if (entry->idx != sink_idx) {
222 int vol = entry->volume;
223 pthread_mutex_unlock(&pulse_mutex);
226 pthread_mutex_unlock(&pulse_mutex);
227 /* first time requires a prime callback call because we only get
228 * updates when the volume actually changes, but we need it to
229 * be correct even if it never changes */
230 pa_threaded_mainloop_lock(main_loop);
231 get_sink_info(context, sink_idx, sink_name);
232 pa_threaded_mainloop_unlock(main_loop);
233 /* show 0 while we don't have this information */
238 * detect and, if necessary, initialize the PulseAudio API
240 bool pulse_initialize(void) {
242 main_loop = pa_threaded_mainloop_new();
247 api = pa_threaded_mainloop_get_api(main_loop);
252 pa_proplist *proplist = pa_proplist_new();
253 pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, APP_NAME);
254 pa_proplist_sets(proplist, PA_PROP_APPLICATION_ID, APP_ID);
255 pa_proplist_sets(proplist, PA_PROP_APPLICATION_VERSION, VERSION);
256 context = pa_context_new_with_proplist(api, APP_NAME, proplist);
257 pa_proplist_free(proplist);
260 pa_context_set_state_callback(context,
261 context_state_callback,
263 if (pa_context_connect(context,
265 PA_CONTEXT_NOFAIL | PA_CONTEXT_NOAUTOSPAWN,
267 pulseaudio_error_log(context);
270 if (!mainloop_thread_running &&
271 pa_threaded_mainloop_start(main_loop) < 0) {
272 pulseaudio_error_log(context);
273 pa_threaded_mainloop_free(main_loop);
277 mainloop_thread_running = true;