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