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;
74 * Scanning arguments to various special text objects
78 const char *scan_gauge(struct text_object *obj, const char *args)
82 g = malloc(sizeof(struct gauge));
83 memset(g, 0, sizeof(struct gauge));
86 g->width = default_gauge_width;
87 g->height = default_gauge_height;
89 /* gauge's argument is either height or height,width */
93 if (sscanf(args, "%d,%d %n", &g->height, &g->width, &n) <= 1) {
94 if (sscanf(args, "%d %n", &g->height, &n) == 2) {
95 g->width = g->height; /*square gauge*/
101 obj->special_data = g;
106 const char *scan_bar(struct text_object *obj, const char *args)
110 b = malloc(sizeof(struct bar));
111 memset(b, 0, sizeof(struct bar));
113 /* zero width means all space that is available */
114 b->width = default_bar_width;
115 b->height = default_bar_height;
116 /* bar's argument is either height or height,width */
120 if (sscanf(args, "%d,%d %n", &b->height, &b->width, &n) <= 1) {
121 sscanf(args, "%d %n", &b->height, &n);
126 obj->special_data = b;
131 char *scan_font(const char *args)
134 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
140 char *scan_graph(struct text_object *obj, const char *args)
144 memset(buf, 0, 1024);
146 g = malloc(sizeof(struct graph));
147 memset(g, 0, sizeof(struct graph));
148 obj->special_data = g;
150 /* zero width means all space that is available */
151 g->width = default_graph_width;
152 g->height = default_graph_height;
157 g->showaslog = FALSE;
159 if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) {
162 if (strstr(args, " "LOGGRAPH) || strncmp(args, LOGGRAPH, strlen(LOGGRAPH)) == 0) {
165 if (sscanf(args, "%d,%d %x %x %u", &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 5) {
169 if (sscanf(args, "%d,%d %x %x", &g->height, &g->width, &g->first_colour, &g->last_colour) == 4) {
172 if (sscanf(args, "%1023s %d,%d %x %x %u", buf, &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 6) {
173 return strndup(buf, text_buffer_size);
176 if (sscanf(args, "%1023s %d,%d %x %x", buf, &g->height, &g->width, &g->first_colour, &g->last_colour) == 5) {
177 return strndup(buf, text_buffer_size);
182 if (sscanf(args, "%x %x %u", &g->first_colour, &g->last_colour, &g->scale) == 3) {
186 if (sscanf(args, "%x %x", &g->first_colour, &g->last_colour) == 2) {
189 if (sscanf(args, "%1023s %x %x %u", buf, &g->first_colour, &g->last_colour, &g->scale) == 4) {
190 return strndup(buf, text_buffer_size);
193 if (sscanf(args, "%1023s %x %x", buf, &g->first_colour, &g->last_colour) == 3) {
194 return strndup(buf, text_buffer_size);
199 if (sscanf(args, "%d,%d %u", &g->height, &g->width, &g->scale) == 3) {
203 if (sscanf(args, "%d,%d", &g->height, &g->width) == 2) {
206 if (sscanf(args, "%1023s %d,%d %u", buf, &g->height, &g->width, &g->scale) < 4) {
208 //TODO: check the return value and throw an error?
209 sscanf(args, "%1023s %d,%d", buf, &g->height, &g->width);
213 return strndup(buf, text_buffer_size);
216 if (buf[0] == '\0') {
219 return strndup(buf, text_buffer_size);
225 * Printing various special text objects
228 static struct special_t *new_special(char *buf, enum special_types t)
230 if (special_count >= max_specials) {
231 CRIT_ERR(NULL, NULL, "too many special things in text");
234 buf[0] = SPECIAL_CHAR;
236 specials[special_count].type = t;
237 return &specials[special_count++];
241 void new_gauge(struct text_object *obj, char *buf, int usage)
243 struct special_t *s = 0;
244 struct gauge *g = obj->special_data;
246 if ((output_methods & TO_X) == 0)
252 s = new_special(buf, GAUGE);
254 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
256 s->height = g->height;
259 void new_bar(struct text_object *obj, char *buf, int usage)
261 struct special_t *s = 0;
262 struct bar *b = obj->special_data;
264 if ((output_methods & TO_X) == 0)
270 s = new_special(buf, BAR);
272 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
274 s->height = b->height;
277 void new_font(char *buf, char *args)
279 if ((output_methods & TO_X) == 0)
283 struct special_t *s = new_special(buf, FONT);
285 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
286 int tmp = selected_font;
288 selected_font = s->font_added = add_font(args);
292 struct special_t *s = new_special(buf, FONT);
293 int tmp = selected_font;
295 selected_font = s->font_added = 0;
300 static void graph_append(struct special_t *graph, double f, char showaslog)
310 if (!graph->scaled && f > graph->graph_scale) {
311 f = graph->graph_scale;
314 graph->graph[0] = f; /* add new data */
315 /* shift all the data by 1 */
316 for (i = graph->graph_width - 1; i > 0; i--) {
317 graph->graph[i] = graph->graph[i - 1];
318 if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) {
319 /* check if we need to update the scale */
320 graph->graph_scale = graph->graph[i - 1];
323 if (graph->scaled && graph->graph[graph->graph_width] > graph->graph_scale) {
324 /* check if we need to update the scale */
325 graph->graph_scale = graph->graph[graph->graph_width];
329 void new_graph(struct text_object *obj, char *buf, double val)
331 struct special_t *s = 0;
332 struct graph *g = obj->special_data;
334 if ((output_methods & TO_X) == 0)
340 s = new_special(buf, GRAPH);
343 if (s->graph == NULL) {
344 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
345 // subtract 2 for the box
346 s->graph_width = s->width /* - 2 */;
348 s->graph_width = MAX_GRAPH_DEPTH - 2;
350 s->graph = malloc(s->graph_width * sizeof(double));
351 memset(s->graph, 0, s->graph_width * sizeof(double));
352 s->graph_scale = 100;
354 s->height = g->height;
355 s->first_colour = adjust_colours(g->first_colour);
356 s->last_colour = adjust_colours(g->last_colour);
359 s->graph_scale = g->scale;
366 s->tempgrad = g->tempgrad;
368 s->graph_width = s->width - 2; // subtract 2 for rectangle around
372 s->graph_scale = log10(s->graph_scale + 1);
375 graph_append(s, val, g->showaslog);
378 void new_hr(char *buf, int a)
380 if ((output_methods & TO_X) == 0)
383 new_special(buf, HORIZONTAL_LINE)->height = a;
386 void new_stippled_hr(char *buf, int a, int b)
388 struct special_t *s = 0;
390 if ((output_methods & TO_X) == 0)
393 s = new_special(buf, STIPPLED_HR);
400 void new_fg(char *buf, long c)
403 if (output_methods & TO_X)
404 new_special(buf, FG)->arg = c;
407 if (output_methods & TO_NCURSES)
408 new_special(buf, FG)->arg = c;
415 void new_bg(char *buf, long c)
417 if ((output_methods & TO_X) == 0)
420 new_special(buf, BG)->arg = c;
424 void new_bar_in_shell(struct text_object *obj, char* buffer, int buf_max_size, double usage)
426 struct bar *b = obj->special_data;
434 width = DEFAULT_BAR_WIDTH_NO_X;
436 if(width<=buf_max_size){
437 int i = 0, j = 0, scaledusage = round_to_int( usage * width / 100);
440 #pragma omp parallel for schedule(dynamic,10)
441 #endif /* HAVE_OPENMP */
442 for(i=0; i<(int)scaledusage; i++) {
445 /* gcc seems to think i is not initialized properly :/ */
448 #pragma omp parallel for schedule(dynamic,10)
449 #endif /* HAVE_OPENMP */
450 for(i = j/* cheats */; i < width; i++) {
457 void new_outline(char *buf, long c)
459 new_special(buf, OUTLINE)->arg = c;
462 void new_offset(char *buf, long c)
464 new_special(buf, OFFSET)->arg = c;
467 void new_voffset(char *buf, long c)
469 new_special(buf, VOFFSET)->arg = c;
472 void new_alignr(char *buf, long c)
474 new_special(buf, ALIGNR)->arg = c;
477 // A postive offset pushes the text further left
478 void new_alignc(char *buf, long c)
480 new_special(buf, ALIGNC)->arg = c;
483 void new_goto(char *buf, long c)
485 new_special(buf, GOTO)->arg = c;
488 void new_tab(char *buf, int a, int b)
490 struct special_t *s = new_special(buf, TAB);