Merge branch 'master' into loadkeys
[platform/upstream/kbd.git] / src / vlock / screen.c
1
2 /*
3
4   Set and restore VC screen for vlock, the VT locking program for linux.
5
6   Copyright (C) 1997-1998  Juan Cespedes <cespedes@debian.org>
7   Copyright (C) 2002, 2004, 2006  Dmitry V. Levin <ldv@altlinux.org>
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13
14   This program 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.
18
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "vlock.h"
31
32 static unsigned char lines, columns;
33 static void *screen_buf = 0;
34 static int vcs = -1;
35
36 void
37 init_screen(void)
38 {
39         int     failed = 1;
40         const char clear_str[] = "\33[3J\33[H\33[J";
41
42         vcs = -1;
43         do
44         {
45                 struct stat st;
46                 unsigned i, size;
47                 char    path[16];
48
49                 if (fstat(STDIN_FILENO, &st) == -1)
50                         break;
51
52                 if (!S_ISCHR(st.st_mode))
53                         break;
54
55                 if ((st.st_rdev >> 8) != 4)
56                         break;
57
58                 i = st.st_rdev & 0xff;
59                 sprintf(path, "/dev/vcsa%u", i);
60                 vcs = open(path, O_RDWR);
61                 if (vcs < 0)
62                         break;
63
64                 if (read(vcs, &columns, 1) != 1)
65                         break;
66
67                 if (read(vcs, &lines, 1) != 1)
68                         break;
69
70                 size = 2 * lines * columns + 2;
71                 screen_buf = malloc(size);
72                 if (!screen_buf)
73                         break;
74
75                 if (read(vcs, screen_buf, size) != (int) size)
76                 {
77                         free(screen_buf);
78                         screen_buf = NULL;
79                         break;
80                 }
81
82                 failed = 0;
83         } while (0);
84
85         if (failed && vcs >= 0)
86         {
87                 close(vcs);
88                 vcs = -1;
89         }
90
91         /* There's no need to use ncurses */
92         if (write(STDOUT_FILENO, clear_str, sizeof(clear_str) - 1) !=
93             sizeof(clear_str) - 1)
94                 return;
95 }
96
97 void
98 restore_screen(void)
99 {
100         if (screen_buf)
101         {
102                 do
103                 {
104                         if (lseek(vcs, 0, SEEK_SET))
105                                 break;
106                         if (write(vcs, &columns, 1) != 1)
107                                 break;
108                         if (write(vcs, &lines, 1) != 1)
109                                 break;
110                         if (write(vcs, screen_buf, 2 * lines * columns + 2) !=
111                             2 * lines * columns + 2)
112                                 break;
113                 } while (0);
114                 close(vcs);
115         }
116 }