Add vim modelines.
[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  * vim: ts=4 sw=4 noet ai cindent syntax=c
27  *
28  */
29 #include "conky.h"
30 #include "colours.h"
31 #ifdef X11
32 #include "fonts.h"
33 #endif /* X11 */
34 #include "logging.h"
35 #include "specials.h"
36 #include <math.h>
37
38 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
39 int max_specials = MAX_SPECIALS_DEFAULT;
40
41 /* create specials array on heap instead of stack with introduction of
42  * max_specials */
43 struct special_t *specials = NULL;
44
45 int special_count;
46
47 int default_bar_width = 0, default_bar_height = 6;
48 #ifdef X11
49 int default_graph_width = 0, default_graph_height = 25;
50 int default_gauge_width = 40, default_gauge_height = 25;
51 #endif /* X11 */
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                         if (sscanf(args, "%d %n", h, &n) == 2) {
70                                 *w = *h; /*square gauge*/
71                         }
72                 }
73                 args += n;
74         }
75
76         return args;
77 }
78 #endif /* X11 */
79
80 const char *scan_bar(const char *args, int *w, int *h)
81 {
82         /* zero width means all space that is available */
83         *w = default_bar_width;
84         *h = default_bar_height;
85         /* bar's argument is either height or height,width */
86         if (args) {
87                 int n = 0;
88
89                 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
90                         sscanf(args, "%d %n", h, &n);
91                 }
92                 args += n;
93         }
94
95         return args;
96 }
97
98 #ifdef X11
99 char *scan_font(const char *args)
100 {
101         if (args && *args) {
102                 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
103         }
104
105         return NULL;
106 }
107
108 char *scan_graph(const char *args, int *w, int *h,
109                  unsigned int *first_colour, unsigned int *last_colour,
110                  unsigned int *scale, char *showaslog, char *tempgrad)
111 {
112         char buf[1024];
113         memset(buf, 0, 1024);
114
115         /* zero width means all space that is available */
116         *w = default_graph_width;
117         *h = default_graph_height;
118         *first_colour = 0;
119         *last_colour = 0;
120         *scale = 0;
121         *tempgrad = FALSE;
122         *showaslog = FALSE;
123         if (args) {
124                 if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) {
125                         *tempgrad = TRUE;
126                 }
127                 if (strstr(args, " "LOGGRAPH) || strncmp(args, LOGGRAPH, strlen(LOGGRAPH)) == 0) {
128                         *showaslog = TRUE;
129                 }
130                 if (sscanf(args, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
131                         return NULL;
132                 }
133                 *scale = 0;
134                 if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
135                         return NULL;
136                 }
137                 if (sscanf(args, "%1023s %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(args, "%1023s %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(args, "%x %x %u", first_colour, last_colour, scale) == 3) {
148                         return NULL;
149                 }
150                 *scale = 0;
151                 if (sscanf(args, "%x %x", first_colour, last_colour) == 2) {
152                         return NULL;
153                 }
154                 if (sscanf(args, "%1023s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
155                         return strndup(buf, text_buffer_size);
156                 }
157                 *scale = 0;
158                 if (sscanf(args, "%1023s %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(args, "%d,%d %u", h, w, scale) == 3) {
165                         return NULL;
166                 }
167                 *scale = 0;
168                 if (sscanf(args, "%d,%d", h, w) == 2) {
169                         return NULL;
170                 }
171                 if (sscanf(args, "%1023s %d,%d %u", buf, h, w, scale) < 4) {
172                         *scale = 0;
173                         //TODO: check the return value and throw an error?
174                         sscanf(args, "%1023s %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 #endif /* X11 */
187
188 /*
189  * Printing various special text objects
190  */
191
192 static struct special_t *new_special(char *buf, enum special_types t)
193 {
194         if (special_count >= max_specials) {
195                 CRIT_ERR(NULL, NULL, "too many special things in text");
196         }
197
198         buf[0] = SPECIAL_CHAR;
199         buf[1] = '\0';
200         specials[special_count].type = t;
201         return &specials[special_count++];
202 }
203
204 #ifdef X11
205 void new_gauge(char *buf, int w, int h, int usage)
206 {
207         struct special_t *s = 0;
208         if ((output_methods & TO_X) == 0)
209                 return;
210
211         s = new_special(buf, GAUGE);
212
213         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
214         s->width = w;
215         s->height = h;
216 }
217
218 void new_bar(char *buf, int w, int h, int usage)
219 {
220         struct special_t *s = 0;
221
222         if ((output_methods & TO_X) == 0)
223                 return;
224
225         s = new_special(buf, BAR);
226
227         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
228         s->width = w;
229         s->height = h;
230 }
231
232 void new_font(char *buf, char *args)
233 {
234         if ((output_methods & TO_X) == 0)
235                 return;
236
237         if (args) {
238                 struct special_t *s = new_special(buf, FONT);
239
240                 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
241                         int tmp = selected_font;
242
243                         selected_font = s->font_added = add_font(args);
244                         selected_font = tmp;
245                 }
246         } else {
247                 struct special_t *s = new_special(buf, FONT);
248                 int tmp = selected_font;
249
250                 selected_font = s->font_added = 0;
251                 selected_font = tmp;
252         }
253 }
254
255 static void graph_append(struct special_t *graph, double f, char showaslog)
256 {
257         int i;
258
259         if (showaslog) {
260 #ifdef MATH
261                 f = log10(f + 1);
262 #endif
263         }
264         
265         if (!graph->scaled && f > graph->graph_scale) {
266                 f = graph->graph_scale;
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 - 1] > graph->graph_scale) {
274                         /* check if we need to update the scale */
275                         graph->graph_scale = graph->graph[i - 1];
276                 }
277         }
278         if (graph->scaled && graph->graph[graph->graph_width] > graph->graph_scale) {
279                 /* check if we need to update the scale */
280                 graph->graph_scale = graph->graph[graph->graph_width];
281         }
282 }
283
284 void new_graph(char *buf, int w, int h, unsigned int first_colour,
285                 unsigned int second_colour, double i, int scale, int append, char showaslog, char tempgrad)
286 {
287         struct special_t *s = 0;
288
289         if ((output_methods & TO_X) == 0)
290                 return;
291
292         s = new_special(buf, GRAPH);
293
294         s->width = w;
295         if (s->graph == NULL) {
296                 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
297                         // subtract 2 for the box
298                         s->graph_width = s->width /* - 2 */;
299                 } else {
300                         s->graph_width = MAX_GRAPH_DEPTH - 2;
301                 }
302                 s->graph = malloc(s->graph_width * sizeof(double));
303                 memset(s->graph, 0, s->graph_width * sizeof(double));
304                 s->graph_scale = 100;
305         }
306         s->height = h;
307         s->first_colour = adjust_colours(first_colour);
308         s->last_colour = adjust_colours(second_colour);
309         if (scale != 0) {
310                 s->scaled = 0;
311                 s->graph_scale = scale;
312                 s->show_scale = 0;
313         } else {
314                 s->scaled = 1;
315                 s->graph_scale = 1;
316                 s->show_scale = 1;
317         }
318         s->tempgrad = tempgrad;
319         /* if (s->width) {
320                 s->graph_width = s->width - 2;  // subtract 2 for rectangle around
321         } */
322 #ifdef MATH
323         if (showaslog) {
324                 s->graph_scale = log10(s->graph_scale + 1);
325         }
326 #endif
327         if (append) {
328                 graph_append(s, i, showaslog);
329         }
330 }
331
332 void new_hr(char *buf, int a)
333 {
334         if ((output_methods & TO_X) == 0)
335                 return;
336
337         new_special(buf, HORIZONTAL_LINE)->height = a;
338 }
339
340 void new_stippled_hr(char *buf, int a, int b)
341 {
342         struct special_t *s = 0;
343
344         if ((output_methods & TO_X) == 0)
345                 return;
346
347         s = new_special(buf, STIPPLED_HR);
348
349         s->height = b;
350         s->arg = a;
351 }
352
353 void new_fg(char *buf, long c)
354 {
355         if ((output_methods & TO_X) == 0)
356                 return;
357
358         new_special(buf, FG)->arg = c;
359 }
360
361 void new_bg(char *buf, long c)
362 {
363         if ((output_methods & TO_X) == 0)
364                 return;
365
366         new_special(buf, BG)->arg = c;
367 }
368 #endif /* X11 */
369
370 void new_bar_in_shell(char* buffer, int buf_max_size, double usage, int width)
371 {
372         if(width<=buf_max_size){
373                 int i = 0, j = 0, scaledusage = round_to_int( usage * width / 100);
374
375                 #ifdef HAVE_OPENMP
376                 #pragma omp parallel for schedule(dynamic,10)
377                 #endif /* HAVE_OPENMP */
378                 for(i=0; i<(int)scaledusage; i++) {
379                         *(buffer+i)='#';
380                 }
381                 /* gcc seems to think i is not initialized properly :/ */
382                 j = i;
383                 #ifdef HAVE_OPENMP
384                 #pragma omp parallel for schedule(dynamic,10)
385                 #endif /* HAVE_OPENMP */
386                 for(i = j/* cheats */; i < width; i++) {
387                         *(buffer+i)='_';
388                 }
389                 *(buffer+i)=0;
390         }
391 }
392
393 void new_outline(char *buf, long c)
394 {
395         new_special(buf, OUTLINE)->arg = c;
396 }
397
398 void new_offset(char *buf, long c)
399 {
400         new_special(buf, OFFSET)->arg = c;
401 }
402
403 void new_voffset(char *buf, long c)
404 {
405         new_special(buf, VOFFSET)->arg = c;
406 }
407
408 void new_alignr(char *buf, long c)
409 {
410         new_special(buf, ALIGNR)->arg = c;
411 }
412
413 // A postive offset pushes the text further left
414 void new_alignc(char *buf, long c)
415 {
416         new_special(buf, ALIGNC)->arg = c;
417 }
418
419 void new_goto(char *buf, long c)
420 {
421         new_special(buf, GOTO)->arg = c;
422 }
423
424 void new_tab(char *buf, int a, int b)
425 {
426         struct special_t *s = new_special(buf, TAB);
427
428         s->width = a;
429         s->arg = b;
430 }
431