1 /** Terminal interface - low-level displaying implementation.
11 #include <sys/types.h>
18 #include "bookmarks/bookmarks.h"
19 #include "config/options.h"
20 #include "main/main.h"
21 #include "main/module.h"
22 #include "main/object.h"
23 #include "main/select.h"
24 #include "osdep/osdep.h"
25 #include "osdep/signals.h"
26 #include "session/session.h"
27 #include "terminal/draw.h"
28 #include "terminal/event.h"
29 #include "terminal/hardio.h"
30 #include "terminal/kbd.h"
31 #include "terminal/screen.h"
32 #include "terminal/terminal.h"
33 #include "terminal/window.h"
34 #include "util/error.h"
35 #include "util/memory.h"
36 #include "util/string.h"
37 #include "viewer/text/textarea.h"
40 INIT_LIST_OF(struct terminal
, terminals
);
42 static void check_if_no_terminal(void);
45 redraw_terminal(struct terminal
*term
)
49 set_redraw_term_event(&ev
, term
->width
, term
->height
);
50 term_send_event(term
, &ev
);
54 redraw_terminal_cls(struct terminal
*term
)
58 set_resize_term_event(&ev
, term
->width
, term
->height
);
59 term_send_event(term
, &ev
);
63 cls_redraw_all_terminals(void)
65 struct terminal
*term
;
67 foreach (term
, terminals
)
68 redraw_terminal_cls(term
);
72 init_term(int fdin
, int fdout
)
74 unsigned char name
[MAX_TERM_LEN
+ 9] = "terminal.";
75 struct terminal
*term
= mem_calloc(1, sizeof(*term
));
78 check_if_no_terminal();
82 term
->screen
= init_screen();
88 init_list(term
->windows
);
92 term
->master
= (term
->fdout
== get_output_handle());
95 get_terminal_name(name
+ 9);
96 term
->spec
= get_opt_rec(config_options
, name
);
97 object_lock(term
->spec
);
99 add_to_list(terminals
, term
);
101 set_handlers(fdin
, (select_handler_T
) in_term
, NULL
,
102 (select_handler_T
) destroy_terminal
, term
);
107 redraw_all_terminals(void)
109 struct terminal
*term
;
111 foreach (term
, terminals
)
116 destroy_terminal(struct terminal
*term
)
118 #ifdef CONFIG_BOOKMARKS
119 bookmark_auto_save_tabs(term
);
122 free_textarea_data(term
);
124 /* delete_window doesn't update term->current_tab, but it
125 calls redraw_terminal, which requires term->current_tab
126 to be valid if there are any tabs left. So set a value
127 that will be valid for that long. */
128 term
->current_tab
= 0;
130 while (!list_empty(term
->windows
))
131 delete_window(term
->windows
.next
);
133 /* mem_free_if(term->cwd); */
134 mem_free_if(term
->title
);
135 if (term
->screen
) done_screen(term
->screen
);
137 clear_handlers(term
->fdin
);
138 mem_free_if(term
->interlink
);
140 if (term
->blocked
!= -1) {
141 close(term
->blocked
);
142 clear_handlers(term
->blocked
);
148 if (term
->fdout
!= 1) {
149 if (term
->fdout
!= term
->fdin
) close(term
->fdout
);
151 unhandle_terminal_signals(term
);
153 #ifndef NO_FORK_ON_EXIT
154 if (!list_empty(terminals
)) {
160 object_unlock(term
->spec
);
162 check_if_no_terminal();
166 destroy_all_terminals(void)
168 while (!list_empty(terminals
))
169 destroy_terminal(terminals
.next
);
173 check_if_no_terminal(void)
175 program
.terminate
= list_empty(terminals
)
176 && !get_opt_bool("ui.sessions.keep_session_active", NULL
);
180 exec_thread(unsigned char *path
, int p
)
182 int plen
= strlen(path
+ 1) + 2;
184 #if defined(HAVE_SETPGID) && !defined(CONFIG_OS_BEOS) && !defined(HAVE_BEGINTHREAD)
185 if (path
[0] == TERM_EXEC_NEWWIN
) setpgid(0, 0);
188 if (path
[plen
]) unlink(path
+ plen
);
192 close_handle(void *h
)
195 clear_handlers((long) h
);
199 unblock_terminal(struct terminal
*term
)
201 close_handle((void *) (long) term
->blocked
);
203 set_handlers(term
->fdin
, (select_handler_T
) in_term
, NULL
,
204 (select_handler_T
) destroy_terminal
, term
);
206 redraw_terminal_cls(term
);
207 if (term
->textarea_data
) /* XXX */
208 textarea_edit(1, term
, NULL
, NULL
, NULL
);
213 exec_on_master_terminal(struct terminal
*term
,
214 unsigned char *path
, int plen
,
215 unsigned char *delete, int dlen
,
219 int param_size
= plen
+ dlen
+ 2 /* 2 null char */ + 1 /* fg */;
220 unsigned char *param
= fmem_alloc(param_size
);
225 memcpy(param
+ 1, path
, plen
+ 1);
226 memcpy(param
+ 1 + plen
+ 1, delete, dlen
+ 1);
228 if (fg
== TERM_EXEC_FG
) block_itrm();
230 blockh
= start_thread((void (*)(void *, int)) exec_thread
,
234 if (fg
== TERM_EXEC_FG
) unblock_itrm();
238 if (fg
== TERM_EXEC_FG
) {
239 term
->blocked
= blockh
;
241 (select_handler_T
) unblock_terminal
,
243 (select_handler_T
) unblock_terminal
,
245 set_handlers(term
->fdin
, NULL
, NULL
,
246 (select_handler_T
) destroy_terminal
,
250 set_handlers(blockh
, close_handle
, NULL
,
251 close_handle
, (void *) (long) blockh
);
256 exec_on_slave_terminal( struct terminal
*term
,
257 unsigned char *path
, int plen
,
258 unsigned char *delete, int dlen
,
261 int data_size
= plen
+ dlen
+ 1 /* 0 */ + 1 /* fg */ + 2 /* 2 null char */;
262 unsigned char *data
= fmem_alloc(data_size
);
268 memcpy(data
+ 2, path
, plen
+ 1);
269 memcpy(data
+ 2 + plen
+ 1, delete, dlen
+ 1);
270 hard_write(term
->fdout
, data
, data_size
);
275 exec_on_terminal(struct terminal
*term
, unsigned char *path
,
276 unsigned char *delete, enum term_exec fg
)
290 dispatch_special(delete);
294 /* TODO: Should this be changed to allow TERM_EXEC_NEWWIN
295 * in a blocked terminal? There is similar code in
296 * in_sock(). --KON, 2007 */
297 if (fg
!= TERM_EXEC_BG
&& is_blocked()) {
302 exec_on_master_terminal(term
,
304 delete, strlen(delete),
307 exec_on_slave_terminal( term
,
309 delete, strlen(delete),
315 exec_shell(struct terminal
*term
)
319 if (!can_open_os_shell(term
->environment
)) return;
323 exec_on_terminal(term
, sh
, "", TERM_EXEC_FG
);
328 do_terminal_function(struct terminal
*term
, unsigned char code
,
331 int data_len
= strlen(data
);
332 unsigned char *x_data
= fmem_alloc(data_len
+ 1 /* code */ + 1 /* null char */);
336 memcpy(x_data
+ 1, data
, data_len
+ 1);
337 exec_on_terminal(term
, NULL
, x_data
, TERM_EXEC_BG
);
342 set_terminal_title(struct terminal
*term
, unsigned char *title
)
344 if (term
->title
&& !strcmp(title
, term
->title
)) return;
345 mem_free_set(&term
->title
, stracpy(title
));
346 do_terminal_function(term
, TERM_FN_TITLE
, title
);
349 static int terminal_pipe
[2];
352 check_terminal_pipes(void)
354 return c_pipe(terminal_pipe
);
358 close_terminal_pipes(void)
360 close(terminal_pipe
[0]);
361 close(terminal_pipe
[1]);
365 attach_terminal(int in
, int out
, int ctl
, void *info
, int len
)
367 struct terminal
*term
;
369 if (set_nonblocking_fd(terminal_pipe
[0]) < 0) return NULL
;
370 if (set_nonblocking_fd(terminal_pipe
[1]) < 0) return NULL
;
371 handle_trm(in
, out
, out
, terminal_pipe
[1], ctl
, info
, len
, 0);
373 term
= init_term(terminal_pipe
[0], out
);
375 close_terminal_pipes();
382 static struct module
*terminal_submodules
[] = {
383 &terminal_screen_module
,
387 struct module terminal_module
= struct_module(
388 /* Because this module is listed in main_modules rather than
389 * in builtin_modules, its name does not appear in the user
390 * interface and so need not be translatable. */
391 /* name: */ "Terminal",
394 /* submodules: */ terminal_submodules
,