Bug 1013: Don't assume errno is between 0 and 100000
[elinks.git] / src / dialogs / status.c
blob908ad0b2a0b5043173b780e94ee333ca044cb247
1 /* Sessions status management */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <string.h>
9 #include "elinks.h"
11 #include "bfu/dialog.h"
12 #include "cache/cache.h"
13 #include "config/options.h"
14 #include "dialogs/progress.h"
15 #include "dialogs/status.h"
16 #include "document/document.h"
17 #include "document/renderer.h"
18 #include "document/view.h"
19 #include "intl/gettext/libintl.h"
20 #include "network/connection.h"
21 #include "network/progress.h"
22 #include "network/state.h"
23 #include "protocol/bittorrent/dialogs.h"
24 #include "protocol/protocol.h"
25 #include "protocol/uri.h"
26 #include "session/download.h"
27 #include "session/session.h"
28 #include "terminal/draw.h"
29 #include "terminal/screen.h"
30 #include "terminal/tab.h"
31 #include "terminal/terminal.h"
32 #include "terminal/window.h"
33 #include "util/color.h"
34 #include "util/conv.h"
35 #include "util/error.h"
36 #include "util/memory.h"
37 #include "util/snprintf.h"
38 #include "util/string.h"
39 #include "viewer/text/form.h"
40 #include "viewer/text/link.h"
41 #include "viewer/text/view.h"
44 unsigned char *
45 get_download_msg(struct download *download, struct terminal *term,
46 int wide, int full, unsigned char *separator)
48 if (!download_is_progressing(download)) {
49 /* DBG("%d -> %s", download->state, _(get_err_msg(download->state), term)); */
50 return stracpy(get_state_message(download->state, term));
53 #ifdef CONFIG_BITTORRENT
54 if (download->conn
55 && download->conn->uri->protocol == PROTOCOL_BITTORRENT)
56 return get_bittorrent_message(download, term, wide, full, separator);
57 #endif
59 return get_progress_msg(download->progress, term, wide, full, separator);
62 #define show_tabs(option, tabs) (((option) > 0) && !((option) == 1 && (tabs) < 2))
64 void
65 update_status(void)
67 int show_title_bar = get_opt_bool("ui.show_title_bar");
68 int show_status_bar = get_opt_bool("ui.show_status_bar");
69 int show_tabs_bar = get_opt_int("ui.tabs.show_bar");
70 int show_tabs_bar_at_top = get_opt_bool("ui.tabs.top");
71 #ifdef CONFIG_LEDS
72 int show_leds = get_opt_bool("ui.leds.enable");
73 #endif
74 int set_window_title = get_opt_bool("ui.window_title");
75 int insert_mode = get_opt_bool("document.browse.forms.insert_mode");
76 struct session *ses;
77 int tabs_count = 1;
78 struct terminal *term = NULL;
80 foreach (ses, sessions) {
81 struct session_status *status = &ses->status;
82 int dirty = 0;
84 /* Try to descrease the number of tab calculation using that
85 * tab sessions share the same term. */
86 if (ses->tab->term != term) {
87 term = ses->tab->term;
88 tabs_count = number_of_tabs(term);
91 if (status->force_show_title_bar >= 0)
92 show_title_bar = status->force_show_title_bar;
93 if (status->show_title_bar != show_title_bar) {
94 status->show_title_bar = show_title_bar;
95 dirty = 1;
98 if (status->force_show_status_bar >= 0)
99 show_status_bar = status->force_show_status_bar;
100 if (status->show_status_bar != show_status_bar) {
101 status->show_status_bar = show_status_bar;
102 dirty = 1;
105 if (show_tabs(show_tabs_bar, tabs_count) != status->show_tabs_bar) {
106 status->show_tabs_bar = show_tabs(show_tabs_bar, tabs_count);
107 dirty = 1;
110 if (status->show_tabs_bar) {
111 if (status->show_tabs_bar_at_top != show_tabs_bar_at_top) {
112 status->show_tabs_bar_at_top = show_tabs_bar_at_top;
113 dirty = 1;
116 #ifdef CONFIG_LEDS
117 if (status->show_leds != show_leds) {
118 status->show_leds = show_leds;
119 dirty = 1;
121 #endif
123 status->set_window_title = set_window_title;
125 /* This more belongs to the current browsing state but ... */
126 if (!insert_mode)
127 ses->insert_mode = INSERT_MODE_LESS;
128 else if (ses->insert_mode == INSERT_MODE_LESS)
129 ses->insert_mode = INSERT_MODE_OFF;
131 if (!dirty) continue;
133 /* Force the current document to be rerendered so the
134 * document view and document height is updated to fit
135 * into the new dimensions. Related to bug 87. */
136 render_document_frames(ses, 1);
138 set_screen_dirty(term->screen, 0, term->height);
142 static unsigned char *
143 get_current_link_info_and_title(struct session *ses,
144 struct document_view *doc_view)
146 unsigned char *link_info, *link_title, *ret = NULL;
148 link_info = get_current_link_info(ses, doc_view);
149 if (!link_info) return NULL;
151 link_title = get_current_link_title(doc_view);
152 if (link_title) {
153 assert(*link_title);
155 ret = straconcat(link_info, " - ", link_title,
156 (unsigned char *) NULL);
157 mem_free(link_info);
158 mem_free(link_title);
161 if (!ret) ret = link_info;
163 return ret;
166 static inline void
167 display_status_bar(struct session *ses, struct terminal *term, int tabs_count)
169 unsigned char *msg = NULL;
170 unsigned int tab_info_len = 0;
171 struct download *download = get_current_download(ses);
172 struct session_status *status = &ses->status;
173 struct color_pair *text_color = NULL;
174 int msglen;
175 struct box box;
177 #ifdef CONFIG_MARKS
178 if (ses->kbdprefix.mark != KP_MARK_NOTHING) {
179 switch (ses->kbdprefix.mark) {
180 case KP_MARK_NOTHING:
181 assert(0);
182 break;
184 case KP_MARK_SET:
185 msg = msg_text(term, N_("Enter a mark to set"));
186 break;
188 case KP_MARK_GOTO:
189 msg = msg_text(term, N_("Enter a mark"
190 " to which to jump"));
191 break;
193 } else
194 #endif
195 if (ses->kbdprefix.repeat_count) {
196 msg = msg_text(term, N_("Keyboard prefix: %d"),
197 ses->kbdprefix.repeat_count);
199 #ifdef CONFIG_ECMASCRIPT
200 else if (ses->status.window_status) {
201 msg = stracpy(ses->status.window_status);
203 #endif
204 else if (download) {
205 struct document_view *doc_view = current_frame(ses);
207 /* Show S_INTERRUPTED message *once* but then show links
208 * again as usual. */
209 /* doc_view->vs may be NULL here in the short interval between
210 * ses_forward() with @loading_in_frame set, disconnecting the
211 * doc_view from vs, and render_document_frames(), detaching
212 * the doc_view. */
213 if (doc_view && doc_view->vs) {
214 static int last_current_link;
215 int ncl = doc_view->vs->current_link;
217 if (is_in_state(download->state, S_INTERRUPTED)
218 && ncl != last_current_link)
219 download->state = connection_state(S_OK);
220 last_current_link = ncl;
222 if (is_in_state(download->state, S_OK)) {
223 if (get_current_link(doc_view)) {
224 msg = get_current_link_info_and_title(ses, doc_view);
225 } else if (ses->navigate_mode == NAVIGATE_CURSOR_ROUTING) {
226 msg = msg_text(term, N_("Cursor position: %dx%d"),
227 ses->tab->x + 1, ses->tab->y + 1);
232 if (!msg) {
233 int full = term->width > 130;
234 int wide = term->width > 80;
236 msg = get_download_msg(download, term, wide, full, ", ");
240 set_box(&box, 0, term->height - 1, term->width, 1);
241 draw_box(term, &box, ' ', 0, get_bfu_color(term, "status.status-bar"));
243 if (!status->show_tabs_bar && tabs_count > 1) {
244 unsigned char tab_info[8];
246 tab_info[tab_info_len++] = '[';
247 ulongcat(tab_info, &tab_info_len, term->current_tab + 1, 4, 0);
248 tab_info[tab_info_len++] = ']';
249 tab_info[tab_info_len++] = ' ';
250 tab_info[tab_info_len] = '\0';
252 text_color = get_bfu_color(term, "status.status-text");
253 draw_text(term, 0, term->height - 1, tab_info, tab_info_len,
254 0, text_color);
257 if (!msg) return;
259 if (!text_color)
260 text_color = get_bfu_color(term, "status.status-text");
262 msglen = strlen(msg);
263 draw_text(term, 0 + tab_info_len, term->height - 1,
264 msg, msglen, 0, text_color);
265 mem_free(msg);
267 if (download_is_progressing(download) && download->progress->size > 0) {
268 int xend = term->width - 1;
269 int width;
271 #ifdef CONFIG_LEDS
272 if (ses->status.show_leds)
273 xend -= term->leds_length;
274 #endif
276 width = int_max(0, xend - msglen - tab_info_len - 1);
277 if (width < 6) return;
278 int_upper_bound(&width, 20);
279 draw_progress_bar(download->progress, term,
280 xend - width, term->height - 1, width,
281 NULL, NULL);
285 static inline void
286 display_tab_bar(struct session *ses, struct terminal *term, int tabs_count)
288 struct color_pair *normal_color = get_bfu_color(term, "tabs.normal");
289 struct color_pair *selected_color = get_bfu_color(term, "tabs.selected");
290 struct color_pair *loading_color = get_bfu_color(term, "tabs.loading");
291 struct color_pair *fresh_color = get_bfu_color(term, "tabs.unvisited");
292 struct color_pair *tabsep_color = get_bfu_color(term, "tabs.separator");
293 struct session_status *status = &ses->status;
294 int tab_width = int_max(1, term->width / tabs_count);
295 int tab_total_width = tab_width * tabs_count;
296 int tab_remain_width = int_max(0, term->width - tab_total_width);
297 int tab_add = int_max(1, (tab_remain_width / tabs_count));
298 int tab_num;
299 struct box box;
301 if (status->show_tabs_bar_at_top) set_box(&box, 0, status->show_title_bar, term->width, 1);
302 else set_box(&box, 0, term->height - (status->show_status_bar ? 2 : 1), 0, 1);
304 for (tab_num = 0; tab_num < tabs_count; tab_num++) {
305 struct download *download = NULL;
306 struct color_pair *color = normal_color;
307 struct window *tab = get_tab_by_number(term, tab_num);
308 struct document_view *doc_view;
309 struct session *tab_ses = tab->data;
310 int actual_tab_width = tab_width - 1;
311 unsigned char *msg;
313 /* Adjust tab size to use full term width. */
314 if (tab_remain_width) {
315 actual_tab_width += tab_add;
316 tab_remain_width -= tab_add;
319 doc_view = tab_ses ? current_frame(tab_ses) : NULL;
321 if (doc_view) {
322 if (doc_view->document->title
323 && *(doc_view->document->title))
324 msg = doc_view->document->title;
325 else
326 msg = _("Untitled", term);
327 } else {
328 msg = _("No document", term);
331 if (tab_num) {
332 draw_char(term, box.x, box.y, BORDER_SVLINE,
333 SCREEN_ATTR_FRAME, tabsep_color);
334 box.x++;
337 if (tab_num == term->current_tab) {
338 color = selected_color;
340 } else {
341 download = get_current_download(tab_ses);
343 if (download && !is_in_state(download->state, S_OK)) {
344 color = loading_color;
345 } else if (!tab_ses || !tab_ses->status.visited) {
346 color = fresh_color;
349 if (!download_is_progressing(download)
350 || download->progress->size <= 0)
351 download = NULL;
354 box.width = actual_tab_width + 1;
355 draw_box(term, &box, ' ', 0, color);
357 if (download) {
358 draw_progress_bar(download->progress, term,
359 box.x, box.y, actual_tab_width,
360 msg, NULL);
361 } else {
362 int msglen = int_min(strlen(msg), actual_tab_width);
364 draw_text(term, box.x, box.y, msg, msglen, 0, color);
367 tab->xpos = box.x;
368 tab->width = actual_tab_width;
369 if (tab_num == tabs_count - 1) {
370 /* This is the last tab, and is therefore followed
371 * by a space, not a separator; increment tab->width
372 * to count that space as part of the tab.
373 * -- Miciah */
374 tab->width++;
377 box.x += actual_tab_width;
381 /* Print page's title and numbering at window top. */
382 static inline void
383 display_title_bar(struct session *ses, struct terminal *term)
385 struct document_view *doc_view;
386 struct document *document;
387 struct string title;
388 unsigned char buf[40];
389 int buflen = 0;
390 int height;
392 /* Clear the old title */
393 if (!get_opt_bool("ui.show_menu_bar_always")) {
394 struct box box;
396 set_box(&box, 0, 0, term->width, 1);
397 draw_box(term, &box, ' ', 0, get_bfu_color(term, "title.title-bar"));
400 doc_view = current_frame(ses);
401 if (!doc_view || !doc_view->document) return;
403 if (!init_string(&title)) return;
405 document = doc_view->document;
407 /* Set up the document page info string: '(' %page '/' %pages ')' */
408 height = doc_view->box.height;
409 if (height < document->height && doc_view->vs) {
410 int pos = doc_view->vs->y + height;
411 int page = 1;
412 int pages = height ? (document->height + height - 1) / height : 1;
414 /* Check if at the end else calculate the page. */
415 if (pos >= document->height) {
416 page = pages;
417 } else if (height) {
418 page = int_min((pos - height / 2) / height + 1, pages);
421 buflen = snprintf(buf, sizeof(buf), " (%d/%d)", page, pages);
422 if (buflen < 0) buflen = 0;
425 if (document->title) {
426 int maxlen = int_max(term->width - 4 - buflen, 0);
427 int titlelen, titlewidth;
429 #ifdef CONFIG_UTF8
430 if (term->utf8_cp) {
431 titlewidth = utf8_ptr2cells(document->title, NULL);
432 titlewidth = int_min(titlewidth, maxlen);
434 titlelen = utf8_cells2bytes(document->title,
435 titlewidth, NULL);
436 } else
437 #endif /* CONFIG_UTF8 */
439 titlewidth = int_min(strlen(document->title), maxlen);
440 titlelen = titlewidth;
443 add_bytes_to_string(&title, document->title, titlelen);
445 if (titlewidth == maxlen)
446 add_bytes_to_string(&title, "...", 3);
449 if (buflen > 0)
450 add_bytes_to_string(&title, buf, buflen);
452 if (title.length) {
453 int x;
454 #ifdef CONFIG_UTF8
455 if (term->utf8_cp) {
456 x = int_max(term->width - 1
457 - utf8_ptr2cells(title.source,
458 title.source
459 + title.length), 0);
460 } else
461 #endif /* CONFIG_UTF8 */
462 x = int_max(term->width - 1 - title.length, 0);
464 draw_text(term, x, 0, title.source, title.length, 0,
465 get_bfu_color(term, "title.title-text"));
468 done_string(&title);
471 static inline void
472 display_window_title(struct session *ses, struct terminal *term)
474 static struct session *last_ses;
475 struct session_status *status = &ses->status;
476 unsigned char *doc_title = NULL;
477 unsigned char *title;
478 int titlelen;
480 if (ses->doc_view
481 && ses->doc_view->document
482 && ses->doc_view->document->title
483 && ses->doc_view->document->title[0])
484 doc_title = ses->doc_view->document->title;
486 title = doc_title ? straconcat(doc_title, " - ELinks",
487 (unsigned char *) NULL)
488 : stracpy("ELinks");
489 if (!title) return;
491 titlelen = strlen(title);
492 if (last_ses != ses
493 || !status->last_title
494 || strlen(status->last_title) != titlelen
495 || memcmp(status->last_title, title, titlelen)) {
496 mem_free_set(&status->last_title, title);
497 set_terminal_title(term, title);
498 last_ses = ses;
499 } else {
500 mem_free(title);
504 #ifdef CONFIG_LEDS
505 static inline void
506 display_leds(struct session *ses, struct session_status *status)
508 if (ses->doc_view && ses->doc_view->document) {
509 struct cache_entry *cached = ses->doc_view->document->cached;
511 if (cached) {
512 if (cached->ssl_info)
513 set_led_value(status->ssl_led, 'S');
514 else
515 unset_led_value(status->ssl_led);
516 } else {
517 /* FIXME: We should do this thing better. */
518 set_led_value(status->ssl_led, '?');
522 if (ses->insert_mode == INSERT_MODE_LESS) {
523 set_led_value(status->insert_mode_led, 'i');
524 } else {
525 if (ses->insert_mode == INSERT_MODE_ON)
526 set_led_value(status->insert_mode_led, 'I');
527 else
528 unset_led_value(status->insert_mode_led);
531 draw_leds(ses);
533 #endif /* CONFIG_LEDS */
535 /* Print statusbar and titlebar, set terminal title. */
536 void
537 print_screen_status(struct session *ses)
539 struct terminal *term = ses->tab->term;
540 struct session_status *status = &ses->status;
541 int tabs_count = number_of_tabs(term);
542 int ses_tab_is_current = (ses->tab == get_current_tab(term));
544 if (ses_tab_is_current) {
545 if (status->set_window_title)
546 display_window_title(ses, term);
548 if (status->show_title_bar)
549 display_title_bar(ses, term);
551 if (status->show_status_bar)
552 display_status_bar(ses, term, tabs_count);
553 #ifdef CONFIG_LEDS
554 if (status->show_leds)
555 display_leds(ses, status);
556 #endif
558 if (!ses->status.visited)
559 ses->status.visited = 1;
562 if (status->show_tabs_bar) {
563 display_tab_bar(ses, term, tabs_count);
566 redraw_from_window(ses->tab);