This source file includes following definitions.
- tty_setup_sigwinch
- sigwinch_handler
- get_maybe_acs
- addch_maybe_acs
- hline_maybe_acs
- vline_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_clip
- tty_fill_region
- 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 <stdlib.h>
36 #include <stdarg.h>
37 #include <signal.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
40 #endif
41 #include <termios.h>
42
43 #include "lib/global.h"
44 #include "lib/strutil.h"
45 #include "lib/util.h"
46
47 #include "tty-internal.h"
48 #include "tty.h"
49 #include "color.h"
50 #include "color-internal.h"
51 #include "key.h"
52 #include "mouse.h"
53 #include "win.h"
54
55
56 #ifdef HAVE_NCURSESW_TERM_H
57 #include <ncursesw/term.h>
58 #elif defined HAVE_NCURSES_TERM_H
59 #include <ncurses/term.h>
60 #else
61 #include <term.h>
62 #endif
63
64
65
66 gboolean ncurses_koi8r_double_line_bug;
67
68
69
70 #if !defined(CTRL)
71 #define CTRL(x) ((x) & 0x1f)
72 #endif
73
74 #define yx_in_screen(y, x) (y >= 0 && y < LINES && x >= 0 && x < COLS)
75
76
77
78
79
80
81
82
83
84
85
86 static int mc_curs_row, mc_curs_col;
87
88
89
90
91
92 static void
93 tty_setup_sigwinch (void (*handler) (int))
94 {
95 #if (NCURSES_VERSION_MAJOR >= 4) && defined(SIGWINCH)
96 struct sigaction act, oact;
97
98 memset (&act, 0, sizeof (act));
99 act.sa_handler = handler;
100 sigemptyset (&act.sa_mask);
101 #ifdef SA_RESTART
102 act.sa_flags = SA_RESTART;
103 #endif
104 my_sigaction (SIGWINCH, &act, &oact);
105 #endif
106
107 tty_create_winch_pipe ();
108 }
109
110
111
112 static void
113 sigwinch_handler (int dummy)
114 {
115 ssize_t n = 0;
116
117 (void) dummy;
118
119 n = write (sigwinch_pipe[1], "", 1);
120 (void) n;
121 }
122
123
124
125 static chtype
126 get_maybe_acs (mc_tty_char_t ch)
127 {
128 switch (ch)
129 {
130 case MC_ACS_HLINE:
131 return ACS_HLINE;
132 case MC_ACS_VLINE:
133 return ACS_VLINE;
134 case MC_ACS_ULCORNER:
135 return ACS_ULCORNER;
136 case MC_ACS_URCORNER:
137 return ACS_URCORNER;
138 case MC_ACS_LLCORNER:
139 return ACS_LLCORNER;
140 case MC_ACS_LRCORNER:
141 return ACS_LRCORNER;
142 case MC_ACS_LTEE:
143 return ACS_LTEE;
144 case MC_ACS_RTEE:
145 return ACS_RTEE;
146 case MC_ACS_TTEE:
147 return ACS_TTEE;
148 case MC_ACS_BTEE:
149 return ACS_BTEE;
150 case MC_ACS_PLUS:
151 return ACS_PLUS;
152
153 default:
154 return ch;
155 }
156 }
157
158
159
160 static inline void
161 addch_maybe_acs (mc_tty_char_t ch)
162 {
163 #ifdef HAVE_NCURSES_WIDECHAR
164 if (mc_global.utf8_display)
165 {
166 wchar_t wch[2] = { ch, 0 };
167 cchar_t ctext;
168
169 setcchar (&ctext, wch, 0, 0, NULL);
170 add_wch (&ctext);
171 }
172 else
173 #endif
174 addch (get_maybe_acs (ch));
175 }
176
177
178
179 static inline void
180 hline_maybe_acs (mc_tty_char_t ch, int len)
181 {
182 #ifdef HAVE_NCURSES_WIDECHAR
183 if (mc_global.utf8_display)
184 {
185 wchar_t wch[2] = { ch, 0 };
186 cchar_t ctext;
187
188 setcchar (&ctext, wch, 0, 0, NULL);
189 hline_set (&ctext, len);
190 }
191 else
192 #endif
193 hline (get_maybe_acs (ch), len);
194 }
195
196
197
198 static inline void
199 vline_maybe_acs (mc_tty_char_t ch, int len)
200 {
201 #ifdef HAVE_NCURSES_WIDECHAR
202 if (mc_global.utf8_display)
203 {
204 wchar_t wch[2] = { ch, 0 };
205 cchar_t ctext;
206
207 setcchar (&ctext, wch, 0, 0, NULL);
208 vline_set (&ctext, len);
209 }
210 else
211 #endif
212 vline (get_maybe_acs (ch), len);
213 }
214
215
216
217
218
219 void
220 tty_init (gboolean mouse_enable, gboolean is_xterm)
221 {
222 struct termios mode;
223
224
225
226 const char *ncurses_ver = curses_version ();
227
228 ncurses_koi8r_double_line_bug =
229 (strncmp (ncurses_ver, "ncurses ", 8) == 0 && strcmp (ncurses_ver + 8, "6.5.20251115") < 0);
230
231 initscr ();
232
233 #ifdef HAVE_ESCDELAY
234
235
236
237
238
239
240
241
242
243 ESCDELAY = 200;
244 #endif
245
246 tcgetattr (STDIN_FILENO, &mode);
247
248 mode.c_cc[VINTR] = CTRL ('g');
249
250 mode.c_cc[VQUIT] = NULL_VALUE;
251 tcsetattr (STDIN_FILENO, TCSANOW, &mode);
252
253
254 def_prog_mode ();
255
256 tty_start_interrupt_key ();
257
258 if (!mouse_enable)
259 use_mouse_p = MOUSE_DISABLED;
260 tty_init_xterm_support (is_xterm);
261 tty_enter_ca_mode ();
262 tty_raw_mode ();
263 noecho ();
264 keypad (stdscr, TRUE);
265 nodelay (stdscr, FALSE);
266
267 tty_setup_sigwinch (sigwinch_handler);
268 }
269
270
271
272 void
273 tty_shutdown (void)
274 {
275 tty_destroy_winch_pipe ();
276 tty_reset_shell_mode ();
277 tty_noraw_mode ();
278 tty_keypad (FALSE);
279 tty_reset_screen ();
280 tty_exit_ca_mode ();
281 }
282
283
284
285 void
286 tty_enter_ca_mode (void)
287 {
288 if (mc_global.tty.xterm_flag && smcup != NULL)
289 {
290 fprintf (stdout, ESC_STR "7" ESC_STR "[?47h");
291 fflush (stdout);
292 }
293 }
294
295
296
297 void
298 tty_exit_ca_mode (void)
299 {
300 if (mc_global.tty.xterm_flag && rmcup != NULL)
301 {
302 fprintf (stdout, ESC_STR "[?47l" ESC_STR "8" ESC_STR "[m");
303 fflush (stdout);
304 }
305 }
306
307
308
309 void
310 tty_change_screen_size (void)
311 {
312 #if defined(TIOCGWINSZ) && NCURSES_VERSION_MAJOR >= 4
313 struct winsize winsz;
314
315 winsz.ws_col = winsz.ws_row = 0;
316
317 #ifndef NCURSES_VERSION
318 tty_noraw_mode ();
319 tty_reset_screen ();
320 #endif
321
322
323 ioctl (fileno (stdout), TIOCGWINSZ, &winsz);
324 if (winsz.ws_col != 0 && winsz.ws_row != 0)
325 {
326 #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
327 resizeterm (winsz.ws_row, winsz.ws_col);
328 clearok (stdscr, TRUE);
329 #else
330 COLS = winsz.ws_col;
331 LINES = winsz.ws_row;
332 #endif
333 }
334 #endif
335
336 #ifdef ENABLE_SUBSHELL
337 if (mc_global.tty.use_subshell)
338 tty_resize (mc_global.tty.subshell_pty);
339 #endif
340 }
341
342
343
344 void
345 tty_reset_prog_mode (void)
346 {
347 reset_prog_mode ();
348 }
349
350
351
352 void
353 tty_reset_shell_mode (void)
354 {
355 reset_shell_mode ();
356 }
357
358
359
360 void
361 tty_raw_mode (void)
362 {
363 raw ();
364 cbreak ();
365 }
366
367
368
369 void
370 tty_noraw_mode (void)
371 {
372 nocbreak ();
373 noraw ();
374 }
375
376
377
378 void
379 tty_noecho (void)
380 {
381 noecho ();
382 }
383
384
385
386 int
387 tty_flush_input (void)
388 {
389 return flushinp ();
390 }
391
392
393
394 void
395 tty_keypad (gboolean set)
396 {
397 keypad (stdscr, (bool) set);
398 }
399
400
401
402 void
403 tty_nodelay (gboolean set)
404 {
405 nodelay (stdscr, (bool) set);
406 }
407
408
409
410 int
411 tty_baudrate (void)
412 {
413 return baudrate ();
414 }
415
416
417
418 int
419 tty_lowlevel_getch (void)
420 {
421 return getch ();
422 }
423
424
425
426 int
427 tty_reset_screen (void)
428 {
429 return endwin ();
430 }
431
432
433
434 void
435 tty_touch_screen (void)
436 {
437 touchwin (stdscr);
438 }
439
440
441
442 void
443 tty_gotoyx (int y, int x)
444 {
445 mc_curs_row = y;
446 mc_curs_col = x;
447
448 if (y < 0)
449 y = 0;
450 if (y >= LINES)
451 y = LINES - 1;
452
453 if (x < 0)
454 x = 0;
455 if (x >= COLS)
456 x = COLS - 1;
457
458 move (y, x);
459 }
460
461
462
463 void
464 tty_getyx (int *py, int *px)
465 {
466 *py = mc_curs_row;
467 *px = mc_curs_col;
468 }
469
470
471
472 void
473 tty_draw_hline (int y, int x, mc_tty_char_t ch, int len)
474 {
475 int x1;
476
477 if (y < 0 || y >= LINES || x >= COLS)
478 return;
479
480 x1 = x;
481
482 if (x < 0)
483 {
484 len += x;
485 if (len <= 0)
486 return;
487 x = 0;
488 }
489
490 len = MAX (len, 0);
491
492 move (y, x);
493 hline_maybe_acs (ch, len);
494 move (y, x1);
495
496 mc_curs_row = y;
497 mc_curs_col = x1;
498 }
499
500
501
502 void
503 tty_draw_vline (int y, int x, mc_tty_char_t ch, int len)
504 {
505 int y1;
506
507 if (x < 0 || x >= COLS || y >= LINES)
508 return;
509
510 y1 = y;
511
512 if (y < 0)
513 {
514 len += y;
515 if (len <= 0)
516 return;
517 y = 0;
518 }
519
520 len = MAX (len, 0);
521
522 move (y, x);
523 vline_maybe_acs (ch, len);
524 move (y1, x);
525
526 mc_curs_row = y1;
527 mc_curs_col = x;
528 }
529
530
531
532
533
534
535
536
537 gboolean
538 tty_clip (int *y, int *x, int *rows, int *cols)
539 {
540 if (*y < 0)
541 {
542 *rows += *y;
543
544 if (*rows <= 0)
545 return FALSE;
546
547 *y = 0;
548 }
549
550 if (*x < 0)
551 {
552 *cols += *x;
553
554 if (*cols <= 0)
555 return FALSE;
556
557 *x = 0;
558 }
559
560 if (*y + *rows > LINES)
561 *rows = LINES - *y;
562
563 if (*rows <= 0)
564 return FALSE;
565
566 if (*x + *cols > COLS)
567 *cols = COLS - *x;
568
569 if (*cols <= 0)
570 return FALSE;
571
572 return TRUE;
573 }
574
575
576
577 void
578 tty_fill_region (int y, int x, int rows, int cols, unsigned char ch)
579 {
580 int i;
581
582 if (!tty_clip (&y, &x, &rows, &cols))
583 return;
584
585 for (i = 0; i < rows; i++)
586 {
587 move (y + i, x);
588 hline (ch, cols);
589 }
590
591 move (y, x);
592
593 mc_curs_row = y;
594 mc_curs_col = x;
595 }
596
597
598
599 void
600 tty_display_8bit (gboolean what)
601 {
602 meta (stdscr, (int) what);
603 }
604
605
606
607 void
608 tty_print_char (mc_tty_char_t c)
609 {
610 if (yx_in_screen (mc_curs_row, mc_curs_col))
611 addch_maybe_acs (c);
612 mc_curs_col++;
613 }
614
615
616
617 void
618 tty_print_anychar (mc_tty_char_t c)
619 {
620 if (mc_global.utf8_display || c > 255)
621 {
622 int res;
623 unsigned char str[MB_LEN_MAX + 1];
624
625 res = g_unichar_to_utf8 (c, (char *) str);
626 if (res == 0)
627 {
628 if (yx_in_screen (mc_curs_row, mc_curs_col))
629 addch_maybe_acs ('.');
630 mc_curs_col++;
631 }
632 else
633 {
634 const char *s;
635
636 str[res] = '\0';
637 s = str_term_form ((char *) str);
638
639 if (yx_in_screen (mc_curs_row, mc_curs_col))
640 addstr (s);
641
642 if (g_unichar_iswide (c))
643 mc_curs_col += 2;
644 else if (!g_unichar_iszerowidth (c))
645 mc_curs_col++;
646 }
647 }
648 else
649 {
650 if (yx_in_screen (mc_curs_row, mc_curs_col))
651 addch_maybe_acs (c);
652 mc_curs_col++;
653 }
654 }
655
656
657
658 void
659 tty_print_string (const char *s)
660 {
661 int len;
662 int start = 0;
663
664 s = str_term_form (s);
665 len = str_term_width1 (s);
666
667
668 if (mc_curs_row < 0 || mc_curs_row >= LINES || mc_curs_col + len <= 0 || mc_curs_col >= COLS)
669 {
670 mc_curs_col += len;
671 return;
672 }
673
674
675 if (mc_curs_col < 0)
676 {
677 start = -mc_curs_col;
678 len += mc_curs_col;
679 mc_curs_col = 0;
680 }
681
682 mc_curs_col += len;
683 if (mc_curs_col >= COLS)
684 len = COLS - (mc_curs_col - len);
685
686 addstr (str_term_substring (s, start, len));
687 }
688
689
690
691 void
692 tty_printf (const char *fmt, ...)
693 {
694 va_list args;
695 char buf[BUF_1K];
696
697 va_start (args, fmt);
698 g_vsnprintf (buf, sizeof (buf), fmt, args);
699 va_end (args);
700 tty_print_string (buf);
701 }
702
703
704
705 int
706 tty_tigetflag (const char *terminfo_cap, MC_UNUSED const char *termcap_cap)
707 {
708 return tigetflag ((NCURSES_CONST char *) terminfo_cap);
709 }
710
711
712
713 int
714 tty_tigetnum (const char *terminfo_cap, MC_UNUSED const char *termcap_cap)
715 {
716 return tigetnum ((NCURSES_CONST char *) terminfo_cap);
717 }
718
719
720
721 char *
722 tty_tigetstr (const char *terminfo_cap, MC_UNUSED const char *termcap_cap)
723 {
724 return tigetstr ((NCURSES_CONST char *) terminfo_cap);
725 }
726
727
728
729 void
730 tty_refresh (void)
731 {
732 refresh ();
733 doupdate ();
734 }
735
736
737
738 void
739 tty_beep (void)
740 {
741 beep ();
742 }
743
744