2 Client interface for General purpose Linux console save/restore server
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
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
35 #include <sys/types.h>
37 #include <sys/consio.h>
38 #include <sys/ioctl.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 ****************************************************************************/
54 int cons_saver_pid
= 1;
55 #endif /* __linux__ */
57 /*** file scope macro definitions ****************************************************************/
59 #if defined(__FreeBSD__)
61 #define cursor_to(x, y) \
64 printf("\x1B[%d;%df", (y) + 1, (x) + 1); \
67 #endif /* __linux__ */
69 /*** file scope type declarations ****************************************************************/
71 /*** file scope variables ************************************************************************/
74 /* The cons saver can't have a pid of 1, used to prevent bunches of
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 /* --------------------------------------------------------------------------------------------- */
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;
96 if (mc_global
.tty
.console_flag
== '\0')
98 /* Paranoid: Is the cons.saver still running? */
99 if (cons_saver_pid
< 1 || kill (cons_saver_pid
, SIGCONT
))
102 mc_global
.tty
.console_flag
= '\0';
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
)
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
++)
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 /* --------------------------------------------------------------------------------------------- */
136 handle_console_linux (console_action_t action
)
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';
154 /* Get the console saver running */
155 cons_saver_pid
= fork ();
156 if (cons_saver_pid
< 0)
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)
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')
177 status
= close (pipefd1
[1]);
178 status
= close (pipefd2
[0]);
179 ret
= waitpid (cons_saver_pid
, &status
, 0);
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)
194 status
= close (pipefd1
[0]);
195 /* Bind the pipe 1 to the standard output */
196 if (dup2 (pipefd2
[1], 1) == -1)
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)
204 status
= close (status
);
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 */
214 mc_global
.tty
.console_flag
= '\0';
215 status
= write (1, &mc_global
.tty
.console_flag
, 1);
217 } /* if (cons_saver_pid ...) */
222 case CONSOLE_RESTORE
:
223 /* Is tty console? */
224 if (mc_global
.tty
.console_flag
== '\0')
226 /* Paranoid: Is the cons.saver still running? */
227 if (cons_saver_pid
< 1 || kill (cons_saver_pid
, SIGCONT
))
230 mc_global
.tty
.console_flag
= '\0';
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 */
246 ret
= waitpid (cons_saver_pid
, &status
, 0);
247 mc_global
.tty
.console_flag
= '\0';
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>
266 if (mc_global
.tty
.console_flag
!= '\0')
269 screen_info
.size
= sizeof (screen_info
);
270 if (ioctl (FD_OUT
, CONS_GETINFO
, &screen_info
) == -1)
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 /* --------------------------------------------------------------------------------------------- */
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 };
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 /* --------------------------------------------------------------------------------------------- */
303 console_restore (void)
307 if (mc_global
.tty
.console_flag
== '\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);
326 /* --------------------------------------------------------------------------------------------- */
329 console_shutdown (void)
331 if (mc_global
.tty
.console_flag
== '\0')
334 g_free (screen_shot
.buf
);
336 mc_global
.tty
.console_flag
= '\0';
339 /* --------------------------------------------------------------------------------------------- */
348 if (mc_global
.tty
.console_flag
== '\0')
351 /* screen_info.size is already set in console_init() */
352 if (ioctl (FD_OUT
, CONS_GETINFO
, &screen_info
) == -1)
358 /* handle console resize */
359 if (screen_info
.mv_csz
!= screen_shot
.xsize
|| screen_info
.mv_rsz
!= screen_shot
.ysize
)
365 if (ioctl (FD_OUT
, CONS_SCRSHOT
, &screen_shot
) == -1)
371 if (ioctl (FD_OUT
, GIO_SCRNMAP
, &map
) == -1)
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
++)
386 screen_shot
.buf
[i
] = (screen_shot
.buf
[i
] & 0xff00)
387 | (unsigned char) revmap
.scrmap
[screen_shot
.buf
[i
] & 0xff];
392 /* --------------------------------------------------------------------------------------------- */
395 show_console_contents_freebsd (int starty
, unsigned char begin_line
, unsigned char end_line
)
400 if (mc_global
.tty
.console_flag
== '\0')
403 for (line
= begin_line
; line
<= end_line
; line
++)
405 tty_gotoyx (starty
+ line
- begin_line
, 0);
406 for (col
= 0; col
< min (COLS
, screen_info
.mv_csz
); col
++)
408 c
= screen_shot
.buf
[line
* screen_info
.mv_csz
+ col
] & 0xFF;
414 /* --------------------------------------------------------------------------------------------- */
417 handle_console_freebsd (console_action_t action
)
433 case CONSOLE_RESTORE
:
440 #endif /* __FreeBSD__ */
442 /* --------------------------------------------------------------------------------------------- */
443 /*** public functions ****************************************************************************/
444 /* --------------------------------------------------------------------------------------------- */
447 show_console_contents (int starty
, unsigned char begin_line
, unsigned char end_line
)
449 tty_set_normal_attrs ();
451 if (look_for_rxvt_extensions ())
453 show_rxvt_contents (starty
, begin_line
, end_line
);
457 show_console_contents_linux (starty
, begin_line
, end_line
);
458 #elif defined (__FreeBSD__)
459 show_console_contents_freebsd (starty
, begin_line
, end_line
);
461 mc_global
.tty
.console_flag
= '\0';
465 /* --------------------------------------------------------------------------------------------- */
468 handle_console (console_action_t action
)
472 if (look_for_rxvt_extensions ())
476 handle_console_linux (action
);
477 #elif defined (__FreeBSD__)
478 handle_console_freebsd (action
);
482 /* --------------------------------------------------------------------------------------------- */