terminal.c 15.2 KB
Newer Older
1
/* Functions related to terminal devices.
Glenn Morris's avatar
Glenn Morris committed
2
   Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3 4 5

This file is part of GNU Emacs.

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 9
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
10 11 12 13 14 15 16

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

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 22 23 24 25 26 27 28 29

#include "lisp.h"
#include "frame.h"
#include "termchar.h"
#include "termhooks.h"
#include "charset.h"
#include "coding.h"
#include "keyboard.h"

30 31
/* Chain of all terminals currently in use. */
struct terminal *terminal_list;
32

33 34
/* The first unallocated terminal id. */
static int next_terminal_id;
35

36 37
/* The initial terminal device, created by initial_term_init. */
struct terminal *initial_terminal;
38 39 40 41

/* Function to use to ring the bell.  */
Lisp_Object Vring_bell_function;

42
static void delete_initial_terminal P_ ((struct terminal *));
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67



void
ring_bell (struct frame *f)
{
  if (!NILP (Vring_bell_function))
    {
      Lisp_Object function;

      /* Temporarily set the global variable to nil
	 so that if we get an error, it stays nil
	 and we don't call it over and over.

	 We don't specbind it, because that would carefully
	 restore the bad value if there's an error
	 and make the loop of errors happen anyway.  */

      function = Vring_bell_function;
      Vring_bell_function = Qnil;

      call0 (function);

      Vring_bell_function = function;
    }
68 69
  else if (FRAME_TERMINAL (f)->ring_bell_hook)
    (*FRAME_TERMINAL (f)->ring_bell_hook) (f);
70 71 72 73 74
}

void
update_begin (struct frame *f)
{
75 76
  if (FRAME_TERMINAL (f)->update_begin_hook)
    (*FRAME_TERMINAL (f)->update_begin_hook) (f);
77 78 79 80 81
}

void
update_end (struct frame *f)
{
82 83
  if (FRAME_TERMINAL (f)->update_end_hook)
    (*FRAME_TERMINAL (f)->update_end_hook) (f);
84 85 86 87 88 89 90 91 92 93
}

/* Specify how many text lines, from the top of the window,
   should be affected by insert-lines and delete-lines operations.
   This, and those operations, are used only within an update
   that is bounded by calls to update_begin and update_end.  */

void
set_terminal_window (struct frame *f, int size)
{
94 95
  if (FRAME_TERMINAL (f)->set_terminal_window_hook)
    (*FRAME_TERMINAL (f)->set_terminal_window_hook) (f, size);
96 97 98 99 100 101 102 103
}

/* Move cursor to row/column position VPOS/HPOS.  HPOS/VPOS are
   frame-relative coordinates.  */

void
cursor_to (struct frame *f, int vpos, int hpos)
{
104 105
  if (FRAME_TERMINAL (f)->cursor_to_hook)
    (*FRAME_TERMINAL (f)->cursor_to_hook) (f, vpos, hpos);
106 107 108 109 110 111 112
}

/* Similar but don't take any account of the wasted characters.  */

void
raw_cursor_to (struct frame *f, int row, int col)
{
113 114
  if (FRAME_TERMINAL (f)->raw_cursor_to_hook)
    (*FRAME_TERMINAL (f)->raw_cursor_to_hook) (f, row, col);  
115 116 117 118 119 120 121 122
}

/* Erase operations */

/* Clear from cursor to end of frame. */
void
clear_to_end (struct frame *f)
{
123 124
  if (FRAME_TERMINAL (f)->clear_to_end_hook)
    (*FRAME_TERMINAL (f)->clear_to_end_hook) (f);
125 126 127 128 129 130 131
}

/* Clear entire frame */

void
clear_frame (struct frame *f)
{
132 133
  if (FRAME_TERMINAL (f)->clear_frame_hook)
    (*FRAME_TERMINAL (f)->clear_frame_hook) (f);
134 135 136 137 138 139 140 141 142 143
}

/* Clear from cursor to end of line.
   Assume that the line is already clear starting at column first_unused_hpos.

   Note that the cursor may be moved, on terminals lacking a `ce' string.  */

void
clear_end_of_line (struct frame *f, int first_unused_hpos)
{
144 145
  if (FRAME_TERMINAL (f)->clear_end_of_line_hook)
    (*FRAME_TERMINAL (f)->clear_end_of_line_hook) (f, first_unused_hpos);
146 147 148 149 150 151 152 153
}

/* Output LEN glyphs starting at STRING at the nominal cursor position.
   Advance the nominal cursor over the text.  */

void
write_glyphs (struct frame *f, struct glyph *string, int len)
{
154 155
  if (FRAME_TERMINAL (f)->write_glyphs_hook)
    (*FRAME_TERMINAL (f)->write_glyphs_hook) (f, string, len);
156 157 158 159 160 161 162 163 164 165 166 167
}

