diff --git a/backend/fbdev.c b/backend/fbdev.c index d7fb08e..210fa7b 100644 --- a/backend/fbdev.c +++ b/backend/fbdev.c @@ -6,8 +6,6 @@ #include #include -#include -#include #include #include #include @@ -15,6 +13,7 @@ #include #include "linux_input.h" +#include "linux_vt.h" #include "twin_backend.h" #include "twin_private.h" @@ -31,8 +30,6 @@ typedef struct { /* Linux virtual terminal (VT) */ int vt_fd; - int vt_num; - bool vt_active; /* Linux framebuffer */ int fb_fd; @@ -139,51 +136,6 @@ static bool twin_fbdev_apply_config(twin_fbdev_t *tx) return true; } -static int twin_vt_open(int vt_num) -{ - int fd; - - char vt_dev[30] = {0}; - snprintf(vt_dev, 30, "/dev/tty%d", vt_num); - - fd = open(vt_dev, O_RDWR); - if (fd < 0) { - log_error("Failed to open %s", vt_dev); - } - - return fd; -} - -static bool twin_vt_setup(twin_fbdev_t *tx) -{ - /* Open VT0 to inquire information */ - if ((tx->vt_fd = twin_vt_open(0)) < -1) { - log_error("Failed to open VT0"); - return false; - } - - /* Inquire for current VT number */ - struct vt_stat vt; - if (ioctl(tx->vt_fd, VT_GETSTATE, &vt) == -1) { - log_error("Failed to get VT number"); - return false; - } - tx->vt_num = vt.v_active; - - /* Open the VT */ - if ((tx->vt_fd = twin_vt_open(tx->vt_num)) < -1) { - return false; - } - - /* Set VT to graphics mode to inhibit command-line text */ - if (ioctl(tx->vt_fd, KDSETMODE, KD_GRAPHICS) < 0) { - log_error("Failed to set KD_GRAPHICS mode"); - return false; - } - - return true; -} - twin_context_t *twin_fbdev_init(int width, int height) { char *fbdev_path = getenv(FBDEV_NAME); @@ -210,7 +162,7 @@ twin_context_t *twin_fbdev_init(int width, int height) } /* Set up virtual terminal environment */ - if (!twin_vt_setup(tx)) { + if (!twin_vt_setup(&tx->vt_fd)) { goto bail_fb_fd; } @@ -262,7 +214,7 @@ static void twin_fbdev_exit(twin_context_t *ctx) return; twin_fbdev_t *tx = PRIV(ctx); - ioctl(tx->vt_fd, KDSETMODE, KD_TEXT); + twin_vt_mode(tx->vt_fd, KD_TEXT); munmap(tx->fb_base, tx->fb_len); twin_linux_input_destroy(tx->input); close(tx->vt_fd); diff --git a/backend/linux_vt.h b/backend/linux_vt.h new file mode 100644 index 0000000..6e6901d --- /dev/null +++ b/backend/linux_vt.h @@ -0,0 +1,68 @@ +/* + * Twin - A Tiny Window System + * Copyright (c) 2024 National Cheng Kung University, Taiwan + * All rights reserved. + */ + +#ifndef _LINUX_VT_H__ +#define _LINUX_VT_H__ + +#include +#include +#include +#include +#include +#include "twin_private.h" + +#define VT_DEV_TTY_MAX 11 +static inline int twin_vt_open(int vt_num) +{ + int fd; + + char vt_dev[VT_DEV_TTY_MAX] = {0}; + snprintf(vt_dev, VT_DEV_TTY_MAX, "/dev/tty%d", vt_num); + + fd = open(vt_dev, O_RDWR); + if (fd < 0) { + log_error("Failed to open %s", vt_dev); + } + + return fd; +} + +static inline int twin_vt_mode(int fd, int mode) +{ + return ioctl(fd, KDSETMODE, mode); +} + +static inline bool twin_vt_setup(int *fd_ptr) +{ + /* Open VT0 to inquire information */ + if ((*fd_ptr = twin_vt_open(0)) < -1) { + log_error("Failed to open VT0"); + return false; + } + + /* Inquire for current VT number */ + struct vt_stat vt; + if (ioctl(*fd_ptr, VT_GETSTATE, &vt) == -1) { + log_error("Failed to get VT number"); + return false; + } + + int vt_num = vt.v_active; + + /* Open the VT */ + if ((*fd_ptr = twin_vt_open(vt_num)) < -1) { + return false; + } + + /* Set VT to graphics mode to inhibit command-line text */ + if (twin_vt_mode(*fd_ptr, KD_GRAPHICS) < 0) { + log_error("Failed to set KD_GRAPHICS mode"); + return false; + } + + return true; +} +#endif