Try fix of compile warnings about assigned but unused variables
[midnight-commander.git] / src / cons.handler.c
blob31b24324a33ae22e5e9b3df7b45757e2ea0dd83c
1 /*
2 Client interface for General purpose Linux console save/restore server
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2006, 2007, 2011
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
28 #include <config.h>
30 #include <stdlib.h>
31 #include <sys/wait.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <sys/types.h>
36 #ifdef __FreeBSD__
37 #include <sys/consio.h>
38 #include <sys/ioctl.h>
39 #endif
40 #include <unistd.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 ****************************************************************************/
53 #ifdef __linux__
54 int cons_saver_pid = 1;
55 #endif /* __linux__ */
57 /*** file scope macro definitions ****************************************************************/
59 #if defined(__FreeBSD__)
60 #define FD_OUT 1
61 #define cursor_to(x, y) \
62 do \
63 { \
64 printf("\x1B[%d;%df", (y) + 1, (x) + 1); \
65 fflush(stdout); \
66 } while (0)
67 #endif /* __linux__ */
69 /*** file scope type declarations ****************************************************************/
71 /*** file scope variables ************************************************************************/
73 #ifdef __linux__
74 /* The cons saver can't have a pid of 1, used to prevent bunches of
75 * #ifdef linux */
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 /* --------------------------------------------------------------------------------------------- */
86 #ifdef __linux__
87 static void
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;
92 int i;
94 /* Is tty console? */
95 if (mc_global.tty.console_flag == '\0')
96 return;
97 /* Paranoid: Is the cons.saver still running? */
98 if (cons_saver_pid < 1 || kill (cons_saver_pid, SIGCONT))
100 cons_saver_pid = 0;
101 mc_global.tty.console_flag = '\0';
102 return;
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)
111 return;
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++)
122 if ((i % COLS) == 0)
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 /* --------------------------------------------------------------------------------------------- */
134 static void
135 handle_console_linux (console_action_t action)
137 char *tty_name;
138 char *mc_conssaver;
139 int status;
141 switch (action)
143 case CONSOLE_INIT:
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';
151 break;
153 /* Get the console saver running */
154 cons_saver_pid = fork ();
155 if (cons_saver_pid < 0)
157 /* Cannot fork */
158 /* Delete pipes */
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)
167 /* Parent */
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')
175 pid_t ret;
176 status = close (pipefd1[1]);
177 status = close (pipefd2[0]);
178 ret = waitpid (cons_saver_pid, &status, 0);
181 else
183 /* Child */
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)
192 break;
193 status = close (pipefd1[0]);
194 /* Bind the pipe 1 to the standard output */
195 if (dup2 (pipefd2[1], 1) == -1)
196 break;
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)
202 break;
203 status = close (status);
204 if (tty_name)
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 */
212 while (0);
213 mc_global.tty.console_flag = '\0';
214 status = write (1, &mc_global.tty.console_flag, 1);
215 _exit (3);
216 } /* if (cons_saver_pid ...) */
217 break;
219 case CONSOLE_DONE:
220 case CONSOLE_SAVE:
221 case CONSOLE_RESTORE:
222 /* Is tty console? */
223 if (mc_global.tty.console_flag == '\0')
224 return;
225 /* Paranoid: Is the cons.saver still running? */
226 if (cons_saver_pid < 1 || kill (cons_saver_pid, SIGCONT))
228 cons_saver_pid = 0;
229 mc_global.tty.console_flag = '\0';
230 return;
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 */
242 close (pipefd1[1]);
243 close (pipefd2[0]);
244 (void) waitpid (cons_saver_pid, &status, 0);
245 mc_global.tty.console_flag = '\0';
247 break;
248 default:
249 break;
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>
261 static void
262 console_init (void)
264 if (mc_global.tty.console_flag != '\0')
265 return;
267 screen_info.size = sizeof (screen_info);
268 if (ioctl (FD_OUT, CONS_GETINFO, &screen_info) == -1)
269 return;
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 /* --------------------------------------------------------------------------------------------- */
281 static void
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 };
289 int bc, tc;
291 tc = attr & 0xF;
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 /* --------------------------------------------------------------------------------------------- */
300 static void
301 console_restore (void)
303 int i, last;
305 if (mc_global.tty.console_flag == '\0')
306 return;
308 cursor_to (0, 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);
321 fflush (stdout);
324 /* --------------------------------------------------------------------------------------------- */
326 static void
327 console_shutdown (void)
329 if (mc_global.tty.console_flag == '\0')
330 return;
332 g_free (screen_shot.buf);
334 mc_global.tty.console_flag = '\0';
337 /* --------------------------------------------------------------------------------------------- */
339 static void
340 console_save (void)
342 int i;
343 scrmap_t map;
344 scrmap_t revmap;
346 if (mc_global.tty.console_flag == '\0')
347 return;
349 /* screen_info.size is already set in console_init() */
350 if (ioctl (FD_OUT, CONS_GETINFO, &screen_info) == -1)
352 console_shutdown ();
353 return;
356 /* handle console resize */
357 if (screen_info.mv_csz != screen_shot.xsize || screen_info.mv_rsz != screen_shot.ysize)
359 console_shutdown ();
360 console_init ();
363 if (ioctl (FD_OUT, CONS_SCRSHOT, &screen_shot) == -1)
365 console_shutdown ();
366 return;
369 if (ioctl (FD_OUT, GIO_SCRNMAP, &map) == -1)
371 console_shutdown ();
372 return;
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++)
383 screen_shot.buf[i] =
384 (screen_shot.buf[i] & 0xff00) | (unsigned char) revmap.
385 scrmap[screen_shot.buf[i] & 0xff];
389 /* --------------------------------------------------------------------------------------------- */
391 static void
392 show_console_contents_freebsd (int starty, unsigned char begin_line, unsigned char end_line)
394 int col, line;
395 char c;
397 if (mc_global.tty.console_flag == '\0')
398 return;
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;
406 tty_print_char (c);
411 /* --------------------------------------------------------------------------------------------- */
413 static void
414 handle_console_freebsd (console_action_t action)
416 switch (action)
418 case CONSOLE_INIT:
419 console_init ();
420 break;
422 case CONSOLE_DONE:
423 console_shutdown ();
424 break;
426 case CONSOLE_SAVE:
427 console_save ();
428 break;
430 case CONSOLE_RESTORE:
431 console_restore ();
432 break;
433 default:
434 break;
437 #endif /* __FreeBSD__ */
439 /* --------------------------------------------------------------------------------------------- */
440 /*** public functions ****************************************************************************/
441 /* --------------------------------------------------------------------------------------------- */
443 void
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);
451 return;
453 #ifdef __linux__
454 show_console_contents_linux (starty, begin_line, end_line);
455 #elif defined (__FreeBSD__)
456 show_console_contents_freebsd (starty, begin_line, end_line);
457 #else
458 mc_global.tty.console_flag = '\0';
459 #endif
462 /* --------------------------------------------------------------------------------------------- */
464 void
465 handle_console (console_action_t action)
467 (void) action;
469 if (look_for_rxvt_extensions ())
470 return;
472 #ifdef __linux__
473 handle_console_linux (action);
474 #elif defined (__FreeBSD__)
475 handle_console_freebsd (action);
476 #endif
479 /* --------------------------------------------------------------------------------------------- */