1 /* Client interface for General purpose Linux console save/restore server
2 Copyright (C) 1994 Janne Kukonlehto <jtklehto@stekt.oulu.fi>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* The cons saver can't have a pid of 1, used to prevent bunches of */
22 int cons_saver_pid
= 1;
23 extern int rxvt_extensions
;
24 signed char console_flag
= 0;
26 #if defined(linux) || defined(__linux__)
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_WAIT_H
35 # include <sys/wait.h>
40 #include "cons.saver.h"
43 static int pipefd1
[2] = {-1, -1}, pipefd2
[2] = {-1, -1};
45 void show_console_contents (int starty
, unsigned char begin_line
, unsigned char end_line
)
47 unsigned char message
= 0;
48 unsigned short bytes
= 0;
53 if (look_for_rxvt_extensions ()) {
54 show_rxvt_contents (starty
, begin_line
, end_line
);
61 /* Paranoid: Is the cons.saver still running? */
62 if (cons_saver_pid
< 1 || kill (cons_saver_pid
, SIGCONT
)){
68 /* Send command to the console handler */
69 message
= CONSOLE_CONTENTS
;
70 write (pipefd1
[1], &message
, 1);
71 /* Check for outdated cons.saver */
72 read (pipefd2
[0], &message
, 1);
73 if (message
!= CONSOLE_CONTENTS
)
76 /* Send the range of lines that we want */
77 write (pipefd1
[1], &begin_line
, 1);
78 write (pipefd1
[1], &end_line
, 1);
79 /* Read the corresponding number of bytes */
80 read (pipefd2
[0], &bytes
, 2);
82 /* Read the bytes and output them */
83 for (i
= 0; i
< bytes
; i
++){
85 move (starty
+(i
/COLS
), 0);
86 read (pipefd2
[0], &message
, 1);
90 /* Read the value of the console_flag */
91 read (pipefd2
[0], &message
, 1);
94 void handle_console (unsigned char action
)
102 if (look_for_rxvt_extensions ())
104 /* Close old pipe ends in case it is the 2nd time we run cons.saver */
107 /* Create two pipes for communication */
110 /* Get the console saver running */
111 cons_saver_pid
= fork ();
112 if (cons_saver_pid
< 0){
120 } else if (cons_saver_pid
> 0){
122 /* Close the extra pipe ends */
125 /* Was the child successful? */
126 read (pipefd2
[0], &console_flag
, 1);
130 waitpid (cons_saver_pid
, &status
, 0);
134 /* Close the extra pipe ends */
137 tty_name
= ttyname (0);
138 /* Bind the pipe 0 to the standard input */
142 /* Bind the pipe 1 to the standard output */
146 /* Bind standard error to /dev/null */
148 open ("/dev/null", O_WRONLY
);
149 /* Exec the console save/restore handler */
150 mc_conssaver
= concat_dir_and_file (mc_home
, "bin/cons.saver");
151 execl (mc_conssaver
, "cons.saver", tty_name
, NULL
);
154 write (1, &console_flag
, 1);
158 } /* if (cons_saver_pid ...) */
163 case CONSOLE_RESTORE
:
164 if (look_for_rxvt_extensions ())
166 /* Is tty console? */
169 /* Paranoid: Is the cons.saver still running? */
170 if (cons_saver_pid
< 1 || kill (cons_saver_pid
, SIGCONT
)){
175 /* Send command to the console handler */
176 write (pipefd1
[1], &action
, 1);
177 if (action
!= CONSOLE_DONE
){
178 /* Wait the console handler to do its job */
179 read (pipefd2
[0], &console_flag
, 1);
181 if (action
== CONSOLE_DONE
|| !console_flag
){
182 /* We are done -> Let's clean up */
185 waitpid (cons_saver_pid
, &status
, 0);
194 #endif /* #ifdef linux */
198 ** SCO console save/restore handling routines
199 ** Copyright (C) 1997 Alex Tkachenko <alex@bcs.zaporizhzhe.ua>
203 #include <sys/types.h>
205 #include <sys/console.h>
206 #include <sys/vtkd.h>
212 #include "cons.saver.h"
214 static int FD_OUT
= 2;
216 static unsigned short* vidbuf
= NULL
;
217 static unsigned short* screen
= NULL
;
218 static int height
= 0, width
= 0, saved_attr
= 0;
221 #define SIG_ACQUIRE 21 /* originally: handset, line status change (?) */
227 int adapter
= ioctl(FD_OUT
, CONS_CURRENT
, 0);
229 vi
.size
= sizeof(struct vid_info
);
230 ioctl(FD_OUT
, CONS_GETINFO
, &(vi
));
231 return (vi
.m_num
== ioctl(FD_OUT
,CONSADP
,adapter
));
237 struct vt_mode smode
;
239 signal(SIG_ACQUIRE
, SIG_DFL
);
240 smode
.mode
= VT_AUTO
;
241 smode
.waitv
= smode
.relsig
= smode
.acqsig
= smode
.frsig
= 0;
242 ioctl(FD_OUT
, VT_SETMODE
, &smode
);
243 ioctl(FD_OUT
, VT_RELDISP
, VT_ACKACQ
);
263 struct m6845_info mi
;
272 struct vt_mode smode
;
275 ** User switched out of our vt. Let's wait until we get SIG_ACQUIRE,
276 ** otherwise we could save wrong screen image
278 signal(SIG_ACQUIRE
, console_acquire_vt
);
279 smode
.mode
= VT_PROCESS
;
281 smode
.waitv
= smode
.relsig
= smode
.acqsig
= smode
.frsig
= SIG_ACQUIRE
;
282 ioctl(FD_OUT
, VT_SETMODE
, &smode
);
287 saved_attr
= ioctl(FD_OUT
, GIO_ATTR
, 0);
289 vidbuf
= (unsigned short*) ioctl(FD_OUT
, MAPCONS
, 0);
291 mi
.size
= sizeof(struct m6845_info
);
292 ioctl(FD_OUT
, CONS_6845INFO
, &mi
);
295 unsigned short* start
= vidbuf
+ mi
.screen_top
;
296 memcpy(screen
, start
, width
* height
* 2);
299 write(FD_OUT
,"\0337",2); /* save cursor position */
305 struct m6845_info mi
;
306 unsigned short* start
;
313 write (FD_OUT
, "\033[2J", 4);
315 mi
.size
= sizeof(struct m6845_info
);
316 ioctl(FD_OUT
, CONS_6845INFO
, &mi
);
318 start
= vidbuf
+ mi
.screen_top
;
319 memcpy(start
, screen
, width
* height
* 2);
320 write(FD_OUT
,"\0338",2); /* restore cursor position */
327 int adapter
= ioctl(FD_OUT
, CONS_CURRENT
, 0);
333 vi
.size
= sizeof(struct vid_info
);
334 ioctl(FD_OUT
, CONS_GETINFO
, &(vi
));
343 screen
= (unsigned short*) xmalloc(height
* width
* 2,"console_init");
350 mode
= ioctl(FD_OUT
, CONS_GET
, 0);
351 ioctl(FD_OUT
, MODESWITCH
| mode
, 0);
358 handle_console (unsigned char action
)
360 if (look_for_rxvt_extensions ())
375 case CONSOLE_RESTORE
:
384 show_console_contents (int starty
, unsigned char begin_line
, unsigned char end_line
)
386 register int i
, len
= (end_line
- begin_line
) * width
;
388 if (look_for_rxvt_extensions ()) {
389 show_rxvt_contents (starty
, begin_line
, end_line
);
392 attrset(DEFAULT_COLOR
);
393 for (i
= 0; i
< len
; i
++)
395 if ((i
% width
) == 0)
396 move (starty
+(i
/width
), 0);
397 addch ((unsigned char)screen
[width
*starty
+ i
]);
401 #endif /* SCO_FLAVOR */
404 #if !defined(linux) && !defined(__linux__) && !defined(SCO_FLAVOR)
408 void show_console_contents (int starty
, unsigned char begin_line
, unsigned char end_line
)
412 if (look_for_rxvt_extensions ()) {
413 show_rxvt_contents (starty
, begin_line
, end_line
);
419 void handle_console (unsigned char action
)
421 look_for_rxvt_extensions ();
424 #endif /* !defined(linux) && !defined(__linux__) && !defined(SCO_FLAVOR) */