tty-ncurses.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. Interface to the terminal controlling library.
  3. Ncurses wrapper.
  4. Copyright (C) 2005, 2006, 2007, 2009, 2011
  5. The Free Software Foundation, Inc.
  6. Written by:
  7. Andrew Borodin <aborodin@vmail.ru>, 2009.
  8. Ilia Maslakov <il.smind@gmail.com>, 2009.
  9. This file is part of the Midnight Commander.
  10. The Midnight Commander is free software: you can redistribute it
  11. and/or modify it under the terms of the GNU General Public License as
  12. published by the Free Software Foundation, either version 3 of the License,
  13. or (at your option) any later version.
  14. The Midnight Commander is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /** \file
  22. * \brief Source: NCurses-based tty layer of Midnight-commander
  23. */
  24. #include <config.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include <signal.h>
  28. #include "lib/global.h"
  29. #include "lib/strutil.h" /* str_term_form */
  30. #ifndef WANT_TERM_H
  31. #define WANT_TERM_H
  32. #endif
  33. #include "tty-internal.h" /* mc_tty_normalize_from_utf8() */
  34. #include "tty.h"
  35. #include "color-internal.h"
  36. #include "mouse.h"
  37. #include "win.h"
  38. /* include at last !!! */
  39. #ifdef WANT_TERM_H
  40. #ifdef HAVE_NCURSES_TERM_H
  41. #include <ncurses/term.h>
  42. #else
  43. #include <term.h>
  44. #endif /* HAVE_NCURSES_TERM_H */
  45. #endif /* WANT_TERM_H */
  46. /*** global variables ****************************************************************************/
  47. /*** file scope macro definitions ****************************************************************/
  48. #if defined(_AIX) && !defined(CTRL)
  49. #define CTRL(x) ((x) & 0x1f)
  50. #endif
  51. #define yx_in_screen(y, x) \
  52. (y >= 0 && y < LINES && x >= 0 && x < COLS)
  53. /*** global variables ****************************************************************************/
  54. /*** file scope type declarations ****************************************************************/
  55. /*** file scope variables ************************************************************************/
  56. /* ncurses supports cursor positions only within window */
  57. /* We use our own cursor coordibates to support partially visible widgets */
  58. static int mc_curs_row, mc_curs_col;
  59. /*** file scope functions ************************************************************************/
  60. /* --------------------------------------------------------------------------------------------- */
  61. /* --------------------------------------------------------------------------------------------- */
  62. static void
  63. tty_setup_sigwinch (void (*handler) (int))
  64. {
  65. #if (NCURSES_VERSION_MAJOR >= 4) && defined (SIGWINCH)
  66. struct sigaction act, oact;
  67. act.sa_handler = handler;
  68. sigemptyset (&act.sa_mask);
  69. act.sa_flags = 0;
  70. #ifdef SA_RESTART
  71. act.sa_flags |= SA_RESTART;
  72. #endif /* SA_RESTART */
  73. sigaction (SIGWINCH, &act, &oact);
  74. #endif /* SIGWINCH */
  75. }
  76. /* --------------------------------------------------------------------------------------------- */
  77. static void
  78. sigwinch_handler (int dummy)
  79. {
  80. (void) dummy;
  81. mc_global.tty.winch_flag = TRUE;
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. /*** public functions ****************************************************************************/
  85. /* --------------------------------------------------------------------------------------------- */
  86. int
  87. mc_tty_normalize_lines_char (const char *ch)
  88. {
  89. char *str2;
  90. int res;
  91. struct mc_tty_lines_struct
  92. {
  93. const char *line;
  94. int line_code;
  95. } const lines_codes[] = {
  96. {"\342\224\230", ACS_LRCORNER}, /* ┌ */
  97. {"\342\224\224", ACS_LLCORNER}, /* └ */
  98. {"\342\224\220", ACS_URCORNER}, /* ┐ */
  99. {"\342\224\214", ACS_ULCORNER}, /* ┘ */
  100. {"\342\224\234", ACS_LTEE}, /* ├ */
  101. {"\342\224\244", ACS_RTEE}, /* ┤ */
  102. {"\342\224\254", ACS_TTEE}, /* ┬ */
  103. {"\342\224\264", ACS_BTEE}, /* ┴ */
  104. {"\342\224\200", ACS_HLINE}, /* ─ */
  105. {"\342\224\202", ACS_VLINE}, /* │ */
  106. {"\342\224\274", ACS_PLUS}, /* ┼ */
  107. {"\342\225\235", ACS_LRCORNER | A_BOLD}, /* ╔ */
  108. {"\342\225\232", ACS_LLCORNER | A_BOLD}, /* ╚ */
  109. {"\342\225\227", ACS_URCORNER | A_BOLD}, /* ╗ */
  110. {"\342\225\224", ACS_ULCORNER | A_BOLD}, /* ╝ */
  111. {"\342\225\237", ACS_LTEE | A_BOLD}, /* ╟ */
  112. {"\342\225\242", ACS_RTEE | A_BOLD}, /* ╢ */
  113. {"\342\225\244", ACS_TTEE | A_BOLD}, /* ╤ */
  114. {"\342\225\247", ACS_BTEE | A_BOLD}, /* ╧ */
  115. {"\342\225\220", ACS_HLINE | A_BOLD}, /* ═ */
  116. {"\342\225\221", ACS_VLINE | A_BOLD}, /* ║ */
  117. {NULL, 0}
  118. };
  119. if (ch == NULL)
  120. return (int) ' ';
  121. for (res = 0; lines_codes[res].line; res++)
  122. {
  123. if (strcmp (ch, lines_codes[res].line) == 0)
  124. return lines_codes[res].line_code;
  125. }
  126. str2 = mc_tty_normalize_from_utf8 (ch);
  127. res = g_utf8_get_char_validated (str2, -1);
  128. if (res < 0)
  129. res = (unsigned char) str2[0];
  130. g_free (str2);
  131. return res;
  132. }
  133. /* --------------------------------------------------------------------------------------------- */
  134. void
  135. tty_init (gboolean mouse_enable, gboolean is_xterm)
  136. {
  137. initscr ();
  138. #ifdef HAVE_ESCDELAY
  139. /*
  140. * If ncurses exports the ESCDELAY variable, it should be set to
  141. * a low value, or you'll experience a delay in processing escape
  142. * sequences that are recognized by mc (e.g. Esc-Esc). On the other
  143. * hand, making ESCDELAY too small can result in some sequences
  144. * (e.g. cursor arrows) being reported as separate keys under heavy
  145. * processor load, and this can be a problem if mc hasn't learned
  146. * them in the "Learn Keys" dialog. The value is in milliseconds.
  147. */
  148. ESCDELAY = 200;
  149. #endif /* HAVE_ESCDELAY */
  150. /* use Ctrl-g to generate SIGINT */
  151. cur_term->Nttyb.c_cc[VINTR] = CTRL ('g'); /* ^g */
  152. /* disable SIGQUIT to allow use Ctrl-\ key */
  153. cur_term->Nttyb.c_cc[VQUIT] = NULL_VALUE;
  154. tcsetattr (cur_term->Filedes, TCSANOW, &cur_term->Nttyb);
  155. tty_start_interrupt_key ();
  156. if (!mouse_enable)
  157. use_mouse_p = MOUSE_DISABLED;
  158. tty_init_xterm_support (is_xterm); /* do it before do_enter_ca_mode() call */
  159. init_mouse ();
  160. do_enter_ca_mode ();
  161. tty_raw_mode ();
  162. noecho ();
  163. keypad (stdscr, TRUE);
  164. nodelay (stdscr, FALSE);
  165. clock_init ();
  166. tty_setup_sigwinch (sigwinch_handler);
  167. }
  168. /* --------------------------------------------------------------------------------------------- */
  169. void
  170. tty_shutdown (void)
  171. {
  172. disable_mouse ();
  173. tty_reset_shell_mode ();
  174. tty_noraw_mode ();
  175. tty_keypad (FALSE);
  176. tty_reset_screen ();
  177. do_exit_ca_mode ();
  178. }
  179. /* --------------------------------------------------------------------------------------------- */
  180. void
  181. tty_reset_prog_mode (void)
  182. {
  183. reset_prog_mode ();
  184. }
  185. /* --------------------------------------------------------------------------------------------- */
  186. void
  187. tty_reset_shell_mode (void)
  188. {
  189. reset_shell_mode ();
  190. }
  191. /* --------------------------------------------------------------------------------------------- */
  192. void
  193. tty_raw_mode (void)
  194. {
  195. raw (); /* FIXME: uneeded? */
  196. cbreak ();
  197. }
  198. /* --------------------------------------------------------------------------------------------- */
  199. void
  200. tty_noraw_mode (void)
  201. {
  202. nocbreak (); /* FIXME: unneeded? */
  203. noraw ();
  204. }
  205. /* --------------------------------------------------------------------------------------------- */
  206. void
  207. tty_noecho (void)
  208. {
  209. noecho ();
  210. }
  211. /* --------------------------------------------------------------------------------------------- */
  212. int
  213. tty_flush_input (void)
  214. {
  215. return flushinp ();
  216. }
  217. /* --------------------------------------------------------------------------------------------- */
  218. void
  219. tty_keypad (gboolean set)
  220. {
  221. keypad (stdscr, (bool) set);
  222. }
  223. /* --------------------------------------------------------------------------------------------- */
  224. void
  225. tty_nodelay (gboolean set)
  226. {
  227. nodelay (stdscr, (bool) set);
  228. }
  229. /* --------------------------------------------------------------------------------------------- */
  230. int
  231. tty_baudrate (void)
  232. {
  233. return baudrate ();
  234. }
  235. /* --------------------------------------------------------------------------------------------- */
  236. int
  237. tty_lowlevel_getch (void)
  238. {
  239. return getch ();
  240. }
  241. /* --------------------------------------------------------------------------------------------- */
  242. int
  243. tty_reset_screen (void)
  244. {
  245. return endwin ();
  246. }
  247. /* --------------------------------------------------------------------------------------------- */
  248. void
  249. tty_touch_screen (void)
  250. {
  251. touchwin (stdscr);
  252. }
  253. /* --------------------------------------------------------------------------------------------- */
  254. void
  255. tty_gotoyx (int y, int x)
  256. {
  257. mc_curs_row = y;
  258. mc_curs_col = x;
  259. if (y < 0)
  260. y = 0;
  261. if (y >= LINES)
  262. y = LINES - 1;
  263. if (x < 0)
  264. x = 0;
  265. if (x >= COLS)
  266. x = COLS - 1;
  267. move (y, x);
  268. }
  269. /* --------------------------------------------------------------------------------------------- */
  270. void
  271. tty_getyx (int *py, int *px)
  272. {
  273. *py = mc_curs_row;
  274. *px = mc_curs_col;
  275. }
  276. /* --------------------------------------------------------------------------------------------- */
  277. void
  278. tty_draw_hline (int y, int x, int ch, int len)
  279. {
  280. int x1;
  281. if (y < 0 || y >= LINES || x >= COLS)
  282. return;
  283. x1 = x;
  284. if (x < 0)
  285. {
  286. len += x;
  287. if (len <= 0)
  288. return;
  289. x = 0;
  290. }
  291. if ((chtype) ch == ACS_HLINE)
  292. ch = mc_tty_frm[MC_TTY_FRM_HORIZ];
  293. move (y, x);
  294. hline (ch, len);
  295. move (y, x1);
  296. mc_curs_row = y;
  297. mc_curs_col = x1;
  298. }
  299. /* --------------------------------------------------------------------------------------------- */
  300. void
  301. tty_draw_vline (int y, int x, int ch, int len)
  302. {
  303. int y1;
  304. if (x < 0 || x >= COLS || y >= LINES)
  305. return;
  306. y1 = y;
  307. if (y < 0)
  308. {
  309. len += y;
  310. if (len <= 0)
  311. return;
  312. y = 0;
  313. }
  314. if ((chtype) ch == ACS_VLINE)
  315. ch = mc_tty_frm[MC_TTY_FRM_VERT];
  316. move (y, x);
  317. vline (ch, len);
  318. move (y1, x);
  319. mc_curs_row = y1;
  320. mc_curs_col = x;
  321. }
  322. /* --------------------------------------------------------------------------------------------- */
  323. void
  324. tty_fill_region (int y, int x, int rows, int cols, unsigned char ch)
  325. {
  326. int i;
  327. if (y < 0)
  328. {
  329. rows += y;
  330. if (rows <= 0)
  331. return;
  332. y = 0;
  333. }
  334. if (x < 0)
  335. {
  336. cols += x;
  337. if (cols <= 0)
  338. return;
  339. x = 0;
  340. }
  341. if (y + rows > LINES)
  342. rows = LINES - y;
  343. if (x + cols > COLS)
  344. cols = COLS - x;
  345. for (i = 0; i < rows; i++)
  346. {
  347. move (y + i, x);
  348. hline (ch, cols);
  349. }
  350. move (y, x);
  351. mc_curs_row = y;
  352. mc_curs_col = x;
  353. }
  354. /* --------------------------------------------------------------------------------------------- */
  355. void
  356. tty_set_alt_charset (gboolean alt_charset)
  357. {
  358. (void) alt_charset;
  359. }
  360. /* --------------------------------------------------------------------------------------------- */
  361. void
  362. tty_display_8bit (gboolean what)
  363. {
  364. meta (stdscr, (int) what);
  365. }
  366. /* --------------------------------------------------------------------------------------------- */
  367. void
  368. tty_print_char (int c)
  369. {
  370. if (yx_in_screen (mc_curs_row, mc_curs_col))
  371. addch (c);
  372. mc_curs_col++;
  373. }
  374. /* --------------------------------------------------------------------------------------------- */
  375. void
  376. tty_print_anychar (int c)
  377. {
  378. unsigned char str[6 + 1];
  379. if (mc_global.utf8_display || c > 255)
  380. {
  381. int res;
  382. res = g_unichar_to_utf8 (c, (char *) str);
  383. if (res == 0)
  384. {
  385. if (yx_in_screen (mc_curs_row, mc_curs_col))
  386. addch ('.');
  387. mc_curs_col++;
  388. }
  389. else
  390. {
  391. const char *s;
  392. str[res] = '\0';
  393. s = str_term_form ((char *) str);
  394. if (yx_in_screen (mc_curs_row, mc_curs_col))
  395. addstr (s);
  396. if (g_unichar_iswide (c))
  397. mc_curs_col += 2;
  398. else if (!g_unichar_iszerowidth (c))
  399. mc_curs_col++;
  400. }
  401. }
  402. else
  403. {
  404. if (yx_in_screen (mc_curs_row, mc_curs_col))
  405. addch (c);
  406. mc_curs_col++;
  407. }
  408. }
  409. /* --------------------------------------------------------------------------------------------- */
  410. void
  411. tty_print_alt_char (int c, gboolean single)
  412. {
  413. if (yx_in_screen (mc_curs_row, mc_curs_col))
  414. {
  415. if ((chtype) c == ACS_VLINE)
  416. c = mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT];
  417. else if ((chtype) c == ACS_HLINE)
  418. c = mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ];
  419. else if ((chtype) c == ACS_LTEE)
  420. c = mc_tty_frm[single ? MC_TTY_FRM_LEFTMIDDLE : MC_TTY_FRM_DLEFTMIDDLE];
  421. else if ((chtype) c == ACS_RTEE)
  422. c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTMIDDLE : MC_TTY_FRM_DRIGHTMIDDLE];
  423. else if ((chtype) c == ACS_ULCORNER)
  424. c = mc_tty_frm[single ? MC_TTY_FRM_LEFTTOP : MC_TTY_FRM_DLEFTTOP];
  425. else if ((chtype) c == ACS_LLCORNER)
  426. c = mc_tty_frm[single ? MC_TTY_FRM_LEFTBOTTOM : MC_TTY_FRM_DLEFTBOTTOM];
  427. else if ((chtype) c == ACS_URCORNER)
  428. c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTTOP : MC_TTY_FRM_DRIGHTTOP];
  429. else if ((chtype) c == ACS_LRCORNER)
  430. c = mc_tty_frm[single ? MC_TTY_FRM_RIGHTBOTTOM : MC_TTY_FRM_DRIGHTBOTTOM];
  431. else if ((chtype) c == ACS_PLUS)
  432. c = mc_tty_frm[MC_TTY_FRM_CROSS];
  433. addch (c);
  434. }
  435. mc_curs_col++;
  436. }
  437. /* --------------------------------------------------------------------------------------------- */
  438. void
  439. tty_print_string (const char *s)
  440. {
  441. int len;
  442. int start = 0;
  443. s = str_term_form (s);
  444. len = str_term_width1 (s);
  445. /* line is upper or below the screen or entire line is before or after scrren */
  446. if (mc_curs_row < 0 || mc_curs_row >= LINES || mc_curs_col + len <= 0 || mc_curs_col >= COLS)
  447. {
  448. mc_curs_col += len;
  449. return;
  450. }
  451. /* skip invisible left part */
  452. if (mc_curs_col < 0)
  453. {
  454. start = -mc_curs_col;
  455. len += mc_curs_col;
  456. mc_curs_col = 0;
  457. }
  458. mc_curs_col += len;
  459. if (mc_curs_col >= COLS)
  460. len = COLS - (mc_curs_col - len);
  461. addstr (str_term_substring (s, start, len));
  462. }
  463. /* --------------------------------------------------------------------------------------------- */
  464. void
  465. tty_printf (const char *fmt, ...)
  466. {
  467. va_list args;
  468. char buf[BUF_1K]; /* FIXME: is it enough? */
  469. va_start (args, fmt);
  470. g_vsnprintf (buf, sizeof (buf), fmt, args);
  471. va_end (args);
  472. tty_print_string (buf);
  473. }
  474. /* --------------------------------------------------------------------------------------------- */
  475. char *
  476. tty_tgetstr (const char *cap)
  477. {
  478. char *unused = NULL;
  479. return tgetstr ((char *) cap, &unused);
  480. }
  481. /* --------------------------------------------------------------------------------------------- */
  482. void
  483. tty_refresh (void)
  484. {
  485. refresh ();
  486. doupdate ();
  487. }
  488. /* --------------------------------------------------------------------------------------------- */
  489. void
  490. tty_beep (void)
  491. {
  492. beep ();
  493. }
  494. /* --------------------------------------------------------------------------------------------- */