tty.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. Interface to the terminal controlling library.
  3. Copyright (C) 2005-2017
  4. 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 <string.h> /* memset() */
  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. int mc_tty_frm[MC_TTY_FRM_MAX];
  40. /*** file scope macro definitions ****************************************************************/
  41. /*** file scope type declarations ****************************************************************/
  42. /*** file scope variables ************************************************************************/
  43. static SIG_ATOMIC_VOLATILE_T got_interrupt = 0;
  44. /*** file scope functions ************************************************************************/
  45. /* --------------------------------------------------------------------------------------------- */
  46. static void
  47. sigintr_handler (int signo)
  48. {
  49. (void) &signo;
  50. got_interrupt = 1;
  51. }
  52. /* --------------------------------------------------------------------------------------------- */
  53. /*** public functions ****************************************************************************/
  54. /* --------------------------------------------------------------------------------------------- */
  55. /**
  56. * Check terminal type. If $TERM is not set or value is empty, mc finishes with EXIT_FAILURE.
  57. *
  58. * @param force_xterm Set forced the XTerm type
  59. *
  60. * @return true if @param force_xterm is true or value of $TERM is one of term*, konsole*
  61. * rxvt*, Eterm or dtterm
  62. */
  63. gboolean
  64. tty_check_term (gboolean force_xterm)
  65. {
  66. const char *termvalue;
  67. const char *xdisplay;
  68. termvalue = getenv ("TERM");
  69. if (termvalue == NULL || *termvalue == '\0')
  70. {
  71. fputs (_("The TERM environment variable is unset!\n"), stderr);
  72. exit (EXIT_FAILURE);
  73. }
  74. xdisplay = getenv ("DISPLAY");
  75. if (xdisplay != NULL && *xdisplay == '\0')
  76. xdisplay = NULL;
  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
  81. || strcmp (termvalue, "dtterm") == 0
  82. || (strncmp (termvalue, "screen", 6) == 0 && xdisplay != NULL);
  83. }
  84. /* --------------------------------------------------------------------------------------------- */
  85. extern void
  86. tty_start_interrupt_key (void)
  87. {
  88. struct sigaction act;
  89. memset (&act, 0, sizeof (act));
  90. act.sa_handler = sigintr_handler;
  91. sigemptyset (&act.sa_mask);
  92. #ifdef SA_RESTART
  93. act.sa_flags = SA_RESTART;
  94. #endif /* SA_RESTART */
  95. sigaction (SIGINT, &act, NULL);
  96. }
  97. /* --------------------------------------------------------------------------------------------- */
  98. extern void
  99. tty_enable_interrupt_key (void)
  100. {
  101. struct sigaction act;
  102. memset (&act, 0, sizeof (act));
  103. act.sa_handler = sigintr_handler;
  104. sigemptyset (&act.sa_mask);
  105. sigaction (SIGINT, &act, NULL);
  106. got_interrupt = 0;
  107. }
  108. /* --------------------------------------------------------------------------------------------- */
  109. extern void
  110. tty_disable_interrupt_key (void)
  111. {
  112. struct sigaction act;
  113. memset (&act, 0, sizeof (act));
  114. act.sa_handler = SIG_IGN;
  115. sigemptyset (&act.sa_mask);
  116. sigaction (SIGINT, &act, NULL);
  117. }
  118. /* --------------------------------------------------------------------------------------------- */
  119. extern gboolean
  120. tty_got_interrupt (void)
  121. {
  122. gboolean rv;
  123. rv = (got_interrupt != 0);
  124. got_interrupt = 0;
  125. return rv;
  126. }
  127. /* --------------------------------------------------------------------------------------------- */
  128. void
  129. tty_print_one_hline (gboolean single)
  130. {
  131. tty_print_alt_char (ACS_HLINE, single);
  132. }
  133. /* --------------------------------------------------------------------------------------------- */
  134. void
  135. tty_print_one_vline (gboolean single)
  136. {
  137. tty_print_alt_char (ACS_VLINE, single);
  138. }
  139. /* --------------------------------------------------------------------------------------------- */
  140. void
  141. tty_draw_box (int y, int x, int ys, int xs, gboolean single)
  142. {
  143. int y2, x2;
  144. if (ys <= 0 || xs <= 0)
  145. return;
  146. ys--;
  147. xs--;
  148. y2 = y + ys;
  149. x2 = x + xs;
  150. tty_draw_vline (y, x, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  151. tty_draw_vline (y, x2, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  152. tty_draw_hline (y, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  153. tty_draw_hline (y2, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  154. tty_gotoyx (y, x);
  155. tty_print_alt_char (ACS_ULCORNER, single);
  156. tty_gotoyx (y2, x);
  157. tty_print_alt_char (ACS_LLCORNER, single);
  158. tty_gotoyx (y, x2);
  159. tty_print_alt_char (ACS_URCORNER, single);
  160. tty_gotoyx (y2, x2);
  161. tty_print_alt_char (ACS_LRCORNER, single);
  162. }
  163. /* --------------------------------------------------------------------------------------------- */
  164. char *
  165. mc_tty_normalize_from_utf8 (const char *str)
  166. {
  167. GIConv conv;
  168. GString *buffer;
  169. const char *_system_codepage = str_detect_termencoding ();
  170. if (str_isutf8 (_system_codepage))
  171. return g_strdup (str);
  172. conv = g_iconv_open (_system_codepage, "UTF-8");
  173. if (conv == INVALID_CONV)
  174. return g_strdup (str);
  175. buffer = g_string_new ("");
  176. if (str_convert (conv, str, buffer) == ESTR_FAILURE)
  177. {
  178. g_string_free (buffer, TRUE);
  179. str_close_conv (conv);
  180. return g_strdup (str);
  181. }
  182. str_close_conv (conv);
  183. return g_string_free (buffer, FALSE);
  184. }
  185. /* --------------------------------------------------------------------------------------------- */
  186. /** Resize given terminal using TIOCSWINSZ, return ioctl() result */
  187. int
  188. tty_resize (int fd)
  189. {
  190. #if defined TIOCSWINSZ
  191. struct winsize tty_size;
  192. tty_size.ws_row = LINES;
  193. tty_size.ws_col = COLS;
  194. tty_size.ws_xpixel = tty_size.ws_ypixel = 0;
  195. return ioctl (fd, TIOCSWINSZ, &tty_size);
  196. #else
  197. return 0;
  198. #endif
  199. }
  200. /* --------------------------------------------------------------------------------------------- */
  201. void
  202. tty_init_xterm_support (gboolean is_xterm)
  203. {
  204. const char *termvalue;
  205. termvalue = getenv ("TERM");
  206. /* Check mouse and ca capabilities */
  207. /* terminfo/termcap structures have been already initialized,
  208. in slang_init() or/and init_curses() */
  209. /* Check terminfo at first, then check termcap */
  210. xmouse_seq = tty_tgetstr ("kmous");
  211. if (xmouse_seq == NULL)
  212. xmouse_seq = tty_tgetstr ("Km");
  213. smcup = tty_tgetstr ("smcup");
  214. if (smcup == NULL)
  215. smcup = tty_tgetstr ("ti");
  216. rmcup = tty_tgetstr ("rmcup");
  217. if (rmcup == NULL)
  218. rmcup = tty_tgetstr ("te");
  219. if (strcmp (termvalue, "cygwin") == 0)
  220. {
  221. is_xterm = TRUE;
  222. use_mouse_p = MOUSE_DISABLED;
  223. }
  224. if (is_xterm)
  225. {
  226. /* Default to the standard xterm sequence */
  227. if (xmouse_seq == NULL)
  228. xmouse_seq = ESC_STR "[M";
  229. /* Enable mouse unless explicitly disabled by --nomouse */
  230. if (use_mouse_p != MOUSE_DISABLED)
  231. {
  232. if (mc_global.tty.old_mouse)
  233. use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
  234. else
  235. {
  236. /* FIXME: this dirty hack to set supported type of tracking the mouse */
  237. const char *color_term = getenv ("COLORTERM");
  238. if (strncmp (termvalue, "rxvt", 4) == 0 ||
  239. (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
  240. strcmp (termvalue, "Eterm") == 0)
  241. use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
  242. else
  243. use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
  244. }
  245. }
  246. }
  247. /* No termcap for SGR extended mouse (yet), hardcode it for now */
  248. if (xmouse_seq != NULL)
  249. xmouse_extended_seq = ESC_STR "[<";
  250. }
  251. /* --------------------------------------------------------------------------------------------- */