1 /* Client interface for General purpose Linux console save/restore server
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
24 #include <sys/types.h>
26 # include <sys/consio.h>
27 # include <sys/ioctl.h>
31 #include <mhl/string.h>
35 #include "cons.saver.h"
37 signed char console_flag
= 0;
41 /* The cons saver can't have a pid of 1, used to prevent bunches of
44 int cons_saver_pid
= 1;
45 static int pipefd1
[2] = { -1, -1 };
46 static int pipefd2
[2] = { -1, -1 };
49 show_console_contents_linux (int starty
, unsigned char begin_line
,
50 unsigned char end_line
)
52 unsigned char message
= 0;
53 unsigned short bytes
= 0;
59 /* Paranoid: Is the cons.saver still running? */
60 if (cons_saver_pid
< 1 || kill (cons_saver_pid
, SIGCONT
)) {
66 /* Send command to the console handler */
67 message
= CONSOLE_CONTENTS
;
68 write (pipefd1
[1], &message
, 1);
69 /* Check for outdated cons.saver */
70 read (pipefd2
[0], &message
, 1);
71 if (message
!= CONSOLE_CONTENTS
)
74 /* Send the range of lines that we want */
75 write (pipefd1
[1], &begin_line
, 1);
76 write (pipefd1
[1], &end_line
, 1);
77 /* Read the corresponding number of bytes */
78 read (pipefd2
[0], &bytes
, 2);
80 /* Read the bytes and output them */
81 for (i
= 0; i
< bytes
; i
++) {
83 move (starty
+ (i
/ COLS
), 0);
84 read (pipefd2
[0], &message
, 1);
88 /* Read the value of the console_flag */
89 read (pipefd2
[0], &message
, 1);
93 handle_console_linux (unsigned char action
)
101 /* Close old pipe ends in case it is the 2nd time we run cons.saver */
104 /* Create two pipes for communication */
107 /* Get the console saver running */
108 cons_saver_pid
= fork ();
109 if (cons_saver_pid
< 0) {
117 } else if (cons_saver_pid
> 0) {
119 /* Close the extra pipe ends */
122 /* Was the child successful? */
123 read (pipefd2
[0], &console_flag
, 1);
127 waitpid (cons_saver_pid
, &status
, 0);
131 /* Close the extra pipe ends */
134 tty_name
= ttyname (0);
135 /* Bind the pipe 0 to the standard input */
139 /* Bind the pipe 1 to the standard output */
143 /* Bind standard error to /dev/null */
145 open ("/dev/null", O_WRONLY
);
147 /* Exec the console save/restore handler */
148 mc_conssaver
= mhl_str_dir_plus_file (SAVERDIR
, "cons.saver");
149 execl (mc_conssaver
, "cons.saver", tty_name
, (char *) NULL
);
151 /* Console is not a tty or execl() failed */
153 write (1, &console_flag
, 1);
155 } /* if (cons_saver_pid ...) */
160 case CONSOLE_RESTORE
:
161 /* Is tty console? */
164 /* Paranoid: Is the cons.saver still running? */
165 if (cons_saver_pid
< 1 || kill (cons_saver_pid
, SIGCONT
)) {
170 /* Send command to the console handler */
171 write (pipefd1
[1], &action
, 1);
172 if (action
!= CONSOLE_DONE
) {
173 /* Wait the console handler to do its job */
174 read (pipefd2
[0], &console_flag
, 1);
176 if (action
== CONSOLE_DONE
|| !console_flag
) {
177 /* We are done -> Let's clean up */
180 waitpid (cons_saver_pid
, &status
, 0);
187 #elif defined(__FreeBSD__)
190 * FreeBSD support copyright (C) 2003 Alexander Serkov <serkov@ukrpost.net>.
191 * Support for screenmaps by Max Khon <fjoe@FreeBSD.org>
196 static struct scrshot screen_shot
;
197 static struct vid_info screen_info
;
205 screen_info
.size
= sizeof (screen_info
);
206 if (ioctl (FD_OUT
, CONS_GETINFO
, &screen_info
) == -1)
209 memset (&screen_shot
, 0, sizeof (screen_shot
));
210 screen_shot
.xsize
= screen_info
.mv_csz
;
211 screen_shot
.ysize
= screen_info
.mv_rsz
;
212 if ((screen_shot
.buf
=
213 g_malloc (screen_info
.mv_csz
* screen_info
.mv_rsz
* 2)) == NULL
)
220 set_attr (unsigned attr
)
223 * Convert color indices returned by SCRSHOT (red=4, green=2, blue=1)
224 * to indices for ANSI sequences (red=1, green=2, blue=4).
226 static const int color_map
[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
230 bc
= (attr
>> 4) & 0xF;
232 printf ("\x1B[%d;%d;3%d;4%dm", (bc
& 8) ? 5 : 25, (tc
& 8) ? 1 : 22,
233 color_map
[tc
& 7], color_map
[bc
& 7]);
236 #define cursor_to(x, y) do { \
237 printf("\x1B[%d;%df", (y) + 1, (x) + 1); \
242 console_restore (void)
251 /* restoring all content up to cursor position */
252 last
= screen_info
.mv_row
* screen_info
.mv_csz
+ screen_info
.mv_col
;
253 for (i
= 0; i
< last
; ++i
) {
254 set_attr ((screen_shot
.buf
[i
] >> 8) & 0xFF);
255 putc (screen_shot
.buf
[i
] & 0xFF, stdout
);
258 /* restoring cursor color */
259 set_attr ((screen_shot
.buf
[last
] >> 8) & 0xFF);
265 console_shutdown (void)
270 g_free (screen_shot
.buf
);
285 /* screen_info.size is already set in console_init() */
286 if (ioctl (FD_OUT
, CONS_GETINFO
, &screen_info
) == -1) {
291 /* handle console resize */
292 if (screen_info
.mv_csz
!= screen_shot
.xsize
293 || screen_info
.mv_rsz
!= screen_shot
.ysize
) {
298 if (ioctl (FD_OUT
, CONS_SCRSHOT
, &screen_shot
) == -1) {
303 if (ioctl (FD_OUT
, GIO_SCRNMAP
, &map
) == -1) {
308 for (i
= 0; i
< 256; i
++) {
309 char *p
= memchr (map
.scrmap
, i
, 256);
310 revmap
.scrmap
[i
] = p
? p
- map
.scrmap
: i
;
313 for (i
= 0; i
< screen_shot
.xsize
* screen_shot
.ysize
; i
++) {
315 (screen_shot
.buf
[i
] & 0xff00) | (unsigned char) revmap
.
316 scrmap
[screen_shot
.buf
[i
] & 0xff];
321 show_console_contents_freebsd (int starty
, unsigned char begin_line
,
322 unsigned char end_line
)
330 for (line
= begin_line
; line
<= end_line
; line
++) {
331 move (starty
+ line
- begin_line
, 0);
332 for (col
= 0; col
< min (COLS
, screen_info
.mv_csz
); col
++) {
333 c
= screen_shot
.buf
[line
* screen_info
.mv_csz
+ col
] & 0xFF;
340 handle_console_freebsd (unsigned char action
)
355 case CONSOLE_RESTORE
:
360 #endif /* __FreeBSD__ */
363 show_console_contents (int starty
, unsigned char begin_line
,
364 unsigned char end_line
)
368 if (look_for_rxvt_extensions ()) {
369 show_rxvt_contents (starty
, begin_line
, end_line
);
373 show_console_contents_linux (starty
, begin_line
, end_line
);
374 #elif defined (__FreeBSD__)
375 show_console_contents_freebsd (starty
, begin_line
, end_line
);
382 handle_console (unsigned char action
)
386 if (look_for_rxvt_extensions ())
390 handle_console_linux (action
);
391 #elif defined (__FreeBSD__)
392 handle_console_freebsd (action
);