Fix some minor graph related bugs.
[monky] / src / specials.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  */
27 #include "conky.h"
28 #include "colours.h"
29 #ifdef X11
30 #include "fonts.h"
31 #endif
32 #include "logging.h"
33 #include "specials.h"
34 #include <math.h>
35
36 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
37 unsigned int max_specials = MAX_SPECIALS_DEFAULT;
38
39 /* create specials array on heap instead of stack with introduction of
40  * max_specials */
41 struct special_t *specials = NULL;
42
43 unsigned int special_count;
44
45 int default_bar_width = 0, default_bar_height = 6;
46 #ifdef X11
47 int default_graph_width = 0, default_graph_height = 25;
48 int default_gauge_width = 40, default_gauge_height = 25;
49 #endif
50
51 /*
52  * Scanning arguments to various special text objects
53  */
54
55 #ifdef X11
56 const char *scan_gauge(const char *args, int *w, int *h)
57 {
58         /*width and height*/
59         *w = default_gauge_width;
60         *h = default_gauge_height;
61
62         /* gauge's argument is either height or height,width */
63         if (args) {
64                 int n = 0;
65
66                 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
67                         if (sscanf(args, "%d %n", h, &n) == 2) {
68                                 *w = *h; /*square gauge*/
69                         }
70                 }
71                 args += n;
72         }
73
74         return args;
75 }
76
77 const char *scan_bar(const char *args, int *w, int *h)
78 {
79         /* zero width means all space that is available */
80         *w = default_bar_width;
81         *h = default_bar_height;
82         /* bar's argument is either height or height,width */
83         if (args) {
84                 int n = 0;
85
86                 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
87                         sscanf(args, "%d %n", h, &n);
88                 }
89                 args += n;
90         }
91
92         return args;
93 }
94
95 char *scan_font(const char *args)
96 {
97         if (args && *args) {
98                 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
99         }
100
101         return NULL;
102 }
103
104 char *scan_graph(const char *args, int *w, int *h,
105                  unsigned int *first_colour, unsigned int *last_colour,
106                  unsigned int *scale, char *showaslog, char *tempgrad)
107 {
108         const char *nographtype;
109         char buf[64];
110         buf[0] = 0;
111
112         /* zero width means all space that is available */
113         *w = default_graph_width;
114         *h = default_graph_height;
115         *first_colour = 0;
116         *last_colour = 0;
117         *scale = 0;
118         *tempgrad = FALSE;
119         if (args) {
120                 // set showaslog and place the rest of the args in nographtype
121                 if (strcasecmp(args, LOGGRAPH) == EQUAL) {
122                         *showaslog = TRUE;
123                         return NULL;
124                 } else if (strcasecmp(args, NORMGRAPH) == EQUAL) {
125                         *showaslog = FALSE;
126                         return NULL;
127                 } else if (strncasecmp(args, LOGGRAPH" ", strlen(LOGGRAPH) + 1 ) == EQUAL) {
128                         *showaslog = TRUE;
129                         nographtype = &args[strlen(LOGGRAPH) + 1];
130                 } else if (strncasecmp(args, NORMGRAPH" ", strlen(NORMGRAPH) + 1 ) == EQUAL) {
131                         *showaslog = FALSE;
132                         nographtype = &args[strlen(NORMGRAPH) + 1];
133                 } else {
134                         *showaslog = FALSE;
135                         nographtype = args;
136                 }
137                 if (strstr(args, " "TEMPGRAD)) {
138                         *tempgrad = TRUE;
139                 }
140                 DBGP("printing graph as %s, other args are: %s", (*showaslog ? "log" : "normal"), nographtype);
141                 //check the rest of the args
142                 if (sscanf(nographtype, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
143                         return NULL;
144                 }
145                 *scale = 0;
146                 if (sscanf(nographtype, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
147                         return NULL;
148                 }
149                 if (sscanf(nographtype, "%63s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) {
150                         return strndup(buf, text_buffer_size);
151                 }
152                 *scale = 0;
153                 if (sscanf(nographtype, "%63s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
154                         return strndup(buf, text_buffer_size);
155                 }
156                 buf[0] = '\0';
157                 *h = 25;
158                 *w = 0;
159                 if (sscanf(nographtype, "%x %x %u", first_colour, last_colour, scale) == 3) {
160                         return NULL;
161                 }
162                 *scale = 0;
163                 if (sscanf(nographtype, "%x %x", first_colour, last_colour) == 2) {
164                         return NULL;
165                 }
166                 if (sscanf(nographtype, "%63s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
167                         return strndup(buf, text_buffer_size);
168                 }
169                 *scale = 0;
170                 if (sscanf(nographtype, "%63s %x %x", buf, first_colour, last_colour) == 3) {
171                         return strndup(buf, text_buffer_size);
172                 }
173                 buf[0] = '\0';
174                 *first_colour = 0;
175                 *last_colour = 0;
176                 if (sscanf(nographtype, "%d,%d %u", h, w, scale) == 3) {
177                         return NULL;
178                 }
179                 *scale = 0;
180                 if (sscanf(nographtype, "%d,%d", h, w) == 2) {
181                         return NULL;
182                 }
183                 if (sscanf(nographtype, "%63s %d,%d %u", buf, h, w, scale) < 4) {
184                         *scale = 0;
185                         //TODO: check the return value and throw an error?
186                         sscanf(nographtype, "%63s %d,%d", buf, h, w);
187                 }
188
189                 return strndup(buf, text_buffer_size);
190         }
191
192         if (buf[0] == '\0') {
193                 return NULL;
194         } else {
195                 return strndup(buf, text_buffer_size);
196         }
197 }
198 #endif
199
200 /*
201  * Printing various special text objects
202  */
203
204 static struct special_t *new_special(char *buf, enum special_types t)
205 {
206         if (special_count >= max_specials) {
207                 CRIT_ERR("too many special things in text");
208         }
209
210         buf[0] = SPECIAL_CHAR;
211         buf[1] = '\0';
212         specials[special_count].type = t;
213         return &specials[special_count++];
214 }
215
216 #ifdef X11
217 void new_gauge(char *buf, int w, int h, int usage)
218 {
219         struct special_t *s = 0;
220         if ((output_methods & TO_X) == 0)
221                 return;
222
223         s = new_special(buf, GAUGE);
224
225         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
226         s->width = w;
227         s->height = h;
228 }
229
230 void new_bar(char *buf, int w, int h, int usage)
231 {
232         struct special_t *s = 0;
233
234         if ((output_methods & TO_X) == 0)
235                 return;
236
237         s = new_special(buf, BAR);
238
239         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
240         s->width = w;
241         s->height = h;
242 }
243
244 void new_font(char *buf, char *args)
245 {
246         if ((output_methods & TO_X) == 0)
247                 return;
248
249         if (args) {
250                 struct special_t *s = new_special(buf, FONT);
251
252                 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
253                         int tmp = selected_font;
254
255                         selected_font = s->font_added = addfont(args);
256                         load_fonts();
257                         selected_font = tmp;
258                 }
259         } else {
260                 struct special_t *s = new_special(buf, FONT);
261                 int tmp = selected_font;
262
263                 selected_font = s->font_added = 0;
264                 selected_font = tmp;
265         }
266 }
267
268 static void graph_append(struct special_t *graph, double f, char showaslog)
269 {
270         int i;
271
272         if (showaslog) {
273 #ifdef MATH
274                 f = log10(f + 1);
275 #endif
276         }
277         
278         if (!graph->scaled && f > graph->graph_scale) {
279                 f = graph->graph_scale;
280         }
281
282 /* Already happens in new_graph
283         if (graph->scaled) {
284                 graph->graph_scale = 1;
285         }
286 */
287         graph->graph[0] = f;    /* add new data */
288         /* shift all the data by 1 */
289         for (i = graph->graph_width - 1; i > 0; i--) {
290                 graph->graph[i] = graph->graph[i - 1];
291                 if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) {
292                         /* check if we need to update the scale */
293                         graph->graph_scale = graph->graph[i - 1];
294                 }
295         }
296         if (graph->scaled && graph->graph[graph->graph_width] > graph->graph_scale) {
297                 /* check if we need to update the scale */
298                 graph->graph_scale = graph->graph[graph->graph_width];
299         }
300 }
301
302 void new_graph(char *buf, int w, int h, unsigned int first_colour,
303                 unsigned int second_colour, double i, int scale, int append, char showaslog, char tempgrad)
304 {
305         struct special_t *s = 0;
306
307         if ((output_methods & TO_X) == 0)
308                 return;
309
310         s = new_special(buf, GRAPH);
311
312         s->width = w;
313         if (s->graph == NULL) {
314                 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
315                         // subtract 2 for the box
316                         s->graph_width = s->width /* - 2 */;
317                 } else {
318                         s->graph_width = MAX_GRAPH_DEPTH - 2;
319                 }
320                 s->graph = malloc(s->graph_width * sizeof(double));
321                 memset(s->graph, 0, s->graph_width * sizeof(double));
322                 s->graph_scale = 100;
323         }
324         s->height = h;
325         s->first_colour = adjust_colors(first_colour);
326         s->last_colour = adjust_colors(second_colour);
327         if (scale != 0) {
328                 s->scaled = 0;
329                 s->graph_scale = scale;
330                 s->show_scale = 0;
331         } else {
332                 s->scaled = 1;
333                 s->graph_scale = 1;
334                 s->show_scale = 1;
335         }
336         s->tempgrad = tempgrad;
337         /* if (s->width) {
338                 s->graph_width = s->width - 2;  // subtract 2 for rectangle around
339         } */
340         if (showaslog) {
341 #ifdef MATH
342                 s->graph_scale = log10(s->graph_scale + 1);
343 #endif
344         }
345         if (append) {
346                 graph_append(s, i, showaslog);
347         }
348 }
349
350 void new_hr(char *buf, int a)
351 {
352         if ((output_methods & TO_X) == 0)
353                 return;
354
355         new_special(buf, HORIZONTAL_LINE)->height = a;
356 }
357
358 void new_stippled_hr(char *buf, int a, int b)
359 {
360         struct special_t *s = 0;
361
362         if ((output_methods & TO_X) == 0)
363                 return;
364
365         s = new_special(buf, STIPPLED_HR);
366
367         s->height = b;
368         s->arg = a;
369 }
370
371 void new_fg(char *buf, long c)
372 {
373         if ((output_methods & TO_X) == 0)
374                 return;
375
376         new_special(buf, FG)->arg = c;
377 }
378
379 void new_bg(char *buf, long c)
380 {
381         if ((output_methods & TO_X) == 0)
382                 return;
383
384         new_special(buf, BG)->arg = c;
385 }
386 #endif
387
388 void new_outline(char *buf, long c)
389 {
390         new_special(buf, OUTLINE)->arg = c;
391 }
392
393 void new_offset(char *buf, long c)
394 {
395         new_special(buf, OFFSET)->arg = c;
396 }
397
398 void new_voffset(char *buf, long c)
399 {
400         new_special(buf, VOFFSET)->arg = c;
401 }
402
403 void new_alignr(char *buf, long c)
404 {
405         new_special(buf, ALIGNR)->arg = c;
406 }
407
408 // A postive offset pushes the text further left
409 void new_alignc(char *buf, long c)
410 {
411         new_special(buf, ALIGNC)->arg = c;
412 }
413
414 void new_goto(char *buf, long c)
415 {
416         new_special(buf, GOTO)->arg = c;
417 }
418
419 void new_tab(char *buf, int a, int b)
420 {
421         struct special_t *s = new_special(buf, TAB);
422
423         s->width = a;
424         s->arg = b;
425 }
426