Ticket 1551: Update GPL version from 2 to 3
[midnight-commander.git] / lib / tty / win.c
blob672e5269e9b58aca57c415de9b7c7064b7e69374
1 /*
2 Terminal management xterm and rxvt support
4 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Andrew Borodin <aborodin@vmail.ru>, 2009.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file win.c
28 * \brief Source: Terminal management xterm and rxvt support
31 #include <config.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <unistd.h>
40 #include "lib/global.h"
41 #include "lib/util.h" /* is_printable() */
42 #include "tty-internal.h"
43 #include "tty.h" /* tty_gotoyx, tty_print_char */
44 #include "win.h"
46 /*** global variables ****************************************************************************/
48 char *smcup = NULL;
49 char *rmcup = NULL;
51 /*** file scope macro definitions ****************************************************************/
53 /*** file scope type declarations ****************************************************************/
55 /*** file scope variables ************************************************************************/
57 static gboolean rxvt_extensions = FALSE;
59 /*** file scope functions ************************************************************************/
60 /* --------------------------------------------------------------------------------------------- */
62 /* my own wierd protocol base 16 - paul */
63 static int
64 rxvt_getc (void)
66 int r;
67 unsigned char c;
69 while (read (0, &c, 1) != 1);
70 if (c == '\n')
71 return -1;
72 r = (c - 'A') * 16;
73 while (read (0, &c, 1) != 1);
74 r += (c - 'A');
75 return r;
78 /* --------------------------------------------------------------------------------------------- */
80 static int
81 anything_ready (void)
83 fd_set fds;
84 struct timeval tv;
86 FD_ZERO (&fds);
87 FD_SET (0, &fds);
88 tv.tv_sec = 0;
89 tv.tv_usec = 0;
90 return select (1, &fds, 0, 0, &tv);
93 /* --------------------------------------------------------------------------------------------- */
94 /*** public functions ****************************************************************************/
95 /* --------------------------------------------------------------------------------------------- */
97 void
98 do_enter_ca_mode (void)
100 if (mc_global.tty.xterm_flag && smcup != NULL)
102 fprintf (stdout, /* ESC_STR ")0" */ ESC_STR "7" ESC_STR "[?47h");
103 fflush (stdout);
107 /* --------------------------------------------------------------------------------------------- */
109 void
110 do_exit_ca_mode (void)
112 if (mc_global.tty.xterm_flag && rmcup != NULL)
114 fprintf (stdout, ESC_STR "[?47l" ESC_STR "8" ESC_STR "[m");
115 fflush (stdout);
119 /* --------------------------------------------------------------------------------------------- */
121 void
122 show_rxvt_contents (int starty, unsigned char y1, unsigned char y2)
124 unsigned char *k;
125 int bytes, i, j, cols = 0;
127 y1 += (mc_global.keybar_visible != 0); /* i don't knwo why we need this - paul */
128 y2 += (mc_global.keybar_visible != 0);
129 while (anything_ready ())
130 tty_lowlevel_getch ();
132 /* my own wierd protocol base 26 - paul */
133 printf (ESC_STR "CL%c%c%c%c\n", (y1 / 26) + 'A', (y1 % 26) + 'A', (y2 / 26) + 'A',
134 (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 mc_global.tty.console_flag = '\004';
182 return rxvt_extensions;
185 /* --------------------------------------------------------------------------------------------- */