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);
218 /* escape quotes at end in case of execgraph */
222 if (_ptr = strrchr(args, '"')) {
223 _size = _ptr - args - 1;
225 _size = _size < 1024 ? _size : 1023;
226 strncpy(buf, args + 1, _size);
232 return strndup(buf, text_buffer_size);
235 if (buf[0] == '\0') {
238 return strndup(buf, text_buffer_size);
244 * Printing various special text objects
247 static struct special_t *new_special(char *buf, enum special_types t)
249 if (special_count >= max_specials) {
250 CRIT_ERR(NULL, NULL, "too many special things in text");
253 buf[0] = SPECIAL_CHAR;
255 specials[special_count].type = t;
256 return &specials[special_count++];
259 void new_gauge_in_shell(struct text_object *obj, char *p, int p_max_size, int usage)
261 static const char *gaugevals[] = { "_. ", "\\. ", " | ", " ./", " ._" };
264 snprintf(p, p_max_size, "%s", gaugevals[round_to_int((double)usage * 4 / 255)]);
268 void new_gauge_in_x11(struct text_object *obj, char *buf, int usage)
270 struct special_t *s = 0;
271 struct gauge *g = obj->special_data;
273 if ((output_methods & TO_X) == 0)
279 s = new_special(buf, GAUGE);
283 s->height = g->height;
287 void new_gauge(struct text_object *obj, char *p, int p_max_size, int usage)
292 usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
295 if (output_methods & TO_X)
296 new_gauge_in_x11(obj, p, usage);
299 new_gauge_in_shell(obj, p, p_max_size, usage);
303 void new_font(char *buf, char *args)
305 if ((output_methods & TO_X) == 0)
309 struct special_t *s = new_special(buf, FONT);
311 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
312 int tmp = selected_font;
314 selected_font = s->font_added = add_font(args);
318 struct special_t *s = new_special(buf, FONT);
319 int tmp = selected_font;
321 selected_font = s->font_added = 0;
326 static void graph_append(struct special_t *graph, double f, char showaslog)
336 if (!graph->scaled && f > graph->graph_scale) {
337 f = graph->graph_scale;
340 /* shift all the data by 1 */
341 for (i = graph->graph_width - 1; i > 0; i--) {
342 graph->graph[i] = graph->graph[i - 1];
343 if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) {
344 /* check if we need to update the scale */
345 graph->graph_scale = graph->graph[i - 1];
348 graph->graph[0] = f; /* add new data */
349 if (graph->scaled && graph->graph[0] > graph->graph_scale) {
350 /* check if we need to update the scale */
351 graph->graph_scale = graph->graph[0];
355 void new_graph(struct text_object *obj, char *buf, int buf_max_size, double val)
357 struct special_t *s = 0;
358 struct graph *g = obj->special_data;
360 if ((output_methods & TO_X) == 0)
363 if (!g || !buf_max_size)
366 s = new_special(buf, GRAPH);
369 if (s->graph == NULL) {
370 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
371 // subtract 2 for the box
372 s->graph_width = s->width /* - 2 */;
374 s->graph_width = MAX_GRAPH_DEPTH - 2;
376 s->graph = malloc(s->graph_width * sizeof(double));
377 memset(s->graph, 0, s->graph_width * sizeof(double));
378 s->graph_scale = 100;
380 s->height = g->height;
381 s->first_colour = adjust_colours(g->first_colour);
382 s->last_colour = adjust_colours(g->last_colour);
385 s->graph_scale = g->scale;
392 s->tempgrad = g->tempgrad;
394 s->graph_width = s->width - 2; // subtract 2 for rectangle around
398 s->graph_scale = log10(s->graph_scale + 1);
401 graph_append(s, val, g->showaslog);
404 void new_hr(char *buf, int a)
406 if ((output_methods & TO_X) == 0)
409 new_special(buf, HORIZONTAL_LINE)->height = a;
412 void scan_stippled_hr(struct text_object *obj, const char *arg)
414 struct stippled_hr *sh;
416 sh = malloc(sizeof(struct stippled_hr));
417 memset(sh, 0, sizeof(struct stippled_hr));
419 sh->arg = get_stippled_borders();
423 if (sscanf(arg, "%d %d", &sh->arg, &sh->height) != 2) {
424 sscanf(arg, "%d", &sh->height);
430 obj->special_data = sh;
433 void new_stippled_hr(struct text_object *obj, char *buf)
435 struct special_t *s = 0;
436 struct stippled_hr *sh = obj->special_data;
438 if ((output_methods & TO_X) == 0)
444 s = new_special(buf, STIPPLED_HR);
446 s->height = sh->height;
451 void new_fg(char *buf, long c)
454 if (output_methods & TO_X)
455 new_special(buf, FG)->arg = c;
458 if (output_methods & TO_NCURSES)
459 new_special(buf, FG)->arg = c;
466 void new_bg(char *buf, long c)
468 if ((output_methods & TO_X) == 0)
471 new_special(buf, BG)->arg = c;
475 static void new_bar_in_shell(struct text_object *obj, char* buffer, int buf_max_size, double usage)
477 struct bar *b = obj->special_data;
478 int width, i, scaledusage;
485 width = DEFAULT_BAR_WIDTH_NO_X;
487 if (width > buf_max_size)
488 width = buf_max_size;
490 scaledusage = round_to_int( usage * width / 255);
492 for (i = 0; i < scaledusage; i++)
495 for (; i < width; i++)
502 static void new_bar_in_x11(struct text_object *obj, char *buf, int usage)
504 struct special_t *s = 0;
505 struct bar *b = obj->special_data;
507 s = new_special(buf, BAR);
510 s->width = b ? b->width : default_bar_width;
511 s->height = b ? b->height : default_bar_height;
515 /* usage is in range [0,255] */
516 void new_bar(struct text_object *obj, char *p, int p_max_size, int usage)
521 usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
524 if ((output_methods & TO_X))
525 new_bar_in_x11(obj, p, usage);
528 new_bar_in_shell(obj, p, p_max_size, usage);
531 void new_outline(char *buf, long c)
533 new_special(buf, OUTLINE)->arg = c;
536 void new_offset(char *buf, long c)
538 new_special(buf, OFFSET)->arg = c;
541 void new_voffset(char *buf, long c)
543 new_special(buf, VOFFSET)->arg = c;
546 void new_alignr(char *buf, long c)
548 new_special(buf, ALIGNR)->arg = c;
551 // A postive offset pushes the text further left
552 void new_alignc(char *buf, long c)
554 new_special(buf, ALIGNC)->arg = c;
557 void new_goto(char *buf, long c)
559 new_special(buf, GOTO)->arg = c;
562 void scan_tab(struct text_object *obj, const char *arg)
566 t = malloc(sizeof(struct tab));
567 memset(t, 0, sizeof(struct tab));
573 if (sscanf(arg, "%d %d", &t->width, &t->arg) != 2) {
574 sscanf(arg, "%d", &t->arg);
580 obj->special_data = t;
583 void new_tab(struct text_object *obj, char *buf)
585 struct special_t *s = 0;
586 struct tab *t = obj->special_data;
591 s = new_special(buf, TAB);