/* Insert LEN glyphs from START at the nominal cursor position.

   If start is zero, insert blanks instead of a string at start */

void
insert_glyphs (struct frame *f, struct glyph *start, int len)
{
  if (len <= 0)
    return;

168 169
  if (FRAME_TERMINAL (f)->insert_glyphs_hook)
    (*FRAME_TERMINAL (f)->insert_glyphs_hook) (f, start, len);
170 171 172 173 174 175 176
}

/* Delete N glyphs at the nominal cursor position. */

void
delete_glyphs (struct frame *f, int n)
{
177 178
  if (FRAME_TERMINAL (f)->delete_glyphs_hook)
    (*FRAME_TERMINAL (f)->delete_glyphs_hook) (f, n);
179 180 181 182 183 184 185
}

/* Insert N lines at vpos VPOS.  If N is negative, delete -N lines.  */

void
ins_del_lines (struct frame *f, int vpos, int n)
{
186 187
  if (FRAME_TERMINAL (f)->ins_del_lines_hook)
    (*FRAME_TERMINAL (f)->ins_del_lines_hook) (f, vpos, n);
188 189 190 191 192
}




193 194
/* Return the terminal object specified by TERMINAL.  TERMINAL may be a
   terminal id, a frame, or nil for the terminal device of the current
195 196 197
   frame.  If THROW is zero, return NULL for failure, otherwise throw
   an error.  */

198 199
struct terminal *
get_terminal (Lisp_Object terminal, int throw)
200
{
201
  struct terminal *result = NULL;
202

203 204
  if (NILP (terminal))
    terminal = selected_frame;
205

206 207 208
  if (TERMINALP (terminal))
    result = XTERMINAL (terminal);

209
  else if (FRAMEP (terminal))
210
    {
211
      result = FRAME_TERMINAL (XFRAME (terminal));
212 213
    }

214 215 216
  if (result && !result->name)
    result = NULL;

217
  if (result == NULL && throw)
218
    wrong_type_argument (Qterminal_live_p, terminal);
219 220 221 222 223 224

  return result;
}



225
/* Create a new terminal object and add it to the terminal list. */
226

227 228
struct terminal *
create_terminal (void)
229
{
230 231 232
  struct terminal *terminal = allocate_terminal ();

  terminal->name = NULL;
233 234
  terminal->next_terminal = terminal_list;
  terminal_list = terminal;
235

236
  terminal->id = next_terminal_id++;
237

238
  terminal->keyboard_coding =
239
    (struct coding_system *) xmalloc (sizeof (struct coding_system));
240
  terminal->terminal_coding =
241 242
    (struct coding_system *) xmalloc (sizeof (struct coding_system));

Miles Bader's avatar
Miles Bader committed
243 244
  setup_coding_system (Qno_conversion, terminal->keyboard_coding);
  setup_coding_system (Qundecided, terminal->terminal_coding);
245

246 247
  terminal->param_alist = Qnil;
  return terminal;
248 249
}

250 251
/* Low-level function to close all frames on a terminal, remove it
   from the terminal list and free its memory.  */
252 253

void
254
delete_terminal (struct terminal *terminal)
255
{
256
  struct terminal **tp;
257
  Lisp_Object tail, frame;
258

259
  /* Protect against recursive calls.  delete_frame calls the
260
     delete_terminal_hook when we delete our last frame.  */
261
  if (!terminal->name)
262
    return;
263 264
  xfree (terminal->name);
  terminal->name = NULL;
265

266
  /* Check for live frames that are still on this terminal. */
267 268 269
  FOR_EACH_FRAME (tail, frame)
    {
      struct frame *f = XFRAME (frame);
270
      if (FRAME_LIVE_P (f) && f->terminal == terminal)
271
        {
272 273
	  /* Pass Qnoelisp rather than Qt.  */
          delete_frame (frame, Qnoelisp);
274 275 276
        }
    }

277 278
  for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
    if (! *tp)
279
      abort ();
280 281
  *tp = terminal->next_terminal;

282 283 284 285
  xfree (terminal->keyboard_coding);
  terminal->keyboard_coding = NULL;
  xfree (terminal->terminal_coding);
  terminal->terminal_coding = NULL;
286

287
  if (terminal->kboard && --terminal->kboard->reference_count == 0)
288 289 290 291
    {
      delete_kboard (terminal->kboard);
      terminal->kboard = NULL;
    }
292 293
}

294 295 296 297
Lisp_Object Qrun_hook_with_args;
static Lisp_Object Qdelete_terminal_functions;
static Lisp_Object Vdelete_terminal_functions;

