1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Any original torsmo code is licensed under the BSD license
7 * All code written since the fork of torsmo is licensed under the GPL
9 * Please see COPYING for details
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14 * All rights reserved.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * vim: ts=4 sw=4 noet ai cindent syntax=c
40 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
41 int max_specials = MAX_SPECIALS_DEFAULT;
43 /* create specials array on heap instead of stack with introduction of
45 struct special_t *specials = NULL;
49 int default_bar_width = 0, default_bar_height = 6;
51 int default_graph_width = 0, default_graph_height = 25;
52 int default_gauge_width = 40, default_gauge_height = 25;
56 * Scanning arguments to various special text objects
60 const char *scan_gauge(const char *args, int *w, int *h)
63 *w = default_gauge_width;
64 *h = default_gauge_height;
66 /* gauge's argument is either height or height,width */
70 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
71 if (sscanf(args, "%d %n", h, &n) == 2) {
72 *w = *h; /*square gauge*/
82 const char *scan_bar(const char *args, int *w, int *h)
84 /* zero width means all space that is available */
85 *w = default_bar_width;
86 *h = default_bar_height;
87 /* bar's argument is either height or height,width */
91 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
92 sscanf(args, "%d %n", h, &n);
101 char *scan_font(const char *args)
104 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
110 char *scan_graph(const char *args, int *w, int *h,
111 unsigned int *first_colour, unsigned int *last_colour,
112 unsigned int *scale, char *showaslog, char *tempgrad)
115 memset(buf, 0, 1024);
117 /* zero width means all space that is available */
118 *w = default_graph_width;
119 *h = default_graph_height;
126 if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) {
129 if (strstr(args, " "LOGGRAPH) || strncmp(args, LOGGRAPH, strlen(LOGGRAPH)) == 0) {
132 if (sscanf(args, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
136 if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
139 if (sscanf(args, "%1023s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) {
140 return strndup(buf, text_buffer_size);
143 if (sscanf(args, "%1023s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
144 return strndup(buf, text_buffer_size);
149 if (sscanf(args, "%x %x %u", first_colour, last_colour, scale) == 3) {
153 if (sscanf(args, "%x %x", first_colour, last_colour) == 2) {
156 if (sscanf(args, "%1023s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
157 return strndup(buf, text_buffer_size);
160 if (sscanf(args, "%1023s %x %x", buf, first_colour, last_colour) == 3) {
161 return strndup(buf, text_buffer_size);
166 if (sscanf(args, "%d,%d %u", h, w, scale) == 3) {
170 if (sscanf(args, "%d,%d", h, w) == 2) {
173 if (sscanf(args, "%1023s %d,%d %u", buf, h, w, scale) < 4) {
175 //TODO: check the return value and throw an error?
176 sscanf(args, "%1023s %d,%d", buf, h, w);
179 return strndup(buf, text_buffer_size);
182 if (buf[0] == '\0') {
185 return strndup(buf, text_buffer_size);
191 * Printing various special text objects
194 static struct special_t *new_special(char *buf, enum special_types t)
196 if (special_count >= max_specials) {
197 CRIT_ERR(NULL, NULL, "too many special things in text");
200 buf[0] = SPECIAL_CHAR;
202 specials[special_count].type = t;
203 return &specials[special_count++];
207 void new_gauge(char *buf, int w, int h, int usage)
209 struct special_t *s = 0;
210 if ((output_methods & TO_X) == 0)
213 s = new_special(buf, GAUGE);
215 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
220 void new_bar(char *buf, int w, int h, int usage)
222 struct special_t *s = 0;
224 if ((output_methods & TO_X) == 0)
227 s = new_special(buf, BAR);
229 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
234 void new_font(char *buf, char *args)
236 if ((output_methods & TO_X) == 0)
240 struct special_t *s = new_special(buf, FONT);
242 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
243 int tmp = selected_font;
245 selected_font = s->font_added = add_font(args);
249 struct special_t *s = new_special(buf, FONT);
250 int tmp = selected_font;
252 selected_font = s->font_added = 0;
257 static void graph_append(struct special_t *graph, double f, char showaslog)
267 if (!graph->scaled && f > graph->graph_scale) {
268 f = graph->graph_scale;
271 graph->graph[0] = f; /* add new data */
272 /* shift all the data by 1 */
273 for (i = graph->graph_width - 1; i > 0; i--) {
274 graph->graph[i] = graph->graph[i - 1];
275 if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) {
276 /* check if we need to update the scale */
277 graph->graph_scale = graph->graph[i - 1];
280 if (graph->scaled && graph->graph[graph->graph_width] > graph->graph_scale) {
281 /* check if we need to update the scale */
282 graph->graph_scale = graph->graph[graph->graph_width];
286 void new_graph(char *buf, int w, int h, unsigned int first_colour,
287 unsigned int second_colour, double i, int scale, int append, char showaslog, char tempgrad)
289 struct special_t *s = 0;
291 if ((output_methods & TO_X) == 0)
294 s = new_special(buf, GRAPH);
297 if (s->graph == NULL) {
298 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
299 // subtract 2 for the box
300 s->graph_width = s->width /* - 2 */;
302 s->graph_width = MAX_GRAPH_DEPTH - 2;
304 s->graph = malloc(s->graph_width * sizeof(double));
305 memset(s->graph, 0, s->graph_width * sizeof(double));
306 s->graph_scale = 100;
309 s->first_colour = adjust_colours(first_colour);
310 s->last_colour = adjust_colours(second_colour);
313 s->graph_scale = scale;
320 s->tempgrad = tempgrad;
322 s->graph_width = s->width - 2; // subtract 2 for rectangle around
326 s->graph_scale = log10(s->graph_scale + 1);
330 graph_append(s, i, showaslog);
334 void new_hr(char *buf, int a)
336 if ((output_methods & TO_X) == 0)
339 new_special(buf, HORIZONTAL_LINE)->height = a;
342 void new_stippled_hr(char *buf, int a, int b)
344 struct special_t *s = 0;
346 if ((output_methods & TO_X) == 0)
349 s = new_special(buf, STIPPLED_HR);
356 void new_fg(char *buf, long c)
359 if (output_methods & TO_X)
360 new_special(buf, FG)->arg = c;
363 if (output_methods & TO_NCURSES)
364 new_special(buf, FG)->arg = c;
371 void new_bg(char *buf, long c)
373 if ((output_methods & TO_X) == 0)
376 new_special(buf, BG)->arg = c;
380 void new_bar_in_shell(char* buffer, int buf_max_size, double usage, int width)
382 if(width<=buf_max_size){
383 int i = 0, j = 0, scaledusage = round_to_int( usage * width / 100);
386 #pragma omp parallel for schedule(dynamic,10)
387 #endif /* HAVE_OPENMP */
388 for(i=0; i<(int)scaledusage; i++) {
391 /* gcc seems to think i is not initialized properly :/ */
394 #pragma omp parallel for schedule(dynamic,10)
395 #endif /* HAVE_OPENMP */
396 for(i = j/* cheats */; i < width; i++) {
403 void new_outline(char *buf, long c)
405 new_special(buf, OUTLINE)->arg = c;
408 void new_offset(char *buf, long c)
410 new_special(buf, OFFSET)->arg = c;
413 void new_voffset(char *buf, long c)
415 new_special(buf, VOFFSET)->arg = c;
418 void new_alignr(char *buf, long c)
420 new_special(buf, ALIGNR)->arg = c;
423 // A postive offset pushes the text further left
424 void new_alignc(char *buf, long c)
426 new_special(buf, ALIGNC)->arg = c;
429 void new_goto(char *buf, long c)
431 new_special(buf, GOTO)->arg = c;
434 void new_tab(char *buf, int a, int b)
436 struct special_t *s = new_special(buf, TAB);