Debian Codegraph @

contrib/gcc/fbmngplay/console.c

raw
1 /*
2 fbmngplay - fb console MNG player.
3 (c) 2001 by Stefan Reinauer, <stepan :at: suse.de>
4
5 This program is based on mngplay, part of libmng, written and (C) by
6 Ralph Giles <giles :at: ashlu.bc.ca>
7
8 This program my be redistributed under the terms of the
9 GNU General Public Licence, version 2, or at your preference,
10 any later version.
11
12 This file is based on getfd.c from the kbd package.
13 */
14
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <linux/kd.h>
20 #include <linux/vt.h>
21 #include <sys/ioctl.h>
22
23 #include "console.h"
24
25 int start_console = 0;
26
27 /*
28 * getfd.c
29 *
30 * Get an fd for use with kbd/console ioctls.
31 * We try several things because opening /dev/console will fail
32 * if someone else used X (which does a chown on /dev/console).
33 */
34
35 static int
36 is_a_console(const int fd) {
37 char arg;
38
39 arg = 0;
40 return (ioctl(fd, KDGKBTYPE, &arg) == 0
41 && ((arg == KB_101) || (arg == KB_84)));
42 }
43
44 static int
45 open_a_console(const char * const fnam) {
46 int fd;
47
48 fd = open(fnam, O_RDONLY);
49 if (fd < 0 && errno == EACCES)
50 fd = open(fnam, O_WRONLY);
51 if (fd < 0)
52 return -1;
53 if (!is_a_console(fd)) {
54 close(fd);
55 return -1;
56 }
57 return fd;
58 }
59
60 int getfd (const char * const nm) {
61 int fd;
62
63 if (nm) {
64 fd = open_a_console(nm);
65 if (fd >= 0)
66 return fd;
67 } else {
68 fd = open_a_console("/dev/tty");
69 if (fd >= 0)
70 return fd;
71
72 fd = open_a_console("/dev/tty0");
73 if (fd >= 0)
74 return fd;
75
76 fd = open_a_console("/dev/console");
77 if (fd >= 0)
78 return fd;
79
80 for (fd = 0; fd < 3; fd++)
81 if (is_a_console(fd))
82 return fd;
83 }
84 fprintf(stderr,
85 "Couldnt get a file descriptor referring to the console\n");
86 exit(1); /* total failure */
87 }
88
89 int fd;
90 int current_console(void)
91 {
92 struct vt_stat vtstat;
93 if (ioctl(fd, VT_GETSTATE, &vtstat)) {
94 fprintf(stderr,"fbmngplay: VT_GETSTATE\n");
95 exit(1);
96 }
97 return vtstat.v_active;
98
99 }
100
101 void init_consoles(void)
102 {
103 // get current tty
104 fd = getfd(0);
105 start_console=current_console();
106 }
107