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