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/>. */
24 #include "character.h"
27 #include "termhooks.h"
30 #if HAVE_STRUCT_UNIPAIR_UNICODE
32 # include <linux/kd.h>
33 # include <sys/ioctl.h>
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. */
49 tset_param_alist (struct terminal
*t
, Lisp_Object val
)
57 ring_bell (struct frame
*f
)
59 if (!NILP (Vring_bell_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
;
76 Vring_bell_function
= function
;
78 else if (FRAME_TERMINAL (f
)->ring_bell_hook
)
79 (*FRAME_TERMINAL (f
)->ring_bell_hook
) (f
);
83 update_begin (struct frame
*f
)
85 if (FRAME_TERMINAL (f
)->update_begin_hook
)
86 (*FRAME_TERMINAL (f
)->update_begin_hook
) (f
);
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. */
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. */
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. */
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. */
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. */
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. */
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. */
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 */
173 insert_glyphs (struct frame
*f
, struct glyph
*start
, int len
)
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. */
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. */
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
)
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. */
221 decode_live_terminal (Lisp_Object terminal
)
223 struct terminal
*t
= decode_terminal (terminal
);
226 wrong_type_argument (Qterminal_live_p
, terminal
);
230 /* Like decode_terminal, but ensure that the resulting terminal object refers
231 to a text-based terminal device. */
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
246 get_named_terminal (const char *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
))
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. */
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
;
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. */
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
;
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
);
311 /* Low-level function to close all frames on a terminal, remove it
312 from the terminal list and free its memory. */
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. */
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
)
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
);
371 struct terminal
*p
= terminal_list
;
372 while (p
&& (p
== t
|| !TERMINAL_ACTIVE_P (p
)))
373 p
= p
->next_terminal
;
376 error ("Attempt to delete the sole active display terminal");
379 if (NILP (Vrun_hooks
))
381 else if (EQ (force
, Qnoelisp
))
383 = Fcons (list3 (Qrun_hook_with_args
,
384 Qdelete_terminal_functions
, terminal
),
387 safe_call2 (Qrun_hook_with_args
, Qdelete_terminal_functions
, terminal
);
389 if (t
->delete_terminal_hook
)
390 (*t
->delete_terminal_hook
) (t
);
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. */)
405 struct terminal
*t
= FRAME_TERMINAL (decode_live_frame (frame
));
411 Lisp_Object terminal
;
412 XSETTERMINAL (terminal
, t
);
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. */)
425 struct terminal
*t
= decode_terminal (object
);
432 case output_initial
: /* The initial frame is like a termcap frame. */
435 case output_x_window
:
439 case output_msdos_raw
:
448 DEFUN ("terminal-list", Fterminal_list
, Sterminal_list
, 0, 0, 0,
449 doc
: /* Return a list of all terminal devices. */)
452 Lisp_Object terminal
, terminals
= Qnil
;
455 for (t
= terminal_list
; t
; t
= t
->next_terminal
)
457 XSETTERMINAL (terminal
, t
);
458 terminals
= Fcons (terminal
, 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. */
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
));
493 Lisp_Object result
= Fcdr (old_alist_elt
);
494 Fsetcdr (old_alist_elt
, value
);
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
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. */
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;
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
));
562 entry_ct
= unimapdesc
.entry_ct
;
563 entries
= alloced
= xrealloc (alloced
, entry_ct
* sizeof *alloced
);
567 t
->glyph_code_table
= glyphtab
;
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. */
575 terminal_glyph_code (struct terminal
*t
, int ch
)
577 #if HAVE_STRUCT_UNIPAIR_UNICODE
578 /* Heuristically assume that a terminal supporting glyph codes is in
579 UTF-8 mode if and only if its coding system is UTF-8 (Bug#26396). */
580 if (t
->type
== output_termcap
581 && t
->terminal_coding
->encoder
== encode_coding_utf_8
)
583 /* As a hack, recompute the table when CH is the maximum
585 if (NILP (t
->glyph_code_table
) || ch
== MAX_CHAR
)
586 calculate_glyph_code_table (t
);
588 if (! EQ (t
->glyph_code_table
, Qt
))
589 return char_table_ref (t
->glyph_code_table
, ch
);
596 /* Initial frame has no device-dependent output data, but has
597 face cache which should be freed when the frame is deleted. */
600 initial_free_frame_resources (struct frame
*f
)
602 eassert (FRAME_INITIAL_P (f
));
603 free_frame_faces (f
);
606 /* Create the bootstrap display terminal for the initial frame.
607 Returns a terminal of type output_initial. */
610 init_initial_terminal (void)
612 if (initialized
|| terminal_list
|| tty_list
)
615 initial_terminal
= create_terminal (output_initial
, NULL
);
616 initial_terminal
->name
= xstrdup ("initial_terminal");
617 initial_terminal
->kboard
= initial_kboard
;
618 initial_terminal
->delete_terminal_hook
= &delete_initial_terminal
;
619 initial_terminal
->delete_frame_hook
= &initial_free_frame_resources
;
620 /* Other hooks are NULL by default. */
622 return initial_terminal
;
625 /* Deletes the bootstrap terminal device.
626 Called through delete_terminal_hook. */
629 delete_initial_terminal (struct terminal
*terminal
)
631 if (terminal
!= initial_terminal
)
634 delete_terminal (terminal
);
635 initial_terminal
= NULL
;
639 syms_of_terminal (void)
642 DEFVAR_LISP ("ring-bell-function", Vring_bell_function
,
643 doc
: /* Non-nil means call this function to ring the bell.
644 The function should accept no arguments. */);
645 Vring_bell_function
= Qnil
;
647 DEFVAR_LISP ("delete-terminal-functions", Vdelete_terminal_functions
,
648 doc
: /* Special hook run when a terminal is deleted.
649 Each function is called with argument, the terminal.
650 This may be called just before actually deleting the terminal,
651 or some time later. */);
652 Vdelete_terminal_functions
= Qnil
;
654 DEFSYM (Qterminal_live_p
, "terminal-live-p");
655 DEFSYM (Qdelete_terminal_functions
, "delete-terminal-functions");
656 DEFSYM (Qrun_hook_with_args
, "run-hook-with-args");
658 defsubr (&Sdelete_terminal
);
659 defsubr (&Sframe_terminal
);
660 defsubr (&Sterminal_live_p
);
661 defsubr (&Sterminal_list
);
662 defsubr (&Sterminal_name
);
663 defsubr (&Sterminal_parameters
);
664 defsubr (&Sterminal_parameter
);
665 defsubr (&Sset_terminal_parameter
);
667 Fprovide (intern_c_string ("multi-tty"), Qnil
);