From 56985c505d4cbbf62825eafb16416f8d3c9ca6d1 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Sun, 18 Dec 2011 16:54:03 +0100 Subject: interface: Use printf precision and padding for volume bar printing --- src/interface.c | 39 +++++++++++++++++++++++++-------------- src/pa-sink-ctl.h | 2 ++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/interface.c b/src/interface.c index fa0eee4..c16f83f 100644 --- a/src/interface.c +++ b/src/interface.c @@ -111,24 +111,31 @@ interface_resize(gpointer data) } static void -print_volume(struct context *ctx, struct vol_ctl *ctl) +allocate_volume_bar(struct context *ctx) { - gint x, y, max_x, max_y, volume_bar_len, vol; + gint x, y, max_x, max_y; + + if (ctx->volume_bar) + return; getyx(ctx->menu_win, y, x); getmaxyx(ctx->menu_win, max_y, max_x); - /* mute button + brackets + space */ - volume_bar_len = max_x - x - 8; - vol = (gint) (volume_bar_len * ctl->vol / PA_VOLUME_NORM); - - wprintw(ctx->menu_win, " [%c]", ctl->mute ? 'M' : ' '); - - wprintw(ctx->menu_win, "["); - for (gint i = 0; i < vol; ++i) - wprintw(ctx->menu_win, "="); - for (gint i = vol; i < volume_bar_len; ++i) - wprintw(ctx->menu_win, " "); - wprintw(ctx->menu_win, "]"); + ctx->volume_bar_len = max_x - x - 8; + ctx->volume_bar = g_new(char, ctx->volume_bar_len+1); + /* FIXME: if (ctx->volume_bar == NULL) */ + memset(ctx->volume_bar, '=', ctx->volume_bar_len); + ctx->volume_bar[ctx->volume_bar_len] = '\0'; +} + +static void +print_volume(struct context *ctx, struct vol_ctl *ctl) +{ + gint vol; + + allocate_volume_bar(ctx); + vol = (gint) (ctx->volume_bar_len * ctl->vol / PA_VOLUME_NORM); + wprintw(ctx->menu_win, " [%c][%-*.*s]", + ctl->mute ? 'M':' ', ctx->volume_bar_len, vol, ctx->volume_bar); } static void @@ -181,6 +188,10 @@ interface_redraw(struct context *ctx) wmove(ctx->menu_win, 2, 2); /* set initial cursor offset */ ctx->max_name_len = 0; + if (ctx->volume_bar) { + g_free(ctx->volume_bar); + ctx->volume_bar = NULL; + } g_list_foreach(ctx->sink_list, max_name_len_helper, ctx); g_list_foreach(ctx->sink_list, print_vol_ctl, ctx); diff --git a/src/pa-sink-ctl.h b/src/pa-sink-ctl.h index 97801d4..bc93ccb 100644 --- a/src/pa-sink-ctl.h +++ b/src/pa-sink-ctl.h @@ -33,6 +33,8 @@ struct context { WINDOW *menu_win; WINDOW *msg_win; + gint volume_bar_len; + gchar *volume_bar; guint resize_source_id; #ifdef HAVE_SIGNALFD -- cgit