summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Stapelberg <stapelberg@users.noreply.github.com>2015-03-29 22:56:50 +0200
committerMichael Stapelberg <stapelberg@users.noreply.github.com>2015-03-29 22:56:50 +0200
commit40d29483c0f7f07645c124fdd76be100c11db4e5 (patch)
tree51a4fd917b7e8053350d84272bd738e5e7c3833a /src
parentbe583ea739fd2412138c1d73a01ca4a5ca4e9252 (diff)
parentb12e11237b1b861648568fcc569bde67b4e15396 (diff)
Merge pull request #15 from Watcom/refactor
refactoring to avoid code duplication
Diffstat (limited to 'src')
-rw-r--r--src/print_volume.c53
1 files changed, 23 insertions, 30 deletions
diff --git a/src/print_volume.c b/src/print_volume.c
index bc469f3..d8766b7 100644
--- a/src/print_volume.c
+++ b/src/print_volume.c
@@ -27,6 +27,26 @@
#include "i3status.h"
#include "queue.h"
+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, "%%");
+ walk += strlen("%");
+ }
+ if (BEGINS_WITH(walk + 1, "volume")) {
+ outwalk += sprintf(outwalk, "%d%%", ivolume);
+ walk += strlen("volume");
+ }
+ }
+ 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) {
char *outwalk = buffer;
int pbval = 1;
@@ -113,21 +133,8 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *
snd_mixer_close(m);
snd_mixer_selem_id_free(sid);
- const char *walk = fmt;
- for (; *walk != '\0'; walk++) {
- if (*walk != '%') {
- *(outwalk++) = *walk;
- continue;
- }
- if (BEGINS_WITH(walk + 1, "%")) {
- outwalk += sprintf(outwalk, "%%");
- walk += strlen("%");
- }
- if (BEGINS_WITH(walk + 1, "volume")) {
- outwalk += sprintf(outwalk, "%d%%", avg);
- walk += strlen("volume");
- }
- }
+ outwalk = apply_volume_format(fmt, outwalk, avg);
+
#endif
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
char *mixerpath;
@@ -162,21 +169,7 @@ void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *
pbval = 0;
}
- const char *walk = fmt;
- for (; *walk != '\0'; walk++) {
- if (*walk != '%') {
- *(outwalk++) = *walk;
- continue;
- }
- if (BEGINS_WITH(walk + 1, "%")) {
- outwalk += sprintf(outwalk, "%%");
- walk += strlen("%");
- }
- if (BEGINS_WITH(walk + 1, "volume")) {
- outwalk += sprintf(outwalk, "%d%%", vol & 0x7f);
- walk += strlen("volume");
- }
- }
+ outwalk = apply_volume_format(fmt, outwalk, vol & 0x7f);
close(mixfd);
#endif