]> git.sur5r.net Git - i3/i3status/blobdiff - src/print_volume.c
Properly output JSON with libyajl
[i3/i3status] / src / print_volume.c
index c13cb1761f4a206e9d2c999cef6c888c9563e153..5c34c3e6d16e0e5ef72b5bc7c5e91ec9381550a3 100644 (file)
@@ -41,8 +41,16 @@ static void free_hdl(struct mixer_hdl *hdl) {
 }
 #endif
 
-void print_volume(const char *fmt, const char *device, const char *mixer, int mixer_idx) {
-/* Printing volume only works with ALSA at the moment */
+void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *device, const char *mixer, int mixer_idx) {
+        char *outwalk = buffer;
+
+        /* Printing volume only works with ALSA at the moment */
+        if (output_format == O_I3BAR) {
+                char *instance;
+                asprintf(&instance, "%s.%s.%d", device, mixer, mixer_idx);
+                INSTANCE(instance);
+                free(instance);
+        }
 #ifdef LINUX
        /* Check if we already opened the mixer and get the handle
         * from cache if so */
@@ -147,11 +155,11 @@ void print_volume(const char *fmt, const char *device, const char *mixer, int mi
        const char *walk = fmt;
        for (; *walk != '\0'; walk++) {
                if (*walk != '%') {
-                       putchar(*walk);
+                        *(outwalk++) = *walk;
                        continue;
                }
                if (BEGINS_WITH(walk+1, "volume")) {
-                       printf("%d%%", avg);
+                       outwalk += sprintf(outwalk, "%d%%", avg);
                        walk += strlen("volume");
                }
        }
@@ -170,14 +178,17 @@ void print_volume(const char *fmt, const char *device, const char *mixer, int mi
         const char *walk = fmt;
         for (; *walk != '\0'; walk++) {
                 if (*walk != '%') {
-                        putchar(*walk);
+                        *(outwalk++) = *walk;
                         continue;
                 }
                 if (BEGINS_WITH(walk+1, "volume")) {
-                        printf("%d%%", vol & 0x7f);
+                        outwalk += sprintf(outwalk, "%d%%", vol & 0x7f);
                         walk += strlen("volume");
                 }
         }
         close(mixfd);
 #endif
+
+        *outwalk = '\0';
+        OUTPUT_FULL_TEXT(buffer);
 }