implement ASCII gauge and enable all gauge objects for use with no X
[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-2009 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 int default_gauge_width = 40, default_gauge_height = 25;
52 #endif /* X11 */
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 #ifdef X11
86 const char *scan_gauge(struct text_object *obj, const char *args)
87 {
88         struct gauge *g;
89
90         g = malloc(sizeof(struct gauge));
91         memset(g, 0, sizeof(struct gauge));
92
93         /*width and height*/
94         g->width = default_gauge_width;
95         g->height = default_gauge_height;
96
97         /* gauge's argument is either height or height,width */
98         if (args) {
99                 int n = 0;
100
101                 if (sscanf(args, "%d,%d %n", &g->height, &g->width, &n) <= 1) {
102                         if (sscanf(args, "%d %n", &g->height, &n) == 2) {
103                                 g->width = g->height; /*square gauge*/
104                         }
105                 }
106                 args += n;
107         }
108
109         obj->special_data = g;
110         return args;
111 }
112 #endif /* X11 */
113
114 const char *scan_bar(struct text_object *obj, const char *args)
115 {
116         struct bar *b;
117
118         b = malloc(sizeof(struct bar));
119         memset(b, 0, sizeof(struct bar));
120
121         /* zero width means all space that is available */
122         b->width = default_bar_width;
123         b->height = default_bar_height;
124         /* bar's argument is either height or height,width */
125         if (args) {
126                 int n = 0;
127
128                 if (sscanf(args, "%d,%d %n", &b->height, &b->width, &n) <= 1) {
129                         sscanf(args, "%d %n", &b->height, &n);
130                 }
131                 args += n;
132         }
133
134         obj->special_data = b;
135         return args;
136 }
137
138 #ifdef X11
139 char *scan_font(const char *args)
140 {
141         if (args && *args) {
142                 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
143         }
144
145         return NULL;
146 }
147
148 char *scan_graph(struct text_object *obj, const char *args, int defscale)
149 {
150         struct graph *g;
151         char buf[1024];
152         memset(buf, 0, 1024);
153
154         g = malloc(sizeof(struct graph));
155         memset(g, 0, sizeof(struct graph));
156         obj->special_data = g;
157
158         /* zero width means all space that is available */
159         g->width = default_graph_width;
160         g->height = default_graph_height;
161         g->first_colour = 0;
162         g->last_colour = 0;
163         g->scale = defscale;
164         g->tempgrad = FALSE;
165         g->showaslog = FALSE;
166         if (args) {
167                 if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) {
168                         g->tempgrad = TRUE;
169                 }
170                 if (strstr(args, " "LOGGRAPH) || strncmp(args, LOGGRAPH, strlen(LOGGRAPH)) == 0) {
171                         g->showaslog = TRUE;
172                 }
173                 if (sscanf(args, "%d,%d %x %x %u", &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 5) {
174                         return NULL;
175                 }
176                 g->scale = defscale;
177                 if (sscanf(args, "%d,%d %x %x", &g->height, &g->width, &g->first_colour, &g->last_colour) == 4) {
178                         return NULL;
179                 }
180                 if (sscanf(args, "%1023s %d,%d %x %x %u", buf, &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 6) {
181                         return strndup(buf, text_buffer_size);
182                 }
183                 g->scale = defscale;
184                 if (sscanf(args, "%1023s %d,%d %x %x", buf, &g->height, &g->width, &g->first_colour, &g->last_colour) == 5) {
185                         return strndup(buf, text_buffer_size);
186                 }
187                 buf[0] = '\0';
188                 g->height = 25;
189                 g->width = 0;
190                 if (sscanf(args, "%x %x %u", &g->first_colour, &g->last_colour, &g->scale) == 3) {
191                         return NULL;
192                 }
193                 g->scale = defscale;
194                 if (sscanf(args, "%x %x", &g->first_colour, &g->last_colour) == 2) {
195                         return NULL;
196                 }
197                 if (sscanf(args, "%1023s %x %x %u", buf, &g->first_colour, &g->last_colour, &g->scale) == 4) {
198                         return strndup(buf, text_buffer_size);
199                 }
200                 g->scale = defscale;
201                 if (sscanf(args, "%1023s %x %x", buf, &g->first_colour, &g->last_colour) == 3) {
202                         return strndup(buf, text_buffer_size);
203                 }
204                 buf[0] = '\0';
205                 g->first_colour = 0;
206                 g->last_colour = 0;
207                 if (sscanf(args, "%d,%d %u", &g->height, &g->width, &g->scale) == 3) {
208                         return NULL;
209                 }
210                 g->scale = defscale;
211                 if (sscanf(args, "%d,%d", &g->height, &g->width) == 2) {
212                         return NULL;
213                 }
214                 if (sscanf(args, "%1023s %d,%d %u", buf, &g->height, &g->width, &g->scale) < 4) {
215                         g->scale = defscale;
216                         //TODO: check the return value and throw an error?
217                         sscanf(args, "%1023s %d,%d", buf, &g->height, &g->width);
218                 }
219 #undef g
220
221                 return strndup(buf, text_buffer_size);
222         }
223
224         if (buf[0] == '\0') {
225                 return NULL;
226         } else {
227                 return strndup(buf, text_buffer_size);
228         }
229 }
230 #endif /* X11 */
231
232 /*
233  * Printing various special text objects
234  */
235
236 static struct special_t *new_special(char *buf, enum special_types t)
237 {
238         if (special_count >= max_specials) {
239                 CRIT_ERR(NULL, NULL, "too many special things in text");
240         }
241
242         buf[0] = SPECIAL_CHAR;
243         buf[1] = '\0';
244         specials[special_count].type = t;
245         return &specials[special_count++];
246 }
247
248 void new_gauge_in_shell(struct text_object *obj, char *p, int p_max_size, int usage)
249 {
250         static const char *gaugevals[] = { "_. ", "\\. ", " | ", " ./", " ._" };
251         (void)obj;
252
253         snprintf(p, p_max_size, "%s", gaugevals[round_to_int((double)usage * 4 / 255)]);
254 }
255
256 #ifdef X11
257 void new_gauge_in_x11(struct text_object *obj, char *buf, int usage)
258 {
259         struct special_t *s = 0;
260         struct gauge *g = obj->special_data;
261
262         if ((output_methods & TO_X) == 0)
263                 return;
264
265         if (!g)
266                 return;
267
268         s = new_special(buf, GAUGE);
269
270         s->arg = usage;
271         s->width = g->width;
272         s->height = g->height;
273 }
274 #endif /* X11 */
275
276 void new_gauge(struct text_object *obj, char *p, int p_max_size, int usage)
277 {
278         if (!p_max_size)
279                 return;
280
281         usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
282
283 #ifdef X11
284         if (output_methods & TO_X)
285                 new_gauge_in_x11(obj, p, usage);
286         else
287 #endif /* X11 */
288                 new_gauge_in_shell(obj, p, p_max_size, usage);
289 }
290
291 #ifdef X11
292 void new_font(char *buf, char *args)
293 {
294         if ((output_methods & TO_X) == 0)
295                 return;
296
297         if (args) {
298                 struct special_t *s = new_special(buf, FONT);
299
300                 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
301                         int tmp = selected_font;
302
303                         selected_font = s->font_added = add_font(args);
304                         selected_font = tmp;
305                 }
306         } else {
307                 struct special_t *s = new_special(buf, FONT);
308                 int tmp = selected_font;
309
310                 selected_font = s->font_added = 0;
311                 selected_font = tmp;
312         }
313 }
314
315 static void graph_append(struct special_t *graph, double f, char showaslog)
316 {
317         int i;
318
319         if (showaslog) {
320 #ifdef MATH
321                 f = log10(f + 1);
322 #endif
323         }
324
325         if (!graph->scaled && f > graph->graph_scale) {
326                 f = graph->graph_scale;
327         }
328
329         graph->graph[0] = f;    /* add new data */
330         /* shift all the data by 1 */
331         for (i = graph->graph_width - 1; i > 0; i--) {
332                 graph->graph[i] = graph->graph[i - 1];
333                 if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) {
334                         /* check if we need to update the scale */
335                         graph->graph_scale = graph->graph[i - 1];
336                 }
337         }
338         if (graph->scaled && graph->graph[graph->graph_width] > graph->graph_scale) {
339                 /* check if we need to update the scale */
340                 graph->graph_scale = graph->graph[graph->graph_width];
341         }
342 }
343
344 void new_graph(struct text_object *obj, char *buf, double val)
345 {
346         struct special_t *s = 0;
347         struct graph *g = obj->special_data;
348
349         if ((output_methods & TO_X) == 0)
350                 return;
351
352         if (!g)
353                 return;
354
355         s = new_special(buf, GRAPH);
356
357         s->width = g->width;
358         if (s->graph == NULL) {
359                 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
360                         // subtract 2 for the box
361                         s->graph_width = s->width /* - 2 */;
362                 } else {
363                         s->graph_width = MAX_GRAPH_DEPTH - 2;
364                 }
365                 s->graph = malloc(s->graph_width * sizeof(double));
366                 memset(s->graph, 0, s->graph_width * sizeof(double));
367                 s->graph_scale = 100;
368         }
369         s->height = g->height;
370         s->first_colour = adjust_colours(g->first_colour);
371         s->last_colour = adjust_colours(g->last_colour);
372         if (g->scale != 0) {
373                 s->scaled = 0;
374                 s->graph_scale = g->scale;
375                 s->show_scale = 0;
376         } else {
377                 s->scaled = 1;
378                 s->graph_scale = 1;
379                 s->show_scale = 1;
380         }
381         s->tempgrad = g->tempgrad;
382         /* if (s->width) {
383                 s->graph_width = s->width - 2;  // subtract 2 for rectangle around
384         } */
385 #ifdef MATH
386         if (g->showaslog) {
387                 s->graph_scale = log10(s->graph_scale + 1);
388         }
389 #endif
390         graph_append(s, val, g->showaslog);
391 }
392
393 void new_hr(char *buf, int a)
394 {
395         if ((output_methods & TO_X) == 0)
396                 return;
397
398         new_special(buf, HORIZONTAL_LINE)->height = a;
399 }
400
401 void scan_stippled_hr(struct text_object *obj, const char *arg)
402 {
403         struct stippled_hr *sh;
404
405         sh = malloc(sizeof(struct stippled_hr));
406         memset(sh, 0, sizeof(struct stippled_hr));
407
408         sh->arg = get_stippled_borders();
409         sh->height = 1;
410
411         if (arg) {
412                 if (sscanf(arg, "%d %d", &sh->arg, &sh->height) != 2) {
413                         sscanf(arg, "%d", &sh->height);
414                 }
415         }
416         if (sh->arg <= 0) {
417                 sh->arg = 1;
418         }
419         obj->special_data = sh;
420 }
421
422 void new_stippled_hr(struct text_object *obj, char *buf)
423 {
424         struct special_t *s = 0;
425         struct stippled_hr *sh = obj->special_data;
426
427         if ((output_methods & TO_X) == 0)
428                 return;
429
430         if (!sh)
431                 return;
432
433         s = new_special(buf, STIPPLED_HR);
434
435         s->height = sh->height;
436         s->arg = sh->arg;
437 }
438 #endif /* X11 */
439
440 void new_fg(char *buf, long c)
441 {
442 #ifdef X11
443         if (output_methods & TO_X)
444                 new_special(buf, FG)->arg = c;
445 #endif /* X11 */
446 #ifdef NCURSES
447         if (output_methods & TO_NCURSES)
448                 new_special(buf, FG)->arg = c;
449 #endif /* NCURSES */
450         UNUSED(buf);
451         UNUSED(c);
452 }
453
454 #ifdef X11
455 void new_bg(char *buf, long c)
456 {
457         if ((output_methods & TO_X) == 0)
458                 return;
459
460         new_special(buf, BG)->arg = c;
461 }
462 #endif /* X11 */
463
464 static void new_bar_in_shell(struct text_object *obj, char* buffer, int buf_max_size, double usage)
465 {
466         struct bar *b = obj->special_data;
467         int width, i, scaledusage;
468
469         if (!b)
470                 return;
471
472         width = b->width;
473         if (!width)
474                 width = DEFAULT_BAR_WIDTH_NO_X;
475
476         if (width > buf_max_size)
477                 width = buf_max_size;
478
479         scaledusage = round_to_int( usage * width / 255);
480
481         for (i = 0; i < scaledusage; i++)
482                 buffer[i] = '#';
483
484         for (; i < width; i++)
485                 buffer[i] = '_';
486
487         buffer[i] = 0;
488 }
489
490 #ifdef X11
491 static void new_bar_in_x11(struct text_object *obj, char *buf, int usage)
492 {
493         struct special_t *s = 0;
494         struct bar *b = obj->special_data;
495
496         if ((output_methods & TO_X) == 0)
497                 return;
498
499         if (!b)
500                 return;
501
502         s = new_special(buf, BAR);
503
504         s->arg = usage;
505         s->width = b->width;
506         s->height = b->height;
507 }
508 #endif /* X11 */
509
510 /* usage is in range [0,255] */
511 void new_bar(struct text_object *obj, char *p, int p_max_size, int usage)
512 {
513         if (!p_max_size)
514                 return;
515
516         usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
517
518 #ifdef X11
519         if ((output_methods & TO_X))
520                 new_bar_in_x11(obj, p, usage);
521         else
522 #endif /* X11 */
523                 new_bar_in_shell(obj, p, p_max_size, usage);
524 }
525
526 void new_outline(char *buf, long c)
527 {
528         new_special(buf, OUTLINE)->arg = c;
529 }
530
531 void new_offset(char *buf, long c)
532 {
533         new_special(buf, OFFSET)->arg = c;
534 }
535
536 void new_voffset(char *buf, long c)
537 {
538         new_special(buf, VOFFSET)->arg = c;
539 }
540
541 void new_alignr(char *buf, long c)
542 {
543         new_special(buf, ALIGNR)->arg = c;
544 }
545
546 // A postive offset pushes the text further left
547 void new_alignc(char *buf, long c)
548 {
549         new_special(buf, ALIGNC)->arg = c;
550 }
551
552 void new_goto(char *buf, long c)
553 {
554         new_special(buf, GOTO)->arg = c;
555 }
556
557 void scan_tab(struct text_object *obj, const char *arg)
558 {
559         struct tab *t;
560
561         t = malloc(sizeof(struct tab));
562         memset(t, 0, sizeof(struct tab));
563
564         t->width = 10;
565         t->arg = 0;
566
567         if (arg) {
568                 if (sscanf(arg, "%d %d", &t->width, &t->arg) != 2) {
569                         sscanf(arg, "%d", &t->arg);
570                 }
571         }
572         if (t->width <= 0) {
573                 t->width = 1;
574         }
575         obj->special_data = t;
576 }
577
578 void new_tab(struct text_object *obj, char *buf)
579 {
580         struct special_t *s = 0;
581         struct tab *t = obj->special_data;
582
583         if (!t)
584                 return;
585
586         s = new_special(buf, TAB);
587         s->width = t->width;
588         s->arg = t->arg;
589 }