tty.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. static void
  47. sigintr_handler (int signo)
  48. {
  49. (void) &signo;
  50. got_interrupt = 1;
  51. }
  52. /*** public functions **************************************************/
  53. extern gboolean
  54. tty_is_slow (void)
  55. {
  56. return slow_tty;
  57. }
  58. extern void
  59. tty_start_interrupt_key (void)
  60. {
  61. struct sigaction act;
  62. act.sa_handler = sigintr_handler;
  63. sigemptyset (&act.sa_mask);
  64. act.sa_flags = SA_RESTART;
  65. sigaction (SIGINT, &act, NULL);
  66. }
  67. extern void
  68. tty_enable_interrupt_key (void)
  69. {
  70. struct sigaction act;
  71. act.sa_handler = sigintr_handler;
  72. sigemptyset (&act.sa_mask);
  73. act.sa_flags = 0;
  74. sigaction (SIGINT, &act, NULL);
  75. got_interrupt = 0;
  76. }
  77. extern void
  78. tty_disable_interrupt_key (void)
  79. {
  80. struct sigaction act;
  81. act.sa_handler = SIG_IGN;
  82. sigemptyset (&act.sa_mask);
  83. act.sa_flags = 0;
  84. sigaction (SIGINT, &act, NULL);
  85. }
  86. extern gboolean
  87. tty_got_interrupt (void)
  88. {
  89. gboolean rv;
  90. rv = (got_interrupt != 0);
  91. got_interrupt = 0;
  92. return rv;
  93. }
  94. void
  95. tty_print_one_hline (gboolean single)
  96. {
  97. tty_print_alt_char (ACS_HLINE, single);
  98. }
  99. void
  100. tty_print_one_vline (gboolean single)
  101. {
  102. tty_print_alt_char (ACS_VLINE, single);
  103. }
  104. void
  105. tty_draw_box (int y, int x, int ys, int xs, gboolean single)
  106. {
  107. ys--;
  108. xs--;
  109. tty_draw_vline (y, x, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  110. tty_draw_vline (y, x + xs, mc_tty_frm[single ? MC_TTY_FRM_VERT : MC_TTY_FRM_DVERT], ys);
  111. tty_draw_hline (y, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  112. tty_draw_hline (y + ys, x, mc_tty_frm[single ? MC_TTY_FRM_HORIZ : MC_TTY_FRM_DHORIZ], xs);
  113. tty_gotoyx (y, x);
  114. tty_print_alt_char (ACS_ULCORNER, single);
  115. tty_gotoyx (y + ys, x);
  116. tty_print_alt_char (ACS_LLCORNER, single);
  117. tty_gotoyx (y, x + xs);
  118. tty_print_alt_char (ACS_URCORNER, single);
  119. tty_gotoyx (y + ys, x + xs);
  120. tty_print_alt_char (ACS_LRCORNER, single);
  121. }
  122. char *
  123. mc_tty_normalize_from_utf8 (const char *str)
  124. {
  125. GIConv conv;
  126. GString *buffer;
  127. const char *_system_codepage = str_detect_termencoding ();
  128. if (str_isutf8 (_system_codepage))
  129. return g_strdup (str);
  130. conv = g_iconv_open (_system_codepage, "UTF-8");
  131. if (conv == INVALID_CONV)
  132. return g_strdup (str);
  133. buffer = g_string_new ("");
  134. if (str_convert (conv, str, buffer) == ESTR_FAILURE)
  135. {
  136. g_string_free (buffer, TRUE);
  137. str_close_conv (conv);
  138. return g_strdup (str);
  139. }
  140. str_close_conv (conv);
  141. return g_string_free (buffer, FALSE);
  142. }