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;
95 if (mc_global
.tty
.console_flag
== '\0')
97 /* Paranoid: Is the cons.saver still running? */
98 if (cons_saver_pid
< 1 || kill (cons_saver_pid
, SIGCONT
))
101 mc_global
.tty
.console_flag
= '\0';
105 /* Send command to the console handler */
106 message
= CONSOLE_CONTENTS
;
107 (void) write (pipefd1
[1], &message
, 1);
108 /* Check for outdated cons.saver */
109 (void) read (pipefd2
[0], &message
, 1);
110 if (message
!= CONSOLE_CONTENTS
)
113 /* Send the range of lines that we want */
114 (void) write (pipefd1
[1], &begin_line
, 1);
115 (void) write (pipefd1
[1], &end_line
, 1);
116 /* Read the corresponding number of bytes */
117 (void) read (pipefd2
[0], &bytes
, 2);
119 /* Read the bytes and output them */
120 for (i
= 0; i
< bytes
; i
++)
123 tty_gotoyx (starty
+ (i
/ COLS
), 0);
124 (void) read (pipefd2
[0], &message
, 1);
125 tty_print_char (message
);
128 /* Read the value of the mc_global.tty.console_flag */
129 (void) read (pipefd2
[0], &message
, 1);
132 /* --------------------------------------------------------------------------------------------- */
135 handle_console_linux (console_action_t action
)
144 /* Close old pipe ends in case it is the 2nd time we run cons.saver */
145 status
= close (pipefd1
[1]);
146 status
= close (pipefd2
[0]);
147 /* Create two pipes for communication */
148 if (!((pipe (pipefd1
) == 0) && ((pipe (pipefd2
)) == 0)))
150 mc_global
.tty
.console_flag
= '\0';
153 /* Get the console saver running */
154 cons_saver_pid
= fork ();
155 if (cons_saver_pid
< 0)
159 status
= close (pipefd1
[1]);
160 status
= close (pipefd1
[0]);
161 status
= close (pipefd2
[1]);
162 status
= close (pipefd2
[0]);
163 mc_global
.tty
.console_flag
= '\0';
165 else if (cons_saver_pid
> 0)
168 /* Close the extra pipe ends */
169 status
= close (pipefd1
[0]);
170 status
= close (pipefd2
[1]);
171 /* Was the child successful? */
172 status
= read (pipefd2
[0], &mc_global
.tty
.console_flag
, 1);
173 if (mc_global
.tty
.console_flag
== '\0')
176 status
= close (pipefd1
[1]);
177 status
= close (pipefd2
[0]);
178 ret
= waitpid (cons_saver_pid
, &status
, 0);
184 /* Close the extra pipe ends */
185 status
= close (pipefd1
[1]);
186 status
= close (pipefd2
[0]);
187 tty_name
= ttyname (0);
188 /* Bind the pipe 0 to the standard input */
191 if (dup2 (pipefd1
[0], 0) == -1)
193 status
= close (pipefd1
[0]);
194 /* Bind the pipe 1 to the standard output */
195 if (dup2 (pipefd2
[1], 1) == -1)
198 status
= close (pipefd2
[1]);
199 /* Bind standard error to /dev/null */
200 status
= open ("/dev/null", O_WRONLY
);
201 if (dup2 (status
, 2) == -1)
203 status
= close (status
);
206 /* Exec the console save/restore handler */
207 mc_conssaver
= mc_build_filename (SAVERDIR
, "cons.saver", NULL
);
208 execl (mc_conssaver
, "cons.saver", tty_name
, (char *) NULL
);
210 /* Console is not a tty or execl() failed */
213 mc_global
.tty
.console_flag
= '\0';
214 status
= write (1, &mc_global
.tty
.console_flag
, 1);
216 } /* if (cons_saver_pid ...) */
221 case CONSOLE_RESTORE
:
222 /* Is tty console? */
223 if (mc_global
.tty
.console_flag
== '\0')
225 /* Paranoid: Is the cons.saver still running? */
226 if (cons_saver_pid
< 1 || kill (cons_saver_pid
, SIGCONT
))
229 mc_global
.tty
.console_flag
= '\0';
232 /* Send command to the console handler */
233 status
= write (pipefd1
[1], &action
, 1);
234 if (action
!= CONSOLE_DONE
)
236 /* Wait the console handler to do its job */
237 status
= read (pipefd2
[0], &mc_global
.tty
.console_flag
, 1);
239 if (action
== CONSOLE_DONE
|| mc_global
.tty
.console_flag
== '\0')
241 /* We are done -> Let's clean up */
244 (void) waitpid (cons_saver_pid
, &status
, 0);
245 mc_global
.tty
.console_flag
= '\0';
253 #elif defined(__FreeBSD__)
255 /* --------------------------------------------------------------------------------------------- */
257 * FreeBSD support copyright (C) 2003 Alexander Serkov <serkov@ukrpost.net>.
258 * Support for screenmaps by Max Khon <fjoe@FreeBSD.org>
264 if (mc_global
.tty
.console_flag
!= '\0')
267 screen_info
.size
= sizeof (screen_info
);
268 if (ioctl (FD_OUT
, CONS_GETINFO
, &screen_info
) == -1)
271 memset (&screen_shot
, 0, sizeof (screen_shot
));
272 screen_shot
.xsize
= screen_info
.mv_csz
;
273 screen_shot
.ysize
= screen_info
.mv_rsz
;
274 screen_shot
.buf
= g_try_malloc (screen_info
.mv_csz
* screen_info
.mv_rsz
* 2);
275 if (screen_shot
.buf
!= NULL
)
276 mc_global
.tty
.console_flag
= '\001';
279 /* --------------------------------------------------------------------------------------------- */
282 set_attr (unsigned attr
)
285 * Convert color indices returned by SCRSHOT (red=4, green=2, blue=1)
286 * to indices for ANSI sequences (red=1, green=2, blue=4).
288 static const int color_map
[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
292 bc
= (attr
>> 4) & 0xF;
294 printf ("\x1B[%d;%d;3%d;4%dm", (bc
& 8) ? 5 : 25, (tc
& 8) ? 1 : 22,
295 color_map
[tc
& 7], color_map
[bc
& 7]);
298 /* --------------------------------------------------------------------------------------------- */
301 console_restore (void)
305 if (mc_global
.tty
.console_flag
== '\0')
310 /* restoring all content up to cursor position */
311 last
= screen_info
.mv_row
* screen_info
.mv_csz
+ screen_info
.mv_col
;
312 for (i
= 0; i
< last
; ++i
)
314 set_attr ((screen_shot
.buf
[i
] >> 8) & 0xFF);
315 putc (screen_shot
.buf
[i
] & 0xFF, stdout
);
318 /* restoring cursor color */
319 set_attr ((screen_shot
.buf
[last
] >> 8) & 0xFF);
324 /* --------------------------------------------------------------------------------------------- */
327 console_shutdown (void)
329 if (mc_global
.tty
.console_flag
== '\0')
332 g_free (screen_shot
.buf
);
334 mc_global
.tty
.console_flag
= '\0';
337 /* --------------------------------------------------------------------------------------------- */
346 if (mc_global
.tty
.console_flag
== '\0')
349 /* screen_info.size is already set in console_init() */
350 if (ioctl (FD_OUT
, CONS_GETINFO
, &screen_info
) == -1)
356 /* handle console resize */
357 if (screen_info
.mv_csz
!= screen_shot
.xsize
|| screen_info
.mv_rsz
!= screen_shot
.ysize
)
363 if (ioctl (FD_OUT
, CONS_SCRSHOT
, &screen_shot
) == -1)
369 if (ioctl (FD_OUT
, GIO_SCRNMAP
, &map
) == -1)
375 for (i
= 0; i
< 256; i
++)
377 char *p
= memchr (map
.scrmap
, i
, 256);
378 revmap
.scrmap
[i
] = p
? p
- map
.scrmap
: i
;
381 for (i
= 0; i
< screen_shot
.xsize
* screen_shot
.ysize
; i
++)
384 (screen_shot
.buf
[i
] & 0xff00) | (unsigned char) revmap
.
385 scrmap
[screen_shot
.buf
[i
] & 0xff];
389 /* --------------------------------------------------------------------------------------------- */
392 show_console_contents_freebsd (int starty
, unsigned char begin_line
, unsigned char end_line
)
397 if (mc_global
.tty
.console_flag
== '\0')
400 for (line
= begin_line
; line
<= end_line
; line
++)
402 tty_gotoyx (starty
+ line
- begin_line
, 0);
403 for (col
= 0; col
< min (COLS
, screen_info
.mv_csz
); col
++)
405 c
= screen_shot
.buf
[line
* screen_info
.mv_csz
+ col
] & 0xFF;
411 /* --------------------------------------------------------------------------------------------- */
414 handle_console_freebsd (console_action_t action
)
430 case CONSOLE_RESTORE
:
437 #endif /* __FreeBSD__ */
439 /* --------------------------------------------------------------------------------------------- */
440 /*** public functions ****************************************************************************/
441 /* --------------------------------------------------------------------------------------------- */
444 show_console_contents (int starty
, unsigned char begin_line
, unsigned char end_line
)
446 tty_set_normal_attrs ();
448 if (look_for_rxvt_extensions ())
450 show_rxvt_contents (starty
, begin_line
, end_line
);
454 show_console_contents_linux (starty
, begin_line
, end_line
);
455 #elif defined (__FreeBSD__)
456 show_console_contents_freebsd (starty
, begin_line
, end_line
);
458 mc_global
.tty
.console_flag
= '\0';
462 /* --------------------------------------------------------------------------------------------- */
465 handle_console (console_action_t action
)
469 if (look_for_rxvt_extensions ())
473 handle_console_linux (action
);
474 #elif defined (__FreeBSD__)
475 handle_console_freebsd (action
);
479 /* --------------------------------------------------------------------------------------------- */