tty-slang.c 14 KB

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