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