(message-do-fcc): Modernise the code slightly.
[emacs.git] / src / terminal.c
blob0b1cbe7b7913bd004036b5e1f430dcd79f5f444b
1 /* Functions related to terminal devices.
2 Copyright (C) 2005-2017 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19 #include <config.h>
21 #include <stdio.h>
23 #include "lisp.h"
24 #include "character.h"
25 #include "frame.h"
26 #include "termchar.h"
27 #include "termhooks.h"
28 #include "keyboard.h"
30 #if HAVE_STRUCT_UNIPAIR_UNICODE
31 # include <errno.h>
32 # include <linux/kd.h>
33 # include <sys/ioctl.h>
34 #endif
36 /* Chain of all terminals currently in use. */
37 struct terminal *terminal_list;
39 /* The first unallocated terminal id. */
40 static int next_terminal_id;
42 /* The initial terminal device, created by initial_term_init. */
43 struct terminal *initial_terminal;
45 static void delete_initial_terminal (struct terminal *);
47 /* This setter is used only in this file, so it can be private. */
48 static void
49 tset_param_alist (struct terminal *t, Lisp_Object val)
51 t->param_alist = val;
56 void
57 ring_bell (struct frame *f)
59 if (!NILP (Vring_bell_function))
61 Lisp_Object function;
63 /* Temporarily set the global variable to nil
64 so that if we get an error, it stays nil
65 and we don't call it over and over.
67 We don't specbind it, because that would carefully
68 restore the bad value if there's an error
69 and make the loop of errors happen anyway. */
71 function = Vring_bell_function;
72 Vring_bell_function = Qnil;
74 call0 (function);
76 Vring_bell_function = function;
78 else if (FRAME_TERMINAL (f)->ring_bell_hook)
79 (*FRAME_TERMINAL (f)->ring_bell_hook) (f);
82 void
83 update_begin (struct frame *f)
85 if (FRAME_TERMINAL (f)->update_begin_hook)
86 (*FRAME_TERMINAL (f)->update_begin_hook) (f);
89 void
90 update_end (struct frame *f)
92 if (FRAME_TERMINAL (f)->update_end_hook)
93 (*FRAME_TERMINAL (f)->update_end_hook) (f);
96 /* Specify how many text lines, from the top of the window,
97 should be affected by insert-lines and delete-lines operations.
98 This, and those operations, are used only within an update
99 that is bounded by calls to update_begin and update_end. */
101 void
102 set_terminal_window (struct frame *f, int size)
104 if (FRAME_TERMINAL (f)->set_terminal_window_hook)
105 (*FRAME_TERMINAL (f)->set_terminal_window_hook) (f, size);
108 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
109 frame-relative coordinates. */
111 void
112 cursor_to (struct frame *f, int vpos, int hpos)
114 if (FRAME_TERMINAL (f)->cursor_to_hook)
115 (*FRAME_TERMINAL (f)->cursor_to_hook) (f, vpos, hpos);
118 /* Similar but don't take any account of the wasted characters. */
120 void
121 raw_cursor_to (struct frame *f, int row, int col)
123 if (FRAME_TERMINAL (f)->raw_cursor_to_hook)
124 (*FRAME_TERMINAL (f)->raw_cursor_to_hook) (f, row, col);
127 /* Erase operations. */
129 /* Clear from cursor to end of frame. */
130 void
131 clear_to_end (struct frame *f)
133 if (FRAME_TERMINAL (f)->clear_to_end_hook)
134 (*FRAME_TERMINAL (f)->clear_to_end_hook) (f);
137 /* Clear entire frame. */
139 void
140 clear_frame (struct frame *f)
142 if (FRAME_TERMINAL (f)->clear_frame_hook)
143 (*FRAME_TERMINAL (f)->clear_frame_hook) (f);
146 /* Clear from cursor to end of line.
147 Assume that the line is already clear starting at column first_unused_hpos.
149 Note that the cursor may be moved, on terminals lacking a `ce' string. */
151 void
152 clear_end_of_line (struct frame *f, int first_unused_hpos)
154 if (FRAME_TERMINAL (f)->clear_end_of_line_hook)
155 (*FRAME_TERMINAL (f)->clear_end_of_line_hook) (f, first_unused_hpos);
158 /* Output LEN glyphs starting at STRING at the nominal cursor position.
159 Advance the nominal cursor over the text. */
161 void
162 write_glyphs (struct frame *f, struct glyph *string, int len)
164 if (FRAME_TERMINAL (f)->write_glyphs_hook)
165 (*FRAME_TERMINAL (f)->write_glyphs_hook) (f, string, len);
168 /* Insert LEN glyphs from START at the nominal cursor position.
170 If start is zero, insert blanks instead of a string at start */
172 void
173 insert_glyphs (struct frame *f, struct glyph *start, int len)
175 if (len <= 0)
176 return;
178 if (FRAME_TERMINAL (f)->insert_glyphs_hook)
179 (*FRAME_TERMINAL (f)->insert_glyphs_hook) (f, start, len);
182 /* Delete N glyphs at the nominal cursor position. */
184 void
185 delete_glyphs (struct frame *f, int n)
187 if (FRAME_TERMINAL (f)->delete_glyphs_hook)
188 (*FRAME_TERMINAL (f)->delete_glyphs_hook) (f, n);
191 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
193 void
194 ins_del_lines (struct frame *f, int vpos, int n)
196 if (FRAME_TERMINAL (f)->ins_del_lines_hook)
197 (*FRAME_TERMINAL (f)->ins_del_lines_hook) (f, vpos, n);
200 /* Return the terminal object specified by TERMINAL. TERMINAL may
201 be a terminal object, a frame, or nil for the terminal device of
202 the current frame. If TERMINAL is neither from the above or the
203 resulting terminal object is deleted, return NULL. */
205 static struct terminal *
206 decode_terminal (Lisp_Object terminal)
208 struct terminal *t;
210 if (NILP (terminal))
211 terminal = selected_frame;
212 t = (TERMINALP (terminal)
213 ? XTERMINAL (terminal)
214 : FRAMEP (terminal) ? FRAME_TERMINAL (XFRAME (terminal)) : NULL);
215 return t && t->name ? t : NULL;
218 /* Like above, but throw an error if TERMINAL is not valid or deleted. */
220 struct terminal *
221 decode_live_terminal (Lisp_Object terminal)
223 struct terminal *t = decode_terminal (terminal);
225 if (!t)
226 wrong_type_argument (Qterminal_live_p, terminal);
227 return t;
230 /* Like decode_terminal, but ensure that the resulting terminal object refers
231 to a text-based terminal device. */
233 struct terminal *
234 decode_tty_terminal (Lisp_Object terminal)
236 struct terminal *t = decode_live_terminal (terminal);
238 return (t->type == output_termcap || t->type == output_msdos_raw) ? t : NULL;
241 /* Return an active (not suspended) text-based terminal device that uses
242 the tty device with the given NAME, or NULL if the named terminal device
243 is not opened. */
245 struct terminal *
246 get_named_terminal (const char *name)
248 struct terminal *t;
250 eassert (name);
252 for (t = terminal_list; t; t = t->next_terminal)
254 if ((t->type == output_termcap || t->type == output_msdos_raw)
255 && !strcmp (t->display_info.tty->name, name)
256 && TERMINAL_ACTIVE_P (t))
257 return t;
259 return NULL;
262 /* Allocate basically initialized terminal. */
264 static struct terminal *
265 allocate_terminal (void)
267 return ALLOCATE_ZEROED_PSEUDOVECTOR
268 (struct terminal, next_terminal, PVEC_TERMINAL);
271 /* Create a new terminal object of TYPE and add it to the terminal list. RIF
272 may be NULL if this terminal type doesn't support window-based redisplay. */
274 struct terminal *
275 create_terminal (enum output_method type, struct redisplay_interface *rif)
277 struct terminal *terminal = allocate_terminal ();
278 Lisp_Object terminal_coding, keyboard_coding;
280 terminal->next_terminal = terminal_list;
281 terminal_list = terminal;
282 terminal->type = type;
283 terminal->rif = rif;
284 terminal->id = next_terminal_id++;
286 terminal->keyboard_coding = xmalloc (sizeof (struct coding_system));
287 terminal->terminal_coding = xmalloc (sizeof (struct coding_system));
289 /* If default coding systems for the terminal and the keyboard are
290 already defined, use them in preference to the defaults. This is
291 needed when Emacs runs in daemon mode. */
292 keyboard_coding =
293 find_symbol_value (intern ("default-keyboard-coding-system"));
294 if (NILP (keyboard_coding)
295 || EQ (keyboard_coding, Qunbound)
296 || NILP (Fcoding_system_p (keyboard_coding)))
297 keyboard_coding = Qno_conversion;
298 terminal_coding =
299 find_symbol_value (intern ("default-terminal-coding-system"));
300 if (NILP (terminal_coding)
301 || EQ (terminal_coding, Qunbound)
302 || NILP (Fcoding_system_p (terminal_coding)))
303 terminal_coding = Qundecided;
305 setup_coding_system (keyboard_coding, terminal->keyboard_coding);
306 setup_coding_system (terminal_coding, terminal->terminal_coding);
308 return terminal;
311 /* Low-level function to close all frames on a terminal, remove it
312 from the terminal list and free its memory. */
314 void
315 delete_terminal (struct terminal *terminal)
317 struct terminal **tp;
318 Lisp_Object tail, frame;
320 /* Protect against recursive calls. delete_frame calls the
321 delete_terminal_hook when we delete our last frame. */
322 if (!terminal->name)
323 return;
324 xfree (terminal->name);
325 terminal->name = NULL;
327 /* Check for live frames that are still on this terminal. */
328 FOR_EACH_FRAME (tail, frame)
330 struct frame *f = XFRAME (frame);
331 if (FRAME_LIVE_P (f) && f->terminal == terminal)
333 /* Pass Qnoelisp rather than Qt. */
334 delete_frame (frame, Qnoelisp);
338 for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
339 if (! *tp)
340 emacs_abort ();
341 *tp = terminal->next_terminal;
343 xfree (terminal->keyboard_coding);
344 terminal->keyboard_coding = NULL;
345 xfree (terminal->terminal_coding);
346 terminal->terminal_coding = NULL;
348 if (terminal->kboard && --terminal->kboard->reference_count == 0)
350 delete_kboard (terminal->kboard);
351 terminal->kboard = NULL;
355 DEFUN ("delete-terminal", Fdelete_terminal, Sdelete_terminal, 0, 2, 0,
356 doc: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
357 TERMINAL may be a terminal object, a frame, or nil (meaning the
358 selected frame's terminal).
360 Normally, you may not delete a display if all other displays are suspended,
361 but if the second argument FORCE is non-nil, you may do so. */)
362 (Lisp_Object terminal, Lisp_Object force)
364 struct terminal *t = decode_terminal (terminal);
366 if (!t)
367 return Qnil;
369 if (NILP (force))
371 struct terminal *p = terminal_list;
372 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
373 p = p->next_terminal;
375 if (!p)
376 error ("Attempt to delete the sole active display terminal");
379 if (NILP (Vrun_hooks))
381 else if (EQ (force, Qnoelisp))
382 pending_funcalls
383 = Fcons (list3 (Qrun_hook_with_args,
384 Qdelete_terminal_functions, terminal),
385 pending_funcalls);
386 else
387 safe_call2 (Qrun_hook_with_args, Qdelete_terminal_functions, terminal);
389 if (t->delete_terminal_hook)
390 (*t->delete_terminal_hook) (t);
391 else
392 delete_terminal (t);
394 return Qnil;
398 DEFUN ("frame-terminal", Fframe_terminal, Sframe_terminal, 0, 1, 0,
399 doc: /* Return the terminal that FRAME is displayed on.
400 If FRAME is nil, the selected frame is used.
402 The terminal device is represented by its integer identifier. */)
403 (Lisp_Object frame)
405 struct terminal *t = FRAME_TERMINAL (decode_live_frame (frame));
407 if (!t)
408 return Qnil;
409 else
411 Lisp_Object terminal;
412 XSETTERMINAL (terminal, t);
413 return terminal;
417 DEFUN ("terminal-live-p", Fterminal_live_p, Sterminal_live_p, 1, 1, 0,
418 doc: /* Return non-nil if OBJECT is a terminal which has not been deleted.
419 Value is nil if OBJECT is not a live display terminal.
420 If object is a live display terminal, the return value indicates what
421 sort of output terminal it uses. See the documentation of `framep' for
422 possible return values. */)
423 (Lisp_Object object)
425 struct terminal *t = decode_terminal (object);
427 if (!t)
428 return Qnil;
430 switch (t->type)
432 case output_initial: /* The initial frame is like a termcap frame. */
433 case output_termcap:
434 return Qt;
435 case output_x_window:
436 return Qx;
437 case output_w32:
438 return Qw32;
439 case output_msdos_raw:
440 return Qpc;
441 case output_ns:
442 return Qns;
443 default:
444 emacs_abort ();
448 DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
449 doc: /* Return a list of all terminal devices. */)
450 (void)
452 Lisp_Object terminal, terminals = Qnil;
453 struct terminal *t;
455 for (t = terminal_list; t; t = t->next_terminal)
457 XSETTERMINAL (terminal, t);
458 terminals = Fcons (terminal, terminals);
461 return terminals;
464 DEFUN ("terminal-name", Fterminal_name, Sterminal_name, 0, 1, 0,
465 doc: /* Return the name of the terminal device TERMINAL.
466 It is not guaranteed that the returned value is unique among opened devices.
468 TERMINAL may be a terminal object, a frame, or nil (meaning the
469 selected frame's terminal). */)
470 (Lisp_Object terminal)
472 struct terminal *t = decode_live_terminal (terminal);
474 return t->name ? build_string (t->name) : Qnil;
479 /* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
480 Return the previous value. */
482 static Lisp_Object
483 store_terminal_param (struct terminal *t, Lisp_Object parameter, Lisp_Object value)
485 Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
486 if (EQ (old_alist_elt, Qnil))
488 tset_param_alist (t, Fcons (Fcons (parameter, value), t->param_alist));
489 return Qnil;
491 else
493 Lisp_Object result = Fcdr (old_alist_elt);
494 Fsetcdr (old_alist_elt, value);
495 return result;
500 DEFUN ("terminal-parameters", Fterminal_parameters, Sterminal_parameters, 0, 1, 0,
501 doc: /* Return the parameter-alist of terminal TERMINAL.
502 The value is a list of elements of the form (PARM . VALUE), where PARM
503 is a symbol.
505 TERMINAL can be a terminal object, a frame, or nil (meaning the
506 selected frame's terminal). */)
507 (Lisp_Object terminal)
509 return Fcopy_alist (decode_live_terminal (terminal)->param_alist);
512 DEFUN ("terminal-parameter", Fterminal_parameter, Sterminal_parameter, 2, 2, 0,
513 doc: /* Return TERMINAL's value for parameter PARAMETER.
514 TERMINAL can be a terminal object, a frame, or nil (meaning the
515 selected frame's terminal). */)
516 (Lisp_Object terminal, Lisp_Object parameter)
518 CHECK_SYMBOL (parameter);
519 return Fcdr (Fassq (parameter, decode_live_terminal (terminal)->param_alist));
522 DEFUN ("set-terminal-parameter", Fset_terminal_parameter,
523 Sset_terminal_parameter, 3, 3, 0,
524 doc: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
525 Return the previous value of PARAMETER.
527 TERMINAL can be a terminal object, a frame or nil (meaning the
528 selected frame's terminal). */)
529 (Lisp_Object terminal, Lisp_Object parameter, Lisp_Object value)
531 return store_terminal_param (decode_live_terminal (terminal), parameter, value);
534 #if HAVE_STRUCT_UNIPAIR_UNICODE
536 /* Compute the glyph code table for T. */
538 static void
539 calculate_glyph_code_table (struct terminal *t)
541 Lisp_Object glyphtab = Qt;
542 enum { initial_unipairs = 1000 };
543 int entry_ct = initial_unipairs;
544 struct unipair unipair_buffer[initial_unipairs];
545 struct unipair *entries = unipair_buffer;
546 struct unipair *alloced = 0;
548 while (true)
550 int fd = fileno (t->display_info.tty->output);
551 struct unimapdesc unimapdesc = { entry_ct, entries };
552 if (ioctl (fd, GIO_UNIMAP, &unimapdesc) == 0)
554 glyphtab = Fmake_char_table (Qnil, make_number (-1));
555 for (int i = 0; i < unimapdesc.entry_ct; i++)
556 char_table_set (glyphtab, entries[i].unicode,
557 make_number (entries[i].fontpos));
558 break;
560 if (errno != ENOMEM)
561 break;
562 entry_ct = unimapdesc.entry_ct;
563 entries = alloced = xrealloc (alloced, entry_ct * sizeof *alloced);
566 xfree (alloced);
567 t->glyph_code_table = glyphtab;
569 #endif
571 /* Return the glyph code in T of character CH, or -1 if CH does not
572 have a font position in T, or nil if T does not report glyph codes. */
574 Lisp_Object
575 terminal_glyph_code (struct terminal *t, int ch)
577 #if HAVE_STRUCT_UNIPAIR_UNICODE
578 if (t->type == output_termcap)
580 /* As a hack, recompute the table when CH is the maximum
581 character. */
582 if (NILP (t->glyph_code_table) || ch == MAX_CHAR)
583 calculate_glyph_code_table (t);
585 if (! EQ (t->glyph_code_table, Qt))
586 return char_table_ref (t->glyph_code_table, ch);
588 #endif
590 return Qnil;
593 /* Initial frame has no device-dependent output data, but has
594 face cache which should be freed when the frame is deleted. */
596 static void
597 initial_free_frame_resources (struct frame *f)
599 eassert (FRAME_INITIAL_P (f));
600 free_frame_faces (f);
603 /* Create the bootstrap display terminal for the initial frame.
604 Returns a terminal of type output_initial. */
606 struct terminal *
607 init_initial_terminal (void)
609 if (initialized || terminal_list || tty_list)
610 emacs_abort ();
612 initial_terminal = create_terminal (output_initial, NULL);
613 initial_terminal->name = xstrdup ("initial_terminal");
614 initial_terminal->kboard = initial_kboard;
615 initial_terminal->delete_terminal_hook = &delete_initial_terminal;
616 initial_terminal->delete_frame_hook = &initial_free_frame_resources;
617 /* Other hooks are NULL by default. */
619 return initial_terminal;
622 /* Deletes the bootstrap terminal device.
623 Called through delete_terminal_hook. */
625 static void
626 delete_initial_terminal (struct terminal *terminal)
628 if (terminal != initial_terminal)
629 emacs_abort ();
631 delete_terminal (terminal);
632 initial_terminal = NULL;
635 void
636 syms_of_terminal (void)
639 DEFVAR_LISP ("ring-bell-function", Vring_bell_function,
640 doc: /* Non-nil means call this function to ring the bell.
641 The function should accept no arguments. */);
642 Vring_bell_function = Qnil;
644 DEFVAR_LISP ("delete-terminal-functions", Vdelete_terminal_functions,
645 doc: /* Special hook run when a terminal is deleted.
646 Each function is called with argument, the terminal.
647 This may be called just before actually deleting the terminal,
648 or some time later. */);
649 Vdelete_terminal_functions = Qnil;
651 DEFSYM (Qterminal_live_p, "terminal-live-p");
652 DEFSYM (Qdelete_terminal_functions, "delete-terminal-functions");
653 DEFSYM (Qrun_hook_with_args, "run-hook-with-args");
655 defsubr (&Sdelete_terminal);
656 defsubr (&Sframe_terminal);
657 defsubr (&Sterminal_live_p);
658 defsubr (&Sterminal_list);
659 defsubr (&Sterminal_name);
660 defsubr (&Sterminal_parameters);
661 defsubr (&Sterminal_parameter);
662 defsubr (&Sset_terminal_parameter);
664 Fprovide (intern_c_string ("multi-tty"), Qnil);