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