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