tty-slang.c 18 KB

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