Use ESC_STR macro instead of hardcoded "\033".
[midnight-commander.git] / lib / tty / win.c
blobe91457e55acee05c88d0931d41926f89645177ab
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.
5 Written by:
6 Andrew Borodin <aborodin@vmail.ru>, 2009.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program 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.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 /** \file win.c
23 * \brief Source: Terminal management xterm and rxvt support
26 #include <config.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <unistd.h>
35 #include "lib/global.h"
36 #include "lib/util.h" /* is_printable() */
37 #include "tty-internal.h"
38 #include "tty.h" /* tty_gotoyx, tty_print_char */
39 #include "win.h"
40 #include "src/consaver/cons.saver.h" /* console_flag */
42 /*** global variables ****************************************************************************/
44 /* This flag is set by xterm detection routine in function main() */
45 /* It is used by function view_other_cmd() */
46 gboolean xterm_flag = FALSE;
48 extern int keybar_visible;
49 char *smcup = NULL;
50 char *rmcup = NULL;
52 /*** file scope macro definitions ****************************************************************/
54 /*** file scope type declarations ****************************************************************/
56 /*** file scope variables ************************************************************************/
58 static gboolean rxvt_extensions = FALSE;
60 /*** file scope functions ************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
63 /* my own wierd protocol base 16 - paul */
64 static int
65 rxvt_getc (void)
67 int r;
68 unsigned char c;
70 while (read (0, &c, 1) != 1);
71 if (c == '\n')
72 return -1;
73 r = (c - 'A') * 16;
74 while (read (0, &c, 1) != 1);
75 r += (c - 'A');
76 return r;
79 /* --------------------------------------------------------------------------------------------- */
81 static int
82 anything_ready (void)
84 fd_set fds;
85 struct timeval tv;
87 FD_ZERO (&fds);
88 FD_SET (0, &fds);
89 tv.tv_sec = 0;
90 tv.tv_usec = 0;
91 return select (1, &fds, 0, 0, &tv);
94 /* --------------------------------------------------------------------------------------------- */
95 /*** public functions ****************************************************************************/
96 /* --------------------------------------------------------------------------------------------- */
98 void
99 do_enter_ca_mode (void)
101 if (xterm_flag && smcup != NULL)
103 fprintf (stdout, /* ESC_STR ")0" */ ESC_STR "7" ESC_STR "[?47h");
104 fflush (stdout);
108 /* --------------------------------------------------------------------------------------------- */
110 void
111 do_exit_ca_mode (void)
113 if (xterm_flag && rmcup != NULL)
115 fprintf (stdout, ESC_STR "[?47l" ESC_STR "8" ESC_STR "[m");
116 fflush (stdout);
120 /* --------------------------------------------------------------------------------------------- */
122 void
123 show_rxvt_contents (int starty, unsigned char y1, unsigned char y2)
125 unsigned char *k;
126 int bytes, i, j, cols = 0;
128 y1 += (keybar_visible != 0); /* i don't knwo why we need this - paul */
129 y2 += (keybar_visible != 0);
130 while (anything_ready ())
131 tty_lowlevel_getch ();
133 /* my own wierd protocol base 26 - paul */
134 printf (ESC_STR "CL%c%c%c%c\n", (y1 / 26) + 'A', (y1 % 26) + 'A', (y2 / 26) + 'A', (y2 % 26) + 'A');
136 bytes = (y2 - y1) * (COLS + 1) + 1; /* *should* be the number of bytes read */
137 j = 0;
138 k = g_malloc (bytes);
139 for (;;)
141 int c;
142 c = rxvt_getc ();
143 if (c < 0)
144 break;
145 if (j < bytes)
146 k[j++] = c;
147 for (cols = 1;; cols++)
149 c = rxvt_getc ();
150 if (c < 0)
151 break;
152 if (j < bytes)
153 k[j++] = c;
156 for (i = 0; i < j; i++)
158 if ((i % cols) == 0)
159 tty_gotoyx (starty + (i / cols), 0);
160 tty_print_char (is_printable (k[i]) ? k[i] : ' ');
162 g_free (k);
165 /* --------------------------------------------------------------------------------------------- */
167 gboolean
168 look_for_rxvt_extensions (void)
170 static gboolean been_called = FALSE;
172 if (!been_called)
174 const char *e = getenv ("RXVT_EXT");
175 rxvt_extensions = ((e != NULL) && (strcmp (e, "1.0") == 0));
176 been_called = TRUE;
179 if (rxvt_extensions)
180 console_flag = 4;
182 return rxvt_extensions;
185 /* --------------------------------------------------------------------------------------------- */