File: leds.c

package info (click to toggle)
elinks 0.13~20190125-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 17,244 kB
  • sloc: ansic: 102,743; sh: 5,265; python: 3,940; perl: 2,175; makefile: 970; pascal: 930; yacc: 295; lisp: 125; ruby: 70; awk: 65
file content (454 lines) | stat: -rw-r--r-- 10,505 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
/* These cute LightEmittingDiode-like indicators. */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif

#include "elinks.h"

#include "bfu/leds.h"
#include "config/options.h"
#include "document/document.h"
#include "document/view.h"
#include "intl/gettext/libintl.h"
#include "main/module.h"
#include "main/timer.h"
#include "session/session.h"
#include "terminal/draw.h"
#include "terminal/tab.h"
#include "terminal/terminal.h"
#include "terminal/window.h"
#include "util/color.h"
#include "util/error.h"
#include "util/time.h"
#include "viewer/timer.h"

/* Current leds allocation:
 * 0 - SSL connection indicator
 * 1 - Insert-mode indicator
 * 2 - JavaScript Error indicator
 * 3 - JavaScript pop-up blocking indicator
 * 4 - unused, reserved for Lua
 * 5 - download in progress */

/* XXX: Currently, the leds toggling is quite hackish, some more work should go
 * to it (ie. some led hooks called in sync_leds() to light the leds
 * dynamically. --pasky */

/* Always reset led to '-' when not used anymore. */

/* If we would do real protection, we would do this as array of pointers. This
 * way someone can just get any struct led and add/subscribe appropriate struct
 * led for his control; however, I bet on programmers' responsibility rather,
 * and hope that everyone will abide the "rules". */

static int timer_duration_backup = 0;

static timer_id_T redraw_timer = TIMER_ID_UNDEF;
static int drawing = 0;

static void redraw_leds(void *);

enum led_option {
	LEDS_CLOCK_TREE,
	LEDS_CLOCK_ENABLE,
	LEDS_CLOCK_FORMAT,
	LEDS_CLOCK_ALIAS,

	LEDS_SHOW_IP_ENABLE,

	LEDS_PANEL_TREE,
	LEDS_PANEL_ENABLE,

	LEDS_OPTIONS,
};

static union option_info led_options[] = {
	INIT_OPT_TREE("ui", N_("Clock"),
		"clock", 0, N_("Digital clock in the status bar.")),

	INIT_OPT_BOOL("ui.clock", N_("Enable"),
		"enable", 0, 0,
		N_("Whether to display a digital clock in the status bar.")),

	INIT_OPT_STRING("ui.clock", N_("Format"),
		"format", 0, "[%H:%M]",
		N_("Format string for the digital clock. See the strftime(3) "
		"manpage for details.")),


	/* Compatibility alias. Added: 2004-04-22, 0.9.CVS. */
	INIT_OPT_ALIAS("ui.timer", "clock", 0, "ui.clock"),

	INIT_OPT_BOOL("ui", N_("Show IP"),
		"show_ip", 0, 0,
		N_("Whether to display IP of the document in the status bar.")),


	INIT_OPT_TREE("ui", N_("LEDs"),
		"leds", 0,
		N_("LEDs (visual indicators) options.")),

	INIT_OPT_BOOL("ui.leds", N_("Enable"),
		"enable", 0, 0,
		N_("Enable LEDs. These visual indicators will inform you "
		"about various states.")),

	NULL_OPTION_INFO,
};

#define get_opt_leds(which)		led_options[(which)].option.value
#define get_leds_clock_enable()		get_opt_leds(LEDS_CLOCK_ENABLE).number
#define get_leds_clock_format()		get_opt_leds(LEDS_CLOCK_FORMAT).string
#define get_leds_panel_enable()		get_opt_leds(LEDS_PANEL_ENABLE).number
#define get_leds_show_ip_enable()	get_opt_leds(LEDS_SHOW_IP_ENABLE).number

void
init_leds(struct module *module)
{
	timer_duration_backup = 0;

	/* We can't setup timer here, because we may not manage to startup in
	 * 100ms and we will get to problems when we will call draw_leds() on
	 * uninitialized terminal. So, we will wait for draw_leds(). */
}

void
done_leds(struct module *module)
{
	kill_timer(&redraw_timer);
}

