win.c 5.2 KB

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