Fix for segfault in top_name stuff.
[monky] / src / specials.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30 #include "conky.h"
31 #include "colours.h"
32 #ifdef X11
33 #include "fonts.h"
34 #endif /* X11 */
35 #include "logging.h"
36 #include "specials.h"
37 #include <math.h>
38
39 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
40 int max_specials = MAX_SPECIALS_DEFAULT;
41
42 /* create specials array on heap instead of stack with introduction of
43  * max_specials */
44 struct special_t *specials = NULL;
45
46 int special_count;
47
48 int default_bar_width = 0, default_bar_height = 6;
49 #ifdef X11
50 int default_graph_width = 0, default_graph_height = 25;
51 #endif /* X11 */
52 int default_gauge_width = 40, default_gauge_height = 25;
53
54 /*
55  * Special data typedefs
56  */
57
58 struct bar {
59         int width, height;
60 };
61
62 struct gauge {
63         int width, height;
64 };
65
66 struct graph {
67         int width, height;
68         unsigned int first_colour, last_colour;
69         unsigned int scale, showaslog;
70         char tempgrad;
71 };
72
73 struct stippled_hr {
74         int height, arg;
75 };
76
77 struct tab {
78         int width, arg;
79 };
80
81 /*
82  * Scanning arguments to various special text objects
83  */
84
85 const char *scan_gauge(struct text_object *obj, const char *args)
86 {
87         struct gauge *g;
88
89         g = malloc(sizeof(struct gauge));
90         memset(g, 0, sizeof(struct gauge));
91
92         /*width and height*/
93         g->width = default_gauge_width;
94         g->height = default_gauge_height;
95
96         /* gauge's argument is either height or height,width */
97         if (args) {
98                 int n = 0;
99
100                 if (sscanf(args, "%d,%d %n", &g->height, &g->width, &n) <= 1) {
101                         if (sscanf(args, "%d %n", &g->height, &n) == 2) {
102                                 g->width = g->height; /*square gauge*/
103                         }
104                 }
105                 args += n;
106         }
107
108         obj->special_data = g;
109         return args;
110 }
111
112 const char *scan_bar(struct text_object *obj, const char *args)
113 {
114         struct bar *b;
115
116         b = malloc(sizeof(struct bar));
117         memset(b, 0, sizeof(struct bar));
118
119         /* zero width means all space that is available */
120         b->width = default_bar_width;
121         b->height = default_bar_height;
122         /* bar's argument is either height or height,width */
123         if (args) {
124                 int n = 0;
125
126                 if (sscanf(args, "%d,%d %n", &b->height, &b->width, &n) <= 1) {
127                         sscanf(args, "%d %n", &b->height, &n);
128                 }
129                 args += n;
130         }
131
132         obj->special_data = b;
133         return args;
134 }
135
136 #ifdef X11
137 char *scan_font(const char *args)
138 {
139         if (args && *args) {
140                 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
141         }
142
143         return NULL;
144 }
145
146 char *scan_graph(struct text_object *obj, const char *args, int defscale)
147 {
148         struct graph *g;
149         char buf[1024];
150         memset(buf, 0, 1024);
151
152         g = malloc(sizeof(struct graph));
153         memset(g, 0, sizeof(struct graph));
154         obj->special_data = g;
155
156         /* zero width means all space that is available */
157         g->width = default_graph_width;
158         g->height = default_graph_height;
159         g->first_colour = 0;
160         g->last_colour = 0;
161         g->scale = defscale;
162         g->tempgrad = FALSE;
163         g->showaslog = FALSE;
164         if (args) {
165                 if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) {
166                         g->tempgrad = TRUE;
167                 }
168                 if (strstr(args, " "LOGGRAPH) || strncmp(args, LOGGRAPH, strlen(LOGGRAPH)) == 0) {
169                         g->showaslog = TRUE;
170                 }
171                 if (sscanf(args, "%d,%d %x %x %u", &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 5) {
172                         return NULL;
173                 }
174                 g->scale = defscale;
175                 if (sscanf(args, "%d,%d %x %x", &g->height, &g->width, &g->first_colour, &g->last_colour) == 4) {
176                         return NULL;
177                 }
178                 if (sscanf(args, "%1023s %d,%d %x %x %u", buf, &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 6) {
179                         return strndup(buf, text_buffer_size);
180                 }
181                 g->scale = defscale;
182                 if (sscanf(args, "%1023s %d,%d %x %x", buf, &g->height, &g->width, &g->first_colour, &g->last_colour) == 5) {
183                         return strndup(buf, text_buffer_size);
184                 }
185                 buf[0] = '\0';
186                 g->height = 25;
187                 g->width = 0;
188                 if (sscanf(args, "%x %x %u", &g->first_colour, &g->last_colour, &g->scale) == 3) {
189                         return NULL;
190                 }
191                 g->scale = defscale;
192                 if (sscanf(args, "%x %x", &g->first_colour, &g->last_colour) == 2) {
193                         return NULL;
194                 }
195                 if (sscanf(args, "%1023s %x %x %u", buf, &g->first_colour, &g->last_colour, &g->scale) == 4) {
196                         return strndup(buf, text_buffer_size);
197                 }
198                 g->scale = defscale;
199                 if (sscanf(args, "%1023s %x %x", buf, &g->first_colour, &g->last_colour) == 3) {
200                         return strndup(buf, text_buffer_size);
201                 }
202                 buf[0] = '\0';
203                 g->first_colour = 0;
204                 g->last_colour = 0;
205                 if (sscanf(args, "%d,%d %u", &g->height, &g->width, &g->scale) == 3) {
206                         return NULL;
207                 }
208                 g->scale = defscale;
209                 if (sscanf(args, "%d,%d", &g->height, &g->width) == 2) {
210                         return NULL;
211                 }
212                 if (sscanf(args, "%1023s %d,%d %u", buf, &g->height, &g->width, &g->scale) < 4) {
213                         g->scale = defscale;
214                         //TODO: check the return value and throw an error?
215                         sscanf(args, "%1023s %d,%d", buf, &g->height, &g->width);
216                 }
217
218                 /* escape quotes at end in case of execgraph */
219                 if (*buf == '"') {
220                         char *_ptr;
221                         size_t _size;
222                         if (_ptr = strrchr(args, '"')) {
223                                 _size = _ptr - args - 1;
224                         }
225                         _size = _size < 1024 ? _size : 1023;
226                         strncpy(buf, args + 1, _size);
227                         buf[_size] = 0;
228                 }
229
230 #undef g
231
232                 return strndup(buf, text_buffer_size);
233         }
234
235         if (buf[0] == '\0') {
236                 return NULL;
237         } else {
238                 return strndup(buf, text_buffer_size);
239         }
240 }
241 #endif /* X11 */
242
243 /*
244  * Printing various special text objects
245  */
246
247 static struct special_t *new_special(char *buf, enum special_types t)
248 {
249         if (special_count >= max_specials) {
250                 CRIT_ERR(NULL, NULL, "too many special things in text");
251         }
252
253         buf[0] = SPECIAL_CHAR;
254         buf[1] = '\0';
255         specials[special_count].type = t;
256         return &specials[special_count++];
257 }
258
259 void new_gauge_in_shell(struct text_object *obj, char *p, int p_max_size, int usage)
260 {
261         static const char *gaugevals[] = { "_. ", "\\. ", " | ", " ./", " ._" };
262         (void)obj;
263
264         snprintf(p, p_max_size, "%s", gaugevals[round_to_int((double)usage * 4 / 255)]);
265 }
266
267 #ifdef X11
268 void new_gauge_in_x11(struct text_object *obj, char *buf, int usage)
269 {
270         struct special_t *s = 0;
271         struct gauge *g = obj->special_data;
272
273         if ((output_methods & TO_X) == 0)
274                 return;
275
276         if (!g)
277                 return;
278
279         s = new_special(buf, GAUGE);
280
281         s->arg = usage;
282         s->width = g->width;
283         s->height = g->height;
284 }
285 #endif /* X11 */
286
287 void new_gauge(struct text_object *obj, char *p, int p_max_size, int usage)
288 {
289         if (!p_max_size)
290                 return;
291
292         usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
293
294 #ifdef X11
295         if (output_methods & TO_X)
296                 new_gauge_in_x11(obj, p, usage);
297         else
298 #endif /* X11 */
299                 new_gauge_in_shell(obj, p, p_max_size, usage);
300 }
301
302 #ifdef X11
303 void new_font(char *buf, char *args)
304 {
305         if ((output_methods & TO_X) == 0)
306                 return;
307
308         if (args) {
309                 struct special_t *s = new_special(buf, FONT);
310
311                 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
312                         int tmp = selected_font;
313
314                         selected_font = s->font_added = add_font(args);
315                         selected_font = tmp;
316                 }
317         } else {
318                 struct special_t *s = new_special(buf, FONT);
319                 int tmp = selected_font;
320
321                 selected_font = s->font_added = 0;
322                 selected_font = tmp;
323         }
324 }
325
326 static void graph_append(struct special_t *graph, double f, char showaslog)
327 {
328         int i;
329
330         if (showaslog) {
331 #ifdef MATH
332                 f = log10(f + 1);
333 #endif
334         }
335
336         if (!graph->scaled && f > graph->graph_scale) {
337                 f = graph->graph_scale;
338         }
339
340         /* shift all the data by 1 */
341         for (i = graph->graph_width - 1; i > 0; i--) {
342                 graph->graph[i] = graph->graph[i - 1];
343                 if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) {
344                         /* check if we need to update the scale */
345                         graph->graph_scale = graph->graph[i - 1];
346                 }
347         }
348         graph->graph[0] = f;    /* add new data */
349         if (graph->scaled && graph->graph[0] > graph->graph_scale) {
350                 /* check if we need to update the scale */
351                 graph->graph_scale = graph->graph[0];
352         }
353 }
354
355 void new_graph(struct text_object *obj, char *buf, int buf_max_size, double val)
356 {
357         struct special_t *s = 0;
358         struct graph *g = obj->special_data;
359
360         if ((output_methods & TO_X) == 0)
361                 return;
362
363         if (!g || !buf_max_size)
364                 return;
365
366         s = new_special(buf, GRAPH);
367
368         s->width = g->width;
369         if (s->graph == NULL) {
370                 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
371                         // subtract 2 for the box
372                         s->graph_width = s->width /* - 2 */;
373                 } else {
374                         s->graph_width = MAX_GRAPH_DEPTH - 2;
375                 }
376                 s->graph = malloc(s->graph_width * sizeof(double));
377                 memset(s->graph, 0, s->graph_width * sizeof(double));
378                 s->graph_scale = 100;
379         }
380         s->height = g->height;
381         s->first_colour = adjust_colours(g->first_colour);
382         s->last_colour = adjust_colours(g->last_colour);
383         if (g->scale != 0) {
384                 s->scaled = 0;
385                 s->graph_scale = g->scale;
386                 s->show_scale = 0;
387         } else {
388                 s->scaled = 1;
389                 s->graph_scale = 1;
390                 s->show_scale = 1;
391         }
392         s->tempgrad = g->tempgrad;
393         /* if (s->width) {
394                 s->graph_width = s->width - 2;  // subtract 2 for rectangle around
395         } */
396 #ifdef MATH
397         if (g->showaslog) {
398                 s->graph_scale = log10(s->graph_scale + 1);
399         }
400 #endif
401         graph_append(s, val, g->showaslog);
402 }
403
404 void new_hr(char *buf, int a)
405 {
406         if ((output_methods & TO_X) == 0)
407                 return;
408
409         new_special(buf, HORIZONTAL_LINE)->height = a;
410 }
411
412 void scan_stippled_hr(struct text_object *obj, const char *arg)
413 {
414         struct stippled_hr *sh;
415
416         sh = malloc(sizeof(struct stippled_hr));
417         memset(sh, 0, sizeof(struct stippled_hr));
418
419         sh->arg = get_stippled_borders();
420         sh->height = 1;
421
422         if (arg) {
423                 if (sscanf(arg, "%d %d", &sh->arg, &sh->height) != 2) {
424                         sscanf(arg, "%d", &sh->height);
425                 }
426         }
427         if (sh->arg <= 0) {
428                 sh->arg = 1;
429         }
430         obj->special_data = sh;
431 }
432
433 void new_stippled_hr(struct text_object *obj, char *buf)
434 {
435         struct special_t *s = 0;
436         struct stippled_hr *sh = obj->special_data;
437
438         if ((output_methods & TO_X) == 0)
439                 return;
440
441         if (!sh)
442                 return;
443
444         s = new_special(buf, STIPPLED_HR);
445
446         s->height = sh->height;
447         s->arg = sh->arg;
448 }
449 #endif /* X11 */
450
451 void new_fg(char *buf, long c)
452 {
453 #ifdef X11
454         if (output_methods & TO_X)
455                 new_special(buf, FG)->arg = c;
456 #endif /* X11 */
457 #ifdef NCURSES
458         if (output_methods & TO_NCURSES)
459                 new_special(buf, FG)->arg = c;
460 #endif /* NCURSES */
461         UNUSED(buf);
462         UNUSED(c);
463 }
464
465 #ifdef X11
466 void new_bg(char *buf, long c)
467 {
468         if ((output_methods & TO_X) == 0)
469                 return;
470
471         new_special(buf, BG)->arg = c;
472 }
473 #endif /* X11 */
474
475 static void new_bar_in_shell(struct text_object *obj, char* buffer, int buf_max_size, double usage)
476 {
477         struct bar *b = obj->special_data;
478         int width, i, scaledusage;
479
480         if (!b)
481                 return;
482
483         width = b->width;
484         if (!width)
485                 width = DEFAULT_BAR_WIDTH_NO_X;
486
487         if (width > buf_max_size)
488                 width = buf_max_size;
489
490         scaledusage = round_to_int( usage * width / 255);
491
492         for (i = 0; i < scaledusage; i++)
493                 buffer[i] = '#';
494
495         for (; i < width; i++)
496                 buffer[i] = '_';
497
498         buffer[i] = 0;
499 }
500
501 #ifdef X11
502 static void new_bar_in_x11(struct text_object *obj, char *buf, int usage)
503 {
504         struct special_t *s = 0;
505         struct bar *b = obj->special_data;
506
507         s = new_special(buf, BAR);
508
509         s->arg = usage;
510         s->width = b ?  b->width : default_bar_width;
511         s->height = b ? b->height : default_bar_height;
512 }
513 #endif /* X11 */
514
515 /* usage is in range [0,255] */
516 void new_bar(struct text_object *obj, char *p, int p_max_size, int usage)
517 {
518         if (!p_max_size)
519                 return;
520
521         usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
522
523 #ifdef X11
524         if ((output_methods & TO_X))
525                 new_bar_in_x11(obj, p, usage);
526         else
527 #endif /* X11 */
528                 new_bar_in_shell(obj, p, p_max_size, usage);
529 }
530
531 void new_outline(char *buf, long c)
532 {
533         new_special(buf, OUTLINE)->arg = c;
534 }
535
536 void new_offset(char *buf, long c)
537 {
538         new_special(buf, OFFSET)->arg = c;
539 }
540
541 void new_voffset(char *buf, long c)
542 {
543         new_special(buf, VOFFSET)->arg = c;
544 }
545
546 void new_alignr(char *buf, long c)
547 {
548         new_special(buf, ALIGNR)->arg = c;
549 }
550
551 // A postive offset pushes the text further left
552 void new_alignc(char *buf, long c)
553 {
554         new_special(buf, ALIGNC)->arg = c;
555 }
556
557 void new_goto(char *buf, long c)
558 {
559         new_special(buf, GOTO)->arg = c;
560 }
561
562 void scan_tab(struct text_object *obj, const char *arg)
563 {
564         struct tab *t;
565
566         t = malloc(sizeof(struct tab));
567         memset(t, 0, sizeof(struct tab));
568
569         t->width = 10;
570         t->arg = 0;
571
572         if (arg) {
573                 if (sscanf(arg, "%d %d", &t->width, &t->arg) != 2) {
574                         sscanf(arg, "%d", &t->arg);
575                 }
576         }
577         if (t->width <= 0) {
578                 t->width = 1;
579         }
580         obj->special_data = t;
581 }
582
583 void new_tab(struct text_object *obj, char *buf)
584 {
585         struct special_t *s = 0;
586         struct tab *t = obj->special_data;
587
588         if (!t)
589                 return;
590
591         s = new_special(buf, TAB);
592         s->width = t->width;
593         s->arg = t->arg;
594 }