summaryrefslogtreecommitdiff
path: root/src/fe-text/statusbar-items.c
blob: de4499b4aeb7b3be00e2308cacf2ba5c4aa94c91 (plain)
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
/*
 statusbar-items.c : irssi

    Copyright (C) 1999-2001 Timo Sirainen

    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 2 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, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "module.h"
#include "signals.h"
#include "settings.h"
#include "servers.h"

#include "themes.h"
#include "statusbar.h"
#include "gui-entry.h"
#include "gui-windows.h"

/* how often to redraw lagging time (seconds) */
#define LAG_REFRESH_TIME 10

static GList *activity_list;
static guint8 actlist_sort;
static GSList *more_visible; /* list of MAIN_WINDOW_RECs which have --more-- */
static GHashTable *input_entries;
static int last_lag, last_lag_unknown, lag_timeout_tag;

static void item_window_active(SBAR_ITEM_REC *item, int get_size_only)
{
	WINDOW_REC *window;

        window = active_win;
	if (item->bar->parent_window != NULL)
		window = item->bar->parent_window->active;

	if (window != NULL && window->active != NULL) {
		statusbar_item_default_handler(item, get_size_only,
					       NULL, "", TRUE);
	} else if (get_size_only) {
                item->min_size = item->max_size = 0;
	}
}

static void item_window_empty(SBAR_ITEM_REC *item, int get_size_only)
{
	WINDOW_REC *window;

        window = active_win;
	if (item->bar->parent_window != NULL)
		window = item->bar->parent_window->active;

	if (window != NULL && window->active == NULL) {
		statusbar_item_default_handler(item, get_size_only,
					       NULL, "", TRUE);
	} else if (get_size_only) {
                item->min_size = item->max_size = 0;
	}
}

static char *get_activity_list(MAIN_WINDOW_REC *window, int normal, int hilight)
{
        THEME_REC *theme;
	GString *str;
	GString *format;
	GList *tmp;
        char *ret, *name, *value;
        int is_det;
	int add_name = settings_get_bool("actlist_names");

	str = g_string_new(NULL);
	format = g_string_new(NULL);

	theme = window != NULL && window->active != NULL &&
		window->active->theme != NULL ?
		window->active->theme : current_theme;

	for (tmp = activity_list; tmp != NULL; tmp = tmp->next) {
		WINDOW_REC *window = tmp->data;

		is_det = window->data_level >= DATA_LEVEL_HILIGHT;
		if ((!is_det && !normal) || (is_det && !hilight))
                        continue;

                /* comma separator */
		if (str->len > 0) {
			value = theme_format_expand(theme, "{sb_act_sep ,}");
			g_string_append(str, value);
			g_free(value);
		}

		switch (window->data_level) {
		case DATA_LEVEL_NONE:
		case DATA_LEVEL_TEXT:
			name = "{sb_act_text %d";
			break;
		case DATA_LEVEL_MSG:
			name = "{sb_act_msg %d";
			break;
		default:
                        if (window->hilight_color == NULL)
				name = "{sb_act_hilight %d";
			else
                                name = NULL;
			break;
		}

		if (name != NULL)
			g_string_printf(format, name, window->refnum);
		else
			g_string_printf(format, "{sb_act_hilight_color %s %d",
						 window->hilight_color,
						 window->refnum);
		if (add_name && window->active != NULL)
			g_string_append_printf(format, ":%s", window->active->visible_name);
		g_string_append_c(format, '}');

		value = theme_format_expand(theme, format->str);
		g_string_append(str, value);
                g_free(value);
	}

	ret = str->len == 0 ? NULL : str->str;
        g_string_free(str, ret == NULL);
	g_string_free(format, TRUE);
        return ret;
}

/* redraw activity, FIXME: if we didn't get enough size, this gets buggy.
   At least "Det:" isn't printed properly. also we should rearrange the
   act list so that the highest priority items comes first. */
