From 376d1f046cc6d6f5aad0a7d7a555dbc96cb657b0 Mon Sep 17 00:00:00 2001 From: Junghoon Date: Tue, 23 Jun 2015 21:12:59 +0900 Subject: [PATCH] pepper: implement virtual terminal setup/restore Change-Id: I7c84d04d8f2ba708fa47ae79a4a5515c042de9a3 --- pepper/src/Makefile.am | 3 +- pepper/src/pepper-utils.h | 7 +++ pepper/src/utils-vt.c | 117 ++++++++++++++++++++++++++++++++++++++++++++ samples/src/drm-backend.c | 5 ++ samples/src/fbdev-backend.c | 5 ++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 pepper/src/utils-vt.c diff --git a/pepper/src/Makefile.am b/pepper/src/Makefile.am index d6385bf..32f3cd4 100644 --- a/pepper/src/Makefile.am +++ b/pepper/src/Makefile.am @@ -22,4 +22,5 @@ libpepper_la_SOURCES = pepper.h \ view.c \ utils-file.c \ utils-map.c \ - utils-log.c + utils-log.c \ + utils-vt.c diff --git a/pepper/src/pepper-utils.h b/pepper/src/pepper-utils.h index 94a22ef..df435f8 100644 --- a/pepper/src/pepper-utils.h +++ b/pepper/src/pepper-utils.h @@ -439,6 +439,13 @@ pepper_matrix_copy(pepper_matrix_t *dst, const pepper_matrix_t *src) memcpy(dst, src, sizeof(pepper_matrix_t)); } +/* Virtual terminal */ +PEPPER_API pepper_bool_t +pepper_virtual_terminal_setup(int tty); + +PEPPER_API void +pepper_virtual_terminal_restore(); + #ifdef __cplusplus } #endif diff --git a/pepper/src/utils-vt.c b/pepper/src/utils-vt.c new file mode 100644 index 0000000..1ad930c --- /dev/null +++ b/pepper/src/utils-vt.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "pepper-utils.h" + +#define PEPPER_ERROR(...) /* TODO */ + +static struct _vt_data +{ + int tty_fd; + int tty_num; + int saved_tty_num; + int kb_mode; +} vt_data; + +PEPPER_API void +pepper_virtual_terminal_restore() +{ + if (vt_data.tty_fd >= 0) + { + int fd = vt_data.tty_fd; + struct vt_mode mode = {0}; + + if ((vt_data.kb_mode >= 0) && (ioctl(fd, KDSKBMODE, vt_data.kb_mode) < 0)) + PEPPER_ERROR(""); + + if (ioctl(fd, KDSETMODE, KD_TEXT) < 0) + PEPPER_ERROR(""); + + mode.mode = VT_AUTO; + if (ioctl(fd, VT_SETMODE, &mode) < 0) + PEPPER_ERROR(""); + + if ((vt_data.saved_tty_num > 0) && (ioctl(fd, VT_ACTIVATE, vt_data.saved_tty_num) < 0)) + PEPPER_ERROR(""); + + close(fd); + } +} + +PEPPER_API pepper_bool_t +pepper_virtual_terminal_setup(int tty) +{ + int fd; + + struct vt_stat stat; + struct vt_mode mode; + + memset(&vt_data, -1, sizeof(vt_data)); + + if (tty == 0) + { + fd = dup(tty); + if (fd < 0) + return PEPPER_FALSE; + + if (ioctl(fd, VT_GETSTATE, &stat) == 0) + vt_data.tty_num = vt_data.saved_tty_num = stat.v_active; + } + else + { + char tty_str[32]; + + sprintf(tty_str, "/dev/tty%d", tty); + fd = open(tty_str, O_RDWR | O_CLOEXEC); + if (fd < 0) + return PEPPER_FALSE; + + if (ioctl(fd, VT_GETSTATE, &stat) == 0) + vt_data.saved_tty_num = stat.v_active; + + vt_data.tty_num = tty; + } + + vt_data.tty_fd = fd; + + if (ioctl(fd, VT_ACTIVATE, vt_data.tty_num) < 0) + goto error; + + if (ioctl(fd, VT_WAITACTIVE, vt_data.tty_num) < 0) + goto error; + + if (ioctl(fd, KDGKBMODE, &vt_data.kb_mode) < 0) + goto error; + + if (ioctl(fd, KDSKBMODE, K_OFF) < 0) + goto error; + + if (ioctl(fd, KDSETMODE, KD_GRAPHICS) < 0) + goto error; + + if (ioctl(fd, VT_GETMODE, &mode) < 0) + goto error; + + mode.mode = VT_PROCESS; + mode.relsig = SIGUSR1; + mode.acqsig = SIGUSR1; + if (ioctl(fd, VT_SETMODE, &mode) < 0) + goto error; + + /* TODO: add signal handling code for SIGUSR1 */ + + return PEPPER_TRUE; + +error: + + pepper_virtual_terminal_restore(); + + return PEPPER_FALSE; +} diff --git a/samples/src/drm-backend.c b/samples/src/drm-backend.c index 513f133..edf51aa 100644 --- a/samples/src/drm-backend.c +++ b/samples/src/drm-backend.c @@ -35,6 +35,9 @@ main(int argc, char **argv) ret = scanf("%c", &cc); } + if (!pepper_virtual_terminal_setup(0/*FIXME*/)) + PEPPER_ASSERT(0); + compositor = pepper_compositor_create("wayland-0"); PEPPER_ASSERT(compositor); @@ -57,5 +60,7 @@ main(int argc, char **argv) pepper_drm_destroy(drm); pepper_compositor_destroy(compositor); + pepper_virtual_terminal_restore(); + return 0; } diff --git a/samples/src/fbdev-backend.c b/samples/src/fbdev-backend.c index 192c9c2..6b0480d 100644 --- a/samples/src/fbdev-backend.c +++ b/samples/src/fbdev-backend.c @@ -37,6 +37,9 @@ main(int argc, char **argv) return -1; } + if (!pepper_virtual_terminal_setup(0/*FIXME*/)) + PEPPER_ASSERT(0); + compositor = pepper_compositor_create("wayland-0"); PEPPER_ASSERT(compositor); @@ -59,5 +62,7 @@ main(int argc, char **argv) pepper_fbdev_destroy(fbdev); pepper_compositor_destroy(compositor); + pepper_virtual_terminal_restore(); + return 0; } -- 2.7.4