void
set_led_value(struct led *led, unsigned char value)
{
	if (value != led->value__) {
		led->value__ = value;
		led->value_changed__ = 1;
	}
}

unsigned char
get_led_value(struct led *led)
{
	return led->value__;
}

void
unset_led_value(struct led *led)
{
	set_led_value(led, '-');
}


void
init_led_panel(struct led_panel *leds)
{
	int i;

	for (i = 0; i < LEDS_COUNT; i++) {
		leds->leds[i].used__ = 0;
		unset_led_value(&leds->leds[i]);
	}
}

static int
draw_timer(struct terminal *term, int xpos, int ypos, struct color_pair *color)
{
	unsigned char s[64];
	int i, length;

	snprintf(s, sizeof(s), "[%d]", get_timer_duration());
	length = strlen(s);

	for (i = length - 1; i >= 0; i--)
		draw_char(term, xpos - (length - i), ypos, s[i], 0, color);

	return length;
}

static int
draw_show_ip(struct session *ses, int xpos, int ypos, struct color_pair *color)
{

	if (ses->doc_view && ses->doc_view->document && ses->doc_view->document->ip) {
		struct terminal *term = ses->tab->term;
		unsigned char *s = ses->doc_view->document->ip;
		int length = strlen(s);
		int i;

		for (i = length - 1; i >= 0; i--)
			draw_char(term, xpos - (length - i), ypos, s[i], 0, color);

		return length;
	}
	return 0;
}


#ifdef HAVE_STRFTIME
static int
draw_clock(struct terminal *term, int xpos, int ypos, struct color_pair *color)
{
	unsigned char s[64];
	time_t curtime = time(NULL);
	struct tm *loctime = localtime(&curtime);
	int i, length;

	length = strftime(s, sizeof(s), get_leds_clock_format(), loctime);
	s[length] = '\0';
	for (i = length - 1; i >= 0; i--)
		draw_char(term, xpos - (length - i), ypos, s[i], 0, color);

	return length;
}
#endif

static milliseconds_T
compute_redraw_interval(void)
{
	if (are_there_downloads())
		return 100;

	/* TODO: Check whether the time format includes seconds.  If not,
	 * return milliseconds to next minute. */

	if (get_leds_clock_enable())
		return 1000;

	return 0;
}

void
draw_leds(struct session *ses)
{
	struct terminal *term = ses->tab->term;
	struct color_pair *led_color = NULL;
	int i;
	int xpos = term->width - LEDS_COUNT - 3;
	int ypos = term->height - 1;

	term->leds_length = 0;

	/* This should be done elsewhere, but this is very nice place where we
	 * could do that easily. */
	if (get_opt_int("ui.timer.enable", NULL) == 2) {
		led_color = get_bfu_color(term, "status.status-text");
		if (!led_color) goto end;

		term->leds_length += draw_timer(term, xpos, ypos, led_color);
	}

	if (!get_leds_panel_enable()) return;

	if (!led_color) {
		led_color = get_bfu_color(term, "status.status-text");
		if (!led_color) goto end;
	}

#ifdef HAVE_STRFTIME
	if (get_leds_clock_enable()) {
		term->leds_length += draw_clock(term, xpos - term->leds_length, ypos, led_color);
	}
#endif

	if (get_leds_show_ip_enable()) {
		struct color_pair *color = get_bfu_color(term, "status.showip-text");

		if (color) term->leds_length += draw_show_ip(ses, xpos - term->leds_length, ypos, color);
	}

	/* We must shift the whole thing by one char to left, because we don't
	 * draft the char in the right-down corner :(. */

	draw_char(term, xpos, ypos, '[', 0, led_color);

	for (i = 0; i < LEDS_COUNT; i++) {
		struct led *led = &ses->status.leds.leds[i];

		draw_char(term, xpos + i + 1, ypos, led->value__, 0, led_color);
		led->value_changed__ = 0;
	}

	draw_char(term, xpos + LEDS_COUNT + 1, ypos, ']', 0, led_color);

	term->leds_length += LEDS_COUNT + 2;

end:
#ifdef CONFIG_UTF8
	if (term->utf8_cp) {
		struct el_box box;
		set_box(&box, xpos, ypos, LEDS_COUNT + 1, 1);
		fix_dwchar_around_box(term, &box, 0, 0, 0);
	}
#endif
	/* Redraw each 100ms. */
	if (!drawing && redraw_timer == TIMER_ID_UNDEF) {
		milliseconds_T delay = compute_redraw_interval();

		if (delay)
			install_timer(&redraw_timer, delay, redraw_leds, NULL);
	}
}

