File: specials.c

package info (click to toggle)
conky 1.9.0-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,764 kB
  • ctags: 3,934
  • sloc: ansic: 27,033; sh: 11,594; xml: 6,001; makefile: 283; python: 16
file content (649 lines) | stat: -rw-r--r-- 14,652 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
 * vim: ts=4 sw=4 noet ai cindent syntax=c
 *
 * Conky, a system monitor, based on torsmo
 *
 * Any original torsmo code is licensed under the BSD license
 *
 * All code written since the fork of torsmo is licensed under the GPL
 *
 * Please see COPYING for details
 *
 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
 *	(see AUTHORS)
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include "conky.h"
#include "colours.h"
#ifdef X11
#include "fonts.h"
#endif /* X11 */
#include "logging.h"
#include "specials.h"
#include <math.h>
#include <ctype.h>

/* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
int max_specials = MAX_SPECIALS_DEFAULT;

/* create specials array on heap instead of stack with introduction of
 * max_specials */
struct special_t *specials = NULL;

int special_count;

int default_bar_width = 0, default_bar_height = 6;
#ifdef X11
int default_graph_width = 0, default_graph_height = 25;
#endif /* X11 */
int default_gauge_width = 40, default_gauge_height = 25;

/*
 * Special data typedefs
 */

struct bar {
	int width, height;
};

struct gauge {
	int width, height;
};

struct graph {
	int width, height;
	unsigned int first_colour, last_colour;
	unsigned int scale, showaslog;
	char tempgrad, dotgraph;
};

struct stippled_hr {
	int height, arg;
};

struct tab {
	int width, arg;
};

/*
 * Scanning arguments to various special text objects
 */

const char *scan_gauge(struct text_object *obj, const char *args)
{
	struct gauge *g;

	g = malloc(sizeof(struct gauge));
	memset(g, 0, sizeof(struct gauge));

	/*width and height*/
	g->width = default_gauge_width;
	g->height = default_gauge_height;

	/* gauge's argument is either height or height,width */
	if (args) {
		int n = 0;

		if (sscanf(args, "%d,%d %n", &g->height, &g->width, &n) <= 1) {
			if (sscanf(args, "%d %n", &g->height, &n) == 2) {
				g->width = g->height; /*square gauge*/
			}
		}
		args += n;
	}

	obj->special_data = g;
	return args;
}

const char *scan_bar(struct text_object *obj, const char *args)
{
	struct bar *b;

	b = malloc(sizeof(struct bar));
	memset(b, 0, sizeof(struct bar));

	/* zero width means all space that is available */
	b->width = default_bar_width;
	b->height = default_bar_height;
	/* bar's argument is either height or height,width */
	if (args) {
		int n = 0;

		if (sscanf(args, "%d,%d %n", &b->height, &b->width, &n) <= 1) {
			sscanf(args, "%d %n", &b->height, &n);
		}
		args += n;
	}

	obj->special_data = b;
	return args;
}

#ifdef X11
char *scan_font(const char *args)
{
	if (args && *args) {
		return strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
	}

	return NULL;
}

