Use bool for boolean in xmenu.c, xml.c
[emacs.git] / src / terminal.c
blob92befd28543b6eb1b3cdf615e1dc476f1a7180c6
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 /* Create a new terminal object of TYPE and add it to the terminal list. RIF
258 may be NULL if this terminal type doesn't support window-based redisplay. */
260 struct terminal *
261 create_terminal (enum output_method type, struct redisplay_interface *rif)
263 struct terminal *terminal = allocate_terminal ();
264 Lisp_Object terminal_coding, keyboard_coding;
266 terminal->next_terminal = terminal_list;
267 terminal_list = terminal;
268 terminal->type = type;
269 terminal->rif = rif;
270 terminal->id = next_terminal_id++;
272 terminal->keyboard_coding = xmalloc (sizeof (struct coding_system));
273 terminal->terminal_coding = xmalloc (sizeof (struct coding_system));
275 /* If default coding systems for the terminal and the keyboard are
276 already defined, use them in preference to the defaults. This is
277 needed when Emacs runs in daemon mode. */
278 keyboard_coding =
279 find_symbol_value (intern ("default-keyboard-coding-system"));
280 if (NILP (keyboard_coding)
281 || EQ (keyboard_coding, Qunbound)
282 || NILP (Fcoding_system_p (keyboard_coding)))
283 keyboard_coding = Qno_conversion;
284 terminal_coding =
285 find_symbol_value (intern ("default-terminal-coding-system"));
286 if (NILP (terminal_coding)
287 || EQ (terminal_coding, Qunbound)
288 || NILP (Fcoding_system_p (terminal_coding)))
289 terminal_coding = Qundecided;
291 setup_coding_system (keyboard_coding, terminal->keyboard_coding);
292 setup_coding_system (terminal_coding, terminal->terminal_coding);
294 return terminal;
297 /* Low-level function to close all frames on a terminal, remove it
298 from the terminal list and free its memory. */
300 void
301 delete_terminal (struct terminal *terminal)
303 struct terminal **tp;
304 Lisp_Object tail, frame;
306 /* Protect against recursive calls. delete_frame calls the
307 delete_terminal_hook when we delete our last frame. */
308 if (!terminal->name)
309 return;
310 xfree (terminal->name);
311 terminal->name = NULL;
313 /* Check for live frames that are still on this terminal. */
314 FOR_EACH_FRAME (tail, frame)
316 struct frame *f = XFRAME (frame);
317 if (FRAME_LIVE_P (f) && f->terminal == terminal)
319 /* Pass Qnoelisp rather than Qt. */
320 delete_frame (frame, Qnoelisp);
324 for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
325 if (! *tp)
326 emacs_abort ();
327 *tp = terminal->next_terminal;
329 xfree (terminal->keyboard_coding);
330 terminal->keyboard_coding = NULL;
331 xfree (terminal->terminal_coding);
332 terminal->terminal_coding = NULL;
334 if (terminal->kboard && --terminal->kboard->reference_count == 0)
336 delete_kboard (terminal->kboard);
337 terminal->kboard = NULL;
341 DEFUN ("delete-terminal", Fdelete_terminal, Sdelete_terminal, 0, 2, 0,
342 doc: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
343 TERMINAL may be a terminal object, a frame, or nil (meaning the
344 selected frame's terminal).
346 Normally, you may not delete a display if all other displays are suspended,
347 but if the second argument FORCE is non-nil, you may do so. */)
348 (Lisp_Object terminal, Lisp_Object force)
350 struct terminal *t = decode_terminal (terminal);
352 if (!t)
353 return Qnil;
355 if (NILP (force))
357 struct terminal *p = terminal_list;
358 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
359 p = p->next_terminal;
361 if (!p)
362 error ("Attempt to delete the sole active display terminal");
365 if (NILP (Vrun_hooks))
367 else if (EQ (force, Qnoelisp))
368 pending_funcalls
369 = Fcons (list3 (Qrun_hook_with_args,
370 Qdelete_terminal_functions, terminal),
371 pending_funcalls);
372 else
373 safe_call2 (Qrun_hook_with_args, Qdelete_terminal_functions, terminal);
375 if (t->delete_terminal_hook)
376 (*t->delete_terminal_hook) (t);
377 else
378 delete_terminal (t);
380 return Qnil;
384 DEFUN ("frame-terminal", Fframe_terminal, Sframe_terminal, 0, 1, 0,
385 doc: /* Return the terminal that FRAME is displayed on.
386 If FRAME is nil, the selected frame is used.
388 The terminal device is represented by its integer identifier. */)
389 (Lisp_Object frame)
391 struct terminal *t = FRAME_TERMINAL (decode_live_frame (frame));
393 if (!t)
394 return Qnil;
395 else
397 Lisp_Object terminal;
398 XSETTERMINAL (terminal, t);
399 return terminal;
403 DEFUN ("terminal-live-p", Fterminal_live_p, Sterminal_live_p, 1, 1, 0,
404 doc: /* Return non-nil if OBJECT is a terminal which has not been deleted.
405 Value is nil if OBJECT is not a live display terminal.
406 If object is a live display terminal, the return value indicates what
407 sort of output terminal it uses. See the documentation of `framep' for
408 possible return values. */)
409 (Lisp_Object object)
411 struct terminal *t = decode_terminal (object);
413 if (!t)
414 return Qnil;
416 switch (t->type)
418 case output_initial: /* The initial frame is like a termcap frame. */
419 case output_termcap:
420 return Qt;
421 case output_x_window:
422 return Qx;
423 case output_w32:
424 return Qw32;
425 case output_msdos_raw:
426 return Qpc;
427 case output_ns:
428 return Qns;
429 default:
430 emacs_abort ();
434 DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
435 doc: /* Return a list of all terminal devices. */)
436 (void)
438 Lisp_Object terminal, terminals = Qnil;
439 struct terminal *t;
441 for (t = terminal_list; t; t = t->next_terminal)
443 XSETTERMINAL (terminal, t);
444 terminals = Fcons (terminal, terminals);
447 return terminals;
450 DEFUN ("terminal-name", Fterminal_name, Sterminal_name, 0, 1, 0,
451 doc: /* Return the name of the terminal device TERMINAL.
452 It is not guaranteed that the returned value is unique among opened devices.
454 TERMINAL may be a terminal object, a frame, or nil (meaning the
455 selected frame's terminal). */)
456 (Lisp_Object terminal)
458 struct terminal *t = decode_live_terminal (terminal);
460 return t->name ? build_string (t->name) : Qnil;
465 /* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
466 Return the previous value. */
468 static Lisp_Object
469 store_terminal_param (struct terminal *t, Lisp_Object parameter, Lisp_Object value)
471 Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
472 if (EQ (old_alist_elt, Qnil))
474 tset_param_alist (t, Fcons (Fcons (parameter, value), t->param_alist));
475 return Qnil;
477 else
479 Lisp_Object result = Fcdr (old_alist_elt);
480 Fsetcdr (old_alist_elt, value);
481 return result;
486 DEFUN ("terminal-parameters", Fterminal_parameters, Sterminal_parameters, 0, 1, 0,
487 doc: /* Return the parameter-alist of terminal TERMINAL.
488 The value is a list of elements of the form (PARM . VALUE), where PARM
489 is a symbol.
491 TERMINAL can be a terminal object, a frame, or nil (meaning the
492 selected frame's terminal). */)
493 (Lisp_Object terminal)
495 return Fcopy_alist (decode_live_terminal (terminal)->param_alist);
498 DEFUN ("terminal-parameter", Fterminal_parameter, Sterminal_parameter, 2, 2, 0,
499 doc: /* Return TERMINAL's value for parameter PARAMETER.
500 TERMINAL can be a terminal object, a frame, or nil (meaning the
501 selected frame's terminal). */)
502 (Lisp_Object terminal, Lisp_Object parameter)
504 CHECK_SYMBOL (parameter);
505 return Fcdr (Fassq (parameter, decode_live_terminal (terminal)->param_alist));
508 DEFUN ("set-terminal-parameter", Fset_terminal_parameter,
509 Sset_terminal_parameter, 3, 3, 0,
510 doc: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
511 Return the previous value of PARAMETER.
513 TERMINAL can be a terminal object, a frame or nil (meaning the
514 selected frame's terminal). */)
515 (Lisp_Object terminal, Lisp_Object parameter, Lisp_Object value)
517 return store_terminal_param (decode_live_terminal (terminal), parameter, value);
520 /* Initial frame has no device-dependent output data, but has
521 face cache which should be freed when the frame is deleted. */
523 static void
524 initial_free_frame_resources (struct frame *f)
526 eassert (FRAME_INITIAL_P (f));
527 free_frame_faces (f);
530 /* Create the bootstrap display terminal for the initial frame.
531 Returns a terminal of type output_initial. */
533 struct terminal *
534 init_initial_terminal (void)
536 if (initialized || terminal_list || tty_list)
537 emacs_abort ();
539 initial_terminal = create_terminal (output_initial, NULL);
540 initial_terminal->name = xstrdup ("initial_terminal");
541 initial_terminal->kboard = initial_kboard;
542 initial_terminal->delete_terminal_hook = &delete_initial_terminal;
543 initial_terminal->delete_frame_hook = &initial_free_frame_resources;
544 /* Other hooks are NULL by default. */
546 return initial_terminal;
549 /* Deletes the bootstrap terminal device.
550 Called through delete_terminal_hook. */
552 static void
553 delete_initial_terminal (struct terminal *terminal)
555 if (terminal != initial_terminal)
556 emacs_abort ();
558 delete_terminal (terminal);
559 initial_terminal = NULL;
562 void
563 syms_of_terminal (void)
566 DEFVAR_LISP ("ring-bell-function", Vring_bell_function,
567 doc: /* Non-nil means call this function to ring the bell.
568 The function should accept no arguments. */);
569 Vring_bell_function = Qnil;
571 DEFVAR_LISP ("delete-terminal-functions", Vdelete_terminal_functions,
572 doc: /* Special hook run when a terminal is deleted.
573 Each function is called with argument, the terminal.
574 This may be called just before actually deleting the terminal,
575 or some time later. */);
576 Vdelete_terminal_functions = Qnil;
578 DEFSYM (Qterminal_live_p, "terminal-live-p");
579 DEFSYM (Qdelete_terminal_functions, "delete-terminal-functions");
580 DEFSYM (Qrun_hook_with_args, "run-hook-with-args");
582 defsubr (&Sdelete_terminal);
583 defsubr (&Sframe_terminal);
584 defsubr (&Sterminal_live_p);
585 defsubr (&Sterminal_list);
586 defsubr (&Sterminal_name);
587 defsubr (&Sterminal_parameters);
588 defsubr (&Sterminal_parameter);
589 defsubr (&Sset_terminal_parameter);
591 Fprovide (intern_c_string ("multi-tty"), Qnil);