1 /* Terminal interface - low-level displaying implementation. */
16 #include "bookmarks/bookmarks.h"
17 #include "config/options.h"
18 #include "intl/gettext/libintl.h"
19 #include "main/main.h"
20 #include "main/module.h"
21 #include "main/object.h"
22 #include "main/select.h"
23 #include "osdep/osdep.h"
24 #include "osdep/signals.h"
25 #include "session/session.h"
26 #include "terminal/draw.h"
27 #include "terminal/event.h"
28 #include "terminal/hardio.h"
29 #include "terminal/kbd.h"
30 #include "terminal/screen.h"
31 #include "terminal/terminal.h"
32 #include "terminal/window.h"
33 #include "util/error.h"
34 #include "util/memory.h"
35 #include "util/string.h"
36 #include "viewer/text/textarea.h"
39 INIT_LIST_HEAD(terminals
);
41 static void check_if_no_terminal(void);
45 was_utf8(int in
, int out
)
47 /* Taken from setedit.
48 * Set cursor in the up left corner. Write "\357\200\240" == U+F020.
49 * Read cursor position. For UTF-8 x will be 2.
50 * For normal mode it will be 4. */
51 static unsigned char *str
= "\033[1;1H\357\200\240\033[6n";
52 unsigned char buf
[20];
55 hard_write(out
, str
, strlen(str
));
58 if (sscanf(buf
,"\033[%d;%dR",&y
,&x
)==2) {
66 redraw_terminal(struct terminal
*term
)
70 set_redraw_term_event(&ev
, term
->width
, term
->height
);
71 term_send_event(term
, &ev
);
75 redraw_terminal_cls(struct terminal
*term
)
79 set_resize_term_event(&ev
, term
->width
, term
->height
);
80 term_send_event(term
, &ev
);
84 cls_redraw_all_terminals(void)
86 struct terminal
*term
;
88 foreach (term
, terminals
)
89 redraw_terminal_cls(term
);
93 init_term(int fdin
, int fdout
)
95 unsigned char name
[MAX_TERM_LEN
+ 9] = "terminal.";
96 struct terminal
*term
= mem_calloc(1, sizeof(*term
));
99 check_if_no_terminal();
103 term
->screen
= init_screen();
109 init_list(term
->windows
);
113 term
->master
= (term
->fdout
== get_output_handle());
116 get_terminal_name(name
+ 9);
117 term
->spec
= get_opt_rec(config_options
, name
);
118 object_lock(term
->spec
);
121 /* The hack to restore console in the right mode */
122 if (get_opt_int_tree(term
->spec
, "type") == TERM_LINUX
) {
123 term
->linux_was_utf8
= was_utf8(get_input_handle(), term
->fdout
);
127 add_to_list(terminals
, term
);
129 set_handlers(fdin
, (select_handler_T
) in_term
, NULL
,
130 (select_handler_T
) destroy_terminal
, term
);
135 redraw_all_terminals(void)
137 struct terminal
*term
;
139 foreach (term
, terminals
)
144 destroy_terminal(struct terminal
*term
)
146 #ifdef CONFIG_BOOKMARKS
147 bookmark_auto_save_tabs(term
);
150 /* delete_window doesn't update term->current_tab, but it
151 calls redraw_terminal, which requires term->current_tab
152 to be valid if there are any tabs left. So set a value
153 that will be valid for that long. */
154 term
->current_tab
= 0;
156 while (!list_empty(term
->windows
))
157 delete_window(term
->windows
.next
);
159 /* mem_free_if(term->cwd); */
160 mem_free_if(term
->title
);
161 if (term
->screen
) done_screen(term
->screen
);
163 clear_handlers(term
->fdin
);
164 mem_free_if(term
->interlink
);
166 if (term
->blocked
!= -1) {
167 close(term
->blocked
);
168 clear_handlers(term
->blocked
);
175 /* This code doesn't work with slave terminals. */
176 if (get_opt_int_tree(term
->spec
, "type") == TERM_LINUX
) {
177 if (term
->linux_was_utf8
) {
178 hard_write(term
->fdout
, "\033%G", 3);
180 hard_write(term
->fdout
, "\033%@", 3);
185 if (term
->fdout
!= 1) {
186 if (term
->fdout
!= term
->fdin
) close(term
->fdout
);
188 unhandle_terminal_signals(term
);
190 #ifndef NO_FORK_ON_EXIT
191 if (!list_empty(terminals
)) {
197 object_unlock(term
->spec
);
199 check_if_no_terminal();
203 destroy_all_terminals(void)
205 while (!list_empty(terminals
))
206 destroy_terminal(terminals
.next
);
210 check_if_no_terminal(void)
212 program
.terminate
= list_empty(terminals
)
213 && !get_opt_bool("ui.sessions.keep_session_active");
217 exec_thread(unsigned char *path
, int p
)
219 int plen
= strlen(path
+ 1) + 2;
221 #if defined(HAVE_SETPGID) && !defined(CONFIG_OS_BEOS) && !defined(HAVE_BEGINTHREAD)
222 if (path
[0] == 2) setpgid(0, 0);
225 if (path
[plen
]) unlink(path
+ plen
);
229 close_handle(void *h
)
232 clear_handlers((long) h
);
236 unblock_terminal(struct terminal
*term
)
238 close_handle((void *) (long) term
->blocked
);
240 set_handlers(term
->fdin
, (select_handler_T
) in_term
, NULL
,
241 (select_handler_T
) destroy_terminal
, term
);
242 unblock_itrm(term
->fdin
);
243 redraw_terminal_cls(term
);
244 if (textarea_editor
) /* XXX */
245 textarea_edit(1, NULL
, NULL
, NULL
, NULL
);
250 exec_on_master_terminal(struct terminal
*term
,
251 unsigned char *path
, int plen
,
252 unsigned char *delete, int dlen
,
256 int param_size
= plen
+ dlen
+ 2 /* 2 null char */ + 1 /* fg */;
257 unsigned char *param
= fmem_alloc(param_size
);
262 memcpy(param
+ 1, path
, plen
+ 1);
263 memcpy(param
+ 1 + plen
+ 1, delete, dlen
+ 1);
265 if (fg
== 1) block_itrm(term
->fdin
);
267 blockh
= start_thread((void (*)(void *, int)) exec_thread
,
271 if (fg
== 1) unblock_itrm(term
->fdin
);
276 term
->blocked
= blockh
;
278 (select_handler_T
) unblock_terminal
,
280 (select_handler_T
) unblock_terminal
,
282 set_handlers(term
->fdin
, NULL
, NULL
,
283 (select_handler_T
) destroy_terminal
,
287 set_handlers(blockh
, close_handle
, NULL
,
288 close_handle
, (void *) (long) blockh
);
293 exec_on_slave_terminal( struct terminal
*term
,
294 unsigned char *path
, int plen
,
295 unsigned char *delete, int dlen
,
298 int data_size
= plen
+ dlen
+ 1 /* 0 */ + 1 /* fg */ + 2 /* 2 null char */;
299 unsigned char *data
= fmem_alloc(data_size
);
305 memcpy(data
+ 2, path
, plen
+ 1);
306 memcpy(data
+ 2 + plen
+ 1, delete, dlen
+ 1);
307 hard_write(term
->fdout
, data
, data_size
);
312 exec_on_terminal(struct terminal
*term
, unsigned char *path
,
313 unsigned char *delete, int fg
)
327 dispatch_special(delete);
331 if (fg
&& is_blocked()) {
336 exec_on_master_terminal(term
,
338 delete, strlen(delete),
341 exec_on_slave_terminal( term
,
343 delete, strlen(delete),
349 exec_shell(struct terminal
*term
)
353 if (!can_open_os_shell(term
->environment
)) return;
357 exec_on_terminal(term
, sh
, "", 1);
362 do_terminal_function(struct terminal
*term
, unsigned char code
,
365 int data_len
= strlen(data
);
366 unsigned char *x_data
= fmem_alloc(data_len
+ 1 /* code */ + 1 /* null char */);
370 memcpy(x_data
+ 1, data
, data_len
+ 1);
371 exec_on_terminal(term
, NULL
, x_data
, 0);
376 set_terminal_title(struct terminal
*term
, unsigned char *title
)
378 if (term
->title
&& !strcmp(title
, term
->title
)) return;
379 mem_free_set(&term
->title
, stracpy(title
));
380 do_terminal_function(term
, TERM_FN_TITLE
, title
);
383 static int terminal_pipe
[2];
386 check_terminal_pipes(void)
388 return c_pipe(terminal_pipe
);
392 close_terminal_pipes(void)
394 close(terminal_pipe
[0]);
395 close(terminal_pipe
[1]);
399 attach_terminal(int in
, int out
, int ctl
, void *info
, int len
)
401 struct terminal
*term
;
403 if (set_nonblocking_fd(terminal_pipe
[0]) < 0) return NULL
;
404 if (set_nonblocking_fd(terminal_pipe
[1]) < 0) return NULL
;
405 handle_trm(in
, out
, out
, terminal_pipe
[1], ctl
, info
, len
, 0);
407 term
= init_term(terminal_pipe
[0], out
);
409 close_terminal_pipes();
416 static struct module
*terminal_submodules
[] = {
417 &terminal_screen_module
,
421 struct module terminal_module
= struct_module(
422 /* name: */ "Terminal",
425 /* submodules: */ terminal_submodules
,