3fa6e0b4710a92787696ae33102d415f63fd4bc9
[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 #undef g
218
219                 return strndup(buf, text_buffer_size);
220         }
221
222         if (buf[0] == '\0') {
223                 return NULL;
224         } else {
225                 return strndup(buf, text_buffer_size);
226         }
227 }
228 #endif /* X11 */
229
230 /*
231  * Printing various special text objects
232  */
233
234 static struct special_t *new_special(char *buf, enum special_types t)
235 {
236         if (special_count >= max_specials) {
237                 CRIT_ERR(NULL, NULL, "too many special things in text");
238         }
239
240         buf[0] = SPECIAL_CHAR;
241         buf[1] = '\0';
242         specials[special_count].type = t;
243         return &specials[special_count++];
244 }
245
246 void new_gauge_in_shell(struct text_object *obj, char *p, int p_max_size, int usage)
247 {
248         static const char *gaugevals[] = { "_. ", "\\. ", " | ", " ./", " ._" };
249         (void)obj;
250
251         snprintf(p, p_max_size, "%s", gaugevals[round_to_int((double)usage * 4 / 255)]);
252 }
253
254 #ifdef X11
255 void new_gauge_in_x11(struct text_object *obj, char *buf, int usage)
256 {
257         struct special_t *s = 0;
258         struct gauge *g = obj->special_data;
259
260         if ((output_methods & TO_X) == 0)
261                 return;
262
263         if (!g)
264                 return;
265
266         s = new_special(buf, GAUGE);
267
268         s->arg = usage;
269         s->width = g->width;
270         s->height = g->height;
271 }
272 #endif /* X11 */
273
274 void new_gauge(struct text_object *obj, char *p, int p_max_size, int usage)
275 {
276         if (!p_max_size)
277                 return;
278
279         usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
280
281 #ifdef X11
282         if (output_methods & TO_X)
283                 new_gauge_in_x11(obj, p, usage);
284         else
285 #endif /* X11 */
286                 new_gauge_in_shell(obj, p, p_max_size, usage);
287 }
288
289 #ifdef X11
290 void new_font(char *buf, char *args)
291 {
292         if ((output_methods & TO_X) == 0)
293                 return;
294
295         if (args) {
296                 struct special_t *s = new_special(buf, FONT);
297
298                 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
299                         int tmp = selected_font;
300
301                         selected_font = s->font_added = add_font(args);
302                         selected_font = tmp;
303                 }
304         } else {
305                 struct special_t *s = new_special(buf, FONT);
306                 int tmp = selected_font;
307
308                 selected_font = s->font_added = 0;
309                 selected_font = tmp;
310         }
311 }
312
313 static void graph_append(struct special_t *graph, double f, char showaslog)
314 {
315         int i;
316
317         if (showaslog) {
318 #ifdef MATH
319                 f = log10(f + 1);
320 #endif
321         }
322
323         if (!graph->scaled && f > graph->graph_scale) {
324                 f = graph->graph_scale;
325         }
326
327         /* shift all the data by 1 */
328         for (i = graph->graph_width - 1; i > 0; i--) {
329                 graph->graph[i] = graph->graph[i - 1];
330                 if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) {
331                         /* check if we need to update the scale */
332                         graph->graph_scale = graph->graph[i - 1];
333                 }
334         }
335         graph->graph[0] = f;    /* add new data */
336         if (graph->scaled && graph->graph[0] > graph->graph_scale) {
337                 /* check if we need to update the scale */
338                 graph->graph_scale = graph->graph[0];
339         }
340 }
341
342 void new_graph(struct text_object *obj, char *buf, int buf_max_size, double val)
343 {
344         struct special_t *s = 0;
345         struct graph *g = obj->special_data;
346
347         if ((output_methods & TO_X) == 0)
348                 return;
349
350         if (!g || !buf_max_size)
351                 return;
352
353         s = new_special(buf, GRAPH);
354
355         s->width = g->width;
356         if (s->graph == NULL) {
357                 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
358                         // subtract 2 for the box
359                         s->graph_width = s->width /* - 2 */;
360                 } else {
361                         s->graph_width = MAX_GRAPH_DEPTH - 2;
362                 }
363                 s->graph = malloc(s->graph_width * sizeof(double));
364                 memset(s->graph, 0, s->graph_width * sizeof(double));
365                 s->graph_scale = 100;
366         }
367         s->height = g->height;
368         s->first_colour = adjust_colours(g->first_colour);
369         s->last_colour = adjust_colours(g->last_colour);
370         if (g->scale != 0) {
371                 s->scaled = 0;
372                 s->graph_scale = g->scale;
373                 s->show_scale = 0;
374         } else {
375                 s->scaled = 1;
376                 s->graph_scale = 1;
377                 s->show_scale = 1;
378         }
379         s->tempgrad = g->tempgrad;
380         /* if (s->width) {
381                 s->graph_width = s->width - 2;  // subtract 2 for rectangle around
382         } */
383 #ifdef MATH
384         if (g->showaslog) {
385                 s->graph_scale = log10(s->graph_scale + 1);
386         }
387 #endif
388         graph_append(s, val, g->showaslog);
389 }
390
391 void new_hr(char *buf, int a)
392 {
393         if ((output_methods & TO_X) == 0)
394                 return;
395
396         new_special(buf, HORIZONTAL_LINE)->height = a;
397 }
398
399 void scan_stippled_hr(struct text_object *obj, const char *arg)
400 {
401         struct stippled_hr *sh;
402
403         sh = malloc(sizeof(struct stippled_hr));
404         memset(sh, 0, sizeof(struct stippled_hr));
405
406         sh->arg = get_stippled_borders();
407         sh->height = 1;
408
409         if (arg) {
410                 if (sscanf(arg, "%d %d", &sh->arg, &sh->height) != 2) {
411                         sscanf(arg, "%d", &sh->height);
412                 }
413         }
414         if (sh->arg <= 0) {
415                 sh->arg = 1;
416         }
417         obj->special_data = sh;
418 }
419
420 void new_stippled_hr(struct text_object *obj, char *buf)
421 {
422         struct special_t *s = 0;
423         struct stippled_hr *sh = obj->special_data;
424
425         if ((output_methods & TO_X) == 0)
426                 return;
427
428         if (!sh)
429                 return;
430
431         s = new_special(buf, STIPPLED_HR);
432
433         s->height = sh->height;
434         s->arg = sh->arg;
435 }
436 #endif /* X11 */
437
438 void new_fg(char *buf, long c)
439 {
440 #ifdef X11
441         if (output_methods & TO_X)
442                 new_special(buf, FG)->arg = c;
443 #endif /* X11 */
444 #ifdef NCURSES
445         if (output_methods & TO_NCURSES)
446                 new_special(buf, FG)->arg = c;
447 #endif /* NCURSES */
448         UNUSED(buf);
449         UNUSED(c);
450 }
451
452 #ifdef X11
453 void new_bg(char *buf, long c)
454 {
455         if ((output_methods & TO_X) == 0)
456                 return;
457
458         new_special(buf, BG)->arg = c;
459 }
460 #endif /* X11 */
461
462 static void new_bar_in_shell(struct text_object *obj, char* buffer, int buf_max_size, double usage)
463 {
464         struct bar *b = obj->special_data;
465         int width, i, scaledusage;
466
467         if (!b)
468                 return;
469
470         width = b->width;
471         if (!width)
472                 width = DEFAULT_BAR_WIDTH_NO_X;
473
474         if (width > buf_max_size)
475                 width = buf_max_size;
476
477         scaledusage = round_to_int( usage * width / 255);
478
479         for (i = 0; i < scaledusage; i++)
480                 buffer[i] = '#';
481
482         for (; i < width; i++)
483                 buffer[i] = '_';
484
485         buffer[i] = 0;
486 }
487
488 #ifdef X11
489 static void new_bar_in_x11(struct text_object *obj, char *buf, int usage)
490 {
491         struct special_t *s = 0;
492         struct bar *b = obj->special_data;
493
494         s = new_special(buf, BAR);
495
496         s->arg = usage;
497         s->width = b ?  b->width : default_bar_width;
498         s->height = b ? b->height : default_bar_height;
499 }
500 #endif /* X11 */
501
502 /* usage is in range [0,255] */
503 void new_bar(struct text_object *obj, char *p, int p_max_size, int usage)
504 {
505         if (!p_max_size)
506                 return;
507
508         usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
509
510 #ifdef X11
511         if ((output_methods & TO_X))
512                 new_bar_in_x11(obj, p, usage);
513         else
514 #endif /* X11 */
515                 new_bar_in_shell(obj, p, p_max_size, usage);
516 }
517
518 void new_outline(char *buf, long c)
519 {
520         new_special(buf, OUTLINE)->arg = c;
521 }
522
523 void new_offset(char *buf, long c)
524 {
525         new_special(buf, OFFSET)->arg = c;
526 }
527
528 void new_voffset(char *buf, long c)
529 {
530         new_special(buf, VOFFSET)->arg = c;
531 }
532
533 void new_alignr(char *buf, long c)
534 {
535         new_special(buf, ALIGNR)->arg = c;
536 }
537
538 // A postive offset pushes the text further left
539 void new_alignc(char *buf, long c)
540 {
541         new_special(buf, ALIGNC)->arg = c;
542 }
543
544 void new_goto(char *buf, long c)
545 {
546         new_special(buf, GOTO)->arg = c;
547 }
548
549 void scan_tab(struct text_object *obj, const char *arg)
550 {
551         struct tab *t;
552
553         t = malloc(sizeof(struct tab));
554         memset(t, 0, sizeof(struct tab));
555
556         t->width = 10;
557         t->arg = 0;
558
559         if (arg) {
560                 if (sscanf(arg, "%d %d", &t->width, &t->arg) != 2) {
561                         sscanf(arg, "%d", &t->arg);
562                 }
563         }
564         if (t->width <= 0) {
565                 t->width = 1;
566         }
567         obj->special_data = t;
568 }
569
570 void new_tab(struct text_object *obj, char *buf)
571 {
572         struct special_t *s = 0;
573         struct tab *t = obj->special_data;
574
575         if (!t)
576                 return;
577
578         s = new_special(buf, TAB);
579         s->width = t->width;
580         s->arg = t->arg;
581 }