Add edit_add_window() function.
[midnight-commander.git] / src / cons.handler.c
blobc2a523ca637f997c62719c852bd0d869d040690d
1 /*
2 Client interface for General purpose Linux console save/restore server
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2006, 2007, 2011
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** \file cons.handler.c
25 * \brief Source: client %interface for General purpose Linux console save/restore server
28 #include <config.h>
30 #include <stdlib.h>
31 #include <sys/wait.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <sys/types.h>
36 #ifdef __FreeBSD__
37 #include <sys/consio.h>
38 #include <sys/ioctl.h>
39 #endif
40 #include <unistd.h>
42 #include "lib/global.h"
44 #include "lib/tty/tty.h"
45 #include "lib/skin.h" /* tty_set_normal_attrs */
46 #include "lib/tty/win.h"
47 #include "lib/util.h" /* mc_build_filename() */
49 #include "consaver/cons.saver.h"
51 /*** global variables ****************************************************************************/
53 #ifdef __linux__
54 int cons_saver_pid = 1;
55 #endif /* __linux__ */
57 /*** file scope macro definitions ****************************************************************/
59 #if defined(__FreeBSD__)
60 #define FD_OUT 1
61 #define cursor_to(x, y) \
62 do \
63 { \
64 printf("\x1B[%d;%df", (y) + 1, (x) + 1); \
65 fflush(stdout); \
66 } while (0)
67 #endif /* __linux__ */
69 /*** file scope type declarations ****************************************************************/
71 /*** file scope variables ************************************************************************/
73 #ifdef __linux__
74 /* The cons saver can't have a pid of 1, used to prevent bunches of
75 * #ifdef linux */
76 static int pipefd1[2] = { -1, -1 };
77 static int pipefd2[2] = { -1, -1 };
78 #elif defined(__FreeBSD__)
79 static struct scrshot screen_shot;
80 static struct vid_info screen_info;
81 #endif /* __linux__ */
83 /*** file scope functions ************************************************************************/
84 /* --------------------------------------------------------------------------------------------- */
86 #ifdef __linux__
87 static void
88 show_console_contents_linux (int starty, unsigned char begin_line, unsigned char end_line)
90 unsigned char message = 0;
91 unsigned short bytes = 0;
92 int i;
93 ssize_t ret;
95 /* Is tty console? */
96 if (mc_global.tty.console_flag == '\0')
97 return;
98 /* Paranoid: Is the cons.saver still running? */
99 if (cons_saver_pid < 1 || kill (cons_saver_pid, SIGCONT))
101 cons_saver_pid = 0;
102 mc_global.tty.console_flag = '\0';
103 return;
106 /* Send command to the console handler */
107 message = CONSOLE_CONTENTS;
108 ret = write (pipefd1[1], &message, 1);
109 /* Check for outdated cons.saver */
110 ret = read (pipefd2[0], &message, 1);
111 if (message != CONSOLE_CONTENTS)
112 return;
114 /* Send the range of lines that we want */
115 ret = write (pipefd1[1], &begin_line, 1);
116 ret = write (pipefd1[1], &end_line, 1);
117 /* Read the corresponding number of bytes */
118 ret = read (pipefd2[0], &bytes, 2);
120 /* Read the bytes and output them */
121 for (i = 0; i < bytes; i++)
123 if ((i % COLS) == 0)
124 tty_gotoyx (starty + (i / COLS), 0);
125 ret = read (pipefd2[0], &message, 1);
126 tty_print_char (message);
129 /* Read the value of the mc_global.tty.console_flag */
130 ret = read (pipefd2[0], &message, 1);
133 /* --------------------------------------------------------------------------------------------- */
135 static void
136 handle_console_linux (console_action_t action)
138 char *tty_name;
139 char *mc_conssaver;
140 int status;
142 switch (action)
144 case CONSOLE_INIT:
145 /* Close old pipe ends in case it is the 2nd time we run cons.saver */
146 status = close (pipefd1[1]);
147 status = close (pipefd2[0]);
148 /* Create two pipes for communication */
149 if (!((pipe (pipefd1) == 0) && ((pipe (pipefd2)) == 0)))
151 mc_global.tty.console_flag = '\0';
152 break;
154 /* Get the console saver running */
155 cons_saver_pid = fork ();
156 if (cons_saver_pid < 0)
158 /* Cannot fork */
159 /* Delete pipes */
160 status = close (pipefd1[1]);
161 status = close (pipefd1[0]);
162 status = close (pipefd2[1]);
163 status = close (pipefd2[0]);
164 mc_global.tty.console_flag = '\0';
166 else if (cons_saver_pid > 0)
168 /* Parent */
169 /* Close the extra pipe ends */
170 status = close (pipefd1[0]);
171 status = close (pipefd2[1]);
172 /* Was the child successful? */
173 status = read (pipefd2[0], &mc_global.tty.console_flag, 1);
174 if (mc_global.tty.console_flag == '\0')
176 pid_t ret;
177 status = close (pipefd1[1]);
178 status = close (pipefd2[0]);
179 ret = waitpid (cons_saver_pid, &status, 0);
182 else
184 /* Child */
185 /* Close the extra pipe ends */
186 status = close (pipefd1[1]);
187 status = close (pipefd2[0]);
188 tty_name = ttyname (0);
189 /* Bind the pipe 0 to the standard input */
192 if (dup2 (pipefd1[0], 0) == -1)
193 break;
194 status = close (pipefd1[0]);
195 /* Bind the pipe 1 to the standard output */
196 if (dup2 (pipefd2[1], 1) == -1)
197 break;
199 status = close (pipefd2[1]);
200 /* Bind standard error to /dev/null */
201 status = open ("/dev/null", O_WRONLY);
202 if (dup2 (status, 2) == -1)
203 break;
204 status = close (status);
205 if (tty_name)
207 /* Exec the console save/restore handler */
208 mc_conssaver = mc_build_filename (SAVERDIR, "cons.saver", NULL);
209 execl (mc_conssaver, "cons.saver", tty_name, (char *) NULL);
211 /* Console is not a tty or execl() failed */
213 while (0);
214 mc_global.tty.console_flag = '\0';
215 status = write (1, &mc_global.tty.console_flag, 1);
216 _exit (3);
217 } /* if (cons_saver_pid ...) */
218 break;
220 case CONSOLE_DONE:
221 case CONSOLE_SAVE:
222 case CONSOLE_RESTORE:
223 /* Is tty console? */
224 if (mc_global.tty.console_flag == '\0')
225 return;
226 /* Paranoid: Is the cons.saver still running? */
227 if (cons_saver_pid < 1 || kill (cons_saver_pid, SIGCONT))
229 cons_saver_pid = 0;
230 mc_global.tty.console_flag = '\0';
231 return;
233 /* Send command to the console handler */
234 status = write (pipefd1[1], &action, 1);
235 if (action != CONSOLE_DONE)
237 /* Wait the console handler to do its job */
238 status = read (pipefd2[0], &mc_global.tty.console_flag, 1);
240 if (action == CONSOLE_DONE || mc_global.tty.console_flag == '\0')
242 /* We are done -> Let's clean up */
243 pid_t ret;
244 close (pipefd1[1]);
245 close (pipefd2[0]);
246 ret = waitpid (cons_saver_pid, &status, 0);
247 mc_global.tty.console_flag = '\0';
249 break;
250 default:
251 break;
255 #elif defined(__FreeBSD__)
257 /* --------------------------------------------------------------------------------------------- */
259 * FreeBSD support copyright (C) 2003 Alexander Serkov <serkov@ukrpost.net>.
260 * Support for screenmaps by Max Khon <fjoe@FreeBSD.org>
263 static void
264 console_init (void)
266 if (mc_global.tty.console_flag != '\0')
267 return;
269 screen_info.size = sizeof (screen_info);
270 if (ioctl (FD_OUT, CONS_GETINFO, &screen_info) == -1)
271 return;
273 memset (&screen_shot, 0, sizeof (screen_shot));
274 screen_shot.xsize = screen_info.mv_csz;
275 screen_shot.ysize = screen_info.mv_rsz;
276 screen_shot.buf = g_try_malloc (screen_info.mv_csz * screen_info.mv_rsz * 2);
277 if (screen_shot.buf != NULL)
278 mc_global.tty.console_flag = '\001';
281 /* --------------------------------------------------------------------------------------------- */
283 static void
284 set_attr (unsigned attr)
287 * Convert color indices returned by SCRSHOT (red=4, green=2, blue=1)
288 * to indices for ANSI sequences (red=1, green=2, blue=4).
290 static const int color_map[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
291 int bc, tc;
293 tc = attr & 0xF;
294 bc = (attr >> 4) & 0xF;
296 printf ("\x1B[%d;%d;3%d;4%dm", (bc & 8) ? 5 : 25, (tc & 8) ? 1 : 22,
297 color_map[tc & 7], color_map[bc & 7]);
300 /* --------------------------------------------------------------------------------------------- */
302 static void
303 console_restore (void)
305 int i, last;
307 if (mc_global.tty.console_flag == '\0')
308 return;
310 cursor_to (0, 0);
312 /* restoring all content up to cursor position */
313 last = screen_info.mv_row * screen_info.mv_csz + screen_info.mv_col;
314 for (i = 0; i < last; ++i)
316 set_attr ((screen_shot.buf[i] >> 8) & 0xFF);
317 putc (screen_shot.buf[i] & 0xFF, stdout);
320 /* restoring cursor color */
321 set_attr ((screen_shot.buf[last] >> 8) & 0xFF);
323 fflush (stdout);
326 /* --------------------------------------------------------------------------------------------- */
328 static void
329 console_shutdown (void)
331 if (mc_global.tty.console_flag == '\0')
332 return;
334 g_free (screen_shot.buf);
336 mc_global.tty.console_flag = '\0';
339 /* --------------------------------------------------------------------------------------------- */
341 static void
342 console_save (void)
344 int i;
345 scrmap_t map;
346 scrmap_t revmap;
348 if (mc_global.tty.console_flag == '\0')
349 return;
351 /* screen_info.size is already set in console_init() */
352 if (ioctl (FD_OUT, CONS_GETINFO, &screen_info) == -1)
354 console_shutdown ();
355 return;
358 /* handle console resize */
359 if (screen_info.mv_csz != screen_shot.xsize || screen_info.mv_rsz != screen_shot.ysize)
361 console_shutdown ();
362 console_init ();
365 if (ioctl (FD_OUT, CONS_SCRSHOT, &screen_shot) == -1)
367 console_shutdown ();
368 return;
371 if (ioctl (FD_OUT, GIO_SCRNMAP, &map) == -1)
373 console_shutdown ();
374 return;
377 for (i = 0; i < 256; i++)
379 char *p = memchr (map.scrmap, i, 256);
380 revmap.scrmap[i] = p ? p - map.scrmap : i;
383 for (i = 0; i < screen_shot.xsize * screen_shot.ysize; i++)
385 screen_shot.buf[i] =
386 (screen_shot.buf[i] & 0xff00) | (unsigned char) revmap.
387 scrmap[screen_shot.buf[i] & 0xff];
391 /* --------------------------------------------------------------------------------------------- */
393 static void
394 show_console_contents_freebsd (int starty, unsigned char begin_line, unsigned char end_line)
396 int col, line;
397 char c;
399 if (mc_global.tty.console_flag == '\0')
400 return;
402 for (line = begin_line; line <= end_line; line++)
404 tty_gotoyx (starty + line - begin_line, 0);
405 for (col = 0; col < min (COLS, screen_info.mv_csz); col++)
407 c = screen_shot.buf[line * screen_info.mv_csz + col] & 0xFF;
408 tty_print_char (c);
413 /* --------------------------------------------------------------------------------------------- */
415 static void
416 handle_console_freebsd (console_action_t action)
418 switch (action)
420 case CONSOLE_INIT:
421 console_init ();
422 break;
424 case CONSOLE_DONE:
425 console_shutdown ();
426 break;
428 case CONSOLE_SAVE:
429 console_save ();
430 break;
432 case CONSOLE_RESTORE:
433 console_restore ();
434 break;
435 default:
436 break;
439 #endif /* __FreeBSD__ */
441 /* --------------------------------------------------------------------------------------------- */
442 /*** public functions ****************************************************************************/
443 /* --------------------------------------------------------------------------------------------- */
445 void
446 show_console_contents (int starty, unsigned char begin_line, unsigned char end_line)
448 tty_set_normal_attrs ();
450 if (look_for_rxvt_extensions ())
452 show_rxvt_contents (starty, begin_line, end_line);
453 return;
455 #ifdef __linux__
456 show_console_contents_linux (starty, begin_line, end_line);
457 #elif defined (__FreeBSD__)
458 show_console_contents_freebsd (starty, begin_line, end_line);
459 #else
460 mc_global.tty.console_flag = '\0';
461 #endif
464 /* --------------------------------------------------------------------------------------------- */
466 void
467 handle_console (console_action_t action)
469 (void) action;
471 if (look_for_rxvt_extensions ())
472 return;
474 #ifdef __linux__
475 handle_console_linux (action);
476 #elif defined (__FreeBSD__)
477 handle_console_freebsd (action);
478 #endif
481 /* --------------------------------------------------------------------------------------------- */