]> git.sur5r.net Git - i3/i3status/blobdiff - src/pulse.c
fix: use SYSCONFDIR in error message
[i3/i3status] / src / pulse.c
index f9ed5d3c6341328bcd9cdee1f859212ed013c6a4..b733f984cefe037305c85efa9b7869e5d6f94ca7 100644 (file)
@@ -1,4 +1,5 @@
 // vim:ts=4:sw=4:expandtab
+#include <config.h>
 #include <string.h>
 #include <stdio.h>
 #include <math.h>
 #define APP_NAME "i3status"
 #define APP_ID "org.i3wm"
 
-typedef struct indexed_volume_s {
+typedef struct index_info_s {
+    char *name;
     uint32_t idx;
     int volume;
-    TAILQ_ENTRY(indexed_volume_s) entries;
-} indexed_volume_t;
+    char description[MAX_SINK_DESCRIPTION_LEN];
+    TAILQ_ENTRY(index_info_s)
+    entries;
+} index_info_t;
 
 static pa_threaded_mainloop *main_loop = NULL;
 static pa_context *context = NULL;
 static pa_mainloop_api *api = NULL;
 static bool context_ready = false;
+static bool mainloop_thread_running = false;
 static uint32_t default_sink_idx = DEFAULT_SINK_INDEX;
