2 Client interface for General purpose Linux console save/restore server
4 Copyright (C) 1994-2016
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
33 #include <sys/types.h>
35 #include <sys/consio.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
41 #include "lib/global.h"
43 #include "lib/unixcompat.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);
134 /* --------------------------------------------------------------------------------------------- */
137 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);
187 /* Close the extra pipe ends */
188 status
= close (pipefd1
[1]);
189 status
= close (pipefd2
[0]);
190 tty_name
= ttyname (0);
191 /* Bind the pipe 0 to the standard input */
194 if (dup2 (pipefd1
[0], STDIN_FILENO
) == -1)
196 status
= close (pipefd1
[0]);
197 /* Bind the pipe 1 to the standard output */
198 if (dup2 (pipefd2
[1], STDOUT_FILENO
) == -1)
201 status
= close (pipefd2
[1]);
202 /* Bind standard error to /dev/null */
203 status
= open ("/dev/null", O_WRONLY
);
204 if (dup2 (status
, STDERR_FILENO
) == -1)
206 status
= close (status
);
207 if (tty_name
!= NULL
)
211 /* Exec the console save/restore handler */
212 mc_conssaver
= mc_build_filename (SAVERDIR
, "cons.saver", NULL
);
213 execl (mc_conssaver
, "cons.saver", tty_name
, (char *) NULL
);
215 /* Console is not a tty or execl() failed */
218 mc_global
.tty
.console_flag
= '\0';
219 status
= write (1, &mc_global
.tty
.console_flag
, 1);
221 } /* if (cons_saver_pid ...) */
226 case CONSOLE_RESTORE
:
227 /* Is tty console? */
228 if (mc_global
.tty
.console_flag
== '\0')
230 /* Paranoid: Is the cons.saver still running? */
231 if (cons_saver_pid
< 1 || kill (cons_saver_pid
, SIGCONT
))
234 mc_global
.tty
.console_flag
= '\0';
237 /* Send command to the console handler */
238 status
= write (pipefd1
[1], &action
, 1);
239 if (action
!= CONSOLE_DONE
)
241 /* Wait the console handler to do its job */
242 status
= read (pipefd2
[0], &mc_global
.tty
.console_flag
, 1);
244 if (action
== CONSOLE_DONE
|| mc_global
.tty
.console_flag
== '\0')
246 /* We are done -> Let's clean up */
250 ret
= waitpid (cons_saver_pid
, &status
, 0);
252 mc_global
.tty
.console_flag
= '\0';
260 #elif defined(__FreeBSD__)
262 /* --------------------------------------------------------------------------------------------- */
264 * FreeBSD support copyright (C) 2003 Alexander Serkov <serkov@ukrpost.net>.
265 * Support for screenmaps by Max Khon <fjoe@FreeBSD.org>
271 if (mc_global
.tty
.console_flag
!= '\0')
274 screen_info
.size
= sizeof (screen_info
);
275 if (ioctl (FD_OUT
, CONS_GETINFO
, &screen_info
) == -1)
278 memset (&screen_shot
, 0, sizeof (screen_shot
));
279 screen_shot
.xsize
= screen_info
.mv_csz
;
280 screen_shot
.ysize
= screen_info
.mv_rsz
;
281 screen_shot
.buf
= g_try_malloc (screen_info
.mv_csz
* screen_info
.mv_rsz
* 2);
282 if (screen_shot
.buf
!= NULL
)
283 mc_global
.tty
.console_flag
= '\001';
286 /* --------------------------------------------------------------------------------------------- */
289 set_attr (unsigned attr
)
292 * Convert color indices returned by SCRSHOT (red=4, green=2, blue=1)
293 * to indices for ANSI sequences (red=1, green=2, blue=4).
295 static const int color_map
[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
299 bc
= (attr
>> 4) & 0xF;
301 printf ("\x1B[%d;%d;3%d;4%dm", (bc
& 8) ? 5 : 25, (tc
& 8) ? 1 : 22,
302 color_map
[tc
& 7], color_map
[bc
& 7]);
305 /* --------------------------------------------------------------------------------------------- */
308 console_restore (void)
312 if (mc_global
.tty
.console_flag
== '\0')
317 /* restoring all content up to cursor position */
318 last
= screen_info
.mv_row
* screen_info
.mv_csz
+ screen_info
.mv_col
;
319 for (i
= 0; i
< last
; ++i
)
321 set_attr ((screen_shot
.buf
[i
] >> 8) & 0xFF);
322 putc (screen_shot
.buf
[i
] & 0xFF, stdout
);
325 /* restoring cursor color */
326 set_attr ((screen_shot
.buf
[last
] >> 8) & 0xFF);
331 /* --------------------------------------------------------------------------------------------- */
334 console_shutdown (void)
336 if (mc_global
.tty
.console_flag
== '\0')
339 g_free (screen_shot
.buf
);
341 mc_global
.tty
.console_flag
= '\0';
344 /* --------------------------------------------------------------------------------------------- */
353 if (mc_global
.tty
.console_flag
== '\0')
356 /* screen_info.size is already set in console_init() */
357 if (ioctl (FD_OUT
, CONS_GETINFO
, &screen_info
) == -1)
363 /* handle console resize */
364 if (screen_info
.mv_csz
!= screen_shot
.xsize
|| screen_info
.mv_rsz
!= screen_shot
.ysize
)
370 if (ioctl (FD_OUT
, CONS_SCRSHOT
, &screen_shot
) == -1)
376 if (ioctl (FD_OUT
, GIO_SCRNMAP
, &map
) == -1)
382 for (i
= 0; i
< 256; i
++)
384 char *p
= memchr (map
.scrmap
, i
, 256);
385 revmap
.scrmap
[i
] = p
? p
- map
.scrmap
: i
;
388 for (i
= 0; i
< screen_shot
.xsize
* screen_shot
.ysize
; i
++)
391 screen_shot
.buf
[i
] = (screen_shot
.buf
[i
] & 0xff00)
392 | (unsigned char) revmap
.scrmap
[screen_shot
.buf
[i
] & 0xff];
397 /* --------------------------------------------------------------------------------------------- */
400 show_console_contents_freebsd (int starty
, unsigned char begin_line
, unsigned char end_line
)
405 if (mc_global
.tty
.console_flag
== '\0')
408 for (line
= begin_line
; line
<= end_line
; line
++)
410 tty_gotoyx (starty
+ line
- begin_line
, 0);
411 for (col
= 0; col
< min (COLS
, screen_info
.mv_csz
); col
++)
413 c
= screen_shot
.buf
[line
* screen_info
.mv_csz
+ col
] & 0xFF;
419 /* --------------------------------------------------------------------------------------------- */
422 handle_console_freebsd (console_action_t action
)
438 case CONSOLE_RESTORE
:
445 #endif /* __FreeBSD__ */
447 /* --------------------------------------------------------------------------------------------- */
448 /*** public functions ****************************************************************************/
449 /* --------------------------------------------------------------------------------------------- */
452 show_console_contents (int starty
, unsigned char begin_line
, unsigned char end_line
)
454 tty_set_normal_attrs ();
456 if (look_for_rxvt_extensions ())
458 show_rxvt_contents (starty
, begin_line
, end_line
);
462 show_console_contents_linux (starty
, begin_line
, end_line
);
463 #elif defined (__FreeBSD__)
464 show_console_contents_freebsd (starty
, begin_line
, end_line
);
466 mc_global
.tty
.console_flag
= '\0';
470 /* --------------------------------------------------------------------------------------------- */
473 handle_console (console_action_t action
)
477 if (look_for_rxvt_extensions ())
481 handle_console_linux (action
);
482 #elif defined (__FreeBSD__)
483 handle_console_freebsd (action
);
487 /* --------------------------------------------------------------------------------------------- */