]> code.delx.au - gnu-emacs/blob - src/terminal.c
Merge from origin/emacs-24
[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 static void delete_initial_terminal (struct terminal *);
41
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)
45 {
46 t->param_alist = val;
47 }
48
49 \f
50
51 void
52 ring_bell (struct frame *f)
53 {
54 if (!NILP (Vring_bell_function))
55 {
56 Lisp_Object function;
57
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.
61
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. */
65
66 function = Vring_bell_function;
67 Vring_bell_function = Qnil;
68
69 call0 (function);
70
71 Vring_bell_function = function;
72 }
73 else if (FRAME_TERMINAL (f)->ring_bell_hook)
74 (*FRAME_TERMINAL (f)->ring_bell_hook) (f);
75 }
76
77 void
78 update_begin (struct frame *f)
79 {
80 if (FRAME_TERMINAL (f)->update_begin_hook)
81 (*FRAME_TERMINAL (f)->update_begin_hook) (f);
82 }
83
84 void
85 update_end (struct frame *f)
86 {
87 if (FRAME_TERMINAL (f)->update_end_hook)
88 (*FRAME_TERMINAL (f)->update_end_hook) (f);
89 }
90
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. */
95
96 void
97 set_terminal_window (struct frame *f, int size)
98 {
99 if (FRAME_TERMINAL (f)->set_terminal_window_hook)
100 (*FRAME_TERMINAL (f)->set_terminal_window_hook) (f, size);
101 }
102
103 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
104 frame-relative coordinates. */
105
106 void
107 cursor_to (struct frame *f, int vpos, int hpos)
108 {
109 if (FRAME_TERMINAL (f)->cursor_to_hook)
110 (*FRAME_TERMINAL (f)->cursor_to_hook) (f, vpos, hpos);
111 }
112
113 /* Similar but don't take any account of the wasted characters. */
114
115 void
116 raw_cursor_to (struct frame *f, int row, int col)
117 {
118 if (FRAME_TERMINAL (f)->raw_cursor_to_hook)
119 (*FRAME_TERMINAL (f)->raw_cursor_to_hook) (f, row, col);
120 }
121
122 /* Erase operations. */
123
124 /* Clear from cursor to end of frame. */
125 void
126 clear_to_end (struct frame *f)
127 {
128 if (FRAME_TERMINAL (f)->clear_to_end_hook)
129 (*FRAME_TERMINAL (f)->clear_to_end_hook) (f);
130 }
131
132 /* Clear entire frame. */
133
134 void
135 clear_frame (struct frame *f)
136 {
137 if (FRAME_TERMINAL (f)->clear_frame_hook)
138 (*FRAME_TERMINAL (f)->clear_frame_hook) (f);
139 }
140
141 /* Clear from cursor to end of line.
142 Assume that the line is already clear starting at column first_unused_hpos.
143
144 Note that the cursor may be moved, on terminals lacking a `ce' string. */
145
146 void
147 clear_end_of_line (struct frame *f, int first_unused_hpos)
148 {
149 if (FRAME_TERMINAL (f)->clear_end_of_line_hook)
150 (*FRAME_TERMINAL (f)->clear_end_of_line_hook) (f, first_unused_hpos);
151 }
152
153 /* Output LEN glyphs starting at STRING at the nominal cursor position.
154 Advance the nominal cursor over the text. */
155
156 void
157 write_glyphs (struct frame *f, struct glyph *string, int len)
158 {
159 if (FRAME_TERMINAL (f)->write_glyphs_hook)
160 (*FRAME_TERMINAL (f)->write_glyphs_hook) (f, string, len);
161 }
162
163 /* Insert LEN glyphs from START at the nominal cursor position.
164
165 If start is zero, insert blanks instead of a string at start */
166
167 void
168 insert_glyphs (struct frame *f, struct glyph *start, int len)
169 {
170 if (len <= 0)
171 return;
172
173 if (FRAME_TERMINAL (f)->insert_glyphs_hook)
174 (*FRAME_TERMINAL (f)->insert_glyphs_hook) (f, start, len);
175 }
176
177 /* Delete N glyphs at the nominal cursor position. */
178
179 void
180 delete_glyphs (struct frame *f, int n)
181 {
182 if (FRAME_TERMINAL (f)->delete_glyphs_hook)
183 (*FRAME_TERMINAL (f)->delete_glyphs_hook) (f, n);
184 }
185
186 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
187
188 void
189 ins_del_lines (struct frame *f, int vpos, int n)
190 {
191 if (FRAME_TERMINAL (f)->ins_del_lines_hook)
192 (*FRAME_TERMINAL (f)->ins_del_lines_hook) (f, vpos, n);
193 }
194
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. */
199
200 static struct terminal *
201 decode_terminal (Lisp_Object terminal)
202 {
203 struct terminal *t;
204
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;
211 }
212
213 /* Like above, but throw an error if TERMINAL is not valid or deleted. */
214
215 struct terminal *
216 decode_live_terminal (Lisp_Object terminal)
217 {
218 struct terminal *t = decode_terminal (terminal);
219
220 if (!t)
221 wrong_type_argument (Qterminal_live_p, terminal);
222 return t;
223 }
224
225 /* Like decode_terminal, but ensure that the resulting terminal object refers
226 to a text-based terminal device. */
227
228 struct terminal *
229 decode_tty_terminal (Lisp_Object terminal)
230 {
231 struct terminal *t = decode_live_terminal (terminal);
232
233 return (t->type == output_termcap || t->type == output_msdos_raw) ? t : NULL;
234 }
235
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. */
239
240 struct terminal *
241 get_named_terminal (const char *name)
242 {
243 struct terminal *t;
244
245 eassert (name);
246
247 for (t = terminal_list; t; t = t->next_terminal)
248 {
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;
253 }
254 return NULL;
255 }
256
257 /* Allocate basically initialized terminal. */
258
259 static struct terminal *
260 allocate_terminal (void)
261 {
262 return ALLOCATE_ZEROED_PSEUDOVECTOR
263 (struct terminal, next_terminal, PVEC_TERMINAL);
264 }
265
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. */
268
269 struct terminal *
270 create_terminal (enum output_method type, struct redisplay_interface *rif)
271 {
272 struct terminal *terminal = allocate_terminal ();
273 Lisp_Object terminal_coding, keyboard_coding;
274
275 terminal->next_terminal = terminal_list;
276 terminal_list = terminal;
277 terminal->type = type;
278 terminal->rif = rif;
279 terminal->id = next_terminal_id++;
280
281 terminal->keyboard_coding = xmalloc (sizeof (struct coding_system));
282 terminal->terminal_coding = xmalloc (sizeof (struct coding_system));
283
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;
299
300 setup_coding_system (keyboard_coding, terminal->keyboard_coding);
301 setup_coding_system (terminal_coding, terminal->terminal_coding);
302
303 return terminal;
304 }
305
306 /* Low-level function to close all frames on a terminal, remove it
307 from the terminal list and free its memory. */
308
309 void
310 delete_terminal (struct terminal *terminal)
311 {
312 struct terminal **tp;
313 Lisp_Object tail, frame;
314
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;
321
322 /* Check for live frames that are still on this terminal. */
323 FOR_EACH_FRAME (tail, frame)
324 {
325 struct frame *f = XFRAME (frame);
326 if (FRAME_LIVE_P (f) && f->terminal == terminal)
327 {
328 /* Pass Qnoelisp rather than Qt. */
329 delete_frame (frame, Qnoelisp);
330 }
331 }
332
333 for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
334 if (! *tp)
335 emacs_abort ();
336 *tp = terminal->next_terminal;
337
338 xfree (terminal->keyboard_coding);
339 terminal->keyboard_coding = NULL;
340 xfree (terminal->terminal_coding);
341 terminal->terminal_coding = NULL;
342
343 if (terminal->kboard && --terminal->kboard->reference_count == 0)
344 {
345 delete_kboard (terminal->kboard);
346 terminal->kboard = NULL;
347 }
348 }
349
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).
354
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)
358 {
359 struct terminal *t = decode_terminal (terminal);
360
361 if (!t)
362 return Qnil;
363
364 if (NILP (force))
365 {
366 struct terminal *p = terminal_list;
367 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
368 p = p->next_terminal;
369
370 if (!p)
371 error ("Attempt to delete the sole active display terminal");
372 }
373
374 if (NILP (Vrun_hooks))
375 ;
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);
383
384 if (t->delete_terminal_hook)
385 (*t->delete_terminal_hook) (t);
386 else
387 delete_terminal (t);
388
389 return Qnil;
390 }
391
392 \f
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.
396
397 The terminal device is represented by its integer identifier. */)
398 (Lisp_Object frame)
399 {
400 struct terminal *t = FRAME_TERMINAL (decode_live_frame (frame));
401
402 if (!t)
403 return Qnil;
404 else
405 {
406 Lisp_Object terminal;
407 XSETTERMINAL (terminal, t);
408 return terminal;
409 }
410 }
411
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)
419 {
420 struct terminal *t = decode_terminal (object);
421
422 if (!t)
423 return Qnil;
424
425 switch (t->type)
426 {
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 ();
440 }
441 }
442
443 DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
444 doc: /* Return a list of all terminal devices. */)
445 (void)
446 {
447 Lisp_Object terminal, terminals = Qnil;
448 struct terminal *t;
449
450 for (t = terminal_list; t; t = t->next_terminal)
451 {
452 XSETTERMINAL (terminal, t);
453 terminals = Fcons (terminal, terminals);
454 }
455
456 return terminals;
457 }
458
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.
462
463 TERMINAL may be a terminal object, a frame, or nil (meaning the
464 selected frame's terminal). */)
465 (Lisp_Object terminal)
466 {
467 struct terminal *t = decode_live_terminal (terminal);
468
469 return t->name ? build_string (t->name) : Qnil;
470 }
471
472
473 \f
474 /* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
475 Return the previous value. */
476
477 static Lisp_Object
478 store_terminal_param (struct terminal *t, Lisp_Object parameter, Lisp_Object value)
479 {
480 Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
481 if (EQ (old_alist_elt, Qnil))
482 {
483 tset_param_alist (t, Fcons (Fcons (parameter, value), t->param_alist));
484 return Qnil;
485 }
486 else
487 {
488 Lisp_Object result = Fcdr (old_alist_elt);
489 Fsetcdr (old_alist_elt, value);
490 return result;
491 }
492 }
493
494
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.
499
500 TERMINAL can be a terminal object, a frame, or nil (meaning the
501 selected frame's terminal). */)
502 (Lisp_Object terminal)
503 {
504 return Fcopy_alist (decode_live_terminal (terminal)->param_alist);
505 }
506
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)
512 {
513 CHECK_SYMBOL (parameter);
514 return Fcdr (Fassq (parameter, decode_live_terminal (terminal)->param_alist));
515 }
516
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.
521
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)
525 {
526 return store_terminal_param (decode_live_terminal (terminal), parameter, value);
527 }
528
529 /* Initial frame has no device-dependent output data, but has
530 face cache which should be freed when the frame is deleted. */
531
532 static void
533 initial_free_frame_resources (struct frame *f)
534 {
535 eassert (FRAME_INITIAL_P (f));
536 free_frame_faces (f);
537 }
538
539 /* Create the bootstrap display terminal for the initial frame.
540 Returns a terminal of type output_initial. */
541
542 struct terminal *
543 init_initial_terminal (void)
544 {
545 if (initialized || terminal_list || tty_list)
546 emacs_abort ();
547
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. */
554
555 return initial_terminal;
556 }
557
558 /* Deletes the bootstrap terminal device.
559 Called through delete_terminal_hook. */
560
561 static void
562 delete_initial_terminal (struct terminal *terminal)
563 {
564 if (terminal != initial_terminal)
565 emacs_abort ();
566
567 delete_terminal (terminal);
568 initial_terminal = NULL;
569 }
570
571 void
572 syms_of_terminal (void)
573 {
574
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;
579
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;
586
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");
590
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);
599
600 Fprovide (intern_c_string ("multi-tty"), Qnil);
601 }