X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fprint_volume.c;h=aa739a1fc2c09899b7457ffe38471d64993b8115;hb=696ddf461fd4fd665bdf46bf1dd26a3210236cff;hp=86b3505713303d304d31bff1676c8cd6c20c0c1b;hpb=15f2279592032af1507de09ef31eba81aee53191;p=i3%2Fi3status diff --git a/src/print_volume.c b/src/print_volume.c index 86b3505..aa739a1 100644 --- a/src/print_volume.c +++ b/src/print_volume.c @@ -4,12 +4,14 @@ #include #include #include +#include #include #include #ifdef LINUX #include #include +#include #endif #if defined(__FreeBSD__) || defined(__DragonFly__) @@ -28,21 +30,40 @@ #include "i3status.h" #include "queue.h" +#define ALSA_VOLUME(channel) \ + err = snd_mixer_selem_get_##channel##_dB_range(elem, &min, &max) || \ + snd_mixer_selem_get_##channel##_dB(elem, 0, &val); \ + if (err != 0 || min >= max) { \ + err = snd_mixer_selem_get_##channel##_volume_range(elem, &min, &max) || \ + snd_mixer_selem_get_##channel##_volume(elem, 0, &val); \ + force_linear = true; \ + } + +#define ALSA_MUTE_SWITCH(channel) \ + if ((err = snd_mixer_selem_get_##channel##_switch(elem, 0, &pbval)) < 0) \ + fprintf(stderr, "i3status: ALSA: " #channel "_switch: %s\n", snd_strerror(err)); \ + if (!pbval) { \ + START_COLOR("color_degraded"); \ + fmt = fmt_muted; \ + } + static char *apply_volume_format(const char *fmt, char *outwalk, int ivolume) { const char *walk = fmt; for (; *walk != '\0'; walk++) { if (*walk != '%') { *(outwalk++) = *walk; - continue; - } - if (BEGINS_WITH(walk + 1, "%")) { - outwalk += sprintf(outwalk, "%%"); + + } else if (BEGINS_WITH(walk + 1, "%")) { + outwalk += sprintf(outwalk, "%s", pct_mark); walk += strlen("%"); - } - if (BEGINS_WITH(walk + 1, "volume")) { - outwalk += sprintf(outwalk, "%d%%", ivolume); + + } else if (BEGINS_WITH(walk + 1, "volume")) { + outwalk += sprintf(outwalk, "%d%s", ivolume, pct_mark); walk += strlen("volume"); + + } else { + *(outwalk++) = '%'; } } return outwalk; @@ -60,16 +81,19 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * free(instance); } -#ifndef __OpenBSD__ +#if !defined(__DragonFly__) && !defined(__OpenBSD__) /* Try PulseAudio first */ /* If the device name has the format "pulse[:N]" where N is the * 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 +109,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) { @@ -105,11 +129,13 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * #endif #ifdef LINUX + const long MAX_LINEAR_DB_SCALE = 24; int err; snd_mixer_t *m; snd_mixer_selem_id_t *sid; snd_mixer_elem_t *elem; long min, max, val; + bool force_linear = false; int avg; if ((err = snd_mixer_open(&m, 0)) < 0) { @@ -147,7 +173,7 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * snd_mixer_selem_id_set_index(sid, mixer_idx); snd_mixer_selem_id_set_name(sid, mixer); if (!(elem = snd_mixer_find_selem(m, sid))) { - fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %i)\n", + fprintf(stderr, "i3status: ALSA: Cannot find mixer %s (index %u)\n", snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid)); snd_mixer_close(m); snd_mixer_selem_id_free(sid); @@ -155,25 +181,38 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * } /* Get the volume range to convert the volume later */ - snd_mixer_selem_get_playback_volume_range(elem, &min, &max); - snd_mixer_handle_events(m); - snd_mixer_selem_get_playback_volume(elem, 0, &val); - if (max != 100) { - float avgf = ((float)val / max) * 100; + if (!strncasecmp(mixer, "capture", strlen("capture"))) { + ALSA_VOLUME(capture) + } else { + ALSA_VOLUME(playback) + } + + if (err != 0) { + fprintf(stderr, "i3status: ALSA: Cannot get playback volume.\n"); + goto out; + } + + /* Use linear mapping for raw register values or small ranges of 24 dB */ + if (force_linear || max - min <= MAX_LINEAR_DB_SCALE * 100) { + float avgf = ((float)(val - min) / (max - min)) * 100; avg = (int)avgf; avg = (avgf - avg < 0.5 ? avg : (avg + 1)); - } else - avg = (int)val; + } else { + /* mapped volume to be more natural for the human ear */ + double normalized = exp10((val - max) / 6000.0); + if (min != SND_CTL_TLV_DB_GAIN_MUTE) { + double min_norm = exp10((min - max) / 6000.0); + normalized = (normalized - min_norm) / (1 - min_norm); + } + avg = lround(normalized * 100); + } /* Check for mute */ if (snd_mixer_selem_has_playback_switch(elem)) { - if ((err = snd_mixer_selem_get_playback_switch(elem, 0, &pbval)) < 0) - fprintf(stderr, "i3status: ALSA: playback_switch: %s\n", snd_strerror(err)); - if (!pbval) { - START_COLOR("color_degraded"); - fmt = fmt_muted; - } + ALSA_MUTE_SWITCH(playback) + } else if (snd_mixer_selem_has_capture_switch(elem)) { + ALSA_MUTE_SWITCH(capture) } snd_mixer_close(m); @@ -207,6 +246,7 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * #if defined(__OpenBSD__) int oclass_idx = -1, master_idx = -1, master_mute_idx = -1; + int master_next = AUDIO_MIXER_LAST; mixer_devinfo_t devinfo, devinfo2; mixer_ctrl_t vinfo; @@ -224,12 +264,17 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * devinfo2.index = 0; while (ioctl(mixfd, AUDIO_MIXER_DEVINFO, &devinfo2) >= 0) { - if ((devinfo2.type == AUDIO_MIXER_VALUE) && (devinfo2.mixer_class == oclass_idx) && (strncmp(devinfo2.label.name, AudioNmaster, MAX_AUDIO_DEV_LEN) == 0)) + if ((devinfo2.type == AUDIO_MIXER_VALUE) && (devinfo2.mixer_class == oclass_idx) && (strncmp(devinfo2.label.name, AudioNmaster, MAX_AUDIO_DEV_LEN) == 0)) { master_idx = devinfo2.index; + master_next = devinfo2.next; + } if ((devinfo2.type == AUDIO_MIXER_ENUM) && (devinfo2.mixer_class == oclass_idx) && (strncmp(devinfo2.label.name, AudioNmute, MAX_AUDIO_DEV_LEN) == 0)) - master_mute_idx = devinfo2.index; + if (master_next == devinfo2.index) + master_mute_idx = devinfo2.index; + if (master_next != AUDIO_MIXER_LAST) + master_next = devinfo2.next; devinfo2.index++; } @@ -242,6 +287,7 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * vinfo.dev = master_idx; vinfo.type = AUDIO_MIXER_VALUE; + vinfo.un.value.num_channels = devinfo.un.v.num_channels; if (ioctl(mixfd, AUDIO_MIXER_READ, &vinfo) == -1) goto out;