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
63 * Scanning arguments to various special text objects
67 const char *scan_gauge(const char *args, int *w, int *h)
70 *w = default_gauge_width;
71 *h = default_gauge_height;
73 /* gauge's argument is either height or height,width */
77 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
78 if (sscanf(args, "%d %n", h, &n) == 2) {
79 *w = *h; /*square gauge*/
89 const char *scan_bar(struct text_object *obj, const char *args)
93 b = malloc(sizeof(struct bar));
94 memset(b, 0, sizeof(struct bar));
96 /* zero width means all space that is available */
97 b->width = default_bar_width;
98 b->height = default_bar_height;
99 /* bar's argument is either height or height,width */
103 if (sscanf(args, "%d,%d %n", &b->height, &b->width, &n) <= 1) {
104 sscanf(args, "%d %n", &b->height, &n);
109 obj->special_data = b;
114 char *scan_font(const char *args)
117 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
123 char *scan_graph(const char *args, int *w, int *h,
124 unsigned int *first_colour, unsigned int *last_colour,
125 unsigned int *scale, char *showaslog, char *tempgrad)
128 memset(buf, 0, 1024);
130 /* zero width means all space that is available */
131 *w = default_graph_width;
132 *h = default_graph_height;
139 if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) {
142 if (strstr(args, " "LOGGRAPH) || strncmp(args, LOGGRAPH, strlen(LOGGRAPH)) == 0) {
145 if (sscanf(args, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
149 if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
152 if (sscanf(args, "%1023s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) {
153 return strndup(buf, text_buffer_size);
156 if (sscanf(args, "%1023s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
157 return strndup(buf, text_buffer_size);
162 if (sscanf(args, "%x %x %u", first_colour, last_colour, scale) == 3) {
166 if (sscanf(args, "%x %x", first_colour, last_colour) == 2) {
169 if (sscanf(args, "%1023s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
170 return strndup(buf, text_buffer_size);
173 if (sscanf(args, "%1023s %x %x", buf, first_colour, last_colour) == 3) {
174 return strndup(buf, text_buffer_size);
179 if (sscanf(args, "%d,%d %u", h, w, scale) == 3) {
183 if (sscanf(args, "%d,%d", h, w) == 2) {
186 if (sscanf(args, "%1023s %d,%d %u", buf, h, w, scale) < 4) {
188 //TODO: check the return value and throw an error?
189 sscanf(args, "%1023s %d,%d", buf, h, w);
192 return strndup(buf, text_buffer_size);
195 if (buf[0] == '\0') {
198 return strndup(buf, text_buffer_size);
204 * Printing various special text objects
207 static struct special_t *new_special(char *buf, enum special_types t)
209 if (special_count >= max_specials) {
210 CRIT_ERR(NULL, NULL, "too many special things in text");
213 buf[0] = SPECIAL_CHAR;
215 specials[special_count].type = t;
216 return &specials[special_count++];
220 void new_gauge(char *buf, int w, int h, int usage)
222 struct special_t *s = 0;
223 if ((output_methods & TO_X) == 0)
226 s = new_special(buf, GAUGE);
228 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
233 void new_bar(struct text_object *obj, char *buf, int usage)
235 struct special_t *s = 0;
236 struct bar *b = obj->special_data;
238 if ((output_methods & TO_X) == 0)
244 s = new_special(buf, BAR);
246 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
248 s->height = b->height;
251 void new_font(char *buf, char *args)
253 if ((output_methods & TO_X) == 0)
257 struct special_t *s = new_special(buf, FONT);
259 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
260 int tmp = selected_font;
262 selected_font = s->font_added = add_font(args);
266 struct special_t *s = new_special(buf, FONT);
267 int tmp = selected_font;
269 selected_font = s->font_added = 0;
274 static void graph_append(struct special_t *graph, double f, char showaslog)
284 if (!graph->scaled && f > graph->graph_scale) {
285 f = graph->graph_scale;
288 graph->graph[0] = f; /* add new data */
289 /* shift all the data by 1 */
290 for (i = graph->graph_width - 1; i > 0; i--) {
291 graph->graph[i] = graph->graph[i - 1];
292 if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) {
293 /* check if we need to update the scale */
294 graph->graph_scale = graph->graph[i - 1];
297 if (graph->scaled && graph->graph[graph->graph_width] > graph->graph_scale) {
298 /* check if we need to update the scale */
299 graph->graph_scale = graph->graph[graph->graph_width];
303 void new_graph(char *buf, int w, int h, unsigned int first_colour,
304 unsigned int second_colour, double i, int scale, int append, char showaslog, char tempgrad)
306 struct special_t *s = 0;
308 if ((output_methods & TO_X) == 0)
311 s = new_special(buf, GRAPH);
314 if (s->graph == NULL) {
315 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
316 // subtract 2 for the box
317 s->graph_width = s->width /* - 2 */;
319 s->graph_width = MAX_GRAPH_DEPTH - 2;
321 s->graph = malloc(s->graph_width * sizeof(double));
322 memset(s->graph, 0, s->graph_width * sizeof(double));
323 s->graph_scale = 100;
326 s->first_colour = adjust_colours(first_colour);
327 s->last_colour = adjust_colours(second_colour);
330 s->graph_scale = scale;
337 s->tempgrad = tempgrad;
339 s->graph_width = s->width - 2; // subtract 2 for rectangle around
343 s->graph_scale = log10(s->graph_scale + 1);
347 graph_append(s, i, showaslog);
351 void new_hr(char *buf, int a)
353 if ((output_methods & TO_X) == 0)
356 new_special(buf, HORIZONTAL_LINE)->height = a;
359 void new_stippled_hr(char *buf, int a, int b)
361 struct special_t *s = 0;
363 if ((output_methods & TO_X) == 0)
366 s = new_special(buf, STIPPLED_HR);
373 void new_fg(char *buf, long c)
376 if (output_methods & TO_X)
377 new_special(buf, FG)->arg = c;
380 if (output_methods & TO_NCURSES)
381 new_special(buf, FG)->arg = c;
388 void new_bg(char *buf, long c)
390 if ((output_methods & TO_X) == 0)
393 new_special(buf, BG)->arg = c;
397 void new_bar_in_shell(struct text_object *obj, char* buffer, int buf_max_size, double usage)
399 struct bar *b = obj->special_data;
407 width = DEFAULT_BAR_WIDTH_NO_X;
409 if(width<=buf_max_size){
410 int i = 0, j = 0, scaledusage = round_to_int( usage * width / 100);
413 #pragma omp parallel for schedule(dynamic,10)
414 #endif /* HAVE_OPENMP */
415 for(i=0; i<(int)scaledusage; i++) {
418 /* gcc seems to think i is not initialized properly :/ */
421 #pragma omp parallel for schedule(dynamic,10)
422 #endif /* HAVE_OPENMP */
423 for(i = j/* cheats */; i < width; i++) {
430 void new_outline(char *buf, long c)
432 new_special(buf, OUTLINE)->arg = c;
435 void new_offset(char *buf, long c)
437 new_special(buf, OFFSET)->arg = c;
440 void new_voffset(char *buf, long c)
442 new_special(buf, VOFFSET)->arg = c;
445 void new_alignr(char *buf, long c)
447 new_special(buf, ALIGNR)->arg = c;
450 // A postive offset pushes the text further left
451 void new_alignc(char *buf, long c)
453 new_special(buf, ALIGNC)->arg = c;
456 void new_goto(char *buf, long c)
458 new_special(buf, GOTO)->arg = c;
461 void new_tab(char *buf, int a, int b)
463 struct special_t *s = new_special(buf, TAB);