1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-09-29 21:49:07 +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;
SIZE_DEFAULTS(graph);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->char_a, &obj->char_b);
buf = scan_graph(obj, arg);
if (buf)
free(buf);
}
void print_loadgraph(struct text_object *obj, char *p)
{
new_graph(p, obj->a, obj->b, obj->c, obj->d, info.loadavg[0],
obj->e, 1, obj->char_a, obj->char_b);
new_graph(obj, p, info.loadavg[0]);
}
#endif /* X11 */

View File

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

View File

@ -357,10 +357,8 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
#ifdef X11
END OBJ(cpugraph, &update_cpu_usage)
char *buf = 0;
SIZE_DEFAULTS(graph);
SCAN_CPU(arg, obj->data.i);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->char_a, &obj->char_b);
buf = scan_graph(obj, arg);
DBGP2("Adding $cpugraph for CPU %d", obj->data.i);
if (buf) free(buf);
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);
scan_exec_arg(obj, arg);
END OBJ(execgraph, 0)
SIZE_DEFAULTS(graph);
scan_exec_arg(obj, arg);
scan_execgraph_arg(obj, arg);
#endif /* X11 */
END OBJ_ARG(execibar, 0, "execibar needs arguments")
scan_execi_arg(obj, arg);
#ifdef X11
END OBJ_ARG(execigraph, 0, "execigraph needs arguments")
SIZE_DEFAULTS(graph);
scan_execi_arg(obj, arg);
scan_execgraph_arg(obj, arg);
END OBJ_ARG(execigauge, 0, "execigauge needs arguments")
SIZE_DEFAULTS(gauge);
scan_execi_arg(obj, arg);
@ -649,9 +645,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
#ifdef X11
END OBJ(memgraph, &update_meminfo)
char *buf = 0;
SIZE_DEFAULTS(graph);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->char_a, &obj->char_b);
buf = scan_graph(obj, arg);
if (buf) free(buf);
#endif /* X11*/
@ -951,9 +945,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: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]")
char *buf = 0;
SIZE_DEFAULTS(graph);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->char_a, &obj->char_b);
buf = scan_graph(obj, arg);
if (buf) {
obj->data.s = buf;
} else {
@ -1036,9 +1028,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
#ifdef X11
END OBJ(apcupsd_loadgraph, &update_apcupsd)
char* buf = 0;
SIZE_DEFAULTS(graph);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->char_a, &obj->char_b);
buf = scan_graph(obj, arg);
if (buf) free(buf);
END OBJ(apcupsd_loadgauge, &update_apcupsd)
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)
{
char *buf = 0;
SIZE_DEFAULTS(graph);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->char_a, &obj->char_b);
buf = scan_graph(obj, arg);
obj->data.opaque = prepare_diskio_stat(dev_name(buf));
if (buf)
@ -167,8 +165,7 @@ void print_diskiograph(struct text_object *obj, int dir, char *p)
else
val = diskio->current_write;
new_graph(p, obj->a, obj->b, obj->c, obj->d,
val, obj->e, 1, obj->char_a, obj->char_b);
new_graph(obj, p, val);
}
#endif /* X11 */

View File

@ -235,6 +235,23 @@ void scan_execi_arg(struct text_object *obj, const char *arg)
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)
{
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)
{
char showaslog = FALSE;
char tempgrad = FALSE;
double barnum;
struct execi_data *ed = obj->data.opaque;
char *cmd;
if (!ed)
return;
cmd = ed->cmd;
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);
read_exec(ed->cmd, p, p_max_size);
barnum = get_barnum(p);
if (barnum > 0) {
new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
100, 1, showaslog, tempgrad);
new_graph(obj, p, round_to_int(barnum));
}
}
@ -390,21 +393,8 @@ void print_execigraph(struct text_object *obj, char *p, int p_max_size)
if (time_to_update(ed)) {
double barnum;
char showaslog = FALSE;
char tempgrad = FALSE;
char *cmd = ed->cmd;
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);
}
obj->char_a = showaslog;
obj->char_b = tempgrad;
read_exec(cmd, p, p_max_size);
read_exec(ed->cmd, p, p_max_size);
barnum = get_barnum(p);
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;
}
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)

View File

