win.c 5.1 KB

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