diff --git a/src/conky.cc b/src/conky.cc index 657977cd..b008e224 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -1526,6 +1526,7 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) w = current->width; if (w == 0) { w = text_start_x + text_width - cur_x - 1; + current->graph_width = w - 1; } if (w < 0) { w = 0; @@ -1539,48 +1540,53 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) XSetLineAttributes(display, window.gc, 1, LineSolid, CapButt, JoinMiter); - if (current->last_colour != 0 - || current->first_colour != 0) { - tmpcolour = do_gradient(w - 1, current->last_colour, current->first_colour); - } - colour_idx = 0; - for (i = w - 2; i > -1; i--) { - if (current->last_colour != 0 - || current->first_colour != 0) { - if (current->tempgrad) { + /* in case we don't have a graph yet */ + if (current->graph) { + + if (current->last_colour != 0 || current->first_colour != 0) { + tmpcolour = do_gradient(w - 1, + current->last_colour, current->first_colour); + } + colour_idx = 0; + for (i = w - 2; i > -1; i--) { + if (current->last_colour != 0 || current->first_colour != 0) { + if (current->tempgrad) { #ifdef DEBUG_lol - assert( - (int)((float)(w - 2) - current->graph[j] * - (w - 2) / (float)current->scale) - < w - 1 - ); - assert( - (int)((float)(w - 2) - current->graph[j] * - (w - 2) / (float)current->scale) - > -1 - ); - if (current->graph[j] == current->scale) { assert( (int)((float)(w - 2) - current->graph[j] * (w - 2) / (float)current->scale) - == 0 + < w-1 ); - } + assert( + (int)((float)(w - 2) - current->graph[j] * + (w - 2) / (float)current->scale) + > -1 + ); + if (current->graph[j] == current->scale) { + assert( + (int)((float)(w - 2) - current->graph[j] * + (w - 2) / (float)current->scale) + == 0 + ); + } #endif /* DEBUG_lol */ - set_foreground_color(tmpcolour[ - (int)((float)(w - 2) - current->graph[j] * - (w - 2) / (float)current->scale) - ]); - } else { - set_foreground_color(tmpcolour[colour_idx++]); + set_foreground_color(tmpcolour[ + (int)((float)(w - 2) - + current->graph[j] * (w - 2) / + (float)current->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 - current->graph[j] * + (h - 1) / current->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 - current->graph[j] * - (h - 1) / current->scale)); - j++; + if (tmpcolour) free(tmpcolour); } free_and_zero(tmpcolour); if (h > cur_y_add diff --git a/src/specials.cc b/src/specials.cc index 08497f3c..e0e87d1f 100644 --- a/src/specials.cc +++ b/src/specials.cc @@ -381,6 +381,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); @@ -392,7 +395,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]; } graph->graph[0] = f; /* add new data */ @@ -421,10 +424,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 = (double*)malloc(s->width * sizeof(double)); - memset(s->graph, 0, s->width * sizeof(double)); - s->scale = 100; + if (s->width) s->graph_width = s->width; + + if (s->graph_width != s->graph_allocated) { + double *graph = static_cast(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->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 96a054c9..5b463d30 100644 --- a/src/specials.h +++ b/src/specials.h @@ -65,6 +65,8 @@ struct special_t { double *graph; double scale; /* maximum value */ short show_scale; + int graph_width; + int graph_allocated; int scaled; /* auto adjust maximum */ unsigned long first_colour; // for graph gradient unsigned long last_colour;