Use ESC_STR macro instead of hardcoded "\033".
[midnight-commander.git] / lib / tty / win.c
blob51418071d9b88462468ab5039deddd760f035ef2
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"
41 /*** global variables ****************************************************************************/
43 char *smcup = NULL;
44 char *rmcup = NULL;
46 /*** file scope macro definitions ****************************************************************/
48 /*** file scope type declarations ****************************************************************/
50 /*** file scope variables ************************************************************************/
52 static gboolean rxvt_extensions = FALSE;
54 /*** file scope functions ************************************************************************/
55 /* --------------------------------------------------------------------------------------------- */
57 /* my own wierd protocol base 16 - paul */
58 static int
59 rxvt_getc (void)
61 int r;
62 unsigned char c;
64 while (read (0, &c, 1) != 1);
65 if (c == '\n')
66 return -1;
67 r = (c - 'A') * 16;
68 while (read (0, &c, 1) != 1);
69 r += (c - 'A');
70 return r;
73 /* --------------------------------------------------------------------------------------------- */
75 static int
76 anything_ready (void)
78 fd_set fds;
79 struct timeval tv;
81 FD_ZERO (&fds);
82 FD_SET (0, &fds);
83 tv.tv_sec = 0;
84 tv.tv_usec = 0;
85 return select (1, &fds, 0, 0, &tv);
88 /* --------------------------------------------------------------------------------------------- */
89 /*** public functions ****************************************************************************/
90 /* --------------------------------------------------------------------------------------------- */
92 void
93 do_enter_ca_mode (void)
95 if (mc_global.tty.xterm_flag && smcup != NULL)
97 fprintf (stdout, /* ESC_STR ")0" */ ESC_STR "7" ESC_STR "[?47h");
98 fflush (stdout);
102 /* --------------------------------------------------------------------------------------------- */
104 void
105 do_exit_ca_mode (void)
107 if (mc_global.tty.xterm_flag && rmcup != NULL)
109 fprintf (stdout, ESC_STR "[?47l" ESC_STR "8" ESC_STR "[m");
110 fflush (stdout);
114 /* --------------------------------------------------------------------------------------------- */
116 void
117 show_rxvt_contents (int starty, unsigned char y1, unsigned char y2)
119 unsigned char *k;
120 int bytes, i, j, cols = 0;
122 y1 += (mc_global.keybar_visible != 0); /* i don't knwo why we need this - paul */
123 y2 += (mc_global.keybar_visible != 0);
124 while (anything_ready ())
125 tty_lowlevel_getch ();
127 /* my own wierd protocol base 26 - paul */
128 printf (ESC_STR "CL%c%c%c%c\n", (y1 / 26) + 'A', (y1 % 26) + 'A', (y2 / 26) + 'A', (y2 % 26) + 'A');
130 bytes = (y2 - y1) * (COLS + 1) + 1; /* *should* be the number of bytes read */
131 j = 0;
132 k = g_malloc (bytes);
133 for (;;)
135 int c;
136 c = rxvt_getc ();
137 if (c < 0)
138 break;
139 if (j < bytes)
140 k[j++] = c;
141 for (cols = 1;; cols++)
143 c = rxvt_getc ();
144 if (c < 0)
145 break;
146 if (j < bytes)
147 k[j++] = c;
150 for (i = 0; i < j; i++)
152 if ((i % cols) == 0)
153 tty_gotoyx (starty + (i / cols), 0);
154 tty_print_char (is_printable (k[i]) ? k[i] : ' ');
156 g_free (k);
159 /* --------------------------------------------------------------------------------------------- */
161 gboolean
162 look_for_rxvt_extensions (void)
164 static gboolean been_called = FALSE;
166 if (!been_called)
168 const char *e = getenv ("RXVT_EXT");
169 rxvt_extensions = ((e != NULL) && (strcmp (e, "1.0") == 0));
170 been_called = TRUE;
173 if (rxvt_extensions)
174 mc_global.tty.console_flag = '\004';
176 return rxvt_extensions;
179 /* --------------------------------------------------------------------------------------------- */