-TAILQ_HEAD(tailhead, indexed_volume_s) cached_volume =
-    TAILQ_HEAD_INITIALIZER(cached_volume);
+TAILQ_HEAD(tailhead, index_info_s)
+cached_info =
+    TAILQ_HEAD_INITIALIZER(cached_info);
 static pthread_mutex_t pulse_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static void pulseaudio_error_log(pa_context *c) {
@@ -41,33 +47,67 @@ static bool pulseaudio_free_operation(pa_context *c, pa_operation *o) {
 }
 
 /*
- * save the volume for the specified sink index
+ * save the info for the specified sink index
  * returning true if the value was changed
  */
-static bool save_volume(uint32_t sink_idx, int new_volume) {
+static bool save_info(uint32_t sink_idx, int new_volume, const char *new_description, const char *name) {
     pthread_mutex_lock(&pulse_mutex);
-    indexed_volume_t *entry;
-    TAILQ_FOREACH(entry, &cached_volume, entries) {
-        if (entry->idx == sink_idx) {
-            const bool changed = (new_volume != entry->volume);
+    index_info_t *entry;
+
+    /* if this is NULL, gracefully handle and replace with empty-string */
+    if (!new_description) {
+        new_description = "";
+        fprintf(stderr, "i3status: PulseAudio: NULL new_description provided\n");
+    }
+
+    TAILQ_FOREACH(entry, &cached_info, entries) {
+        if (name) {
+            if (!entry->name || strcmp(entry->name, name)) {
+                continue;
+            }
+        } else {
+            if (entry->idx != sink_idx) {
+                continue;
+            }
+        }
+
+        bool changed = false;
+
+        if (new_volume != entry->volume) {
             entry->volume = new_volume;
-            pthread_mutex_unlock(&pulse_mutex);
-            return changed;
+            changed = true;
         }
+
+        if (strncmp(entry->description, new_description, sizeof(entry->description))) {
+            strncpy(entry->description, new_description, sizeof(entry->description) - 1);
+            entry->description[sizeof(entry->description) - 1] = '\0';
+            changed = true;
+        }
+
+        pthread_mutex_unlock(&pulse_mutex);
+        return changed;
     }
     /* index not found, store it */
     entry = malloc(sizeof(*entry));
-    TAILQ_INSERT_HEAD(&cached_volume, entry, entries);
+    TAILQ_INSERT_HEAD(&cached_info, entry, entries);
     entry->idx = sink_idx;
     entry->volume = new_volume;
+    strncpy(entry->description, new_description, sizeof(entry->description) - 1);
+    entry->description[sizeof(entry->description) - 1] = '\0';
+    if (name) {
+        entry->name = malloc(strlen(name) + 1);
+        strcpy(entry->name, name);
+    } else {
+        entry->name = NULL;
+    }
     pthread_mutex_unlock(&pulse_mutex);
     return true;
 }
 
-static void store_volume_from_sink_cb(pa_context *c,
-                                      const pa_sink_info *info,
-                                      int eol,
-                                      void *userdata) {
+static void store_info_from_sink_cb(pa_context *c,
+                                    const pa_sink_info *info,
+                                    int eol,
+                                    void *userdata) {
     if (eol < 0) {
         if (pa_context_errno(c) == PA_ERR_NOENTITY)
             return;
@@ -87,20 +127,26 @@ static void store_volume_from_sink_cb(pa_context *c,
      * DEFAULT_SINK_INDEX as the index, and another with its proper value
      * (using bitwise OR to avoid early-out logic) */
     if ((info->index == default_sink_idx &&
-         save_volume(DEFAULT_SINK_INDEX, composed_volume)) |
-        save_volume(info->index, composed_volume)) {
-        /* if the volume or mute flag changed, wake the main thread */
+         save_info(DEFAULT_SINK_INDEX, composed_volume, info->description, NULL)) |
+        save_info(info->index, composed_volume, info->description, info->name)) {
+        /* if the volume, mute flag or description changed, wake the main thread */
         pthread_kill(main_thread, SIGUSR1);
     }
 }
 
-static void get_sink_info(pa_context *c, uint32_t idx) {
-    pa_operation *o =
-        idx == DEFAULT_SINK_INDEX ? pa_context_get_sink_info_by_name(
-                                        c, "@DEFAULT_SINK@", store_volume_from_sink_cb, NULL)
-                                  : pa_context_get_sink_info_by_index(
-                                        c, idx, store_volume_from_sink_cb, NULL);
-    pulseaudio_free_operation(c, o);
+static void get_sink_info(pa_context *c, uint32_t idx, const char *name) {
+    pa_operation *o;
+
+    if (name || idx == DEFAULT_SINK_INDEX) {
+        o = pa_context_get_sink_info_by_name(
+            c, name ? name : "@DEFAULT_SINK@", store_info_from_sink_cb, NULL);
+    } else {
+        o = pa_context_get_sink_info_by_index(
+            c, idx, store_info_from_sink_cb, NULL);
+    }
+    if (o) {
+        pulseaudio_free_operation(c, o);
+    }
 }
 
 static void store_default_sink_cb(pa_context *c,
@@ -111,7 +157,7 @@ static void store_default_sink_cb(pa_context *c,
         if (default_sink_idx != i->index) {
             /* default sink changed? */
             default_sink_idx = i->index;
-            store_volume_from_sink_cb(c, i, eol, userdata);
+            store_info_from_sink_cb(c, i, eol, userdata);
         }
     }
 }
@@ -137,7 +183,7 @@ static void subscribe_cb(pa_context *c, pa_subscription_event_type_t t,
             update_default_sink(c);
             break;
         case PA_SUBSCRIPTION_EVENT_SINK:
-            get_sink_info(c, idx);
+            get_sink_info(c, idx, NULL);
             break;
         default:
             break;
@@ -152,6 +198,7 @@ static void context_state_callback(pa_context *c, void *userdata) {
         case PA_CONTEXT_SETTING_NAME:
         case PA_CONTEXT_TERMINATED:
         default:
+            context_ready = false;
             break;
 
         case PA_CONTEXT_READY: {
@@ -169,7 +216,10 @@ static void context_state_callback(pa_context *c, void *userdata) {
         } break;
 
         case PA_CONTEXT_FAILED:
-            pulseaudio_error_log(c);
+            /* server disconnected us, attempt to reconnect */
+            context_ready = false;
+            pa_context_unref(context);
+            context = NULL;
             break;
     }
 }
@@ -178,30 +228,71 @@ static void context_state_callback(pa_context *c, void *userdata) {
  * returns the current volume in percent, which, as per PulseAudio,
  * may be > 100%
  */
-int volume_pulseaudio(uint32_t sink_idx) {
+int volume_pulseaudio(uint32_t sink_idx, const char *sink_name) {
     if (!context_ready || default_sink_idx == DEFAULT_SINK_INDEX)
         return -1;
 
     pthread_mutex_lock(&pulse_mutex);
-    const indexed_volume_t *entry;
-    TAILQ_FOREACH(entry, &cached_volume, entries) {
-        if (entry->idx == sink_idx) {
-            int vol = entry->volume;
-            pthread_mutex_unlock(&pulse_mutex);
-            return vol;
+    const index_info_t *entry;
+    TAILQ_FOREACH(entry, &cached_info, entries) {
+        if (sink_name) {
+            if (!entry->name || strcmp(entry->name, sink_name)) {
+                continue;
+            }
+        } else {
+            if (entry->idx != sink_idx) {
+                continue;
+            }
         }
+        int vol = entry->volume;
+        pthread_mutex_unlock(&pulse_mutex);
+        return vol;
     }
     pthread_mutex_unlock(&pulse_mutex);
-    /* first time requires a prime callback call because we only get
-     * updates when the volume actually changes, but we need it to
-     * be correct even if it never changes */
+    /* first time requires a prime callback call because we only get updates
+     * when the description or volume actually changes, but we need it to be
+     * correct even if it never changes */
     pa_threaded_mainloop_lock(main_loop);
-    get_sink_info(context, sink_idx);
+    get_sink_info(context, sink_idx, sink_name);
     pa_threaded_mainloop_unlock(main_loop);
     /* show 0 while we don't have this information */
     return 0;
 }
 
+bool description_pulseaudio(uint32_t sink_idx, const char *sink_name, char buffer[MAX_SINK_DESCRIPTION_LEN]) {
+    if (!context_ready || default_sink_idx == DEFAULT_SINK_INDEX) {
+        return false;
+    }
+
+    pthread_mutex_lock(&pulse_mutex);
+    const index_info_t *entry;
+    TAILQ_FOREACH(entry, &cached_info, entries) {
+        if (sink_name) {
+            if (!entry->name || strcmp(entry->name, sink_name)) {
+                continue;
+            }
+        } else {
+            if (entry->idx != sink_idx) {
+                continue;
+            }
+        }
+        strncpy(buffer, entry->description, sizeof(entry->description) - 1);
+        pthread_mutex_unlock(&pulse_mutex);
+        buffer[sizeof(entry->description) - 1] = '\0';
+        return true;
+    }
+    pthread_mutex_unlock(&pulse_mutex);
+    /* first time requires a prime callback call because we only get updates
+     * when the description or volume actually changes, but we need it to be
+     * correct even if it never changes */
+    pa_threaded_mainloop_lock(main_loop);
+    get_sink_info(context, sink_idx, sink_name);
+    pa_threaded_mainloop_unlock(main_loop);
+    /* show empty string while we don't have this information */
+    buffer[0] = '\0';
+    return true;
+}
+
 /*
  *  detect and, if necessary, initialize the PulseAudio API
  */
@@ -220,7 +311,7 @@ bool pulse_initialize(void) {
         pa_proplist *proplist = pa_proplist_new();
         pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, APP_NAME);
         pa_proplist_sets(proplist, PA_PROP_APPLICATION_ID, APP_ID);
-        pa_proplist_sets(proplist, PA_PROP_APPLICATION_VERSION, VERSION);
+        pa_proplist_sets(proplist, PA_PROP_APPLICATION_VERSION, I3STATUS_VERSION);
         context = pa_context_new_with_proplist(api, APP_NAME, proplist);
         pa_proplist_free(proplist);
         if (!context)
@@ -235,12 +326,14 @@ bool pulse_initialize(void) {
             pulseaudio_error_log(context);
             return false;
         }
-        if (pa_threaded_mainloop_start(main_loop) < 0) {
+        if (!mainloop_thread_running &&
+            pa_threaded_mainloop_start(main_loop) < 0) {
             pulseaudio_error_log(context);
             pa_threaded_mainloop_free(main_loop);
             main_loop = NULL;
             return false;
         }
+        mainloop_thread_running = true;
     }
     return true;
 }