/* Determine if leds redrawing is necessary. Returns non-zero if so. */
static int
sync_leds(struct session *ses)
{
	int i;
	int timer_duration;

#ifdef HAVE_STRFTIME
	/* Check if clock was enabled and update if needed. */
	if (get_leds_clock_enable()) {
		/* We _always_ update when clock is enabled
		 * Not perfect. --Zas */
		return 1;
	}
#endif

	for (i = 0; i < LEDS_COUNT; i++) {
		struct led *led = &ses->status.leds.leds[i];

		if (led->value_changed__)
			return 1;
	}

	/* Check if timer was updated. */
	timer_duration = get_timer_duration();
	if (timer_duration_backup != timer_duration) {
		timer_duration_backup = timer_duration;
		return 1;
	}

	return 0;
}

static void
update_download_led(struct session *ses)
{
	struct session_status *status = &ses->status;

	if (are_there_downloads()) {
		unsigned char led = get_led_value(status->download_led);

		switch (led) {
			case '-' : led = '\\'; break;
			case '\\': led = '|'; break;
			case '|' : led = '/'; break;
			default: led = '-';
		}

		set_led_value(status->download_led, led);
	} else {
		unset_led_value(status->download_led);
	}
}

/* Timer callback for @redraw_timer.  As explained in @install_timer,
 * this function must erase the expired timer ID from all variables.  */
static void
redraw_leds(void *xxx)
{
	struct terminal *term;
	milliseconds_T delay;

	redraw_timer = TIMER_ID_UNDEF;

	if (!get_leds_panel_enable()
	    && get_opt_int("ui.timer.enable", NULL) != 2) {
		return;
	}

	delay = compute_redraw_interval();
	if (delay)
		install_timer(&redraw_timer, delay, redraw_leds, NULL);

	if (drawing) return;
	drawing = 1;

	foreach (term, terminals) {
		struct session *ses;
		struct window *win;
		
		if (list_empty(term->windows)) continue;
		
		win = get_current_tab(term);
		assert(win);
		ses = win->data;

		update_download_led(ses);
		if (!sync_leds(ses))
			continue;
		redraw_terminal(term);
		draw_leds(ses);
	}
	drawing = 0;
}

void
menu_leds_info(struct terminal *term, void *xxx, void *xxxx)
{
	/* If LEDs ever get more dynamic we might have to change this, but it
	 * should do for now. --jonas */
	info_box(term, MSGBOX_FREE_TEXT | MSGBOX_SCROLLABLE,
	 	 N_("LED indicators"), ALIGN_LEFT,
		 msg_text(term, N_("What the different LEDs indicate:\n"
		 	"\n"
			"[SIJP--]\n"
			" |||||`- Download in progress\n"
			" ||||`-- Unused\n"
			" |||`--- A JavaScript pop-up window was blocked\n"
			" ||`---- A JavaScript error has occurred\n"
			" |`----- The state of insert mode for text-input form-fields\n"
			" |       'i' means modeless, 'I' means insert mode is on\n"
			" `------ Whether an SSL connection was used\n"
			"\n"
			"'-' generally indicates that the LED is off.")));
}


struct led *
register_led(struct session *ses, int number)
{
	struct led *led;

	if (number >= LEDS_COUNT || number < 0)
		return NULL;

	led = &ses->status.leds.leds[number];
	if (led->used__)
		return NULL;

	led->used__ = 1;

	return led;
}

void
unregister_led(struct led *led)
{
	assertm(led->used__, "Attempted to unregister unused led!");
	led->used__ = 0;
	unset_led_value(led);
}

struct module leds_module = struct_module(
	/* name: */		N_("LED indicators"),
	/* options: */		led_options,
	/* events: */		NULL,
	/* submodules: */	NULL,
	/* data: */		NULL,
	/* init: */		init_leds,
	/* done: */		done_leds
);