Merge pull request #409 from Stunkymonkey/format_placeholder-volume

Format placeholder volume
This commit is contained in:
Ingo Bürk 2020-05-01 09:42:49 +02:00 committed by GitHub
commit 675c423647
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,6 +31,8 @@
#include "i3status.h" #include "i3status.h"
#include "queue.h" #include "queue.h"
#define STRING_SIZE 10
#define ALSA_VOLUME(channel) \ #define ALSA_VOLUME(channel) \
err = snd_mixer_selem_get_##channel##_dB_range(elem, &min, &max) || \ err = snd_mixer_selem_get_##channel##_dB_range(elem, &min, &max) || \
snd_mixer_selem_get_##channel##_dB(elem, 0, &val); \ snd_mixer_selem_get_##channel##_dB(elem, 0, &val); \
@ -48,30 +50,20 @@
fmt = fmt_muted; \ fmt = fmt_muted; \
} }
static char *apply_volume_format(const char *fmt, char *outwalk, int ivolume, const char *devicename) { static char *apply_volume_format(const char *fmt, char *buffer, int ivolume, const char *devicename) {
const char *walk = fmt; char string_volume[STRING_SIZE];
for (; *walk != '\0'; walk++) { snprintf(string_volume, STRING_SIZE, "%d%s", ivolume, pct_mark);
if (*walk != '%') {
*(outwalk++) = *walk;
} else if (BEGINS_WITH(walk + 1, "%")) { placeholder_t placeholders[] = {
outwalk += sprintf(outwalk, "%s", pct_mark); {.name = "%%", .value = pct_mark},
walk += strlen("%"); {.name = "%volume", .value = string_volume},
{.name = "%devicename", .value = devicename}};
} else if (BEGINS_WITH(walk + 1, "volume")) { const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
outwalk += sprintf(outwalk, "%d%s", ivolume, pct_mark); buffer = format_placeholders(fmt, &placeholders[0], num);
walk += strlen("volume");
} else if (BEGINS_WITH(walk + 1, "devicename")) { return buffer;
outwalk += sprintf(outwalk, "%s", devicename);
walk += strlen("devicename");
} else {
*(outwalk++) = '%';
}
}
return outwalk;
} }
void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *fmt_muted, const char *device, const char *mixer, int mixer_idx) { void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *fmt_muted, const char *device, const char *mixer, int mixer_idx) {
@ -119,11 +111,11 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *
/* negative result means error, stick to 0 */ /* negative result means error, stick to 0 */
if (ivolume < 0) if (ivolume < 0)
ivolume = 0; ivolume = 0;
outwalk = apply_volume_format(muted ? fmt_muted : fmt, buffer = apply_volume_format(muted ? fmt_muted : fmt,
outwalk, buffer,
ivolume, ivolume,
description); description);
goto out; goto out_with_format;
} else if (!strcasecmp(device, "default") && pulse_initialize()) { } else if (!strcasecmp(device, "default") && pulse_initialize()) {
/* no device specified or "default" set */ /* no device specified or "default" set */
char description[MAX_SINK_DESCRIPTION_LEN]; char description[MAX_SINK_DESCRIPTION_LEN];
@ -136,11 +128,11 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *
START_COLOR("color_degraded"); START_COLOR("color_degraded");
pbval = 0; pbval = 0;
} }
outwalk = apply_volume_format(muted ? fmt_muted : fmt, buffer = apply_volume_format(muted ? fmt_muted : fmt,
outwalk, buffer,
ivolume, ivolume,
description); description);
goto out; goto out_with_format;
} }
/* negative result or NULL description means error, fail PulseAudio attempt */ /* negative result or NULL description means error, fail PulseAudio attempt */
} }
@ -242,10 +234,11 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *
ALSA_MUTE_SWITCH(capture) ALSA_MUTE_SWITCH(capture)
} }
outwalk = apply_volume_format(fmt, outwalk, avg, mixer_name); buffer = apply_volume_format(fmt, buffer, avg, mixer_name);
snd_mixer_close(m); snd_mixer_close(m);
snd_mixer_selem_id_free(sid); snd_mixer_selem_id_free(sid);
goto out_with_format;
#endif #endif
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
@ -356,13 +349,20 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *
} }
#endif #endif
outwalk = apply_volume_format(fmt, outwalk, vol & 0x7f, devicename); buffer = apply_volume_format(fmt, buffer, vol & 0x7f, devicename);
close(mixfd); close(mixfd);
goto out_with_format;
#endif #endif
out: out:
*outwalk = '\0';
if (!pbval) if (!pbval)
END_COLOR; END_COLOR;
OUTPUT_FULL_TEXT(buffer); OUTPUT_FULL_TEXT(buffer);
return;
out_with_format:
if (!pbval)
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
free(buffer);
} }