2 statusbar-items.c : irssi
4 Copyright (C) 1999-2001 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "statusbar.h"
28 #include "gui-entry.h"
29 #include "gui-windows.h"
31 /* how often to redraw lagging time (seconds) */
32 #define LAG_REFRESH_TIME 10
34 static GList *activity_list;
35 static GSList *more_visible; /* list of MAIN_WINDOW_RECs which have --more-- */
36 static GHashTable *input_entries;
37 static int last_lag, last_lag_unknown, lag_timeout_tag;
39 static void item_window_active(SBAR_ITEM_REC *item, int get_size_only)
44 if (item->bar->parent_window != NULL)
45 window = item->bar->parent_window->active;
47 if (window != NULL && window->active != NULL) {
48 statusbar_item_default_handler(item, get_size_only,
50 } else if (get_size_only) {
51 item->min_size = item->max_size = 0;
55 static void item_window_empty(SBAR_ITEM_REC *item, int get_size_only)
60 if (item->bar->parent_window != NULL)
61 window = item->bar->parent_window->active;
63 if (window != NULL && window->active == NULL) {
64 statusbar_item_default_handler(item, get_size_only,
66 } else if (get_size_only) {
67 item->min_size = item->max_size = 0;
71 static char *get_activity_list(MAIN_WINDOW_REC *window, int normal, int hilight)
76 char *ret, *name, *format, *value;
79 str = g_string_new(NULL);
81 theme = window != NULL && window->active != NULL &&
82 window->active->theme != NULL ?
83 window->active->theme : current_theme;
85 for (tmp = activity_list; tmp != NULL; tmp = tmp->next) {
86 WINDOW_REC *window = tmp->data;
88 is_det = window->data_level >= DATA_LEVEL_HILIGHT;
89 if ((!is_det && !normal) || (is_det && !hilight))
94 value = theme_format_expand(theme, "{sb_act_sep ,}");
95 g_string_append(str, value);
99 switch (window->data_level) {
100 case DATA_LEVEL_NONE:
101 case DATA_LEVEL_TEXT:
102 name = "{sb_act_text %d}";
105 name = "{sb_act_msg %d}";
108 if (window->hilight_color == NULL)
109 name = "{sb_act_hilight %d}";
116 format = g_strdup_printf(name, window->refnum);
118 format = g_strdup_printf("{sb_act_hilight_color %s %d}",
119 window->hilight_color,
122 value = theme_format_expand(theme, format);
123 g_string_append(str, value);
129 ret = str->len == 0 ? NULL : str->str;
130 g_string_free(str, ret == NULL);
134 /* redraw activity, FIXME: if we didn't get enough size, this gets buggy.
135 At least "Det:" isn't printed properly. also we should rearrange the
136 act list so that the highest priority items comes first. */
137 static void item_act(SBAR_ITEM_REC *item, int get_size_only)
141 actlist = get_activity_list(item->bar->parent_window, TRUE, TRUE);
142 if (actlist == NULL) {
144 item->min_size = item->max_size = 0;
148 statusbar_item_default_handler(item, get_size_only,
149 NULL, actlist, FALSE);
151 g_free_not_null(actlist);
154 static void sig_statusbar_activity_hilight(WINDOW_REC *window, gpointer oldlevel)
159 g_return_if_fail(window != NULL);
161 if (settings_get_bool("actlist_moves")) {
162 /* Move the window to the first in the activity list */
163 if (g_list_find(activity_list, window) != NULL)
164 activity_list = g_list_remove(activity_list, window);
165 if (window->data_level != 0)
166 activity_list = g_list_prepend(activity_list, window);
167 statusbar_items_redraw("act");
171 if (g_list_find(activity_list, window) != NULL) {
172 /* already in activity list */
173 if (window->data_level == 0) {
174 /* remove from activity list */
175 activity_list = g_list_remove(activity_list, window);
176 statusbar_items_redraw("act");
177 } else if (window->data_level != GPOINTER_TO_INT(oldlevel) ||
178 window->hilight_color != 0) {
179 /* different level as last time (or maybe different
180 hilight color?), just redraw it. */
181 statusbar_items_redraw("act");
186 if (window->data_level == 0)
189 /* add window to activity list .. */
191 for (tmp = activity_list; tmp != NULL; tmp = tmp->next, inspos++) {
192 WINDOW_REC *rec = tmp->data;
194 if (window->refnum < rec->refnum) {
196 g_list_insert(activity_list, window, inspos);
201 activity_list = g_list_append(activity_list, window);
203 statusbar_items_redraw("act");
206 static void sig_statusbar_activity_window_destroyed(WINDOW_REC *window)
208 g_return_if_fail(window != NULL);
210 if (g_list_find(activity_list, window) != NULL)
211 activity_list = g_list_remove(activity_list, window);
212 statusbar_items_redraw("act");
215 static void sig_statusbar_activity_updated(void)
217 statusbar_items_redraw("act");
220 static void item_more(SBAR_ITEM_REC *item, int get_size_only)
222 MAIN_WINDOW_REC *mainwin;
225 if (active_win == NULL) {
229 mainwin = WINDOW_MAIN(active_win);
230 visible = WINDOW_GUI(active_win)->view->more_text;
235 more_visible = g_slist_remove(more_visible, mainwin);
237 item->min_size = item->max_size = 0;
241 more_visible = g_slist_prepend(more_visible, mainwin);
242 statusbar_item_default_handler(item, get_size_only, NULL, "", FALSE);
245 static void sig_statusbar_more_updated(void)
249 visible = g_slist_find(more_visible, WINDOW_MAIN(active_win)) != NULL;
250 if (WINDOW_GUI(active_win)->view->more_text != visible)
251 statusbar_items_redraw("more");
254 /* Returns the lag in milliseconds. If we haven't been able to ask the lag
255 for a while, unknown is set to TRUE. */
256 static int get_lag(SERVER_REC *server, int *unknown)
262 if (server == NULL || server->lag_last_check == 0) {
263 /* lag has not been asked even once yet */
267 if (server->lag_sent.tv_sec == 0) {
268 /* no lag queries going on currently */
272 /* we're not sure about our current lag.. */
275 lag = (long) (time(NULL)-server->lag_sent.tv_sec);
276 if (server->lag/1000 > lag) {
277 /* we've been waiting the lag reply less time than
278 what last known lag was -> use the last known lag */
282 /* return how long we have been waiting for lag reply */
286 static void item_lag(SBAR_ITEM_REC *item, int get_size_only)
289 char str[MAX_INT_STRLEN+10];
290 int lag, lag_unknown;
292 server = active_win == NULL ? NULL : active_win->active_server;
293 lag = get_lag(server, &lag_unknown)/10;
295 if (lag <= 0 || lag < settings_get_int("lag_min_show")) {
296 /* don't print the lag item */
298 item->min_size = item->max_size = 0;
303 last_lag_unknown = lag_unknown;
306 g_snprintf(str, sizeof(str), "%d (?""?)", lag/100);
308 g_snprintf(str, sizeof(str),
309 lag%100 == 0 ? "%d" : "%d.%02d", lag/100, lag%100);
312 statusbar_item_default_handler(item, get_size_only,
316 static void lag_check_update(void)
319 int lag, lag_unknown;
321 server = active_win == NULL ? NULL : active_win->active_server;
322 lag = get_lag(server, &lag_unknown)/10;
324 if (lag < settings_get_int("lag_min_show"))
327 if (lag != last_lag || (lag > 0 && lag_unknown != last_lag_unknown))
328 statusbar_items_redraw("lag");
331 static void sig_server_lag_updated(SERVER_REC *server)
333 if (active_win != NULL && active_win->active_server == server)
337 static int sig_lag_timeout(void)
343 static void item_input(SBAR_ITEM_REC *item, int get_size_only)
347 rec = g_hash_table_lookup(input_entries, item->bar->config->name);
349 rec = gui_entry_create(item->xpos, item->bar->real_ypos,
350 item->size, term_type == TERM_TYPE_UTF8);
351 gui_entry_set_active(rec);
352 g_hash_table_insert(input_entries,
353 g_strdup(item->bar->config->name), rec);
357 item->min_size = 2+term_width/10;
358 item->max_size = term_width;
362 gui_entry_move(rec, item->xpos, item->bar->real_ypos,
364 gui_entry_redraw(rec); /* FIXME: this is only necessary with ^L.. */
367 static void read_settings(void)
369 if (active_entry != NULL)
370 gui_entry_set_utf8(active_entry, term_type == TERM_TYPE_UTF8);
373 void statusbar_items_init(void)
375 settings_add_int("misc", "lag_min_show", 100);
376 settings_add_bool("lookandfeel", "actlist_moves", FALSE);
378 statusbar_item_register("window", NULL, item_window_active);
379 statusbar_item_register("window_empty", NULL, item_window_empty);
380 statusbar_item_register("prompt", NULL, item_window_active);
381 statusbar_item_register("prompt_empty", NULL, item_window_empty);
382 statusbar_item_register("topic", NULL, item_window_active);
383 statusbar_item_register("topic_empty", NULL, item_window_empty);
384 statusbar_item_register("lag", NULL, item_lag);
385 statusbar_item_register("act", NULL, item_act);
386 statusbar_item_register("more", NULL, item_more);
387 statusbar_item_register("input", NULL, item_input);
390 activity_list = NULL;
391 signal_add("window activity", (SIGNAL_FUNC) sig_statusbar_activity_hilight);
392 signal_add("window destroyed", (SIGNAL_FUNC) sig_statusbar_activity_window_destroyed);
393 signal_add("window refnum changed", (SIGNAL_FUNC) sig_statusbar_activity_updated);
397 signal_add("gui page scrolled", (SIGNAL_FUNC) sig_statusbar_more_updated);
398 signal_add("window changed", (SIGNAL_FUNC) sig_statusbar_more_updated);
399 signal_add_last("gui print text finished", (SIGNAL_FUNC) sig_statusbar_more_updated);
400 signal_add_last("command clear", (SIGNAL_FUNC) sig_statusbar_more_updated);
401 signal_add_last("command scrollback", (SIGNAL_FUNC) sig_statusbar_more_updated);
404 last_lag = 0; last_lag_unknown = FALSE;
405 signal_add("server lag", (SIGNAL_FUNC) sig_server_lag_updated);
406 signal_add("window changed", (SIGNAL_FUNC) lag_check_update);
407 signal_add("window server changed", (SIGNAL_FUNC) lag_check_update);
408 lag_timeout_tag = g_timeout_add(5000, (GSourceFunc) sig_lag_timeout, NULL);
411 input_entries = g_hash_table_new((GHashFunc) g_str_hash,
412 (GCompareFunc) g_str_equal);
415 signal_add_last("setup changed", (SIGNAL_FUNC) read_settings);
418 void statusbar_items_deinit(void)
421 signal_remove("window activity", (SIGNAL_FUNC) sig_statusbar_activity_hilight);
422 signal_remove("window destroyed", (SIGNAL_FUNC) sig_statusbar_activity_window_destroyed);
423 signal_remove("window refnum changed", (SIGNAL_FUNC) sig_statusbar_activity_updated);
424 g_list_free(activity_list);
425 activity_list = NULL;
428 g_slist_free(more_visible);
429 signal_remove("gui page scrolled", (SIGNAL_FUNC) sig_statusbar_more_updated);
430 signal_remove("window changed", (SIGNAL_FUNC) sig_statusbar_more_updated);
431 signal_remove("gui print text finished", (SIGNAL_FUNC) sig_statusbar_more_updated);
432 signal_remove("command clear", (SIGNAL_FUNC) sig_statusbar_more_updated);
433 signal_remove("command scrollback", (SIGNAL_FUNC) sig_statusbar_more_updated);
436 signal_remove("server lag", (SIGNAL_FUNC) sig_server_lag_updated);
437 signal_remove("window changed", (SIGNAL_FUNC) lag_check_update);
438 signal_remove("window server changed", (SIGNAL_FUNC) lag_check_update);
439 g_source_remove(lag_timeout_tag);
442 g_hash_table_foreach(input_entries, (GHFunc) g_free, NULL);
443 g_hash_table_destroy(input_entries);
445 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);