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
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
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.
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/>.
39 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
40 int max_specials = MAX_SPECIALS_DEFAULT;
42 /* create specials array on heap instead of stack with introduction of
44 struct special_t *specials = NULL;
48 int default_bar_width = 0, default_bar_height = 6;
50 int default_graph_width = 0, default_graph_height = 25;
52 int default_gauge_width = 40, default_gauge_height = 25;
55 * Special data typedefs
68 unsigned int first_colour, last_colour;
69 unsigned int scale, showaslog;
82 * Scanning arguments to various special text objects
85 const char *scan_gauge(struct text_object *obj, const char *args)
89 g = malloc(sizeof(struct gauge));
90 memset(g, 0, sizeof(struct gauge));
93 g->width = default_gauge_width;
94 g->height = default_gauge_height;
96 /* gauge's argument is either height or height,width */
100 if (sscanf(args, "%d,%d %n", &g->height, &g->width, &n) <= 1) {
101 if (sscanf(args, "%d %n", &g->height, &n) == 2) {
102 g->width = g->height; /*square gauge*/
108 obj->special_data = g;
112 const char *scan_bar(struct text_object *obj, const char *args)
116 b = malloc(sizeof(struct bar));
117 memset(b, 0, sizeof(struct bar));
119 /* zero width means all space that is available */
120 b->width = default_bar_width;
121 b->height = default_bar_height;
122 /* bar's argument is either height or height,width */
126 if (sscanf(args, "%d,%d %n", &b->height, &b->width, &n) <= 1) {
127 sscanf(args, "%d %n", &b->height, &n);
132 obj->special_data = b;
137 char *scan_font(const char *args)
140 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
146 char *scan_graph(struct text_object *obj, const char *args, int defscale)
150 memset(buf, 0, 1024);
152 g = malloc(sizeof(struct graph));
153 memset(g, 0, sizeof(struct graph));
154 obj->special_data = g;
156 /* zero width means all space that is available */
157 g->width = default_graph_width;
158 g->height = default_graph_height;
163 g->showaslog = FALSE;
165 if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) {
168 if (strstr(args, " "LOGGRAPH) || strncmp(args, LOGGRAPH, strlen(LOGGRAPH)) == 0) {
171 if (sscanf(args, "%d,%d %x %x %u", &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 5) {
175 if (sscanf(args, "%d,%d %x %x", &g->height, &g->width, &g->first_colour, &g->last_colour) == 4) {
178 if (sscanf(args, "%1023s %d,%d %x %x %u", buf, &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 6) {
179 return strndup(buf, text_buffer_size);
182 if (sscanf(args, "%1023s %d,%d %x %x", buf, &g->height, &g->width, &g->first_colour, &g->last_colour) == 5) {
183 return strndup(buf, text_buffer_size);
188 if (sscanf(args, "%x %x %u", &g->first_colour, &g->last_colour, &g->scale) == 3) {
192 if (sscanf(args, "%x %x", &g->first_colour, &g->last_colour) == 2) {
195 if (sscanf(args, "%1023s %x %x %u", buf, &g->first_colour, &g->last_colour, &g->scale) == 4) {
196 return strndup(buf, text_buffer_size);
199 if (sscanf(args, "%1023s %x %x", buf, &g->first_colour, &g->last_colour) == 3) {
200 return strndup(buf, text_buffer_size);
205 if (sscanf(args, "%d,%d %u", &g->height, &g->width, &g->scale) == 3) {
209 if (sscanf(args, "%d,%d", &g->height, &g->width) == 2) {
212 if (sscanf(args, "%1023s %d,%d %u", buf, &g->height, &g->width, &g->scale) < 4) {
214 //TODO: check the return value and throw an error?
215 sscanf(args, "%1023s %d,%d", buf, &g->height, &g->width);
219 return strndup(buf, text_buffer_size);
222 if (buf[0] == '\0') {
225 return strndup(buf, text_buffer_size);
231 * Printing various special text objects
234 static struct special_t *new_special(char *buf, enum special_types t)
236 if (special_count >= max_specials) {
237 CRIT_ERR(NULL, NULL, "too many special things in text");
240 buf[0] = SPECIAL_CHAR;
242 specials[special_count].type = t;
243 return &specials[special_count++];
246 void new_gauge_in_shell(struct text_object *obj, char *p, int p_max_size, int usage)
248 static const char *gaugevals[] = { "_. ", "\\. ", " | ", " ./", " ._" };
251 snprintf(p, p_max_size, "%s", gaugevals[round_to_int((double)usage * 4 / 255)]);
255 void new_gauge_in_x11(struct text_object *obj, char *buf, int usage)
257 struct special_t *s = 0;
258 struct gauge *g = obj->special_data;
260 if ((output_methods & TO_X) == 0)
266 s = new_special(buf, GAUGE);
270 s->height = g->height;
274 void new_gauge(struct text_object *obj, char *p, int p_max_size, int usage)
279 usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
282 if (output_methods & TO_X)
283 new_gauge_in_x11(obj, p, usage);
286 new_gauge_in_shell(obj, p, p_max_size, usage);
290 void new_font(char *buf, char *args)
292 if ((output_methods & TO_X) == 0)
296 struct special_t *s = new_special(buf, FONT);
298 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
299 int tmp = selected_font;
301 selected_font = s->font_added = add_font(args);
305 struct special_t *s = new_special(buf, FONT);
306 int tmp = selected_font;
308 selected_font = s->font_added = 0;
313 static void graph_append(struct special_t *graph, double f, char showaslog)
323 if (!graph->scaled && f > graph->graph_scale) {
324 f = graph->graph_scale;
327 /* shift all the data by 1 */
328 for (i = graph->graph_width - 1; i > 0; i--) {
329 graph->graph[i] = graph->graph[i - 1];
330 if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) {
331 /* check if we need to update the scale */
332 graph->graph_scale = graph->graph[i - 1];
335 graph->graph[0] = f; /* add new data */
336 if (graph->scaled && graph->graph[0] > graph->graph_scale) {
337 /* check if we need to update the scale */
338 graph->graph_scale = graph->graph[0];
342 void new_graph(struct text_object *obj, char *buf, int buf_max_size, double val)
344 struct special_t *s = 0;
345 struct graph *g = obj->special_data;
347 if ((output_methods & TO_X) == 0)
350 if (!g || !buf_max_size)
353 s = new_special(buf, GRAPH);
356 if (s->graph == NULL) {
357 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
358 // subtract 2 for the box
359 s->graph_width = s->width /* - 2 */;
361 s->graph_width = MAX_GRAPH_DEPTH - 2;
363 s->graph = malloc(s->graph_width * sizeof(double));
364 memset(s->graph, 0, s->graph_width * sizeof(double));
365 s->graph_scale = 100;
367 s->height = g->height;
368 s->first_colour = adjust_colours(g->first_colour);
369 s->last_colour = adjust_colours(g->last_colour);
372 s->graph_scale = g->scale;
379 s->tempgrad = g->tempgrad;
381 s->graph_width = s->width - 2; // subtract 2 for rectangle around
385 s->graph_scale = log10(s->graph_scale + 1);
388 graph_append(s, val, g->showaslog);
391 void new_hr(char *buf, int a)
393 if ((output_methods & TO_X) == 0)
396 new_special(buf, HORIZONTAL_LINE)->height = a;
399 void scan_stippled_hr(struct text_object *obj, const char *arg)
401 struct stippled_hr *sh;
403 sh = malloc(sizeof(struct stippled_hr));
404 memset(sh, 0, sizeof(struct stippled_hr));
406 sh->arg = get_stippled_borders();
410 if (sscanf(arg, "%d %d", &sh->arg, &sh->height) != 2) {
411 sscanf(arg, "%d", &sh->height);
417 obj->special_data = sh;
420 void new_stippled_hr(struct text_object *obj, char *buf)
422 struct special_t *s = 0;
423 struct stippled_hr *sh = obj->special_data;
425 if ((output_methods & TO_X) == 0)
431 s = new_special(buf, STIPPLED_HR);
433 s->height = sh->height;
438 void new_fg(char *buf, long c)
441 if (output_methods & TO_X)
442 new_special(buf, FG)->arg = c;
445 if (output_methods & TO_NCURSES)
446 new_special(buf, FG)->arg = c;
453 void new_bg(char *buf, long c)
455 if ((output_methods & TO_X) == 0)
458 new_special(buf, BG)->arg = c;
462 static void new_bar_in_shell(struct text_object *obj, char* buffer, int buf_max_size, double usage)
464 struct bar *b = obj->special_data;
465 int width, i, scaledusage;
472 width = DEFAULT_BAR_WIDTH_NO_X;
474 if (width > buf_max_size)
475 width = buf_max_size;
477 scaledusage = round_to_int( usage * width / 255);
479 for (i = 0; i < scaledusage; i++)
482 for (; i < width; i++)
489 static void new_bar_in_x11(struct text_object *obj, char *buf, int usage)
491 struct special_t *s = 0;
492 struct bar *b = obj->special_data;
494 s = new_special(buf, BAR);
497 s->width = b ? b->width : default_bar_width;
498 s->height = b ? b->height : default_bar_height;
502 /* usage is in range [0,255] */
503 void new_bar(struct text_object *obj, char *p, int p_max_size, int usage)
508 usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
511 if ((output_methods & TO_X))
512 new_bar_in_x11(obj, p, usage);
515 new_bar_in_shell(obj, p, p_max_size, usage);
518 void new_outline(char *buf, long c)
520 new_special(buf, OUTLINE)->arg = c;
523 void new_offset(char *buf, long c)
525 new_special(buf, OFFSET)->arg = c;
528 void new_voffset(char *buf, long c)
530 new_special(buf, VOFFSET)->arg = c;
533 void new_alignr(char *buf, long c)
535 new_special(buf, ALIGNR)->arg = c;
538 // A postive offset pushes the text further left
539 void new_alignc(char *buf, long c)
541 new_special(buf, ALIGNC)->arg = c;
544 void new_goto(char *buf, long c)
546 new_special(buf, GOTO)->arg = c;
549 void scan_tab(struct text_object *obj, const char *arg)
553 t = malloc(sizeof(struct tab));
554 memset(t, 0, sizeof(struct tab));
560 if (sscanf(arg, "%d %d", &t->width, &t->arg) != 2) {
561 sscanf(arg, "%d", &t->arg);
567 obj->special_data = t;
570 void new_tab(struct text_object *obj, char *buf)
572 struct special_t *s = 0;
573 struct tab *t = obj->special_data;
578 s = new_special(buf, TAB);