tty.c 9.6 KB

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