The UTF-8 detection reactivated. It trashes slightly screen,
[elinks.git] / src / terminal / terminal.c
blob83675cd4cfecea6ca110acb377eb429cb9907cfc
1 /* Terminal interface - low-level displaying implementation. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #ifdef HAVE_UNISTD_H
11 #include <unistd.h>
12 #endif
14 #include "elinks.h"
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);
43 static int
44 was_utf8(int in, int out)
46 /* Taken from setedit.
47 * Set cursor in the up left corner. Write "\357\200\240" == U+F020.
48 * Read cursor position. For UTF-8 x will be 2.
49 * For normal mode it will be 4. */
50 static unsigned char *str = "\033[1;1H\357\200\240\033[6n";
51 unsigned char buf[20];
52 int x, y;
54 hard_write(out, str, strlen(str));
55 buf[0] = '\0';
56 read(in, buf, 6);
57 if (sscanf(buf,"\033[%d;%dR",&y,&x)==2) {
58 if (x > 2) return 0;
60 return 1;
63 void
64 redraw_terminal(struct terminal *term)
66 struct term_event ev;
68 set_redraw_term_event(&ev, term->width, term->height);
69 term_send_event(term, &ev);
72 void
73 redraw_terminal_cls(struct terminal *term)
75 struct term_event ev;
77 set_resize_term_event(&ev, term->width, term->height);
78 term_send_event(term, &ev);
81 void
82 cls_redraw_all_terminals(void)
84 struct terminal *term;
86 foreach (term, terminals)
87 redraw_terminal_cls(term);
90 struct terminal *
91 init_term(int fdin, int fdout)
93 unsigned char name[MAX_TERM_LEN + 9] = "terminal.";
94 struct terminal *term = mem_calloc(1, sizeof(*term));
96 if (!term) {
97 check_if_no_terminal();
98 return NULL;
101 term->screen = init_screen();
102 if (!term->screen) {
103 mem_free(term);
104 return NULL;
107 init_list(term->windows);
109 term->fdin = fdin;
110 term->fdout = fdout;
111 term->master = (term->fdout == get_output_handle());
112 term->blocked = -1;
114 get_terminal_name(name + 9);
115 term->spec = get_opt_rec(config_options, name);
116 object_lock(term->spec);
118 /* The hack to restore console in the right mode */
119 if (get_opt_int_tree(term->spec, "type") == TERM_LINUX) {
120 term->linux_was_utf8 = was_utf8(get_input_handle(), get_output_handle());
123 add_to_list(terminals, term);
125 set_handlers(fdin, (select_handler_T) in_term, NULL,
126 (select_handler_T) destroy_terminal, term);
127 return term;
130 void
131 redraw_all_terminals(void)
133 struct terminal *term;
135 foreach (term, terminals)
136 redraw_screen(term);
139 void
140 destroy_terminal(struct terminal *term)
142 #ifdef CONFIG_BOOKMARKS
143 bookmark_auto_save_tabs(term);
144 #endif
146 /* delete_window doesn't update term->current_tab, but it
147 calls redraw_terminal, which requires term->current_tab
148 to be valid if there are any tabs left. So set a value
149 that will be valid for that long. */
150 term->current_tab = 0;
152 while (!list_empty(term->windows))
153 delete_window(term->windows.next);
155 /* mem_free_if(term->cwd); */
156 mem_free_if(term->title);
157 if (term->screen) done_screen(term->screen);
159 clear_handlers(term->fdin);
160 mem_free_if(term->interlink);
162 if (term->blocked != -1) {
163 close(term->blocked);
164 clear_handlers(term->blocked);
167 del_from_list(term);
168 close(term->fdin);
170 if (get_opt_int_tree(term->spec, "type") == TERM_LINUX) {
171 if (term->linux_was_utf8) {
172 hard_write(term->fdout, "\033%G", 3);
173 } else {
174 hard_write(term->fdout, "\033%@", 3);
178 if (term->fdout != 1) {
179 if (term->fdout != term->fdin) close(term->fdout);
180 } else {
181 unhandle_terminal_signals(term);
182 free_all_itrms();
183 #ifndef NO_FORK_ON_EXIT
184 if (!list_empty(terminals)) {
185 if (fork()) exit(0);
187 #endif
190 object_unlock(term->spec);
191 mem_free(term);
192 check_if_no_terminal();
195 void
196 destroy_all_terminals(void)
198 while (!list_empty(terminals))
199 destroy_terminal(terminals.next);
202 static void
203 check_if_no_terminal(void)
205 program.terminate = list_empty(terminals)
206 && !get_opt_bool("ui.sessions.keep_session_active");
209 void
210 exec_thread(unsigned char *path, int p)
212 int plen = strlen(path + 1) + 2;
214 #if defined(HAVE_SETPGID) && !defined(CONFIG_OS_BEOS) && !defined(HAVE_BEGINTHREAD)
215 if (path[0] == 2) setpgid(0, 0);
216 #endif
217 exe(path + 1);
218 if (path[plen]) unlink(path + plen);
221 void
222 close_handle(void *h)
224 close((long) h);
225 clear_handlers((long) h);
228 static void
229 unblock_terminal(struct terminal *term)
231 close_handle((void *) (long) term->blocked);
232 term->blocked = -1;
233 set_handlers(term->fdin, (select_handler_T) in_term, NULL,
234 (select_handler_T) destroy_terminal, term);
235 unblock_itrm(term->fdin);
236 redraw_terminal_cls(term);
237 if (textarea_editor) /* XXX */
238 textarea_edit(1, NULL, NULL, NULL, NULL);
242 static void
243 exec_on_master_terminal(struct terminal *term,
244 unsigned char *path, int plen,
245 unsigned char *delete, int dlen,
246 int fg)
248 int blockh;
249 int param_size = plen + dlen + 2 /* 2 null char */ + 1 /* fg */;
250 unsigned char *param = fmem_alloc(param_size);
252 if (!param) return;
254 param[0] = fg;
255 memcpy(param + 1, path, plen + 1);
256 memcpy(param + 1 + plen + 1, delete, dlen + 1);
258 if (fg == 1) block_itrm(term->fdin);
260 blockh = start_thread((void (*)(void *, int)) exec_thread,
261 param, param_size);
262 fmem_free(param);
263 if (blockh == -1) {
264 if (fg == 1) unblock_itrm(term->fdin);
265 return;
268 if (fg == 1) {
269 term->blocked = blockh;
270 set_handlers(blockh,
271 (select_handler_T) unblock_terminal,
272 NULL,
273 (select_handler_T) unblock_terminal,
274 term);
275 set_handlers(term->fdin, NULL, NULL,
276 (select_handler_T) destroy_terminal,
277 term);
279 } else {
280 set_handlers(blockh, close_handle, NULL,
281 close_handle, (void *) (long) blockh);
285 static void
286 exec_on_slave_terminal( struct terminal *term,
287 unsigned char *path, int plen,
288 unsigned char *delete, int dlen,
289 int fg)
291 int data_size = plen + dlen + 1 /* 0 */ + 1 /* fg */ + 2 /* 2 null char */;
292 unsigned char *data = fmem_alloc(data_size);
294 if (!data) return;
296 data[0] = 0;
297 data[1] = fg;
298 memcpy(data + 2, path, plen + 1);
299 memcpy(data + 2 + plen + 1, delete, dlen + 1);
300 hard_write(term->fdout, data, data_size);
301 fmem_free(data);
304 void
305 exec_on_terminal(struct terminal *term, unsigned char *path,
306 unsigned char *delete, int fg)
308 if (path) {
309 if (!*path) return;
310 } else {
311 path = "";
314 #ifdef NO_FG_EXEC
315 fg = 0;
316 #endif
318 if (term->master) {
319 if (!*path) {
320 dispatch_special(delete);
321 return;
324 if (fg && is_blocked()) {
325 unlink(delete);
326 return;
329 exec_on_master_terminal(term,
330 path, strlen(path),
331 delete, strlen(delete),
332 fg);
333 } else {
334 exec_on_slave_terminal( term,
335 path, strlen(path),
336 delete, strlen(delete),
337 fg);
341 void
342 exec_shell(struct terminal *term)
344 unsigned char *sh;
346 if (!can_open_os_shell(term->environment)) return;
348 sh = get_shell();
349 if (sh && *sh)
350 exec_on_terminal(term, sh, "", 1);
354 void
355 do_terminal_function(struct terminal *term, unsigned char code,
356 unsigned char *data)
358 int data_len = strlen(data);
359 unsigned char *x_data = fmem_alloc(data_len + 1 /* code */ + 1 /* null char */);
361 if (!x_data) return;
362 x_data[0] = code;
363 memcpy(x_data + 1, data, data_len + 1);
364 exec_on_terminal(term, NULL, x_data, 0);
365 fmem_free(x_data);
368 void
369 set_terminal_title(struct terminal *term, unsigned char *title)
371 if (term->title && !strcmp(title, term->title)) return;
372 mem_free_set(&term->title, stracpy(title));
373 do_terminal_function(term, TERM_FN_TITLE, title);
376 static int terminal_pipe[2];
379 check_terminal_pipes(void)
381 return c_pipe(terminal_pipe);
384 void
385 close_terminal_pipes(void)
387 close(terminal_pipe[0]);
388 close(terminal_pipe[1]);
391 struct terminal *
392 attach_terminal(int in, int out, int ctl, void *info, int len)
394 struct terminal *term;
396 if (set_nonblocking_fd(terminal_pipe[0]) < 0) return NULL;
397 if (set_nonblocking_fd(terminal_pipe[1]) < 0) return NULL;
398 handle_trm(in, out, out, terminal_pipe[1], ctl, info, len, 0);
400 term = init_term(terminal_pipe[0], out);
401 if (!term) {
402 close_terminal_pipes();
403 return NULL;
406 return term;
409 static struct module *terminal_submodules[] = {
410 &terminal_screen_module,
411 NULL
414 struct module terminal_module = struct_module(
415 /* name: */ "Terminal",
416 /* options: */ NULL,
417 /* hooks: */ NULL,
418 /* submodules: */ terminal_submodules,
419 /* data: */ NULL,
420 /* init: */ NULL,
421 /* done: */ NULL