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-2009 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;
51 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
86 const char *scan_gauge(struct text_object *obj, const char *args)
90 g = malloc(sizeof(struct gauge));
91 memset(g, 0, sizeof(struct gauge));
94 g->width = default_gauge_width;
95 g->height = default_gauge_height;
97 /* gauge's argument is either height or height,width */
101 if (sscanf(args, "%d,%d %n", &g->height, &g->width, &n) <= 1) {
102 if (sscanf(args, "%d %n", &g->height, &n) == 2) {
103 g->width = g->height; /*square gauge*/
109 obj->special_data = g;
114 const char *scan_bar(struct text_object *obj, const char *args)
118 b = malloc(sizeof(struct bar));
119 memset(b, 0, sizeof(struct bar));
121 /* zero width means all space that is available */
122 b->width = default_bar_width;
123 b->height = default_bar_height;
124 /* bar's argument is either height or height,width */
128 if (sscanf(args, "%d,%d %n", &b->height, &b->width, &n) <= 1) {
129 sscanf(args, "%d %n", &b->height, &n);
134 obj->special_data = b;
139 char *scan_font(const char *args)
142 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
148 char *scan_graph(struct text_object *obj, const char *args, int defscale)
152 memset(buf, 0, 1024);
154 g = malloc(sizeof(struct graph));
155 memset(g, 0, sizeof(struct graph));
156 obj->special_data = g;
158 /* zero width means all space that is available */
159 g->width = default_graph_width;
160 g->height = default_graph_height;
165 g->showaslog = FALSE;
167 if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) {
170 if (strstr(args, " "LOGGRAPH) || strncmp(args, LOGGRAPH, strlen(LOGGRAPH)) == 0) {
173 if (sscanf(args, "%d,%d %x %x %u", &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 5) {
177 if (sscanf(args, "%d,%d %x %x", &g->height, &g->width, &g->first_colour, &g->last_colour) == 4) {
180 if (sscanf(args, "%1023s %d,%d %x %x %u", buf, &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 6) {
181 return strndup(buf, text_buffer_size);
184 if (sscanf(args, "%1023s %d,%d %x %x", buf, &g->height, &g->width, &g->first_colour, &g->last_colour) == 5) {
185 return strndup(buf, text_buffer_size);
190 if (sscanf(args, "%x %x %u", &g->first_colour, &g->last_colour, &g->scale) == 3) {
194 if (sscanf(args, "%x %x", &g->first_colour, &g->last_colour) == 2) {
197 if (sscanf(args, "%1023s %x %x %u", buf, &g->first_colour, &g->last_colour, &g->scale) == 4) {
198 return strndup(buf, text_buffer_size);
201 if (sscanf(args, "%1023s %x %x", buf, &g->first_colour, &g->last_colour) == 3) {
202 return strndup(buf, text_buffer_size);
207 if (sscanf(args, "%d,%d %u", &g->height, &g->width, &g->scale) == 3) {
211 if (sscanf(args, "%d,%d", &g->height, &g->width) == 2) {
214 if (sscanf(args, "%1023s %d,%d %u", buf, &g->height, &g->width, &g->scale) < 4) {
216 //TODO: check the return value and throw an error?
217 sscanf(args, "%1023s %d,%d", buf, &g->height, &g->width);
221 return strndup(buf, text_buffer_size);
224 if (buf[0] == '\0') {
227 return strndup(buf, text_buffer_size);
233 * Printing various special text objects
236 static struct special_t *new_special(char *buf, enum special_types t)
238 if (special_count >= max_specials) {
239 CRIT_ERR(NULL, NULL, "too many special things in text");
242 buf[0] = SPECIAL_CHAR;
244 specials[special_count].type = t;
245 return &specials[special_count++];
249 void new_gauge(struct text_object *obj, char *buf, int usage)
251 struct special_t *s = 0;
252 struct gauge *g = obj->special_data;
254 if ((output_methods & TO_X) == 0)
260 s = new_special(buf, GAUGE);
262 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
264 s->height = g->height;
267 void new_font(char *buf, char *args)
269 if ((output_methods & TO_X) == 0)
273 struct special_t *s = new_special(buf, FONT);
275 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
276 int tmp = selected_font;
278 selected_font = s->font_added = add_font(args);
282 struct special_t *s = new_special(buf, FONT);
283 int tmp = selected_font;
285 selected_font = s->font_added = 0;
290 static void graph_append(struct special_t *graph, double f, char showaslog)
300 if (!graph->scaled && f > graph->graph_scale) {
301 f = graph->graph_scale;
304 graph->graph[0] = f; /* add new data */
305 /* shift all the data by 1 */
306 for (i = graph->graph_width - 1; i > 0; i--) {
307 graph->graph[i] = graph->graph[i - 1];
308 if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) {
309 /* check if we need to update the scale */
310 graph->graph_scale = graph->graph[i - 1];
313 if (graph->scaled && graph->graph[graph->graph_width] > graph->graph_scale) {
314 /* check if we need to update the scale */
315 graph->graph_scale = graph->graph[graph->graph_width];
319 void new_graph(struct text_object *obj, char *buf, double val)
321 struct special_t *s = 0;
322 struct graph *g = obj->special_data;
324 if ((output_methods & TO_X) == 0)
330 s = new_special(buf, GRAPH);
333 if (s->graph == NULL) {
334 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
335 // subtract 2 for the box
336 s->graph_width = s->width /* - 2 */;
338 s->graph_width = MAX_GRAPH_DEPTH - 2;
340 s->graph = malloc(s->graph_width * sizeof(double));
341 memset(s->graph, 0, s->graph_width * sizeof(double));
342 s->graph_scale = 100;
344 s->height = g->height;
345 s->first_colour = adjust_colours(g->first_colour);
346 s->last_colour = adjust_colours(g->last_colour);
349 s->graph_scale = g->scale;
356 s->tempgrad = g->tempgrad;
358 s->graph_width = s->width - 2; // subtract 2 for rectangle around
362 s->graph_scale = log10(s->graph_scale + 1);
365 graph_append(s, val, g->showaslog);
368 void new_hr(char *buf, int a)
370 if ((output_methods & TO_X) == 0)
373 new_special(buf, HORIZONTAL_LINE)->height = a;
376 void scan_stippled_hr(struct text_object *obj, const char *arg)
378 struct stippled_hr *sh;
380 sh = malloc(sizeof(struct stippled_hr));
381 memset(sh, 0, sizeof(struct stippled_hr));
383 sh->arg = get_stippled_borders();
387 if (sscanf(arg, "%d %d", &sh->arg, &sh->height) != 2) {
388 sscanf(arg, "%d", &sh->height);
394 obj->special_data = sh;
397 void new_stippled_hr(struct text_object *obj, char *buf)
399 struct special_t *s = 0;
400 struct stippled_hr *sh = obj->special_data;
402 if ((output_methods & TO_X) == 0)
408 s = new_special(buf, STIPPLED_HR);
410 s->height = sh->height;
415 void new_fg(char *buf, long c)
418 if (output_methods & TO_X)
419 new_special(buf, FG)->arg = c;
422 if (output_methods & TO_NCURSES)
423 new_special(buf, FG)->arg = c;
430 void new_bg(char *buf, long c)
432 if ((output_methods & TO_X) == 0)
435 new_special(buf, BG)->arg = c;
439 static void new_bar_in_shell(struct text_object *obj, char* buffer, int buf_max_size, double usage)
441 struct bar *b = obj->special_data;
442 int width, i, scaledusage;
449 width = DEFAULT_BAR_WIDTH_NO_X;
451 if (width > buf_max_size)
452 width = buf_max_size;
454 scaledusage = round_to_int( usage * width / 255);
456 for (i = 0; i < scaledusage; i++)
459 for (; i < width; i++)
466 static void new_bar_in_x11(struct text_object *obj, char *buf, int usage)
468 struct special_t *s = 0;
469 struct bar *b = obj->special_data;
471 if ((output_methods & TO_X) == 0)
477 s = new_special(buf, BAR);
481 s->height = b->height;
485 /* usage is in range [0,255] */
486 void new_bar(struct text_object *obj, char *p, int p_max_size, int usage)
491 usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
494 if ((output_methods & TO_X))
495 new_bar_in_x11(obj, p, usage);
498 new_bar_in_shell(obj, p, p_max_size, usage);
501 void new_outline(char *buf, long c)
503 new_special(buf, OUTLINE)->arg = c;
506 void new_offset(char *buf, long c)
508 new_special(buf, OFFSET)->arg = c;
511 void new_voffset(char *buf, long c)
513 new_special(buf, VOFFSET)->arg = c;
516 void new_alignr(char *buf, long c)
518 new_special(buf, ALIGNR)->arg = c;
521 // A postive offset pushes the text further left
522 void new_alignc(char *buf, long c)
524 new_special(buf, ALIGNC)->arg = c;
527 void new_goto(char *buf, long c)
529 new_special(buf, GOTO)->arg = c;
532 void scan_tab(struct text_object *obj, const char *arg)
536 t = malloc(sizeof(struct tab));
537 memset(t, 0, sizeof(struct tab));
543 if (sscanf(arg, "%d %d", &t->width, &t->arg) != 2) {
544 sscanf(arg, "%d", &t->arg);
550 obj->special_data = t;
553 void new_tab(struct text_object *obj, char *buf)
555 struct special_t *s = 0;
556 struct tab *t = obj->special_data;
561 s = new_special(buf, TAB);