Merge from origin/emacs-24
[emacs.git] / src / terminal.c
blobb48d0623e126822ec197cb8859eacfe2c253361a
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 /* Chain of all terminals currently in use. */
32 struct terminal *terminal_list;
34 /* The first unallocated terminal id. */
35 static int next_terminal_id;
37 /* The initial terminal device, created by initial_term_init. */
38 struct terminal *initial_terminal;
40 static void delete_initial_terminal (struct terminal *);
42 /* This setter is used only in this file, so it can be private. */
43 static void
44 tset_param_alist (struct terminal *t, Lisp_Object val)
46 t->param_alist = val;
51 void
52 ring_bell (struct frame *f)
54 if (!NILP (Vring_bell_function))
56 Lisp_Object function;
58 /* Temporarily set the global variable to nil
59 so that if we get an error, it stays nil
60 and we don't call it over and over.
62 We don't specbind it, because that would carefully
63 restore the bad value if there's an error
64 and make the loop of errors happen anyway. */
66 function = Vring_bell_function;
67 Vring_bell_function = Qnil;
69 call0 (function);
71 Vring_bell_function = function;
73 else if (FRAME_TERMINAL (f)->ring_bell_hook)
74 (*FRAME_TERMINAL (f)->ring_bell_hook) (f);
77 void
78 update_begin (struct frame *f)
80 if (FRAME_TERMINAL (f)->update_begin_hook)
81 (*FRAME_TERMINAL (f)->update_begin_hook) (f);
84 void
85 update_end (struct frame *f)
87 if (FRAME_TERMINAL (f)->update_end_hook)
88 (*FRAME_TERMINAL (f)->update_end_hook) (f);
91 /* Specify how many text lines, from the top of the window,
92 should be affected by insert-lines and delete-lines operations.
93 This, and those operations, are used only within an update
94 that is bounded by calls to update_begin and update_end. */
96 void
97 set_terminal_window (struct frame *f, int size)
99 if (FRAME_TERMINAL (f)->set_terminal_window_hook)
100 (*FRAME_TERMINAL (f)->set_terminal_window_hook) (f, size);
103 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
104 frame-relative coordinates. */
106 void
107 cursor_to (struct frame *f, int vpos, int hpos)
109 if (FRAME_TERMINAL (f)->cursor_to_hook)
110 (*FRAME_TERMINAL (f)->cursor_to_hook) (f, vpos, hpos);
113 /* Similar but don't take any account of the wasted characters. */
115 void
116 raw_cursor_to (struct frame *f, int row, int col)
118 if (FRAME_TERMINAL (f)->raw_cursor_to_hook)
119 (*FRAME_TERMINAL (f)->raw_cursor_to_hook) (f, row, col);
122 /* Erase operations. */
124 /* Clear from cursor to end of frame. */
125 void
126 clear_to_end (struct frame *f)
128 if (FRAME_TERMINAL (f)->clear_to_end_hook)
129 (*FRAME_TERMINAL (f)->clear_to_end_hook) (f);
132 /* Clear entire frame. */
134 void
135 clear_frame (struct frame *f)
137 if (FRAME_TERMINAL (f)->clear_frame_hook)
138 (*FRAME_TERMINAL (f)->clear_frame_hook) (f);
141 /* Clear from cursor to end of line.
142 Assume that the line is already clear starting at column first_unused_hpos.
144 Note that the cursor may be moved, on terminals lacking a `ce' string. */
146 void
147 clear_end_of_line (struct frame *f, int first_unused_hpos)
149 if (FRAME_TERMINAL (f)->clear_end_of_line_hook)
150 (*FRAME_TERMINAL (f)->clear_end_of_line_hook) (f, first_unused_hpos);
153 /* Output LEN glyphs starting at STRING at the nominal cursor position.
154 Advance the nominal cursor over the text. */
156 void
157 write_glyphs (struct frame *f, struct glyph *string, int len)
159 if (FRAME_TERMINAL (f)->write_glyphs_hook)
160 (*FRAME_TERMINAL (f)->write_glyphs_hook) (f, string, len);
163 /* Insert LEN glyphs from START at the nominal cursor position.
165 If start is zero, insert blanks instead of a string at start */
167 void
168 insert_glyphs (struct frame *f, struct glyph *start, int len)
170 if (len <= 0)
171 return;
173 if (FRAME_TERMINAL (f)->insert_glyphs_hook)
174 (*FRAME_TERMINAL (f)->insert_glyphs_hook) (f, start, len);
177 /* Delete N glyphs at the nominal cursor position. */
179 void
180 delete_glyphs (struct frame *f, int n)
182 if (FRAME_TERMINAL (f)->delete_glyphs_hook)
183 (*FRAME_TERMINAL (f)->delete_glyphs_hook) (f, n);
186 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
188 void
189 ins_del_lines (struct frame *f, int vpos, int n)
191 if (FRAME_TERMINAL (f)->ins_del_lines_hook)
192 (*FRAME_TERMINAL (f)->ins_del_lines_hook) (f, vpos, n);
195 /* Return the terminal object specified by TERMINAL. TERMINAL may
196 be a terminal object, a frame, or nil for the terminal device of
197 the current frame. If TERMINAL is neither from the above or the
198 resulting terminal object is deleted, return NULL. */
200 static struct terminal *
201 decode_terminal (Lisp_Object terminal)
203 struct terminal *t;
205 if (NILP (terminal))
206 terminal = selected_frame;
207 t = (TERMINALP (terminal)
208 ? XTERMINAL (terminal)
209 : FRAMEP (terminal) ? FRAME_TERMINAL (XFRAME (terminal)) : NULL);
210 return t && t->name ? t : NULL;
213 /* Like above, but throw an error if TERMINAL is not valid or deleted. */
215 struct terminal *
216 decode_live_terminal (Lisp_Object terminal)
218 struct terminal *t = decode_terminal (terminal);
220 if (!t)
221 wrong_type_argument (Qterminal_live_p, terminal);
222 return t;
225 /* Like decode_terminal, but ensure that the resulting terminal object refers
226 to a text-based terminal device. */
228 struct terminal *
229 decode_tty_terminal (Lisp_Object terminal)
231 struct terminal *t = decode_live_terminal (terminal);
233 return (t->type == output_termcap || t->type == output_msdos_raw) ? t : NULL;
236 /* Return an active (not suspended) text-based terminal device that uses
237 the tty device with the given NAME, or NULL if the named terminal device
238 is not opened. */
240 struct terminal *
241 get_named_terminal (const char *name)
243 struct terminal *t;
245 eassert (name);
247 for (t = terminal_list; t; t = t->next_terminal)
249 if ((t->type == output_termcap || t->type == output_msdos_raw)
250 && !strcmp (t->display_info.tty->name, name)
251 && TERMINAL_ACTIVE_P (t))
252 return t;
254 return NULL;
257 /* Allocate basically initialized terminal. */
259 static struct terminal *
260 allocate_terminal (void)
262 return ALLOCATE_ZEROED_PSEUDOVECTOR
263 (struct terminal, next_terminal, PVEC_TERMINAL);
266 /* Create a new terminal object of TYPE and add it to the terminal list. RIF
267 may be NULL if this terminal type doesn't support window-based redisplay. */
269 struct terminal *
270 create_terminal (enum output_method type, struct redisplay_interface *rif)
272 struct terminal *terminal = allocate_terminal ();
273 Lisp_Object terminal_coding, keyboard_coding;
275 terminal->next_terminal = terminal_list;
276 terminal_list = terminal;
277 terminal->type = type;
278 terminal->rif = rif;
279 terminal->id = next_terminal_id++;
281 terminal->keyboard_coding = xmalloc (sizeof (struct coding_system));
282 terminal->terminal_coding = xmalloc (sizeof (struct coding_system));
284 /* If default coding systems for the terminal and the keyboard are
285 already defined, use them in preference to the defaults. This is
286 needed when Emacs runs in daemon mode. */
287 keyboard_coding =
288 find_symbol_value (intern ("default-keyboard-coding-system"));
289 if (NILP (keyboard_coding)
290 || EQ (keyboard_coding, Qunbound)
291 || NILP (Fcoding_system_p (keyboard_coding)))
292 keyboard_coding = Qno_conversion;
293 terminal_coding =
294 find_symbol_value (intern ("default-terminal-coding-system"));
295 if (NILP (terminal_coding)
296 || EQ (terminal_coding, Qunbound)
297 || NILP (Fcoding_system_p (terminal_coding)))
298 terminal_coding = Qundecided;
300 setup_coding_system (keyboard_coding, terminal->keyboard_coding);
301 setup_coding_system (terminal_coding, terminal->terminal_coding);
303 return terminal;
306 /* Low-level function to close all frames on a terminal, remove it
307 from the terminal list and free its memory. */
309 void
310 delete_terminal (struct terminal *terminal)
312 struct terminal **tp;
313 Lisp_Object tail, frame;
315 /* Protect against recursive calls. delete_frame calls the
316 delete_terminal_hook when we delete our last frame. */
317 if (!terminal->name)
318 return;
319 xfree (terminal->name);
320 terminal->name = NULL;
322 /* Check for live frames that are still on this terminal. */
323 FOR_EACH_FRAME (tail, frame)
325 struct frame *f = XFRAME (frame);
326 if (FRAME_LIVE_P (f) && f->terminal == terminal)
328 /* Pass Qnoelisp rather than Qt. */
329 delete_frame (frame, Qnoelisp);
333 for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
334 if (! *tp)
335 emacs_abort ();
336 *tp = terminal->next_terminal;
338 xfree (terminal->keyboard_coding);
339 terminal->keyboard_coding = NULL;
340 xfree (terminal->terminal_coding);
341 terminal->terminal_coding = NULL;
343 if (terminal->kboard && --terminal->kboard->reference_count == 0)
345 delete_kboard (terminal->kboard);
346 terminal->kboard = NULL;
350 DEFUN ("delete-terminal", Fdelete_terminal, Sdelete_terminal, 0, 2, 0,
351 doc: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
352 TERMINAL may be a terminal object, a frame, or nil (meaning the
353 selected frame's terminal).
355 Normally, you may not delete a display if all other displays are suspended,
356 but if the second argument FORCE is non-nil, you may do so. */)
357 (Lisp_Object terminal, Lisp_Object force)
359 struct terminal *t = decode_terminal (terminal);
361 if (!t)
362 return Qnil;
364 if (NILP (force))
366 struct terminal *p = terminal_list;
367 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
368 p = p->next_terminal;
370 if (!p)
371 error ("Attempt to delete the sole active display terminal");
374 if (NILP (Vrun_hooks))
376 else if (EQ (force, Qnoelisp))
377 pending_funcalls
378 = Fcons (list3 (Qrun_hook_with_args,
379 Qdelete_terminal_functions, terminal),
380 pending_funcalls);
381 else
382 safe_call2 (Qrun_hook_with_args, Qdelete_terminal_functions, terminal);
384 if (t->delete_terminal_hook)
385 (*t->delete_terminal_hook) (t);
386 else
387 delete_terminal (t);
389 return Qnil;
393 DEFUN ("frame-terminal", Fframe_terminal, Sframe_terminal, 0, 1, 0,
394 doc: /* Return the terminal that FRAME is displayed on.
395 If FRAME is nil, the selected frame is used.
397 The terminal device is represented by its integer identifier. */)
398 (Lisp_Object frame)
400 struct terminal *t = FRAME_TERMINAL (decode_live_frame (frame));
402 if (!t)
403 return Qnil;
404 else
406 Lisp_Object terminal;
407 XSETTERMINAL (terminal, t);
408 return terminal;
412 DEFUN ("terminal-live-p", Fterminal_live_p, Sterminal_live_p, 1, 1, 0,
413 doc: /* Return non-nil if OBJECT is a terminal which has not been deleted.
414 Value is nil if OBJECT is not a live display terminal.
415 If object is a live display terminal, the return value indicates what
416 sort of output terminal it uses. See the documentation of `framep' for
417 possible return values. */)
418 (Lisp_Object object)
420 struct terminal *t = decode_terminal (object);
422 if (!t)
423 return Qnil;
425 switch (t->type)
427 case output_initial: /* The initial frame is like a termcap frame. */
428 case output_termcap:
429 return Qt;
430 case output_x_window:
431 return Qx;
432 case output_w32:
433 return Qw32;
434 case output_msdos_raw:
435 return Qpc;
436 case output_ns:
437 return Qns;
438 default:
439 emacs_abort ();
443 DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
444 doc: /* Return a list of all terminal devices. */)
445 (void)
447 Lisp_Object terminal, terminals = Qnil;
448 struct terminal *t;
450 for (t = terminal_list; t; t = t->next_terminal)
452 XSETTERMINAL (terminal, t);
453 terminals = Fcons (terminal, terminals);
456 return terminals;
459 DEFUN ("terminal-name", Fterminal_name, Sterminal_name, 0, 1, 0,
460 doc: /* Return the name of the terminal device TERMINAL.
461 It is not guaranteed that the returned value is unique among opened devices.
463 TERMINAL may be a terminal object, a frame, or nil (meaning the
464 selected frame's terminal). */)
465 (Lisp_Object terminal)
467 struct terminal *t = decode_live_terminal (terminal);
469 return t->name ? build_string (t->name) : Qnil;
474 /* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
475 Return the previous value. */
477 static Lisp_Object
478 store_terminal_param (struct terminal *t, Lisp_Object parameter, Lisp_Object value)
480 Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
481 if (EQ (old_alist_elt, Qnil))
483 tset_param_alist (t, Fcons (Fcons (parameter, value), t->param_alist));
484 return Qnil;
486 else
488 Lisp_Object result = Fcdr (old_alist_elt);
489 Fsetcdr (old_alist_elt, value);
490 return result;
495 DEFUN ("terminal-parameters", Fterminal_parameters, Sterminal_parameters, 0, 1, 0,
496 doc: /* Return the parameter-alist of terminal TERMINAL.
497 The value is a list of elements of the form (PARM . VALUE), where PARM
498 is a symbol.
500 TERMINAL can be a terminal object, a frame, or nil (meaning the
501 selected frame's terminal). */)
502 (Lisp_Object terminal)
504 return Fcopy_alist (decode_live_terminal (terminal)->param_alist);
507 DEFUN ("terminal-parameter", Fterminal_parameter, Sterminal_parameter, 2, 2, 0,
508 doc: /* Return TERMINAL's value for parameter PARAMETER.
509 TERMINAL can be a terminal object, a frame, or nil (meaning the
510 selected frame's terminal). */)
511 (Lisp_Object terminal, Lisp_Object parameter)
513 CHECK_SYMBOL (parameter);
514 return Fcdr (Fassq (parameter, decode_live_terminal (terminal)->param_alist));
517 DEFUN ("set-terminal-parameter", Fset_terminal_parameter,
518 Sset_terminal_parameter, 3, 3, 0,
519 doc: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
520 Return the previous value of PARAMETER.
522 TERMINAL can be a terminal object, a frame or nil (meaning the
523 selected frame's terminal). */)
524 (Lisp_Object terminal, Lisp_Object parameter, Lisp_Object value)
526 return store_terminal_param (decode_live_terminal (terminal), parameter, value);
529 /* Initial frame has no device-dependent output data, but has
530 face cache which should be freed when the frame is deleted. */
532 static void
533 initial_free_frame_resources (struct frame *f)
535 eassert (FRAME_INITIAL_P (f));
536 free_frame_faces (f);
539 /* Create the bootstrap display terminal for the initial frame.
540 Returns a terminal of type output_initial. */
542 struct terminal *
543 init_initial_terminal (void)
545 if (initialized || terminal_list || tty_list)
546 emacs_abort ();
548 initial_terminal = create_terminal (output_initial, NULL);
549 initial_terminal->name = xstrdup ("initial_terminal");
550 initial_terminal->kboard = initial_kboard;
551 initial_terminal->delete_terminal_hook = &delete_initial_terminal;
552 initial_terminal->delete_frame_hook = &initial_free_frame_resources;
553 /* Other hooks are NULL by default. */
555 return initial_terminal;
558 /* Deletes the bootstrap terminal device.
559 Called through delete_terminal_hook. */
561 static void
562 delete_initial_terminal (struct terminal *terminal)
564 if (terminal != initial_terminal)
565 emacs_abort ();
567 delete_terminal (terminal);
568 initial_terminal = NULL;
571 void
572 syms_of_terminal (void)
575 DEFVAR_LISP ("ring-bell-function", Vring_bell_function,
576 doc: /* Non-nil means call this function to ring the bell.
577 The function should accept no arguments. */);
578 Vring_bell_function = Qnil;
580 DEFVAR_LISP ("delete-terminal-functions", Vdelete_terminal_functions,
581 doc: /* Special hook run when a terminal is deleted.
582 Each function is called with argument, the terminal.
583 This may be called just before actually deleting the terminal,
584 or some time later. */);
585 Vdelete_terminal_functions = Qnil;
587 DEFSYM (Qterminal_live_p, "terminal-live-p");
588 DEFSYM (Qdelete_terminal_functions, "delete-terminal-functions");
589 DEFSYM (Qrun_hook_with_args, "run-hook-with-args");
591 defsubr (&Sdelete_terminal);
592 defsubr (&Sframe_terminal);
593 defsubr (&Sterminal_live_p);
594 defsubr (&Sterminal_list);
595 defsubr (&Sterminal_name);
596 defsubr (&Sterminal_parameters);
597 defsubr (&Sterminal_parameter);
598 defsubr (&Sset_terminal_parameter);
600 Fprovide (intern_c_string ("multi-tty"), Qnil);