1 /* Functions related to terminal devices.
2 Copyright (C) 2005-2015 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
9 (at 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/>. */
26 #include "termhooks.h"
31 #if HAVE_STRUCT_UNIPAIR_UNICODE
33 # include <linux/kd.h>
34 # include <sys/ioctl.h>
37 /* Chain of all terminals currently in use. */
38 struct terminal
*terminal_list
;
40 /* The first unallocated terminal id. */
41 static int next_terminal_id
;
43 /* The initial terminal device, created by initial_term_init. */
44 struct terminal
*initial_terminal
;
46 static void delete_initial_terminal (struct terminal
*);
48 /* This setter is used only in this file, so it can be private. */
50 tset_param_alist (struct terminal
*t
, Lisp_Object val
)
58 ring_bell (struct frame
*f
)
60 if (!NILP (Vring_bell_function
))
64 /* Temporarily set the global variable to nil
65 so that if we get an error, it stays nil
66 and we don't call it over and over.
68 We don't specbind it, because that would carefully
69 restore the bad value if there's an error
70 and make the loop of errors happen anyway. */
72 function
= Vring_bell_function
;
73 Vring_bell_function
= Qnil
;
77 Vring_bell_function
= function
;
79 else if (FRAME_TERMINAL (f
)->ring_bell_hook
)
80 (*FRAME_TERMINAL (f
)->ring_bell_hook
) (f
);
84 update_begin (struct frame
*f
)
86 if (FRAME_TERMINAL (f
)->update_begin_hook
)
87 (*FRAME_TERMINAL (f
)->update_begin_hook
) (f
);
91 update_end (struct frame
*f
)
93 if (FRAME_TERMINAL (f
)->update_end_hook
)
94 (*FRAME_TERMINAL (f
)->update_end_hook
) (f
);
97 /* Specify how many text lines, from the top of the window,
98 should be affected by insert-lines and delete-lines operations.
99 This, and those operations, are used only within an update
100 that is bounded by calls to update_begin and update_end. */
103 set_terminal_window (struct frame
*f
, int size
)
105 if (FRAME_TERMINAL (f
)->set_terminal_window_hook
)
106 (*FRAME_TERMINAL (f
)->set_terminal_window_hook
) (f
, size
);
109 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
110 frame-relative coordinates. */
113 cursor_to (struct frame
*f
, int vpos
, int hpos
)
115 if (FRAME_TERMINAL (f
)->cursor_to_hook
)
116 (*FRAME_TERMINAL (f
)->cursor_to_hook
) (f
, vpos
, hpos
);
119 /* Similar but don't take any account of the wasted characters. */
122 raw_cursor_to (struct frame
*f
, int row
, int col
)
124 if (FRAME_TERMINAL (f
)->raw_cursor_to_hook
)
125 (*FRAME_TERMINAL (f
)->raw_cursor_to_hook
) (f
, row
, col
);
128 /* Erase operations. */
130 /* Clear from cursor to end of frame. */
132 clear_to_end (struct frame
*f
)
134 if (FRAME_TERMINAL (f
)->clear_to_end_hook
)
135 (*FRAME_TERMINAL (f
)->clear_to_end_hook
) (f
);
138 /* Clear entire frame. */
141 clear_frame (struct frame
*f
)
143 if (FRAME_TERMINAL (f
)->clear_frame_hook
)
144 (*FRAME_TERMINAL (f
)->clear_frame_hook
) (f
);
147 /* Clear from cursor to end of line.
148 Assume that the line is already clear starting at column first_unused_hpos.
150 Note that the cursor may be moved, on terminals lacking a `ce' string. */
153 clear_end_of_line (struct frame
*f
, int first_unused_hpos
)
155 if (FRAME_TERMINAL (f
)->clear_end_of_line_hook
)
156 (*FRAME_TERMINAL (f
)->clear_end_of_line_hook
) (f
, first_unused_hpos
);
159 /* Output LEN glyphs starting at STRING at the nominal cursor position.
160 Advance the nominal cursor over the text. */
163 write_glyphs (struct frame
*f
, struct glyph
*string
, int len
)
165 if (FRAME_TERMINAL (f
)->write_glyphs_hook
)
166 (*FRAME_TERMINAL (f
)->write_glyphs_hook
) (f
, string
, len
);
169 /* Insert LEN glyphs from START at the nominal cursor position.
171 If start is zero, insert blanks instead of a string at start */
174 insert_glyphs (struct frame
*f
, struct glyph
*start
, int len
)
179 if (FRAME_TERMINAL (f
)->insert_glyphs_hook
)
180 (*FRAME_TERMINAL (f
)->insert_glyphs_hook
) (f
, start
, len
);
183 /* Delete N glyphs at the nominal cursor position. */
186 delete_glyphs (struct frame
*f
, int n
)
188 if (FRAME_TERMINAL (f
)->delete_glyphs_hook
)
189 (*FRAME_TERMINAL (f
)->delete_glyphs_hook
) (f
, n
);
192 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
195 ins_del_lines (struct frame
*f
, int vpos
, int n
)
197 if (FRAME_TERMINAL (f
)->ins_del_lines_hook
)
198 (*FRAME_TERMINAL (f
)->ins_del_lines_hook
) (f
, vpos
, n
);
201 /* Return the terminal object specified by TERMINAL. TERMINAL may
202 be a terminal object, a frame, or nil for the terminal device of
203 the current frame. If TERMINAL is neither from the above or the
204 resulting terminal object is deleted, return NULL. */
206 static struct terminal
*
207 decode_terminal (Lisp_Object terminal
)
212 terminal
= selected_frame
;
213 t
= (TERMINALP (terminal
)
214 ? XTERMINAL (terminal
)
215 : FRAMEP (terminal
) ? FRAME_TERMINAL (XFRAME (terminal
)) : NULL
);
216 return t
&& t
->name
? t
: NULL
;
219 /* Like above, but throw an error if TERMINAL is not valid or deleted. */
222 decode_live_terminal (Lisp_Object terminal
)
224 struct terminal
*t
= decode_terminal (terminal
);
227 wrong_type_argument (Qterminal_live_p
, terminal
);
231 /* Like decode_terminal, but ensure that the resulting terminal object refers
232 to a text-based terminal device. */
235 decode_tty_terminal (Lisp_Object terminal
)
237 struct terminal
*t
= decode_live_terminal (terminal
);
239 return (t
->type
== output_termcap
|| t
->type
== output_msdos_raw
) ? t
: NULL
;
242 /* Return an active (not suspended) text-based terminal device that uses
243 the tty device with the given NAME, or NULL if the named terminal device
247 get_named_terminal (const char *name
)
253 for (t
= terminal_list
; t
; t
= t
->next_terminal
)
255 if ((t
->type
== output_termcap
|| t
->type
== output_msdos_raw
)
256 && !strcmp (t
->display_info
.tty
->name
, name
)
257 && TERMINAL_ACTIVE_P (t
))
263 /* Allocate basically initialized terminal. */
265 static struct terminal
*
266 allocate_terminal (void)
268 return ALLOCATE_ZEROED_PSEUDOVECTOR
269 (struct terminal
, next_terminal
, PVEC_TERMINAL
);
272 /* Create a new terminal object of TYPE and add it to the terminal list. RIF
273 may be NULL if this terminal type doesn't support window-based redisplay. */
276 create_terminal (enum output_method type
, struct redisplay_interface
*rif
)
278 struct terminal
*terminal
= allocate_terminal ();
279 Lisp_Object terminal_coding
, keyboard_coding
;
281 terminal
->next_terminal
= terminal_list
;
282 terminal_list
= terminal
;
283 terminal
->type
= type
;
285 terminal
->id
= next_terminal_id
++;
287 terminal
->keyboard_coding
= xmalloc (sizeof (struct coding_system
));
288 terminal
->terminal_coding
= xmalloc (sizeof (struct coding_system
));
290 /* If default coding systems for the terminal and the keyboard are
291 already defined, use them in preference to the defaults. This is
292 needed when Emacs runs in daemon mode. */
294 find_symbol_value (intern ("default-keyboard-coding-system"));
295 if (NILP (keyboard_coding
)
296 || EQ (keyboard_coding
, Qunbound
)
297 || NILP (Fcoding_system_p (keyboard_coding
)))
298 keyboard_coding
= Qno_conversion
;
300 find_symbol_value (intern ("default-terminal-coding-system"));
301 if (NILP (terminal_coding
)
302 || EQ (terminal_coding
, Qunbound
)
303 || NILP (Fcoding_system_p (terminal_coding
)))
304 terminal_coding
= Qundecided
;
306 setup_coding_system (keyboard_coding
, terminal
->keyboard_coding
);
307 setup_coding_system (terminal_coding
, terminal
->terminal_coding
);
312 /* Low-level function to close all frames on a terminal, remove it
313 from the terminal list and free its memory. */
316 delete_terminal (struct terminal
*terminal
)
318 struct terminal
**tp
;
319 Lisp_Object tail
, frame
;
321 /* Protect against recursive calls. delete_frame calls the
322 delete_terminal_hook when we delete our last frame. */
325 xfree (terminal
->name
);
326 terminal
->name
= NULL
;
328 /* Check for live frames that are still on this terminal. */
329 FOR_EACH_FRAME (tail
, frame
)
331 struct frame
*f
= XFRAME (frame
);
332 if (FRAME_LIVE_P (f
) && f
->terminal
== terminal
)
334 /* Pass Qnoelisp rather than Qt. */
335 delete_frame (frame
, Qnoelisp
);
339 for (tp
= &terminal_list
; *tp
!= terminal
; tp
= &(*tp
)->next_terminal
)
342 *tp
= terminal
->next_terminal
;
344 xfree (terminal
->keyboard_coding
);
345 terminal
->keyboard_coding
= NULL
;
346 xfree (terminal
->terminal_coding
);
347 terminal
->terminal_coding
= NULL
;
349 if (terminal
->kboard
&& --terminal
->kboard
->reference_count
== 0)
351 delete_kboard (terminal
->kboard
);
352 terminal
->kboard
= NULL
;
356 DEFUN ("delete-terminal", Fdelete_terminal
, Sdelete_terminal
, 0, 2, 0,
357 doc
: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
358 TERMINAL may be a terminal object, a frame, or nil (meaning the
359 selected frame's terminal).
361 Normally, you may not delete a display if all other displays are suspended,
362 but if the second argument FORCE is non-nil, you may do so. */)
363 (Lisp_Object terminal
, Lisp_Object force
)
365 struct terminal
*t
= decode_terminal (terminal
);
372 struct terminal
*p
= terminal_list
;
373 while (p
&& (p
== t
|| !TERMINAL_ACTIVE_P (p
)))
374 p
= p
->next_terminal
;
377 error ("Attempt to delete the sole active display terminal");
380 if (NILP (Vrun_hooks
))
382 else if (EQ (force
, Qnoelisp
))
384 = Fcons (list3 (Qrun_hook_with_args
,
385 Qdelete_terminal_functions
, terminal
),
388 safe_call2 (Qrun_hook_with_args
, Qdelete_terminal_functions
, terminal
);
390 if (t
->delete_terminal_hook
)
391 (*t
->delete_terminal_hook
) (t
);
399 DEFUN ("frame-terminal", Fframe_terminal
, Sframe_terminal
, 0, 1, 0,
400 doc
: /* Return the terminal that FRAME is displayed on.
401 If FRAME is nil, the selected frame is used.
403 The terminal device is represented by its integer identifier. */)
406 struct terminal
*t
= FRAME_TERMINAL (decode_live_frame (frame
));
412 Lisp_Object terminal
;
413 XSETTERMINAL (terminal
, t
);
418 DEFUN ("terminal-live-p", Fterminal_live_p
, Sterminal_live_p
, 1, 1, 0,
419 doc
: /* Return non-nil if OBJECT is a terminal which has not been deleted.
420 Value is nil if OBJECT is not a live display terminal.
421 If object is a live display terminal, the return value indicates what
422 sort of output terminal it uses. See the documentation of `framep' for
423 possible return values. */)
426 struct terminal
*t
= decode_terminal (object
);
433 case output_initial
: /* The initial frame is like a termcap frame. */
436 case output_x_window
:
440 case output_msdos_raw
:
449 DEFUN ("terminal-list", Fterminal_list
, Sterminal_list
, 0, 0, 0,
450 doc
: /* Return a list of all terminal devices. */)
453 Lisp_Object terminal
, terminals
= Qnil
;
456 for (t
= terminal_list
; t
; t
= t
->next_terminal
)
458 XSETTERMINAL (terminal
, t
);
459 terminals
= Fcons (terminal
, terminals
);
465 DEFUN ("terminal-name", Fterminal_name
, Sterminal_name
, 0, 1, 0,
466 doc
: /* Return the name of the terminal device TERMINAL.
467 It is not guaranteed that the returned value is unique among opened devices.
469 TERMINAL may be a terminal object, a frame, or nil (meaning the
470 selected frame's terminal). */)
471 (Lisp_Object terminal
)
473 struct terminal
*t
= decode_live_terminal (terminal
);
475 return t
->name
? build_string (t
->name
) : Qnil
;
480 /* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
481 Return the previous value. */
484 store_terminal_param (struct terminal
*t
, Lisp_Object parameter
, Lisp_Object value
)
486 Lisp_Object old_alist_elt
= Fassq (parameter
, t
->param_alist
);
487 if (EQ (old_alist_elt
, Qnil
))
489 tset_param_alist (t
, Fcons (Fcons (parameter
, value
), t
->param_alist
));
494 Lisp_Object result
= Fcdr (old_alist_elt
);
495 Fsetcdr (old_alist_elt
, value
);
501 DEFUN ("terminal-parameters", Fterminal_parameters
, Sterminal_parameters
, 0, 1, 0,
502 doc
: /* Return the parameter-alist of terminal TERMINAL.
503 The value is a list of elements of the form (PARM . VALUE), where PARM
506 TERMINAL can be a terminal object, a frame, or nil (meaning the
507 selected frame's terminal). */)
508 (Lisp_Object terminal
)
510 return Fcopy_alist (decode_live_terminal (terminal
)->param_alist
);
513 DEFUN ("terminal-parameter", Fterminal_parameter
, Sterminal_parameter
, 2, 2, 0,
514 doc
: /* Return TERMINAL's value for parameter PARAMETER.
515 TERMINAL can be a terminal object, a frame, or nil (meaning the
516 selected frame's terminal). */)
517 (Lisp_Object terminal
, Lisp_Object parameter
)
519 CHECK_SYMBOL (parameter
);
520 return Fcdr (Fassq (parameter
, decode_live_terminal (terminal
)->param_alist
));
523 DEFUN ("set-terminal-parameter", Fset_terminal_parameter
,
524 Sset_terminal_parameter
, 3, 3, 0,
525 doc
: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
526 Return the previous value of PARAMETER.
528 TERMINAL can be a terminal object, a frame or nil (meaning the
529 selected frame's terminal). */)
530 (Lisp_Object terminal
, Lisp_Object parameter
, Lisp_Object value
)
532 return store_terminal_param (decode_live_terminal (terminal
), parameter
, value
);
535 #if HAVE_STRUCT_UNIPAIR_UNICODE
537 /* Compute the glyph code table for T. */
540 calculate_glyph_code_table (struct terminal
*t
)
542 Lisp_Object glyphtab
= Qt
;
543 enum { initial_unipairs
= 1000 };
544 int entry_ct
= initial_unipairs
;
545 struct unipair unipair_buffer
[initial_unipairs
];
546 struct unipair
*entries
= unipair_buffer
;
547 struct unipair
*alloced
= 0;
551 int fd
= fileno (t
->display_info
.tty
->output
);
552 struct unimapdesc unimapdesc
= { entry_ct
, entries
};
553 if (ioctl (fd
, GIO_UNIMAP
, &unimapdesc
) == 0)
555 glyphtab
= Fmake_char_table (Qnil
, make_number (-1));
556 for (int i
= 0; i
< unimapdesc
.entry_ct
; i
++)
557 char_table_set (glyphtab
, entries
[i
].unicode
,
558 make_number (entries
[i
].fontpos
));
563 entry_ct
= unimapdesc
.entry_ct
;
564 entries
= alloced
= xrealloc (alloced
, entry_ct
* sizeof *alloced
);
568 t
->glyph_code_table
= glyphtab
;
572 /* Return the glyph code in T of character CH, or -1 if CH does not
573 have a font position in T, or nil if T does not report glyph codes. */
576 terminal_glyph_code (struct terminal
*t
, int ch
)
578 #if HAVE_STRUCT_UNIPAIR_UNICODE
579 if (t
->type
== output_termcap
)
581 /* As a hack, recompute the table when CH is the maximum
583 if (NILP (t
->glyph_code_table
) || ch
== MAX_CHAR
)
584 calculate_glyph_code_table (t
);
586 if (! EQ (t
->glyph_code_table
, Qt
))
587 return char_table_ref (t
->glyph_code_table
, ch
);
594 /* Initial frame has no device-dependent output data, but has
595 face cache which should be freed when the frame is deleted. */
598 initial_free_frame_resources (struct frame
*f
)
600 eassert (FRAME_INITIAL_P (f
));
601 free_frame_faces (f
);
604 /* Create the bootstrap display terminal for the initial frame.
605 Returns a terminal of type output_initial. */
608 init_initial_terminal (void)
610 if (initialized
|| terminal_list
|| tty_list
)
613 initial_terminal
= create_terminal (output_initial
, NULL
);
614 initial_terminal
->name
= xstrdup ("initial_terminal");
615 initial_terminal
->kboard
= initial_kboard
;
616 initial_terminal
->delete_terminal_hook
= &delete_initial_terminal
;
617 initial_terminal
->delete_frame_hook
= &initial_free_frame_resources
;
618 /* Other hooks are NULL by default. */
620 return initial_terminal
;
623 /* Deletes the bootstrap terminal device.
624 Called through delete_terminal_hook. */
627 delete_initial_terminal (struct terminal
*terminal
)
629 if (terminal
!= initial_terminal
)
632 delete_terminal (terminal
);
633 initial_terminal
= NULL
;
637 syms_of_terminal (void)
640 DEFVAR_LISP ("ring-bell-function", Vring_bell_function
,
641 doc
: /* Non-nil means call this function to ring the bell.
642 The function should accept no arguments. */);
643 Vring_bell_function
= Qnil
;
645 DEFVAR_LISP ("delete-terminal-functions", Vdelete_terminal_functions
,
646 doc
: /* Special hook run when a terminal is deleted.
647 Each function is called with argument, the terminal.
648 This may be called just before actually deleting the terminal,
649 or some time later. */);
650 Vdelete_terminal_functions
= Qnil
;
652 DEFSYM (Qterminal_live_p
, "terminal-live-p");
653 DEFSYM (Qdelete_terminal_functions
, "delete-terminal-functions");
654 DEFSYM (Qrun_hook_with_args
, "run-hook-with-args");
656 defsubr (&Sdelete_terminal
);
657 defsubr (&Sframe_terminal
);
658 defsubr (&Sterminal_live_p
);
659 defsubr (&Sterminal_list
);
660 defsubr (&Sterminal_name
);
661 defsubr (&Sterminal_parameters
);
662 defsubr (&Sterminal_parameter
);
663 defsubr (&Sset_terminal_parameter
);
665 Fprovide (intern_c_string ("multi-tty"), Qnil
);