X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fprint_volume.c;h=d3a97084554d55c47d391e35f66b44fd591e7828;hb=f0cd726bf0dabb20b81b456e93889638a5ed3413;hp=bc14ed878cca21704664a61beb9a943a1fa6c9a9;hpb=1b3aa404858b476015a2b29d0c42f7dbbed915bc;p=i3%2Fi3status diff --git a/src/print_volume.c b/src/print_volume.c index bc14ed8..d3a9708 100644 --- a/src/print_volume.c +++ b/src/print_volume.c @@ -11,12 +11,18 @@ #include #endif -#ifdef __FreeBSD__ +#if defined(__FreeBSD__) || defined(__DragonFly__) #include #include #include #endif +#ifdef __OpenBSD__ +#include +#include +#include +#endif + #include "i3status.h" #include "queue.h" @@ -45,6 +51,7 @@ static void free_hdl(struct mixer_hdl *hdl) { 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) { @@ -70,64 +77,64 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * if (!found) { if ((hdl = calloc(sizeof(struct mixer_hdl), 1)) == NULL) - return; + goto out; if ((hdl->device = strdup(device)) == NULL) { free(hdl); - return; + goto out; } if ((hdl->mixer = strdup(mixer)) == NULL) { free(hdl->device); free(hdl); - return; + goto out; } hdl->mixer_idx = mixer_idx; snd_mixer_selem_id_malloc(&(hdl->sid)); if (hdl->sid == NULL) { free_hdl(hdl); - return; + goto out; } 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; + goto out; } /* 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; + goto out; } /* 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; + goto out; } 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; + goto out; } /* Find the given mixer */ 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); - return; + goto out; } /* Get the volume range to convert the volume later */ @@ -147,11 +154,12 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * /* 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; @@ -166,17 +174,33 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * } } #endif -#ifdef __FreeBSD__ - char mixerpath[] = "/dev/mixer"; +#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) + char *mixerpath; + char defaultmixer[] = "/dev/mixer"; int mixfd, vol, devmask = 0; + pbval = 1; + + if (mixer_idx > 0) + asprintf(&mixerpath, "/dev/mixer%d", mixer_idx); + else + mixerpath = defaultmixer; if ((mixfd = open(mixerpath, O_RDWR)) < 0) return; + + if (mixer_idx > 0) + free(mixerpath); + if (ioctl(mixfd, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) return; if (ioctl(mixfd, MIXER_READ(0),&vol) == -1) return; + if (((vol & 0x7f) == 0) && (((vol >> 8) & 0x7f) == 0)) { + START_COLOR("color_degraded"); + pbval = 0; + } + const char *walk = fmt; for (; *walk != '\0'; walk++) { if (*walk != '%') { @@ -191,6 +215,10 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * close(mixfd); #endif +out: *outwalk = '\0'; OUTPUT_FULL_TEXT(buffer); + + if (!pbval) + END_COLOR; }