tty-slang.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. Interface to the terminal controlling library.
  3. Slang wrapper.
  4. Copyright (C) 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2009
  7. Egmont Koblinger <egmont@gmail.com>, 2010
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software; you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation; either version 2 of the
  12. License, or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be
  14. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. MA 02110-1301, USA.
  21. */
  22. /** \file
  23. * \brief Source: S-Lang-based tty layer of Midnight Commander
  24. */
  25. #include <config.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <termios.h>
  30. #include <sys/types.h> /* size_t */
  31. #include <unistd.h>
  32. #include <signal.h>
  33. #include "lib/global.h"
  34. #include "lib/strutil.h" /* str_term_form */
  35. #include "lib/util.h" /* is_printable() */
  36. #include "tty-internal.h" /* slow_tty */
  37. #include "tty.h"
  38. #include "color.h"
  39. #include "color-slang.h"
  40. #include "color-internal.h"
  41. #include "mouse.h" /* Gpm_Event is required in key.h */
  42. #include "key.h" /* define_sequence */
  43. #include "win.h"
  44. /*** global variables ****************************************************************************/
  45. extern int reset_hp_softkeys;
  46. /*** file scope macro definitions ****************************************************************/
  47. #ifndef SA_RESTART
  48. #define SA_RESTART 0
  49. #endif
  50. #ifndef SLTT_MAX_SCREEN_COLS
  51. #define SLTT_MAX_SCREEN_COLS 512
  52. #endif
  53. #ifndef SLTT_MAX_SCREEN_ROWS
  54. #define SLTT_MAX_SCREEN_ROWS 512
  55. #endif
  56. /*** file scope type declarations ****************************************************************/
  57. /*** file scope variables ************************************************************************/
  58. /* Various saved termios settings that we control here */
  59. static struct termios boot_mode;
  60. static struct termios new_mode;
  61. /* Controls whether we should wait for input in tty_lowlevel_getch */
  62. static gboolean no_slang_delay;
  63. /* This table describes which capabilities we want and which values we
  64. * assign to them.
  65. */
  66. static const struct
  67. {
  68. int key_code;
  69. const char *key_name;
  70. } key_table[] =
  71. {
  72. /* *INDENT-OFF* */
  73. { KEY_F (0), "k0" },
  74. { KEY_F (1), "k1" },
  75. { KEY_F (2), "k2" },
  76. { KEY_F (3), "k3" },
  77. { KEY_F (4), "k4" },
  78. { KEY_F (5), "k5" },
  79. { KEY_F (6), "k6" },
  80. { KEY_F (7), "k7" },
  81. { KEY_F (8), "k8" },
  82. { KEY_F (9), "k9" },
  83. { KEY_F (10), "k;" },
  84. { KEY_F (11), "F1" },
  85. { KEY_F (12), "F2" },
  86. { KEY_F (13), "F3" },
  87. { KEY_F (14), "F4" },
  88. { KEY_F (15), "F5" },
  89. { KEY_F (16), "F6" },
  90. { KEY_F (17), "F7" },
  91. { KEY_F (18), "F8" },
  92. { KEY_F (19), "F9" },
  93. { KEY_F (20), "FA" },
  94. { KEY_IC, "kI" },
  95. { KEY_NPAGE, "kN" },
  96. { KEY_PPAGE, "kP" },
  97. { KEY_LEFT, "kl" },
  98. { KEY_RIGHT, "kr" },
  99. { KEY_UP, "ku" },
  100. { KEY_DOWN, "kd" },
  101. { KEY_DC, "kD" },
  102. { KEY_BACKSPACE, "kb" },
  103. { KEY_HOME, "kh" },
  104. { KEY_END, "@7" },
  105. { 0, NULL }
  106. /* *INDENT-ON* */
  107. };
  108. /*** file scope functions ************************************************************************/
  109. /* --------------------------------------------------------------------------------------------- */
  110. /* HP Terminals have capabilities (pfkey, pfloc, pfx) to program function keys.
  111. elm 2.4pl15 invoked with the -K option utilizes these softkeys and the
  112. consequence is that function keys don't work in MC sometimes...
  113. Unfortunately I don't now the one and only escape sequence to turn off.
  114. softkeys (elm uses three different capabilities to turn on softkeys and two.
  115. capabilities to turn them off)..
  116. Among other things elm uses the pair we already use in slang_keypad. That's.
  117. the reason why I call slang_reset_softkeys from slang_keypad. In lack of
  118. something better the softkeys are programmed to their defaults from the
  119. termcap/terminfo database.
  120. The escape sequence to program the softkeys is taken from elm and it is.
  121. hardcoded because neither slang nor ncurses 4.1 know how to 'printf' this.
  122. sequence. -- Norbert
  123. */
  124. static void
  125. slang_reset_softkeys (void)
  126. {
  127. int key;
  128. char *send;
  129. static const char display[] = " ";
  130. char tmp[BUF_SMALL];
  131. for (key = 1; key < 9; key++)
  132. {
  133. g_snprintf (tmp, sizeof (tmp), "k%d", key);
  134. send = (char *) SLtt_tgetstr (tmp);
  135. if (send != NULL)
  136. {
  137. g_snprintf (tmp, sizeof (tmp), "\033&f%dk%dd%dL%s%s", key,
  138. (int) (sizeof (display) - 1), (int) strlen (send), display, send);
  139. SLtt_write_string (tmp);
  140. }
  141. }
  142. }
  143. /* --------------------------------------------------------------------------------------------- */
  144. static void
  145. do_define_key (int code, const char *strcap)
  146. {
  147. char *seq;
  148. seq = (char *) SLtt_tgetstr ((char *) strcap);
  149. if (seq != NULL)
  150. define_sequence (code, seq, MCKEY_NOACTION);
  151. }
  152. /* --------------------------------------------------------------------------------------------- */
  153. static void
  154. load_terminfo_keys (void)
  155. {
  156. int i;
  157. for (i = 0; key_table[i].key_code; i++)
  158. do_define_key (key_table[i].key_code, key_table[i].key_name);
  159. }
  160. /* --------------------------------------------------------------------------------------------- */
  161. /*** public functions ****************************************************************************/
  162. /* --------------------------------------------------------------------------------------------- */
  163. int
  164. mc_tty_normalize_lines_char (const char *str)
  165. {
  166. char *str2;
  167. int res;
  168. struct mc_tty_lines_struct
  169. {
  170. const char *line;
  171. int line_code;
  172. } const lines_codes[] = {
  173. {"\342\224\214", SLSMG_ULCORN_CHAR},
  174. {"\342\224\220", SLSMG_URCORN_CHAR},
  175. {"\342\224\224", SLSMG_LLCORN_CHAR},
  176. {"\342\224\230", SLSMG_LRCORN_CHAR},
  177. {"\342\224\234", SLSMG_LTEE_CHAR},
  178. {"\342\224\244", SLSMG_RTEE_CHAR},
  179. {"\342\224\254", SLSMG_UTEE_CHAR},
  180. {"\342\224\264", SLSMG_DTEE_CHAR},
  181. {"\342\224\200", SLSMG_HLINE_CHAR},
  182. {"\342\224\202", SLSMG_VLINE_CHAR},
  183. {"\342\224\274", SLSMG_PLUS_CHAR},
  184. {NULL, 0}
  185. };
  186. if (!str)
  187. return (int) ' ';
  188. for (res = 0; lines_codes[res].line; res++)
  189. {
  190. if (strcmp (str, lines_codes[res].line) == 0)
  191. return lines_codes[res].line_code;
  192. }
  193. str2 = mc_tty_normalize_from_utf8 (str);
  194. res = g_utf8_get_char_validated (str2, -1);
  195. if (res < 0)
  196. res = (unsigned char) str2[0];
  197. g_free (str2);
  198. return res;
  199. }
  200. /* --------------------------------------------------------------------------------------------- */
  201. void
  202. tty_init (gboolean slow, gboolean ugly_lines)
  203. {
  204. slow_tty = slow;
  205. ugly_line_drawing = ugly_lines;
  206. SLtt_Ignore_Beep = 1;
  207. SLutf8_enable (-1); /* has to be called first before any of the other functions. */
  208. SLtt_get_terminfo ();
  209. /*
  210. * If the terminal in not in terminfo but begins with a well-known
  211. * string such as "linux" or "xterm" S-Lang will go on, but the
  212. * terminal size and several other variables won't be initialized
  213. * (as of S-Lang 1.4.4). Detect it and abort. Also detect extremely
  214. * small, large and negative screen dimensions.
  215. */
  216. if ((COLS < 10) || (LINES < 5)
  217. || (COLS > SLTT_MAX_SCREEN_COLS) || (LINES > SLTT_MAX_SCREEN_ROWS))
  218. {
  219. fprintf (stderr,
  220. _("Screen size %dx%d is not supported.\n"
  221. "Check the TERM environment variable.\n"), COLS, LINES);
  222. exit (EXIT_FAILURE);
  223. }
  224. tcgetattr (fileno (stdin), &boot_mode);
  225. /* 255 = ignore abort char; XCTRL('g') for abort char = ^g */
  226. SLang_init_tty (XCTRL ('g'), 1, 0);
  227. if (ugly_lines)
  228. SLtt_Has_Alt_Charset = 0;
  229. /* If SLang uses fileno(stderr) for terminal input MC will hang
  230. if we call SLang_getkey between calls to open_error_pipe and
  231. close_error_pipe, e.g. when we do a growing view of an gzipped
  232. file. */
  233. if (SLang_TT_Read_FD == fileno (stderr))
  234. SLang_TT_Read_FD = fileno (stdin);
  235. if (tcgetattr (SLang_TT_Read_FD, &new_mode) == 0)
  236. {
  237. #ifdef VDSUSP
  238. new_mode.c_cc[VDSUSP] = NULL_VALUE; /* to ignore ^Y */
  239. #endif
  240. #ifdef VLNEXT
  241. new_mode.c_cc[VLNEXT] = NULL_VALUE; /* to ignore ^V */
  242. #endif
  243. tcsetattr (SLang_TT_Read_FD, TCSADRAIN, &new_mode);
  244. }
  245. tty_reset_prog_mode ();
  246. load_terminfo_keys ();
  247. SLtt_Blink_Mode = tty_use_256colors ()? 1 : 0;
  248. tty_start_interrupt_key ();
  249. /* It's the small part from the previous init_key() */
  250. init_key_input_fd ();
  251. SLsmg_init_smg ();
  252. do_enter_ca_mode ();
  253. tty_keypad (TRUE);
  254. tty_nodelay (FALSE);
  255. }
  256. /* --------------------------------------------------------------------------------------------- */
  257. void
  258. tty_shutdown (void)
  259. {
  260. char *op_cap;
  261. SLsmg_reset_smg ();
  262. tty_reset_shell_mode ();
  263. do_exit_ca_mode ();
  264. SLang_reset_tty ();
  265. /* Load the op capability to reset the colors to those that were
  266. * active when the program was started up
  267. */
  268. op_cap = SLtt_tgetstr ((char *) "op");
  269. if (op_cap != NULL)
  270. {
  271. fputs (op_cap, stdout);
  272. fflush (stdout);
  273. }
  274. }
  275. /* --------------------------------------------------------------------------------------------- */
  276. /* Done each time we come back from done mode */
  277. void
  278. tty_reset_prog_mode (void)
  279. {
  280. tcsetattr (SLang_TT_Read_FD, TCSANOW, &new_mode);
  281. SLsmg_init_smg ();
  282. SLsmg_touch_lines (0, LINES);
  283. }
  284. /* --------------------------------------------------------------------------------------------- */
  285. /* Called each time we want to shutdown slang screen manager */
  286. void
  287. tty_reset_shell_mode (void)
  288. {
  289. tcsetattr (SLang_TT_Read_FD, TCSANOW, &boot_mode);
  290. }
  291. /* --------------------------------------------------------------------------------------------- */
  292. void
  293. tty_raw_mode (void)
  294. {
  295. tcsetattr (SLang_TT_Read_FD, TCSANOW, &new_mode);
  296. }
  297. /* --------------------------------------------------------------------------------------------- */
  298. void
  299. tty_noraw_mode (void)
  300. {
  301. }
  302. /* --------------------------------------------------------------------------------------------- */
  303. void
  304. tty_noecho (void)
  305. {
  306. }
  307. /* --------------------------------------------------------------------------------------------- */
  308. int
  309. tty_flush_input (void)
  310. {
  311. return 0; /* OK */
  312. }
  313. /* --------------------------------------------------------------------------------------------- */
  314. void
  315. tty_keypad (gboolean set)
  316. {
  317. char *keypad_string;
  318. keypad_string = (char *) SLtt_tgetstr ((char *) (set ? "ks" : "ke"));
  319. if (keypad_string != NULL)
  320. SLtt_write_string (keypad_string);
  321. if (set && reset_hp_softkeys)
  322. slang_reset_softkeys ();
  323. }
  324. /* --------------------------------------------------------------------------------------------- */
  325. void
  326. tty_nodelay (gboolean set)
  327. {
  328. no_slang_delay = set;
  329. }
  330. /* --------------------------------------------------------------------------------------------- */
  331. int
  332. tty_baudrate (void)
  333. {
  334. return SLang_TT_Baud_Rate;
  335. }
  336. /* --------------------------------------------------------------------------------------------- */
  337. int
  338. tty_lowlevel_getch (void)
  339. {
  340. int c;
  341. if (no_slang_delay && (SLang_input_pending (0) == 0))
  342. return -1;
  343. c = SLang_getkey ();
  344. if (c == SLANG_GETKEY_ERROR)
  345. {
  346. fprintf (stderr,
  347. "SLang_getkey returned SLANG_GETKEY_ERROR\n"
  348. "Assuming EOF on stdin and exiting\n");
  349. exit (EXIT_FAILURE);
  350. }
  351. return c;
  352. }
  353. /* --------------------------------------------------------------------------------------------- */
  354. int
  355. tty_reset_screen (void)
  356. {
  357. SLsmg_reset_smg ();
  358. return 0; /* OK */
  359. }
  360. /* --------------------------------------------------------------------------------------------- */
  361. void
  362. tty_touch_screen (void)
  363. {
  364. SLsmg_touch_lines (0, LINES);
  365. }
  366. /* --------------------------------------------------------------------------------------------- */
  367. void
  368. tty_gotoyx (int y, int x)
  369. {
  370. SLsmg_gotorc (y, x);
  371. }
  372. /* --------------------------------------------------------------------------------------------- */
  373. void
  374. tty_getyx (int *py, int *px)
  375. {
  376. *py = SLsmg_get_row ();
  377. *px = SLsmg_get_column ();
  378. }
  379. /* --------------------------------------------------------------------------------------------- */
  380. /* if x < 0 or y < 0, draw line staring from current position */
  381. void
  382. tty_draw_hline (int y, int x, int ch, int len)
  383. {
  384. if (ch == ACS_HLINE)
  385. ch = mc_tty_frm[MC_TTY_FRM_HORIZ];
  386. if ((y < 0) || (x < 0))
  387. {
  388. y = SLsmg_get_row ();
  389. x = SLsmg_get_column ();
  390. }
  391. else
  392. SLsmg_gotorc (y, x);
  393. if (ch == 0)
  394. ch = ACS_HLINE;
  395. if (ch == ACS_HLINE)
  396. SLsmg_draw_hline (len);
  397. else
  398. while (len-- != 0)
  399. tty_print_char (ch);
  400. SLsmg_gotorc (y, x);
  401. }
  402. /* --------------------------------------------------------------------------------------------- */
  403. /* if x < 0 or y < 0, draw line staring from current position */
  404. void
  405. tty_draw_vline (int y, int x, int ch, int len)
  406. {
  407. if (ch == ACS_VLINE)
  408. ch = mc_tty_frm[MC_TTY_FRM_VERT];
  409. if ((y < 0) || (x < 0))
  410. {
  411. y = SLsmg_get_row ();
  412. x = SLsmg_get_column ();
  413. }
  414. else
  415. SLsmg_gotorc (y, x);
  416. if (ch == 0)
  417. ch = ACS_VLINE;
  418. if (ch == ACS_VLINE)
  419. SLsmg_draw_vline (len);
  420. else
  421. {
  422. int pos = 0;
  423. while (len-- != 0)
  424. {
  425. SLsmg_gotorc (y + pos, x);
  426. tty_print_char (ch);
  427. pos++;
  428. }
  429. }
  430. SLsmg_gotorc (y, x);
  431. }
  432. /* --------------------------------------------------------------------------------------------- */
  433. void
  434. tty_fill_region (int y, int x, int rows, int cols, unsigned char ch)
  435. {
  436. SLsmg_fill_region (y, x, rows, cols, ch);
  437. }
  438. /* --------------------------------------------------------------------------------------------- */
  439. void
  440. tty_set_alt_charset (gboolean alt_charset)
  441. {
  442. SLsmg_set_char_set ((int) alt_charset);
  443. }
  444. /* --------------------------------------------------------------------------------------------- */
  445. void
  446. tty_display_8bit (gboolean what)
  447. {
  448. SLsmg_Display_Eight_Bit = what ? 128 : 160;
  449. }
  450. /* --------------------------------------------------------------------------------------------- */
  451. void
  452. tty_print_char (int c)
  453. {
  454. SLsmg_write_char ((SLwchar_Type) ((unsigned int) c));
  455. }
  456. /* --------------------------------------------------------------------------------------------- */
  457. void
  458. tty_print_alt_char (int c, gboolean single)
  459. {
  460. #define DRAW(x, y) (x == y) \
  461. ? SLsmg_draw_object (SLsmg_get_row(), SLsmg_get_column(), x) \
  462. : SLsmg_write_char ((unsigned int) y)
  463. switch (c)
  464. {
  465. case ACS_VLINE:
  466. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT]);
  467. break;
  468. case ACS_HLINE:
  469. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ]);
  470. break;
  471. case ACS_LTEE:
  472. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_LEFTMIDDLE : MC_TTY_FRM_DLEFTMIDDLE]);
  473. break;
  474. case ACS_RTEE:
  475. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_RIGHTMIDDLE : MC_TTY_FRM_DRIGHTMIDDLE]);
  476. break;
  477. case ACS_ULCORNER:
  478. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_LEFTTOP : MC_TTY_FRM_DLEFTTOP]);
  479. break;
  480. case ACS_LLCORNER:
  481. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_LEFTBOTTOM : MC_TTY_FRM_DLEFTBOTTOM]);
  482. break;
  483. case ACS_URCORNER:
  484. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_RIGHTTOP : MC_TTY_FRM_DRIGHTTOP]);
  485. break;
  486. case ACS_LRCORNER:
  487. DRAW (c, mc_tty_frm[single ? MC_TTY_FRM_RIGHTBOTTOM : MC_TTY_FRM_DRIGHTBOTTOM]);
  488. break;
  489. case ACS_PLUS:
  490. DRAW (c, mc_tty_frm[MC_TTY_FRM_CROSS]);
  491. break;
  492. default:
  493. SLsmg_write_char ((unsigned int) c);
  494. }
  495. #undef DRAW
  496. }
  497. /* --------------------------------------------------------------------------------------------- */
  498. void
  499. tty_print_anychar (int c)
  500. {
  501. char str[6 + 1];
  502. if (c > 255)
  503. {
  504. int res = g_unichar_to_utf8 (c, str);
  505. if (res == 0)
  506. {
  507. str[0] = '.';
  508. str[1] = '\0';
  509. }
  510. else
  511. {
  512. str[res] = '\0';
  513. }
  514. SLsmg_write_string ((char *) str_term_form (str));
  515. }
  516. else
  517. {
  518. if (!is_printable (c))
  519. c = '.';
  520. SLsmg_write_char ((SLwchar_Type) ((unsigned int) c));
  521. }
  522. }
  523. /* --------------------------------------------------------------------------------------------- */
  524. void
  525. tty_print_string (const char *s)
  526. {
  527. SLsmg_write_string ((char *) str_term_form (s));
  528. }
  529. /* --------------------------------------------------------------------------------------------- */
  530. void
  531. tty_printf (const char *fmt, ...)
  532. {
  533. va_list args;
  534. va_start (args, fmt);
  535. SLsmg_vprintf ((char *) fmt, args);
  536. va_end (args);
  537. }
  538. /* --------------------------------------------------------------------------------------------- */
  539. char *
  540. tty_tgetstr (const char *cap)
  541. {
  542. return SLtt_tgetstr ((char *) cap);
  543. }
  544. /* --------------------------------------------------------------------------------------------- */
  545. void
  546. tty_refresh (void)
  547. {
  548. SLsmg_refresh ();
  549. }
  550. /* --------------------------------------------------------------------------------------------- */
  551. void
  552. tty_setup_sigwinch (void (*handler) (int))
  553. {
  554. #ifdef SIGWINCH
  555. struct sigaction act, oact;
  556. act.sa_handler = handler;
  557. sigemptyset (&act.sa_mask);
  558. act.sa_flags = 0;
  559. #ifdef SA_RESTART
  560. act.sa_flags |= SA_RESTART;
  561. #endif /* SA_RESTART */
  562. sigaction (SIGWINCH, &act, &oact);
  563. #endif /* SIGWINCH */
  564. }
  565. /* --------------------------------------------------------------------------------------------- */
  566. void
  567. tty_beep (void)
  568. {
  569. SLtt_beep ();
  570. }
  571. /* --------------------------------------------------------------------------------------------- */