]> code.delx.au - gnu-emacs/blob - src/terminal.c
Compute C decls for DEFSYMs automatically
[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 /* Create a new terminal object of TYPE and add it to the terminal list. RIF
258 may be NULL if this terminal type doesn't support window-based redisplay. */
259
260 struct terminal *
261 create_terminal (enum output_method type, struct redisplay_interface *rif)
262 {
263 struct terminal *terminal = allocate_terminal ();
264 Lisp_Object terminal_coding, keyboard_coding;
265
266 terminal->next_terminal = terminal_list;
267 terminal_list = terminal;
268 terminal->type = type;
269 terminal->rif = rif;
270 terminal->id = next_terminal_id++;
271
272 terminal->keyboard_coding = xmalloc (sizeof (struct coding_system));
273 terminal->terminal_coding = xmalloc (sizeof (struct coding_system));
274
275 /* If default coding systems for the terminal and the keyboard are
276 already defined, use them in preference to the defaults. This is
277 needed when Emacs runs in daemon mode. */
278 keyboard_coding =
279 find_symbol_value (intern ("default-keyboard-coding-system"));
280 if (NILP (keyboard_coding)
281 || EQ (keyboard_coding, Qunbound)
282 || NILP (Fcoding_system_p (keyboard_coding)))
283 keyboard_coding = Qno_conversion;
284 terminal_coding =
285 find_symbol_value (intern ("default-terminal-coding-system"));
286 if (NILP (terminal_coding)
287 || EQ (terminal_coding, Qunbound)
288 || NILP (Fcoding_system_p (terminal_coding)))
289 terminal_coding = Qundecided;
290
291 setup_coding_system (keyboard_coding, terminal->keyboard_coding);
292 setup_coding_system (terminal_coding, terminal->terminal_coding);
293
294 return terminal;
295 }
296
297 /* Low-level function to close all frames on a terminal, remove it
298 from the terminal list and free its memory. */
299
300 void
301 delete_terminal (struct terminal *terminal)
302 {
303 struct terminal **tp;
304 Lisp_Object tail, frame;
305
306 /* Protect against recursive calls. delete_frame calls the
307 delete_terminal_hook when we delete our last frame. */
308 if (!terminal->name)
309 return;
310 xfree (terminal->name);
311 terminal->name = NULL;
312
313 /* Check for live frames that are still on this terminal. */
314 FOR_EACH_FRAME (tail, frame)
315 {
316 struct frame *f = XFRAME (frame);
317 if (FRAME_LIVE_P (f) && f->terminal == terminal)
318 {
319 /* Pass Qnoelisp rather than Qt. */
320 delete_frame (frame, Qnoelisp);
321 }
322 }
323
324 for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
325 if (! *tp)
326 emacs_abort ();
327 *tp = terminal->next_terminal;
328
329 xfree (terminal->keyboard_coding);
330 terminal->keyboard_coding = NULL;
331 xfree (terminal->terminal_coding);
332 terminal->terminal_coding = NULL;
333
334 if (terminal->kboard && --terminal->kboard->reference_count == 0)
335 {
336 delete_kboard (terminal->kboard);
337 terminal->kboard = NULL;
338 }
339 }
340
341 DEFUN ("delete-terminal", Fdelete_terminal, Sdelete_terminal, 0, 2, 0,
342 doc: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
343 TERMINAL may be a terminal object, a frame, or nil (meaning the
344 selected frame's terminal).
345
346 Normally, you may not delete a display if all other displays are suspended,
347 but if the second argument FORCE is non-nil, you may do so. */)
348 (Lisp_Object terminal, Lisp_Object force)
349 {
350 struct terminal *t = decode_terminal (terminal);
351
352 if (!t)
353 return Qnil;
354
355 if (NILP (force))
356 {
357 struct terminal *p = terminal_list;
358 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
359 p = p->next_terminal;
360
361 if (!p)
362 error ("Attempt to delete the sole active display terminal");
363 }
364
365 if (NILP (Vrun_hooks))
366 ;
367 else if (EQ (force, Qnoelisp))
368 pending_funcalls
369 = Fcons (list3 (Qrun_hook_with_args,
370 Qdelete_terminal_functions, terminal),
371 pending_funcalls);
372 else
373 safe_call2 (Qrun_hook_with_args, Qdelete_terminal_functions, terminal);
374
375 if (t->delete_terminal_hook)
376 (*t->delete_terminal_hook) (t);
377 else
378 delete_terminal (t);
379
380 return Qnil;
381 }
382
383 \f
384 DEFUN ("frame-terminal", Fframe_terminal, Sframe_terminal, 0, 1, 0,
385 doc: /* Return the terminal that FRAME is displayed on.
386 If FRAME is nil, the selected frame is used.
387
388 The terminal device is represented by its integer identifier. */)
389 (Lisp_Object frame)
390 {
391 struct terminal *t = FRAME_TERMINAL (decode_live_frame (frame));
392
393 if (!t)
394 return Qnil;
395 else
396 {
397 Lisp_Object terminal;
398 XSETTERMINAL (terminal, t);
399 return terminal;
400 }
401 }
402
403 DEFUN ("terminal-live-p", Fterminal_live_p, Sterminal_live_p, 1, 1, 0,
404 doc: /* Return non-nil if OBJECT is a terminal which has not been deleted.
405 Value is nil if OBJECT is not a live display terminal.
406 If object is a live display terminal, the return value indicates what
407 sort of output terminal it uses. See the documentation of `framep' for
408 possible return values. */)
409 (Lisp_Object object)
410 {
411 struct terminal *t = decode_terminal (object);
412
413 if (!t)
414 return Qnil;
415
416 switch (t->type)
417 {
418 case output_initial: /* The initial frame is like a termcap frame. */
419 case output_termcap:
420 return Qt;
421 case output_x_window:
422 return Qx;
423 case output_w32:
424 return Qw32;
425 case output_msdos_raw:
426 return Qpc;
427 case output_ns:
428 return Qns;
429 default:
430 emacs_abort ();
431 }
432 }
433
434 DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
435 doc: /* Return a list of all terminal devices. */)
436 (void)
437 {
438 Lisp_Object terminal, terminals = Qnil;
439 struct terminal *t;
440
441 for (t = terminal_list; t; t = t->next_terminal)
442 {
443 XSETTERMINAL (terminal, t);
444 terminals = Fcons (terminal, terminals);
445 }
446
447 return terminals;
448 }
449
450 DEFUN ("terminal-name", Fterminal_name, Sterminal_name, 0, 1, 0,
451 doc: /* Return the name of the terminal device TERMINAL.
452 It is not guaranteed that the returned value is unique among opened devices.
453
454 TERMINAL may be a terminal object, a frame, or nil (meaning the
455 selected frame's terminal). */)
456 (Lisp_Object terminal)
457 {
458 struct terminal *t = decode_live_terminal (terminal);
459
460 return t->name ? build_string (t->name) : Qnil;
461 }
462
463
464 \f
465 /* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
466 Return the previous value. */
467
468 static Lisp_Object
469 store_terminal_param (struct terminal *t, Lisp_Object parameter, Lisp_Object value)
470 {
471 Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
472 if (EQ (old_alist_elt, Qnil))
473 {
474 tset_param_alist (t, Fcons (Fcons (parameter, value), t->param_alist));
475 return Qnil;
476 }
477 else
478 {
479 Lisp_Object result = Fcdr (old_alist_elt);
480 Fsetcdr (old_alist_elt, value);
481 return result;
482 }
483 }
484
485
486 DEFUN ("terminal-parameters", Fterminal_parameters, Sterminal_parameters, 0, 1, 0,
487 doc: /* Return the parameter-alist of terminal TERMINAL.
488 The value is a list of elements of the form (PARM . VALUE), where PARM
489 is a symbol.
490
491 TERMINAL can be a terminal object, a frame, or nil (meaning the
492 selected frame's terminal). */)
493 (Lisp_Object terminal)
494 {
495 return Fcopy_alist (decode_live_terminal (terminal)->param_alist);
496 }
497
498 DEFUN ("terminal-parameter", Fterminal_parameter, Sterminal_parameter, 2, 2, 0,
499 doc: /* Return TERMINAL's value for parameter PARAMETER.
500 TERMINAL can be a terminal object, a frame, or nil (meaning the
501 selected frame's terminal). */)
502 (Lisp_Object terminal, Lisp_Object parameter)
503 {
504 CHECK_SYMBOL (parameter);
505 return Fcdr (Fassq (parameter, decode_live_terminal (terminal)->param_alist));
506 }
507
508 DEFUN ("set-terminal-parameter", Fset_terminal_parameter,
509 Sset_terminal_parameter, 3, 3, 0,
510 doc: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
511 Return the previous value of PARAMETER.
512
513 TERMINAL can be a terminal object, a frame or nil (meaning the
514 selected frame's terminal). */)
515 (Lisp_Object terminal, Lisp_Object parameter, Lisp_Object value)
516 {
517 return store_terminal_param (decode_live_terminal (terminal), parameter, value);
518 }
519
520 /* Initial frame has no device-dependent output data, but has
521 face cache which should be freed when the frame is deleted. */
522
523 static void
524 initial_free_frame_resources (struct frame *f)
525 {
526 eassert (FRAME_INITIAL_P (f));
527 free_frame_faces (f);
528 }
529
530 /* Create the bootstrap display terminal for the initial frame.
531 Returns a terminal of type output_initial. */
532
533 struct terminal *
534 init_initial_terminal (void)
535 {
536 if (initialized || terminal_list || tty_list)
537 emacs_abort ();
538
539 initial_terminal = create_terminal (output_initial, NULL);
540 initial_terminal->name = xstrdup ("initial_terminal");
541 initial_terminal->kboard = initial_kboard;
542 initial_terminal->delete_terminal_hook = &delete_initial_terminal;
543 initial_terminal->delete_frame_hook = &initial_free_frame_resources;
544 /* Other hooks are NULL by default. */
545
546 return initial_terminal;
547 }
548
549 /* Deletes the bootstrap terminal device.
550 Called through delete_terminal_hook. */
551
552 static void
553 delete_initial_terminal (struct terminal *terminal)
554 {
555 if (terminal != initial_terminal)
556 emacs_abort ();
557
558 delete_terminal (terminal);
559 initial_terminal = NULL;
560 }
561
562 void
563 syms_of_terminal (void)
564 {
565
566 DEFVAR_LISP ("ring-bell-function", Vring_bell_function,
567 doc: /* Non-nil means call this function to ring the bell.
568 The function should accept no arguments. */);
569 Vring_bell_function = Qnil;
570
571 DEFVAR_LISP ("delete-terminal-functions", Vdelete_terminal_functions,
572 doc: /* Special hook run when a terminal is deleted.
573 Each function is called with argument, the terminal.
574 This may be called just before actually deleting the terminal,
575 or some time later. */);
576 Vdelete_terminal_functions = Qnil;
577
578 DEFSYM (Qterminal_live_p, "terminal-live-p");
579 DEFSYM (Qdelete_terminal_functions, "delete-terminal-functions");
580 DEFSYM (Qrun_hook_with_args, "run-hook-with-args");
581
582 defsubr (&Sdelete_terminal);
583 defsubr (&Sframe_terminal);
584 defsubr (&Sterminal_live_p);
585 defsubr (&Sterminal_list);
586 defsubr (&Sterminal_name);
587 defsubr (&Sterminal_parameters);
588 defsubr (&Sterminal_parameter);
589 defsubr (&Sset_terminal_parameter);
590
591 Fprovide (intern_c_string ("multi-tty"), Qnil);
592 }