diff --git a/src/conky.c b/src/conky.c index 53e9d6d6..8f1e01ec 100644 --- a/src/conky.c +++ b/src/conky.c @@ -2994,6 +2994,7 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) w = specials[special_index].width; if (w == 0) { w = text_start_x + text_width - cur_x - 1; + specials[special_index].graph_width = w - 1; } if (w < 0) { w = 0; @@ -3007,52 +3008,58 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) XSetLineAttributes(display, window.gc, 1, LineSolid, CapButt, JoinMiter); - if (specials[special_index].last_colour != 0 - || specials[special_index].first_colour != 0) { - tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour); - } - colour_idx = 0; - for (i = w - 2; i > -1; i--) { + /* in case we don't have a graph yet */ + if (specials[special_index].graph) { + if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) { - if (specials[special_index].tempgrad) { + tmpcolour = do_gradient(w - 1, + specials[special_index].last_colour, + specials[special_index].first_colour); + } + colour_idx = 0; + for (i = w - 2; i > -1; i--) { + if (specials[special_index].last_colour != 0 + || specials[special_index].first_colour != 0) { + if (specials[special_index].tempgrad) { #ifdef DEBUG_lol - assert( - (int)((float)(w - 2) - specials[special_index].graph[j] * - (w - 2) / (float)specials[special_index].graph_scale) - < w - 1 - ); - assert( - (int)((float)(w - 2) - specials[special_index].graph[j] * - (w - 2) / (float)specials[special_index].graph_scale) - > -1 - ); - if (specials[special_index].graph[j] == specials[special_index].graph_scale) { assert( (int)((float)(w - 2) - specials[special_index].graph[j] * (w - 2) / (float)specials[special_index].graph_scale) - == 0 + < w - 1 ); - } + assert( + (int)((float)(w - 2) - specials[special_index].graph[j] * + (w - 2) / (float)specials[special_index].graph_scale) + > -1 + ); + if (specials[special_index].graph[j] == specials[special_index].graph_scale) { + assert( + (int)((float)(w - 2) - specials[special_index].graph[j] * + (w - 2) / (float)specials[special_index].graph_scale) + == 0 + ); + } #endif /* DEBUG_lol */ - set_foreground_color(tmpcolour[ - (int)((float)(w - 2) - - specials[special_index].graph[j] - * (w - 2) / - (float)specials[special_index].graph_scale) - ]); - } else { - set_foreground_color(tmpcolour[colour_idx++]); + set_foreground_color(tmpcolour[ + (int)((float)(w - 2) - + specials[special_index].graph[j] + * (w - 2) / + (float)specials[special_index].graph_scale) + ]); + } else { + set_foreground_color(tmpcolour[colour_idx++]); + } } + /* this is mugfugly, but it works */ + XDrawLine(display, window.drawable, window.gc, + cur_x + i + 1, by + h, cur_x + i + 1, + round_to_int((double)by + h - specials[special_index].graph[j] * + (h - 1) / specials[special_index].graph_scale)); + ++j; } - /* this is mugfugly, but it works */ - XDrawLine(display, window.drawable, window.gc, - cur_x + i + 1, by + h, cur_x + i + 1, - round_to_int((double)by + h - specials[special_index].graph[j] * - (h - 1) / specials[special_index].graph_scale)); - ++j; + if (tmpcolour) free(tmpcolour); } - if (tmpcolour) free(tmpcolour); if (h > cur_y_add && h > font_h) { cur_y_add = h; diff --git a/src/specials.c b/src/specials.c index 4aec9fdd..dc0dfa32 100644 --- a/src/specials.c +++ b/src/specials.c @@ -353,6 +353,9 @@ static void graph_append(struct special_t *graph, double f, char showaslog) { int i; + /* do nothing if we don't even have a graph yet */ + if (!graph->graph) return; + if (showaslog) { #ifdef MATH f = log10(f + 1); @@ -364,7 +367,7 @@ static void graph_append(struct special_t *graph, double f, char showaslog) } /* shift all the data by 1 */ - for (i = graph->width - 1; i > 0; i--) { + for (i = graph->graph_allocated - 1; i > 0; i--) { graph->graph[i] = graph->graph[i - 1]; if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) { /* check if we need to update the scale */ @@ -392,10 +395,25 @@ void new_graph(struct text_object *obj, char *buf, int buf_max_size, double val) s = new_special(buf, GRAPH); s->width = g->width; - if (s->graph == NULL) { - s->graph = malloc(s->width * sizeof(double)); - memset(s->graph, 0, s->width * sizeof(double)); - s->graph_scale = 100; + if (s->width) s->graph_width = s->width; + + if (s->graph_width != s->graph_allocated) { + char *graph = realloc(s->graph, s->graph_width * sizeof(double)); + DBGP("reallocing graph from %d to %d", s->graph_allocated, s->graph_width); + if (!s->graph) { + /* initialize */ + memset(graph, 0, s->graph_width * sizeof(double)); + s->graph_scale = 100; + } else { + if (s->graph_width > s->graph_allocated) { + /* initialize the new region */ + memset(graph + (s->graph_allocated * sizeof(double)), 0, + (s->graph_width - s->graph_allocated) * + sizeof(double)); + } + } + s->graph = graph; + s->graph_allocated = s->graph_width; } s->height = g->height; s->first_colour = adjust_colours(g->first_colour); diff --git a/src/specials.h b/src/specials.h index 0ae6dd18..dfa20fdd 100644 --- a/src/specials.h +++ b/src/specials.h @@ -64,6 +64,8 @@ struct special_t { double *graph; double graph_scale; short show_scale; + int graph_width; + int graph_allocated; int scaled; unsigned long first_colour; // for graph gradient unsigned long last_colour;