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