(edit_move_block_to_left): reduce variable scope.
[midnight-commander.git] / lib / tty / win.c
blob86150d149c1081d51d15d1765ef7ccbb9b0fe073
1 /*
2 Terminal management xterm and rxvt support
4 Copyright (C) 1995-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Andrew Borodin <aborodin@vmail.ru>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /** \file win.c
27 * \brief Source: Terminal management xterm and rxvt support
30 #include <config.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <unistd.h>
38 #include "lib/global.h"
39 #include "lib/util.h" /* is_printable() */
40 #include "tty-internal.h"
41 #include "tty.h" /* tty_gotoyx, tty_print_char */
42 #include "win.h"
44 /*** global variables ****************************************************************************/
46 char *smcup = NULL;
47 char *rmcup = NULL;
49 /*** file scope macro definitions ****************************************************************/
51 /*** file scope type declarations ****************************************************************/
53 /*** file scope variables ************************************************************************/
55 static gboolean rxvt_extensions = FALSE;
57 /*** file scope functions ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
60 /* my own weird protocol base 16 - paul */
61 static int
62 rxvt_getc (void)
64 int r;
65 unsigned char c;
67 while (read (0, &c, 1) != 1);
68 if (c == '\n')
69 return -1;
70 r = (c - 'A') * 16;
71 while (read (0, &c, 1) != 1);
72 r += (c - 'A');
73 return r;
76 /* --------------------------------------------------------------------------------------------- */
78 static int
79 anything_ready (void)
81 fd_set fds;
82 struct timeval tv;
84 FD_ZERO (&fds);
85 FD_SET (0, &fds);
86 tv.tv_sec = 0;
87 tv.tv_usec = 0;
88 return select (1, &fds, 0, 0, &tv);
91 /* --------------------------------------------------------------------------------------------- */
92 /*** public functions ****************************************************************************/
93 /* --------------------------------------------------------------------------------------------- */
95 void
96 show_rxvt_contents (int starty, unsigned char y1, unsigned char y2)
98 unsigned char *k;
99 int bytes, i, j, cols = 0;
101 y1 += (mc_global.keybar_visible != 0); /* i don't knwo why we need this - paul */
102 y2 += (mc_global.keybar_visible != 0);
103 while (anything_ready ())
104 tty_lowlevel_getch ();
106 /* my own weird protocol base 26 - paul */
107 printf (ESC_STR "CL%c%c%c%c\n", (y1 / 26) + 'A', (y1 % 26) + 'A', (y2 / 26) + 'A',
108 (y2 % 26) + 'A');
110 bytes = (y2 - y1) * (COLS + 1) + 1; /* *should* be the number of bytes read */
111 j = 0;
112 k = g_malloc (bytes);
113 while (TRUE)
115 int c;
117 c = rxvt_getc ();
118 if (c < 0)
119 break;
120 if (j < bytes)
121 k[j++] = c;
122 for (cols = 1;; cols++)
124 c = rxvt_getc ();
125 if (c < 0)
126 break;
127 if (j < bytes)
128 k[j++] = c;
131 for (i = 0; i < j; i++)
133 if ((i % cols) == 0)
134 tty_gotoyx (starty + (i / cols), 0);
135 tty_print_char (is_printable (k[i]) ? k[i] : ' ');
137 g_free (k);
140 /* --------------------------------------------------------------------------------------------- */
142 gboolean
143 look_for_rxvt_extensions (void)
145 static gboolean been_called = FALSE;
147 if (!been_called)
149 const char *e = getenv ("RXVT_EXT");
150 rxvt_extensions = ((e != NULL) && (strcmp (e, "1.0") == 0));
151 been_called = TRUE;
154 if (rxvt_extensions)
155 mc_global.tty.console_flag = '\004';
157 return rxvt_extensions;
160 /* --------------------------------------------------------------------------------------------- */