]> git.sur5r.net Git - i3/i3status/blobdiff - src/print_volume.c
changing volume color from bad to degraded if muted
[i3/i3status] / src / print_volume.c
index 2945b1ec060314f8cc257d91a28219fafd9b5f40..cf42f3e98aed404fc5f7a8bf0fbed1d2dbfa9e9f 100644 (file)
@@ -3,12 +3,26 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <yajl/yajl_gen.h>
+#include <yajl/yajl_version.h>
 
 #ifdef LINUX
 #include <alsa/asoundlib.h>
 #include <alloca.h>
 #endif
 
+#if defined(__FreeBSD__) || defined(__DragonFly__)
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/soundcard.h>
+#endif
+
+#ifdef __OpenBSD__
+#include <fcntl.h>
+#include <unistd.h>
+#include <soundcard.h>
+#endif
+
 #include "i3status.h"
 #include "queue.h"
 
@@ -35,8 +49,17 @@ 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;
+       int pbval = 1;
+
+        /* 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 */
@@ -75,14 +98,14 @@ void print_volume(const char *fmt, const char *device, const char *mixer, int mi
                }
 
                if ((err = snd_mixer_open(&(hdl->m), 0)) < 0) {
-                       fprintf(stderr, "ALSA: Cannot open mixer: %s\n", snd_strerror(err));
+                       fprintf(stderr, "i3status: ALSA: Cannot open mixer: %s\n", snd_strerror(err));
                        free_hdl(hdl);
                        return;
                }
 
                /* Attach this mixer handle to the given device */
                if ((err = snd_mixer_attach(hdl->m, device)) < 0) {
-                       fprintf(stderr, "ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
+                       fprintf(stderr, "i3status: ALSA: Cannot attach mixer to device: %s\n", snd_strerror(err));
                        snd_mixer_close(hdl->m);
                        free_hdl(hdl);
                        return;
@@ -90,14 +113,14 @@ void print_volume(const char *fmt, const char *device, const char *mixer, int mi
 
                /* Register this mixer */
                if ((err = snd_mixer_selem_register(hdl->m, NULL, NULL)) < 0) {
-                       fprintf(stderr, "ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
+                       fprintf(stderr, "i3status: ALSA: snd_mixer_selem_register: %s\n", snd_strerror(err));
                        snd_mixer_close(hdl->m);
                        free_hdl(hdl);
                        return;
                }
 
                if ((err = snd_mixer_load(hdl->m)) < 0) {
-                       fprintf(stderr, "ALSA: snd_mixer_load: %s\n", snd_strerror(err));
+                       fprintf(stderr, "i3status: ALSA: snd_mixer_load: %s\n", snd_strerror(err));
                        snd_mixer_close(hdl->m);
                        free_hdl(hdl);
                        return;
@@ -107,7 +130,7 @@ void print_volume(const char *fmt, const char *device, const char *mixer, int mi
                snd_mixer_selem_id_set_index(hdl->sid, mixer_idx);
                snd_mixer_selem_id_set_name(hdl->sid, mixer);
                if (!(hdl->elem = snd_mixer_find_selem(hdl->m, hdl->sid))) {
-                       fprintf(stderr, "ALSA: Cannot find mixer %s (index %i)\n",
+                       fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %i)\n",
                                snd_mixer_selem_id_get_name(hdl->sid), snd_mixer_selem_id_get_index(hdl->sid));
                        snd_mixer_close(hdl->m);
                        free_hdl(hdl);
@@ -131,23 +154,54 @@ void print_volume(const char *fmt, const char *device, const char *mixer, int mi
 
        /* Check for mute */
        if (snd_mixer_selem_has_playback_switch(hdl->elem)) {
-               int pbval;
                if ((err = snd_mixer_selem_get_playback_switch(hdl->elem, 0, &pbval)) < 0)
-                       fprintf (stderr, "ALSA: playback_switch: %s\n", snd_strerror(err));
-               if (!pbval)
+                       fprintf (stderr, "i3status: ALSA: playback_switch: %s\n", snd_strerror(err));
+               if (!pbval)  {
+                       START_COLOR("color_degraded");
                        avg = 0;
+               }
        }
 
        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");
                }
        }
 #endif
+#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
+        char mixerpath[] = "/dev/mixer";
+        int mixfd, vol, devmask = 0;
+
+        if ((mixfd = open(mixerpath, O_RDWR)) < 0)
+                return;
+        if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
+                return;
+        if (ioctl(mixfd, MIXER_READ(0),&vol) == -1)
+                return;
+
+        const char *walk = fmt;
+        for (; *walk != '\0'; walk++) {
+                if (*walk != '%') {
+                        *(outwalk++) = *walk;
+                        continue;
+                }
+                if (BEGINS_WITH(walk+1, "volume")) {
+                        outwalk += sprintf(outwalk, "%d%%", vol & 0x7f);
+                        walk += strlen("volume");
+                }
+        }
+        close(mixfd);
+#endif
+
+        *outwalk = '\0';
+        OUTPUT_FULL_TEXT(buffer);
+
+        if (!pbval)
+               END_COLOR;
 }