tty-slang.c 14 KB

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