declare smobs in alloc.c
[bpt/emacs.git] / src / terminal.c
CommitLineData
ed8dad6b 1/* Functions related to terminal devices.
ba318903 2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
ed8dad6b
KL
3
4This file is part of GNU Emacs.
5
9ec0b715 6GNU Emacs is free software: you can redistribute it and/or modify
ed8dad6b 7it under the terms of the GNU General Public License as published by
9ec0b715
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
ed8dad6b
KL
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9ec0b715 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
ed8dad6b
KL
18
19#include <config.h>
3f22b86f 20
60f5c938 21#include <stdio.h>
ed8dad6b
KL
22
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"
30
6ed8eeff
KL
31/* Chain of all terminals currently in use. */
32struct terminal *terminal_list;
ed8dad6b 33
6ed8eeff
KL
34/* The first unallocated terminal id. */
35static int next_terminal_id;
ed8dad6b 36
6ed8eeff
KL
37/* The initial terminal device, created by initial_term_init. */
38struct terminal *initial_terminal;
ed8dad6b 39
959067a1
DA
40static Lisp_Object Qterminal_live_p;
41
f57e2426 42static void delete_initial_terminal (struct terminal *);
ed8dad6b 43
3f22b86f 44/* This setter is used only in this file, so it can be private. */
b0ab8123 45static void
3f22b86f
PE
46tset_param_alist (struct terminal *t, Lisp_Object val)
47{
48 t->param_alist = val;
49}
50
ed8dad6b
KL
51\f
52
53void
54ring_bell (struct frame *f)
55{
56 if (!NILP (Vring_bell_function))
57 {
58 Lisp_Object function;
59
60 /* Temporarily set the global variable to nil
61 so that if we get an error, it stays nil
62 and we don't call it over and over.
63
64 We don't specbind it, because that would carefully
65 restore the bad value if there's an error
66 and make the loop of errors happen anyway. */
67
68 function = Vring_bell_function;
69 Vring_bell_function = Qnil;
70
71 call0 (function);
72
73 Vring_bell_function = function;
74 }
6ed8eeff
KL
75 else if (FRAME_TERMINAL (f)->ring_bell_hook)
76 (*FRAME_TERMINAL (f)->ring_bell_hook) (f);
ed8dad6b
KL
77}
78
79void
80update_begin (struct frame *f)
81{
6ed8eeff
KL
82 if (FRAME_TERMINAL (f)->update_begin_hook)
83 (*FRAME_TERMINAL (f)->update_begin_hook) (f);
ed8dad6b
KL
84}
85
86void
87update_end (struct frame *f)
88{
6ed8eeff
KL
89 if (FRAME_TERMINAL (f)->update_end_hook)
90 (*FRAME_TERMINAL (f)->update_end_hook) (f);
ed8dad6b
KL
91}
92
93/* Specify how many text lines, from the top of the window,
94 should be affected by insert-lines and delete-lines operations.
95 This, and those operations, are used only within an update
96 that is bounded by calls to update_begin and update_end. */
97
98void
99set_terminal_window (struct frame *f, int size)
100{
6ed8eeff
KL
101 if (FRAME_TERMINAL (f)->set_terminal_window_hook)
102 (*FRAME_TERMINAL (f)->set_terminal_window_hook) (f, size);
ed8dad6b
KL
103}
104
105/* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
106 frame-relative coordinates. */
107
108void
109cursor_to (struct frame *f, int vpos, int hpos)
110{
6ed8eeff
KL
111 if (FRAME_TERMINAL (f)->cursor_to_hook)
112 (*FRAME_TERMINAL (f)->cursor_to_hook) (f, vpos, hpos);
ed8dad6b
KL
113}
114
115/* Similar but don't take any account of the wasted characters. */
116
117void
118raw_cursor_to (struct frame *f, int row, int col)
119{
6ed8eeff 120 if (FRAME_TERMINAL (f)->raw_cursor_to_hook)
426994c3 121 (*FRAME_TERMINAL (f)->raw_cursor_to_hook) (f, row, col);
ed8dad6b
KL
122}
123
124/* Erase operations */
125
126/* Clear from cursor to end of frame. */
127void
128clear_to_end (struct frame *f)
129{
6ed8eeff
KL
130 if (FRAME_TERMINAL (f)->clear_to_end_hook)
131 (*FRAME_TERMINAL (f)->clear_to_end_hook) (f);
ed8dad6b
KL
132}
133
134/* Clear entire frame */
135
136void
137clear_frame (struct frame *f)
138{
6ed8eeff
KL
139 if (FRAME_TERMINAL (f)->clear_frame_hook)
140 (*FRAME_TERMINAL (f)->clear_frame_hook) (f);
ed8dad6b
KL
141}
142
143/* Clear from cursor to end of line.
144 Assume that the line is already clear starting at column first_unused_hpos.
145
146 Note that the cursor may be moved, on terminals lacking a `ce' string. */
147
148void
149clear_end_of_line (struct frame *f, int first_unused_hpos)
150{
6ed8eeff
KL
151 if (FRAME_TERMINAL (f)->clear_end_of_line_hook)
152 (*FRAME_TERMINAL (f)->clear_end_of_line_hook) (f, first_unused_hpos);
ed8dad6b
KL
153}
154
155/* Output LEN glyphs starting at STRING at the nominal cursor position.
156 Advance the nominal cursor over the text. */
157
158void
159write_glyphs (struct frame *f, struct glyph *string, int len)
160{
6ed8eeff
KL
161 if (FRAME_TERMINAL (f)->write_glyphs_hook)
162 (*FRAME_TERMINAL (f)->write_glyphs_hook) (f, string, len);
ed8dad6b
KL
163}
164
165/* Insert LEN glyphs from START at the nominal cursor position.
166
167 If start is zero, insert blanks instead of a string at start */
168
169void
170insert_glyphs (struct frame *f, struct glyph *start, int len)
171{
172 if (len <= 0)
173 return;
174
6ed8eeff
KL
175 if (FRAME_TERMINAL (f)->insert_glyphs_hook)
176 (*FRAME_TERMINAL (f)->insert_glyphs_hook) (f, start, len);
ed8dad6b
KL
177}
178
179/* Delete N glyphs at the nominal cursor position. */
180
181void
182delete_glyphs (struct frame *f, int n)
183{
6ed8eeff
KL
184 if (FRAME_TERMINAL (f)->delete_glyphs_hook)
185 (*FRAME_TERMINAL (f)->delete_glyphs_hook) (f, n);
ed8dad6b
KL
186}
187
188/* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
189
190void
191ins_del_lines (struct frame *f, int vpos, int n)
192{
6ed8eeff
KL
193 if (FRAME_TERMINAL (f)->ins_del_lines_hook)
194 (*FRAME_TERMINAL (f)->ins_del_lines_hook) (f, vpos, n);
ed8dad6b
KL
195}
196
197
198\f
199
e0c3684a
CY
200/* Return the terminal object specified by TERMINAL. TERMINAL may be
201 a terminal object, a frame, or nil for the terminal device of the
653d4f43 202 current frame. If THROW is false, return NULL for failure,
e0c3684a 203 otherwise throw an error. */
ed8dad6b 204
6ed8eeff 205struct terminal *
653d4f43 206get_terminal (Lisp_Object terminal, bool throw)
ed8dad6b 207{
6ed8eeff 208 struct terminal *result = NULL;
ed8dad6b 209
6ed8eeff
KL
210 if (NILP (terminal))
211 terminal = selected_frame;
ed8dad6b 212
d30a25b3
SM
213 if (TERMINALP (terminal))
214 result = XTERMINAL (terminal);
6ed8eeff 215 else if (FRAMEP (terminal))
e0c3684a 216 result = FRAME_TERMINAL (XFRAME (terminal));
ed8dad6b 217
d30a25b3
SM
218 if (result && !result->name)
219 result = NULL;
220
ed8dad6b 221 if (result == NULL && throw)
6ed8eeff 222 wrong_type_argument (Qterminal_live_p, terminal);
ed8dad6b
KL
223
224 return result;
225}
226
6f1d9822
DA
227/* Create a new terminal object of TYPE and add it to the terminal list. RIF
228 may be NULL if this terminal type doesn't support window-based redisplay. */
ed8dad6b 229
6ed8eeff 230struct terminal *
6f1d9822 231create_terminal (enum output_method type, struct redisplay_interface *rif)
ed8dad6b 232{
d30a25b3 233 struct terminal *terminal = allocate_terminal ();
d6de49a1 234 Lisp_Object terminal_coding, keyboard_coding;
d30a25b3 235
6ed8eeff
KL
236 terminal->next_terminal = terminal_list;
237 terminal_list = terminal;
6f1d9822
DA
238 terminal->type = type;
239 terminal->rif = rif;
6ed8eeff 240 terminal->id = next_terminal_id++;
ed8dad6b 241
23f86fce
DA
242 terminal->keyboard_coding = xmalloc (sizeof (struct coding_system));
243 terminal->terminal_coding = xmalloc (sizeof (struct coding_system));
ed8dad6b 244
d6de49a1
EZ
245 /* If default coding systems for the terminal and the keyboard are
246 already defined, use them in preference to the defaults. This is
247 needed when Emacs runs in daemon mode. */
248 keyboard_coding =
249 find_symbol_value (intern ("default-keyboard-coding-system"));
250 if (NILP (keyboard_coding)
251 || EQ (keyboard_coding, Qunbound)
252 || NILP (Fcoding_system_p (keyboard_coding)))
253 keyboard_coding = Qno_conversion;
254 terminal_coding =
255 find_symbol_value (intern ("default-terminal-coding-system"));
256 if (NILP (terminal_coding)
257 || EQ (terminal_coding, Qunbound)
258 || NILP (Fcoding_system_p (terminal_coding)))
259 terminal_coding = Qundecided;
260
261 setup_coding_system (keyboard_coding, terminal->keyboard_coding);
262 setup_coding_system (terminal_coding, terminal->terminal_coding);
ed8dad6b 263
6ed8eeff 264 return terminal;
ed8dad6b
KL
265}
266
ab797f65
KL
267/* Low-level function to close all frames on a terminal, remove it
268 from the terminal list and free its memory. */
ed8dad6b
KL
269
270void
6ed8eeff 271delete_terminal (struct terminal *terminal)
ed8dad6b 272{
6ed8eeff 273 struct terminal **tp;
ed8dad6b 274 Lisp_Object tail, frame;
a98f1617 275
56f2de10 276 /* Protect against recursive calls. delete_frame calls the
ab797f65 277 delete_terminal_hook when we delete our last frame. */
d30a25b3 278 if (!terminal->name)
a98f1617 279 return;
d30a25b3
SM
280 xfree (terminal->name);
281 terminal->name = NULL;
a98f1617 282
cbae07d5 283 /* Check for live frames that are still on this terminal. */
ed8dad6b
KL
284 FOR_EACH_FRAME (tail, frame)
285 {
286 struct frame *f = XFRAME (frame);
6ed8eeff 287 if (FRAME_LIVE_P (f) && f->terminal == terminal)
ed8dad6b 288 {
56f2de10
MR
289 /* Pass Qnoelisp rather than Qt. */
290 delete_frame (frame, Qnoelisp);
ed8dad6b
KL
291 }
292 }
293
6ed8eeff
KL
294 for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
295 if (! *tp)
1088b922 296 emacs_abort ();
6ed8eeff
KL
297 *tp = terminal->next_terminal;
298
d30a25b3
SM
299 xfree (terminal->keyboard_coding);
300 terminal->keyboard_coding = NULL;
301 xfree (terminal->terminal_coding);
302 terminal->terminal_coding = NULL;
56f2de10 303
6ed8eeff 304 if (terminal->kboard && --terminal->kboard->reference_count == 0)
d30a25b3
SM
305 {
306 delete_kboard (terminal->kboard);
307 terminal->kboard = NULL;
308 }
ed8dad6b
KL
309}
310
58555d81
SM
311Lisp_Object Qrun_hook_with_args;
312static Lisp_Object Qdelete_terminal_functions;
a7ca3326 313DEFUN ("delete-terminal", Fdelete_terminal, Sdelete_terminal, 0, 2, 0,
6ed8eeff 314 doc: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
e173bbce
CY
315TERMINAL may be a terminal object, a frame, or nil (meaning the
316selected frame's terminal).
ed8dad6b
KL
317
318Normally, you may not delete a display if all other displays are suspended,
319but if the second argument FORCE is non-nil, you may do so. */)
5842a27b 320 (Lisp_Object terminal, Lisp_Object force)
ed8dad6b 321{
5b8de9c5 322 struct terminal *t = get_terminal (terminal, 0);
ed8dad6b 323
6ed8eeff 324 if (!t)
ed8dad6b
KL
325 return Qnil;
326
5b8de9c5
SM
327 if (NILP (force))
328 {
329 struct terminal *p = terminal_list;
330 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
331 p = p->next_terminal;
56f2de10 332
5b8de9c5
SM
333 if (!p)
334 error ("Attempt to delete the sole active display terminal");
335 }
ed8dad6b 336
58555d81
SM
337 if (NILP (Vrun_hooks))
338 ;
339 else if (EQ (force, Qnoelisp))
340 pending_funcalls
341 = Fcons (list3 (Qrun_hook_with_args,
342 Qdelete_terminal_functions, terminal),
343 pending_funcalls);
344 else
345 safe_call2 (Qrun_hook_with_args, Qdelete_terminal_functions, terminal);
346
6ed8eeff
KL
347 if (t->delete_terminal_hook)
348 (*t->delete_terminal_hook) (t);
ed8dad6b 349 else
6ed8eeff 350 delete_terminal (t);
ed8dad6b
KL
351
352 return Qnil;
353}
354
6ed8eeff 355\f
a7ca3326 356DEFUN ("frame-terminal", Fframe_terminal, Sframe_terminal, 0, 1, 0,
6ed8eeff
KL
357 doc: /* Return the terminal that FRAME is displayed on.
358If FRAME is nil, the selected frame is used.
359
360The terminal device is represented by its integer identifier. */)
5842a27b 361 (Lisp_Object frame)
6ed8eeff 362{
d9f07150 363 struct terminal *t = FRAME_TERMINAL (decode_live_frame (frame));
6ed8eeff
KL
364
365 if (!t)
366 return Qnil;
367 else
d30a25b3
SM
368 {
369 Lisp_Object terminal;
370 XSETTERMINAL (terminal, t);
371 return terminal;
372 }
6ed8eeff
KL
373}
374
375DEFUN ("terminal-live-p", Fterminal_live_p, Sterminal_live_p, 1, 1, 0,
376 doc: /* Return non-nil if OBJECT is a terminal which has not been deleted.
377Value is nil if OBJECT is not a live display terminal.
378If object is a live display terminal, the return value indicates what
379sort of output terminal it uses. See the documentation of `framep' for
d30a25b3 380possible return values. */)
5842a27b 381 (Lisp_Object object)
ed8dad6b 382{
6ed8eeff 383 struct terminal *t;
56f2de10 384
6ed8eeff 385 t = get_terminal (object, 0);
ed8dad6b 386
6ed8eeff 387 if (!t)
ed8dad6b
KL
388 return Qnil;
389
6ed8eeff 390 switch (t->type)
ed8dad6b
KL
391 {
392 case output_initial: /* The initial frame is like a termcap frame. */
393 case output_termcap:
394 return Qt;
395 case output_x_window:
396 return Qx;
397 case output_w32:
398 return Qw32;
399 case output_msdos_raw:
400 return Qpc;
edfda783
AR
401 case output_ns:
402 return Qns;
ed8dad6b 403 default:
1088b922 404 emacs_abort ();
ed8dad6b
KL
405 }
406}
407
6ed8eeff 408DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
d30a25b3 409 doc: /* Return a list of all terminal devices. */)
5842a27b 410 (void)
ed8dad6b 411{
d30a25b3 412 Lisp_Object terminal, terminals = Qnil;
6ed8eeff 413 struct terminal *t;
ed8dad6b 414
6ed8eeff 415 for (t = terminal_list; t; t = t->next_terminal)
d30a25b3
SM
416 {
417 XSETTERMINAL (terminal, t);
418 terminals = Fcons (terminal, terminals);
419 }
ed8dad6b 420
6ed8eeff 421 return terminals;
ed8dad6b
KL
422}
423
6ed8eeff
KL
424DEFUN ("terminal-name", Fterminal_name, Sterminal_name, 0, 1, 0,
425 doc: /* Return the name of the terminal device TERMINAL.
ed8dad6b
KL
426It is not guaranteed that the returned value is unique among opened devices.
427
e173bbce 428TERMINAL may be a terminal object, a frame, or nil (meaning the
6ed8eeff 429selected frame's terminal). */)
5842a27b 430 (Lisp_Object terminal)
ed8dad6b 431{
c5911e55
SM
432 struct terminal *t
433 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
ed8dad6b 434
c5911e55 435 return t->name ? build_string (t->name) : Qnil;
ed8dad6b
KL
436}
437
438
439\f
6ed8eeff 440/* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
ed8dad6b
KL
441 Return the previous value. */
442
426994c3 443static Lisp_Object
971de7fb 444store_terminal_param (struct terminal *t, Lisp_Object parameter, Lisp_Object value)
ed8dad6b 445{
6ed8eeff 446 Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
ed8dad6b
KL
447 if (EQ (old_alist_elt, Qnil))
448 {
3f22b86f 449 tset_param_alist (t, Fcons (Fcons (parameter, value), t->param_alist));
ed8dad6b
KL
450 return Qnil;
451 }
452 else
453 {
454 Lisp_Object result = Fcdr (old_alist_elt);
455 Fsetcdr (old_alist_elt, value);
456 return result;
457 }
458}
459
460
461DEFUN ("terminal-parameters", Fterminal_parameters, Sterminal_parameters, 0, 1, 0,
462 doc: /* Return the parameter-alist of terminal TERMINAL.
463The value is a list of elements of the form (PARM . VALUE), where PARM
464is a symbol.
465
e173bbce
CY
466TERMINAL can be a terminal object, a frame, or nil (meaning the
467selected frame's terminal). */)
5842a27b 468 (Lisp_Object terminal)
ed8dad6b 469{
c5911e55
SM
470 struct terminal *t
471 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
6ed8eeff 472 return Fcopy_alist (t->param_alist);
ed8dad6b
KL
473}
474
475DEFUN ("terminal-parameter", Fterminal_parameter, Sterminal_parameter, 2, 2, 0,
476 doc: /* Return TERMINAL's value for parameter PARAMETER.
e173bbce
CY
477TERMINAL can be a terminal object, a frame, or nil (meaning the
478selected frame's terminal). */)
5842a27b 479 (Lisp_Object terminal, Lisp_Object parameter)
ed8dad6b
KL
480{
481 Lisp_Object value;
c5911e55
SM
482 struct terminal *t
483 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
ed8dad6b 484 CHECK_SYMBOL (parameter);
6ed8eeff 485 value = Fcdr (Fassq (parameter, t->param_alist));
ed8dad6b
KL
486 return value;
487}
488
ed8dad6b
KL
489DEFUN ("set-terminal-parameter", Fset_terminal_parameter,
490 Sset_terminal_parameter, 3, 3, 0,
491 doc: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
492Return the previous value of PARAMETER.
493
e173bbce
CY
494TERMINAL can be a terminal object, a frame or nil (meaning the
495selected frame's terminal). */)
5842a27b 496 (Lisp_Object terminal, Lisp_Object parameter, Lisp_Object value)
ed8dad6b 497{
c5911e55
SM
498 struct terminal *t
499 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
6ed8eeff 500 return store_terminal_param (t, parameter, value);
ed8dad6b
KL
501}
502
6affb4a8
DA
503/* Initial frame has no device-dependent output data, but has
504 face cache which should be freed when the frame is deleted. */
505
506static void
507initial_free_frame_resources (struct frame *f)
508{
509 eassert (FRAME_INITIAL_P (f));
510 free_frame_faces (f);
511}
ed8dad6b 512
6ed8eeff
KL
513/* Create the bootstrap display terminal for the initial frame.
514 Returns a terminal of type output_initial. */
ed8dad6b 515
6ed8eeff
KL
516struct terminal *
517init_initial_terminal (void)
ed8dad6b 518{
6ed8eeff 519 if (initialized || terminal_list || tty_list)
1088b922 520 emacs_abort ();
ed8dad6b 521
6f1d9822 522 initial_terminal = create_terminal (output_initial, NULL);
6ed8eeff
KL
523 initial_terminal->name = xstrdup ("initial_terminal");
524 initial_terminal->kboard = initial_kboard;
6ed8eeff 525 initial_terminal->delete_terminal_hook = &delete_initial_terminal;
6affb4a8 526 initial_terminal->delete_frame_hook = &initial_free_frame_resources;
6f1d9822 527 /* Other hooks are NULL by default. */
ed8dad6b 528
6ed8eeff 529 return initial_terminal;
ed8dad6b
KL
530}
531
6ed8eeff
KL
532/* Deletes the bootstrap terminal device.
533 Called through delete_terminal_hook. */
ed8dad6b 534
6ed8eeff
KL
535static void
536delete_initial_terminal (struct terminal *terminal)
ed8dad6b 537{
6ed8eeff 538 if (terminal != initial_terminal)
1088b922 539 emacs_abort ();
ed8dad6b 540
6ed8eeff
KL
541 delete_terminal (terminal);
542 initial_terminal = NULL;
ed8dad6b
KL
543}
544
545void
971de7fb 546syms_of_terminal (void)
ed8dad6b 547{
fe6aa7a1 548#include "terminal.x"
ed8dad6b 549
29208e82 550 DEFVAR_LISP ("ring-bell-function", Vring_bell_function,
ed8dad6b
KL
551 doc: /* Non-nil means call this function to ring the bell.
552The function should accept no arguments. */);
553 Vring_bell_function = Qnil;
554
29208e82 555 DEFVAR_LISP ("delete-terminal-functions", Vdelete_terminal_functions,
58555d81
SM
556 doc: /* Special hook run when a terminal is deleted.
557Each function is called with argument, the terminal.
558This may be called just before actually deleting the terminal,
559or some time later. */);
560 Vdelete_terminal_functions = Qnil;
959067a1
DA
561
562 DEFSYM (Qterminal_live_p, "terminal-live-p");
cd3520a4
JB
563 DEFSYM (Qdelete_terminal_functions, "delete-terminal-functions");
564 DEFSYM (Qrun_hook_with_args, "run-hook-with-args");
58555d81 565
d67b4f80 566 Fprovide (intern_c_string ("multi-tty"), Qnil);
ed8dad6b 567}