Skip to content

Commit c7bbcc7

Browse files
authored
Merge pull request #72 from Bennctu/vt_indep
Decouple Linux VT
2 parents 182ebcf + f8e868b commit c7bbcc7

File tree

2 files changed

+71
-51
lines changed

2 files changed

+71
-51
lines changed

backend/fbdev.c

+3-51
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77
#include <fcntl.h>
88
#include <linux/fb.h>
9-
#include <linux/kd.h>
10-
#include <linux/vt.h>
119
#include <stdlib.h>
1210
#include <sys/ioctl.h>
1311
#include <sys/mman.h>
1412
#include <twin.h>
1513
#include <unistd.h>
1614

1715
#include "linux_input.h"
16+
#include "linux_vt.h"
1817
#include "twin_backend.h"
1918
#include "twin_private.h"
2019

@@ -31,8 +30,6 @@ typedef struct {
3130

3231
/* Linux virtual terminal (VT) */
3332
int vt_fd;
34-
int vt_num;
35-
bool vt_active;
3633

3734
/* Linux framebuffer */
3835
int fb_fd;
@@ -139,51 +136,6 @@ static bool twin_fbdev_apply_config(twin_fbdev_t *tx)
139136
return true;
140137
}
141138

142-
static int twin_vt_open(int vt_num)
143-
{
144-
int fd;
145-
146-
char vt_dev[30] = {0};
147-
snprintf(vt_dev, 30, "/dev/tty%d", vt_num);
148-
149-
fd = open(vt_dev, O_RDWR);
150-
if (fd < 0) {
151-
log_error("Failed to open %s", vt_dev);
152-
}
153-
154-
return fd;
155-
}
156-
157-
static bool twin_vt_setup(twin_fbdev_t *tx)
158-
{
159-
/* Open VT0 to inquire information */
160-
if ((tx->vt_fd = twin_vt_open(0)) < -1) {
161-
log_error("Failed to open VT0");
162-
return false;
163-
}
164-
165-
/* Inquire for current VT number */
166-
struct vt_stat vt;
167-
if (ioctl(tx->vt_fd, VT_GETSTATE, &vt) == -1) {
168-
log_error("Failed to get VT number");
169-
return false;
170-
}
171-
tx->vt_num = vt.v_active;
172-
173-
/* Open the VT */
174-
if ((tx->vt_fd = twin_vt_open(tx->vt_num)) < -1) {
175-
return false;
176-
}
177-
178-
/* Set VT to graphics mode to inhibit command-line text */
179-
if (ioctl(tx->vt_fd, KDSETMODE, KD_GRAPHICS) < 0) {
180-
log_error("Failed to set KD_GRAPHICS mode");
181-
return false;
182-
}
183-
184-
return true;
185-
}
186-
187139
twin_context_t *twin_fbdev_init(int width, int height)
188140
{
189141
char *fbdev_path = getenv(FBDEV_NAME);
@@ -210,7 +162,7 @@ twin_context_t *twin_fbdev_init(int width, int height)
210162
}
211163

212164
/* Set up virtual terminal environment */
213-
if (!twin_vt_setup(tx)) {
165+
if (!twin_vt_setup(&tx->vt_fd)) {
214166
goto bail_fb_fd;
215167
}
216168

@@ -262,7 +214,7 @@ static void twin_fbdev_exit(twin_context_t *ctx)
262214
return;
263215

264216
twin_fbdev_t *tx = PRIV(ctx);
265-
ioctl(tx->vt_fd, KDSETMODE, KD_TEXT);
217+
twin_vt_mode(tx->vt_fd, KD_TEXT);
266218
munmap(tx->fb_base, tx->fb_len);
267219
twin_linux_input_destroy(tx->input);
268220
close(tx->vt_fd);

backend/linux_vt.h

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Twin - A Tiny Window System
3+
* Copyright (c) 2024 National Cheng Kung University, Taiwan
4+
* All rights reserved.
5+
*/
6+
7+
#ifndef _LINUX_VT_H__
8+
#define _LINUX_VT_H__
9+
10+
#include <fcntl.h>
11+
#include <linux/kd.h>
12+
#include <linux/vt.h>
13+
#include <stdlib.h>
14+
#include <sys/ioctl.h>
15+
#include "twin_private.h"
16+
17+
#define VT_DEV_TTY_MAX 11
18+
static inline int twin_vt_open(int vt_num)
19+
{
20+
int fd;
21+
22+
char vt_dev[VT_DEV_TTY_MAX] = {0};
23+
snprintf(vt_dev, VT_DEV_TTY_MAX, "/dev/tty%d", vt_num);
24+
25+
fd = open(vt_dev, O_RDWR);
26+
if (fd < 0) {
27+
log_error("Failed to open %s", vt_dev);
28+
}
29+
30+
return fd;
31+
}
32+
33+
static inline int twin_vt_mode(int fd, int mode)
34+
{
35+
return ioctl(fd, KDSETMODE, mode);
36+
}
37+
38+
static inline bool twin_vt_setup(int *fd_ptr)
39+
{
40+
/* Open VT0 to inquire information */
41+
if ((*fd_ptr = twin_vt_open(0)) < -1) {
42+
log_error("Failed to open VT0");
43+
return false;
44+
}
45+
46+
/* Inquire for current VT number */
47+
struct vt_stat vt;
48+
if (ioctl(*fd_ptr, VT_GETSTATE, &vt) == -1) {
49+
log_error("Failed to get VT number");
50+
return false;
51+
}
52+
53+
int vt_num = vt.v_active;
54+
55+
/* Open the VT */
56+
if ((*fd_ptr = twin_vt_open(vt_num)) < -1) {
57+
return false;
58+
}
59+
60+
/* Set VT to graphics mode to inhibit command-line text */
61+
if (twin_vt_mode(*fd_ptr, KD_GRAPHICS) < 0) {
62+
log_error("Failed to set KD_GRAPHICS mode");
63+
return false;
64+
}
65+
66+
return true;
67+
}
68+
#endif

0 commit comments

Comments
 (0)