4 Set and restore VC screen for vlock, the VT locking program for linux.
6 Copyright (C) 1997-1998 Juan Cespedes <cespedes@debian.org>
7 Copyright (C) 2002, 2004, 2006 Dmitry V. Levin <ldv@altlinux.org>
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.
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.
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
33 static unsigned char lines, columns;
34 static void *screen_buf = 0;
37 void init_screen(void)
40 const char clear_str[] = "\33[3J\33[H\33[J";
48 if (fstat(STDIN_FILENO, &st) == -1)
51 if (!S_ISCHR(st.st_mode))
54 if ((st.st_rdev >> 8) != 4)
57 i = st.st_rdev & 0xff;
58 sprintf(path, "/dev/vcsa%u", i);
59 vcs = open(path, O_RDWR);
63 if (read(vcs, &columns, 1) != 1)
66 if (read(vcs, &lines, 1) != 1)
69 size = 2 * lines * columns + 2;
70 screen_buf = malloc(size);
74 if (read(vcs, screen_buf, size) != (int)size) {
83 if (failed && vcs >= 0) {
88 /* There's no need to use ncurses */
89 if (write(STDOUT_FILENO, clear_str, sizeof(clear_str) - 1) !=
90 sizeof(clear_str) - 1)
94 void restore_screen(void)
98 if (lseek(vcs, 0, SEEK_SET))
100 if (write(vcs, &columns, 1) != 1)
102 if (write(vcs, &lines, 1) != 1)
104 if (write(vcs, screen_buf, 2 * lines * columns + 2) !=
105 2 * lines * columns + 2)