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