1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2025-01-16 04:02:15 +00:00

hook in the scale value for bar and gauge objects

This patch already implements complete auto-scaling for bars and gauges,
therefore introducing a flags field in order to signal whether
auto-scaling is enabled (and the scale field contains just the max value
seen so far).
This commit is contained in:
Phil Sutter 2009-12-02 00:21:38 +01:00
parent dae785e5d0
commit 82ef68aafa
7 changed files with 72 additions and 39 deletions

View File

@ -1289,12 +1289,14 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied)
case BAR: case BAR:
{ {
int h, bar_usage, by; int h, bar_usage, by;
float scale;
if (cur_x - text_start_x > maximum_width if (cur_x - text_start_x > maximum_width
&& maximum_width > 0) { && maximum_width > 0) {
break; break;
} }
h = specials[special_index].height; h = specials[special_index].height;
bar_usage = specials[special_index].arg; bar_usage = specials[special_index].arg;
scale = specials[special_index].graph_scale;
by = cur_y - (font_ascent() / 2) - 1; by = cur_y - (font_ascent() / 2) - 1;
if (h < font_h) { if (h < font_h) {
@ -1314,7 +1316,7 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied)
XDrawRectangle(display, window.drawable, window.gc, cur_x, XDrawRectangle(display, window.drawable, window.gc, cur_x,
by, w, h); by, w, h);
XFillRectangle(display, window.drawable, window.gc, cur_x, XFillRectangle(display, window.drawable, window.gc, cur_x,
by, w * bar_usage / 255, h); by, w * bar_usage / scale, h);
if (h > cur_y_add if (h > cur_y_add
&& h > font_h) { && h > font_h) {
cur_y_add = h; cur_y_add = h;
@ -1329,6 +1331,7 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied)
#ifdef MATH #ifdef MATH
float angle, px, py; float angle, px, py;
int usage; int usage;
float scale;
#endif /* MATH */ #endif /* MATH */
if (cur_x - text_start_x > maximum_width if (cur_x - text_start_x > maximum_width
@ -1358,7 +1361,8 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied)
#ifdef MATH #ifdef MATH
usage = specials[special_index].arg; usage = specials[special_index].arg;
angle = (M_PI)*(float)(usage)/255.; scale = specials[special_index].graph_scale;
angle = (M_PI)*(float)(usage)/scale;
px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle); px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
py = (float)(by+(h))-(float)(h)*sin(angle); py = (float)(by+(h))-(float)(h)*sin(angle);

View File

@ -297,7 +297,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
END OBJ(battery_bar, 0) END OBJ(battery_bar, 0)
char bat[64]; char bat[64];
if (arg) { if (arg) {
arg = scan_bar(obj, arg); arg = scan_bar(obj, arg, 255);
sscanf(arg, "%63s", bat); sscanf(arg, "%63s", bat);
} else { } else {
strcpy(bat, "BAT0"); strcpy(bat, "BAT0");
@ -407,12 +407,12 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
DBGP2("Adding $cpu for CPU %d", obj->data.i); DBGP2("Adding $cpu for CPU %d", obj->data.i);
END OBJ(cpugauge, &update_cpu_usage) END OBJ(cpugauge, &update_cpu_usage)
SCAN_CPU(arg, obj->data.i); SCAN_CPU(arg, obj->data.i);
scan_gauge(obj, arg); scan_gauge(obj, arg, 255);
obj->callbacks.gaugeval = &cpu_barval; obj->callbacks.gaugeval = &cpu_barval;
DBGP2("Adding $cpugauge for CPU %d", obj->data.i); DBGP2("Adding $cpugauge for CPU %d", obj->data.i);
END OBJ(cpubar, &update_cpu_usage) END OBJ(cpubar, &update_cpu_usage)
SCAN_CPU(arg, obj->data.i); SCAN_CPU(arg, obj->data.i);
scan_bar(obj, arg); scan_bar(obj, arg, 255);
obj->callbacks.barval = &cpu_barval; obj->callbacks.barval = &cpu_barval;
DBGP2("Adding $cpubar for CPU %d", obj->data.i); DBGP2("Adding $cpubar for CPU %d", obj->data.i);
#ifdef X11 #ifdef X11
@ -812,10 +812,10 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
END OBJ(memperc, &update_meminfo) END OBJ(memperc, &update_meminfo)
obj->callbacks.percentage = &mem_percentage; obj->callbacks.percentage = &mem_percentage;
END OBJ(memgauge, &update_meminfo) END OBJ(memgauge, &update_meminfo)
scan_gauge(obj, arg); scan_gauge(obj, arg, 255);
obj->callbacks.gaugeval = &mem_barval; obj->callbacks.gaugeval = &mem_barval;
END OBJ(membar, &update_meminfo) END OBJ(membar, &update_meminfo)
scan_bar(obj, arg); scan_bar(obj, arg, 255);
obj->callbacks.barval = &mem_barval; obj->callbacks.barval = &mem_barval;
#ifdef X11 #ifdef X11
END OBJ(memgraph, &update_meminfo) END OBJ(memgraph, &update_meminfo)
@ -1032,7 +1032,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
END OBJ(swapperc, &update_meminfo) END OBJ(swapperc, &update_meminfo)
obj->callbacks.percentage = &swap_percentage; obj->callbacks.percentage = &swap_percentage;
END OBJ(swapbar, &update_meminfo) END OBJ(swapbar, &update_meminfo)
scan_bar(obj, arg); scan_bar(obj, arg, 255);
obj->callbacks.barval = &swap_barval; obj->callbacks.barval = &swap_barval;
/* XXX: swapgraph, swapgauge? */ /* XXX: swapgraph, swapgauge? */
END OBJ(sysname, 0) END OBJ(sysname, 0)
@ -1167,7 +1167,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
NORM_ERR("first argument to smapi_bat_bar must be an integer value"); NORM_ERR("first argument to smapi_bat_bar must be an integer value");
obj->data.i = -1; obj->data.i = -1;
} else } else
arg = scan_bar(obj, arg + cnt); arg = scan_bar(obj, arg + cnt, 255);
obj->callbacks.barval = &smapi_bat_barval; obj->callbacks.barval = &smapi_bat_barval;
#endif /* IBM */ #endif /* IBM */
#ifdef MPD #ifdef MPD
@ -1243,7 +1243,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
obj->callbacks.print = &print_mpd_status; obj->callbacks.print = &print_mpd_status;
obj->callbacks.free = &free_mpd; obj->callbacks.free = &free_mpd;
END OBJ(mpd_bar, &update_mpd) END OBJ(mpd_bar, &update_mpd)
scan_bar(obj, arg); scan_bar(obj, arg, 255);
init_mpd(); init_mpd();
obj->callbacks.barval = &mpd_barval; obj->callbacks.barval = &mpd_barval;
obj->callbacks.free = &free_mpd; obj->callbacks.free = &free_mpd;
@ -1340,7 +1340,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
obj->callbacks.print = &print_xmms2_percent; obj->callbacks.print = &print_xmms2_percent;
obj->callbacks.free = &free_xmms2; obj->callbacks.free = &free_xmms2;
END OBJ(xmms2_bar, &update_xmms2) END OBJ(xmms2_bar, &update_xmms2)
scan_bar(obj, arg); scan_bar(obj, arg, 255);
obj->callbacks.barval = &xmms2_barval; obj->callbacks.barval = &xmms2_barval;
obj->callbacks.free = &free_xmms2; obj->callbacks.free = &free_xmms2;
END OBJ(xmms2_smart, &update_xmms2) END OBJ(xmms2_smart, &update_xmms2)
@ -1390,7 +1390,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
END OBJ(audacious_main_volume, &update_audacious) END OBJ(audacious_main_volume, &update_audacious)
obj->callbacks.print = &print_audacious_main_volume; obj->callbacks.print = &print_audacious_main_volume;
END OBJ(audacious_bar, &update_audacious) END OBJ(audacious_bar, &update_audacious)
scan_bar(obj, arg); scan_bar(obj, arg, 255);
obj->callbacks.barval = &audacious_barval; obj->callbacks.barval = &audacious_barval;
#endif /* AUDACIOUS */ #endif /* AUDACIOUS */
#ifdef BMPX #ifdef BMPX
@ -1449,7 +1449,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
obj->callbacks.print = &print_lua_parse; obj->callbacks.print = &print_lua_parse;
obj->callbacks.free = &gen_free_opaque; obj->callbacks.free = &gen_free_opaque;
END OBJ_ARG(lua_bar, 0, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]") END OBJ_ARG(lua_bar, 0, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]")
arg = scan_bar(obj, arg); arg = scan_bar(obj, arg, 255);
if(arg) { if(arg) {
obj->data.s = strndup(arg, text_buffer_size); obj->data.s = strndup(arg, text_buffer_size);
} else { } else {
@ -1460,7 +1460,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
#ifdef X11 #ifdef X11
END OBJ_ARG(lua_graph, 0, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]") END OBJ_ARG(lua_graph, 0, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]")
char *buf = 0; char *buf = 0;
buf = scan_graph(obj, arg, 0); buf = scan_graph(obj, arg, 255);
if (buf) { if (buf) {
obj->data.s = buf; obj->data.s = buf;
} else { } else {
@ -1470,7 +1470,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
obj->callbacks.free = &gen_free_opaque; obj->callbacks.free = &gen_free_opaque;
#endif /* X11 */ #endif /* X11 */
END OBJ_ARG(lua_gauge, 0, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]") END OBJ_ARG(lua_gauge, 0, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]")
arg = scan_gauge(obj, arg); arg = scan_gauge(obj, arg, 255);
if (arg) { if (arg) {
obj->data.s = strndup(arg, text_buffer_size); obj->data.s = strndup(arg, text_buffer_size);
} else { } else {
@ -1499,7 +1499,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
END OBJ(entropy_poolsize, &update_entropy) END OBJ(entropy_poolsize, &update_entropy)
obj->callbacks.print = &print_entropy_poolsize; obj->callbacks.print = &print_entropy_poolsize;
END OBJ(entropy_bar, &update_entropy) END OBJ(entropy_bar, &update_entropy)
scan_bar(obj, arg); scan_bar(obj, arg, 255);
obj->callbacks.barval = &entropy_barval; obj->callbacks.barval = &entropy_barval;
END OBJ_ARG(include, 0, "include needs a argument") END OBJ_ARG(include, 0, "include needs a argument")
struct conftree *leaf = conftree_add(currentconffile, arg); struct conftree *leaf = conftree_add(currentconffile, arg);
@ -1571,17 +1571,17 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
END OBJ(apcupsd_load, &update_apcupsd) END OBJ(apcupsd_load, &update_apcupsd)
obj->callbacks.print = &print_apcupsd_load; obj->callbacks.print = &print_apcupsd_load;
END OBJ(apcupsd_loadbar, &update_apcupsd) END OBJ(apcupsd_loadbar, &update_apcupsd)
scan_bar(obj, arg); scan_bar(obj, arg, 255);
obj->callbacks.barval = &apcupsd_loadbarval; obj->callbacks.barval = &apcupsd_loadbarval;
#ifdef X11 #ifdef X11
END OBJ(apcupsd_loadgraph, &update_apcupsd) END OBJ(apcupsd_loadgraph, &update_apcupsd)
char* buf = 0; char* buf = 0;
buf = scan_graph(obj, arg, 0); buf = scan_graph(obj, arg, 255);
if (buf) free(buf); if (buf) free(buf);
obj->callbacks.graphval = &apcupsd_loadbarval; obj->callbacks.graphval = &apcupsd_loadbarval;
#endif /* X11 */ #endif /* X11 */
END OBJ(apcupsd_loadgauge, &update_apcupsd) END OBJ(apcupsd_loadgauge, &update_apcupsd)
scan_gauge(obj, arg); scan_gauge(obj, arg, 255);
obj->callbacks.gaugeval = &apcupsd_loadbarval; obj->callbacks.gaugeval = &apcupsd_loadbarval;
END OBJ(apcupsd_charge, &update_apcupsd) END OBJ(apcupsd_charge, &update_apcupsd)
obj->callbacks.print = &print_apcupsd_charge; obj->callbacks.print = &print_apcupsd_charge;

View File

@ -197,7 +197,7 @@ void get_fs_type(const char *path, char *result)
void init_fs_bar(struct text_object *obj, const char *arg) void init_fs_bar(struct text_object *obj, const char *arg)
{ {
arg = scan_bar(obj, arg); arg = scan_bar(obj, arg, 255);
if (arg) { if (arg) {
while (isspace(*arg)) { while (isspace(*arg)) {
arg++; arg++;

View File

@ -313,10 +313,10 @@ void scan_mixer_bar(struct text_object *obj, const char *arg)
if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) { if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
obj->data.i = mixer_init(buf1); obj->data.i = mixer_init(buf1);
scan_bar(obj, arg + n); scan_bar(obj, arg + n, 255);
} else { } else {
obj->data.i = mixer_init(NULL); obj->data.i = mixer_init(NULL);
scan_bar(obj, arg); scan_bar(obj, arg, 255);
} }
} }

View File

@ -83,7 +83,7 @@ void parse_net_stat_arg(struct text_object *obj, const char *arg, void *free_at_
void parse_net_stat_bar_arg(struct text_object *obj, const char *arg, void *free_at_crash) void parse_net_stat_bar_arg(struct text_object *obj, const char *arg, void *free_at_crash)
{ {
if (arg) { if (arg) {
arg = scan_bar(obj, arg); arg = scan_bar(obj, arg, 0);
obj->data.opaque = get_net_stat(arg, obj, free_at_crash); obj->data.opaque = get_net_stat(arg, obj, free_at_crash);
} else { } else {
// default to DEFAULTNETDEV // default to DEFAULTNETDEV

View File

@ -51,24 +51,31 @@ int default_graph_width = 0, default_graph_height = 25;
#endif /* X11 */ #endif /* X11 */
int default_gauge_width = 40, default_gauge_height = 25; int default_gauge_width = 40, default_gauge_height = 25;
/* special data types flags */
#define SF_SCALED (1 << 0)
#define SF_SHOWLOG (1 << 1)
/* /*
* Special data typedefs * Special data typedefs
*/ */
struct bar { struct bar {
char flags;
int width, height; int width, height;
unsigned int scale; unsigned int scale;
}; };
struct gauge { struct gauge {
char flags;
int width, height; int width, height;
unsigned int scale; unsigned int scale;
}; };
struct graph { struct graph {
char flags;
int width, height; int width, height;
unsigned int first_colour, last_colour; unsigned int first_colour, last_colour;
unsigned int scale, showaslog; unsigned int scale;
char tempgrad; char tempgrad;
}; };
@ -84,7 +91,7 @@ struct tab {
* Scanning arguments to various special text objects * Scanning arguments to various special text objects
*/ */
const char *scan_gauge(struct text_object *obj, const char *args) const char *scan_gauge(struct text_object *obj, const char *args, unsigned int scale)
{ {
struct gauge *g; struct gauge *g;
@ -95,6 +102,11 @@ const char *scan_gauge(struct text_object *obj, const char *args)
g->width = default_gauge_width; g->width = default_gauge_width;
g->height = default_gauge_height; g->height = default_gauge_height;
if (scale)
g->scale = scale;
else
g->flags |= SF_SCALED;
/* gauge's argument is either height or height,width */ /* gauge's argument is either height or height,width */
if (args) { if (args) {
int n = 0; int n = 0;
@ -111,7 +123,7 @@ const char *scan_gauge(struct text_object *obj, const char *args)
return args; return args;
} }
const char *scan_bar(struct text_object *obj, const char *args) const char *scan_bar(struct text_object *obj, const char *args, unsigned int scale)
{ {
struct bar *b; struct bar *b;
@ -121,6 +133,12 @@ const char *scan_bar(struct text_object *obj, const char *args)
/* zero width means all space that is available */ /* zero width means all space that is available */
b->width = default_bar_width; b->width = default_bar_width;
b->height = default_bar_height; b->height = default_bar_height;
if (scale)
b->scale = scale;
else
b->flags |= SF_SCALED;
/* bar's argument is either height or height,width */ /* bar's argument is either height or height,width */
if (args) { if (args) {
int n = 0; int n = 0;
@ -159,13 +177,12 @@ char *scan_graph(struct text_object *obj, const char *args, int defscale)
g->last_colour = 0; g->last_colour = 0;
g->scale = defscale; g->scale = defscale;
g->tempgrad = FALSE; g->tempgrad = FALSE;
g->showaslog = FALSE;
if (args) { if (args) {
if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) { if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) {
g->tempgrad = TRUE; g->tempgrad = TRUE;
} }
if (strstr(args, " "LOGGRAPH) || strncmp(args, LOGGRAPH, strlen(LOGGRAPH)) == 0) { if (strstr(args, " "LOGGRAPH) || strncmp(args, LOGGRAPH, strlen(LOGGRAPH)) == 0) {
g->showaslog = TRUE; g->flags |= SF_SHOWLOG;
} }
if (sscanf(args, "%d,%d %x %x %u", &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 5) { if (sscanf(args, "%d,%d %x %x %u", &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 5) {
return NULL; return NULL;
@ -245,9 +262,9 @@ static struct special_t *new_special(char *buf, enum special_types t)
void new_gauge_in_shell(struct text_object *obj, char *p, int p_max_size, int usage) void new_gauge_in_shell(struct text_object *obj, char *p, int p_max_size, int usage)
{ {
static const char *gaugevals[] = { "_. ", "\\. ", " | ", " ./", " ._" }; static const char *gaugevals[] = { "_. ", "\\. ", " | ", " ./", " ._" };
(void)obj; struct gauge *g = obj->special_data;
snprintf(p, p_max_size, "%s", gaugevals[round_to_int((double)usage * 4 / 255)]); snprintf(p, p_max_size, "%s", gaugevals[round_to_int((double)usage * 4 / g->scale)]);
} }
#ifdef X11 #ifdef X11
@ -267,15 +284,21 @@ void new_gauge_in_x11(struct text_object *obj, char *buf, int usage)
s->arg = usage; s->arg = usage;
s->width = g->width; s->width = g->width;
s->height = g->height; s->height = g->height;
s->graph_scale = g->scale;
} }
#endif /* X11 */ #endif /* X11 */
void new_gauge(struct text_object *obj, char *p, int p_max_size, int usage) void new_gauge(struct text_object *obj, char *p, int p_max_size, int usage)
{ {
if (!p_max_size) struct gauge *g = obj->special_data;
if (!p_max_size || !g)
return; return;
usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage); if (g->flags & SF_SCALED)
g->scale = MAX(g->scale, (unsigned int)usage);
else
usage = MIN(g->scale, (unsigned int)usage);
#ifdef X11 #ifdef X11
if (output_methods & TO_X) if (output_methods & TO_X)
@ -381,11 +404,11 @@ void new_graph(struct text_object *obj, char *buf, int buf_max_size, double val)
s->graph_width = s->width - 2; // subtract 2 for rectangle around s->graph_width = s->width - 2; // subtract 2 for rectangle around
} */ } */
#ifdef MATH #ifdef MATH
if (g->showaslog) { if (g->flags & SF_SHOWLOG) {
s->graph_scale = log10(s->graph_scale + 1); s->graph_scale = log10(s->graph_scale + 1);
} }
#endif #endif
graph_append(s, val, g->showaslog); graph_append(s, val, g->flags & SF_SHOWLOG);
} }
void new_hr(struct text_object *obj, char *p, int p_max_size) void new_hr(struct text_object *obj, char *p, int p_max_size)
@ -481,7 +504,7 @@ static void new_bar_in_shell(struct text_object *obj, char* buffer, int buf_max_
if (width > buf_max_size) if (width > buf_max_size)
width = buf_max_size; width = buf_max_size;
scaledusage = round_to_int( usage * width / 255); scaledusage = round_to_int( usage * width / b->scale);
for (i = 0; i < scaledusage; i++) for (i = 0; i < scaledusage; i++)
buffer[i] = '#'; buffer[i] = '#';
@ -509,16 +532,22 @@ static void new_bar_in_x11(struct text_object *obj, char *buf, int usage)
s->arg = usage; s->arg = usage;
s->width = b->width; s->width = b->width;
s->height = b->height; s->height = b->height;
s->graph_scale = b->scale;
} }
#endif /* X11 */ #endif /* X11 */
/* usage is in range [0,255] */ /* usage is in range [0,255] */
void new_bar(struct text_object *obj, char *p, int p_max_size, int usage) void new_bar(struct text_object *obj, char *p, int p_max_size, int usage)
{ {
if (!p_max_size) struct bar *b = obj->special_data;
if (!p_max_size || !b)
return; return;
usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage); if (b->flags & SF_SCALED)
b->scale = MAX(b->scale, (unsigned int)usage);
else
usage = MIN(b->scale, (unsigned int)usage);
#ifdef X11 #ifdef X11
if ((output_methods & TO_X)) if ((output_methods & TO_X))

View File

@ -94,8 +94,8 @@ struct text_object;
extern int max_specials; extern int max_specials;
/* scanning special arguments */ /* scanning special arguments */
const char *scan_bar(struct text_object *, const char *); const char *scan_bar(struct text_object *, const char *, unsigned int);
const char *scan_gauge(struct text_object *, const char *); const char *scan_gauge(struct text_object *, const char *, unsigned int);
#ifdef X11 #ifdef X11
void scan_font(struct text_object *, const char *); void scan_font(struct text_object *, const char *);
char *scan_graph(struct text_object *, const char *, int); char *scan_graph(struct text_object *, const char *, int);