char *scan_graph(struct text_object *obj, const char *args, int defscale)
{
	struct graph *g;
	char buf[1024];
	memset(buf, 0, 1024);

	g = malloc(sizeof(struct graph));
	memset(g, 0, sizeof(struct graph));
	obj->special_data = g;

	/* zero width means all space that is available */
	g->width = default_graph_width;
	g->height = default_graph_height;
	g->first_colour = 0;
	g->last_colour = 0;
	g->scale = defscale;
	g->tempgrad = FALSE;
	g->dotgraph = FALSE;
	g->showaslog = FALSE;
	if (args) {
		if (strstr(args, " "TEMPGRAD) || strncmp(args, TEMPGRAD, strlen(TEMPGRAD)) == 0) {
			g->tempgrad = TRUE;
		}
		if (strstr(args, " "DOTGRAPH) || strncmp(args, DOTGRAPH, strlen(DOTGRAPH)) == 0) {
			g->dotgraph = TRUE;
		}
		if (strstr(args, " "LOGGRAPH) || strncmp(args, LOGGRAPH, strlen(LOGGRAPH)) == 0) {
			g->showaslog = TRUE;
		}
		if (sscanf(args, "%d,%d %x %x %u", &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 5) {
			return NULL;
		}
		g->scale = defscale;
		if (sscanf(args, "%d,%d %x %x", &g->height, &g->width, &g->first_colour, &g->last_colour) == 4) {
			return NULL;
		}
		if (sscanf(args, "%1023s %d,%d %x %x %u", buf, &g->height, &g->width, &g->first_colour, &g->last_colour, &g->scale) == 6) {
			return strndup(buf, text_buffer_size);
		}
		g->scale = defscale;
		if (sscanf(args, "%1023s %d,%d %x %x", buf, &g->height, &g->width, &g->first_colour, &g->last_colour) == 5) {
			return strndup(buf, text_buffer_size);
		}
		buf[0] = '\0';
		g->height = 25;
		g->width = 0;
		if (sscanf(args, "%x %x %u", &g->first_colour, &g->last_colour, &g->scale) == 3) {
			return NULL;
		}
		g->scale = defscale;
		if (sscanf(args, "%x %x", &g->first_colour, &g->last_colour) == 2) {
			return NULL;
		}
		if (sscanf(args, "%1023s %x %x %u", buf, &g->first_colour, &g->last_colour, &g->scale) == 4) {
			return strndup(buf, text_buffer_size);
		}
		g->scale = defscale;
		if (sscanf(args, "%1023s %x %x", buf, &g->first_colour, &g->last_colour) == 3) {
			return strndup(buf, text_buffer_size);
		}
		buf[0] = '\0';
		g->first_colour = 0;
		g->last_colour = 0;
		if (sscanf(args, "%d,%d %u", &g->height, &g->width, &g->scale) == 3) {
			return NULL;
		}
		g->scale = defscale;
		if (sscanf(args, "%d,%d", &g->height, &g->width) == 2) {
			return NULL;
		}
		if (sscanf(args, "%1023s %d,%d %u", buf, &g->height, &g->width, &g->scale) < 4) {
			g->scale = defscale;
			//TODO: check the return value and throw an error?
			sscanf(args, "%1023s %d,%d", buf, &g->height, &g->width);
		}

		return strndup(buf, text_buffer_size);
	}

	if (buf[0] == '\0') {
		return NULL;
	} else {
		return strndup(buf, text_buffer_size);
	}
}

// scan_graph is a mess and it does not work for execgraph, so i'll just rewrite it for this case
char *scan_execgraph(struct text_object *obj, const char *arg)
{
	struct graph *g;

	g = malloc(sizeof(struct graph));
	memset(g, 0, sizeof(struct graph));
	obj->special_data = g;

	/* zero width means all space that is available */
	g->width = default_graph_width;
	g->height = default_graph_height;
	g->first_colour = 0;
	g->last_colour = 0;
	g->scale = 100;
	g->tempgrad = FALSE;
	g->showaslog = FALSE;

	while(arg && *arg) {
		while(isspace(*arg)) ++arg;

		if(strncmp(arg, TEMPGRAD, strlen(TEMPGRAD)) == 0 && !isalnum(arg[strlen(TEMPGRAD)])) {
			g->tempgrad = TRUE;
			arg += strlen(TEMPGRAD);
			continue;
		}

		if(strncmp(arg, LOGGRAPH, strlen(LOGGRAPH)) == 0 && !isalnum(arg[strlen(LOGGRAPH)])) {
			g->showaslog = TRUE;
			arg += strlen(LOGGRAPH);
			continue;
		}

		return strdup(arg);
	}

	return NULL;
}
#endif /* X11 */

/*
 * Printing various special text objects
 */

static struct special_t *new_special(char *buf, enum special_types t)
{
	if (special_count >= max_specials) {
		CRIT_ERR(NULL, NULL, "too many special things in text");
	}

	buf[0] = SPECIAL_CHAR;
	buf[1] = '\0';
	specials[special_count].type = t;
	return &specials[special_count++];
}

void new_gauge_in_shell(struct text_object *obj, char *p, int p_max_size, int usage)
{
	static const char *gaugevals[] = { "_. ", "\\. ", " | ", " ./", " ._" };
	(void)obj;

	snprintf(p, p_max_size, "%s", gaugevals[round_to_int((double)usage * 4 / 255)]);
}

#ifdef X11
void new_gauge_in_x11(struct text_object *obj, char *buf, int usage)
{
	struct special_t *s = 0;
	struct gauge *g = obj->special_data;

	if ((output_methods & TO_X) == 0)
		return;

	if (!g)
		return;

	s = new_special(buf, GAUGE);

	s->arg = usage;
	s->width = g->width;
	s->height = g->height;
}
#endif /* X11 */

void new_gauge(struct text_object *obj, char *p, int p_max_size, int usage)
{
	if (!p_max_size)
		return;

	usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);

#ifdef X11
	if (output_methods & TO_X)
		new_gauge_in_x11(obj, p, usage);
	else
#endif /* X11 */
		new_gauge_in_shell(obj, p, p_max_size, usage);
}

