1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
35 unsigned int max_specials = MAX_SPECIALS_DEFAULT;
37 /* create specials array on heap instead of stack with introduction of
39 struct special_t *specials = NULL;
41 unsigned int special_count;
44 * Scanning arguments to various special text objects
46 const char *scan_bar(const char *args, int *w, int *h)
48 /* zero width means all space that is available */
51 /* bar's argument is either height or height,width */
55 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1) {
56 sscanf(args, "%d %n", h, &n);
64 char *scan_font(const char *args)
67 return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
73 char *scan_graph(const char *args, int *w, int *h,
74 unsigned int *first_colour, unsigned int *last_colour,
75 unsigned int *scale, char *showaslog)
77 const char *nographtype;
81 /* zero width means all space that is available */
88 //set showaslog and place the rest of the args in nographtype
89 if(strcasecmp(args, LOGGRAPH) == EQUAL) {
92 }else if(strcasecmp(args, NORMGRAPH) == EQUAL) {
95 }else if(strncasecmp(args, LOGGRAPH" ", strlen(LOGGRAPH) + 1 ) == EQUAL) {
97 nographtype = &args[strlen(LOGGRAPH) + 1];
98 }else if(strncasecmp(args, NORMGRAPH" ", strlen(NORMGRAPH) + 1 ) == EQUAL) {
100 nographtype = &args[strlen(NORMGRAPH) + 1];
105 DBGP("printing graph as %s, other args are: %s", (*showaslog ? "log" : "normal"), nographtype);
106 //check the rest of the args
107 if (sscanf(nographtype, "%d,%d %x %x %u", h, w, first_colour, last_colour, scale) == 5) {
111 if (sscanf(nographtype, "%d,%d %x %x", h, w, first_colour, last_colour) == 4) {
114 if (sscanf(nographtype, "%63s %d,%d %x %x %u", buf, h, w, first_colour, last_colour, scale) == 6) {
115 return strndup(buf, text_buffer_size);
118 if (sscanf(nographtype, "%63s %d,%d %x %x", buf, h, w, first_colour, last_colour) == 5) {
119 return strndup(buf, text_buffer_size);
124 if (sscanf(nographtype, "%x %x %u", first_colour, last_colour, scale) == 3) {
128 if (sscanf(nographtype, "%x %x", first_colour, last_colour) == 2) {
131 if (sscanf(nographtype, "%63s %x %x %u", buf, first_colour, last_colour, scale) == 4) {
132 return strndup(buf, text_buffer_size);
135 if (sscanf(nographtype, "%63s %x %x", buf, first_colour, last_colour) == 3) {
136 return strndup(buf, text_buffer_size);
141 if (sscanf(nographtype, "%d,%d %u", h, w, scale) == 3) {
145 if (sscanf(nographtype, "%d,%d", h, w) == 2) {
148 if (sscanf(nographtype, "%63s %d,%d %u", buf, h, w, scale) < 4) {
150 //TODO: check the return value and throw an error?
151 sscanf(nographtype, "%63s %d,%d", buf, h, w);
154 return strndup(buf, text_buffer_size);
157 if (buf[0] == '\0') {
160 return strndup(buf, text_buffer_size);
165 * Printing various special text objects
168 static struct special_t *new_special(char *buf, enum special_types t)
170 if (special_count >= max_specials) {
171 CRIT_ERR("too many special things in text");
174 buf[0] = SPECIAL_CHAR;
176 specials[special_count].type = t;
177 return &specials[special_count++];
180 void new_bar(char *buf, int w, int h, int usage)
182 struct special_t *s = new_special(buf, BAR);
184 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
189 void new_font(char *buf, char *args)
191 if ((output_methods & TO_X) == 0)
195 struct special_t *s = new_special(buf, FONT);
197 if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
198 int tmp = selected_font;
200 selected_font = s->font_added = addfont(args);
205 struct special_t *s = new_special(buf, FONT);
206 int tmp = selected_font;
208 selected_font = s->font_added = 0;
218 static void graph_append(struct special_t *graph, double f, char showaslog)
228 if (!graph->scaled && f > graph->graph_scale) {
229 f = graph->graph_scale;
232 /* Already happens in new_graph
234 graph->graph_scale = 1;
237 graph->graph[0] = f; /* add new data */
238 /* shift all the data by 1 */
239 for (i = graph->graph_width - 1; i > 0; i--) {
240 graph->graph[i] = graph->graph[i - 1];
241 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
242 /* check if we need to update the scale */
243 graph->graph_scale = graph->graph[i];
248 void new_graph(char *buf, int w, int h, unsigned int first_colour,
249 unsigned int second_colour, double i, int scale, int append, char showaslog)
251 struct special_t *s = new_special(buf, GRAPH);
254 if (s->graph == NULL) {
255 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
256 // subtract 2 for the box
257 s->graph_width = s->width /* - 2 */;
259 s->graph_width = MAX_GRAPH_DEPTH - 2;
261 s->graph = malloc(s->graph_width * sizeof(double));
262 memset(s->graph, 0, s->graph_width * sizeof(double));
263 s->graph_scale = 100;
266 s->first_colour = adjust_colors(first_colour);
267 s->last_colour = adjust_colors(second_colour);
270 s->graph_scale = scale;
278 s->graph_width = s->width - 2; // subtract 2 for rectangle around
282 s->graph_scale = log10(s->graph_scale + 1);
286 graph_append(s, i, showaslog);
290 void new_hr(char *buf, int a)
292 new_special(buf, HORIZONTAL_LINE)->height = a;
295 void new_stippled_hr(char *buf, int a, int b)
297 struct special_t *s = new_special(buf, STIPPLED_HR);
303 void new_fg(char *buf, long c)
305 new_special(buf, FG)->arg = c;
308 void new_bg(char *buf, long c)
310 new_special(buf, BG)->arg = c;
313 void new_outline(char *buf, long c)
315 new_special(buf, OUTLINE)->arg = c;
318 void new_offset(char *buf, long c)
320 new_special(buf, OFFSET)->arg = c;
323 void new_voffset(char *buf, long c)
325 new_special(buf, VOFFSET)->arg = c;
328 void new_alignr(char *buf, long c)
330 new_special(buf, ALIGNR)->arg = c;
333 // A postive offset pushes the text further left
334 void new_alignc(char *buf, long c)
336 new_special(buf, ALIGNC)->arg = c;
339 void new_goto(char *buf, long c)
341 new_special(buf, GOTO)->arg = c;
344 void new_tab(char *buf, int a, int b)
346 struct special_t *s = new_special(buf, TAB);