1 /* Event system support routines. */
16 #include "intl/gettext/libintl.h"
17 #include "main/main.h" /* terminate */
18 #include "main/object.h"
19 #include "session/session.h"
20 #include "terminal/draw.h"
21 #include "terminal/event.h"
22 #include "terminal/kbd.h"
23 #include "terminal/mouse.h"
24 #include "terminal/tab.h"
25 #include "terminal/terminal.h"
26 #include "terminal/screen.h"
27 #include "terminal/window.h"
28 #include "util/conv.h"
29 #include "util/error.h"
30 #include "util/memory.h"
31 #include "util/snprintf.h"
32 #include "util/string.h"
33 #include "viewer/timer.h"
36 /* Information used for communication between ELinks instances */
37 struct terminal_interlink
{
38 /* How big the input queue is and how much is free */
42 /* UTF8 input key value decoding data. */
49 /* This is the queue of events as coming from the other ELinks instance
50 * owning the hosting terminal. */
51 unsigned char input_queue
[1];
56 term_send_event(struct terminal
*term
, struct term_event
*ev
)
61 if_assert_failed
return;
67 int width
= ev
->info
.size
.width
;
68 int height
= ev
->info
.size
.height
;
70 if (width
< 0 || height
< 0) {
71 ERROR(gettext("Bad terminal size: %d, %d"),
76 resize_screen(term
, width
, height
);
81 /* Nasty hack to avoid assertion failures when doing -remote
82 * stuff and the client exits right away */
83 if (!term
->screen
->image
) break;
86 term
->redrawing
= TREDRAW_DELAYED
;
87 /* Note that you do NOT want to ever go and create new
88 * window inside EVENT_INIT handler (it'll get second
89 * EVENT_INIT here). Perhaps the best thing you could do
90 * is registering a bottom-half handler which will open
93 if (ev
->ev
== EVENT_RESIZE
) {
94 /* We want to propagate EVENT_RESIZE even to inactive
95 * tabs! Nothing wrong will get drawn (in the final
96 * result) as the active tab is always the first one,
97 * thus will be drawn last here. Thanks, Witek!
99 foreachback (win
, term
->windows
)
100 win
->handler(win
, ev
);
103 foreachback (win
, term
->windows
)
104 if (!inactive_tab(win
))
105 win
->handler(win
, ev
);
107 term
->redrawing
= TREDRAW_READY
;
113 assert(!list_empty(term
->windows
));
114 if_assert_failed
break;
116 /* We need to send event to correct tab, not to the first one. --karpov */
117 /* ...if we want to send it to a tab at all. --pasky */
118 win
= term
->windows
.next
;
119 if (win
->type
== WINDOW_TAB
) {
120 win
= get_current_tab(term
);
121 assertm(win
, "No tab to send the event to!");
122 if_assert_failed
return;
125 win
->handler(win
, ev
);
130 term_send_ucs(struct terminal
*term
, struct term_event
*ev
, unicode_val_T u
)
132 unsigned char *recoded
;
134 recoded
= u2cp_no_nbsp(u
, get_opt_int_tree(term
->spec
, "charset"));
135 if (!recoded
) recoded
= "*";
137 ev
->info
.keyboard
.key
= *recoded
;
138 term_send_event(term
, ev
);
144 check_terminal_name(struct terminal
*term
, struct terminal_info
*info
)
146 unsigned char name
[MAX_TERM_LEN
+ 10];
149 /* We check TERM env. var for sanity, and fallback to _template_ if
150 * needed. This way we prevent elinks.conf potential corruption. */
151 for (i
= 0; info
->name
[i
]; i
++) {
152 if (isident(info
->name
[i
])) continue;
154 usrerror(_("Warning: terminal name contains illicit chars.", term
));
158 snprintf(name
, sizeof(name
), "terminal.%s", info
->name
);
160 /* Unlock the default _template_ option tree that was asigned by
161 * init_term() and get the correct one. */
162 object_unlock(term
->spec
);
163 term
->spec
= get_opt_rec(config_options
, name
);
164 object_lock(term
->spec
);
169 ignore_mouse_event(struct terminal
*term
, struct term_event
*ev
)
171 struct term_event_mouse
*prev
= &term
->prev_mouse_event
;
172 struct term_event_mouse
*current
= &ev
->info
.mouse
;
174 if (check_mouse_action(ev
, B_UP
)
175 && current
->y
== prev
->y
176 && (current
->button
& ~BM_ACT
) == (prev
->button
& ~BM_ACT
)) {
177 do_not_ignore_next_mouse_event(term
);
182 copy_struct(prev
, current
);
189 handle_interlink_event(struct terminal
*term
, struct term_event
*ev
)
191 struct terminal_info
*info
= NULL
;
192 struct terminal_interlink
*interlink
= term
->interlink
;
196 if (interlink
->qlen
< TERMINAL_INFO_SIZE
)
199 info
= (struct terminal_info
*) ev
;
201 if (interlink
->qlen
< TERMINAL_INFO_SIZE
+ info
->length
)
204 info
->name
[MAX_TERM_LEN
- 1] = 0;
205 check_terminal_name(term
, info
);
207 memcpy(term
->cwd
, info
->cwd
, MAX_CWD_LEN
);
208 term
->cwd
[MAX_CWD_LEN
- 1] = 0;
210 term
->environment
= info
->system_env
;
212 /* We need to make sure that it is possible to draw on the
213 * terminal screen before decoding the session info so that
214 * handling of bad URL syntax by openning msg_box() will be
216 term_send_event(term
, ev
);
218 /* Either the initialization of the first session failed or we
219 * are doing a remote session so quit.*/
220 if (!decode_session_info(term
, info
)) {
221 destroy_terminal(term
);
222 /* Make sure the user is notified if the initialization
223 * of the first session fails. */
224 if (program
.terminate
) {
225 usrerror(_("Failed to create session.", term
));
226 program
.retval
= RET_FATAL
;
231 ev
->ev
= EVENT_REDRAW
;
235 term_send_event(term
, ev
);
241 if (!ignore_mouse_event(term
, ev
))
242 term_send_event(term
, ev
);
249 int key
= get_kbd_key(ev
);
253 if (check_kbd_modifier(ev
, KBD_MOD_CTRL
) && toupper(key
) == 'L') {
254 redraw_terminal_cls(term
);
257 } else if (key
== KBD_CTRL_C
) {
258 destroy_terminal(term
);
262 if (interlink
->utf_8
.len
) {
263 utf8_io
= get_opt_bool_tree(term
->spec
, "utf_8_io");
265 if ((key
& 0xC0) == 0x80 && utf8_io
) {
266 interlink
->utf_8
.ucs
<<= 6;
267 interlink
->utf_8
.ucs
|= key
& 0x3F;
268 if (! --interlink
->utf_8
.len
) {
269 unicode_val_T u
= interlink
->utf_8
.ucs
;
271 if (u
< interlink
->utf_8
.min
)
273 term_send_ucs(term
, ev
, u
);
278 interlink
->utf_8
.len
= 0;
279 term_send_ucs(term
, ev
, UCS_NO_CHAR
);
283 if (key
< 0x80 || key
> 0xFF
285 ? !get_opt_bool_tree(term
->spec
, "utf_8_io")
288 term_send_event(term
, ev
);
291 } else if ((key
& 0xC0) == 0xC0 && (key
& 0xFE) != 0xFE) {
292 unsigned int mask
, cov
= 0x80;
295 for (mask
= 0x80; key
& mask
; mask
>>= 1) {
297 interlink
->utf_8
.min
= cov
;
298 cov
= 1 << (1 + 5 * len
);
301 interlink
->utf_8
.len
= len
- 1;
302 interlink
->utf_8
.ucs
= key
& (mask
- 1);
306 term_send_ucs(term
, ev
, UCS_NO_CHAR
);
311 destroy_terminal(term
);
315 ERROR(gettext("Bad event %d"), ev
->ev
);
318 /* For EVENT_INIT we read a liitle more */
319 if (info
) return TERMINAL_INFO_SIZE
+ info
->length
;
324 in_term(struct terminal
*term
)
326 struct terminal_interlink
*interlink
= term
->interlink
;
331 || !interlink
->qfreespace
332 || interlink
->qfreespace
- interlink
->qlen
> ALLOC_GR
) {
333 int qlen
= interlink
? interlink
->qlen
: 0;
334 int queuesize
= ((qlen
+ ALLOC_GR
) & ~(ALLOC_GR
- 1));
335 int newsize
= sizeof(*interlink
) + queuesize
;
337 interlink
= mem_realloc(interlink
, newsize
);
339 destroy_terminal(term
);
343 /* Blank the members for the first allocation */
344 if (!term
->interlink
)
345 memset(interlink
, 0, sizeof(*interlink
));
347 term
->interlink
= interlink
;
348 interlink
->qfreespace
= queuesize
- interlink
->qlen
;
351 iq
= interlink
->input_queue
;
352 r
= safe_read(term
->fdin
, iq
+ interlink
->qlen
, interlink
->qfreespace
);
354 if (r
== -1 && errno
!= ECONNRESET
)
355 ERROR(gettext("Could not read event: %d (%s)"),
356 errno
, (unsigned char *) strerror(errno
));
358 destroy_terminal(term
);
362 interlink
->qlen
+= r
;
363 interlink
->qfreespace
-= r
;
365 while (interlink
->qlen
>= sizeof(struct term_event
)) {
366 struct term_event
*ev
= (struct term_event
*) iq
;
367 int event_size
= handle_interlink_event(term
, ev
);
369 /* If the event was not handled save the bytes in the queue for
370 * later in case more stuff is read later. */
371 if (!event_size
) break;
373 /* Acount for the handled bytes */
374 interlink
->qlen
-= event_size
;
375 interlink
->qfreespace
+= event_size
;
377 /* If there are no more bytes to handle stop else move next
378 * event bytes to the front of the queue. */
379 if (!interlink
->qlen
) break;
380 memmove(iq
, iq
+ event_size
, interlink
->qlen
);