#ifdef X11
int find_font(char *name)
{
	int i;
	for (i = 0; i < font_count; i++)
		if (strncmp(name, fonts[i].name, DEFAULT_TEXT_BUFFER_SIZE) == EQUAL)
			return i;
	return 0;
}

void new_font(char *buf, char *args)
{
	if ((output_methods & TO_X) == 0)
		return;

	if (args) {
		struct special_t *s = new_special(buf, FONT);
		int index;
		if (index = find_font(args))
		{
			s->font_added = index;
			return;
		}

		if (s->font_added > font_count || !s->font_added || (strncmp(args, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) {
			int tmp = selected_font;

			selected_font = s->font_added = add_font(args);
			selected_font = tmp;
		}
	} else {
		struct special_t *s = new_special(buf, FONT);
		int tmp = selected_font;

		selected_font = s->font_added = 0;
		selected_font = tmp;
	}
}

static void graph_append(struct special_t *graph, double f, char showaslog)
{
	int i;

	/* do nothing if we don't even have a graph yet */
	if (!graph->graph) return;

	if (showaslog) {
#ifdef MATH
		f = log10(f + 1);
#endif
	}

	if (!graph->scaled && f > graph->graph_scale) {
		f = graph->graph_scale;
	}

	/* shift all the data by 1 */
	for (i = graph->graph_allocated - 1; i > 0; i--) {
		graph->graph[i] = graph->graph[i - 1];
		if (graph->scaled && graph->graph[i - 1] > graph->graph_scale) {
			/* check if we need to update the scale */
			graph->graph_scale = graph->graph[i - 1];
		}
	}
	graph->graph[0] = f;	/* add new data */
	if (graph->scaled && graph->graph[0] > graph->graph_scale) {
		/* check if we need to update the scale */
		graph->graph_scale = graph->graph[0];
	}
}

void new_graph(struct text_object *obj, char *buf, int buf_max_size, double val)
{
	struct special_t *s = 0;
	struct graph *g = obj->special_data;

	if ((output_methods & TO_X) == 0)
		return;

	if (!g || !buf_max_size)
		return;

	s = new_special(buf, GRAPH);

	s->width = g->width;
	if (s->width) s->graph_width = s->width;

	if (s->graph_width != s->graph_allocated) {
		char *graph = realloc(s->graph, s->graph_width * sizeof(double));
		DBGP("reallocing graph from %d to %d", s->graph_allocated, s->graph_width);
		if (!s->graph) {
			/* initialize */
			memset(graph, 0, s->graph_width * sizeof(double));
			s->graph_scale = 100;
		} else {
			if (s->graph_width > s->graph_allocated) {
				/* initialize the new region */
				memset(graph + (s->graph_allocated * sizeof(double)), 0,
						(s->graph_width - s->graph_allocated) *
						sizeof(double));
			}
		}
		s->graph = graph;
		s->graph_allocated = s->graph_width;
	}
	s->height = g->height;
	s->first_colour = adjust_colours(g->first_colour);
	s->last_colour = adjust_colours(g->last_colour);
	if (g->scale != 0) {
		s->scaled = 0;
		s->graph_scale = g->scale;
		s->show_scale = 0;
	} else {
		s->scaled = 1;
		s->graph_scale = 1;
		s->show_scale = 1;
	}
	s->tempgrad = g->tempgrad;
	s->dotgraph = g->dotgraph;
#ifdef MATH
	if (g->showaslog) {
		s->graph_scale = log10(s->graph_scale + 1);
	}
#endif
	graph_append(s, val, g->showaslog);
}

void new_hr(char *buf, int a)
{
	if ((output_methods & TO_X) == 0)
		return;

	new_special(buf, HORIZONTAL_LINE)->height = a;
}

void scan_stippled_hr(struct text_object *obj, const char *arg)
{
	struct stippled_hr *sh;

	sh = malloc(sizeof(struct stippled_hr));
	memset(sh, 0, sizeof(struct stippled_hr));

	sh->arg = get_stippled_borders();
	sh->height = 1;

	if (arg) {
		if (sscanf(arg, "%d %d", &sh->arg, &sh->height) != 2) {
			sscanf(arg, "%d", &sh->height);
		}
	}
	if (sh->arg <= 0) {
		sh->arg = 1;
	}
	obj->special_data = sh;
}

void new_stippled_hr(struct text_object *obj, char *buf)
{
	struct special_t *s = 0;
	struct stippled_hr *sh = obj->special_data;

	if ((output_methods & TO_X) == 0)
		return;

	if (!sh)
		return;

	s = new_special(buf, STIPPLED_HR);

	s->height = sh->height;
	s->arg = sh->arg;
}
#endif /* X11 */

void new_fg(char *buf, long c)
{
#ifdef X11
	if (output_methods & TO_X)
		new_special(buf, FG)->arg = c;
#endif /* X11 */
#ifdef NCURSES
	if (output_methods & TO_NCURSES)
		new_special(buf, FG)->arg = c;
#endif /* NCURSES */
	UNUSED(buf);
	UNUSED(c);
}

#ifdef X11
void new_bg(char *buf, long c)
{
	if ((output_methods & TO_X) == 0)
		return;

	new_special(buf, BG)->arg = c;
}
#endif /* X11 */

static void new_bar_in_shell(struct text_object *obj, char* buffer, int buf_max_size, double usage)
{
	struct bar *b = obj->special_data;
	int width, i, scaledusage;

	if (!b)
		return;

	width = b->width;
	if (!width)
		width = DEFAULT_BAR_WIDTH_NO_X;

	if (width > buf_max_size)
		width = buf_max_size;

	scaledusage = round_to_int( usage * width / 255);

	for (i = 0; i < scaledusage; i++)
		buffer[i] = '#';

	for (; i < width; i++)
		buffer[i] = '_';

	buffer[i] = 0;
}

#ifdef X11
static void new_bar_in_x11(struct text_object *obj, char *buf, int usage)
{
	struct special_t *s = 0;
	struct bar *b = obj->special_data;

	s = new_special(buf, BAR);

	s->arg = usage;
	s->width = b ?  b->width : default_bar_width;
	s->height = b ? b->height : default_bar_height;
}
#endif /* X11 */

/* usage is in range [0,255] */
void new_bar(struct text_object *obj, char *p, int p_max_size, int usage)
{
	if (!p_max_size)
		return;

	usage = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);

#ifdef X11
	if ((output_methods & TO_X))
		new_bar_in_x11(obj, p, usage);
	else
#endif /* X11 */
		new_bar_in_shell(obj, p, p_max_size, usage);
}

void new_outline(char *buf, long c)
{
	new_special(buf, OUTLINE)->arg = c;
}

void new_offset(char *buf, long c)
{
	new_special(buf, OFFSET)->arg = c;
}

void new_voffset(char *buf, long c)
{
	new_special(buf, VOFFSET)->arg = c;
}

void new_alignr(char *buf, long c)
{
	new_special(buf, ALIGNR)->arg = c;
}

// A postive offset pushes the text further left
void new_alignc(char *buf, long c)
{
	new_special(buf, ALIGNC)->arg = c;
}

void new_goto(char *buf, long c)
{
	new_special(buf, GOTO)->arg = c;
}

void scan_tab(struct text_object *obj, const char *arg)
{
	struct tab *t;

	t = malloc(sizeof(struct tab));
	memset(t, 0, sizeof(struct tab));

	t->width = 10;
	t->arg = 0;

	if (arg) {
		if (sscanf(arg, "%d %d", &t->width, &t->arg) != 2) {
			sscanf(arg, "%d", &t->arg);
		}
	}
	if (t->width <= 0) {
		t->width = 1;
	}
	obj->special_data = t;
}

void new_tab(struct text_object *obj, char *buf)
{
	struct special_t *s = 0;
	struct tab *t = obj->special_data;

	if (!t)
		return;

	s = new_special(buf, TAB);
	s->width = t->width;
	s->arg = t->arg;
}