tty.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "lib/global.h"
  28. #include "lib/strutil.h"
  29. #include "tty.h"
  30. #include "tty-internal.h"
  31. /*** global variables ****************************************************************************/
  32. /* If true program softkeys (HP terminals only) on startup and after every
  33. command ran in the subshell to the description found in the termcap/terminfo
  34. database */
  35. int reset_hp_softkeys = 0;
  36. /* If true lines are drown by spaces */
  37. gboolean slow_tty = FALSE;
  38. /* If true use +, -, | for line drawing */
  39. gboolean ugly_line_drawing = FALSE;
  40. int mc_tty_frm[MC_TTY_FRM_MAX];
  41. /*** file scope macro definitions ****************************************************************/
  42. /*** file scope type declarations ****************************************************************/
  43. /*** file scope variables ************************************************************************/
  44. static volatile sig_atomic_t got_interrupt = 0;
  45. /*** file scope functions ************************************************************************/
  46. /* --------------------------------------------------------------------------------------------- */
  47. static void
  48. sigintr_handler (int signo)
  49. {
  50. (void) &signo;
  51. got_interrupt = 1;
  52. }
  53. /* --------------------------------------------------------------------------------------------- */
  54. /*** public functions ****************************************************************************/
  55. /* --------------------------------------------------------------------------------------------- */
  56. extern gboolean
  57. tty_is_slow (void)
  58. {
  59. return slow_tty;
  60. }
  61. /* --------------------------------------------------------------------------------------------- */
  62. extern void
  63. tty_start_interrupt_key (void)
  64. {
  65. struct sigaction act;
  66. act.sa_handler = sigintr_handler;
  67. sigemptyset (&act.sa_mask);
  68. act.sa_flags = SA_RESTART;
  69. sigaction (SIGINT, &act, NULL);
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. extern void
  73. tty_enable_interrupt_key (void)
  74. {
  75. struct sigaction act;
  76. act.sa_handler = sigintr_handler;
  77. sigemptyset (&act.sa_mask);
  78. act.sa_flags = 0;
  79. sigaction (SIGINT, &act, NULL);
  80. got_interrupt = 0;
  81. }
  82. /* --------------------------------------------------------------------------------------------- */
  83. extern void
  84. tty_disable_interrupt_key (void)
  85. {
  86. struct sigaction act;
  87. act.sa_handler = SIG_IGN;
  88. sigemptyset (&act.sa_mask);
  89. act.sa_flags = 0;
  90. sigaction (SIGINT, &act, NULL);
  91. }
  92. /* --------------------------------------------------------------------------------------------- */
  93. extern gboolean
  94. tty_got_interrupt (void)
  95. {
  96. gboolean rv;
  97. rv = (got_interrupt != 0);
  98. got_interrupt = 0;
  99. return rv;
  100. }
  101. /* --------------------------------------------------------------------------------------------- */
  102. void
  103. tty_print_one_hline (gboolean single)
  104. {
  105. tty_print_alt_char (ACS_HLINE, single);
  106. }
  107. /* --------------------------------------------------------------------------------------------- */
  108. void
  109. tty_print_one_vline (gboolean single)
  110. {
  111. tty_print_alt_char (ACS_VLINE, single);
  112. }
  113. /* --------------------------------------------------------------------------------------------- */
  114. void
  115. tty_draw_box (int y, int x, int ys, int xs, gboolean single)
  116. {
  117. ys--;
  118. xs--;
  119. tty_draw_vline (y, x, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  120. tty_draw_vline (y, x + xs, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  121. tty_draw_hline (y, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  122. tty_draw_hline (y + ys, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  123. tty_gotoyx (y, x);
  124. tty_print_alt_char (ACS_ULCORNER, single);
  125. tty_gotoyx (y + ys, x);
  126. tty_print_alt_char (ACS_LLCORNER, single);
  127. tty_gotoyx (y, x + xs);
  128. tty_print_alt_char (ACS_URCORNER, single);
  129. tty_gotoyx (y + ys, x + xs);
  130. tty_print_alt_char (ACS_LRCORNER, single);
  131. }
  132. /* --------------------------------------------------------------------------------------------- */
  133. char *
  134. mc_tty_normalize_from_utf8 (const char *str)
  135. {
  136. GIConv conv;
  137. GString *buffer;
  138. const char *_system_codepage = str_detect_termencoding ();
  139. if (str_isutf8 (_system_codepage))
  140. return g_strdup (str);
  141. conv = g_iconv_open (_system_codepage, "UTF-8");
  142. if (conv == INVALID_CONV)
  143. return g_strdup (str);
  144. buffer = g_string_new ("");
  145. if (str_convert (conv, str, buffer) == ESTR_FAILURE)
  146. {
  147. g_string_free (buffer, TRUE);
  148. str_close_conv (conv);
  149. return g_strdup (str);
  150. }
  151. str_close_conv (conv);
  152. return g_string_free (buffer, FALSE);
  153. }
  154. /* --------------------------------------------------------------------------------------------- */