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