reload_config() fix, fix default net graph args.
[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 /* X11 */
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 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 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 /* X11 */
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 #endif /* X11 */
77
78 const char *scan_bar(const char *args, int *w, int *h)
79 {
80         /* zero width means all space that is available */
81         *w = default_bar_width;
82         *h = default_bar_height;
83         /* bar's argument is either height or height,width */
84         if (args) {
85                 int n = 0;
86
87                 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
88                         sscanf(args, "%d %n", h, &n);
89                 }
90                 args += n;
91         }
92
93         return args;
94 }
95
96 #ifdef X11
97 char *scan_font(const char *args)
98 {
99         if (args && *args) {
100                 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
101         }
102
103         return NULL;
104 }
105
106 char *scan_graph(const char *args, int *w, int *h,
107                  unsigned int *first_colour, unsigned int *last_colour,
108                  unsigned int *scale, char *showaslog, char *tempgrad)
109 {
110         const char *nographtype;
111         char buf[64];
112         buf[0] = 0;
113
114         /* zero width means all space that is available */
115         *w = default_graph_width;
116         *h = default_graph_height;
117         *first_colour = 0;
118         *last_colour = 0;
119         *scale = 0;
120         *tempgrad = FALSE;
121         if (args) {
122                 // set showaslog and place the rest of the args in nographtype
123                 if (strcasecmp(args, LOGGRAPH) == EQUAL) {
124                         *showaslog = TRUE;
125                         return NULL;
126                 } else if (strcasecmp(args, NORMGRAPH) == EQUAL) {
127                         *showaslog = FALSE;
128                         return NULL;
129                 } else if (strncasecmp(args, LOGGRAPH" ", strlen(LOGGRAPH) + 1 ) == EQUAL) {
130                         *showaslog = TRUE;
131                         nographtype = &args[strlen(LOGGRAPH) + 1];
132                 } else if (strncasecmp(args, NORMGRAPH" ", strlen(NORMGRAPH) + 1 ) == EQUAL) {
133                         *showaslog = FALSE;
134                         nographtype = &args[strlen(NORMGRAPH) + 1];
135                 } else {
136                         *showaslog = FALSE;
137                         nographtype = args;
138                 }
139                 if (strstr(args, " "TEMPGRAD)) {
140                         *tempgrad = TRUE;
141                 }
142                 DBGP("printing graph as %s, other args are: %s", (*showaslog ? "log" : "normal"), nographtype);
143                 //check the rest of the args
144                 if (sscanf(nographtype, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
145                         return NULL;
146                 }
147                 *scale = 0;
148                 if (sscanf(nographtype, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
149                         return NULL;
150                 }
151                 if (sscanf(nographtype, "%63s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) {
152                         return strndup(buf, text_buffer_size);
153                 }
154                 *scale = 0;
155                 if (sscanf(nographtype, "%63s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
156                         return strndup(buf, text_buffer_size);
157                 }
158                 buf[0] = '\0';
159                 *h = 25;
160                 *w = 0;
161                 if (sscanf(nographtype, "%x %x %u", first_colour, last_colour, scale) == 3) {
162                         return NULL;
163                 }
164                 *scale = 0;
165                 if (sscanf(nographtype, "%x %x", first_colour, last_colour) == 2) {
166                         return NULL;
167                 }
168                 if (sscanf(nographtype, "%63s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
169                         return strndup(buf, text_buffer_size);
170                 }
171                 *scale = 0;
172                 if (sscanf(nographtype, "%63s %x %x", buf, first_colour, last_colour) == 3) {
173                         return strndup(buf, text_buffer_size);
174                 }
175                 buf[0] = '\0';
176                 *first_colour = 0;
177                 *last_colour = 0;
178                 if (sscanf(nographtype, "%d,%d %u", h, w, scale) == 3) {
179                         return NULL;
180                 }
181                 *scale = 0;
182                 if (sscanf(nographtype, "%d,%d", h, w) == 2) {
183                         return NULL;
184                 }
185                 if (sscanf(nographtype, "%63s %d,%d %u", buf, h, w, scale) < 4) {
186                         *scale = 0;
187                         //TODO: check the return value and throw an error?
188                         sscanf(nographtype, "%63s %d,%d", buf, h, w);
189                 }
190
191                 return strndup(buf, text_buffer_size);
192         }
193
194         if (buf[0] == '\0') {
195                 return NULL;
196         } else {
197                 return strndup(buf, text_buffer_size);
198         }
199 }
200 #endif /* X11 */
201
202 /*
203  * Printing various special text objects
204  */
205
206 static struct special_t *new_special(char *buf, enum special_types t)
207 {
208         if (special_count >= max_specials) {
209                 CRIT_ERR("too many special things in text");
210         }
211
212         buf[0] = SPECIAL_CHAR;
213         buf[1] = '\0';
214         specials[special_count].type = t;
215         return &specials[special_count++];
216 }
217
218 #ifdef X11
219 void new_gauge(char *buf, int w, int h, int usage)
220 {
221         struct special_t *s = 0;
222         if ((output_methods & TO_X) == 0)
223                 return;
224
225         s = new_special(buf, GAUGE);
226
227         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
228         s->width = w;
229         s->height = h;
230 }
231
232 void new_bar(char *buf, int w, int h, int usage)
233 {
234         struct special_t *s = 0;
235
236         if ((output_methods & TO_X) == 0)
237                 return;
238
239         s = new_special(buf, BAR);
240
241         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
242         s->width = w;
243         s->height = h;
244 }
245
246 void new_font(char *buf, char *args)
247 {
248         if ((output_methods & TO_X) == 0)
249                 return;
250
251         if (args) {
252                 struct special_t *s = new_special(buf, FONT);
253
254                 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
255                         int tmp = selected_font;
256
257                         selected_font = s->font_added = add_font(args);
258                         selected_font = tmp;
259                 }
260         } else {
261                 struct special_t *s = new_special(buf, FONT);
262                 int tmp = selected_font;
263
264                 selected_font = s->font_added = 0;
265                 selected_font = tmp;
266         }
267 }
268
269 static void graph_append(struct special_t *graph, double f, char showaslog)
270 {
271         int i;
272
273 #ifdef MATH
274         if (showaslog) {
275                 f = log10(f + 1);
276         }
277 #endif
278         
279         if (!graph->scaled && f > graph->graph_scale) {
280                 f = graph->graph_scale;
281         }
282
283         graph->graph[0] = f;    /* add new data */
284         /* shift all the data by 1 */
285         for (i = graph->graph_width - 1; i > 0; i--) {
286                 graph->graph[i] = graph->graph[i - 1];
287                 if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) {
288                         /* check if we need to update the scale */
289                         graph->graph_scale = graph->graph[i - 1];
290                 }
291         }
292         if (graph->scaled && graph->graph[graph->graph_width] > graph->graph_scale) {
293                 /* check if we need to update the scale */
294                 graph->graph_scale = graph->graph[graph->graph_width];
295         }
296 }
297
298 void new_graph(char *buf, int w, int h, unsigned int first_colour,
299                 unsigned int second_colour, double i, int scale, int append, char showaslog, char tempgrad)
300 {
301         struct special_t *s = 0;
302
303         if ((output_methods & TO_X) == 0)
304                 return;
305
306         s = new_special(buf, GRAPH);
307
308         s->width = w;
309         if (s->graph == NULL) {
310                 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
311                         // subtract 2 for the box
312                         s->graph_width = s->width /* - 2 */;
313                 } else {
314                         s->graph_width = MAX_GRAPH_DEPTH - 2;
315                 }
316                 s->graph = malloc(s->graph_width * sizeof(double));
317                 memset(s->graph, 0, s->graph_width * sizeof(double));
318                 s->graph_scale = 100;
319         }
320         s->height = h;
321         s->first_colour = adjust_colours(first_colour);
322         s->last_colour = adjust_colours(second_colour);
323         if (scale != 0) {
324                 s->scaled = 0;
325                 s->graph_scale = scale;
326                 s->show_scale = 0;
327         } else {
328                 s->scaled = 1;
329                 s->graph_scale = 1;
330                 s->show_scale = 1;
331         }
332         s->tempgrad = tempgrad;
333         /* if (s->width) {
334                 s->graph_width = s->width - 2;  // subtract 2 for rectangle around
335         } */
336 #ifdef MATH
337         if (showaslog) {
338                 s->graph_scale = log10(s->graph_scale + 1);
339         }
340 #endif
341         if (append) {
342                 graph_append(s, i, showaslog);
343         }
344 }
345
346 void new_hr(char *buf, int a)
347 {
348         if ((output_methods & TO_X) == 0)
349                 return;
350
351         new_special(buf, HORIZONTAL_LINE)->height = a;
352 }
353
354 void new_stippled_hr(char *buf, int a, int b)
355 {
356         struct special_t *s = 0;
357
358         if ((output_methods & TO_X) == 0)
359                 return;
360
361         s = new_special(buf, STIPPLED_HR);
362
363         s->height = b;
364         s->arg = a;
365 }
366
367 void new_fg(char *buf, long c)
368 {
369         if ((output_methods & TO_X) == 0)
370                 return;
371
372         new_special(buf, FG)->arg = c;
373 }
374
375 void new_bg(char *buf, long c)
376 {
377         if ((output_methods & TO_X) == 0)
378                 return;
379
380         new_special(buf, BG)->arg = c;
381 }
382 #endif /* X11 */
383
384 void new_bar_in_shell(char* buffer, int buf_max_size, double usage, int width)
385 {
386         if(width<=buf_max_size){
387                 int i = 0, j = 0, scaledusage = round_to_int( usage * width / 100);
388
389                 #ifdef HAVE_OPENMP
390                 #pragma omp parallel for
391                 #endif /* HAVE_OPENMP */
392                 for(i=0; i<(int)scaledusage; i++) {
393                         *(buffer+i)='#';
394                 }
395                 /* gcc seems to think i is not initialized properly :/ */
396                 j = i;
397                 #ifdef HAVE_OPENMP
398                 #pragma omp parallel for
399                 #endif /* HAVE_OPENMP */
400                 for(i = j/* cheats */; i < width; i++) {
401                         *(buffer+i)='_';
402                 }
403                 *(buffer+i)=0;
404         }
405 }
406
407 void new_outline(char *buf, long c)
408 {
409         new_special(buf, OUTLINE)->arg = c;
410 }
411
412 void new_offset(char *buf, long c)
413 {
414         new_special(buf, OFFSET)->arg = c;
415 }
416
417 void new_voffset(char *buf, long c)
418 {
419         new_special(buf, VOFFSET)->arg = c;
420 }
421
422 void new_alignr(char *buf, long c)
423 {
424         new_special(buf, ALIGNR)->arg = c;
425 }
426
427 // A postive offset pushes the text further left
428 void new_alignc(char *buf, long c)
429 {
430         new_special(buf, ALIGNC)->arg = c;
431 }
432
433 void new_goto(char *buf, long c)
434 {
435         new_special(buf, GOTO)->arg = c;
436 }
437
438 void new_tab(char *buf, int a, int b)
439 {
440         struct special_t *s = new_special(buf, TAB);
441
442         s->width = a;
443         s->arg = b;
444 }
445