1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-17 18:45:10 +00:00

specials: convert graph objects to new style

This commit is contained in:
Phil Sutter 2009-10-29 03:08:08 +01:00
parent a4777c7c55
commit bbec0db498
9 changed files with 101 additions and 126 deletions

View File

@ -448,16 +448,13 @@ void scan_loadgraph_arg(struct text_object *obj, const char *arg)
{ {
char *buf = 0; char *buf = 0;
SIZE_DEFAULTS(graph); buf = scan_graph(obj, arg);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->char_a, &obj->char_b);
if (buf) if (buf)
free(buf); free(buf);
} }
void print_loadgraph(struct text_object *obj, char *p) void print_loadgraph(struct text_object *obj, char *p)
{ {
new_graph(p, obj->a, obj->b, obj->c, obj->d, info.loadavg[0], new_graph(obj, p, info.loadavg[0]);
obj->e, 1, obj->char_a, obj->char_b);
} }
#endif /* X11 */ #endif /* X11 */

View File

@ -935,9 +935,7 @@ void generate_text_internal(char *p, int p_max_size,
} }
#ifdef X11 #ifdef X11
OBJ(cpugraph) { OBJ(cpugraph) {
new_graph(p, obj->a, obj->b, obj->c, obj->d, new_graph(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 100));
round_to_int(cur->cpu_usage[obj->data.i] * 100),
100, 1, obj->char_a, obj->char_b);
} }
OBJ(loadgraph) { OBJ(loadgraph) {
print_loadgraph(obj, p); print_loadgraph(obj, p);
@ -1332,8 +1330,7 @@ void generate_text_internal(char *p, int p_max_size,
OBJ(lua_graph) { OBJ(lua_graph) {
double per; double per;
if (llua_getnumber(obj->data.s, &per)) { if (llua_getnumber(obj->data.s, &per)) {
new_graph(p, obj->a, obj->b, obj->c, obj->d, new_graph(obj, p, per);
per, obj->e, 1, obj->char_a, obj->char_b);
} }
} }
OBJ(lua_gauge) { OBJ(lua_gauge) {
@ -1483,9 +1480,7 @@ void generate_text_internal(char *p, int p_max_size,
} }
#ifdef X11 #ifdef X11
OBJ(memgraph) { OBJ(memgraph) {
new_graph(p, obj->a, obj->b, obj->c, obj->d, new_graph(obj, p, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
100, 1, obj->char_a, obj->char_b);
} }
#endif /* X11 */ #endif /* X11 */
/* mixer stuff */ /* mixer stuff */
@ -2262,8 +2257,7 @@ void generate_text_internal(char *p, int p_max_size,
OBJ(apcupsd_loadgraph) { OBJ(apcupsd_loadgraph) {
double progress; double progress;
progress = atof(cur->apcupsd.items[APCUPSD_LOAD]); progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
new_graph(p, obj->a, obj->b, obj->c, obj->d, new_graph(obj, p, (int)progress);
(int)progress, 100, 1, obj->char_a, obj->char_b);
} }
OBJ(apcupsd_loadgauge) { OBJ(apcupsd_loadgauge) {
double progress; double progress;

View File

@ -357,10 +357,8 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
#ifdef X11 #ifdef X11
END OBJ(cpugraph, &update_cpu_usage) END OBJ(cpugraph, &update_cpu_usage)
char *buf = 0; char *buf = 0;
SIZE_DEFAULTS(graph);
SCAN_CPU(arg, obj->data.i); SCAN_CPU(arg, obj->data.i);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, buf = scan_graph(obj, arg);
&obj->e, &obj->char_a, &obj->char_b);
DBGP2("Adding $cpugraph for CPU %d", obj->data.i); DBGP2("Adding $cpugraph for CPU %d", obj->data.i);
if (buf) free(buf); if (buf) free(buf);
END OBJ(loadgraph, &update_load_average) END OBJ(loadgraph, &update_load_average)
@ -477,15 +475,13 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
SIZE_DEFAULTS(gauge); SIZE_DEFAULTS(gauge);
scan_exec_arg(obj, arg); scan_exec_arg(obj, arg);
END OBJ(execgraph, 0) END OBJ(execgraph, 0)
SIZE_DEFAULTS(graph); scan_execgraph_arg(obj, arg);
scan_exec_arg(obj, arg);
#endif /* X11 */ #endif /* X11 */
END OBJ_ARG(execibar, 0, "execibar needs arguments") END OBJ_ARG(execibar, 0, "execibar needs arguments")
scan_execi_arg(obj, arg); scan_execi_arg(obj, arg);
#ifdef X11 #ifdef X11
END OBJ_ARG(execigraph, 0, "execigraph needs arguments") END OBJ_ARG(execigraph, 0, "execigraph needs arguments")
SIZE_DEFAULTS(graph); scan_execgraph_arg(obj, arg);
scan_execi_arg(obj, arg);
END OBJ_ARG(execigauge, 0, "execigauge needs arguments") END OBJ_ARG(execigauge, 0, "execigauge needs arguments")
SIZE_DEFAULTS(gauge); SIZE_DEFAULTS(gauge);
scan_execi_arg(obj, arg); scan_execi_arg(obj, arg);
@ -649,9 +645,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
#ifdef X11 #ifdef X11
END OBJ(memgraph, &update_meminfo) END OBJ(memgraph, &update_meminfo)
char *buf = 0; char *buf = 0;
SIZE_DEFAULTS(graph); buf = scan_graph(obj, arg);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->char_a, &obj->char_b);
if (buf) free(buf); if (buf) free(buf);
#endif /* X11*/ #endif /* X11*/
@ -951,9 +945,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;
SIZE_DEFAULTS(graph); buf = scan_graph(obj, arg);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->char_a, &obj->char_b);
if (buf) { if (buf) {
obj->data.s = buf; obj->data.s = buf;
} else { } else {
@ -1036,9 +1028,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
#ifdef X11 #ifdef X11
END OBJ(apcupsd_loadgraph, &update_apcupsd) END OBJ(apcupsd_loadgraph, &update_apcupsd)
char* buf = 0; char* buf = 0;
SIZE_DEFAULTS(graph); buf = scan_graph(obj, arg);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->char_a, &obj->char_b);
if (buf) free(buf); if (buf) free(buf);
END OBJ(apcupsd_loadgauge, &update_apcupsd) END OBJ(apcupsd_loadgauge, &update_apcupsd)
SIZE_DEFAULTS(gauge); SIZE_DEFAULTS(gauge);

View File

@ -143,9 +143,7 @@ void print_diskio(struct text_object *obj, int dir, char *p, int p_max_size)
void parse_diskiograph_arg(struct text_object *obj, const char *arg) void parse_diskiograph_arg(struct text_object *obj, const char *arg)
{ {
char *buf = 0; char *buf = 0;
SIZE_DEFAULTS(graph); buf = scan_graph(obj, arg);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->char_a, &obj->char_b);
obj->data.opaque = prepare_diskio_stat(dev_name(buf)); obj->data.opaque = prepare_diskio_stat(dev_name(buf));
if (buf) if (buf)
@ -167,8 +165,7 @@ void print_diskiograph(struct text_object *obj, int dir, char *p)
else else
val = diskio->current_write; val = diskio->current_write;
new_graph(p, obj->a, obj->b, obj->c, obj->d, new_graph(obj, p, val);
val, obj->e, 1, obj->char_a, obj->char_b);
} }
#endif /* X11 */ #endif /* X11 */

View File

@ -235,6 +235,23 @@ void scan_execi_arg(struct text_object *obj, const char *arg)
obj->data.opaque = ed; obj->data.opaque = ed;
} }
void scan_execgraph_arg(struct text_object *obj, const char *arg)
{
struct execi_data *ed;
char *buf;
ed = malloc(sizeof(struct execi_data));
memset(ed, 0, sizeof(struct execi_data));
buf = scan_graph(obj, arg);
if (!buf) {
NORM_ERR("missing command argument to execgraph object");
return;
}
ed->cmd = buf;
obj->data.opaque = ed;
}
void print_exec(struct text_object *obj, char *p, int p_max_size) void print_exec(struct text_object *obj, char *p, int p_max_size)
{ {
read_exec(obj->data.s, p, p_max_size); read_exec(obj->data.s, p, p_max_size);
@ -353,31 +370,17 @@ void print_execgauge(struct text_object *obj, char *p, int p_max_size)
void print_execgraph(struct text_object *obj, char *p, int p_max_size) void print_execgraph(struct text_object *obj, char *p, int p_max_size)
{ {
char showaslog = FALSE;
char tempgrad = FALSE;
double barnum; double barnum;
struct execi_data *ed = obj->data.opaque; struct execi_data *ed = obj->data.opaque;
char *cmd;
if (!ed) if (!ed)
return; return;
cmd = ed->cmd; read_exec(ed->cmd, p, p_max_size);
if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
tempgrad = TRUE;
cmd += strlen(" "TEMPGRAD);
}
if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
showaslog = TRUE;
cmd += strlen(" "LOGGRAPH);
}
read_exec(cmd, p, p_max_size);
barnum = get_barnum(p); barnum = get_barnum(p);
if (barnum > 0) { if (barnum > 0) {
new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum), new_graph(obj, p, round_to_int(barnum));
100, 1, showaslog, tempgrad);
} }
} }
@ -390,21 +393,8 @@ void print_execigraph(struct text_object *obj, char *p, int p_max_size)
if (time_to_update(ed)) { if (time_to_update(ed)) {
double barnum; double barnum;
char showaslog = FALSE;
char tempgrad = FALSE;
char *cmd = ed->cmd;
if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) { read_exec(ed->cmd, p, p_max_size);
tempgrad = TRUE;
cmd += strlen(" "TEMPGRAD);
}
if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
showaslog = TRUE;
cmd += strlen(" "LOGGRAPH);
}
obj->char_a = showaslog;
obj->char_b = tempgrad;
read_exec(cmd, p, p_max_size);
barnum = get_barnum(p); barnum = get_barnum(p);
if (barnum >= 0.0) { if (barnum >= 0.0) {
@ -412,7 +402,7 @@ void print_execigraph(struct text_object *obj, char *p, int p_max_size)
} }
ed->last_update = current_update_time; ed->last_update = current_update_time;
} }
new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b); new_graph(obj, p, (int) (obj->f));
} }
void print_execigauge(struct text_object *obj, char *p, int p_max_size) void print_execigauge(struct text_object *obj, char *p, int p_max_size)