static void item_act(SBAR_ITEM_REC *item, int get_size_only)
{
	char *actlist;
	int max_size;

	if (get_size_only) {
		if (activity_list == NULL)
			item->min_size = item->max_size = 0;
		/* Skip activity calculation on regular trigger, only
		   set dirty */
		return;
	}

	actlist = get_activity_list(item->bar->parent_window, TRUE, TRUE);
	if (actlist == NULL) {
		return;
	}

	max_size = item->max_size;
	statusbar_item_default_handler(item, TRUE,
				       NULL, actlist, FALSE);
	statusbar_item_default_handler(item, FALSE,
				       NULL, actlist, FALSE);
	if (max_size != item->max_size) {
		/* Due to above hack of skipping the calculation, we
		   need to manually trigger the redraw process now or
		   we won't see the item */
		item->bar->dirty = item->dirty = TRUE;
		statusbar_redraw(item->bar, TRUE);
		statusbar_redraw_dirty();
	}

	g_free_not_null(actlist);
}

static int window_level_recent_cmp(WINDOW_REC *w1, WINDOW_REC *w2)
{
	if (w1->data_level >= w2->data_level)
		return -1;
	else
		return 1;
}

static int window_level_cmp(WINDOW_REC *w1, WINDOW_REC *w2)
{
	if (w1->data_level > w2->data_level ||
	    (w1->data_level == w2->data_level && w1->refnum < w2->refnum))
		return -1;
	else
		return 1;
}

static void sig_statusbar_activity_hilight(WINDOW_REC *window, gpointer oldlevel)
{
	GList *node;

	g_return_if_fail(window != NULL);

	node = g_list_find(activity_list, window);

	if (actlist_sort == 1) {
		/* Move the window to the first in the activity list */
		if (node != NULL)
			activity_list = g_list_delete_link(activity_list, node);
		if (window->data_level != 0)
			activity_list = g_list_prepend(activity_list, window);
		statusbar_items_redraw("act");
		return;
	}

	if (actlist_sort == 2) {
		if (node != NULL) {
			if (window->data_level == GPOINTER_TO_INT(oldlevel)) {
				if (window->hilight_color != 0)
					statusbar_items_redraw("act");
				return;
			}
			activity_list = g_list_delete_link(activity_list, node);
		}
		if (window->data_level != 0)
			activity_list = g_list_insert_sorted(activity_list, window, (GCompareFunc)
							     window_level_cmp);
		statusbar_items_redraw("act");
		return;
	}

	if (actlist_sort == 3) {
		if (node != NULL)
			activity_list = g_list_delete_link(activity_list, node);
		if (window->data_level != 0)
			activity_list = g_list_insert_sorted(activity_list, window, (GCompareFunc)
							     window_level_recent_cmp);
		statusbar_items_redraw("act");
		return;
	}

	if (node != NULL) {
		/* already in activity list */
		if (window->data_level == 0) {
			/* remove from activity list */
			activity_list = g_list_delete_link(activity_list, node);
			statusbar_items_redraw("act");
		} else if (window->data_level != GPOINTER_TO_INT(oldlevel) ||
			 window->hilight_color != 0) {
			/* different level as last time (or maybe different
			   hilight color?), just redraw it. */
			statusbar_items_redraw("act");
		}
		return;
	}

	if (window->data_level == 0)
		return;

	/* add window to activity list .. */
	activity_list = g_list_insert_sorted(activity_list, window, (GCompareFunc)
					     window_refnum_cmp);

	statusbar_items_redraw("act");
}

static void sig_statusbar_activity_window_destroyed(WINDOW_REC *window)
{
	GList *node;

	g_return_if_fail(window != NULL);

	node = g_list_find(activity_list, window);
	if (node != NULL)
		activity_list = g_list_delete_link(activity_list, node);
	statusbar_items_redraw("act");
}

static void sig_statusbar_activity_updated(void)
{
	statusbar_items_redraw("act");
}

static void item_more(SBAR_ITEM_REC *item, int get_size_only)
{
        MAIN_WINDOW_REC *mainwin;
	int visible;

	if (active_win == NULL) {
                mainwin = NULL;
		visible = FALSE;
	} else {
		mainwin = WINDOW_MAIN(active_win);
		visible = WINDOW_GUI(active_win)->view->more_text;
	}

	if (!visible) {
		if (mainwin != NULL)
			more_visible = g_slist_remove(more_visible, mainwin);
		if (get_size_only)
			item->min_size = item->max_size = 0;
		return;
	}

	more_visible = g_slist_prepend(more_visible, mainwin);
	statusbar_item_default_handler(item, get_size_only, NULL, "", FALSE);
}

