diff --git a/src/conky.c b/src/conky.c index afe3da0a..c6eeca79 100644 --- a/src/conky.c +++ b/src/conky.c @@ -1289,12 +1289,14 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) case BAR: { int h, bar_usage, by; + float scale; if (cur_x - text_start_x > maximum_width && maximum_width > 0) { break; } h = specials[special_index].height; bar_usage = specials[special_index].arg; + scale = specials[special_index].graph_scale; by = cur_y - (font_ascent() / 2) - 1; 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, by, w, h); 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 && h > font_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 float angle, px, py; int usage; + float scale; #endif /* MATH */ 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 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); py = (float)(by+(h))-(float)(h)*sin(angle); diff --git a/src/core.c b/src/core.c index 6f4b09cd..0d8415a1 100644 --- a/src/core.c +++ b/src/core.c @@ -297,7 +297,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long END OBJ(battery_bar, 0) char bat[64]; if (arg) { - arg = scan_bar(obj, arg); + arg = scan_bar(obj, arg, 255); sscanf(arg, "%63s", bat); } else { 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); END OBJ(cpugauge, &update_cpu_usage) SCAN_CPU(arg, obj->data.i); - scan_gauge(obj, arg); + scan_gauge(obj, arg, 255); obj->callbacks.gaugeval = &cpu_barval; DBGP2("Adding $cpugauge for CPU %d", obj->data.i); END OBJ(cpubar, &update_cpu_usage) SCAN_CPU(arg, obj->data.i); - scan_bar(obj, arg); + scan_bar(obj, arg, 255); obj->callbacks.barval = &cpu_barval; DBGP2("Adding $cpubar for CPU %d", obj->data.i); #ifdef X11 @@ -812,10 +812,10 @@ struct text_object *construct_text_object(const char *s, const char *arg, long END OBJ(memperc, &update_meminfo) obj->callbacks.percentage = &mem_percentage; END OBJ(memgauge, &update_meminfo) - scan_gauge(obj, arg); + scan_gauge(obj, arg, 255); obj->callbacks.gaugeval = &mem_barval; END OBJ(membar, &update_meminfo) - scan_bar(obj, arg); + scan_bar(obj, arg, 255); obj->callbacks.barval = &mem_barval; #ifdef X11 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) obj->callbacks.percentage = &swap_percentage; END OBJ(swapbar, &update_meminfo) - scan_bar(obj, arg); + scan_bar(obj, arg, 255); obj->callbacks.barval = &swap_barval; /* XXX: swapgraph, swapgauge? */ 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"); obj->data.i = -1; } else - arg = scan_bar(obj, arg + cnt); + arg = scan_bar(obj, arg + cnt, 255); obj->callbacks.barval = &smapi_bat_barval; #endif /* IBM */ #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.free = &free_mpd; END OBJ(mpd_bar, &update_mpd) - scan_bar(obj, arg); + scan_bar(obj, arg, 255); init_mpd(); obj->callbacks.barval = &mpd_barval; 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.free = &free_xmms2; END OBJ(xmms2_bar, &update_xmms2) - scan_bar(obj, arg); + scan_bar(obj, arg, 255); obj->callbacks.barval = &xmms2_barval; obj->callbacks.free = &free_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) obj->callbacks.print = &print_audacious_main_volume; END OBJ(audacious_bar, &update_audacious) - scan_bar(obj, arg); + scan_bar(obj, arg, 255); obj->callbacks.barval = &audacious_barval; #endif /* AUDACIOUS */ #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.free = &gen_free_opaque; END OBJ_ARG(lua_bar, 0, "lua_bar needs arguments: , [function parameters]") - arg = scan_bar(obj, arg); + arg = scan_bar(obj, arg, 255); if(arg) { obj->data.s = strndup(arg, text_buffer_size); } else { @@ -1460,7 +1460,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long #ifdef X11 END OBJ_ARG(lua_graph, 0, "lua_graph needs arguments: [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]") char *buf = 0; - buf = scan_graph(obj, arg, 0); + buf = scan_graph(obj, arg, 255); if (buf) { obj->data.s = buf; } else { @@ -1470,7 +1470,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long obj->callbacks.free = &gen_free_opaque; #endif /* X11 */ END OBJ_ARG(lua_gauge, 0, "lua_gauge needs arguments: , [function parameters]") - arg = scan_gauge(obj, arg); + arg = scan_gauge(obj, arg, 255); if (arg) { obj->data.s = strndup(arg, text_buffer_size); } else { @@ -1499,7 +1499,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long END OBJ(entropy_poolsize, &update_entropy) obj->callbacks.print = &print_entropy_poolsize; END OBJ(entropy_bar, &update_entropy) - scan_bar(obj, arg); + scan_bar(obj, arg, 255); obj->callbacks.barval = &entropy_barval; END OBJ_ARG(include, 0, "include needs a argument") 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) obj->callbacks.print = &print_apcupsd_load; END OBJ(apcupsd_loadbar, &update_apcupsd) - scan_bar(obj, arg); + scan_bar(obj, arg, 255); obj->callbacks.barval = &apcupsd_loadbarval; #ifdef X11 END OBJ(apcupsd_loadgraph, &update_apcupsd) char* buf = 0; - buf = scan_graph(obj, arg, 0); + buf = scan_graph(obj, arg, 255); if (buf) free(buf); obj->callbacks.graphval = &apcupsd_loadbarval; #endif /* X11 */ END OBJ(apcupsd_loadgauge, &update_apcupsd) - scan_gauge(obj, arg); + scan_gauge(obj, arg, 255); obj->callbacks.gaugeval = &apcupsd_loadbarval; END OBJ(apcupsd_charge, &update_apcupsd) obj->callbacks.print = &print_apcupsd_charge; diff --git a/src/fs.c b/src/fs.c index a899b243..15196980 100644 --- a/src/fs.c +++ b/src/fs.c @@ -197,7 +197,7 @@ void get_fs_type(const char *path, char *result) void init_fs_bar(struct text_object *obj, const char *arg) { - arg = scan_bar(obj, arg); + arg = scan_bar(obj, arg, 255); if (arg) { while (isspace(*arg)) { arg++; diff --git a/src/mixer.c b/src/mixer.c index 655e4dbb..81a4ef23 100644 --- a/src/mixer.c +++ b/src/mixer.c @@ -313,10 +313,10 @@ void scan_mixer_bar(struct text_object *obj, const char *arg) if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) { obj->data.i = mixer_init(buf1); - scan_bar(obj, arg + n); + scan_bar(obj, arg + n, 255); } else { obj->data.i = mixer_init(NULL); - scan_bar(obj, arg); + scan_bar(obj, arg, 255); } } diff --git a/src/net_stat.c b/src/net_stat.c index 33068e49..d6d69ec7 100644 --- a/src/net_stat.c +++ b/src/net_stat.c @@ -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) { if (arg) { - arg = scan_bar(obj, arg); + arg = scan_bar(obj, arg, 0); obj->data.opaque = get_net_stat(arg, obj, free_at_crash); } else { // default to DEFAULTNETDEV diff --git a/src/specials.c b/src/specials.c index 92de494a..3f905c0a 100644 --- a/src/specials.c +++ b/src/specials.c @@ -51,24 +51,31 @@ int default_graph_width = 0, default_graph_height = 25; #endif /* X11 */ 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 */ struct bar { + char flags; int width, height; unsigned int scale; }; struct gauge { + char flags; int width, height; unsigned int scale; }; struct graph { + char flags; int width, height; unsigned int first_colour, last_colour; - unsigned int scale, showaslog; + unsigned int scale; char tempgrad; }; @@ -84,7 +91,7 @@ struct tab { * 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; @@ -95,6 +102,11 @@ const char *scan_gauge(struct text_object *obj, const char *args) g->width = default_gauge_width; g->height = default_gauge_height; + if (scale) + g->scale = scale; + else + g->flags |= SF_SCALED; + /* gauge's argument is either height or height,width */ if (args) { int n = 0; @@ -111,7 +123,7 @@ const char *scan_gauge(struct text_object *obj, const char *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; @@ -121,6 +133,12 @@ const char *scan_bar(struct text_object *obj, const char *args) /* zero width means all space that is available */ b->width = default_bar_width; b->height = default_bar_height; + + if (scale) + b->scale = scale; + else + b->flags |= SF_SCALED; + /* bar's argument is either height or height,width */ if (args) { int n = 0; @@ -159,13 +177,12 @@ char *scan_graph(struct text_object *obj, const char *args, int defscale) g->last_colour = 0; g->scale = defscale; g->tempgrad = FALSE; - g->showaslog = FALSE; if (args) { if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) { g->tempgrad = TRUE; } 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) { 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) { 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 @@ -267,15 +284,21 @@ void new_gauge_in_x11(struct text_object *obj, char *buf, int usage) s->arg = usage; s->width = g->width; s->height = g->height; + s->graph_scale = g->scale; } #endif /* X11 */ 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; - 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 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 } */ #ifdef MATH - if (g->showaslog) { + if (g->flags & SF_SHOWLOG) { s->graph_scale = log10(s->graph_scale + 1); } #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) @@ -481,7 +504,7 @@ static void new_bar_in_shell(struct text_object *obj, char* buffer, int buf_max_ if (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++) buffer[i] = '#'; @@ -509,16 +532,22 @@ static void new_bar_in_x11(struct text_object *obj, char *buf, int usage) s->arg = usage; s->width = b->width; s->height = b->height; + s->graph_scale = b->scale; } #endif /* X11 */ /* usage is in range [0,255] */ 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; - 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 if ((output_methods & TO_X)) diff --git a/src/specials.h b/src/specials.h index 4f81164e..576868d6 100644 --- a/src/specials.h +++ b/src/specials.h @@ -94,8 +94,8 @@ struct text_object; extern int max_specials; /* scanning special arguments */ -const char *scan_bar(struct text_object *, const char *); -const char *scan_gauge(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 *, unsigned int); #ifdef X11 void scan_font(struct text_object *, const char *); char *scan_graph(struct text_object *, const char *, int);