View File

@ -36,6 +36,7 @@ extern pid_t childpid;
void scan_exec_arg(struct text_object *, const char *); void scan_exec_arg(struct text_object *, const char *);
void scan_pre_exec_arg(struct text_object *, const char *); void scan_pre_exec_arg(struct text_object *, const char *);
void scan_execi_arg(struct text_object *, const char *); void scan_execi_arg(struct text_object *, const char *);
void scan_execgraph_arg(struct text_object *, const char *);
void print_exec(struct text_object *, char *, int); void print_exec(struct text_object *, char *, int);
void print_execp(struct text_object *, char *, int); void print_execp(struct text_object *, char *, int);
void print_execi(struct text_object *, char *, int); void print_execi(struct text_object *, char *, int);

View File

@ -193,9 +193,7 @@ void print_addrs(struct text_object *obj, char *p, int p_max_size)
void parse_net_stat_graph_arg(struct text_object *obj, const char *arg, void *free_at_crash) void parse_net_stat_graph_arg(struct text_object *obj, const char *arg, void *free_at_crash)
{ {
char *buf = 0; char *buf = 0;
SIZE_DEFAULTS(graph); buf = scan_graph(obj, arg);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->char_a, &obj->char_b);
// default to DEFAULTNETDEV // default to DEFAULTNETDEV
if (buf) { if (buf) {
@ -213,8 +211,7 @@ void print_downspeedgraph(struct text_object *obj, char *p)
if (!ns) if (!ns)
return; return;
new_graph(p, obj->a, obj->b, obj->c, obj->d, new_graph(obj, p, ns->recv_speed / 1024.0);
ns->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
} }
void print_upspeedgraph(struct text_object *obj, char *p) void print_upspeedgraph(struct text_object *obj, char *p)
@ -224,8 +221,7 @@ void print_upspeedgraph(struct text_object *obj, char *p)
if (!ns) if (!ns)
return; return;
new_graph(p, obj->a, obj->b, obj->c, obj->d, new_graph(obj, p, ns->trans_speed / 1024.0);
ns->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
} }
#endif /* X11 */ #endif /* X11 */

View File

@ -59,6 +59,13 @@ struct bar {
int width, height; int width, height;
}; };
struct graph {
int width, height;
unsigned int first_colour, last_colour;
unsigned int scale, showaslog;
char tempgrad;
};
/* /*
* Scanning arguments to various special text objects * Scanning arguments to various special text objects
*/ */
@ -120,74 +127,78 @@ char *scan_font(const char *args)
return NULL; return NULL;
} }
char *scan_graph(const char *args, int *w, int *h, char *scan_graph(struct text_object *obj, const char *args)
unsigned int *first_colour, unsigned int *last_colour,
unsigned int *scale, char *showaslog, char *tempgrad)
{ {
struct graph *g;
char buf[1024]; char buf[1024];
memset(buf, 0, 1024); memset(buf, 0, 1024);
g = malloc(sizeof(struct graph));
memset(g, 0, sizeof(struct graph));
obj->special_data = g;
/* zero width means all space that is available */ /* zero width means all space that is available */
*w = default_graph_width; g->width = default_graph_width;
*h = default_graph_height; g->height = default_graph_height;
*first_colour = 0; g->first_colour = 0;
*last_colour = 0; g->last_colour = 0;
*scale = 0; g->scale = 0;
*tempgrad = FALSE; g->tempgrad = FALSE;
*showaslog = 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) {
*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) {
*showaslog = TRUE; g->showaslog = TRUE;
} }
if (sscanf(args, "%d,%d %x %x %u", h, w, first_colour, last_colour, 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;
} }
*scale = 0; g->scale = 0;
if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) { if (sscanf(args, "%d,%d %x %x", &g->height, &g->width, &g->first_colour, &g->last_colour) == 4) {
return NULL; return NULL;
} }
if (sscanf(args, "%1023s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) { if (sscanf(args, "%1023s %d,%d %x %x %u", buf, &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 6) {
return strndup(buf, text_buffer_size); return strndup(buf, text_buffer_size);
} }
*scale = 0; g->scale = 0;
if (sscanf(args, "%1023s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) { if (sscanf(args, "%1023s %d,%d %x %x", buf, &g->height, &g->width, &g->first_colour, &g->last_colour) == 5) {
return strndup(buf, text_buffer_size); return strndup(buf, text_buffer_size);
} }
buf[0] = '\0'; buf[0] = '\0';
*h = 25; g->height = 25;
*w = 0; g->width = 0;
if (sscanf(args, "%x %x %u", first_colour, last_colour, scale) == 3) { if (sscanf(args, "%x %x %u", &g->first_colour, &g->last_colour, &g->scale) == 3) {
return NULL; return NULL;
} }
*scale = 0; g->scale = 0;
if (sscanf(args, "%x %x", first_colour, last_colour) == 2) { if (sscanf(args, "%x %x", &g->first_colour, &g->last_colour) == 2) {
return NULL; return NULL;
} }
if (sscanf(args, "%1023s %x %x %u", buf, first_colour, last_colour, scale) == 4) { if (sscanf(args, "%1023s %x %x %u", buf, &g->first_colour, &g->last_colour, &g->scale) == 4) {
return strndup(buf, text_buffer_size); return strndup(buf, text_buffer_size);
} }
*scale = 0; g->scale = 0;
if (sscanf(args, "%1023s %x %x", buf, first_colour, last_colour) == 3) { if (sscanf(args, "%1023s %x %x", buf, &g->first_colour, &g->last_colour) == 3) {
return strndup(buf, text_buffer_size); return strndup(buf, text_buffer_size);
} }
buf[0] = '\0'; buf[0] = '\0';
*first_colour = 0; g->first_colour = 0;
*last_colour = 0; g->last_colour = 0;
if (sscanf(args, "%d,%d %u", h, w, scale) == 3) { if (sscanf(args, "%d,%d %u", &g->height, &g->width, &g->scale) == 3) {
return NULL; return NULL;
} }
*scale = 0; g->scale = 0;
if (sscanf(args, "%d,%d", h, w) == 2) { if (sscanf(args, "%d,%d", &g->height, &g->width) == 2) {
return NULL; return NULL;
} }
if (sscanf(args, "%1023s %d,%d %u", buf, h, w, scale) < 4) { if (sscanf(args, "%1023s %d,%d %u", buf, &g->height, &g->width, &g->scale) < 4) {
*scale = 0; g->scale = 0;
//TODO: check the return value and throw an error? //TODO: check the return value and throw an error?
sscanf(args, "%1023s %d,%d", buf, h, w); sscanf(args, "%1023s %d,%d", buf, &g->height, &g->width);
} }
#undef g
return strndup(buf, text_buffer_size); return strndup(buf, text_buffer_size);
} }
@ -280,7 +291,7 @@ static void graph_append(struct special_t *graph, double f, char showaslog)
f = log10(f + 1); f = log10(f + 1);
#endif #endif
} }
if (!graph->scaled && f > graph->graph_scale) { if (!graph->scaled && f > graph->graph_scale) {
f = graph->graph_scale; f = graph->graph_scale;
} }
@ -300,17 +311,20 @@ static void graph_append(struct special_t *graph, double f, char showaslog)
} }
} }
void new_graph(char *buf, int w, int h, unsigned int first_colour, void new_graph(struct text_object *obj, char *buf, double val)
unsigned int second_colour, double i, int scale, int append, char showaslog, char tempgrad)
{ {
struct special_t *s = 0; struct special_t *s = 0;
struct graph *g = obj->special_data;
if ((output_methods & TO_X) == 0) if ((output_methods & TO_X) == 0)
return; return;
if (!g)
return;
s = new_special(buf, GRAPH); s = new_special(buf, GRAPH);
s->width = w; s->width = g->width;
if (s->graph == NULL) { if (s->graph == NULL) {
if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) { if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
// subtract 2 for the box // subtract 2 for the box
@ -322,30 +336,28 @@ void new_graph(char *buf, int w, int h, unsigned int first_colour,
memset(s->graph, 0, s->graph_width * sizeof(double)); memset(s->graph, 0, s->graph_width * sizeof(double));
s->graph_scale = 100; s->graph_scale = 100;
} }
s->height = h; s->height = g->height;
s->first_colour = adjust_colours(first_colour); s->first_colour = adjust_colours(g->first_colour);
s->last_colour = adjust_colours(second_colour); s->last_colour = adjust_colours(g->last_colour);
if (scale != 0) { if (g->scale != 0) {
s->scaled = 0; s->scaled = 0;
s->graph_scale = scale; s->graph_scale = g->scale;
s->show_scale = 0; s->show_scale = 0;
} else { } else {
s->scaled = 1; s->scaled = 1;
s->graph_scale = 1; s->graph_scale = 1;
s->show_scale = 1; s->show_scale = 1;
} }
s->tempgrad = tempgrad; s->tempgrad = g->tempgrad;
/* if (s->width) { /* if (s->width) {
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 (showaslog) { if (g->showaslog) {
s->graph_scale = log10(s->graph_scale + 1); s->graph_scale = log10(s->graph_scale + 1);
} }
#endif #endif
if (append) { graph_append(s, val, g->showaslog);
graph_append(s, i, showaslog);
}
} }
void new_hr(char *buf, int a) void new_hr(char *buf, int a)

View File

@ -103,15 +103,13 @@ const char *scan_bar(struct text_object *, const char *);
#ifdef X11 #ifdef X11
const char *scan_gauge(const char *, int *, int *); const char *scan_gauge(const char *, int *, int *);
char *scan_font(const char *); char *scan_font(const char *);
char *scan_graph(const char *, int *, int *, unsigned int *, char *scan_graph(struct text_object *, const char *);
unsigned int *, unsigned int *, char *, char *);
/* printing specials */ /* printing specials */
void new_gauge(char *, int, int, int); void new_gauge(char *, int, int, int);
void new_bar(struct text_object *, char *, int); void new_bar(struct text_object *, char *, int);
void new_font(char *, char *); void new_font(char *, char *);
void new_graph(char *, int, int, unsigned int, void new_graph(struct text_object *, char *, double);
unsigned int, double, int, int, char, char);
void new_hr(char *, int); void new_hr(char *, int);
void new_stippled_hr(char *, int, int); void new_stippled_hr(char *, int, int);
#endif #endif