static void sig_statusbar_more_updated(void)
{
	int visible;

	 /* no active window, for example during /window hide */
	if (active_win == NULL)
		return;

        visible = g_slist_find(more_visible, WINDOW_MAIN(active_win)) != NULL;
	if (WINDOW_GUI(active_win)->view->more_text != visible)
                statusbar_items_redraw("more");
}

/* Returns the lag in milliseconds. If we haven't been able to ask the lag
   for a while, unknown is set to TRUE. */
static int get_lag(SERVER_REC *server, int *unknown)
{
	long lag;

        *unknown = FALSE;

	if (server == NULL || server->lag_last_check == 0) {
                /* lag has not been asked even once yet */
		return 0;
	}

	if (server->lag_sent.tv_sec == 0) {
		/* no lag queries going on currently */
                return server->lag;
	}

        /* we're not sure about our current lag.. */
	*unknown = TRUE;

        lag = (long) (time(NULL)-server->lag_sent.tv_sec);
	if (server->lag/1000 > lag) {
		/* we've been waiting the lag reply less time than
		   what last known lag was -> use the last known lag */
		return server->lag;
	}

        /* return how long we have been waiting for lag reply */
        return lag*1000;
}

static void item_lag(SBAR_ITEM_REC *item, int get_size_only)
{
	SERVER_REC *server;
        char str[MAX_INT_STRLEN+10];
	int lag, lag_unknown;

	server = active_win == NULL ? NULL : active_win->active_server;
	lag = get_lag(server, &lag_unknown);

	if (lag <= 0 || lag < settings_get_time("lag_min_show")) {
		/* don't print the lag item */
		if (get_size_only)
			item->min_size = item->max_size = 0;
		return;
	}

	lag /= 10;
	last_lag = lag;
	last_lag_unknown = lag_unknown;

	if (lag_unknown) {
		// "??)" in C becomes ']'
		// See: https://en.wikipedia.org/wiki/Digraphs_and_trigraphs#C
		g_snprintf(str, sizeof(str), "%d (?""?)", lag / 100);
	} else {
		if (lag % 100 == 0)
			g_snprintf(str, sizeof(str), "%d", lag / 100);
		else
			g_snprintf(str, sizeof(str), "%d.%02d", lag / 100, lag % 100);
	}

	statusbar_item_default_handler(item, get_size_only,
				       NULL, str, TRUE);
}

static void lag_check_update(void)
{
	SERVER_REC *server;
	int lag, lag_unknown;

	server = active_win == NULL ? NULL : active_win->active_server;
	lag = get_lag(server, &lag_unknown)/10;

	if (lag < settings_get_time("lag_min_show"))
		lag = 0;
	else
		lag /= 10;

	if (lag != last_lag || (lag > 0 && lag_unknown != last_lag_unknown))
                statusbar_items_redraw("lag");
}

static void sig_server_lag_updated(SERVER_REC *server)
{
	if (active_win != NULL && active_win->active_server == server)
                lag_check_update();
}

static int sig_lag_timeout(void)
{
        lag_check_update();
        return 1;
}

static void item_input(SBAR_ITEM_REC *item, int get_size_only)
{
	GUI_ENTRY_REC *rec;

	rec = g_hash_table_lookup(input_entries, item->bar->config->name);
	if (rec == NULL) {
		rec = gui_entry_create(item->xpos, item->bar->real_ypos,
				       item->size, term_type == TERM_TYPE_UTF8);
		gui_entry_set_active(rec);
		g_hash_table_insert(input_entries,
				    g_strdup(item->bar->config->name), rec);
	}

	if (get_size_only) {
		item->min_size = 2+term_width/10;
                item->max_size = term_width;
                return;
	}

	gui_entry_move(rec, item->xpos, item->bar->real_ypos,
		       item->size);
	gui_entry_redraw(rec); /* FIXME: this is only necessary with ^L.. */
}

