tty.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. Interface to the terminal controlling library.
  3. Copyright (C) 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
  4. Written by:
  5. Roland Illig <roland.illig@gmx.de>, 2005.
  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 tty.c
  22. * \brief Source: %interface to the terminal controlling library
  23. */
  24. #include <config.h>
  25. #include <signal.h>
  26. #include <stdarg.h>
  27. #include <stdlib.h>
  28. #include <unistd.h> /* exit() */
  29. #ifdef HAVE_SYS_IOCTL_H
  30. #include <sys/ioctl.h>
  31. #endif
  32. #include "lib/global.h"
  33. #include "lib/strutil.h"
  34. #include "tty.h"
  35. #include "tty-internal.h"
  36. #include "mouse.h" /* use_mouse_p */
  37. #include "win.h"
  38. /*** global variables ****************************************************************************/
  39. /* If true program softkeys (HP terminals only) on startup and after every
  40. command ran in the subshell to the description found in the termcap/terminfo
  41. database */
  42. int reset_hp_softkeys = 0;
  43. int mc_tty_frm[MC_TTY_FRM_MAX];
  44. /*** file scope macro definitions ****************************************************************/
  45. /*** file scope type declarations ****************************************************************/
  46. /*** file scope variables ************************************************************************/
  47. static volatile sig_atomic_t got_interrupt = 0;
  48. /*** file scope functions ************************************************************************/
  49. /* --------------------------------------------------------------------------------------------- */
  50. static void
  51. sigintr_handler (int signo)
  52. {
  53. (void) &signo;
  54. got_interrupt = 1;
  55. }
  56. /* --------------------------------------------------------------------------------------------- */
  57. /*** public functions ****************************************************************************/
  58. /* --------------------------------------------------------------------------------------------- */
  59. /**
  60. * Check terminal type. If $TERM is not set or value is empty, mc finishes with EXIT_FAILURE.
  61. *
  62. * @param force_xterm Set forced the XTerm type
  63. *
  64. * @return true if @param force_xterm is true or value of $TERM is one of term*, konsole*
  65. * rxvt*, Eterm or dtterm
  66. */
  67. gboolean
  68. tty_check_term (gboolean force_xterm)
  69. {
  70. const char *termvalue;
  71. termvalue = getenv ("TERM");
  72. if (termvalue == NULL || *termvalue == '\0')
  73. {
  74. fputs (_("The TERM environment variable is unset!\n"), stderr);
  75. exit (EXIT_FAILURE);
  76. }
  77. return force_xterm || strncmp (termvalue, "xterm", 5) == 0
  78. || strncmp (termvalue, "konsole", 7) == 0
  79. || strncmp (termvalue, "rxvt", 4) == 0
  80. || strcmp (termvalue, "Eterm") == 0 || strcmp (termvalue, "dtterm") == 0;
  81. }
  82. /* --------------------------------------------------------------------------------------------- */
  83. extern void
  84. tty_start_interrupt_key (void)
  85. {
  86. struct sigaction act;
  87. act.sa_handler = sigintr_handler;
  88. sigemptyset (&act.sa_mask);
  89. act.sa_flags = SA_RESTART;
  90. sigaction (SIGINT, &act, NULL);
  91. }
  92. /* --------------------------------------------------------------------------------------------- */
  93. extern void
  94. tty_enable_interrupt_key (void)
  95. {
  96. struct sigaction act;
  97. act.sa_handler = sigintr_handler;
  98. sigemptyset (&act.sa_mask);
  99. act.sa_flags = 0;
  100. sigaction (SIGINT, &act, NULL);
  101. got_interrupt = 0;
  102. }
  103. /* --------------------------------------------------------------------------------------------- */
  104. extern void
  105. tty_disable_interrupt_key (void)
  106. {
  107. struct sigaction act;
  108. act.sa_handler = SIG_IGN;
  109. sigemptyset (&act.sa_mask);
  110. act.sa_flags = 0;
  111. sigaction (SIGINT, &act, NULL);
  112. }
  113. /* --------------------------------------------------------------------------------------------- */
  114. extern gboolean
  115. tty_got_interrupt (void)
  116. {
  117. gboolean rv;
  118. rv = (got_interrupt != 0);
  119. got_interrupt = 0;
  120. return rv;
  121. }
  122. /* --------------------------------------------------------------------------------------------- */
  123. void
  124. tty_print_one_hline (gboolean single)
  125. {
  126. tty_print_alt_char (ACS_HLINE, single);
  127. }
  128. /* --------------------------------------------------------------------------------------------- */
  129. void
  130. tty_print_one_vline (gboolean single)
  131. {
  132. tty_print_alt_char (ACS_VLINE, single);
  133. }
  134. /* --------------------------------------------------------------------------------------------- */
  135. void
  136. tty_draw_box (int y, int x, int ys, int xs, gboolean single)
  137. {
  138. ys--;
  139. xs--;
  140. tty_draw_vline (y, x, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  141. tty_draw_vline (y, x + xs, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  142. tty_draw_hline (y, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  143. tty_draw_hline (y + ys, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  144. tty_gotoyx (y, x);
  145. tty_print_alt_char (ACS_ULCORNER, single);
  146. tty_gotoyx (y + ys, x);
  147. tty_print_alt_char (ACS_LLCORNER, single);
  148. tty_gotoyx (y, x + xs);
  149. tty_print_alt_char (ACS_URCORNER, single);
  150. tty_gotoyx (y + ys, x + xs);
  151. tty_print_alt_char (ACS_LRCORNER, single);
  152. }
  153. /* --------------------------------------------------------------------------------------------- */
  154. char *
  155. mc_tty_normalize_from_utf8 (const char *str)
  156. {
  157. GIConv conv;
  158. GString *buffer;
  159. const char *_system_codepage = str_detect_termencoding ();
  160. if (str_isutf8 (_system_codepage))
  161. return g_strdup (str);
  162. conv = g_iconv_open (_system_codepage, "UTF-8");
  163. if (conv == INVALID_CONV)
  164. return g_strdup (str);
  165. buffer = g_string_new ("");
  166. if (str_convert (conv, str, buffer) == ESTR_FAILURE)
  167. {
  168. g_string_free (buffer, TRUE);
  169. str_close_conv (conv);
  170. return g_strdup (str);
  171. }
  172. str_close_conv (conv);
  173. return g_string_free (buffer, FALSE);
  174. }
  175. /* --------------------------------------------------------------------------------------------- */
  176. /** Resize given terminal using TIOCSWINSZ, return ioctl() result */
  177. int
  178. tty_resize (int fd)
  179. {
  180. #if defined TIOCSWINSZ
  181. struct winsize tty_size;
  182. tty_size.ws_row = LINES;
  183. tty_size.ws_col = COLS;
  184. tty_size.ws_xpixel = tty_size.ws_ypixel = 0;
  185. return ioctl (fd, TIOCSWINSZ, &tty_size);
  186. #else
  187. return 0;
  188. #endif
  189. }
  190. /* --------------------------------------------------------------------------------------------- */
  191. void
  192. tty_change_screen_size (void)
  193. {
  194. #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
  195. #if defined TIOCGWINSZ
  196. struct winsize winsz;
  197. winsz.ws_col = winsz.ws_row = 0;
  198. /* Ioctl on the STDIN_FILENO */
  199. ioctl (0, TIOCGWINSZ, &winsz);
  200. if (winsz.ws_col && winsz.ws_row)
  201. {
  202. #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
  203. resizeterm (winsz.ws_row, winsz.ws_col);
  204. clearok (stdscr, TRUE); /* sigwinch's should use a semaphore! */
  205. #else
  206. COLS = winsz.ws_col;
  207. LINES = winsz.ws_row;
  208. #endif
  209. #ifdef HAVE_SUBSHELL_SUPPORT
  210. if (!mc_global.tty.use_subshell)
  211. return;
  212. tty_resize (mc_global.tty.subshell_pty);
  213. #endif
  214. }
  215. #endif /* TIOCGWINSZ */
  216. #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
  217. }
  218. /* --------------------------------------------------------------------------------------------- */
  219. void
  220. tty_init_xterm_support (gboolean is_xterm)
  221. {
  222. const char *termvalue;
  223. termvalue = getenv ("TERM");
  224. /* Check mouse and ca capabilities */
  225. /* terminfo/termcap structures have been already initialized,
  226. in slang_init() or/and init_curses() */
  227. /* Check terminfo at first, then check termcap */
  228. xmouse_seq = tty_tgetstr ("kmous");
  229. if (xmouse_seq == NULL)
  230. xmouse_seq = tty_tgetstr ("Km");
  231. smcup = tty_tgetstr ("smcup");
  232. if (smcup == NULL)
  233. smcup = tty_tgetstr ("ti");
  234. rmcup = tty_tgetstr ("rmcup");
  235. if (rmcup == NULL)
  236. rmcup = tty_tgetstr ("te");
  237. if (strcmp (termvalue, "cygwin") == 0)
  238. {
  239. is_xterm = TRUE;
  240. use_mouse_p = MOUSE_DISABLED;
  241. }
  242. if (is_xterm)
  243. {
  244. /* Default to the standard xterm sequence */
  245. if (xmouse_seq == NULL)
  246. xmouse_seq = ESC_STR "[M";
  247. /* Enable mouse unless explicitly disabled by --nomouse */
  248. if (use_mouse_p != MOUSE_DISABLED)
  249. {
  250. if (mc_global.tty.old_mouse)
  251. use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
  252. else
  253. {
  254. /* FIXME: this dirty hack to set supported type of tracking the mouse */
  255. const char *color_term = getenv ("COLORTERM");
  256. if (strncmp (termvalue, "rxvt", 4) == 0 ||
  257. (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
  258. strcmp (termvalue, "Eterm") == 0)
  259. use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
  260. else
  261. use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
  262. }
  263. }
  264. }
  265. }
  266. /* --------------------------------------------------------------------------------------------- */