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