@ -36,6 +36,7 @@ extern pid_t childpid;
void scan_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_execgraph_arg(struct text_object *, const char *);
void print_exec(struct text_object *, char *, int);
void print_execp(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)
{
char *buf = 0;
SIZE_DEFAULTS(graph);
buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
&obj->e, &obj->char_a, &obj->char_b);
buf = scan_graph(obj, arg);
// default to DEFAULTNETDEV
if (buf) {
@ -213,8 +211,7 @@ void print_downspeedgraph(struct text_object *obj, char *p)
if (!ns)
return;
new_graph(p, obj->a, obj->b, obj->c, obj->d,
ns->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
new_graph(obj, p, ns->recv_speed / 1024.0);
}
void print_upspeedgraph(struct text_object *obj, char *p)
@ -224,8 +221,7 @@ void print_upspeedgraph(struct text_object *obj, char *p)
if (!ns)
return;
new_graph(p, obj->a, obj->b, obj->c, obj->d,
ns->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
new_graph(obj, p, ns->trans_speed / 1024.0);
}
#endif /* X11 */

View File

@ -59,6 +59,13 @@ struct bar {
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
*/
@ -120,74 +127,78 @@ char *scan_font(const char *args)
return NULL;
}
char *scan_graph(const char *args, int *w, int *h,
unsigned int *first_colour, unsigned int *last_colour,
unsigned int *scale, char *showaslog, char *tempgrad)
char *scan_graph(struct text_object *obj, const char *args)
{
struct graph *g;
char buf[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 */
*w = default_graph_width;
*h = default_graph_height;
*first_colour = 0;
*last_colour = 0;
*scale = 0;
*tempgrad = FALSE;
*showaslog = FALSE;
g->width = default_graph_width;
g->height = default_graph_height;
g->first_colour = 0;
g->last_colour = 0;
g->scale = 0;
g->tempgrad = FALSE;
g->showaslog = FALSE;
if (args) {
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) {
*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;
}
*scale = 0;
if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
g->scale = 0;
if (sscanf(args, "%d,%d %x %x", &g->height, &g->width, &g->first_colour, &g->last_colour) == 4) {
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);
}
*scale = 0;
if (sscanf(args, "%1023s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
g->scale = 0;
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);
}
buf[0] = '\0';
*h = 25;
*w = 0;
if (sscanf(args, "%x %x %u", first_colour, last_colour, scale) == 3) {
g->height = 25;
g->width = 0;
if (sscanf(args, "%x %x %u", &g->first_colour, &g->last_colour, &g->scale) == 3) {
return NULL;
}
*scale = 0;
if (sscanf(args, "%x %x", first_colour, last_colour) == 2) {
g->scale = 0;
if (sscanf(args, "%x %x", &g->first_colour, &g->last_colour) == 2) {
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);
}
*scale = 0;
if (sscanf(args, "%1023s %x %x", buf, first_colour, last_colour) == 3) {
g->scale = 0;
if (sscanf(args, "%1023s %x %x", buf, &g->first_colour, &g->last_colour) == 3) {
return strndup(buf, text_buffer_size);
}
buf[0] = '\0';
*first_colour = 0;
*last_colour = 0;
if (sscanf(args, "%d,%d %u", h, w, scale) == 3) {
g->first_colour = 0;
g->last_colour = 0;
if (sscanf(args, "%d,%d %u", &g->height, &g->width, &g->scale) == 3) {
return NULL;
}
*scale = 0;
if (sscanf(args, "%d,%d", h, w) == 2) {
g->scale = 0;
if (sscanf(args, "%d,%d", &g->height, &g->width) == 2) {
return NULL;
}
if (sscanf(args, "%1023s %d,%d %u", buf, h, w, scale) < 4) {
*scale = 0;
if (sscanf(args, "%1023s %d,%d %u", buf, &g->height, &g->width, &g->scale) < 4) {
g->scale = 0;
//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);
}
@ -280,7 +291,7 @@ static void graph_append(struct special_t *graph, double f, char showaslog)
f = log10(f + 1);
#endif
}
if (!graph->scaled && 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,
unsigned int second_colour, double i, int scale, int append, char showaslog, char tempgrad)
void new_graph(struct text_object *obj, char *buf, double val)
{
struct special_t *s = 0;
struct graph *g = obj->special_data;
if ((output_methods & TO_X) == 0)
return;
if (!g)
return;
s = new_special(buf, GRAPH);
s->width = w;
s->width = g->width;
if (s->graph == NULL) {
if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
// 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));
s->graph_scale = 100;
}
s->height = h;
s->first_colour = adjust_colours(first_colour);
s->last_colour = adjust_colours(second_colour);
if (scale != 0) {
s->height = g->height;
s->first_colour = adjust_colours(g->first_colour);
s->last_colour = adjust_colours(g->last_colour);
if (g->scale != 0) {
s->scaled = 0;
s->graph_scale = scale;
s->graph_scale = g->scale;
s->show_scale = 0;
} else {
s->scaled = 1;
s->graph_scale = 1;
s->show_scale = 1;
}
s->tempgrad = tempgrad;
s->tempgrad = g->tempgrad;
/* if (s->width) {
s->graph_width = s->width - 2; // subtract 2 for rectangle around
} */
#ifdef MATH
if (showaslog) {
if (g->showaslog) {
s->graph_scale = log10(s->graph_scale + 1);
}
#endif
if (append) {
graph_append(s, i, showaslog);
}
graph_append(s, val, g->showaslog);
}
void new_hr(char *buf, int a)

View File

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