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