tty.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #ifdef HAVE_SYS_IOCTL_H
  28. #include <sys/ioctl.h>
  29. #endif
  30. #include "lib/global.h"
  31. #include "lib/strutil.h"
  32. #include "tty.h"
  33. #include "tty-internal.h"
  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. extern gboolean
  61. tty_is_slow (void)
  62. {
  63. return slow_tty;
  64. }
  65. /* --------------------------------------------------------------------------------------------- */
  66. extern void
  67. tty_start_interrupt_key (void)
  68. {
  69. struct sigaction act;
  70. act.sa_handler = sigintr_handler;
  71. sigemptyset (&act.sa_mask);
  72. act.sa_flags = SA_RESTART;
  73. sigaction (SIGINT, &act, NULL);
  74. }
  75. /* --------------------------------------------------------------------------------------------- */
  76. extern void
  77. tty_enable_interrupt_key (void)
  78. {
  79. struct sigaction act;
  80. act.sa_handler = sigintr_handler;
  81. sigemptyset (&act.sa_mask);
  82. act.sa_flags = 0;
  83. sigaction (SIGINT, &act, NULL);
  84. got_interrupt = 0;
  85. }
  86. /* --------------------------------------------------------------------------------------------- */
  87. extern void
  88. tty_disable_interrupt_key (void)
  89. {
  90. struct sigaction act;
  91. act.sa_handler = SIG_IGN;
  92. sigemptyset (&act.sa_mask);
  93. act.sa_flags = 0;
  94. sigaction (SIGINT, &act, NULL);
  95. }
  96. /* --------------------------------------------------------------------------------------------- */
  97. extern gboolean
  98. tty_got_interrupt (void)
  99. {
  100. gboolean rv;
  101. rv = (got_interrupt != 0);
  102. got_interrupt = 0;
  103. return rv;
  104. }
  105. /* --------------------------------------------------------------------------------------------- */
  106. void
  107. tty_print_one_hline (gboolean single)
  108. {
  109. tty_print_alt_char (ACS_HLINE, single);
  110. }
  111. /* --------------------------------------------------------------------------------------------- */
  112. void
  113. tty_print_one_vline (gboolean single)
  114. {
  115. tty_print_alt_char (ACS_VLINE, single);
  116. }
  117. /* --------------------------------------------------------------------------------------------- */
  118. void
  119. tty_draw_box (int y, int x, int ys, int xs, gboolean single)
  120. {
  121. ys--;
  122. xs--;
  123. tty_draw_vline (y, x, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  124. tty_draw_vline (y, x + xs, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  125. tty_draw_hline (y, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  126. tty_draw_hline (y + ys, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  127. tty_gotoyx (y, x);
  128. tty_print_alt_char (ACS_ULCORNER, single);
  129. tty_gotoyx (y + ys, x);
  130. tty_print_alt_char (ACS_LLCORNER, single);
  131. tty_gotoyx (y, x + xs);
  132. tty_print_alt_char (ACS_URCORNER, single);
  133. tty_gotoyx (y + ys, x + xs);
  134. tty_print_alt_char (ACS_LRCORNER, single);
  135. }
  136. /* --------------------------------------------------------------------------------------------- */
  137. char *
  138. mc_tty_normalize_from_utf8 (const char *str)
  139. {
  140. GIConv conv;
  141. GString *buffer;
  142. const char *_system_codepage = str_detect_termencoding ();
  143. if (str_isutf8 (_system_codepage))
  144. return g_strdup (str);
  145. conv = g_iconv_open (_system_codepage, "UTF-8");
  146. if (conv == INVALID_CONV)
  147. return g_strdup (str);
  148. buffer = g_string_new ("");
  149. if (str_convert (conv, str, buffer) == ESTR_FAILURE)
  150. {
  151. g_string_free (buffer, TRUE);
  152. str_close_conv (conv);
  153. return g_strdup (str);
  154. }
  155. str_close_conv (conv);
  156. return g_string_free (buffer, FALSE);
  157. }
  158. /* --------------------------------------------------------------------------------------------- */
  159. /** Resize given terminal using TIOCSWINSZ, return ioctl() result */
  160. int
  161. tty_resize (int fd)
  162. {
  163. #if defined TIOCSWINSZ
  164. struct winsize tty_size;
  165. tty_size.ws_row = LINES;
  166. tty_size.ws_col = COLS;
  167. tty_size.ws_xpixel = tty_size.ws_ypixel = 0;
  168. return ioctl (fd, TIOCSWINSZ, &tty_size);
  169. #else
  170. return 0;
  171. #endif
  172. }
  173. /* --------------------------------------------------------------------------------------------- */
  174. void
  175. tty_low_level_change_screen_size (void)
  176. {
  177. #if defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4
  178. #if defined TIOCGWINSZ
  179. struct winsize winsz;
  180. winsz.ws_col = winsz.ws_row = 0;
  181. /* Ioctl on the STDIN_FILENO */
  182. ioctl (0, TIOCGWINSZ, &winsz);
  183. if (winsz.ws_col && winsz.ws_row)
  184. {
  185. #if defined(NCURSES_VERSION) && defined(HAVE_RESIZETERM)
  186. resizeterm (winsz.ws_row, winsz.ws_col);
  187. clearok (stdscr, TRUE); /* sigwinch's should use a semaphore! */
  188. #else
  189. COLS = winsz.ws_col;
  190. LINES = winsz.ws_row;
  191. #endif
  192. #ifdef HAVE_SUBSHELL_SUPPORT
  193. if (!mc_global.tty.use_subshell)
  194. return;
  195. tty_resize (mc_global.tty.subshell_pty);
  196. #endif
  197. }
  198. #endif /* TIOCGWINSZ */
  199. #endif /* defined(HAVE_SLANG) || NCURSES_VERSION_MAJOR >= 4 */
  200. }
  201. /* --------------------------------------------------------------------------------------------- */