]> git.sur5r.net Git - i3/i3status/blobdiff - src/print_volume.c
Add battery capacity parsing (#216)
[i3/i3status] / src / print_volume.c
index 86b3505713303d304d31bff1676c8cd6c20c0c1b..51e84f31364a8730e6fb9d6ee5394f174c2e943c 100644 (file)
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <err.h>
+#include <ctype.h>
 #include <yajl/yajl_gen.h>
 #include <yajl/yajl_version.h>
 
@@ -37,11 +38,11 @@ static char *apply_volume_format(const char *fmt, char *outwalk, int ivolume) {
             continue;
         }
         if (BEGINS_WITH(walk + 1, "%")) {
-            outwalk += sprintf(outwalk, "%%");
+            outwalk += sprintf(outwalk, "%s", pct_mark);
             walk += strlen("%");
         }
         if (BEGINS_WITH(walk + 1, "volume")) {
-            outwalk += sprintf(outwalk, "%d%%", ivolume);
+            outwalk += sprintf(outwalk, "%d%s", ivolume, pct_mark);
             walk += strlen("volume");
         }
     }
@@ -67,9 +68,12 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *
      * index of the PulseAudio sink then force PulseAudio, optionally
      * overriding the default sink */
     if (!strncasecmp(device, "pulse", strlen("pulse"))) {
-        uint32_t sink_idx = device[5] == ':' ? (uint32_t)atoi(device + 6)
-                                             : DEFAULT_SINK_INDEX;
-        int cvolume = pulse_initialize() ? volume_pulseaudio(sink_idx) : 0;
+        uint32_t sink_idx = device[strlen("pulse")] == ':' ? (uint32_t)atoi(device + strlen("pulse:")) : DEFAULT_SINK_INDEX;
+        const char *sink_name = device[strlen("pulse")] == ':' &&
+                                        !isdigit(device[strlen("pulse:")])
+                                    ? device + strlen("pulse:")
+                                    : NULL;
+        int cvolume = pulse_initialize() ? volume_pulseaudio(sink_idx, sink_name) : 0;
         int ivolume = DECOMPOSE_VOLUME(cvolume);
         bool muted = DECOMPOSE_MUTED(cvolume);
         if (muted) {
@@ -85,7 +89,7 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *
         goto out;
     } else if (!strcasecmp(device, "default") && pulse_initialize()) {
         /* no device specified or "default" set */
-        int cvolume = volume_pulseaudio(DEFAULT_SINK_INDEX);
+        int cvolume = volume_pulseaudio(DEFAULT_SINK_INDEX, NULL);
         int ivolume = DECOMPOSE_VOLUME(cvolume);
         bool muted = DECOMPOSE_MUTED(cvolume);
         if (ivolume >= 0) {