298 299
DEFUN ("delete-terminal", Fdelete_terminal, Sdelete_terminal, 0, 2, 0,
       doc: /* Delete TERMINAL by deleting all frames on it and closing the terminal.
300 301
TERMINAL may be a terminal object, a frame, or nil (meaning the
selected frame's terminal).
302 303 304

Normally, you may not delete a display if all other displays are suspended,
but if the second argument FORCE is non-nil, you may do so. */)
305 306
  (terminal, force)
     Lisp_Object terminal, force;
307
{
308
  struct terminal *t = get_terminal (terminal, 0);
309

310
  if (!t)
311 312
    return Qnil;

313 314 315 316 317
  if (NILP (force))
    {
      struct terminal *p = terminal_list;
      while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
	p = p->next_terminal;
318

319 320 321
      if (!p)
	error ("Attempt to delete the sole active display terminal");
    }
322

323 324 325 326 327 328 329 330 331 332
  if (NILP (Vrun_hooks))
    ;
  else if (EQ (force, Qnoelisp))
    pending_funcalls
      = Fcons (list3 (Qrun_hook_with_args,
		      Qdelete_terminal_functions, terminal),
	       pending_funcalls);
  else
    safe_call2 (Qrun_hook_with_args, Qdelete_terminal_functions, terminal);

333 334
  if (t->delete_terminal_hook)
    (*t->delete_terminal_hook) (t);
335
  else
336
    delete_terminal (t);
337 338 339 340

  return Qnil;
}

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

DEFUN ("frame-terminal", Fframe_terminal, Sframe_terminal, 0, 1, 0,
       doc: /* Return the terminal that FRAME is displayed on.
If FRAME is nil, the selected frame is used.

The terminal device is represented by its integer identifier.  */)
  (frame)
     Lisp_Object frame;
{
  struct terminal *t;

  if (NILP (frame))
    frame = selected_frame;

  CHECK_LIVE_FRAME (frame);

357
  t = FRAME_TERMINAL (XFRAME (frame));
358 359 360 361

  if (!t)
    return Qnil;
  else
362 363 364 365 366
    {
      Lisp_Object terminal;
      XSETTERMINAL (terminal, t);
      return terminal;
    }
367 368 369 370 371 372 373
}

DEFUN ("terminal-live-p", Fterminal_live_p, Sterminal_live_p, 1, 1, 0,
       doc: /* Return non-nil if OBJECT is a terminal which has not been deleted.
Value is nil if OBJECT is not a live display terminal.
If object is a live display terminal, the return value indicates what
sort of output terminal it uses.  See the documentation of `framep' for
374
possible return values.  */)
375 376 377
     (object)
     Lisp_Object object;
{
378
  struct terminal *t;
379

380
  t = get_terminal (object, 0);
381

382
  if (!t)
383 384
    return Qnil;

385
  switch (t->type)
386 387 388 389 390 391 392 393 394 395 396 397
    {
    case output_initial: /* The initial frame is like a termcap frame. */
    case output_termcap:
      return Qt;
    case output_x_window:
      return Qx;
    case output_w32:
      return Qw32;
    case output_msdos_raw:
      return Qpc;
    case output_mac:
      return Qmac;
398 399
    case output_ns:
      return Qns;
400 401 402 403 404
    default:
      abort ();
    }
}

405
DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
406
       doc: /* Return a list of all terminal devices.  */)
407 408
  ()
{
409
  Lisp_Object terminal, terminals = Qnil;
410
  struct terminal *t;
411

412
  for (t = terminal_list; t; t = t->next_terminal)
413 414 415 416
    {
      XSETTERMINAL (terminal, t);
      terminals = Fcons (terminal, terminals);
    }
417

418
  return terminals;
419 420
}

421 422
DEFUN ("terminal-name", Fterminal_name, Sterminal_name, 0, 1, 0,
       doc: /* Return the name of the terminal device TERMINAL.
423 424
It is not guaranteed that the returned value is unique among opened devices.

425
TERMINAL may be a terminal object, a frame, or nil (meaning the
426 427 428
selected frame's terminal). */)
  (terminal)
     Lisp_Object terminal;
429
{
430 431
  struct terminal *t
    = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
432

433
  return t->name ? build_string (t->name) : Qnil;
434 435 436 437
}



438
/* Return the value of terminal parameter PARAM in terminal T.  */
439
Lisp_Object
440 441
get_terminal_param (t, param)
     struct terminal *t;
442 443
     Lisp_Object param;
{
444
  Lisp_Object tem = Fassq (param, t->param_alist);
445 446 447 448 449
  if (EQ (tem, Qnil))
    return tem;
  return Fcdr (tem);
}

450
/* Set the value of terminal parameter PARAMETER in terminal D to VALUE.
451 452 453
   Return the previous value.  */

Lisp_Object
454 455
store_terminal_param (t, parameter, value)
     struct terminal *t;