static void read_settings(void)
{
	const char *str;

	if (active_entry != NULL)
		gui_entry_set_utf8(active_entry, term_type == TERM_TYPE_UTF8);

	str = settings_get_str("actlist_sort");
	if (g_ascii_strcasecmp(str, "recent") == 0)
		actlist_sort = 1;
	else if (g_ascii_strcasecmp(str, "level") == 0)
		actlist_sort = 2;
	else if (g_ascii_strcasecmp(str, "level,recent") == 0)
		actlist_sort = 3;
	else {
		settings_set_str("actlist_sort", "refnum");
		actlist_sort = 0;
	}
}

void statusbar_items_init(void)
{
	settings_add_time("misc", "lag_min_show", "1sec");
	settings_add_str("lookandfeel", "actlist_sort", "refnum");
	settings_add_bool("lookandfeel", "actlist_names", FALSE);

	statusbar_item_register("window", NULL, item_window_active);
	statusbar_item_register("window_empty", NULL, item_window_empty);
	statusbar_item_register("prompt", NULL, item_window_active);
	statusbar_item_register("prompt_empty", NULL, item_window_empty);
	statusbar_item_register("topic", NULL, item_window_active);
	statusbar_item_register("topic_empty", NULL, item_window_empty);
	statusbar_item_register("lag", NULL, item_lag);
	statusbar_item_register("act", NULL, item_act);
	statusbar_item_register("more", NULL, item_more);
	statusbar_item_register("input", NULL, item_input);

        /* activity */
	activity_list = NULL;
	signal_add("window activity", (SIGNAL_FUNC) sig_statusbar_activity_hilight);
	signal_add("window destroyed", (SIGNAL_FUNC) sig_statusbar_activity_window_destroyed);
	signal_add("window refnum changed", (SIGNAL_FUNC) sig_statusbar_activity_updated);

        /* more */
        more_visible = NULL;
	signal_add("gui page scrolled", (SIGNAL_FUNC) sig_statusbar_more_updated);
	signal_add("window changed", (SIGNAL_FUNC) sig_statusbar_more_updated);
	signal_add_last("gui print text finished", (SIGNAL_FUNC) sig_statusbar_more_updated);
	signal_add_last("command clear", (SIGNAL_FUNC) sig_statusbar_more_updated);
	signal_add_last("command scrollback", (SIGNAL_FUNC) sig_statusbar_more_updated);

        /* lag */
	last_lag = 0; last_lag_unknown = FALSE;
	signal_add("server lag", (SIGNAL_FUNC) sig_server_lag_updated);
	signal_add("window changed", (SIGNAL_FUNC) lag_check_update);
	signal_add("window server changed", (SIGNAL_FUNC) lag_check_update);
        lag_timeout_tag = g_timeout_add(5000, (GSourceFunc) sig_lag_timeout, NULL);

        /* input */
	input_entries = g_hash_table_new((GHashFunc) g_str_hash,
					 (GCompareFunc) g_str_equal);

	read_settings();
        signal_add_last("setup changed", (SIGNAL_FUNC) read_settings);
}

void statusbar_items_deinit(void)
{
        /* activity */
	signal_remove("window activity", (SIGNAL_FUNC) sig_statusbar_activity_hilight);
	signal_remove("window destroyed", (SIGNAL_FUNC) sig_statusbar_activity_window_destroyed);
	signal_remove("window refnum changed", (SIGNAL_FUNC) sig_statusbar_activity_updated);
	g_list_free(activity_list);
        activity_list = NULL;

        /* more */
        g_slist_free(more_visible);
	signal_remove("gui page scrolled", (SIGNAL_FUNC) sig_statusbar_more_updated);
	signal_remove("window changed", (SIGNAL_FUNC) sig_statusbar_more_updated);
	signal_remove("gui print text finished", (SIGNAL_FUNC) sig_statusbar_more_updated);
	signal_remove("command clear", (SIGNAL_FUNC) sig_statusbar_more_updated);
	signal_remove("command scrollback", (SIGNAL_FUNC) sig_statusbar_more_updated);

        /* lag */
	signal_remove("server lag", (SIGNAL_FUNC) sig_server_lag_updated);
	signal_remove("window changed", (SIGNAL_FUNC) lag_check_update);
	signal_remove("window server changed", (SIGNAL_FUNC) lag_check_update);
        g_source_remove(lag_timeout_tag);

        /* input */
        g_hash_table_foreach(input_entries, (GHFunc) g_free, NULL);
	g_hash_table_destroy(input_entries);

        signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
}