From 44fad4d2cd707bbb1151925ff1223161f4ef1fc0 Mon Sep 17 00:00:00 2001 From: Felix Buehler Date: Tue, 7 Apr 2020 23:02:30 +0200 Subject: use format_placeholder for volume --- src/print_volume.c | 51 +++++++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/src/print_volume.c b/src/print_volume.c index 89955a8..778be0f 100644 --- a/src/print_volume.c +++ b/src/print_volume.c @@ -31,6 +31,8 @@ #include "i3status.h" #include "queue.h" +#define STRING_SIZE 10 + #define ALSA_VOLUME(channel) \ err = snd_mixer_selem_get_##channel##_dB_range(elem, &min, &max) || \ snd_mixer_selem_get_##channel##_dB(elem, 0, &val); \ @@ -48,30 +50,20 @@ fmt = fmt_muted; \ } -static char *apply_volume_format(const char *fmt, char *outwalk, int ivolume, const char *devicename) { - const char *walk = fmt; - - for (; *walk != '\0'; walk++) { - if (*walk != '%') { - *(outwalk++) = *walk; +static char *apply_volume_format(const char *fmt, char *buffer, int ivolume, const char *devicename) { + char string_volume[STRING_SIZE]; - } else if (BEGINS_WITH(walk + 1, "%")) { - outwalk += sprintf(outwalk, "%s", pct_mark); - walk += strlen("%"); + snprintf(string_volume, STRING_SIZE, "%d%s", ivolume, pct_mark); - } else if (BEGINS_WITH(walk + 1, "volume")) { - outwalk += sprintf(outwalk, "%d%s", ivolume, pct_mark); - walk += strlen("volume"); + placeholder_t placeholders[] = { + {.name = "%%", .value = pct_mark}, + {.name = "%volume", .value = string_volume}, + {.name = "%devicename", .value = devicename}}; - } else if (BEGINS_WITH(walk + 1, "devicename")) { - outwalk += sprintf(outwalk, "%s", devicename); - walk += strlen("devicename"); + const size_t num = sizeof(placeholders) / sizeof(placeholder_t); + buffer = format_placeholders(fmt, &placeholders[0], num); - } else { - *(outwalk++) = '%'; - } - } - return outwalk; + return buffer; } 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,10 +111,10 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * /* negative result means error, stick to 0 */ if (ivolume < 0) ivolume = 0; - outwalk = apply_volume_format(muted ? fmt_muted : fmt, - outwalk, - ivolume, - description); + buffer = apply_volume_format(muted ? fmt_muted : fmt, + buffer, + ivolume, + description); goto out; } else if (!strcasecmp(device, "default") && pulse_initialize()) { /* no device specified or "default" set */ @@ -136,10 +128,10 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * START_COLOR("color_degraded"); pbval = 0; } - outwalk = apply_volume_format(muted ? fmt_muted : fmt, - outwalk, - ivolume, - description); + buffer = apply_volume_format(muted ? fmt_muted : fmt, + buffer, + ivolume, + description); goto out; } /* negative result or NULL description means error, fail PulseAudio attempt */ @@ -356,12 +348,11 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * } #endif - outwalk = apply_volume_format(fmt, outwalk, vol & 0x7f, devicename); + buffer = apply_volume_format(fmt, buffer, vol & 0x7f, devicename); close(mixfd); #endif out: - *outwalk = '\0'; if (!pbval) END_COLOR; OUTPUT_FULL_TEXT(buffer); -- cgit v1.2.3 From 269c06c4bb49a22ebe132029ea7fd9f11722be16 Mon Sep 17 00:00:00 2001 From: Felix Buehler Date: Sun, 26 Apr 2020 21:43:08 +0200 Subject: [volume] fix memory leak mend --- src/print_volume.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/print_volume.c b/src/print_volume.c index 778be0f..0331ed5 100644 --- a/src/print_volume.c +++ b/src/print_volume.c @@ -115,7 +115,7 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * buffer, ivolume, description); - goto out; + goto out_with_format; } else if (!strcasecmp(device, "default") && pulse_initialize()) { /* no device specified or "default" set */ char description[MAX_SINK_DESCRIPTION_LEN]; @@ -132,7 +132,7 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * buffer, ivolume, description); - goto out; + goto out_with_format; } /* negative result or NULL description means error, fail PulseAudio attempt */ } @@ -234,10 +234,11 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * 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_selem_id_free(sid); + goto out_with_format; #endif #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) @@ -350,10 +351,18 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char * #endif buffer = apply_volume_format(fmt, buffer, vol & 0x7f, devicename); close(mixfd); + goto out_with_format; #endif out: if (!pbval) END_COLOR; OUTPUT_FULL_TEXT(buffer); + return; + +out_with_format: + if (!pbval) + END_COLOR; + OUTPUT_FULL_TEXT(buffer); + free(buffer); } -- cgit v1.2.3