Updated Italian translation
[midnight-commander.git] / lib / tty / win.c
blob5ac8a5ae153480f570d207c672d009fdb527b43e
1 /*
2 Terminal management xterm and rxvt support
4 Copyright (C) 1995-2024
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 #ifdef HAVE_SYS_SELECT_H
36 #include <sys/select.h>
37 #else
38 #include <sys/time.h>
39 #include <sys/types.h>
40 #include <unistd.h>
41 #endif
43 #include "lib/global.h"
44 #include "lib/util.h" /* is_printable() */
45 #include "tty-internal.h"
46 #include "tty.h" /* tty_gotoyx, tty_print_char */
47 #include "win.h"
49 /*** global variables ****************************************************************************/
51 char *smcup = NULL;
52 char *rmcup = NULL;
54 /*** file scope macro definitions ****************************************************************/
56 /*** file scope type declarations ****************************************************************/
58 /*** forward declarations (file scope functions) *************************************************/
60 /*** file scope variables ************************************************************************/
62 static gboolean rxvt_extensions = FALSE;
64 /* --------------------------------------------------------------------------------------------- */
65 /*** file scope functions ************************************************************************/
66 /* --------------------------------------------------------------------------------------------- */
68 /* my own weird protocol base 16 - paul */
69 static int
70 rxvt_getc (void)
72 int r;
73 unsigned char c;
75 while (read (0, &c, 1) != 1);
76 if (c == '\n')
77 return -1;
78 r = (c - 'A') * 16;
79 while (read (0, &c, 1) != 1);
80 r += (c - 'A');
81 return r;
84 /* --------------------------------------------------------------------------------------------- */
86 static int
87 anything_ready (void)
89 fd_set fds;
90 struct timeval tv;
92 FD_ZERO (&fds);
93 FD_SET (0, &fds);
94 tv.tv_sec = 0;
95 tv.tv_usec = 0;
96 return select (1, &fds, 0, 0, &tv);
99 /* --------------------------------------------------------------------------------------------- */
100 /*** public functions ****************************************************************************/
101 /* --------------------------------------------------------------------------------------------- */
103 void
104 show_rxvt_contents (int starty, unsigned char y1, unsigned char y2)
106 unsigned char *k;
107 int bytes, i, j, cols = 0;
109 y1 += mc_global.keybar_visible != 0 ? 1 : 0; /* i don't know why we need this - paul */
110 y2 += mc_global.keybar_visible != 0 ? 1 : 0;
111 while (anything_ready ())
112 tty_lowlevel_getch ();
114 /* my own weird protocol base 26 - paul */
115 printf (ESC_STR "CL%c%c%c%c\n", (y1 / 26) + 'A', (y1 % 26) + 'A', (y2 / 26) + 'A',
116 (y2 % 26) + 'A');
118 bytes = (y2 - y1) * (COLS + 1) + 1; /* *should* be the number of bytes read */
119 j = 0;
120 k = g_malloc (bytes);
121 while (TRUE)
123 int c;
125 c = rxvt_getc ();
126 if (c < 0)
127 break;
128 if (j < bytes)
129 k[j++] = c;
130 for (cols = 1;; cols++)
132 c = rxvt_getc ();
133 if (c < 0)
134 break;
135 if (j < bytes)
136 k[j++] = c;
139 for (i = 0; i < j; i++)
141 if ((i % cols) == 0)
142 tty_gotoyx (starty + (i / cols), 0);
143 tty_print_char (is_printable (k[i]) ? k[i] : ' ');
145 g_free (k);
148 /* --------------------------------------------------------------------------------------------- */
150 gboolean
151 look_for_rxvt_extensions (void)
153 static gboolean been_called = FALSE;
155 if (!been_called)
157 const char *e = getenv ("RXVT_EXT");
158 rxvt_extensions = ((e != NULL) && (strcmp (e, "1.0") == 0));
159 been_called = TRUE;
162 if (rxvt_extensions)
163 mc_global.tty.console_flag = '\004';
165 return rxvt_extensions;
168 /* --------------------------------------------------------------------------------------------- */