This source file includes following definitions.
- tty_setup_sigwinch
- sigwinch_handler
- do_define_key
- load_terminfo_keys
- get_maybe_acs
- tty_init
- tty_shutdown
- tty_enter_ca_mode
- tty_exit_ca_mode
- tty_change_screen_size
- tty_reset_prog_mode
- tty_reset_shell_mode
- tty_raw_mode
- tty_noraw_mode
- tty_noecho
- tty_flush_input
- tty_keypad
- tty_nodelay
- tty_baudrate
- tty_lowlevel_getch
- tty_reset_screen
- tty_touch_screen
- tty_gotoyx
- tty_getyx
- tty_draw_hline
- tty_draw_vline
- tty_fill_region
- tty_colorize_area
- tty_display_8bit
- tty_print_char
- tty_print_anychar
- tty_print_string
- tty_printf
- tty_tigetflag
- tty_tigetnum
- tty_tigetstr
- tty_refresh
- tty_beep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 #include <config.h>
33
34 #include <limits.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #ifdef HAVE_SYS_IOCTL_H
41 #include <sys/ioctl.h>
42 #endif
43 #include <termios.h>
44
45 #include "lib/global.h"
46 #include "lib/strutil.h"
47 #include "lib/util.h"
48
49 #include "tty-internal.h"
50 #include "tty.h"
51 #include "color.h"
52 #include "color-slang.h"
53 #include "color-internal.h"
54 #include "mouse.h"
55 #include "key.h"
56 #include "win.h"
57
58
59
60
61
62 #ifndef SLTT_MAX_SCREEN_COLS
63 #define SLTT_MAX_SCREEN_COLS 512
64 #endif
65
66 #ifndef SLTT_MAX_SCREEN_ROWS
67 #define SLTT_MAX_SCREEN_ROWS 512
68 #endif
69
70
71
72
73
74
75
76
77 static struct termios boot_mode;
78 static struct termios new_mode;
79
80
81 static gboolean no_slang_delay;
82
83 static gboolean slsmg_active = FALSE;
84
85
86
87
88 static const struct
89 {
90 int key_code;
91 const char *key_name;
92 } key_table[] = {
93 { KEY_F (0), "k0" },
94 { KEY_F (1), "k1" },
95 { KEY_F (2), "k2" },
96 { KEY_F (3), "k3" },
97 { KEY_F (4), "k4" },
98 { KEY_F (5), "k5" },
99 { KEY_F (6), "k6" },
100 { KEY_F (7), "k7" },
101 { KEY_F (8), "k8" },
102 { KEY_F (9), "k9" },
103 { KEY_F (10), "k;" },
104 { KEY_F (11), "F1" },
105 { KEY_F (12), "F2" },
106 { KEY_F (13), "F3" },
107 { KEY_F (14), "F4" },
108 { KEY_F (15), "F5" },
109 { KEY_F (16), "F6" },
110 { KEY_F (17), "F7" },
111 { KEY_F (18), "F8" },
112 { KEY_F (19), "F9" },
113 { KEY_F (20), "FA" },
114 { KEY_IC, "kI" },
115 { KEY_NPAGE, "kN" },
116 { KEY_PPAGE, "kP" },
117 { KEY_LEFT, "kl" },
118 { KEY_RIGHT, "kr" },
119 { KEY_UP, "ku" },
120 { KEY_DOWN, "kd" },
121 { KEY_DC, "kD" },
122 { KEY_BACKSPACE, "kb" },
123 { KEY_HOME, "kh" },
124 { KEY_END, "@7" },
125 {
126 0,
127 NULL,
128 },
129 };
130
131
132
133
134
135 static void
136 tty_setup_sigwinch (void (*handler) (int))
137 {
138 (void) SLsignal (SIGWINCH, handler);
139 tty_create_winch_pipe ();
140 }
141
142
143
144 static void
145 sigwinch_handler (int dummy)
146 {
147 ssize_t n = 0;
148
149 (void) dummy;
150
151 n = write (sigwinch_pipe[1], "", 1);
152 (void) n;
153
154 (void) SLsignal (SIGWINCH, sigwinch_handler);
155 }
156
157
158
159 static void
160 do_define_key (int code, const char *strcap)
161 {
162 char *seq;
163
164 seq = SLtt_tgetstr ((SLFUTURE_CONST char *) strcap);
165 if (seq != NULL)
166 define_sequence (code, seq, MCKEY_NOACTION);
167 }
168
169
170
171 static void
172 load_terminfo_keys (void)
173 {
174 int i;
175
176 for (i = 0; key_table[i].key_code; i++)
177 do_define_key (key_table[i].key_code, key_table[i].key_name);
178 }
179
180
181
182 static int
183 get_maybe_acs (mc_tty_char_t c, gboolean *alt_char)
184 {
185 *alt_char = TRUE;
186
187 switch (c)
188 {
189 case MC_ACS_HLINE:
190 return SLSMG_HLINE_CHAR;
191 case MC_ACS_VLINE:
192 return SLSMG_VLINE_CHAR;
193 case MC_ACS_ULCORNER:
194 return SLSMG_ULCORN_CHAR;
195 case MC_ACS_URCORNER:
196 return SLSMG_URCORN_CHAR;
197 case MC_ACS_LLCORNER:
198 return SLSMG_LLCORN_CHAR;
199 case MC_ACS_LRCORNER:
200 return SLSMG_LRCORN_CHAR;
201 case MC_ACS_LTEE:
202 return SLSMG_LTEE_CHAR;
203 case MC_ACS_RTEE:
204 return SLSMG_RTEE_CHAR;
205 case MC_ACS_TTEE:
206 return SLSMG_UTEE_CHAR;
207 case MC_ACS_BTEE:
208 return SLSMG_DTEE_CHAR;
209 case MC_ACS_PLUS:
210 return SLSMG_PLUS_CHAR;
211
212 default:
213 *alt_char = FALSE;
214 return c;
215 }
216 }
217
218
219
220
221
222 void
223 tty_init (gboolean mouse_enable, gboolean is_xterm)
224 {
225 SLtt_Ignore_Beep = 1;
226
227 SLutf8_enable (-1);
228 SLtt_get_terminfo ();
229
230
231
232
233
234
235
236 if ((COLS < 10)
237 || (LINES < 5)
238 #if SLANG_VERSION < 20303
239
240
241 || (COLS > SLTT_MAX_SCREEN_COLS) || (LINES > SLTT_MAX_SCREEN_ROWS)
242 #endif
243 )
244 {
245 fprintf (stderr,
246 _ ("Screen size %dx%d is not supported.\n"
247 "Check the TERM environment variable.\n"),
248 COLS, LINES);
249 exit (EXIT_FAILURE);
250 }
251
252 tcgetattr (fileno (stdin), &boot_mode);
253
254 SLang_init_tty (XCTRL ('g'), 1, 0);
255
256 if (mc_global.tty.ugly_line_drawing)
257 SLtt_Has_Alt_Charset = 0;
258
259 tcgetattr (SLang_TT_Read_FD, &new_mode);
260
261 tty_reset_prog_mode ();
262 load_terminfo_keys ();
263
264 SLtt_Blink_Mode = (tty_use_256colors (NULL) || tty_use_truecolors (NULL)) ? 1 : 0;
265
266 tty_start_interrupt_key ();
267
268
269 init_key_input_fd ();
270
271
272
273
274
275
276 tty_display_8bit (FALSE);
277
278 SLsmg_init_smg ();
279 slsmg_active = TRUE;
280 if (!mouse_enable)
281 use_mouse_p = MOUSE_DISABLED;
282 tty_init_xterm_support (is_xterm);
283 tty_enter_ca_mode ();
284 tty_keypad (TRUE);
285 tty_nodelay (FALSE);
286
287 tty_setup_sigwinch (sigwinch_handler);
288 }
289
290
291
292 void
293 tty_shutdown (void)
294 {
295 char *op_cap;
296
297 tty_destroy_winch_pipe ();
298 tty_reset_shell_mode ();
299 tty_noraw_mode ();
300 tty_keypad (FALSE);
301 tty_reset_screen ();
302 tty_exit_ca_mode ();
303 SLang_reset_tty ();
304 slsmg_active = FALSE;
305
306
307
308
309 op_cap = SLtt_tgetstr ((SLFUTURE_CONST char *) "op");
310 if (op_cap != NULL)
311 {
312 fputs (op_cap, stdout);
313 fflush (stdout);
314 }
315 }
316
317
318
319 void
320 tty_enter_ca_mode (void)
321 {
322
323 }
324
325
326
327 void
328 tty_exit_ca_mode (void)
329 {
330
331 }
332
333
334
335 void
336 tty_change_screen_size (void)
337 {
338 SLtt_get_screen_size ();
339 if (slsmg_active)
340 SLsmg_reinit_smg ();
341
342 #ifdef ENABLE_SUBSHELL
343 if (mc_global.tty.use_subshell)
344 tty_resize (mc_global.tty.subshell_pty);
345 #endif
346 }
347
348
349
350
351 void
352 tty_reset_prog_mode (void)
353 {
354 tcsetattr (SLang_TT_Read_FD, TCSANOW, &new_mode);
355 SLsmg_init_smg ();
356 slsmg_active = TRUE;
357 SLsmg_touch_lines (0, LINES);
358 }
359
360
361
362
363 void
364 tty_reset_shell_mode (void)
365 {
366 tcsetattr (SLang_TT_Read_FD, TCSANOW, &boot_mode);
367 }
368
369
370
371 void
372 tty_raw_mode (void)
373 {
374 tcsetattr (SLang_TT_Read_FD, TCSANOW, &new_mode);
375 }
376
377
378
379 void
380 tty_noraw_mode (void)
381 {
382 }
383
384
385
386 void
387 tty_noecho (void)
388 {
389 }
390
391
392
393 int
394 tty_flush_input (void)
395 {
396 return 0;
397 }
398
399
400
401 void
402 tty_keypad (gboolean set)
403 {
404 char *keypad_string;
405
406 keypad_string = SLtt_tgetstr ((SLFUTURE_CONST char *) (set ? "ks" : "ke"));
407
408 if (keypad_string != NULL)
409 SLtt_write_string (keypad_string);
410 }
411
412
413
414 void
415 tty_nodelay (gboolean set)
416 {
417 no_slang_delay = set;
418 }
419
420
421
422 int
423 tty_baudrate (void)
424 {
425 return SLang_TT_Baud_Rate;
426 }
427
428
429
430 int
431 tty_lowlevel_getch (void)
432 {
433 int c;
434
435 if (no_slang_delay && (SLang_input_pending (0) == 0))
436 return -1;
437
438 c = SLang_getkey ();
439 if (c == SLANG_GETKEY_ERROR)
440 {
441 fprintf (stderr,
442 "SLang_getkey returned SLANG_GETKEY_ERROR\n"
443 "Assuming EOF on stdin and exiting\n");
444 exit (EXIT_FAILURE);
445 }
446
447 return c;
448 }
449
450
451
452 int
453 tty_reset_screen (void)
454 {
455 SLsmg_reset_smg ();
456 slsmg_active = FALSE;
457 return 0;
458 }
459
460
461
462 void
463 tty_touch_screen (void)
464 {
465 SLsmg_touch_lines (0, LINES);
466 }
467
468
469
470 void
471 tty_gotoyx (int y, int x)
472 {
473 SLsmg_gotorc (y, x);
474 }
475
476
477
478 void
479 tty_getyx (int *py, int *px)
480 {
481 *py = SLsmg_get_row ();
482 *px = SLsmg_get_column ();
483 }
484
485
486
487 void
488 tty_draw_hline (int y, int x, mc_tty_char_t ch, int len)
489 {
490 int x1;
491
492 if (y < 0 || y >= LINES || x >= COLS)
493 return;
494
495 x1 = x;
496
497 if (x < 0)
498 {
499 len += x;
500 if (len <= 0)
501 return;
502 x = 0;
503 }
504
505 SLsmg_gotorc (y, x);
506
507 if ((mc_global.utf8_display && ch == 0x2500) || ch == MC_ACS_HLINE)
508 SLsmg_draw_hline (len);
509 else
510 while (len-- != 0)
511 tty_print_char (ch);
512
513 SLsmg_gotorc (y, x1);
514 }
515
516
517
518 void
519 tty_draw_vline (int y, int x, mc_tty_char_t ch, int len)
520 {
521 int y1;
522
523 if (x < 0 || x >= COLS || y >= LINES)
524 return;
525
526 y1 = y;
527
528 if (y < 0)
529 {
530 len += y;
531 if (len <= 0)
532 return;
533 y = 0;
534 }
535
536 SLsmg_gotorc (y, x);
537
538 if ((mc_global.utf8_display && ch == 0x2502) || ch == MC_ACS_VLINE)
539 SLsmg_draw_vline (len);
540 else
541 {
542 int pos = 0;
543
544 while (len-- != 0)
545 {
546 SLsmg_gotorc (y + pos, x);
547 tty_print_char (ch);
548 pos++;
549 }
550 }
551
552 SLsmg_gotorc (y1, x);
553 }
554
555
556
557 void
558 tty_fill_region (int y, int x, int rows, int cols, unsigned char ch)
559 {
560 SLsmg_fill_region (y, x, rows, cols, ch);
561 }
562
563
564
565 void
566 tty_colorize_area (int y, int x, int rows, int cols, int color)
567 {
568 if (use_colors)
569 SLsmg_set_color_in_region (color, y, x, rows, cols);
570 }
571
572
573
574 void
575 tty_display_8bit (gboolean what)
576 {
577 SLsmg_Display_Eight_Bit = what ? 128 : 160;
578 }
579
580
581
582 void
583 tty_print_char (mc_tty_char_t c)
584 {
585 gboolean alt_char = FALSE;
586 int char_maybe_acs = c;
587
588 if (!mc_global.utf8_display)
589 char_maybe_acs = get_maybe_acs (char_maybe_acs, &alt_char);
590
591 if (alt_char)
592 SLsmg_draw_object (SLsmg_get_row (), SLsmg_get_column (), char_maybe_acs);
593 else
594 SLsmg_write_char ((SLwchar_Type) ((unsigned int) char_maybe_acs));
595 }
596
597
598
599 void
600 tty_print_anychar (mc_tty_char_t c)
601 {
602 if (c > 255)
603 {
604 char str[MB_LEN_MAX + 1];
605 int res;
606
607 res = g_unichar_to_utf8 (c, str);
608 if (res == 0)
609 {
610 str[0] = '.';
611 str[1] = '\0';
612 }
613 else
614 {
615 str[res] = '\0';
616 }
617 SLsmg_write_string ((char *) str_term_form (str));
618 }
619 else
620 {
621 if (!is_printable (c))
622 c = '.';
623 SLsmg_write_char ((SLwchar_Type) ((unsigned int) c));
624 }
625 }
626
627
628
629 void
630 tty_print_string (const char *s)
631 {
632 SLsmg_write_string ((char *) str_term_form (s));
633 }
634
635
636
637 void
638 tty_printf (const char *fmt, ...)
639 {
640 va_list args;
641
642 va_start (args, fmt);
643 SLsmg_vprintf ((char *) fmt, args);
644 va_end (args);
645 }
646
647
648
649
650
651
652
653
654
655
656
657 int
658 tty_tigetflag (const char *terminfo_cap, const char *termcap_cap)
659 {
660 return SLtt_tgetflag ((SLFUTURE_CONST char *) (termcap_cap ? termcap_cap : terminfo_cap));
661 }
662
663
664
665 int
666 tty_tigetnum (const char *terminfo_cap, const char *termcap_cap)
667 {
668 return SLtt_tgetnum ((SLFUTURE_CONST char *) (termcap_cap ? termcap_cap : terminfo_cap));
669 }
670
671
672
673 char *
674 tty_tigetstr (const char *terminfo_cap, const char *termcap_cap)
675 {
676 return SLtt_tgetstr ((SLFUTURE_CONST char *) (termcap_cap ? termcap_cap : terminfo_cap));
677 }
678
679
680
681 void
682 tty_refresh (void)
683 {
684 SLsmg_refresh ();
685 }
686
687
688
689 void
690 tty_beep (void)
691 {
692 SLtt_beep ();
693 }
694
695