Merge branch '1771_menu_hide_cursor'
[midnight-commander.git] / src / cons.handler.c
blobcee2503d484fb3b3c8b9ce9e5b24a163d2896b59
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. */
19 /** \file cons.handler.c
20 * \brief Source: client %interface for General purpose Linux console save/restore server
23 #include <config.h>
25 #include <stdlib.h>
26 #include <sys/wait.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <sys/types.h>
31 #ifdef __FreeBSD__
32 # include <sys/consio.h>
33 # include <sys/ioctl.h>
34 #endif
35 #include <unistd.h>
37 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/skin.h" /* tty_set_normal_attrs */
41 #include "lib/tty/win.h"
43 #include "consaver/cons.saver.h"
45 signed char console_flag = 0;
47 #ifdef __linux__
49 /* The cons saver can't have a pid of 1, used to prevent bunches of
50 * #ifdef linux */
52 int cons_saver_pid = 1;
53 static int pipefd1[2] = { -1, -1 };
54 static int pipefd2[2] = { -1, -1 };
56 static void
57 show_console_contents_linux (int starty, unsigned char begin_line, unsigned char end_line)
59 unsigned char message = 0;
60 unsigned short bytes = 0;
61 int i;
62 ssize_t ret;
64 /* Is tty console? */
65 if (!console_flag)
66 return;
67 /* Paranoid: Is the cons.saver still running? */
68 if (cons_saver_pid < 1 || kill (cons_saver_pid, SIGCONT))
70 cons_saver_pid = 0;
71 console_flag = 0;
72 return;
75 /* Send command to the console handler */
76 message = CONSOLE_CONTENTS;
77 ret = write (pipefd1[1], &message, 1);
78 /* Check for outdated cons.saver */
79 ret = read (pipefd2[0], &message, 1);
80 if (message != CONSOLE_CONTENTS)
81 return;
83 /* Send the range of lines that we want */
84 ret = write (pipefd1[1], &begin_line, 1);
85 ret = write (pipefd1[1], &end_line, 1);
86 /* Read the corresponding number of bytes */
87 ret = read (pipefd2[0], &bytes, 2);
89 /* Read the bytes and output them */
90 for (i = 0; i < bytes; i++)
92 if ((i % COLS) == 0)
93 tty_gotoyx (starty + (i / COLS), 0);
94 ret = read (pipefd2[0], &message, 1);
95 tty_print_char (message);
98 /* Read the value of the console_flag */
99 ret = read (pipefd2[0], &message, 1);
102 static void
103 handle_console_linux (unsigned char action)
105 char *tty_name;
106 char *mc_conssaver;
107 int status;
109 switch (action)
111 case CONSOLE_INIT:
112 /* Close old pipe ends in case it is the 2nd time we run cons.saver */
113 status = close (pipefd1[1]);
114 status = close (pipefd2[0]);
115 /* Create two pipes for communication */
116 if (!((pipe (pipefd1) == 0) && ((pipe (pipefd2)) == 0)))
118 console_flag = 0;
119 break;
121 /* Get the console saver running */
122 cons_saver_pid = fork ();
123 if (cons_saver_pid < 0)
125 /* Cannot fork */
126 /* Delete pipes */
127 status = close (pipefd1[1]);
128 status = close (pipefd1[0]);
129 status = close (pipefd2[1]);
130 status = close (pipefd2[0]);
131 console_flag = 0;
133 else if (cons_saver_pid > 0)
135 /* Parent */
136 /* Close the extra pipe ends */
137 status = close (pipefd1[0]);
138 status = close (pipefd2[1]);
139 /* Was the child successful? */
140 status = read (pipefd2[0], &console_flag, 1);
141 if (!console_flag)
143 pid_t ret;
144 status = close (pipefd1[1]);
145 status = close (pipefd2[0]);
146 ret = waitpid (cons_saver_pid, &status, 0);
149 else
151 /* Child */
152 /* Close the extra pipe ends */
153 status = close (pipefd1[1]);
154 status = close (pipefd2[0]);
155 tty_name = ttyname (0);
156 /* Bind the pipe 0 to the standard input */
159 if (dup2 (pipefd1[0], 0) == -1)
160 break;
161 status = close (pipefd1[0]);
162 /* Bind the pipe 1 to the standard output */
163 if (dup2 (pipefd2[1], 1) == -1)
164 break;
166 status = close (pipefd2[1]);
167 /* Bind standard error to /dev/null */
168 status = open ("/dev/null", O_WRONLY);
169 if (dup2 (status, 2) == -1)
170 break;
171 status = close (status);
172 if (tty_name)
174 /* Exec the console save/restore handler */
175 mc_conssaver = concat_dir_and_file (SAVERDIR, "cons.saver");
176 execl (mc_conssaver, "cons.saver", tty_name, (char *) NULL);
178 /* Console is not a tty or execl() failed */
180 while (0);
181 console_flag = 0;
182 status = write (1, &console_flag, 1);
183 _exit (3);
184 } /* if (cons_saver_pid ...) */
185 break;
187 case CONSOLE_DONE:
188 case CONSOLE_SAVE:
189 case CONSOLE_RESTORE:
190 /* Is tty console? */
191 if (!console_flag)
192 return;
193 /* Paranoid: Is the cons.saver still running? */
194 if (cons_saver_pid < 1 || kill (cons_saver_pid, SIGCONT))
196 cons_saver_pid = 0;
197 console_flag = 0;
198 return;
200 /* Send command to the console handler */
201 status = write (pipefd1[1], &action, 1);
202 if (action != CONSOLE_DONE)
204 /* Wait the console handler to do its job */
205 status = read (pipefd2[0], &console_flag, 1);
207 if (action == CONSOLE_DONE || !console_flag)
209 /* We are done -> Let's clean up */
210 pid_t ret;
211 close (pipefd1[1]);
212 close (pipefd2[0]);
213 ret = waitpid (cons_saver_pid, &status, 0);
214 console_flag = 0;
216 break;
220 #elif defined(__FreeBSD__)
223 * FreeBSD support copyright (C) 2003 Alexander Serkov <serkov@ukrpost.net>.
224 * Support for screenmaps by Max Khon <fjoe@FreeBSD.org>
227 #define FD_OUT 1
229 static struct scrshot screen_shot;
230 static struct vid_info screen_info;
232 static void
233 console_init (void)
235 if (console_flag)
236 return;
238 screen_info.size = sizeof (screen_info);
239 if (ioctl (FD_OUT, CONS_GETINFO, &screen_info) == -1)
240 return;
242 memset (&screen_shot, 0, sizeof (screen_shot));
243 screen_shot.xsize = screen_info.mv_csz;
244 screen_shot.ysize = screen_info.mv_rsz;
245 screen_shot.buf = g_try_malloc (screen_info.mv_csz * screen_info.mv_rsz * 2);
246 if (screen_shot.buf != NULL)
247 console_flag = 1;
250 static void
251 set_attr (unsigned attr)
254 * Convert color indices returned by SCRSHOT (red=4, green=2, blue=1)
255 * to indices for ANSI sequences (red=1, green=2, blue=4).
257 static const int color_map[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
258 int bc, tc;
260 tc = attr & 0xF;
261 bc = (attr >> 4) & 0xF;
263 printf ("\x1B[%d;%d;3%d;4%dm", (bc & 8) ? 5 : 25, (tc & 8) ? 1 : 22,
264 color_map[tc & 7], color_map[bc & 7]);
267 #define cursor_to(x, y) do { \
268 printf("\x1B[%d;%df", (y) + 1, (x) + 1); \
269 fflush(stdout); \
270 } while (0)
272 static void
273 console_restore (void)
275 int i, last;
277 if (!console_flag)
278 return;
280 cursor_to (0, 0);
282 /* restoring all content up to cursor position */
283 last = screen_info.mv_row * screen_info.mv_csz + screen_info.mv_col;
284 for (i = 0; i < last; ++i)
286 set_attr ((screen_shot.buf[i] >> 8) & 0xFF);
287 putc (screen_shot.buf[i] & 0xFF, stdout);
290 /* restoring cursor color */
291 set_attr ((screen_shot.buf[last] >> 8) & 0xFF);
293 fflush (stdout);
296 static void
297 console_shutdown (void)
299 if (!console_flag)
300 return;
302 g_free (screen_shot.buf);
304 console_flag = 0;
307 static void
308 console_save (void)
310 int i;
311 scrmap_t map;
312 scrmap_t revmap;
314 if (!console_flag)
315 return;
317 /* screen_info.size is already set in console_init() */
318 if (ioctl (FD_OUT, CONS_GETINFO, &screen_info) == -1)
320 console_shutdown ();
321 return;
324 /* handle console resize */
325 if (screen_info.mv_csz != screen_shot.xsize || screen_info.mv_rsz != screen_shot.ysize)
327 console_shutdown ();
328 console_init ();
331 if (ioctl (FD_OUT, CONS_SCRSHOT, &screen_shot) == -1)
333 console_shutdown ();
334 return;
337 if (ioctl (FD_OUT, GIO_SCRNMAP, &map) == -1)
339 console_shutdown ();
340 return;
343 for (i = 0; i < 256; i++)
345 char *p = memchr (map.scrmap, i, 256);
346 revmap.scrmap[i] = p ? p - map.scrmap : i;
349 for (i = 0; i < screen_shot.xsize * screen_shot.ysize; i++)
351 screen_shot.buf[i] =
352 (screen_shot.buf[i] & 0xff00) | (unsigned char) revmap.
353 scrmap[screen_shot.buf[i] & 0xff];
357 static void
358 show_console_contents_freebsd (int starty, unsigned char begin_line, unsigned char end_line)
360 int col, line;
361 char c;
363 if (!console_flag)
364 return;
366 for (line = begin_line; line <= end_line; line++)
368 tty_gotoyx (starty + line - begin_line, 0);
369 for (col = 0; col < min (COLS, screen_info.mv_csz); col++)
371 c = screen_shot.buf[line * screen_info.mv_csz + col] & 0xFF;
372 tty_print_char (c);
377 static void
378 handle_console_freebsd (unsigned char action)
380 switch (action)
382 case CONSOLE_INIT:
383 console_init ();
384 break;
386 case CONSOLE_DONE:
387 console_shutdown ();
388 break;
390 case CONSOLE_SAVE:
391 console_save ();
392 break;
394 case CONSOLE_RESTORE:
395 console_restore ();
396 break;
399 #endif /* __FreeBSD__ */
401 void
402 show_console_contents (int starty, unsigned char begin_line, unsigned char end_line)
404 tty_set_normal_attrs ();
406 if (look_for_rxvt_extensions ())
408 show_rxvt_contents (starty, begin_line, end_line);
409 return;
411 #ifdef __linux__
412 show_console_contents_linux (starty, begin_line, end_line);
413 #elif defined (__FreeBSD__)
414 show_console_contents_freebsd (starty, begin_line, end_line);
415 #else
416 console_flag = 0;
417 #endif
420 void
421 handle_console (unsigned char action)
423 (void) action;
425 if (look_for_rxvt_extensions ())
426 return;
428 #ifdef __linux__
429 handle_console_linux (action);
430 #elif defined (__FreeBSD__)
431 handle_console_freebsd (action);
432 #endif