tty.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #include "lib/global.h"
  30. #include "lib/strutil.h"
  31. #include "tty.h"
  32. #include "tty-internal.h"
  33. #include "mouse.h" /* use_mouse_p */
  34. #include "win.h"
  35. /*** global variables ****************************************************************************/
  36. /* If true program softkeys (HP terminals only) on startup and after every
  37. command ran in the subshell to the description found in the termcap/terminfo
  38. database */
  39. int reset_hp_softkeys = 0;
  40. /* If true lines are drown by spaces */
  41. gboolean slow_tty = FALSE;
  42. /* If true use +, -, | for line drawing */
  43. gboolean ugly_line_drawing = FALSE;
  44. int mc_tty_frm[MC_TTY_FRM_MAX];
  45. /*** file scope macro definitions ****************************************************************/
  46. /*** file scope type declarations ****************************************************************/
  47. /*** file scope variables ************************************************************************/
  48. static volatile sig_atomic_t got_interrupt = 0;
  49. /*** file scope functions ************************************************************************/
  50. /* --------------------------------------------------------------------------------------------- */
  51. static void
  52. sigintr_handler (int signo)
  53. {
  54. (void) &signo;
  55. got_interrupt = 1;
  56. }
  57. /* --------------------------------------------------------------------------------------------- */
  58. /*** public functions ****************************************************************************/
  59. /* --------------------------------------------------------------------------------------------- */
  60. /**
  61. * Check terminal type. If $TERM is not set or value is empty, mc finishes with EXIT_FAILURE.
  62. *
  63. * @param force_xterm Set forced the XTerm type
  64. *
  65. * @return true if @param force_xterm is true or value of $TERM is one of term*, konsole*
  66. * rxvt*, Eterm or dtterm
  67. */
  68. gboolean
  69. tty_check_term (gboolean force_xterm)
  70. {
  71. const char *termvalue;
  72. termvalue = getenv ("TERM");
  73. if (termvalue == NULL || *termvalue == '\0')
  74. {
  75. fputs (_("The TERM environment variable is unset!\n"), stderr);
  76. exit (EXIT_FAILURE);
  77. }
  78. return force_xterm || strncmp (termvalue, "xterm", 5) == 0
  79. || strncmp (termvalue, "konsole", 7) == 0
  80. || strncmp (termvalue, "rxvt", 4) == 0
  81. || strcmp (termvalue, "Eterm") == 0 || strcmp (termvalue, "dtterm") == 0;
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. extern gboolean
  85. tty_is_slow (void)
  86. {
  87. return slow_tty;
  88. }
  89. /* --------------------------------------------------------------------------------------------- */
  90. extern void
  91. tty_start_interrupt_key (void)
  92. {
  93. struct sigaction act;
  94. act.sa_handler = sigintr_handler;
  95. sigemptyset (&act.sa_mask);
  96. act.sa_flags = SA_RESTART;
  97. sigaction (SIGINT, &act, NULL);
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. extern void
  101. tty_enable_interrupt_key (void)
  102. {
  103. struct sigaction act;
  104. act.sa_handler = sigintr_handler;
  105. sigemptyset (&act.sa_mask);
  106. act.sa_flags = 0;
  107. sigaction (SIGINT, &act, NULL);
  108. got_interrupt = 0;
  109. }
  110. /* --------------------------------------------------------------------------------------------- */
  111. extern void
  112. tty_disable_interrupt_key (void)
  113. {
  114. struct sigaction act;
  115. act.sa_handler = SIG_IGN;
  116. sigemptyset (&act.sa_mask);
  117. act.sa_flags = 0;
  118. sigaction (SIGINT, &act, NULL);
  119. }
  120. /* --------------------------------------------------------------------------------------------- */
  121. extern gboolean
  122. tty_got_interrupt (void)
  123. {
  124. gboolean rv;
  125. rv = (got_interrupt != 0);
  126. got_interrupt = 0;
  127. return rv;
  128. }
  129. /* --------------------------------------------------------------------------------------------- */
  130. void
  131. tty_print_one_hline (gboolean single)
  132. {
  133. tty_print_alt_char (ACS_HLINE, single);
  134. }
  135. /* --------------------------------------------------------------------------------------------- */
  136. void
  137. tty_print_one_vline (gboolean single)
  138. {
  139. tty_print_alt_char (ACS_VLINE, single);
  140. }
  141. /* --------------------------------------------------------------------------------------------- */
  142. void
  143. tty_draw_box (int y, int x, int ys, int xs, gboolean single)
  144. {
  145. ys--;
  146. xs--;
  147. tty_draw_vline (y, x, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  148. tty_draw_vline (y, x + xs, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  149. tty_draw_hline (y, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  150. tty_draw_hline (y + ys, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  151. tty_gotoyx (y, x);
  152. tty_print_alt_char (ACS_ULCORNER, single);
  153. tty_gotoyx (y + ys, x);
  154. tty_print_alt_char (ACS_LLCORNER, single);
  155. tty_gotoyx (y, x + xs);
  156. tty_print_alt_char (ACS_URCORNER, single);
  157. tty_gotoyx (y + ys, x + xs);
  158. tty_print_alt_char (ACS_LRCORNER, single);
  159. }
  160. /* --------------------------------------------------------------------------------------------- */
  161. char *
  162. mc_tty_normalize_from_utf8 (const char *str)
  163. {
  164. GIConv conv;
  165. GString *buffer;
  166. const char *_system_codepage = str_detect_termencoding ();
  167. if (str_isutf8 (_system_codepage))
  168. return g_strdup (str);
  169. conv = g_iconv_open (_system_codepage, "UTF-8");
  170. if (conv == INVALID_CONV)
  171. return g_strdup (str);
  172. buffer = g_string_new ("");
  173. if (str_convert (conv, str, buffer) == ESTR_FAILURE)
  174. {
  175. g_string_free (buffer, TRUE);
  176. str_close_conv (conv);
  177. return g_strdup (str);
  178. }
  179. str_close_conv (conv);
  180. return g_string_free (buffer, FALSE);
  181. }
  182. /* --------------------------------------------------------------------------------------------- */
  183. void
  184. tty_init_xterm_support (gboolean is_xterm)
  185. {
  186. const char *termvalue;
  187. termvalue = getenv ("TERM");
  188. /* Check mouse and ca capabilities */
  189. /* terminfo/termcap structures have been already initialized,
  190. in slang_init() or/and init_curses() */
  191. /* Check terminfo at first, then check termcap */
  192. xmouse_seq = tty_tgetstr ("kmous");
  193. if (xmouse_seq == NULL)
  194. xmouse_seq = tty_tgetstr ("Km");
  195. smcup = tty_tgetstr ("smcup");
  196. if (smcup == NULL)
  197. smcup = tty_tgetstr ("ti");
  198. rmcup = tty_tgetstr ("rmcup");
  199. if (rmcup == NULL)
  200. rmcup = tty_tgetstr ("te");
  201. if (strcmp (termvalue, "cygwin") == 0)
  202. {
  203. is_xterm = TRUE;
  204. use_mouse_p = MOUSE_DISABLED;
  205. }
  206. if (is_xterm)
  207. {
  208. /* Default to the standard xterm sequence */
  209. if (xmouse_seq == NULL)
  210. xmouse_seq = ESC_STR "[M";
  211. /* Enable mouse unless explicitly disabled by --nomouse */
  212. if (use_mouse_p != MOUSE_DISABLED)
  213. {
  214. if (old_mouse)
  215. use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
  216. else
  217. {
  218. /* FIXME: this dirty hack to set supported type of tracking the mouse */
  219. const char *color_term = getenv ("COLORTERM");
  220. if (strncmp (termvalue, "rxvt", 4) == 0 ||
  221. (color_term != NULL && strncmp (color_term, "rxvt", 4) == 0) ||
  222. strcmp (termvalue, "Eterm") == 0)
  223. use_mouse_p = MOUSE_XTERM_NORMAL_TRACKING;
  224. else
  225. use_mouse_p = MOUSE_XTERM_BUTTON_EVENT_TRACKING;
  226. }
  227. }
  228. }
  229. }
  230. /* --------------------------------------------------------------------------------------------- */