win.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Terminal management xterm and rxvt support
  3. Copyright (C) 1995-2021
  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. #ifdef HAVE_SYS_SELECT_H
  27. #include <sys/select.h>
  28. #else
  29. #include <sys/time.h>
  30. #include <sys/types.h>
  31. #include <unistd.h>
  32. #endif
  33. #include "lib/global.h"
  34. #include "lib/util.h" /* is_printable() */
  35. #include "tty-internal.h"
  36. #include "tty.h" /* tty_gotoyx, tty_print_char */
  37. #include "win.h"
  38. /*** global variables ****************************************************************************/
  39. char *smcup = NULL;
  40. char *rmcup = NULL;
  41. /*** file scope macro definitions ****************************************************************/
  42. /*** file scope type declarations ****************************************************************/
  43. /*** file scope variables ************************************************************************/
  44. static gboolean rxvt_extensions = FALSE;
  45. /*** file scope functions ************************************************************************/
  46. /* --------------------------------------------------------------------------------------------- */
  47. /* my own weird protocol base 16 - paul */
  48. static int
  49. rxvt_getc (void)
  50. {
  51. int r;
  52. unsigned char c;
  53. while (read (0, &c, 1) != 1);
  54. if (c == '\n')
  55. return -1;
  56. r = (c - 'A') * 16;
  57. while (read (0, &c, 1) != 1);
  58. r += (c - 'A');
  59. return r;
  60. }
  61. /* --------------------------------------------------------------------------------------------- */
  62. static int
  63. anything_ready (void)
  64. {
  65. fd_set fds;
  66. struct timeval tv;
  67. FD_ZERO (&fds);
  68. FD_SET (0, &fds);
  69. tv.tv_sec = 0;
  70. tv.tv_usec = 0;
  71. return select (1, &fds, 0, 0, &tv);
  72. }
  73. /* --------------------------------------------------------------------------------------------- */
  74. /*** public functions ****************************************************************************/
  75. /* --------------------------------------------------------------------------------------------- */
  76. void
  77. show_rxvt_contents (int starty, unsigned char y1, unsigned char y2)
  78. {
  79. unsigned char *k;
  80. int bytes, i, j, cols = 0;
  81. y1 += mc_global.keybar_visible != 0 ? 1 : 0; /* i don't knwo why we need this - paul */
  82. y2 += mc_global.keybar_visible != 0 ? 1 : 0;
  83. while (anything_ready ())
  84. tty_lowlevel_getch ();
  85. /* my own weird protocol base 26 - paul */
  86. printf (ESC_STR "CL%c%c%c%c\n", (y1 / 26) + 'A', (y1 % 26) + 'A', (y2 / 26) + 'A',
  87. (y2 % 26) + 'A');
  88. bytes = (y2 - y1) * (COLS + 1) + 1; /* *should* be the number of bytes read */
  89. j = 0;
  90. k = g_malloc (bytes);
  91. while (TRUE)
  92. {
  93. int c;
  94. c = rxvt_getc ();
  95. if (c < 0)
  96. break;
  97. if (j < bytes)
  98. k[j++] = c;
  99. for (cols = 1;; cols++)
  100. {
  101. c = rxvt_getc ();
  102. if (c < 0)
  103. break;
  104. if (j < bytes)
  105. k[j++] = c;
  106. }
  107. }
  108. for (i = 0; i < j; i++)
  109. {
  110. if ((i % cols) == 0)
  111. tty_gotoyx (starty + (i / cols), 0);
  112. tty_print_char (is_printable (k[i]) ? k[i] : ' ');
  113. }
  114. g_free (k);
  115. }
  116. /* --------------------------------------------------------------------------------------------- */
  117. gboolean
  118. look_for_rxvt_extensions (void)
  119. {
  120. static gboolean been_called = FALSE;
  121. if (!been_called)
  122. {
  123. const char *e = getenv ("RXVT_EXT");
  124. rxvt_extensions = ((e != NULL) && (strcmp (e, "1.0") == 0));
  125. been_called = TRUE;
  126. }
  127. if (rxvt_extensions)
  128. mc_global.tty.console_flag = '\004';
  129. return rxvt_extensions;
  130. }
  131. /* --------------------------------------------------------------------------------------------- */