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