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