This source file includes following definitions.
- tset_param_alist
- ring_bell
- update_begin
- update_end
- set_terminal_window
- cursor_to
- raw_cursor_to
- clear_to_end
- clear_frame
- clear_end_of_line
- write_glyphs
- insert_glyphs
- delete_glyphs
- ins_del_lines
- decode_terminal
- decode_live_terminal
- decode_tty_terminal
- get_named_terminal
- allocate_terminal
- create_terminal
- delete_terminal
- delete_terminal_internal
- DEFUN
- DEFUN
- DEFUN
- DEFUN
- store_terminal_param
- DEFUN
- calculate_glyph_code_table
- terminal_glyph_code
- initial_free_frame_resources
- init_initial_terminal
- delete_initial_terminal
- syms_of_terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21 #include "lisp.h"
22 #include "character.h"
23 #include "frame.h"
24 #include "termchar.h"
25 #include "termhooks.h"
26 #include "keyboard.h"
27
28 #if HAVE_STRUCT_UNIPAIR_UNICODE
29 # include <errno.h>
30 # include <linux/kd.h>
31 # include <sys/ioctl.h>
32 #endif
33
34
35 struct terminal *terminal_list;
36
37
38 static int next_terminal_id;
39
40
41 struct terminal *initial_terminal;
42
43 static void delete_initial_terminal (struct terminal *);
44
45
46 static void
47 tset_param_alist (struct terminal *t, Lisp_Object val)
48 {
49 t->param_alist = val;
50 }
51
52
53
54 void
55 ring_bell (struct frame *f)
56 {
57 if (!NILP (Vring_bell_function))
58 {
59 Lisp_Object function;
60
61
62
63
64
65
66
67
68
69 function = Vring_bell_function;
70 Vring_bell_function = Qnil;
71
72 call0 (function);
73
74 Vring_bell_function = function;
75 }
76 else if (FRAME_TERMINAL (f)->ring_bell_hook)
77 (*FRAME_TERMINAL (f)->ring_bell_hook) (f);
78 }
79
80 void
81 update_begin (struct frame *f)
82 {
83 if (FRAME_TERMINAL (f)->update_begin_hook)
84 (*FRAME_TERMINAL (f)->update_begin_hook) (f);
85 }
86
87 void
88 update_end (struct frame *f)
89 {
90 if (FRAME_TERMINAL (f)->update_end_hook)
91 (*FRAME_TERMINAL (f)->update_end_hook) (f);
92 }
93
94
95
96
97
98
99 void
100 set_terminal_window (struct frame *f, int size)
101 {
102 if (FRAME_TERMINAL (f)->set_terminal_window_hook)
103 (*FRAME_TERMINAL (f)->set_terminal_window_hook) (f, size);
104 }
105
106
107
108
109 void
110 cursor_to (struct frame *f, int vpos, int hpos)
111 {
112 if (FRAME_TERMINAL (f)->cursor_to_hook)
113 (*FRAME_TERMINAL (f)->cursor_to_hook) (f, vpos, hpos);
114 }
115
116
117
118 void
119 raw_cursor_to (struct frame *f, int row, int col)
120 {
121 if (FRAME_TERMINAL (f)->raw_cursor_to_hook)
122 (*FRAME_TERMINAL (f)->raw_cursor_to_hook) (f, row, col);
123 }
124
125
126
127
128 void
129 clear_to_end (struct frame *f)
130 {
131 if (FRAME_TERMINAL (f)->clear_to_end_hook)
132 (*FRAME_TERMINAL (f)->clear_to_end_hook) (f);
133 }
134
135
136
137 void
138 clear_frame (struct frame *f)
139 {
140 if (FRAME_TERMINAL (f)->clear_frame_hook)
141 (*FRAME_TERMINAL (f)->clear_frame_hook) (f);
142 }
143
144
145
146
147
148
149 void
150 clear_end_of_line (struct frame *f, int first_unused_hpos)
151 {
152 if (FRAME_TERMINAL (f)->clear_end_of_line_hook)
153 (*FRAME_TERMINAL (f)->clear_end_of_line_hook) (f, first_unused_hpos);
154 }
155
156
157
158
159 void
160 write_glyphs (struct frame *f, struct glyph *string, int len)
161 {
162 if (FRAME_TERMINAL (f)->write_glyphs_hook)
163 (*FRAME_TERMINAL (f)->write_glyphs_hook) (f, string, len);
164 }
165
166
167
168
169
170 void
171 insert_glyphs (struct frame *f, struct glyph *start, int len)
172 {
173 if (len <= 0)
174 return;
175
176 if (FRAME_TERMINAL (f)->insert_glyphs_hook)
177 (*FRAME_TERMINAL (f)->insert_glyphs_hook) (f, start, len);
178 }
179
180
181
182 void
183 delete_glyphs (struct frame *f, int n)
184 {
185 if (FRAME_TERMINAL (f)->delete_glyphs_hook)
186 (*FRAME_TERMINAL (f)->delete_glyphs_hook) (f, n);
187 }
188
189
190
191 void
192 ins_del_lines (struct frame *f, int vpos, int n)
193 {
194 if (FRAME_TERMINAL (f)->ins_del_lines_hook)
195 (*FRAME_TERMINAL (f)->ins_del_lines_hook) (f, vpos, n);
196 }
197
198
199
200
201
202
203 static struct terminal *
204 decode_terminal (Lisp_Object terminal)
205 {
206 struct terminal *t;
207
208 if (NILP (terminal))
209 terminal = selected_frame;
210 t = (TERMINALP (terminal)
211 ? XTERMINAL (terminal)
212 : FRAMEP (terminal) ? FRAME_TERMINAL (XFRAME (terminal)) : NULL);
213 return t && t->name ? t : NULL;
214 }
215
216
217
218 struct terminal *
219 decode_live_terminal (Lisp_Object terminal)
220 {
221 struct terminal *t = decode_terminal (terminal);
222
223 if (!t)
224 wrong_type_argument (Qterminal_live_p, terminal);
225 return t;
226 }
227
228
229
230
231 struct terminal *
232 decode_tty_terminal (Lisp_Object terminal)
233 {
234 struct terminal *t = decode_live_terminal (terminal);
235
236 return (t->type == output_termcap || t->type == output_msdos_raw) ? t : NULL;
237 }
238
239
240
241
242
243 struct terminal *
244 get_named_terminal (const char *name)
245 {
246 struct terminal *t;
247
248 eassert (name);
249
250 for (t = terminal_list; t; t = t->next_terminal)
251 {
252 if ((t->type == output_termcap || t->type == output_msdos_raw)
253 && !strcmp (t->display_info.tty->name, name)
254 && TERMINAL_ACTIVE_P (t))
255 return t;
256 }
257 return NULL;
258 }
259
260
261
262 static struct terminal *
263 allocate_terminal (void)
264 {
265 return ALLOCATE_ZEROED_PSEUDOVECTOR (struct terminal, glyph_code_table,
266 PVEC_TERMINAL);
267 }
268
269
270
271
272 struct terminal *
273 create_terminal (enum output_method type, struct redisplay_interface *rif)
274 {
275 struct terminal *terminal = allocate_terminal ();
276 Lisp_Object terminal_coding, keyboard_coding;
277
278 terminal->next_terminal = terminal_list;
279 terminal_list = terminal;
280 terminal->type = type;
281 terminal->rif = rif;
282 terminal->id = next_terminal_id++;
283
284 terminal->keyboard_coding = xmalloc (sizeof (struct coding_system));
285 terminal->terminal_coding = xmalloc (sizeof (struct coding_system));
286
287
288
289
290 keyboard_coding =
291 find_symbol_value (intern ("default-keyboard-coding-system"));
292 if (NILP (keyboard_coding)
293 || BASE_EQ (keyboard_coding, Qunbound)
294 || NILP (Fcoding_system_p (keyboard_coding)))
295 keyboard_coding = Qno_conversion;
296 terminal_coding =
297 find_symbol_value (intern ("default-terminal-coding-system"));
298 if (NILP (terminal_coding)
299 || BASE_EQ (terminal_coding, Qunbound)
300 || NILP (Fcoding_system_p (terminal_coding)))
301 terminal_coding = Qundecided;
302
303 setup_coding_system (keyboard_coding, terminal->keyboard_coding);
304 setup_coding_system (terminal_coding, terminal->terminal_coding);
305
306 return terminal;
307 }
308
309
310
311
312 void
313 delete_terminal (struct terminal *terminal)
314 {
315 Lisp_Object tail, frame;
316
317
318
319 if (!terminal->name)
320 return;
321 xfree (terminal->name);
322 terminal->name = NULL;
323
324
325 FOR_EACH_FRAME (tail, frame)
326 {
327 struct frame *f = XFRAME (frame);
328 if (FRAME_LIVE_P (f) && f->terminal == terminal)
329 {
330
331 delete_frame (frame, Qnoelisp);
332 }
333 }
334
335 delete_terminal_internal (terminal);
336 }
337
338 void
339 delete_terminal_internal (struct terminal *terminal)
340 {
341 struct terminal **tp;
342
343 for (tp = &terminal_list; *tp != terminal; tp = &(*tp)->next_terminal)
344 if (! *tp)
345 emacs_abort ();
346 *tp = terminal->next_terminal;
347
348 xfree (terminal->keyboard_coding);
349 terminal->keyboard_coding = NULL;
350 xfree (terminal->terminal_coding);
351 terminal->terminal_coding = NULL;
352
353 if (terminal->kboard && --terminal->kboard->reference_count == 0)
354 {
355 delete_kboard (terminal->kboard);
356 terminal->kboard = NULL;
357 }
358 }
359
360 DEFUN ("delete-terminal", Fdelete_terminal, Sdelete_terminal, 0, 2, 0,
361 doc:
362
363
364
365
366 )
367 (Lisp_Object terminal, Lisp_Object force)
368 {
369 struct terminal *t = decode_terminal (terminal);
370
371 if (!t)
372 return Qnil;
373
374 if (NILP (force))
375 {
376 struct terminal *p = terminal_list;
377 while (p && (p == t || !TERMINAL_ACTIVE_P (p)))
378 p = p->next_terminal;
379
380 if (!p)
381 error ("Attempt to delete the sole active display terminal");
382 }
383
384 if (NILP (Vrun_hooks))
385 ;
386 else if (EQ (force, Qnoelisp))
387 pending_funcalls
388 = Fcons (list3 (Qrun_hook_with_args,
389 Qdelete_terminal_functions, terminal),
390 pending_funcalls);
391 else
392 safe_call2 (Qrun_hook_with_args, Qdelete_terminal_functions, terminal);
393
394 if (t->delete_terminal_hook)
395 (*t->delete_terminal_hook) (t);
396 else
397 delete_terminal (t);
398
399 return Qnil;
400 }
401
402
403 DEFUN ("frame-terminal", Fframe_terminal, Sframe_terminal, 0, 1, 0,
404 doc:
405
406
407 )
408 (Lisp_Object frame)
409 {
410 struct terminal *t = FRAME_TERMINAL (decode_live_frame (frame));
411
412 if (!t)
413 return Qnil;
414 else
415 {
416 Lisp_Object terminal;
417 XSETTERMINAL (terminal, t);
418 return terminal;
419 }
420 }
421
422 DEFUN ("terminal-live-p", Fterminal_live_p, Sterminal_live_p, 1, 1, 0,
423 doc:
424
425
426
427
428
429 )
430 (Lisp_Object object)
431 {
432 struct terminal *t = decode_terminal (object);
433
434 if (!t)
435 return Qnil;
436
437 switch (t->type)
438 {
439 case output_initial:
440 case output_termcap:
441 return Qt;
442 case output_x_window:
443 return Qx;
444 case output_w32:
445 return Qw32;
446 case output_msdos_raw:
447 return Qpc;
448 case output_ns:
449 return Qns;
450 case output_pgtk:
451 return Qpgtk;
452 case output_haiku:
453 return Qhaiku;
454 case output_android:
455 return Qandroid;
456 default:
457 emacs_abort ();
458 }
459 }
460
461 DEFUN ("terminal-list", Fterminal_list, Sterminal_list, 0, 0, 0,
462 doc: )
463 (void)
464 {
465 Lisp_Object terminal, terminals = Qnil;
466 struct terminal *t;
467
468 for (t = terminal_list; t; t = t->next_terminal)
469 {
470 XSETTERMINAL (terminal, t);
471 terminals = Fcons (terminal, terminals);
472 }
473
474 return terminals;
475 }
476
477 DEFUN ("terminal-name", Fterminal_name, Sterminal_name, 0, 1, 0,
478 doc:
479
480
481
482 )
483 (Lisp_Object terminal)
484 {
485 struct terminal *t = decode_live_terminal (terminal);
486
487 return t->name ? build_string (t->name) : Qnil;
488 }
489
490
491
492
493
494
495 static Lisp_Object
496 store_terminal_param (struct terminal *t, Lisp_Object parameter, Lisp_Object value)
497 {
498 Lisp_Object old_alist_elt = Fassq (parameter, t->param_alist);
499 if (NILP (old_alist_elt))
500 {
501 tset_param_alist (t, Fcons (Fcons (parameter, value), t->param_alist));
502 return Qnil;
503 }
504 else
505 {
506 Lisp_Object result = Fcdr (old_alist_elt);
507 Fsetcdr (old_alist_elt, value);
508 return result;
509 }
510 }
511
512
513 DEFUN ("terminal-parameters", Fterminal_parameters, Sterminal_parameters, 0, 1, 0,
514 doc:
515
516
517
518
519 )
520 (Lisp_Object terminal)
521 {
522 return Fcopy_alist (decode_live_terminal (terminal)->param_alist);
523 }
524
525 DEFUN ("terminal-parameter", Fterminal_parameter, Sterminal_parameter, 2, 2, 0,
526 doc:
527
528 )
529 (Lisp_Object terminal, Lisp_Object parameter)
530 {
531 CHECK_SYMBOL (parameter);
532 return Fcdr (Fassq (parameter, decode_live_terminal (terminal)->param_alist));
533 }
534
535 DEFUN ("set-terminal-parameter", Fset_terminal_parameter,
536 Sset_terminal_parameter, 3, 3, 0,
537 doc:
538
539
540
541 )
542 (Lisp_Object terminal, Lisp_Object parameter, Lisp_Object value)
543 {
544 return store_terminal_param (decode_live_terminal (terminal), parameter, value);
545 }
546
547 #if HAVE_STRUCT_UNIPAIR_UNICODE
548
549
550
551 static void
552 calculate_glyph_code_table (struct terminal *t)
553 {
554 Lisp_Object glyphtab = Qt;
555 enum { initial_unipairs = 1000 };
556 int entry_ct = initial_unipairs;
557 struct unipair unipair_buffer[initial_unipairs];
558 struct unipair *entries = unipair_buffer;
559 struct unipair *alloced = 0;
560
561 while (true)
562 {
563 int fd = fileno (t->display_info.tty->output);
564 struct unimapdesc unimapdesc = { entry_ct, entries };
565 if (ioctl (fd, GIO_UNIMAP, &unimapdesc) == 0)
566 {
567 glyphtab = Fmake_char_table (Qnil, make_fixnum (-1));
568 for (int i = 0; i < unimapdesc.entry_ct; i++)
569 char_table_set (glyphtab, entries[i].unicode,
570 make_fixnum (entries[i].fontpos));
571 break;
572 }
573 if (errno != ENOMEM)
574 break;
575 entry_ct = unimapdesc.entry_ct;
576 entries = alloced = xrealloc (alloced, entry_ct * sizeof *alloced);
577 }
578
579 xfree (alloced);
580 t->glyph_code_table = glyphtab;
581 }
582 #endif
583
584
585
586
587 Lisp_Object
588 terminal_glyph_code (struct terminal *t, int ch)
589 {
590 #if HAVE_STRUCT_UNIPAIR_UNICODE
591
592
593 if (t->type == output_termcap
594 && t->terminal_coding->encoder == encode_coding_utf_8)
595 {
596
597
598 if (NILP (t->glyph_code_table) || ch == MAX_CHAR)
599 calculate_glyph_code_table (t);
600
601 if (! EQ (t->glyph_code_table, Qt))
602 return char_table_ref (t->glyph_code_table, ch);
603 }
604 #endif
605
606 return Qnil;
607 }
608
609
610
611
612 static void
613 initial_free_frame_resources (struct frame *f)
614 {
615 eassert (FRAME_INITIAL_P (f));
616 free_frame_faces (f);
617 }
618
619
620
621
622 struct terminal *
623 init_initial_terminal (void)
624 {
625 if (initialized || terminal_list || tty_list)
626 emacs_abort ();
627
628 initial_terminal = create_terminal (output_initial, NULL);
629
630
631 initial_terminal->name = xstrdup ("initial_terminal");
632 initial_terminal->kboard = initial_kboard;
633 initial_terminal->delete_terminal_hook = &delete_initial_terminal;
634 initial_terminal->delete_frame_hook = &initial_free_frame_resources;
635 initial_terminal->defined_color_hook = &tty_defined_color;
636
637
638 return initial_terminal;
639 }
640
641
642
643
644 static void
645 delete_initial_terminal (struct terminal *terminal)
646 {
647 if (terminal != initial_terminal)
648 emacs_abort ();
649
650 delete_terminal (terminal);
651 initial_terminal = NULL;
652 }
653
654 void
655 syms_of_terminal (void)
656 {
657
658 DEFVAR_LISP ("ring-bell-function", Vring_bell_function,
659 doc:
660 );
661 Vring_bell_function = Qnil;
662
663 DEFVAR_LISP ("delete-terminal-functions", Vdelete_terminal_functions,
664 doc:
665
666
667 );
668 Vdelete_terminal_functions = Qnil;
669
670 DEFSYM (Qterminal_live_p, "terminal-live-p");
671 DEFSYM (Qdelete_terminal_functions, "delete-terminal-functions");
672 DEFSYM (Qrun_hook_with_args, "run-hook-with-args");
673
674 defsubr (&Sdelete_terminal);
675 defsubr (&Sframe_terminal);
676 defsubr (&Sterminal_live_p);
677 defsubr (&Sterminal_list);
678 defsubr (&Sterminal_name);
679 defsubr (&Sterminal_parameters);
680 defsubr (&Sterminal_parameter);
681 defsubr (&Sset_terminal_parameter);
682
683 Fprovide (intern_c_string ("multi-tty"), Qnil);
684 }