Update copyright years, update license to GPLv3 or later.
[bpt/emacs.git] / src / terminal.c
1 /* Functions related to terminal devices.
2 Copyright (C) 2005, 2006, 2007 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, or (at your option)
9 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; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
20
21 #include <config.h>
22 #include <stdio.h>
23
24 #include "lisp.h"
25 #include "frame.h"
26 #include "termchar.h"
27 #include "termhooks.h"
28 #include "charset.h"
29 #include "coding.h"
30 #include "keyboard.h"
31
32 /* Chain of all terminals currently in use. */
33 struct terminal *terminal_list;
34
35 /* The first unallocated terminal id. */
36 static int next_terminal_id;
37
38 /* The initial terminal device, created by initial_term_init. */
39 struct terminal *initial_terminal;
40
41 /* Function to use to ring the bell. */
42 Lisp_Object Vring_bell_function;
43
44 static void delete_initial_terminal P_ ((struct terminal *));
45
46 \f
47
48 void
49 ring_bell (struct frame *f)
50 {
51 if (!NILP (Vring_bell_function))
52 {
53 Lisp_Object function;
54
55 /* Temporarily set the global variable to nil
56 so that if we get an error, it stays nil
57 and we don't call it over and over.
58
59 We don't specbind it, because that would carefully
60 restore the bad value if there's an error
61 and make the loop of errors happen anyway. */
62
63 function = Vring_bell_function;
64 Vring_bell_function = Qnil;
65
66 call0 (function);
67
68 Vring_bell_function = function;
69 }
70 else if (FRAME_TERMINAL (f)->ring_bell_hook)
71 (*FRAME_TERMINAL (f)->ring_bell_hook) (f);
72 }
73
74 void
75 update_begin (struct frame *f)
76 {
77 if (FRAME_TERMINAL (f)->update_begin_hook)
78 (*FRAME_TERMINAL (f)->update_begin_hook) (f);
79 }
80
81 void
82 update_end (struct frame *f)
83 {
84 if (FRAME_TERMINAL (f)->update_end_hook)
85 (*FRAME_TERMINAL (f)->update_end_hook) (f);
86 }
87
88 /* Specify how many text lines, from the top of the window,
89 should be affected by insert-lines and delete-lines operations.
90 This, and those operations, are used only within an update
91 that is bounded by calls to update_begin and update_end. */
92
93 void
94 set_terminal_window (struct frame *f, int size)
95 {
96 if (FRAME_TERMINAL (f)->set_terminal_window_hook)
97 (*FRAME_TERMINAL (f)->set_terminal_window_hook) (f, size);
98 }
99
100 /* Move cursor to row/column position VPOS/HPOS. HPOS/VPOS are
101 frame-relative coordinates. */
102
103 void
104 cursor_to (struct frame *f, int vpos, int hpos)
105 {
106 if (FRAME_TERMINAL (f)->cursor_to_hook)
107 (*FRAME_TERMINAL (f)->cursor_to_hook) (f, vpos, hpos);
108 }
109
110 /* Similar but don't take any account of the wasted characters. */
111
112 void
113 raw_cursor_to (struct frame *f, int row, int col)
114 {
115 if (FRAME_TERMINAL (f)->raw_cursor_to_hook)
116 (*FRAME_TERMINAL (f)->raw_cursor_to_hook) (f, row, col);
117 }
118
119 /* Erase operations */
120
121 /* Clear from cursor to end of frame. */
122 void
123 clear_to_end (struct frame *f)
124 {
125 if (FRAME_TERMINAL (f)->clear_to_end_hook)
126 (*FRAME_TERMINAL (f)->clear_to_end_hook) (f);
127 }
128
129 /* Clear entire frame */
130
131 void
132 clear_frame (struct frame *f)
133 {
134 if (FRAME_TERMINAL (f)->clear_frame_hook)
135 (*FRAME_TERMINAL (f)->clear_frame_hook) (f);
136 }
137
138 /* Clear from cursor to end of line.
139 Assume that the line is already clear starting at column first_unused_hpos.
140
141 Note that the cursor may be moved, on terminals lacking a `ce' string. */
142
143 void
144 clear_end_of_line (struct frame *f, int first_unused_hpos)
145 {
146 if (FRAME_TERMINAL (f)->clear_end_of_line_hook)
147 (*FRAME_TERMINAL (f)->clear_end_of_line_hook) (f, first_unused_hpos);
148 }
149
150 /* Output LEN glyphs starting at STRING at the nominal cursor position.
151 Advance the nominal cursor over the text. */
152
153 void
154 write_glyphs (struct frame *f, struct glyph *string, int len)
155 {
156 if (FRAME_TERMINAL (f)->write_glyphs_hook)
157 (*FRAME_TERMINAL (f)->write_glyphs_hook) (f, string, len);
158 }
159
160 /* Insert LEN glyphs from START at the nominal cursor position.
161
162 If start is zero, insert blanks instead of a string at start */
163
164 void
165 insert_glyphs (struct frame *f, struct glyph *start, int len)
166 {
167 if (len <= 0)
168 return;
169
170 if (FRAME_TERMINAL (f)->insert_glyphs_hook)
171 (*FRAME_TERMINAL (f)->insert_glyphs_hook) (f, start, len);
172 }
173
174 /* Delete N glyphs at the nominal cursor position. */
175
176 void
177 delete_glyphs (struct frame *f, int n)
178 {
179 if (FRAME_TERMINAL (f)->delete_glyphs_hook)
180 (*FRAME_TERMINAL (f)->delete_glyphs_hook) (f, n);
181 }
182
183 /* Insert N lines at vpos VPOS. If N is negative, delete -N lines. */
184
185 void
186 ins_del_lines (struct frame *f, int vpos, int n)
187 {
188 if (FRAME_TERMINAL (f)->ins_del_lines_hook)
189 (*FRAME_TERMINAL (f)->ins_del_lines_hook) (f, vpos, n);
190 }
191
192
193 \f
194
195 /* Return the terminal object specified by TERMINAL. TERMINAL may be a
196 terminal id, a frame, or nil for the terminal device of the current
197 frame. If THROW is zero, return NULL for failure, otherwise throw
198 an error. */
199
200 struct terminal *
201 get_terminal (Lisp_Object terminal, int throw)
202 {
203 struct terminal *result = NULL;
204
205 if (NILP (terminal))
206 terminal = selected_frame;
207
208 if (INTEGERP (terminal))
209 {
210 struct terminal *t;
211
212 for (t = terminal_list; t; t = t->next_terminal)
213 {
214 if (t->id == XINT (terminal))
215 {
216 result = t;
217 break;
218 }
219 }
220 }
221 else if (FRAMEP (terminal))
222 {
223 result = FRAME_TERMINAL (XFRAME (terminal));
224 }
225
226 if (result == NULL && throw)
227 wrong_type_argument (Qterminal_live_p, terminal);
228
229 return result;
230 }
231
232 \f
233
234 /* Create a new terminal object and add it to the terminal list. */
235
236 struct terminal *
237 create_terminal (void)
238 {
239 struct terminal *terminal = (struct terminal *) xmalloc (sizeof (struct terminal));
240
241 bzero (terminal, sizeof (struct terminal));
242 terminal->next_terminal = terminal_list;
243 terminal_list = terminal;
244
245 terminal->id = next_terminal_id++;
246
247 terminal->keyboard_coding =
248 (struct coding_system *) xmalloc (sizeof (struct coding_system));
249 terminal->terminal_coding =
250 (struct coding_system *) xmalloc (sizeof (struct coding_system));
251
252 setup_coding_system (Qnil, terminal->keyboard_coding);
253 setup_coding_system (Qnil, terminal->terminal_coding);
254
255 terminal->param_alist = Qnil;
256 return terminal;
257 }
258
259 /* Mark the Lisp pointers in the terminal objects.
260 Called by the Fgarbage_collector. */
261
262 void
263 mark_terminals (void)
264 {
265 struct terminal *t;
266 for (t = terminal_list; t; t = t->next_terminal)
267 {
268 mark_object (t->param_alist);
269 }
270 }
271
272
273 /* Low-level function to close all frames on a terminal, remove it
274 from the terminal list and free its memory. */
275
276 void
277 delete_terminal (struct terminal *terminal)
278 {
279 struct terminal **tp;
280 Lisp_Object tail, frame;
281
282 /* Protect against recursive calls. Fdelete_frame calls the
283 delete_terminal_hook when we delete our last frame. */
284 if (terminal->deleted)
285 return;
286 terminal->deleted = 1;
287
288 /* Check for live frames that are still on this terminal. */
289 FOR_EACH_FRAME (tail, frame)
290 {
291 struct frame *f = XFRAME (frame);
292 if (FRAME_LIVE_P (f) && f->terminal == terminal)
293 {
294 Fdelete_frame (frame, Qt);
295 }
296 }
297
298 for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
299 if (! *tp)
300 abort ();
301 *tp = terminal->next_terminal;
302
303 if (terminal->keyboard_coding)
304 xfree (terminal->keyboard_coding);
305 if (terminal->terminal_coding)
306 xfree (terminal->terminal_coding);
307 if (terminal->name)
308 xfree (terminal->name);
309
310 #ifdef MULTI_KBOARD
311 if (terminal->kboard && --terminal->kboard->reference_count == 0)
312 delete_kboard (terminal->kboard);
313 #endif
314
315 bzero (terminal, sizeof (struct terminal));
316 xfree (terminal);
317 }
318
319 DEFUN ("delete-terminal", Fdelete_terminal, Sdelete_terminal, 0, 2, 0,
320 doc: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
321 TERMINAL may be a terminal id, a frame, or nil (meaning the selected
322 frame's terminal).
323
324 Normally, you may not delete a display if all other displays are suspended,
325 but if the second argument FORCE is non-nil, you may do so. */)
326 (terminal, force)
327 Lisp_Object terminal, force;
328 {
329 struct terminal *t, *p;
330
331 t = get_terminal (terminal, 0);
332
333 if (!t)
334 return Qnil;
335
336 p = terminal_list;
337 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
338 p = p->next_terminal;
339
340 if (NILP (force) && !p)
341 error ("Attempt to delete the sole active display terminal");
342
343 if (t->delete_terminal_hook)
344 (*t->delete_terminal_hook) (t);
345 else
346 delete_terminal (t);
347
348 return Qnil;
349 }
350
351 \f
352 DEFUN ("frame-terminal", Fframe_terminal, Sframe_terminal, 0, 1, 0,
353 doc: /* Return the terminal that FRAME is displayed on.
354 If FRAME is nil, the selected frame is used.
355
356 The terminal device is represented by its integer identifier. */)
357 (frame)
358 Lisp_Object frame;
359 {
360 struct terminal *t;
361
362 if (NILP (frame))
363 frame = selected_frame;
364
365 CHECK_LIVE_FRAME (frame);
366
367 t = get_terminal (frame, 0);
368
369 if (!t)
370 return Qnil;
371 else
372 return make_number (t->id);
373 }
374
375 DEFUN ("terminal-live-p", Fterminal_live_p, Sterminal_live_p, 1, 1, 0,
376 doc: /* Return non-nil if OBJECT is a terminal which has not been deleted.
377 Value is nil if OBJECT is not a live display terminal.
378 If object is a live display terminal, the return value indicates what
379 sort of output terminal it uses. See the documentation of `framep' for
380 possible return values.
381
382 Display terminals are represented by their integer identifiers. */)
383 (object)
384 Lisp_Object object;
385 {
386 struct terminal *t;
387
388 if (!INTEGERP (object))
389 return Qnil;
390
391 t = get_terminal (object, 0);
392
393 if (!t)
394 return Qnil;
395
396 switch (t->type)
397 {
398 case output_initial: /* The initial frame is like a termcap frame. */
399 case output_termcap:
400 return Qt;
401 case output_x_window:
402 return Qx;
403 case output_w32:
404 return Qw32;
405 case output_msdos_raw:
406 return Qpc;
407 case output_mac:
408 return Qmac;
409 default:
410 abort ();
411 }
412 }
413
414 DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
415 doc: /* Return a list of all terminal devices.
416 Terminal devices are represented by their integer identifiers. */)
417 ()
418 {
419 Lisp_Object terminals = Qnil;
420 struct terminal *t;
421
422 for (t = terminal_list; t; t = t->next_terminal)
423 terminals = Fcons (make_number (t->id), terminals);
424
425 return terminals;
426 }
427
428 DEFUN ("terminal-name", Fterminal_name, Sterminal_name, 0, 1, 0,
429 doc: /* Return the name of the terminal device TERMINAL.
430 It is not guaranteed that the returned value is unique among opened devices.
431
432 TERMINAL may be a terminal id, a frame, or nil (meaning the
433 selected frame's terminal). */)
434 (terminal)
435 Lisp_Object terminal;
436 {
437 struct terminal *t = get_terminal (terminal, 1);
438
439 if (t->name)
440 return build_string (t->name);
441 else
442 return Qnil;
443 }
444
445
446 \f
447 /* Return the value of terminal parameter PARAM in terminal T. */
448 Lisp_Object
449 get_terminal_param (t, param)
450 struct terminal *t;
451 Lisp_Object param;
452 {
453 Lisp_Object tem = Fassq (param, t->param_alist);
454 if (EQ (tem, Qnil))
455 return tem;
456 return Fcdr (tem);
457 }
458
459 /* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
460 Return the previous value. */
461
462 Lisp_Object
463 store_terminal_param (t, parameter, value)
464 struct terminal *t;
465 Lisp_Object parameter;
466 Lisp_Object value;
467 {
468 Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
469 if (EQ (old_alist_elt, Qnil))
470 {
471 t->param_alist = Fcons (Fcons (parameter, value), t->param_alist);
472 return Qnil;
473 }
474 else
475 {
476 Lisp_Object result = Fcdr (old_alist_elt);
477 Fsetcdr (old_alist_elt, value);
478 return result;
479 }
480 }
481
482
483 DEFUN ("terminal-parameters", Fterminal_parameters, Sterminal_parameters, 0, 1, 0,
484 doc: /* Return the parameter-alist of terminal TERMINAL.
485 The value is a list of elements of the form (PARM . VALUE), where PARM
486 is a symbol.
487
488 TERMINAL can be a terminal id, a frame or nil (meaning the selected
489 frame's terminal). */)
490 (terminal)
491 Lisp_Object terminal;
492 {
493 struct terminal *t = get_terminal (terminal, 1);
494 return Fcopy_alist (t->param_alist);
495 }
496
497 DEFUN ("terminal-parameter", Fterminal_parameter, Sterminal_parameter, 2, 2, 0,
498 doc: /* Return TERMINAL's value for parameter PARAMETER.
499 TERMINAL can be a terminal id, a frame or nil (meaning the selected
500 frame's terminal). */)
501 (terminal, parameter)
502 Lisp_Object terminal;
503 Lisp_Object parameter;
504 {
505 Lisp_Object value;
506 struct terminal *t = get_terminal (terminal, 1);
507 CHECK_SYMBOL (parameter);
508 value = Fcdr (Fassq (parameter, t->param_alist));
509 return value;
510 }
511
512 DEFUN ("modify-terminal-parameters", Fmodify_terminal_parameters,
513 Smodify_terminal_parameters, 2, 2, 0,
514 doc: /* Modify the parameters of terminal TERMINAL according to ALIST.
515 ALIST is an alist of parameters to change and their new values.
516 Each element of ALIST has the form (PARM . VALUE), where PARM is a symbol.
517
518 TERMINAL can be a terminal id, a frame or nil (meaning the selected
519 frame's terminal). */)
520 (terminal, alist)
521 Lisp_Object terminal;
522 Lisp_Object alist;
523 {
524 Lisp_Object tail, prop, val;
525 struct terminal *t = get_terminal (terminal, 1);
526 int length = XINT (Fsafe_length (alist));
527 int i;
528 Lisp_Object *parms = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
529 Lisp_Object *values = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
530
531 /* Extract parm names and values into those vectors. */
532
533 i = 0;
534 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
535 {
536 Lisp_Object elt;
537
538 elt = Fcar (tail);
539 parms[i] = Fcar (elt);
540 values[i] = Fcdr (elt);
541 i++;
542 }
543
544 /* Now process them in reverse of specified order. */
545 for (i--; i >= 0; i--)
546 {
547 prop = parms[i];
548 val = values[i];
549 store_terminal_param (t, prop, val);
550 }
551 return Qnil;
552 }
553
554 DEFUN ("set-terminal-parameter", Fset_terminal_parameter,
555 Sset_terminal_parameter, 3, 3, 0,
556 doc: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
557 Return the previous value of PARAMETER.
558
559 TERMINAL can be a terminal id, a frame or nil (meaning the selected
560 frame's terminal). */)
561 (terminal, parameter, value)
562 Lisp_Object terminal;
563 Lisp_Object parameter;
564 Lisp_Object value;
565 {
566 struct terminal *t = get_terminal (terminal, 1);
567 return store_terminal_param (t, parameter, value);
568 }
569
570 \f
571
572 /* Create the bootstrap display terminal for the initial frame.
573 Returns a terminal of type output_initial. */
574
575 struct terminal *
576 init_initial_terminal (void)
577 {
578 if (initialized || terminal_list || tty_list)
579 abort ();
580
581 initial_terminal = create_terminal ();
582 initial_terminal->type = output_initial;
583 initial_terminal->name = xstrdup ("initial_terminal");
584 #ifdef MULTI_KBOARD
585 initial_terminal->kboard = initial_kboard;
586 #endif
587 initial_terminal->delete_terminal_hook = &delete_initial_terminal;
588 /* All other hooks are NULL. */
589
590 return initial_terminal;
591 }
592
593 /* Deletes the bootstrap terminal device.
594 Called through delete_terminal_hook. */
595
596 static void
597 delete_initial_terminal (struct terminal *terminal)
598 {
599 if (terminal != initial_terminal)
600 abort ();
601
602 delete_terminal (terminal);
603 initial_terminal = NULL;
604 }
605
606 void
607 syms_of_terminal ()
608 {
609
610 DEFVAR_LISP ("ring-bell-function", &Vring_bell_function,
611 doc: /* Non-nil means call this function to ring the bell.
612 The function should accept no arguments. */);
613 Vring_bell_function = Qnil;
614
615 defsubr (&Sdelete_terminal);
616 defsubr (&Sframe_terminal);
617 defsubr (&Sterminal_live_p);
618 defsubr (&Sterminal_list);
619 defsubr (&Sterminal_name);
620 defsubr (&Sterminal_parameters);
621 defsubr (&Sterminal_parameter);
622 defsubr (&Smodify_terminal_parameters);
623 defsubr (&Sset_terminal_parameter);
624
625 Fprovide (intern ("multi-tty"), Qnil);
626 }
627
628 /* arch-tag: e9af6f27-b483-47dc-bb1a-730c1c5cab03
629 (do not change this comment) */