Fix: make sure compiling without X11 works
[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-2008 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 /*
46  * Scanning arguments to various special text objects
47  */
48
49 const char *scan_gauge(const char *args, int *w, int *h)
50 {
51         /*width and height*/
52         *w = 25;
53         *h = 25;
54
55         /* gauge's argument is either height or height,width */
56         if (args) {
57                 int n = 0;
58
59                 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
60                         sscanf(args, "%d %n", h, &n);
61                         *w = *h; /*square gauge*/
62                 }
63                 args += n;
64         }
65
66         return args;
67 }
68
69 const char *scan_bar(const char *args, int *w, int *h)
70 {
71         /* zero width means all space that is available */
72         *w = 0;
73         *h = 6;
74         /* bar's argument is either height or height,width */
75         if (args) {
76                 int n = 0;
77
78                 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
79                         sscanf(args, "%d %n", h, &n);
80                 }
81                 args += n;
82         }
83
84         return args;
85 }
86
87 char *scan_font(const char *args)
88 {
89         if (args && *args) {
90                 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
91         }
92
93         return NULL;
94 }
95
96 char *scan_graph(const char *args, int *w, int *h,
97                  unsigned int *first_colour, unsigned int *last_colour,
98                  unsigned int *scale, char *showaslog)
99 {
100         const char *nographtype;
101         char buf[64];
102         buf[0] = 0;
103
104         /* zero width means all space that is available */
105         *w = 0;
106         *h = 25;
107         *first_colour = 0;
108         *last_colour = 0;
109         *scale = 0;
110         if (args) {
111                 //set showaslog and place the rest of the args in nographtype
112                 if(strcasecmp(args, LOGGRAPH) == EQUAL) {
113                         *showaslog = TRUE;
114                         return NULL;
115                 }else if(strcasecmp(args, NORMGRAPH) == EQUAL) {
116                         *showaslog = FALSE;
117                         return NULL;
118                 }else if(strncasecmp(args, LOGGRAPH" ", strlen(LOGGRAPH) + 1 ) == EQUAL) {
119                         *showaslog = TRUE;
120                         nographtype = &args[strlen(LOGGRAPH) + 1];
121                 }else if(strncasecmp(args, NORMGRAPH" ", strlen(NORMGRAPH) + 1 ) == EQUAL) {
122                         *showaslog = FALSE;
123                         nographtype = &args[strlen(NORMGRAPH) + 1];
124                 }else{
125                         *showaslog = FALSE;
126                         nographtype = args;
127                 }
128                 DBGP("printing graph as %s, other args are: %s", (*showaslog ? "log" : "normal"), nographtype);
129                 //check the rest of the args
130                 if (sscanf(nographtype, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
131                         return NULL;
132                 }
133                 *scale = 0;
134                 if (sscanf(nographtype, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
135                         return NULL;
136                 }
137                 if (sscanf(nographtype, "%63s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) {
138                         return strndup(buf, text_buffer_size);
139                 }
140                 *scale = 0;
141                 if (sscanf(nographtype, "%63s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
142                         return strndup(buf, text_buffer_size);
143                 }
144                 buf[0] = '\0';
145                 *h = 25;
146                 *w = 0;
147                 if (sscanf(nographtype, "%x %x %u", first_colour, last_colour, scale) == 3) {
148                         return NULL;
149                 }
150                 *scale = 0;
151                 if (sscanf(nographtype, "%x %x", first_colour, last_colour) == 2) {
152                         return NULL;
153                 }
154                 if (sscanf(nographtype, "%63s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
155                         return strndup(buf, text_buffer_size);
156                 }
157                 *scale = 0;
158                 if (sscanf(nographtype, "%63s %x %x", buf, first_colour, last_colour) == 3) {
159                         return strndup(buf, text_buffer_size);
160                 }
161                 buf[0] = '\0';
162                 *first_colour = 0;
163                 *last_colour = 0;
164                 if (sscanf(nographtype, "%d,%d %u", h, w, scale) == 3) {
165                         return NULL;
166                 }
167                 *scale = 0;
168                 if (sscanf(nographtype, "%d,%d", h, w) == 2) {
169                         return NULL;
170                 }
171                 if (sscanf(nographtype, "%63s %d,%d %u", buf, h, w, scale) < 4) {
172                         *scale = 0;
173                         //TODO: check the return value and throw an error?
174                         sscanf(nographtype, "%63s %d,%d", buf, h, w);
175                 }
176
177                 return strndup(buf, text_buffer_size);
178         }
179
180         if (buf[0] == '\0') {
181                 return NULL;
182         } else {
183                 return strndup(buf, text_buffer_size);
184         }
185 }
186
187 /*
188  * Printing various special text objects
189  */
190
191 static struct special_t *new_special(char *buf, enum special_types t)
192 {
193         if (special_count >= max_specials) {
194                 CRIT_ERR("too many special things in text");
195         }
196
197         buf[0] = SPECIAL_CHAR;
198         buf[1] = '\0';
199         specials[special_count].type = t;
200         return &specials[special_count++];
201 }
202
203 void new_gauge(char *buf, int w, int h, int usage)
204 {
205         struct special_t *s = new_special(buf, GAUGE);
206
207         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
208         s->width = w;
209         s->height = h;
210 }
211
212 void new_bar(char *buf, int w, int h, int usage)
213 {
214         struct special_t *s = new_special(buf, BAR);
215
216         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
217         s->width = w;
218         s->height = h;
219 }
220
221 void new_font(char *buf, char *args)
222 {
223         if ((output_methods & TO_X) == 0)
224                 return;
225 #ifdef X11
226         if (args) {
227                 struct special_t *s = new_special(buf, FONT);
228
229                 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
230                         int tmp = selected_font;
231
232                         selected_font = s->font_added = addfont(args);
233                         load_fonts();
234                         selected_font = tmp;
235                 }
236         } else {
237                 struct special_t *s = new_special(buf, FONT);
238                 int tmp = selected_font;
239
240                 selected_font = s->font_added = 0;
241                 selected_font = tmp;
242         }
243 #else
244         (void)buf;
245         (void)args;
246         return;
247 #endif
248 }
249
250 static void graph_append(struct special_t *graph, double f, char showaslog)
251 {
252         int i;
253
254         if (showaslog) {
255 #ifdef MATH
256                 f = log10(f + 1);
257 #endif
258         }
259         
260         if (!graph->scaled && f > graph->graph_scale) {
261                 f = graph->graph_scale;
262         }
263
264 /* Already happens in new_graph
265         if (graph->scaled) {
266                 graph->graph_scale = 1;
267         }
268 */
269         graph->graph[0] = f;    /* add new data */
270         /* shift all the data by 1 */
271         for (i = graph->graph_width - 1; i > 0; i--) {
272                 graph->graph[i] = graph->graph[i - 1];
273                 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
274                         /* check if we need to update the scale */
275                         graph->graph_scale = graph->graph[i];
276                 }
277         }
278 }
279
280 void new_graph(char *buf, int w, int h, unsigned int first_colour,
281                 unsigned int second_colour, double i, int scale, int append, char showaslog)
282 {
283         struct special_t *s = new_special(buf, GRAPH);
284
285         s->width = w;
286         if (s->graph == NULL) {
287                 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
288                         // subtract 2 for the box
289                         s->graph_width = s->width /* - 2 */;
290                 } else {
291                         s->graph_width = MAX_GRAPH_DEPTH - 2;
292                 }
293                 s->graph = malloc(s->graph_width * sizeof(double));
294                 memset(s->graph, 0, s->graph_width * sizeof(double));
295                 s->graph_scale = 100;
296         }
297         s->height = h;
298         s->first_colour = adjust_colors(first_colour);
299         s->last_colour = adjust_colors(second_colour);
300         if (scale != 0) {
301                 s->scaled = 0;
302                 s->graph_scale = scale;
303                 s->show_scale = 0;
304         } else {
305                 s->scaled = 1;
306                 s->graph_scale = 1;
307                 s->show_scale = 1;
308         }
309         /* if (s->width) {
310                 s->graph_width = s->width - 2;  // subtract 2 for rectangle around
311         } */
312         if (showaslog) {
313 #ifdef MATH
314                 s->graph_scale = log10(s->graph_scale + 1);
315 #endif
316         }
317         if (append) {
318                 graph_append(s, i, showaslog);
319         }
320 }
321
322 void new_hr(char *buf, int a)
323 {
324         new_special(buf, HORIZONTAL_LINE)->height = a;
325 }
326
327 void new_stippled_hr(char *buf, int a, int b)
328 {
329         struct special_t *s = new_special(buf, STIPPLED_HR);
330
331         s->height = b;
332         s->arg = a;
333 }
334
335 void new_fg(char *buf, long c)
336 {
337         new_special(buf, FG)->arg = c;
338 }
339
340 void new_bg(char *buf, long c)
341 {
342         new_special(buf, BG)->arg = c;
343 }
344
345 void new_outline(char *buf, long c)
346 {
347         new_special(buf, OUTLINE)->arg = c;
348 }
349
350 void new_offset(char *buf, long c)
351 {
352         new_special(buf, OFFSET)->arg = c;
353 }
354
355 void new_voffset(char *buf, long c)
356 {
357         new_special(buf, VOFFSET)->arg = c;
358 }
359
360 void new_alignr(char *buf, long c)
361 {
362         new_special(buf, ALIGNR)->arg = c;
363 }
364
365 // A postive offset pushes the text further left
366 void new_alignc(char *buf, long c)
367 {
368         new_special(buf, ALIGNC)->arg = c;
369 }
370
371 void new_goto(char *buf, long c)
372 {
373         new_special(buf, GOTO)->arg = c;
374 }
375
376 void new_tab(char *buf, int a, int b)
377 {
378         struct special_t *s = new_special(buf, TAB);
379
380         s->width = a;
381         s->arg = b;
382 }
383