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