Fix build errors.
[monky] / src / specials.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  */
27 #include "conky.h"
28 #include "colours.h"
29 #ifdef X11
30 #include "fonts.h"
31 #endif
32 #include "logging.h"
33 #include "specials.h"
34 #include <math.h>
35
36 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
37 unsigned int max_specials = MAX_SPECIALS_DEFAULT;
38
39 /* create specials array on heap instead of stack with introduction of
40  * max_specials */
41 struct special_t *specials = NULL;
42
43 unsigned int special_count;
44
45 int default_bar_width = 0, default_bar_height = 6;
46 int default_graph_width = 0, default_graph_height = 25;
47 int default_gauge_width = 50, default_gauge_height = 25;
48
49 /*
50  * Scanning arguments to various special text objects
51  */
52
53 const char *scan_gauge(const char *args, int *w, int *h)
54 {
55         /*width and height*/
56         *w = default_gauge_width;
57         *h = default_gauge_height;
58
59         /* gauge's argument is either height or height,width */
60         if (args) {
61                 int n = 0;
62
63                 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
64                         sscanf(args, "%d %n", h, &n);
65                         *w = *h; /*square gauge*/
66                 }
67                 args += n;
68         }
69
70         return args;
71 }
72
73 const char *scan_bar(const char *args, int *w, int *h)
74 {
75         /* zero width means all space that is available */
76         *w = default_bar_width;
77         *h = default_bar_height;
78         /* bar's argument is either height or height,width */
79         if (args) {
80                 int n = 0;
81
82                 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
83                         sscanf(args, "%d %n", h, &n);
84                 }
85                 args += n;
86         }
87
88         return args;
89 }
90
91 char *scan_font(const char *args)
92 {
93         if (args && *args) {
94                 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
95         }
96
97         return NULL;
98 }
99
100 char *scan_graph(const char *args, int *w, int *h,
101                  unsigned int *first_colour, unsigned int *last_colour,
102                  unsigned int *scale, char *showaslog)
103 {
104         const char *nographtype;
105         char buf[64];
106         buf[0] = 0;
107
108         /* zero width means all space that is available */
109         *w = default_graph_width;
110         *h = default_graph_height;
111         *first_colour = 0;
112         *last_colour = 0;
113         *scale = 0;
114         if (args) {
115                 //set showaslog and place the rest of the args in nographtype
116                 if(strcasecmp(args, LOGGRAPH) == EQUAL) {
117                         *showaslog = TRUE;
118                         return NULL;
119                 }else if(strcasecmp(args, NORMGRAPH) == EQUAL) {
120                         *showaslog = FALSE;
121                         return NULL;
122                 }else if(strncasecmp(args, LOGGRAPH" ", strlen(LOGGRAPH) + 1 ) == EQUAL) {
123                         *showaslog = TRUE;
124                         nographtype = &args[strlen(LOGGRAPH) + 1];
125                 }else if(strncasecmp(args, NORMGRAPH" ", strlen(NORMGRAPH) + 1 ) == EQUAL) {
126                         *showaslog = FALSE;
127                         nographtype = &args[strlen(NORMGRAPH) + 1];
128                 }else{
129                         *showaslog = FALSE;
130                         nographtype = args;
131                 }
132                 DBGP("printing graph as %s, other args are: %s", (*showaslog ? "log" : "normal"), nographtype);
133                 //check the rest of the args
134                 if (sscanf(nographtype, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
135                         return NULL;
136                 }
137                 *scale = 0;
138                 if (sscanf(nographtype, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
139                         return NULL;
140                 }
141                 if (sscanf(nographtype, "%63s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) {
142                         return strndup(buf, text_buffer_size);
143                 }
144                 *scale = 0;
145                 if (sscanf(nographtype, "%63s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
146                         return strndup(buf, text_buffer_size);
147                 }
148                 buf[0] = '\0';
149                 *h = 25;
150                 *w = 0;
151                 if (sscanf(nographtype, "%x %x %u", first_colour, last_colour, scale) == 3) {
152                         return NULL;
153                 }
154                 *scale = 0;
155                 if (sscanf(nographtype, "%x %x", first_colour, last_colour) == 2) {
156                         return NULL;
157                 }
158                 if (sscanf(nographtype, "%63s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
159                         return strndup(buf, text_buffer_size);
160                 }
161                 *scale = 0;
162                 if (sscanf(nographtype, "%63s %x %x", buf, first_colour, last_colour) == 3) {
163                         return strndup(buf, text_buffer_size);
164                 }
165                 buf[0] = '\0';
166                 *first_colour = 0;
167                 *last_colour = 0;
168                 if (sscanf(nographtype, "%d,%d %u", h, w, scale) == 3) {
169                         return NULL;
170                 }
171                 *scale = 0;
172                 if (sscanf(nographtype, "%d,%d", h, w) == 2) {
173                         return NULL;
174                 }
175                 if (sscanf(nographtype, "%63s %d,%d %u", buf, h, w, scale) < 4) {
176                         *scale = 0;
177                         //TODO: check the return value and throw an error?
178                         sscanf(nographtype, "%63s %d,%d", buf, h, w);
179                 }
180
181                 return strndup(buf, text_buffer_size);
182         }
183
184         if (buf[0] == '\0') {
185                 return NULL;
186         } else {
187                 return strndup(buf, text_buffer_size);
188         }
189 }
190
191 /*
192  * Printing various special text objects
193  */
194
195 static struct special_t *new_special(char *buf, enum special_types t)
196 {
197         if (special_count >= max_specials) {
198                 CRIT_ERR("too many special things in text");
199         }
200
201         buf[0] = SPECIAL_CHAR;
202         buf[1] = '\0';
203         specials[special_count].type = t;
204         return &specials[special_count++];
205 }
206
207 void new_gauge(char *buf, int w, int h, int usage)
208 {
209 #ifdef X11
210         struct special_t *s = 0;
211         if ((output_methods & TO_X) == 0)
212                 return;
213
214         s = new_special(buf, GAUGE);
215
216         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
217         s->width = w;
218         s->height = h;
219 #endif
220 }
221
222 void new_bar(char *buf, int w, int h, int usage)
223 {
224 #ifdef X11
225         struct special_t *s = 0;
226
227         if ((output_methods & TO_X) == 0)
228                 return;
229
230         s = new_special(buf, BAR);
231
232         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
233         s->width = w;
234         s->height = h;
235 #endif
236 }
237
238 void new_font(char *buf, char *args)
239 {
240 #ifdef X11
241         if ((output_methods & TO_X) == 0)
242                 return;
243
244         if (args) {
245                 struct special_t *s = new_special(buf, FONT);
246
247                 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
248                         int tmp = selected_font;
249
250                         selected_font = s->font_added = addfont(args);
251                         load_fonts();
252                         selected_font = tmp;
253                 }
254         } else {
255                 struct special_t *s = new_special(buf, FONT);
256                 int tmp = selected_font;
257
258                 selected_font = s->font_added = 0;
259                 selected_font = tmp;
260         }
261 #else
262         (void)buf;
263         (void)args;
264         return;
265 #endif
266 }
267
268 static void graph_append(struct special_t *graph, double f, char showaslog)
269 {
270         int i;
271
272         if (showaslog) {
273 #ifdef MATH
274                 f = log10(f + 1);
275 #endif
276         }
277         
278         if (!graph->scaled && f > graph->graph_scale) {
279                 f = graph->graph_scale;
280         }
281
282 /* Already happens in new_graph
283         if (graph->scaled) {
284                 graph->graph_scale = 1;
285         }
286 */
287         graph->graph[0] = f;    /* add new data */
288         /* shift all the data by 1 */
289         for (i = graph->graph_width - 1; i > 0; i--) {
290                 graph->graph[i] = graph->graph[i - 1];
291                 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
292                         /* check if we need to update the scale */
293                         graph->graph_scale = graph->graph[i];
294                 }
295         }
296 }
297
298 void new_graph(char *buf, int w, int h, unsigned int first_colour,
299                 unsigned int second_colour, double i, int scale, int append, char showaslog)
300 {
301 #ifdef X11
302         struct special_t *s = 0;
303
304         if ((output_methods & TO_X) == 0)
305                 return;
306
307         s = new_special(buf, GRAPH);
308
309         s->width = w;
310         if (s->graph == NULL) {
311                 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
312                         // subtract 2 for the box
313                         s->graph_width = s->width /* - 2 */;
314                 } else {
315                         s->graph_width = MAX_GRAPH_DEPTH - 2;
316                 }
317                 s->graph = malloc(s->graph_width * sizeof(double));
318                 memset(s->graph, 0, s->graph_width * sizeof(double));
319                 s->graph_scale = 100;
320         }
321         s->height = h;
322         s->first_colour = adjust_colors(first_colour);
323         s->last_colour = adjust_colors(second_colour);
324         if (scale != 0) {
325                 s->scaled = 0;
326                 s->graph_scale = scale;
327                 s->show_scale = 0;
328         } else {
329                 s->scaled = 1;
330                 s->graph_scale = 1;
331                 s->show_scale = 1;
332         }
333         /* if (s->width) {
334                 s->graph_width = s->width - 2;  // subtract 2 for rectangle around
335         } */
336         if (showaslog) {
337 #ifdef MATH
338                 s->graph_scale = log10(s->graph_scale + 1);
339 #endif
340         }
341         if (append) {
342                 graph_append(s, i, showaslog);
343         }
344 #endif
345 }
346
347 void new_hr(char *buf, int a)
348 {
349 #ifdef X11
350         if ((output_methods & TO_X) == 0)
351                 return;
352
353         new_special(buf, HORIZONTAL_LINE)->height = a;
354 #endif
355 }
356
357 void new_stippled_hr(char *buf, int a, int b)
358 {
359 #ifdef X11
360         struct special_t *s = 0;
361
362         if ((output_methods & TO_X) == 0)
363                 return;
364
365         s = new_special(buf, STIPPLED_HR);
366
367         s->height = b;
368         s->arg = a;
369 #endif
370 }
371
372 void new_fg(char *buf, long c)
373 {
374 #ifdef X11
375         if ((output_methods & TO_X) == 0)
376                 return;
377
378         new_special(buf, FG)->arg = c;
379 #endif
380 }
381
382 void new_bg(char *buf, long c)
383 {
384 #ifdef X11
385         if ((output_methods & TO_X) == 0)
386                 return;
387
388         new_special(buf, BG)->arg = c;
389 #endif
390 }
391
392 void new_outline(char *buf, long c)
393 {
394         new_special(buf, OUTLINE)->arg = c;
395 }
396
397 void new_offset(char *buf, long c)
398 {
399         new_special(buf, OFFSET)->arg = c;
400 }
401
402 void new_voffset(char *buf, long c)
403 {
404         new_special(buf, VOFFSET)->arg = c;
405 }
406
407 void new_alignr(char *buf, long c)
408 {
409         new_special(buf, ALIGNR)->arg = c;
410 }
411
412 // A postive offset pushes the text further left
413 void new_alignc(char *buf, long c)
414 {
415         new_special(buf, ALIGNC)->arg = c;
416 }
417
418 void new_goto(char *buf, long c)
419 {
420         new_special(buf, GOTO)->arg = c;
421 }
422
423 void new_tab(char *buf, int a, int b)
424 {
425         struct special_t *s = new_special(buf, TAB);
426
427         s->width = a;
428         s->arg = b;
429 }
430