1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
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.
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/>.
36 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
37 unsigned int max_specials = MAX_SPECIALS_DEFAULT;
39 /* create specials array on heap instead of stack with introduction of
41 struct special_t *specials = NULL;
43 unsigned int special_count;
45 int default_bar_width = 0, default_bar_height = 6;
46 int default_graph_width = 0, default_graph_height = 25;
47 int default_gauge_width = 50, default_gauge_height = 25;
50 * Scanning arguments to various special text objects
53 const char *scan_gauge(const char *args, int *w, int *h)
56 *w = default_gauge_width;
57 *h = default_gauge_height;
59 /* gauge's argument is either height or height,width */
63 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
64 sscanf(args, "%d %n", h, &n);
65 *w = *h; /*square gauge*/
73 const char *scan_bar(const char *args, int *w, int *h)
75 /* zero width means all space that is available */
76 *w = default_bar_width;
77 *h = default_bar_height;
78 /* bar's argument is either height or height,width */
82 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
83 sscanf(args, "%d %n", h, &n);
91 char *scan_font(const char *args)
94 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
100 char *scan_graph(const char *args, int *w, int *h,
101 unsigned int *first_colour, unsigned int *last_colour,
102 unsigned int *scale, char *showaslog)
104 const char *nographtype;
108 /* zero width means all space that is available */
109 *w = default_graph_width;
110 *h = default_graph_height;
115 //set showaslog and place the rest of the args in nographtype
116 if(strcasecmp(args, LOGGRAPH) == EQUAL) {
119 }else if(strcasecmp(args, NORMGRAPH) == EQUAL) {
122 }else if(strncasecmp(args, LOGGRAPH" ", strlen(LOGGRAPH) + 1 ) == EQUAL) {
124 nographtype = &args[strlen(LOGGRAPH) + 1];
125 }else if(strncasecmp(args, NORMGRAPH" ", strlen(NORMGRAPH) + 1 ) == EQUAL) {
127 nographtype = &args[strlen(NORMGRAPH) + 1];
132 DBGP("printing graph as %s, other args are: %s", (*showaslog ? "log" : "normal"), nographtype);
133 //check the rest of the args
134 if (sscanf(nographtype, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
138 if (sscanf(nographtype, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
141 if (sscanf(nographtype, "%63s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) {
142 return strndup(buf, text_buffer_size);
145 if (sscanf(nographtype, "%63s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
146 return strndup(buf, text_buffer_size);
151 if (sscanf(nographtype, "%x %x %u", first_colour, last_colour, scale) == 3) {
155 if (sscanf(nographtype, "%x %x", first_colour, last_colour) == 2) {
158 if (sscanf(nographtype, "%63s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
159 return strndup(buf, text_buffer_size);
162 if (sscanf(nographtype, "%63s %x %x", buf, first_colour, last_colour) == 3) {
163 return strndup(buf, text_buffer_size);
168 if (sscanf(nographtype, "%d,%d %u", h, w, scale) == 3) {
172 if (sscanf(nographtype, "%d,%d", h, w) == 2) {
175 if (sscanf(nographtype, "%63s %d,%d %u", buf, h, w, scale) < 4) {
177 //TODO: check the return value and throw an error?
178 sscanf(nographtype, "%63s %d,%d", buf, h, w);
181 return strndup(buf, text_buffer_size);
184 if (buf[0] == '\0') {
187 return strndup(buf, text_buffer_size);
192 * Printing various special text objects
195 static struct special_t *new_special(char *buf, enum special_types t)
197 if (special_count >= max_specials) {
198 CRIT_ERR("too many special things in text");
201 buf[0] = SPECIAL_CHAR;
203 specials[special_count].type = t;
204 return &specials[special_count++];
207 void new_gauge(char *buf, int w, int h, int usage)
210 if ((output_methods & TO_X) == 0)
213 struct special_t *s = new_special(buf, GAUGE);
215 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
221 void new_bar(char *buf, int w, int h, int usage)
224 if ((output_methods & TO_X) == 0)
227 struct special_t *s = new_special(buf, BAR);
229 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
235 void new_font(char *buf, char *args)
238 if ((output_methods & TO_X) == 0)
242 struct special_t *s = new_special(buf, FONT);
244 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
245 int tmp = selected_font;
247 selected_font = s->font_added = addfont(args);
252 struct special_t *s = new_special(buf, FONT);
253 int tmp = selected_font;
255 selected_font = s->font_added = 0;
265 static void graph_append(struct special_t *graph, double f, char showaslog)
275 if (!graph->scaled && f > graph->graph_scale) {
276 f = graph->graph_scale;
279 /* Already happens in new_graph
281 graph->graph_scale = 1;
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];
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)
299 if ((output_methods & TO_X) == 0)
302 struct special_t *s = new_special(buf, GRAPH);
305 if (s->graph == NULL) {
306 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
307 // subtract 2 for the box
308 s->graph_width = s->width /* - 2 */;
310 s->graph_width = MAX_GRAPH_DEPTH - 2;
312 s->graph = malloc(s->graph_width * sizeof(double));
313 memset(s->graph, 0, s->graph_width * sizeof(double));
314 s->graph_scale = 100;
317 s->first_colour = adjust_colors(first_colour);
318 s->last_colour = adjust_colors(second_colour);
321 s->graph_scale = scale;
329 s->graph_width = s->width - 2; // subtract 2 for rectangle around
333 s->graph_scale = log10(s->graph_scale + 1);
337 graph_append(s, i, showaslog);
342 void new_hr(char *buf, int a)
345 if ((output_methods & TO_X) == 0)
348 new_special(buf, HORIZONTAL_LINE)->height = a;
352 void new_stippled_hr(char *buf, int a, int b)
355 if ((output_methods & TO_X) == 0)
358 struct special_t *s = new_special(buf, STIPPLED_HR);
365 void new_fg(char *buf, long c)
368 if ((output_methods & TO_X) == 0)
371 new_special(buf, FG)->arg = c;
375 void new_bg(char *buf, long c)
378 if ((output_methods & TO_X) == 0)
381 new_special(buf, BG)->arg = c;
385 void new_outline(char *buf, long c)
387 new_special(buf, OUTLINE)->arg = c;
390 void new_offset(char *buf, long c)
392 new_special(buf, OFFSET)->arg = c;
395 void new_voffset(char *buf, long c)
397 new_special(buf, VOFFSET)->arg = c;
400 void new_alignr(char *buf, long c)
402 new_special(buf, ALIGNR)->arg = c;
405 // A postive offset pushes the text further left
406 void new_alignc(char *buf, long c)
408 new_special(buf, ALIGNC)->arg = c;
411 void new_goto(char *buf, long c)
413 new_special(buf, GOTO)->arg = c;
416 void new_tab(char *buf, int a, int b)
418 struct special_t *s = new_special(buf, TAB);