1f448a3e4f2cdaa226e23854425b7f6bcf10495c
[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 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30
31 #include "vlock.h"
32
33 static unsigned char lines, columns;
34 static void *screen_buf = 0;
35 static int vcs          = -1;
36
37 void 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                 struct stat st;
45                 unsigned i, size;
46                 char path[16];
47
48                 if (fstat(STDIN_FILENO, &st) == -1)
49                         break;
50
51                 if (!S_ISCHR(st.st_mode))
52                         break;
53
54                 if ((st.st_rdev >> 8) != 4)
55                         break;
56
57                 i = st.st_rdev & 0xff;
58                 sprintf(path, "/dev/vcsa%u", i);
59                 vcs = open(path, O_RDWR);
60                 if (vcs < 0)
61                         break;
62
63                 if (read(vcs, &columns, 1) != 1)
64                         break;
65
66                 if (read(vcs, &lines, 1) != 1)
67                         break;
68
69                 size       = 2 * lines * columns + 2;
70                 screen_buf = malloc(size);
71                 if (!screen_buf)
72                         break;
73
74                 if (read(vcs, screen_buf, size) != (int)size) {
75                         free(screen_buf);
76                         screen_buf = NULL;
77                         break;
78                 }
79
80                 failed = 0;
81         } while (0);
82
83         if (failed && vcs >= 0) {
84                 close(vcs);
85                 vcs = -1;
86         }
87
88         /* There's no need to use ncurses */
89         if (write(STDOUT_FILENO, clear_str, sizeof(clear_str) - 1) !=
90             sizeof(clear_str) - 1)
91                 return;
92 }
93
94 void restore_screen(void)
95 {
96         if (screen_buf) {
97                 do {
98                         if (lseek(vcs, 0, SEEK_SET))
99                                 break;
100                         if (write(vcs, &columns, 1) != 1)
101                                 break;
102                         if (write(vcs, &lines, 1) != 1)
103                                 break;
104                         if (write(vcs, screen_buf, 2 * lines * columns + 2) !=
105                             2 * lines * columns + 2)
106                                 break;
107                 } while (0);
108                 close(vcs);
109         }
110 }