456 457 458
     Lisp_Object parameter;
     Lisp_Object value;
{
459
  Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
460 461
  if (EQ (old_alist_elt, Qnil))
    {
462
      t->param_alist = Fcons (Fcons (parameter, value), t->param_alist);
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
      return Qnil;
    }
  else
    {
      Lisp_Object result = Fcdr (old_alist_elt);
      Fsetcdr (old_alist_elt, value);
      return result;
    }
}


DEFUN ("terminal-parameters", Fterminal_parameters, Sterminal_parameters, 0, 1, 0,
       doc: /* Return the parameter-alist of terminal TERMINAL.
The value is a list of elements of the form (PARM . VALUE), where PARM
is a symbol.

479 480
TERMINAL can be a terminal object, a frame, or nil (meaning the
selected frame's terminal).  */)
481 482 483
     (terminal)
     Lisp_Object terminal;
{
484 485
  struct terminal *t
    = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
486
  return Fcopy_alist (t->param_alist);
487 488 489 490
}

DEFUN ("terminal-parameter", Fterminal_parameter, Sterminal_parameter, 2, 2, 0,
       doc: /* Return TERMINAL's value for parameter PARAMETER.
491 492
TERMINAL can be a terminal object, a frame, or nil (meaning the
selected frame's terminal).  */)
493 494 495 496 497
     (terminal, parameter)
     Lisp_Object terminal;
     Lisp_Object parameter;
{
  Lisp_Object value;
498 499
  struct terminal *t
    = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
500
  CHECK_SYMBOL (parameter);
501
  value = Fcdr (Fassq (parameter, t->param_alist));
502 503 504 505 506 507 508 509
  return value;
}

DEFUN ("set-terminal-parameter", Fset_terminal_parameter,
       Sset_terminal_parameter, 3, 3, 0,
       doc: /* Set TERMINAL's value for parameter PARAMETER to VALUE.
Return the previous value of PARAMETER.

510 511
TERMINAL can be a terminal object, a frame or nil (meaning the
selected frame's terminal).  */)
512 513 514 515 516
     (terminal, parameter, value)
     Lisp_Object terminal;
     Lisp_Object parameter;
     Lisp_Object value;
{
517 518
  struct terminal *t
    = TERMINALP (terminal) ? XTERMINAL (terminal) : get_terminal (terminal, 1);
519
  return store_terminal_param (t, parameter, value);
520 521 522 523
}



524 525
/* Create the bootstrap display terminal for the initial frame.
   Returns a terminal of type output_initial.  */
526

527 528
struct terminal *
init_initial_terminal (void)
529
{
530
  if (initialized || terminal_list || tty_list)
531 532
    abort ();

533 534 535 536 537
  initial_terminal = create_terminal ();
  initial_terminal->type = output_initial;
  initial_terminal->name = xstrdup ("initial_terminal");
  initial_terminal->kboard = initial_kboard;
  initial_terminal->delete_terminal_hook = &delete_initial_terminal;
538 539
  /* All other hooks are NULL. */

540
  return initial_terminal;
541 542
}

543 544
/* Deletes the bootstrap terminal device.
   Called through delete_terminal_hook. */
545

546 547
static void
delete_initial_terminal (struct terminal *terminal)
548
{
549
  if (terminal != initial_terminal)
550 551
    abort ();

552 553
  delete_terminal (terminal);
  initial_terminal = NULL;
554 555 556 557 558 559 560 561 562 563 564
}

void
syms_of_terminal ()
{

  DEFVAR_LISP ("ring-bell-function", &Vring_bell_function,
    doc: /* Non-nil means call this function to ring the bell.
The function should accept no arguments.  */);
  Vring_bell_function = Qnil;

565 566 567 568 569 570 571 572 573 574 575
  DEFVAR_LISP ("delete-terminal-functions", &Vdelete_terminal_functions,
    doc: /* Special hook run when a terminal is deleted.
Each function is called with argument, the terminal.
This may be called just before actually deleting the terminal,
or some time later.  */);
  Vdelete_terminal_functions = Qnil;
  Qdelete_terminal_functions = intern ("delete-terminal-functions");
  staticpro (&Qdelete_terminal_functions);
  Qrun_hook_with_args = intern ("run-hook-with-args");
  staticpro (&Qrun_hook_with_args);

576 577 578 579 580
  defsubr (&Sdelete_terminal);
  defsubr (&Sframe_terminal);
  defsubr (&Sterminal_live_p);
  defsubr (&Sterminal_list);
  defsubr (&Sterminal_name);
581 582 583 584 585 586 587 588 589
  defsubr (&Sterminal_parameters);
  defsubr (&Sterminal_parameter);
  defsubr (&Sset_terminal_parameter);

  Fprovide (intern ("multi-tty"), Qnil);
}

/* arch-tag: e9af6f27-b483-47dc-bb1a-730c1c5cab03
   (do not change this comment) */