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
64 unsigned int first_colour, last_colour;
65 unsigned int scale, showaslog;
70 * Scanning arguments to various special text objects
74 const char *scan_gauge(const char *args, int *w, int *h)
77 *w = default_gauge_width;
78 *h = default_gauge_height;
80 /* gauge's argument is either height or height,width */
84 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
85 if (sscanf(args, "%d %n", h, &n) == 2) {
86 *w = *h; /*square gauge*/
96 const char *scan_bar(struct text_object *obj, const char *args)
100 b = malloc(sizeof(struct bar));
101 memset(b, 0, sizeof(struct bar));
103 /* zero width means all space that is available */
104 b->width = default_bar_width;
105 b->height = default_bar_height;
106 /* bar's argument is either height or height,width */
110 if (sscanf(args, "%d,%d %n", &b->height, &b->width, &n) <= 1) {
111 sscanf(args, "%d %n", &b->height, &n);
116 obj->special_data = b;
121 char *scan_font(const char *args)
124 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
130 char *scan_graph(struct text_object *obj, const char *args)
134 memset(buf, 0, 1024);
136 g = malloc(sizeof(struct graph));
137 memset(g, 0, sizeof(struct graph));
138 obj->special_data = g;
140 /* zero width means all space that is available */
141 g->width = default_graph_width;
142 g->height = default_graph_height;
147 g->showaslog = FALSE;
149 if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) {
152 if (strstr(args, " "LOGGRAPH) || strncmp(args, LOGGRAPH, strlen(LOGGRAPH)) == 0) {
155 if (sscanf(args, "%d,%d %x %x %u", &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 5) {
159 if (sscanf(args, "%d,%d %x %x", &g->height, &g->width, &g->first_colour, &g->last_colour) == 4) {
162 if (sscanf(args, "%1023s %d,%d %x %x %u", buf, &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 6) {
163 return strndup(buf, text_buffer_size);
166 if (sscanf(args, "%1023s %d,%d %x %x", buf, &g->height, &g->width, &g->first_colour, &g->last_colour) == 5) {
167 return strndup(buf, text_buffer_size);
172 if (sscanf(args, "%x %x %u", &g->first_colour, &g->last_colour, &g->scale) == 3) {
176 if (sscanf(args, "%x %x", &g->first_colour, &g->last_colour) == 2) {
179 if (sscanf(args, "%1023s %x %x %u", buf, &g->first_colour, &g->last_colour, &g->scale) == 4) {
180 return strndup(buf, text_buffer_size);
183 if (sscanf(args, "%1023s %x %x", buf, &g->first_colour, &g->last_colour) == 3) {
184 return strndup(buf, text_buffer_size);
189 if (sscanf(args, "%d,%d %u", &g->height, &g->width, &g->scale) == 3) {
193 if (sscanf(args, "%d,%d", &g->height, &g->width) == 2) {
196 if (sscanf(args, "%1023s %d,%d %u", buf, &g->height, &g->width, &g->scale) < 4) {
198 //TODO: check the return value and throw an error?
199 sscanf(args, "%1023s %d,%d", buf, &g->height, &g->width);
203 return strndup(buf, text_buffer_size);
206 if (buf[0] == '\0') {
209 return strndup(buf, text_buffer_size);
215 * Printing various special text objects
218 static struct special_t *new_special(char *buf, enum special_types t)
220 if (special_count >= max_specials) {
221 CRIT_ERR(NULL, NULL, "too many special things in text");
224 buf[0] = SPECIAL_CHAR;
226 specials[special_count].type = t;
227 return &specials[special_count++];
231 void new_gauge(char *buf, int w, int h, int usage)
233 struct special_t *s = 0;
234 if ((output_methods & TO_X) == 0)
237 s = new_special(buf, GAUGE);
239 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
244 void new_bar(struct text_object *obj, char *buf, int usage)
246 struct special_t *s = 0;
247 struct bar *b = obj->special_data;
249 if ((output_methods & TO_X) == 0)
255 s = new_special(buf, BAR);
257 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
259 s->height = b->height;
262 void new_font(char *buf, char *args)
264 if ((output_methods & TO_X) == 0)
268 struct special_t *s = new_special(buf, FONT);
270 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
271 int tmp = selected_font;
273 selected_font = s->font_added = add_font(args);
277 struct special_t *s = new_special(buf, FONT);
278 int tmp = selected_font;
280 selected_font = s->font_added = 0;
285 static void graph_append(struct special_t *graph, double f, char showaslog)
295 if (!graph->scaled && f > graph->graph_scale) {
296 f = graph->graph_scale;
299 graph->graph[0] = f; /* add new data */
300 /* shift all the data by 1 */
301 for (i = graph->graph_width - 1; i > 0; i--) {
302 graph->graph[i] = graph->graph[i - 1];
303 if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) {
304 /* check if we need to update the scale */
305 graph->graph_scale = graph->graph[i - 1];
308 if (graph->scaled && graph->graph[graph->graph_width] > graph->graph_scale) {
309 /* check if we need to update the scale */
310 graph->graph_scale = graph->graph[graph->graph_width];
314 void new_graph(struct text_object *obj, char *buf, double val)
316 struct special_t *s = 0;
317 struct graph *g = obj->special_data;
319 if ((output_methods & TO_X) == 0)
325 s = new_special(buf, GRAPH);
328 if (s->graph == NULL) {
329 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
330 // subtract 2 for the box
331 s->graph_width = s->width /* - 2 */;
333 s->graph_width = MAX_GRAPH_DEPTH - 2;
335 s->graph = malloc(s->graph_width * sizeof(double));
336 memset(s->graph, 0, s->graph_width * sizeof(double));
337 s->graph_scale = 100;
339 s->height = g->height;
340 s->first_colour = adjust_colours(g->first_colour);
341 s->last_colour = adjust_colours(g->last_colour);
344 s->graph_scale = g->scale;
351 s->tempgrad = g->tempgrad;
353 s->graph_width = s->width - 2; // subtract 2 for rectangle around
357 s->graph_scale = log10(s->graph_scale + 1);
360 graph_append(s, val, g->showaslog);
363 void new_hr(char *buf, int a)
365 if ((output_methods & TO_X) == 0)
368 new_special(buf, HORIZONTAL_LINE)->height = a;
371 void new_stippled_hr(char *buf, int a, int b)
373 struct special_t *s = 0;
375 if ((output_methods & TO_X) == 0)
378 s = new_special(buf, STIPPLED_HR);
385 void new_fg(char *buf, long c)
388 if (output_methods & TO_X)
389 new_special(buf, FG)->arg = c;
392 if (output_methods & TO_NCURSES)
393 new_special(buf, FG)->arg = c;
400 void new_bg(char *buf, long c)
402 if ((output_methods & TO_X) == 0)
405 new_special(buf, BG)->arg = c;
409 void new_bar_in_shell(struct text_object *obj, char* buffer, int buf_max_size, double usage)
411 struct bar *b = obj->special_data;
419 width = DEFAULT_BAR_WIDTH_NO_X;
421 if(width<=buf_max_size){
422 int i = 0, j = 0, scaledusage = round_to_int( usage * width / 100);
425 #pragma omp parallel for schedule(dynamic,10)
426 #endif /* HAVE_OPENMP */
427 for(i=0; i<(int)scaledusage; i++) {
430 /* gcc seems to think i is not initialized properly :/ */
433 #pragma omp parallel for schedule(dynamic,10)
434 #endif /* HAVE_OPENMP */
435 for(i = j/* cheats */; i < width; i++) {
442 void new_outline(char *buf, long c)
444 new_special(buf, OUTLINE)->arg = c;
447 void new_offset(char *buf, long c)
449 new_special(buf, OFFSET)->arg = c;
452 void new_voffset(char *buf, long c)
454 new_special(buf, VOFFSET)->arg = c;
457 void new_alignr(char *buf, long c)
459 new_special(buf, ALIGNR)->arg = c;
462 // A postive offset pushes the text further left
463 void new_alignc(char *buf, long c)
465 new_special(buf, ALIGNC)->arg = c;
468 void new_goto(char *buf, long c)
470 new_special(buf, GOTO)->arg = c;
473 void new_tab(char *buf, int a, int b)
475 struct special_t *s = new_special(buf, TAB);