Port recent Linux console changes to RHEL 6
[emacs.git] / src / terminal.c
blobd7c16d9d036afff118c8a383190162c9e620ffb0
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/>. */
19 #include <config.h>
21 #include <stdio.h>
23 #include "lisp.h"
24 #include "frame.h"
25 #include "termchar.h"
26 #include "termhooks.h"
27 #include "charset.h"
28 #include "coding.h"
29 #include "keyboard.h"
31 #ifdef HAVE_LINUX_KD_H
32 # include <errno.h>
33 # include <linux/kd.h>
34 # include <sys/ioctl.h>
35 #endif
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. */
49 static void
50 tset_param_alist (struct terminal *t, Lisp_Object val)
52 t->param_alist = val;
57 void
58 ring_bell (struct frame *f)
60 if (!NILP (Vring_bell_function))
62 Lisp_Object 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;
75 call0 (function);
77 Vring_bell_function = function;
79 else if (FRAME_TERMINAL (f)->ring_bell_hook)
80 (*FRAME_TERMINAL (f)->ring_bell_hook) (f);
83 void
84 update_begin (struct frame *f)
86 if (FRAME_TERMINAL (f)->update_begin_hook)
87 (*FRAME_TERMINAL (f)->update_begin_hook) (f);
90 void
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. */
102 void
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. */
112 void
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. */
121 void
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. */
131 void
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. */
140 void
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. */
152 void
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. */
162 void
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 */
173 void
174 insert_glyphs (struct frame *f, struct glyph *start, int len)
176 if (len <= 0)
177 return;
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. */
185 void
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. */
194 void
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)
209 struct terminal *t;
211 if (NILP (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. */
221 struct terminal *
222 decode_live_terminal (Lisp_Object terminal)
224 struct terminal *t = decode_terminal (terminal);
226 if (!t)
227 wrong_type_argument (Qterminal_live_p, terminal);
228 return t;
231 /* Like decode_terminal, but ensure that the resulting terminal object refers
232 to a text-based terminal device. */
234 struct terminal *
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
244 is not opened. */
246 struct terminal *
247 get_named_terminal (const char *name)
249 struct terminal *t;
251 eassert (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))
258 return t;
260 return NULL;
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. */
275 struct terminal *
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;
284 terminal->rif = rif;
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. */
293 keyboard_coding =
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;
299 terminal_coding =
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);
309 return terminal;
312 /* Low-level function to close all frames on a terminal, remove it
313 from the terminal list and free its memory. */
315 void
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. */
323 if (!terminal->name)
324 return;
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)
340 if (! *tp)
341 emacs_abort ();
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);
367 if (!t)
368 return Qnil;
370 if (NILP (force))
372 struct terminal *p = terminal_list;
373 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
374 p = p->next_terminal;
376 if (!p)
377 error ("Attempt to delete the sole active display terminal");
380 if (NILP (Vrun_hooks))
382 else if (EQ (force, Qnoelisp))
383 pending_funcalls
384 = Fcons (list3 (Qrun_hook_with_args,
385 Qdelete_terminal_functions, terminal),
386 pending_funcalls);
387 else
388 safe_call2 (Qrun_hook_with_args, Qdelete_terminal_functions, terminal);
390 if (t->delete_terminal_hook)
391 (*t->delete_terminal_hook) (t);
392 else
393 delete_terminal (t);
395 return Qnil;
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. */)
404 (Lisp_Object frame)
406 struct terminal *t = FRAME_TERMINAL (decode_live_frame (frame));
408 if (!t)
409 return Qnil;
410 else
412 Lisp_Object terminal;
413 XSETTERMINAL (terminal, t);
414 return terminal;
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. */)
424 (Lisp_Object object)
426 struct terminal *t = decode_terminal (object);
428 if (!t)
429 return Qnil;
431 switch (t->type)
433 case output_initial: /* The initial frame is like a termcap frame. */
434 case output_termcap:
435 return Qt;
436 case output_x_window:
437 return Qx;
438 case output_w32:
439 return Qw32;
440 case output_msdos_raw:
441 return Qpc;
442 case output_ns:
443 return Qns;
444 default:
445 emacs_abort ();
449 DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
450 doc: /* Return a list of all terminal devices. */)
451 (void)
453 Lisp_Object terminal, terminals = Qnil;
454 struct terminal *t;
456 for (t = terminal_list; t; t = t->next_terminal)
458 XSETTERMINAL (terminal, t);
459 terminals = Fcons (terminal, terminals);
462 return 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. */
483 static Lisp_Object
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));
490 return Qnil;
492 else
494 Lisp_Object result = Fcdr (old_alist_elt);
495 Fsetcdr (old_alist_elt, value);
496 return result;
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
504 is a symbol.
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_LINUX_KD_H
537 /* Compute the glyph code table for T. */
539 static void
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;
549 while (true)
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));
559 break;
561 if (errno != ENOMEM)
562 break;
563 entry_ct = unimapdesc.entry_ct;
564 entries = alloced = xrealloc (alloced, entry_ct * sizeof *alloced);
567 xfree (alloced);
568 t->glyph_code_table = glyphtab;
570 #endif
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. */
575 Lisp_Object
576 terminal_glyph_code (struct terminal *t, int ch)
578 #if HAVE_LINUX_KD_H
579 if (t->type == output_termcap)
581 /* As a hack, recompute the table when CH is the maximum
582 character. */
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);
589 #endif
591 return Qnil;
594 /* Initial frame has no device-dependent output data, but has
595 face cache which should be freed when the frame is deleted. */
597 static void
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. */
607 struct terminal *
608 init_initial_terminal (void)
610 if (initialized || terminal_list || tty_list)
611 emacs_abort ();
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. */
626 static void
627 delete_initial_terminal (struct terminal *terminal)
629 if (terminal != initial_terminal)
630 emacs_abort ();
632 delete_terminal (terminal);
633 initial_terminal = NULL;
636 void
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);