1 /** Event system support routines.
17 #include "intl/gettext/libintl.h"
18 #include "main/main.h" /* terminate */
19 #include "main/object.h"
20 #include "session/session.h"
21 #include "terminal/draw.h"
22 #include "terminal/event.h"
23 #include "terminal/kbd.h"
24 #include "terminal/mouse.h"
25 #include "terminal/tab.h"
26 #include "terminal/terminal.h"
27 #include "terminal/screen.h"
28 #include "terminal/window.h"
29 #include "util/conv.h"
30 #include "util/error.h"
31 #include "util/memory.h"
32 #include "util/snprintf.h"
33 #include "util/string.h"
34 #include "viewer/timer.h"
37 /** Information used for communication between ELinks instances */
38 struct terminal_interlink
{
39 /** How big the input queue is */
41 /** How much is free */
44 /** UTF-8 input key value decoding data. */
49 /** Modifier keys from the key event that carried the
50 * first byte of the character. We need this because
51 * ELinks sees e.g. ESC U+00F6 as 0x1B 0xC3 0xB6 and
52 * converts it to Alt-0xC3 0xB6, attaching the
53 * modifier to the first byte only. */
54 term_event_modifier_T modifier
;
57 /** This is the queue of events as coming from the other
58 * ELinks instance owning the hosting terminal. */
59 unsigned char input_queue
[1];
64 term_send_event(struct terminal
*term
, struct term_event
*ev
)
69 if_assert_failed
return;
75 int width
= ev
->info
.size
.width
;
76 int height
= ev
->info
.size
.height
;
78 if (width
< 0 || height
< 0) {
79 ERROR(gettext("Bad terminal size: %d, %d"),
84 resize_screen(term
, width
, height
);
89 /* Nasty hack to avoid assertion failures when doing -remote
90 * stuff and the client exits right away */
91 if (!term
->screen
->image
) break;
94 term
->redrawing
= TREDRAW_DELAYED
;
95 /* Note that you do NOT want to ever go and create new
96 * window inside EVENT_INIT handler (it'll get second
97 * EVENT_INIT here). Perhaps the best thing you could do
98 * is registering a bottom-half handler which will open
101 if (ev
->ev
== EVENT_RESIZE
) {
102 /* We want to propagate EVENT_RESIZE even to inactive
103 * tabs! Nothing wrong will get drawn (in the final
104 * result) as the active tab is always the first one,
105 * thus will be drawn last here. Thanks, Witek!
107 foreachback (win
, term
->windows
)
108 win
->handler(win
, ev
);
111 foreachback (win
, term
->windows
)
112 if (!inactive_tab(win
))
113 win
->handler(win
, ev
);
115 term
->redrawing
= TREDRAW_READY
;
121 assert(!list_empty(term
->windows
));
122 if_assert_failed
break;
124 /* We need to send event to correct tab, not to the first one. --karpov */
125 /* ...if we want to send it to a tab at all. --pasky */
126 win
= term
->windows
.next
;
127 if (win
->type
== WINDOW_TAB
) {
128 win
= get_current_tab(term
);
129 assertm(win
!= NULL
, "No tab to send the event to!");
130 if_assert_failed
return;
133 win
->handler(win
, ev
);
138 term_send_ucs(struct terminal
*term
, unicode_val_T u
,
139 term_event_modifier_T modifier
)
142 struct term_event ev
;
144 set_kbd_term_event(&ev
, u
, modifier
);
145 term_send_event(term
, &ev
);
146 #else /* !CONFIG_UTF8 */
147 struct term_event ev
;
148 const unsigned char *recoded
;
150 set_kbd_term_event(&ev
, KBD_UNDEF
, modifier
);
151 recoded
= u2cp_no_nbsp(u
, get_opt_codepage_tree(term
->spec
, "charset"));
152 if (!recoded
) recoded
= "*";
154 ev
.info
.keyboard
.key
= *recoded
;
155 term_send_event(term
, &ev
);
158 #endif /* !CONFIG_UTF8 */
162 check_terminal_name(struct terminal
*term
, struct terminal_info
*info
)
164 unsigned char name
[MAX_TERM_LEN
+ 10];
167 /* We check TERM env. var for sanity, and fallback to _template_ if
168 * needed. This way we prevent elinks.conf potential corruption. */
169 for (i
= 0; info
->name
[i
]; i
++) {
170 if (isident(info
->name
[i
])) continue;
172 usrerror(_("Warning: terminal name contains illicit chars.", term
));
176 snprintf(name
, sizeof(name
), "terminal.%s", info
->name
);
178 /* Unlock the default _template_ option tree that was asigned by
179 * init_term() and get the correct one. */
180 object_unlock(term
->spec
);
181 term
->spec
= get_opt_rec(config_options
, name
);
182 object_lock(term
->spec
);
184 /* Probably not best place for set this. But now we finally have
185 * term->spec and term->utf8 should be set before decode session info.
187 term
->utf8_cp
= is_cp_utf8(get_opt_codepage_tree(term
->spec
,
189 /* Force UTF-8 I/O if the UTF-8 charset is selected. Various
190 * places assume that the terminal's charset is unibyte if
191 * UTF-8 I/O is disabled. (bug 827) */
192 term
->utf8_io
= term
->utf8_cp
193 || get_opt_bool_tree(term
->spec
, "utf_8_io");
194 #endif /* CONFIG_UTF8 */
199 ignore_mouse_event(struct terminal
*term
, struct term_event
*ev
)
201 struct term_event_mouse
*prev
= &term
->prev_mouse_event
;
202 struct term_event_mouse
*current
= &ev
->info
.mouse
;
204 if (check_mouse_action(ev
, B_UP
)
205 && current
->y
== prev
->y
206 && (current
->button
& ~BM_ACT
) == (prev
->button
& ~BM_ACT
)) {
207 do_not_ignore_next_mouse_event(term
);
212 copy_struct(prev
, current
);
219 handle_interlink_event(struct terminal
*term
, struct interlink_event
*ilev
)
221 struct terminal_info
*info
= NULL
;
222 struct terminal_interlink
*interlink
= term
->interlink
;
223 struct term_event tev
;
227 if (interlink
->qlen
< TERMINAL_INFO_SIZE
)
230 info
= (struct terminal_info
*) ilev
;
232 if (interlink
->qlen
< TERMINAL_INFO_SIZE
+ info
->length
)
235 info
->name
[MAX_TERM_LEN
- 1] = 0;
236 check_terminal_name(term
, info
);
238 memcpy(term
->cwd
, info
->cwd
, MAX_CWD_LEN
);
239 term
->cwd
[MAX_CWD_LEN
- 1] = 0;
241 term
->environment
= info
->system_env
;
243 /* We need to make sure that it is possible to draw on the
244 * terminal screen before decoding the session info so that
245 * handling of bad URL syntax by openning msg_box() will be
247 set_init_term_event(&tev
,
248 ilev
->info
.size
.width
,
249 ilev
->info
.size
.height
);
250 term_send_event(term
, &tev
);
252 /* Either the initialization of the first session failed or we
253 * are doing a remote session so quit.*/
254 if (!decode_session_info(term
, info
)) {
255 destroy_terminal(term
);
256 /* Make sure the user is notified if the initialization
257 * of the first session fails. */
258 if (program
.terminate
) {
259 usrerror(_("Failed to create session.", term
));
260 program
.retval
= RET_FATAL
;
265 ilev
->ev
= EVENT_REDRAW
;
269 set_wh_term_event(&tev
, ilev
->ev
,
270 ilev
->info
.size
.width
,
271 ilev
->info
.size
.height
);
272 term_send_event(term
, &tev
);
279 copy_struct(&tev
.info
.mouse
, &ilev
->info
.mouse
);
280 if (!ignore_mouse_event(term
, &tev
))
281 term_send_event(term
, &tev
);
288 int key
= ilev
->info
.keyboard
.key
;
289 term_event_modifier_T modifier
= ilev
->info
.keyboard
.modifier
;
296 if (modifier
== KBD_MOD_CTRL
&& (key
== 'l' || key
== 'L')) {
297 redraw_terminal_cls(term
);
300 } else if (key
== KBD_CTRL_C
) {
301 destroy_terminal(term
);
305 /* Character Conversions. */
307 /* struct term_event_keyboard carries UCS-4.
308 * - If the "utf_8_io" option is true or the "charset"
309 * option refers to UTF-8, then term->utf8_io is true,
310 * and handle_interlink_event() converts from UTF-8
312 * - Otherwise, handle_interlink_event() converts from
313 * the codepage specified with the "charset" option
315 utf8_io
= term
->utf8_io
;
317 /* struct term_event_keyboard carries bytes in the
318 * charset of the terminal.
319 * - If the "utf_8_io" option is true, then
320 * handle_interlink_event() converts from UTF-8 to
321 * UCS-4, and term_send_ucs() converts from UCS-4 to
322 * the codepage specified with the "charset" option;
323 * this codepage cannot be UTF-8.
324 * - Otherwise, handle_interlink_event() passes the
325 * bytes straight through. */
326 utf8_io
= get_opt_bool_tree(term
->spec
, "utf_8_io");
327 #endif /* CONFIG_UTF8 */
329 /* In UTF-8 byte sequences that have more than one byte, the
330 * first byte is between 0xC0 and 0xFF and the remaining bytes
331 * are between 0x80 and 0xBF. If there is just one byte, then
332 * it is between 0x00 and 0x7F and it is straight ASCII.
333 * (All 'betweens' are inclusive.) */
335 if (interlink
->utf8
.len
) {
336 /* A previous call to handle_interlink_event
337 * got a UTF-8 start byte. */
339 if (key
>= 0x80 && key
<= 0xBF && utf8_io
) {
340 /* This is a UTF-8 continuation byte. */
342 interlink
->utf8
.ucs
<<= 6;
343 interlink
->utf8
.ucs
|= key
& 0x3F;
344 if (! --interlink
->utf8
.len
) {
345 unicode_val_T u
= interlink
->utf8
.ucs
;
347 /* UTF-8 allows neither overlong
348 * sequences nor surrogates. */
349 if (u
< interlink
->utf8
.min
350 || is_utf16_surrogate(u
))
351 u
= UCS_REPLACEMENT_CHARACTER
;
352 term_send_ucs(term
, u
,
353 term
->interlink
->utf8
.modifier
);
358 /* The byte sequence for this character
359 * is ending prematurely. Send
360 * UCS_REPLACEMENT_CHARACTER for the
361 * terminated character, but don't break;
362 * let this byte be handled below. */
364 interlink
->utf8
.len
= 0;
365 term_send_ucs(term
, UCS_REPLACEMENT_CHARACTER
,
366 term
->interlink
->utf8
.modifier
);
370 /* Note: We know that key <= 0xFF. */
372 if (key
< 0x80 || !utf8_io
) {
373 /* This byte is not part of a multibyte character
374 * encoding: either it is outside of the ranges for
375 * UTF-8 start and continuation bytes or UTF-8 I/O mode
379 if (key
>= 0 && !utf8_io
) {
380 /* Not special and UTF-8 mode is disabled:
381 * recode from the terminal charset to UCS-4. */
383 key
= cp2u(get_opt_codepage_tree(term
->spec
,
386 term_send_ucs(term
, key
, modifier
);
389 #endif /* !CONFIG_UTF8 */
391 /* It must be special (e.g., F1 or Enter)
392 * or a single-byte UTF-8 character. */
393 set_kbd_term_event(&tev
, key
, modifier
);
394 term_send_event(term
, &tev
);
397 } else if ((key
& 0xC0) == 0xC0 && (key
& 0xFE) != 0xFE) {
398 /* This is a UTF-8 start byte. */
400 /* Minimum character values for UTF-8 octet
401 * sequences of each length, from RFC 2279.
402 * According to the RFC, UTF-8 decoders should
403 * reject characters that are encoded with
404 * more octets than necessary. (RFC 3629,
405 * which ELinks does not yet otherwise follow,
406 * tightened the "should" to "MUST".) */
407 static const unicode_val_T min
[] = {
408 0x00000080, /* ... 0x000007FF with 2 octets */
409 0x00000800, /* ... 0x0000FFFF with 3 octets */
410 0x00010000, /* ... 0x001FFFFF with 4 octets */
411 0x00200000, /* ... 0x03FFFFFF with 5 octets */
412 0x04000000 /* ... 0x7FFFFFFF with 6 octets */
417 /* Set @len = the number of contiguous 1's
418 * in the most significant bits of the first
419 * octet, i.e. @key. It is also the number
420 * of octets in the character. Leave @mask
421 * pointing to the first 0 bit found. */
422 for (mask
= 0x80; key
& mask
; mask
>>= 1)
425 /* This will hold because @key was checked above. */
426 assert(len
>= 2 && len
<= 6);
427 if_assert_failed
goto invalid_utf8_start_byte
;
429 interlink
->utf8
.min
= min
[len
- 2];
430 interlink
->utf8
.len
= len
- 1;
431 interlink
->utf8
.ucs
= key
& (mask
- 1);
432 interlink
->utf8
.modifier
= modifier
;
436 invalid_utf8_start_byte
:
437 term_send_ucs(term
, UCS_REPLACEMENT_CHARACTER
, modifier
);
442 destroy_terminal(term
);
446 ERROR(gettext("Bad event %d"), ilev
->ev
);
449 /* For EVENT_INIT we read a liitle more */
450 if (info
) return TERMINAL_INFO_SIZE
+ info
->length
;
451 return sizeof(*ilev
);
455 in_term(struct terminal
*term
)
457 struct terminal_interlink
*interlink
= term
->interlink
;
462 || !interlink
->qfreespace
463 || interlink
->qfreespace
- interlink
->qlen
> ALLOC_GR
) {
464 int qlen
= interlink
? interlink
->qlen
: 0;
465 int queuesize
= ((qlen
+ ALLOC_GR
) & ~(ALLOC_GR
- 1));
466 int newsize
= sizeof(*interlink
) + queuesize
;
468 interlink
= mem_realloc(interlink
, newsize
);
470 destroy_terminal(term
);
474 /* Blank the members for the first allocation */
475 if (!term
->interlink
)
476 memset(interlink
, 0, sizeof(*interlink
));
478 term
->interlink
= interlink
;
479 interlink
->qfreespace
= queuesize
- interlink
->qlen
;
482 iq
= interlink
->input_queue
;
483 r
= safe_read(term
->fdin
, iq
+ interlink
->qlen
, interlink
->qfreespace
);
485 if (r
== -1 && errno
!= ECONNRESET
)
486 ERROR(gettext("Could not read event: %d (%s)"),
487 errno
, (unsigned char *) strerror(errno
));
489 destroy_terminal(term
);
493 interlink
->qlen
+= r
;
494 interlink
->qfreespace
-= r
;
496 while (interlink
->qlen
>= sizeof(struct interlink_event
)) {
497 struct interlink_event
*ev
= (struct interlink_event
*) iq
;
498 int event_size
= handle_interlink_event(term
, ev
);
500 /* If the event was not handled save the bytes in the queue for
501 * later in case more stuff is read later. */
502 if (!event_size
) break;
504 /* Acount for the handled bytes */
505 interlink
->qlen
-= event_size
;
506 interlink
->qfreespace
+= event_size
;
508 /* If there are no more bytes to handle stop else move next
509 * event bytes to the front of the queue. */
510 if (!interlink
->qlen
) break;
511 memmove(iq
, iq
+ event_size
, interlink
->qlen
);