-
Notifications
You must be signed in to change notification settings - Fork 3
/
psplash-console.c
190 lines (151 loc) · 4.15 KB
/
psplash-console.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* pslash - a lightweight framebuffer splashscreen for embedded devices.
*
* Copyright (c) 2006 Matthew Allum <mallum@o-hand.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "psplash.h"
/* Globals, needed for signal handling */
static int ConsoleFd = -1;
static int VTNum = -1;
static int VTNumInitial = -1;
static int Visible = 1;
static void vt_request(int UNUSED(sig))
{
ulog(LOG_DEBUG, "mark, visible:%i\n", Visible);
if (Visible)
{
/* Allow Switch Away */
if (ioctl(ConsoleFd, VT_RELDISP, 1) < 0)
perror("Error cannot switch away from console");
Visible = 0;
/* FIXME:
* We likely now want to signal the main loop as to exit
* and we've now likely switched to the X tty. Note, this
* seems to happen anyway atm due to select() call getting
* a signal interuption error - not sure if this is really
* reliable however.
*/
}
else
{
if (ioctl(ConsoleFd, VT_RELDISP, VT_ACKACQ))
perror("Error can't acknowledge VT switch");
Visible = 1;
/* FIXME: need to schedule repaint some how ? */
}
}
static void psplash_console_ignore_switches(void)
{
struct sigaction act;
struct vt_mode vt_mode;
if (ioctl(ConsoleFd, VT_GETMODE, &vt_mode) < 0)
{
perror("Error VT_SETMODE failed");
return;
}
act.sa_handler = SIG_IGN;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGUSR1, &act, 0);
vt_mode.mode = VT_AUTO;
vt_mode.relsig = 0;
vt_mode.acqsig = 0;
if (ioctl(ConsoleFd, VT_SETMODE, &vt_mode) < 0)
perror("Error VT_SETMODE failed");
}
static void psplash_console_handle_switches(void)
{
struct sigaction act;
struct vt_mode vt_mode;
if (ioctl(ConsoleFd, VT_GETMODE, &vt_mode) < 0)
{
perror("Error VT_SETMODE failed");
return;
}
act.sa_handler = vt_request;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGUSR1, &act, 0);
vt_mode.mode = VT_PROCESS;
vt_mode.relsig = SIGUSR1;
vt_mode.acqsig = SIGUSR1;
if (ioctl(ConsoleFd, VT_SETMODE, &vt_mode) < 0)
perror("Error VT_SETMODE failed");
}
void psplash_console_switch(void)
{
char vtname[10];
int fd;
struct vt_stat vt_state;
if ((fd = open("/dev/tty0", O_WRONLY, 0)) < 0)
{
perror("Error Cannot open /dev/tty0");
return;
}
/* Find next free terminal */
if ((ioctl(fd, VT_OPENQRY, &VTNum) < 0))
{
perror("Error unable to find a free virtual terminal");
close(fd);
return;
}
close(fd);
sprintf(vtname, "/dev/tty%d", VTNum);
if ((ConsoleFd = open(vtname, O_RDWR | O_NDELAY, 0)) < 0)
{
fprintf(stderr, "Error cannot open %s: %s\n", vtname, strerror(errno));
return;
}
if (ioctl(ConsoleFd, VT_GETSTATE, &vt_state) == 0)
VTNumInitial = vt_state.v_active;
/* Switch to new free terminal */
psplash_console_ignore_switches();
if (ioctl(ConsoleFd, VT_ACTIVATE, VTNum) != 0)
perror("Error VT_ACTIVATE failed");
if (ioctl(ConsoleFd, VT_WAITACTIVE, VTNum) != 0)
perror("Error VT_WAITACTIVE failed\n");
psplash_console_handle_switches();
if (ioctl(ConsoleFd, KDSETMODE, KD_GRAPHICS) < 0)
perror("Error KDSETMODE KD_GRAPHICS failed\n");
return;
}
void psplash_console_reset(void)
{
int fd;
struct vt_stat vt_state;
if (ConsoleFd < 0)
return;
/* Back to text mode */
ioctl(ConsoleFd, KDSETMODE, KD_TEXT);
psplash_console_ignore_switches();
/* Attempt to switch back to initial console if were still active */
ioctl(ConsoleFd, VT_GETSTATE, &vt_state);
if (VTNum == vt_state.v_active)
{
if (VTNumInitial > -1)
{
ioctl(ConsoleFd, VT_ACTIVATE, VTNumInitial);
ioctl(ConsoleFd, VT_WAITACTIVE, VTNumInitial);
VTNumInitial = -1;
}
}
/* Cleanup */
close(ConsoleFd);
if ((fd = open("/dev/tty0", O_RDWR | O_NDELAY, 0)) >= 0)
{
ioctl(fd, VT_DISALLOCATE, VTNum);
close(fd);
}
return;
}