pepper: implement virtual terminal setup/restore
authorJunghoon <jh13.son@samsung.com>
Tue, 23 Jun 2015 12:12:59 +0000 (21:12 +0900)
committerTaekyun Kim <tkq.kim@samsung.com>
Tue, 7 Jul 2015 06:56:51 +0000 (15:56 +0900)
Change-Id: I7c84d04d8f2ba708fa47ae79a4a5515c042de9a3

pepper/src/Makefile.am
pepper/src/pepper-utils.h
pepper/src/utils-vt.c [new file with mode: 0644]
samples/src/drm-backend.c
samples/src/fbdev-backend.c

index d6385bf..32f3cd4 100644 (file)
@@ -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
index 94a22ef..df435f8 100644 (file)
@@ -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 (file)
index 0000000..1ad930c
--- /dev/null
@@ -0,0 +1,117 @@
+#include <stdio.h>
+#include <signal.h>
+#include <unistd.h>
+#include <linux/kd.h>
+#include <linux/vt.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#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;
+}
index 513f133..edf51aa 100644 (file)
@@ -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;
 }
index 192c9c2..6b0480d 100644 (file)
@@ -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;
 }