win.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Terminal management xterm and rxvt support
  3. Copyright (C) 1995-2014
  4. Free Software Foundation, Inc.
  5. Written by:
  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 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU 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, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file win.c
  20. * \brief Source: Terminal management xterm and rxvt support
  21. */
  22. #include <config.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #include "lib/global.h"
  29. #include "lib/util.h" /* is_printable() */
  30. #include "tty-internal.h"
  31. #include "tty.h" /* tty_gotoyx, tty_print_char */
  32. #include "win.h"
  33. /*** global variables ****************************************************************************/
  34. char *smcup = NULL;
  35. char *rmcup = NULL;
  36. /*** file scope macro definitions ****************************************************************/
  37. /*** file scope type declarations ****************************************************************/
  38. /*** file scope variables ************************************************************************/
  39. static gboolean rxvt_extensions = FALSE;
  40. /*** file scope functions ************************************************************************/
  41. /* --------------------------------------------------------------------------------------------- */
  42. /* my own weird protocol base 16 - paul */
  43. static int
  44. rxvt_getc (void)
  45. {
  46. int r;
  47. unsigned char c;
  48. while (read (0, &c, 1) != 1);
  49. if (c == '\n')
  50. return -1;
  51. r = (c - 'A') * 16;
  52. while (read (0, &c, 1) != 1);
  53. r += (c - 'A');
  54. return r;
  55. }
  56. /* --------------------------------------------------------------------------------------------- */
  57. static int
  58. anything_ready (void)
  59. {
  60. fd_set fds;
  61. struct timeval tv;
  62. FD_ZERO (&fds);
  63. FD_SET (0, &fds);
  64. tv.tv_sec = 0;
  65. tv.tv_usec = 0;
  66. return select (1, &fds, 0, 0, &tv);
  67. }
  68. /* --------------------------------------------------------------------------------------------- */
  69. /*** public functions ****************************************************************************/
  70. /* --------------------------------------------------------------------------------------------- */
  71. void
  72. do_enter_ca_mode (void)
  73. {
  74. if (mc_global.tty.xterm_flag && smcup != NULL)
  75. {
  76. fprintf (stdout, /* ESC_STR ")0" */ ESC_STR "7" ESC_STR "[?47h");
  77. fflush (stdout);
  78. }
  79. }
  80. /* --------------------------------------------------------------------------------------------- */
  81. void
  82. do_exit_ca_mode (void)
  83. {
  84. if (mc_global.tty.xterm_flag && rmcup != NULL)
  85. {
  86. fprintf (stdout, ESC_STR "[?47l" ESC_STR "8" ESC_STR "[m");
  87. fflush (stdout);
  88. }
  89. }
  90. /* --------------------------------------------------------------------------------------------- */
  91. void
  92. show_rxvt_contents (int starty, unsigned char y1, unsigned char y2)
  93. {
  94. unsigned char *k;
  95. int bytes, i, j, cols = 0;
  96. y1 += (mc_global.keybar_visible != 0); /* i don't knwo why we need this - paul */
  97. y2 += (mc_global.keybar_visible != 0);
  98. while (anything_ready ())
  99. tty_lowlevel_getch ();
  100. /* my own weird protocol base 26 - paul */
  101. printf (ESC_STR "CL%c%c%c%c\n", (y1 / 26) + 'A', (y1 % 26) + 'A', (y2 / 26) + 'A',
  102. (y2 % 26) + 'A');
  103. bytes = (y2 - y1) * (COLS + 1) + 1; /* *should* be the number of bytes read */
  104. j = 0;
  105. k = g_malloc (bytes);
  106. while (TRUE)
  107. {
  108. int c;
  109. c = rxvt_getc ();
  110. if (c < 0)
  111. break;
  112. if (j < bytes)
  113. k[j++] = c;
  114. for (cols = 1;; cols++)
  115. {
  116. c = rxvt_getc ();
  117. if (c < 0)
  118. break;
  119. if (j < bytes)
  120. k[j++] = c;
  121. }
  122. }
  123. for (i = 0; i < j; i++)
  124. {
  125. if ((i % cols) == 0)
  126. tty_gotoyx (starty + (i / cols), 0);
  127. tty_print_char (is_printable (k[i]) ? k[i] : ' ');
  128. }
  129. g_free (k);
  130. }
  131. /* --------------------------------------------------------------------------------------------- */
  132. gboolean
  133. look_for_rxvt_extensions (void)
  134. {
  135. static gboolean been_called = FALSE;
  136. if (!been_called)
  137. {
  138. const char *e = getenv ("RXVT_EXT");
  139. rxvt_extensions = ((e != NULL) && (strcmp (e, "1.0") == 0));
  140. been_called = TRUE;
  141. }
  142. if (rxvt_extensions)
  143. mc_global.tty.console_flag = '\004';
  144. return rxvt_extensions;
  145. }
  146. /* --------------------------------------------------------------------------------------------- */