-
Notifications
You must be signed in to change notification settings - Fork 1
/
vt.c
93 lines (80 loc) · 1.63 KB
/
vt.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
/*
* vt.c
*
* vt switch handling
*/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <linux/vt.h>
/*
* whether the terminal is switched out or not
*/
volatile int vt_suspend;
/*
* terminal needs redrawing
*/
volatile int vt_redraw;
/*
* called when the terminal switches in (1) or out (0)
*/
void *switcherdata;
void (*switcher)(int, void *);
void noswitcher(int inout, void *data) {
(void) inout;
(void) data;
}
/*
* signal handler: leave the virtual terminal
*/
void sigusr1(int s) {
(void) s;
switcher(0, switcherdata);
ioctl(STDIN_FILENO, VT_RELDISP, 1);
vt_suspend = 1;
}
/*
* signal handler: enter the virtual terminal
*/
void sigusr2(int s) {
(void) s;
switcher(1, switcherdata);
ioctl(STDIN_FILENO, VT_RELDISP, VT_ACKACQ);
vt_suspend = 0;
vt_redraw = 1;
}
/*
* setup virtual terminal for suspend and resume
*/
void vt_setup(void (*switcherfunction)(int, void *), void *data) {
struct vt_mode vtmode;
int ttyfd;
int res;
switcher = switcherfunction ? switcherfunction : noswitcher;
switcherdata = data;
vt_suspend = 0;
vt_redraw = 0;
signal(SIGUSR1, sigusr1);
signal(SIGUSR2, sigusr2);
ttyfd = STDIN_FILENO;
res = ioctl(ttyfd, VT_GETMODE, &vtmode);
if (res == -1) {
perror("VT_GETMODE");
return;
}
vtmode.mode = VT_PROCESS;
vtmode.relsig = SIGUSR1;
vtmode.acqsig = SIGUSR2;
res = ioctl(ttyfd, VT_SETMODE, &vtmode);
if (res == -1) {
perror("VT_SETMODE");
return;
}
// tell the kernel not to restore the text on page
// uncomment when program is finished
// res = ioctl(tty, KDSETMODE, KD_GRAPHICS);
// check res
}