2 Client interface for General purpose Linux console save/restore server
4 Copyright (C) 1994-2014
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software: you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation, either version 3 of the License,
12 or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /** \file cons.handler.c
24 * \brief Source: client %interface for General purpose Linux console save/restore server
34 #include <sys/types.h>
36 #include <sys/consio.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
42 #include "lib/global.h"
44 #include "lib/unixcompat.h"
45 #include "lib/tty/tty.h"
46 #include "lib/skin.h" /* tty_set_normal_attrs */
47 #include "lib/tty/win.h"
48 #include "lib/util.h" /* mc_build_filename() */
50 #include "consaver/cons.saver.h"
52 /*** global variables ****************************************************************************/
55 int cons_saver_pid
= 1;
56 #endif /* __linux__ */
58 /*** file scope macro definitions ****************************************************************/
60 #if defined(__FreeBSD__)
62 #define cursor_to(x, y) \
65 printf("\x1B[%d;%df", (y) + 1, (x) + 1); \
68 #endif /* __linux__ */
70 /*** file scope type declarations ****************************************************************/
72 /*** file scope variables ************************************************************************/
75 /* The cons saver can't have a pid of 1, used to prevent bunches of
77 static int pipefd1
[2] = { -1, -1 };
78 static int pipefd2
[2] = { -1, -1 };
79 #elif defined(__FreeBSD__)
80 static struct scrshot screen_shot
;
81 static struct vid_info screen_info
;
82 #endif /* __linux__ */
84 /*** file scope functions ************************************************************************/
85 /* --------------------------------------------------------------------------------------------- */
89 show_console_contents_linux (int starty
, unsigned char begin_line
, unsigned char end_line
)
91 unsigned char message
= 0;
92 unsigned short bytes
= 0;
97 if (mc_global
.tty
.console_flag
== '\0')
99 /* Paranoid: Is the cons.saver still running? */
100 if (cons_saver_pid
< 1 || kill (cons_saver_pid
, SIGCONT
))
103 mc_global
.tty
.console_flag
= '\0';
107 /* Send command to the console handler */
108 message
= CONSOLE_CONTENTS
;
109 ret
= write (pipefd1
[1], &message
, 1);
110 /* Check for outdated cons.saver */
111 ret
= read (pipefd2
[0], &message
, 1);
112 if (message
!= CONSOLE_CONTENTS
)
115 /* Send the range of lines that we want */
116 ret
= write (pipefd1
[1], &begin_line
, 1);
117 ret
= write (pipefd1
[1], &end_line
, 1);
118 /* Read the corresponding number of bytes */
119 ret
= read (pipefd2
[0], &bytes
, 2);
121 /* Read the bytes and output them */
122 for (i
= 0; i
< bytes
; i
++)
125 tty_gotoyx (starty
+ (i
/ COLS
), 0);
126 ret
= read (pipefd2
[0], &message
, 1);
127 tty_print_char (message
);
130 /* Read the value of the mc_global.tty.console_flag */
131 ret
= read (pipefd2
[0], &message
, 1);
135 /* --------------------------------------------------------------------------------------------- */
138 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);
188 /* Close the extra pipe ends */
189 status
= close (pipefd1
[1]);
190 status
= close (pipefd2
[0]);
191 tty_name
= ttyname (0);
192 /* Bind the pipe 0 to the standard input */
195 if (dup2 (pipefd1
[0], STDIN_FILENO
) == -1)
197 status
= close (pipefd1
[0]);
198 /* Bind the pipe 1 to the standard output */
199 if (dup2 (pipefd2
[1], STDOUT_FILENO
) == -1)
202 status
= close (pipefd2
[1]);
203 /* Bind standard error to /dev/null */
204 status
= open ("/dev/null", O_WRONLY
);
205 if (dup2 (status
, STDERR_FILENO
) == -1)
207 status
= close (status
);
208 if (tty_name
!= NULL
)
212 /* Exec the console save/restore handler */
213 mc_conssaver
= mc_build_filename (SAVERDIR
, "cons.saver", NULL
);
214 execl (mc_conssaver
, "cons.saver", tty_name
, (char *) NULL
);
216 /* Console is not a tty or execl() failed */
219 mc_global
.tty
.console_flag
= '\0';
220 status
= write (1, &mc_global
.tty
.console_flag
, 1);
222 } /* if (cons_saver_pid ...) */
227 case CONSOLE_RESTORE
:
228 /* Is tty console? */
229 if (mc_global
.tty
.console_flag
== '\0')
231 /* Paranoid: Is the cons.saver still running? */
232 if (cons_saver_pid
< 1 || kill (cons_saver_pid
, SIGCONT
))
235 mc_global
.tty
.console_flag
= '\0';
238 /* Send command to the console handler */
239 status
= write (pipefd1
[1], &action
, 1);
240 if (action
!= CONSOLE_DONE
)
242 /* Wait the console handler to do its job */
243 status
= read (pipefd2
[0], &mc_global
.tty
.console_flag
, 1);
245 if (action
== CONSOLE_DONE
|| mc_global
.tty
.console_flag
== '\0')
247 /* We are done -> Let's clean up */
251 ret
= waitpid (cons_saver_pid
, &status
, 0);
253 mc_global
.tty
.console_flag
= '\0';
261 #elif defined(__FreeBSD__)
263 /* --------------------------------------------------------------------------------------------- */
265 * FreeBSD support copyright (C) 2003 Alexander Serkov <serkov@ukrpost.net>.
266 * Support for screenmaps by Max Khon <fjoe@FreeBSD.org>
272 if (mc_global
.tty
.console_flag
!= '\0')
275 screen_info
.size
= sizeof (screen_info
);
276 if (ioctl (FD_OUT
, CONS_GETINFO
, &screen_info
) == -1)
279 memset (&screen_shot
, 0, sizeof (screen_shot
));
280 screen_shot
.xsize
= screen_info
.mv_csz
;
281 screen_shot
.ysize
= screen_info
.mv_rsz
;
282 screen_shot
.buf
= g_try_malloc (screen_info
.mv_csz
* screen_info
.mv_rsz
* 2);
283 if (screen_shot
.buf
!= NULL
)
284 mc_global
.tty
.console_flag
= '\001';
287 /* --------------------------------------------------------------------------------------------- */
290 set_attr (unsigned attr
)
293 * Convert color indices returned by SCRSHOT (red=4, green=2, blue=1)
294 * to indices for ANSI sequences (red=1, green=2, blue=4).
296 static const int color_map
[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
300 bc
= (attr
>> 4) & 0xF;
302 printf ("\x1B[%d;%d;3%d;4%dm", (bc
& 8) ? 5 : 25, (tc
& 8) ? 1 : 22,
303 color_map
[tc
& 7], color_map
[bc
& 7]);
306 /* --------------------------------------------------------------------------------------------- */
309 console_restore (void)
313 if (mc_global
.tty
.console_flag
== '\0')
318 /* restoring all content up to cursor position */
319 last
= screen_info
.mv_row
* screen_info
.mv_csz
+ screen_info
.mv_col
;
320 for (i
= 0; i
< last
; ++i
)
322 set_attr ((screen_shot
.buf
[i
] >> 8) & 0xFF);
323 putc (screen_shot
.buf
[i
] & 0xFF, stdout
);
326 /* restoring cursor color */
327 set_attr ((screen_shot
.buf
[last
] >> 8) & 0xFF);
332 /* --------------------------------------------------------------------------------------------- */
335 console_shutdown (void)
337 if (mc_global
.tty
.console_flag
== '\0')
340 g_free (screen_shot
.buf
);
342 mc_global
.tty
.console_flag
= '\0';
345 /* --------------------------------------------------------------------------------------------- */
354 if (mc_global
.tty
.console_flag
== '\0')
357 /* screen_info.size is already set in console_init() */
358 if (ioctl (FD_OUT
, CONS_GETINFO
, &screen_info
) == -1)
364 /* handle console resize */
365 if (screen_info
.mv_csz
!= screen_shot
.xsize
|| screen_info
.mv_rsz
!= screen_shot
.ysize
)
371 if (ioctl (FD_OUT
, CONS_SCRSHOT
, &screen_shot
) == -1)
377 if (ioctl (FD_OUT
, GIO_SCRNMAP
, &map
) == -1)
383 for (i
= 0; i
< 256; i
++)
385 char *p
= memchr (map
.scrmap
, i
, 256);
386 revmap
.scrmap
[i
] = p
? p
- map
.scrmap
: i
;
389 for (i
= 0; i
< screen_shot
.xsize
* screen_shot
.ysize
; i
++)
392 screen_shot
.buf
[i
] = (screen_shot
.buf
[i
] & 0xff00)
393 | (unsigned char) revmap
.scrmap
[screen_shot
.buf
[i
] & 0xff];
398 /* --------------------------------------------------------------------------------------------- */
401 show_console_contents_freebsd (int starty
, unsigned char begin_line
, unsigned char end_line
)
406 if (mc_global
.tty
.console_flag
== '\0')
409 for (line
= begin_line
; line
<= end_line
; line
++)
411 tty_gotoyx (starty
+ line
- begin_line
, 0);
412 for (col
= 0; col
< min (COLS
, screen_info
.mv_csz
); col
++)
414 c
= screen_shot
.buf
[line
* screen_info
.mv_csz
+ col
] & 0xFF;
420 /* --------------------------------------------------------------------------------------------- */
423 handle_console_freebsd (console_action_t action
)
439 case CONSOLE_RESTORE
:
446 #endif /* __FreeBSD__ */
448 /* --------------------------------------------------------------------------------------------- */
449 /*** public functions ****************************************************************************/
450 /* --------------------------------------------------------------------------------------------- */
453 show_console_contents (int starty
, unsigned char begin_line
, unsigned char end_line
)
455 tty_set_normal_attrs ();
457 if (look_for_rxvt_extensions ())
459 show_rxvt_contents (starty
, begin_line
, end_line
);
463 show_console_contents_linux (starty
, begin_line
, end_line
);
464 #elif defined (__FreeBSD__)
465 show_console_contents_freebsd (starty
, begin_line
, end_line
);
467 mc_global
.tty
.console_flag
= '\0';
471 /* --------------------------------------------------------------------------------------------- */
474 handle_console (console_action_t action
)
478 if (look_for_rxvt_extensions ())
482 handle_console_linux (action
);
483 #elif defined (__FreeBSD__)
484 handle_console_freebsd (action
);
488 /* --------------------------------------------------------------------------------------------- */