{
// create a new console
int fd= open(TTYDEV, O_RDWR | O_SYNC);
if (fd<0) {
loge("Can't open %s", TTYDEV);
return -1;
}
return fd;
}
int release_display(char *tty)
{
err = ioctl(fd, VT_RELDISP, (void*)1);
if( err < 0)
loge("ioctl(%d, VT_RELDISP, 1) failed %d \n",fd, errno);
// to make sure that we are in text mode
err = ioctl(fd, KDSETMODE, (void*) KD_TEXT);
if (err<0) {
loge("ioctl(%d, KDSETMODE, ...) failed, err %d \n", fd, err);
}
struct vt_stat vs;
err = ioctl(fd, VT_GETSTATE, &vs);
if (err<0) {
loge("ioctl(%d, VT_GETSTATE, ...) failed, err %d\n", fd, err );
}
close(fd);
return 0;
}
int acquire_display()
{
int fd = 0;
int err ;
int vtnum = 7;
// switch to console 7 (which is what X normaly uses)
loge("switch to graphics mode\n");
do {
err = ioctl(fd, VT_ACTIVATE, (void*)vtnum);
} while(err < 0 && errno == EINTR);
if (err<0) {
loge("ioctl VT_ACTIVATE failed, err= %d\n", errno);
}
do {
err = ioctl(fd, VT_WAITACTIVE, (void*)vtnum);
} while(err < 0 && errno == EINTR);
if (err<0) {
loge("ioctl(%d, VT_WAITACTIVE, ...) failed, %d %d for %d\n",
fd, err, errno, vtnum);
}
// open the new console
close(fd);
fd = open_tty();
//reopen, since tty has changed
// switch to graphic mode
err = ioctl(fd, KDSETMODE, (void*)KD_GRAPHICS);
if( err < 0 )
loge("ioctl(%d, KDSETMODE, KD_GRAPHICS) failed, err %d\n", fd, err);
// set up signals so we're notified when the console changes
// we can't use SIGUSR1 because it's used by the java-vm
struct vt_mode vm;
vm.mode = VT_PROCESS;
vm.waitv = 0;
vm.relsig = SIGREL;
vm.acqsig = SIGACQ;
vm.frsig = 0;
err = ioctl(fd, VT_SETMODE, &vm);
if (err<0) {
loge("ioctl(%d, VT_SETMODE, ...) failed, %d\n \
can't switch to VT_MODE\n", fd, errno);
}
// get the current console
struct vt_stat vs;
err = ioctl(fd, VT_GETSTATE, &vs);
if (err<0) {
loge("ioctl(%d, VT_GETSTATE, ...) failed, err %d\n", fd, err );
}
}
切换到图形模式:
ioctl(fd, KDSETMODE, KD_GRAPHICS)
当前VT设置为KD_GRAPHICS模式,禁止显示字符
ioctl(fd, VT_SETMODE, &vm);
当前VT设置为VT_PROCESS模式,设置切换发生的信号名称。
控制台的切换是否有效由程序来控制,
设置为文本模式:
ioctl(fd, VT_RELDISP, (void*)1);
在VT_PROCESS模式下,发送VT_RELDISP命令才能完成VT切换.
VT_PROCESS 下程序接收到信号后判断是否需要真的切换VT,否则可以忽略来自内核的信号,继续停在图形模式。
ioctl(fd, KDSETMODE, KD_TEXT)
切换到文本模式