]> code.delx.au - gnu-emacs/blob - src/terminal.c
Merge from emacs--devo--0
[gnu-emacs] / src / terminal.c
1 /* Functions related to terminal devices.
2 Copyright (C) 2005, 2006, 2007, 2008 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 (TERMINALP (terminal))
209 result = XTERMINAL (terminal);
210
211 else if (FRAMEP (terminal))
212 {
213 result = FRAME_TERMINAL (XFRAME (terminal));
214 }
215
216 if (result && !result->name)
217 result = NULL;
218
219 if (result == NULL && throw)
220 wrong_type_argument (Qterminal_live_p, terminal);
221
222 return result;
223 }
224
225 \f
226
227 /* Create a new terminal object and add it to the terminal list. */
228
229 struct terminal *
230 create_terminal (void)
231 {
232 struct terminal *terminal = allocate_terminal ();
233
234 terminal->name = NULL;
235 terminal->next_terminal = terminal_list;
236 terminal_list = terminal;
237
238 terminal->id = next_terminal_id++;
239
240 terminal->keyboard_coding =
241 (struct coding_system *) xmalloc (sizeof (struct coding_system));
242 terminal->terminal_coding =
243 (struct coding_system *) xmalloc (sizeof (struct coding_system));
244
245 setup_coding_system (Qno_conversion, terminal->keyboard_coding);
246 setup_coding_system (Qundecided, terminal->terminal_coding);
247
248 terminal->param_alist = Qnil;
249 return terminal;
250 }
251
252 /* Low-level function to close all frames on a terminal, remove it
253 from the terminal list and free its memory. */
254
255 void
256 delete_terminal (struct terminal *terminal)
257 {
258 struct terminal **tp;
259 Lisp_Object tail, frame;
260
261 /* Protect against recursive calls. Fdelete_frame calls the
262 delete_terminal_hook when we delete our last frame. */
263 if (!terminal->name)
264 return;
265 xfree (terminal->name);
266 terminal->name = NULL;
267
268 /* Check for live frames that are still on this terminal. */
269 FOR_EACH_FRAME (tail, frame)
270 {
271 struct frame *f = XFRAME (frame);
272 if (FRAME_LIVE_P (f) && f->terminal == terminal)
273 {
274 Fdelete_frame (frame, Qt);
275 }
276 }
277
278 for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
279 if (! *tp)
280 abort ();
281 *tp = terminal->next_terminal;
282
283 xfree (terminal->keyboard_coding);
284 terminal->keyboard_coding = NULL;
285 xfree (terminal->terminal_coding);
286 terminal->terminal_coding = NULL;
287
288 #ifdef MULTI_KBOARD
289 if (terminal->kboard && --terminal->kboard->reference_count == 0)
290 {
291 delete_kboard (terminal->kboard);
292 terminal->kboard = NULL;
293 }
294 #endif
295 }
296
297 DEFUN ("delete-terminal", Fdelete_terminal, Sdelete_terminal, 0, 2, 0,
298 doc: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
299 TERMINAL may be a terminal id, a frame, or nil (meaning the selected
300 frame's terminal).
301
302 Normally, you may not delete a display if all other displays are suspended,
303 but if the second argument FORCE is non-nil, you may do so. */)
304 (terminal, force)
305 Lisp_Object terminal, force;
306 {
307 struct terminal *t, *p;
308
309 t = get_terminal (terminal, 0);
310
311 if (!t)
312 return Qnil;
313
314 p = terminal_list;
315 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
316 p = p->next_terminal;
317
318 if (NILP (force) && !p)
319 error ("Attempt to delete the sole active display terminal");
320
321 if (t->delete_terminal_hook)
322 (*t->delete_terminal_hook) (t);
323 else
324 delete_terminal (t);
325
326 return Qnil;
327 }
328
329 \f
330 DEFUN ("frame-terminal", Fframe_terminal, Sframe_terminal, 0, 1, 0,
331 doc: /* Return the terminal that FRAME is displayed on.
332 If FRAME is nil, the selected frame is used.
333
334 The terminal device is represented by its integer identifier. */)
335 (frame)
336 Lisp_Object frame;
337 {
338 struct terminal *t;
339
340 if (NILP (frame))
341 frame = selected_frame;
342
343 CHECK_LIVE_FRAME (frame);
344
345 t = FRAME_TERMINAL (XFRAME (frame));
346
347 if (!t)
348 return Qnil;
349 else
350 {
351 Lisp_Object terminal;
352 XSETTERMINAL (terminal, t);
353 return terminal;
354 }
355 }
356
357 DEFUN ("terminal-live-p", Fterminal_live_p, Sterminal_live_p, 1, 1, 0,
358 doc: /* Return non-nil if OBJECT is a terminal which has not been deleted.
359 Value is nil if OBJECT is not a live display terminal.
360 If object is a live display terminal, the return value indicates what
361 sort of output terminal it uses. See the documentation of `framep' for
362 possible return values. */)
363 (object)
364 Lisp_Object object;
365 {
366 struct terminal *t;
367
368 t = get_terminal (object, 0);
369
370 if (!t)
371 return Qnil;
372
373 switch (t->type)
374 {
375 case output_initial: /* The initial frame is like a termcap frame. */
376 case output_termcap:
377 return Qt;
378 case output_x_window:
379 return Qx;
380 case output_w32:
381 return Qw32;
382 case output_msdos_raw:
383 return Qpc;
384 case output_mac:
385 return Qmac;
386 default:
387 abort ();
388 }
389 }
390
391 DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
392 doc: /* Return a list of all terminal devices. */)
393 ()
394 {
395 Lisp_Object terminal, terminals = Qnil;
396 struct terminal *t;
397
398 for (t = terminal_list; t; t = t->next_terminal)
399 {
400 XSETTERMINAL (terminal, t);
401 terminals = Fcons (terminal, terminals);
402 }
403
404 return terminals;
405 }
406
407 DEFUN ("terminal-name", Fterminal_name, Sterminal_name, 0, 1, 0,
408 doc: /* Return the name of the terminal device TERMINAL.
409 It is not guaranteed that the returned value is unique among opened devices.
410
411 TERMINAL may be a terminal id, a frame, or nil (meaning the
412 selected frame's terminal). */)
413 (terminal)
414 Lisp_Object terminal;
415 {
416 struct terminal *t
417 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
418
419 return t->name ? build_string (t->name) : Qnil;
420 }
421
422
423 \f
424 /* Return the value of terminal parameter PARAM in terminal T. */
425 Lisp_Object
426 get_terminal_param (t, param)
427 struct terminal *t;
428 Lisp_Object param;
429 {
430 Lisp_Object tem = Fassq (param, t->param_alist);
431 if (EQ (tem, Qnil))
432 return tem;
433 return Fcdr (tem);
434 }
435
436 /* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
437 Return the previous value. */
438
439 Lisp_Object
440 store_terminal_param (t, parameter, value)
441 struct terminal *t;
442 Lisp_Object parameter;
443 Lisp_Object value;
444 {
445 Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
446 if (EQ (old_alist_elt, Qnil))
447 {
448 t->param_alist = Fcons (Fcons (parameter, value), t->param_alist);
449 return Qnil;
450 }
451 else
452 {
453 Lisp_Object result = Fcdr (old_alist_elt);
454 Fsetcdr (old_alist_elt, value);
455 return result;
456 }
457 }
458
459
460 DEFUN ("terminal-parameters", Fterminal_parameters, Sterminal_parameters, 0, 1, 0,
461 doc: /* Return the parameter-alist of terminal TERMINAL.
462 The value is a list of elements of the form (PARM . VALUE), where PARM
463 is a symbol.
464
465 TERMINAL can be a terminal id, a frame or nil (meaning the selected
466 frame's terminal). */)
467 (terminal)
468 Lisp_Object terminal;
469 {
470 struct terminal *t
471 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
472 return Fcopy_alist (t->param_alist);
473 }
474
475 DEFUN ("terminal-parameter", Fterminal_parameter, Sterminal_parameter, 2, 2, 0,
476 doc: /* Return TERMINAL's value for parameter PARAMETER.
477 TERMINAL can be a terminal id, a frame or nil (meaning the selected
478 frame's terminal). */)
479 (terminal, parameter)
480 Lisp_Object terminal;
481 Lisp_Object parameter;
482 {
483 Lisp_Object value;
484 struct terminal *t
485 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
486 CHECK_SYMBOL (parameter);
487 value = Fcdr (Fassq (parameter, t->param_alist));
488 return value;
489 }
490
491 DEFUN ("set-terminal-parameter", Fset_terminal_parameter,
492 Sset_terminal_parameter, 3, 3, 0,
493 doc: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
494 Return the previous value of PARAMETER.
495
496 TERMINAL can be a terminal id, a frame or nil (meaning the selected
497 frame's terminal). */)
498 (terminal, parameter, value)
499 Lisp_Object terminal;
500 Lisp_Object parameter;
501 Lisp_Object value;
502 {
503 struct terminal *t
504 = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
505 return store_terminal_param (t, parameter, value);
506 }
507
508 \f
509
510 /* Create the bootstrap display terminal for the initial frame.
511 Returns a terminal of type output_initial. */
512
513 struct terminal *
514 init_initial_terminal (void)
515 {
516 if (initialized || terminal_list || tty_list)
517 abort ();
518
519 initial_terminal = create_terminal ();
520 initial_terminal->type = output_initial;
521 initial_terminal->name = xstrdup ("initial_terminal");
522 #ifdef MULTI_KBOARD
523 initial_terminal->kboard = initial_kboard;
524 #endif
525 initial_terminal->delete_terminal_hook = &delete_initial_terminal;
526 /* All other hooks are NULL. */
527
528 return initial_terminal;
529 }
530
531 /* Deletes the bootstrap terminal device.
532 Called through delete_terminal_hook. */
533
534 static void
535 delete_initial_terminal (struct terminal *terminal)
536 {
537 if (terminal != initial_terminal)
538 abort ();
539
540 delete_terminal (terminal);
541 initial_terminal = NULL;
542 }
543
544 void
545 syms_of_terminal ()
546 {
547
548 DEFVAR_LISP ("ring-bell-function", &Vring_bell_function,
549 doc: /* Non-nil means call this function to ring the bell.
550 The function should accept no arguments. */);
551 Vring_bell_function = Qnil;
552
553 defsubr (&Sdelete_terminal);
554 defsubr (&Sframe_terminal);
555 defsubr (&Sterminal_live_p);
556 defsubr (&Sterminal_list);
557 defsubr (&Sterminal_name);
558 defsubr (&Sterminal_parameters);
559 defsubr (&Sterminal_parameter);
560 defsubr (&Sset_terminal_parameter);
561
562 Fprovide (intern ("multi-tty"), Qnil);
563 }
564
565 /* arch-tag: e9af6f27-b483-47dc-bb1a-730c1c5cab03
566 (do not change this comment) */