launcher: Move rest of tty object into struct weston_launcher

The struct weston_launcher object will now either handle tty and vt switching
details in-process (when running weston directly as root) or talk to
the weston-launch process.
diff --git a/src/Makefile.am b/src/Makefile.am
index 92426b0..1ec7e88 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -144,7 +144,6 @@
 	$(GCC_CFLAGS)
 drm_backend_la_SOURCES =			\
 	compositor-drm.c			\
-	tty.c					\
 	udev-seat.c				\
 	udev-seat.h				\
 	evdev.c					\
@@ -193,7 +192,8 @@
 	rpi-renderer.c				\
 	rpi-renderer.h				\
 	rpi-bcm-stubs.h 			\
-	tty.c					\
+	launcher-util.c				\
+	launcher-util.h				\
 	evdev.c					\
 	evdev.h					\
 	evdev-touchpad.c
@@ -224,13 +224,13 @@
 	$(GCC_CFLAGS)
 fbdev_backend_la_SOURCES = \
 	compositor-fbdev.c \
-	tty.c \
 	udev-seat.c \
 	udev-seat.h \
 	evdev.c \
 	evdev.h \
 	evdev-touchpad.c \
-	launcher-util.c
+	launcher-util.c \
+	launcher-util.h
 endif
 
 if ENABLE_RDP_COMPOSITOR
diff --git a/src/compositor-drm.c b/src/compositor-drm.c
index b53cd9f..58697ed 100644
--- a/src/compositor-drm.c
+++ b/src/compositor-drm.c
@@ -30,6 +30,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <linux/input.h>
+#include <linux/vt.h>
 #include <assert.h>
 #include <sys/mman.h>
 #include <time.h>
@@ -83,7 +84,6 @@
 	int num_crtcs;
 	uint32_t crtc_allocator;
 	uint32_t connector_allocator;
-	struct tty *tty;
 	struct wl_listener session_listener;
 
 	/* we need these parameters in order to not fail drmModeAddFB2()
@@ -2240,13 +2240,7 @@
 static void
 drm_restore(struct weston_compositor *ec)
 {
-	struct drm_compositor *d = (struct drm_compositor *) ec;
-
-	if (ec->launcher == NULL) {
-		if (drmDropMaster(d->drm.fd) < 0)
-			weston_log("failed to drop master: %m\n");
-		tty_reset(d->tty);
-	}
+	weston_launcher_restore(ec->launcher);
 }
 
 static void
@@ -2268,11 +2262,7 @@
 	if (d->gbm)
 		gbm_device_destroy(d->gbm);
 
-	if (d->base.launcher == NULL) {
-		if (drmDropMaster(d->drm.fd) < 0)
-			weston_log("failed to drop master: %m\n");
-		tty_destroy(d->tty);
-	}
+	weston_launcher_destroy(d->base.launcher);
 
 	close(d->drm.fd);
 
@@ -2368,10 +2358,9 @@
 static void
 switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
 {
-	struct drm_compositor *ec = data;
+	struct weston_compositor *compositor = data;
 
-	if (ec->tty)
-		tty_activate_vt(ec->tty, key - KEY_F1 + 1);
+	weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
 }
 
 /*
@@ -2575,7 +2564,7 @@
 
 	/* Check if we run drm-backend using weston-launch */
 	ec->base.launcher = weston_launcher_connect(&ec->base);
-	if (ec->base.launcher == NULL && geteuid() != 0) {
+	if (ec->base.launcher == NULL) {
 		weston_log("fatal: drm backend should be run "
 			   "using weston-launch binary or as root\n");
 		goto err_compositor;
@@ -2584,24 +2573,17 @@
 	ec->udev = udev_new();
 	if (ec->udev == NULL) {
 		weston_log("failed to initialize udev context\n");
-		goto err_compositor;
+		goto err_launcher;
 	}
 
 	ec->base.wl_display = display;
 	ec->session_listener.notify = session_notify;
 	wl_signal_add(&ec->base.session_signal, &ec->session_listener);
-	if (ec->base.launcher == NULL) {
-		ec->tty = tty_create(&ec->base, tty);
-		if (!ec->tty) {
-			weston_log("failed to initialize tty\n");
-			goto err_udev;
-		}
-	}
 
 	drm_device = find_primary_gpu(ec, seat_id);
 	if (drm_device == NULL) {
 		weston_log("no drm device found\n");
-		goto err_tty;
+		goto err_udev;
 	}
 	path = udev_device_get_syspath(drm_device);
 
@@ -2696,11 +2678,8 @@
 	destroy_sprites(ec);
 err_udev_dev:
 	udev_device_unref(drm_device);
-err_tty:
-	if (ec->base.launcher == NULL && drmDropMaster(ec->drm.fd) < 0)
-		weston_log("failed to drop master: %m\n");
-	if (ec->tty)
-		tty_destroy(ec->tty);
+err_launcher:
+	weston_launcher_destroy(ec->base.launcher);
 err_udev:
 	udev_unref(ec->udev);
 err_compositor:
diff --git a/src/compositor-fbdev.c b/src/compositor-fbdev.c
index 9fa4217..2b48319 100644
--- a/src/compositor-fbdev.c
+++ b/src/compositor-fbdev.c
@@ -50,7 +50,6 @@
 	uint32_t prev_state;
 
 	struct udev *udev;
-	struct tty *tty;
 	struct udev_input input;
 	int use_pixman;
 	struct wl_listener session_listener;
@@ -799,7 +798,7 @@
 
 	/* Chain up. */
 	compositor->base.renderer->destroy(&compositor->base);
-	tty_destroy(compositor->tty);
+	weston_launcher_destroy(compositor->base.launcher);
 
 	free(compositor);
 }
@@ -849,19 +848,17 @@
 }
 
 static void
-fbdev_restore(struct weston_compositor *base)
+fbdev_restore(struct weston_compositor *compositor)
 {
-	struct fbdev_compositor *compositor = to_fbdev_compositor(base);
-
-	tty_reset(compositor->tty);
+	weston_launcher_restore(compositor->launcher);
 }
 
 static void
 switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
 {
-	struct fbdev_compositor *ec = data;
+	struct weston_compositor *compositor = data;
 
-	tty_activate_vt(ec->tty, key - KEY_F1 + 1);
+	weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
 }
 
 static struct weston_compositor *
@@ -901,9 +898,9 @@
 	compositor->session_listener.notify = session_notify;
 	wl_signal_add(&compositor->base.session_signal,
 		      &compositor->session_listener);
-	compositor->tty = tty_create(&compositor->base, param->tty);
-	if (!compositor->tty) {
-		weston_log("Failed to initialize tty.\n");
+	compositor->base.launcher = weston_launcher_connect(&compositor->base);
+	if (!compositor->base.launcher) {
+		weston_log("Failed to set up launcher.\n");
 		goto out_udev;
 	}
 
@@ -921,12 +918,12 @@
 		                                  compositor);
 	if (compositor->use_pixman) {
 		if (pixman_renderer_init(&compositor->base) < 0)
-			goto out_tty;
+			goto out_launcher;
 	} else {
 		if (gl_renderer_create(&compositor->base, EGL_DEFAULT_DISPLAY,
 			gl_renderer_opaque_attribs, NULL) < 0) {
 			weston_log("gl_renderer_create failed.\n");
-			goto out_tty;
+			goto out_launcher;
 		}
 	}
 
@@ -940,8 +937,8 @@
 out_pixman:
 	compositor->base.renderer->destroy(&compositor->base);
 
-out_tty:
-	tty_destroy(compositor->tty);
+out_launcher:
+	weston_launcher_destroy(compositor->base.launcher);
 
 out_udev:
 	udev_unref(compositor->udev);
diff --git a/src/compositor-rpi.c b/src/compositor-rpi.c
index ba92485..9a507f5 100644
--- a/src/compositor-rpi.c
+++ b/src/compositor-rpi.c
@@ -44,6 +44,7 @@
 #include "compositor.h"
 #include "rpi-renderer.h"
 #include "evdev.h"
+#include "launcher-util.h"
 
 #if 0
 #define DBG(...) \
@@ -86,7 +87,6 @@
 	uint32_t prev_state;
 
 	struct udev *udev;
-	struct tty *tty;
 	struct wl_listener session_listener;
 
 	int single_buffer;
@@ -656,7 +656,7 @@
 	weston_compositor_shutdown(&compositor->base);
 
 	compositor->base.renderer->destroy(&compositor->base);
-	tty_destroy(compositor->tty);
+	weston_launcher_destroy(compositor->base.launcher);
 
 	bcm_host_deinit();
 	free(compositor);
@@ -705,19 +705,17 @@
 }
 
 static void
-rpi_restore(struct weston_compositor *base)
+rpi_restore(struct weston_compositor *compositor)
 {
-	struct rpi_compositor *compositor = to_rpi_compositor(base);
-
-	tty_reset(compositor->tty);
+	weston_launcher_restore(compositor->launcher);
 }
 
 static void
 switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
 {
-	struct rpi_compositor *ec = data;
+	struct weston_compositor *compositor = data;
 
-	tty_activate_vt(ec->tty, key - KEY_F1 + 1);
+	weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
 }
 
 struct rpi_parameters {
@@ -754,8 +752,8 @@
 	compositor->session_listener.notify = session_notify;
 	wl_signal_add(&compositor->base.session_signal,
 		      &compositor ->session_listener);
-	compositor->tty = tty_create(&compositor->base, param->tty);
-	if (!compositor->tty) {
+	compositor->base.launcher = weston_launcher_connect(&compositor->base);
+	if (!compositor->base.launcher) {
 		weston_log("Failed to initialize tty.\n");
 		goto out_udev;
 	}
@@ -785,7 +783,7 @@
 	bcm_host_init();
 
 	if (rpi_renderer_create(&compositor->base, &param->renderer) < 0)
-		goto out_tty;
+		goto out_launcher;
 
 	if (rpi_output_create(compositor, param->output_transform) < 0)
 		goto out_renderer;
@@ -797,8 +795,8 @@
 out_renderer:
 	compositor->base.renderer->destroy(&compositor->base);
 
-out_tty:
-	tty_destroy(compositor->tty);
+out_launcher:
+	weston_launcher_destroy(compositor->base.launcher);
 
 out_udev:
 	udev_unref(compositor->udev);
diff --git a/src/launcher-util.c b/src/launcher-util.c
index 75e7051..a2ad0ec 100644
--- a/src/launcher-util.c
+++ b/src/launcher-util.c
@@ -28,12 +28,17 @@
 #include <string.h>
 
 #include <errno.h>
+#include <signal.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/uio.h>
+#include <sys/ioctl.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <linux/vt.h>
+#include <linux/kd.h>
+#include <linux/major.h>
 
 #include <xf86drm.h>
 
@@ -41,19 +46,28 @@
 #include "launcher-util.h"
 #include "weston-launch.h"
 
+#define DRM_MAJOR 226
+
+#ifndef KDSKBMUTE
+#define KDSKBMUTE	0x4B51
+#endif
+
 union cmsg_data { unsigned char b[4]; int fd; };
 
 struct weston_launcher {
 	struct weston_compositor *compositor;
 	int fd;
 	struct wl_event_source *source;
+
+	int kb_mode, tty, drm_fd;
+	struct wl_event_source *vt_source;
 };
 
 int
 weston_launcher_open(struct weston_launcher *launcher,
 		     const char *path, int flags)
 {
-	int n, ret = -1;
+	int n, fd, ret = -1;
 	struct msghdr msg;
 	struct cmsghdr *cmsg;
 	struct iovec iov;
@@ -61,9 +75,24 @@
 	char control[CMSG_SPACE(sizeof data->fd)];
 	ssize_t len;
 	struct weston_launcher_open *message;
+	struct stat s;
 
-	if (launcher == NULL)
-		return open(path, flags | O_CLOEXEC);
+	if (launcher->fd == -1) {
+		fd = open(path, flags | O_CLOEXEC);
+
+		if (fd == -1)
+			return -1;
+
+		if (fstat(fd, &s) == -1) {
+			close(fd);
+			return -1;
+		}
+
+		if (major(s.st_rdev) == DRM_MAJOR)
+			launcher->drm_fd = fd;
+
+		return fd;
+	}
 
 	n = sizeof(*message) + strlen(path) + 1;
 	message = malloc(n);
@@ -112,6 +141,23 @@
 	return data->fd;
 }
 
+void
+weston_launcher_restore(struct weston_launcher *launcher)
+{
+	struct vt_mode mode = { 0 };
+
+	if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
+	    ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
+		weston_log("failed to restore kb mode: %m\n");
+
+	if (ioctl(launcher->tty, KDSETMODE, KD_TEXT))
+		weston_log("failed to set KD_TEXT mode on tty: %m\n");
+
+	mode.mode = VT_AUTO;
+	if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0)
+		weston_log("could not reset vt handling\n");
+}
+
 static int
 weston_launcher_data(int fd, uint32_t mask, void *data)
 {
@@ -120,6 +166,10 @@
 
 	if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
 		weston_log("launcher socket closed, exiting\n");
+		/* Normally the weston-launch will reset the tty, but
+		 * in this case it died or something, so do it here so
+		 * we don't end up with a stuck vt. */
+		weston_launcher_restore(launcher);
 		exit(-1);
 	}
 
@@ -146,30 +196,111 @@
 	return 1;
 }
 
+static int
+vt_handler(int signal_number, void *data)
+{
+	struct weston_launcher *launcher = data;
+	struct weston_compositor *compositor = launcher->compositor;
+
+	if (compositor->session_active) {
+		compositor->session_active = 0;
+		wl_signal_emit(&compositor->session_signal, compositor);
+		if (launcher->drm_fd != -1)
+			drmDropMaster(launcher->drm_fd);
+		ioctl(launcher->tty, VT_RELDISP, 1);
+	} else {
+		ioctl(launcher->tty, VT_RELDISP, VT_ACKACQ);
+		if (launcher->drm_fd != -1)
+			drmSetMaster(launcher->drm_fd);
+		compositor->session_active = 1;
+		wl_signal_emit(&compositor->session_signal, compositor);
+	}
+
+	return 1;
+}
+
+static int
+setup_tty(struct weston_launcher *launcher)
+{
+	struct wl_event_loop *loop;
+	struct vt_mode mode = { 0 };
+	struct stat buf;
+	int ret;
+
+	if (fstat(STDIN_FILENO, &buf) == -1 ||
+	    major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
+		weston_log("stdin not a vt\n");
+		return -1;
+	}
+
+	launcher->tty = STDIN_FILENO;
+	if (ioctl(launcher->tty, KDGKBMODE, &launcher->kb_mode)) {
+		weston_log("failed to read keyboard mode: %m\n");
+		return -1;
+	}
+
+	if (ioctl(launcher->tty, KDSKBMUTE, 1) &&
+	    ioctl(launcher->tty, KDSKBMODE, K_OFF)) {
+		weston_log("failed to set K_OFF keyboard mode: %m\n");
+		return -1;
+	}
+
+	ret = ioctl(launcher->tty, KDSETMODE, KD_GRAPHICS);
+	if (ret) {
+		weston_log("failed to set KD_GRAPHICS mode on tty: %m\n");
+		return -1;
+	}
+
+	mode.mode = VT_PROCESS;
+	mode.relsig = SIGUSR1;
+	mode.acqsig = SIGUSR1;
+	if (ioctl(launcher->tty, VT_SETMODE, &mode) < 0) {
+		weston_log("failed to take control of vt handling\n");
+		return -1;
+	}
+
+	loop = wl_display_get_event_loop(launcher->compositor->wl_display);
+	launcher->vt_source =
+		wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, launcher);
+	if (!launcher->vt_source)
+		return -1;
+
+	return 0;
+}
+
+int
+weston_launcher_activate_vt(struct weston_launcher *launcher, int vt)
+{
+	return ioctl(launcher->tty, VT_ACTIVATE, vt);
+}
+
 struct weston_launcher *
 weston_launcher_connect(struct weston_compositor *compositor)
 {
 	struct weston_launcher *launcher;
 	struct wl_event_loop *loop;
-	int fd;
-
-	fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
-	if (fd == -1)
-		return NULL;
 
 	launcher = malloc(sizeof *launcher);
 	if (launcher == NULL)
 		return NULL;
 
 	launcher->compositor = compositor;
-	launcher->fd = fd;
-
-	loop = wl_display_get_event_loop(compositor->wl_display);
-	launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
-						WL_EVENT_READABLE,
-						weston_launcher_data,
-						launcher);
-	if (launcher->source == NULL) {
+	launcher->drm_fd = -1;
+	launcher->fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
+	if (launcher->fd != -1) {
+		launcher->tty = weston_environment_get_fd("WESTON_TTY_FD");
+		loop = wl_display_get_event_loop(compositor->wl_display);
+		launcher->source = wl_event_loop_add_fd(loop, launcher->fd,
+							WL_EVENT_READABLE,
+							weston_launcher_data,
+							launcher);
+		if (launcher->source == NULL) {
+			free(launcher);
+			return NULL;
+		}
+	} else if (geteuid() == 0) {
+		setup_tty(launcher);
+	} else {
 		free(launcher);
 		return NULL;
 	}
@@ -180,6 +311,13 @@
 void
 weston_launcher_destroy(struct weston_launcher *launcher)
 {
-	close(launcher->fd);
+	if (launcher->fd != -1) {
+		close(launcher->fd);
+		wl_event_source_remove(launcher->source);
+	} else {
+		weston_launcher_restore(launcher);
+		wl_event_source_remove(launcher->vt_source);
+	}
+
 	free(launcher);
 }
diff --git a/src/launcher-util.h b/src/launcher-util.h
index 5fa9248..ab66916 100644
--- a/src/launcher-util.h
+++ b/src/launcher-util.h
@@ -39,4 +39,10 @@
 weston_launcher_open(struct weston_launcher *launcher,
 		     const char *path, int flags);
 
+int
+weston_launcher_activate_vt(struct weston_launcher *launcher, int vt);
+
+void
+weston_launcher_restore(struct weston_launcher *launcher);
+
 #endif
diff --git a/src/tty.c b/src/tty.c
deleted file mode 100644
index 6beeb3c..0000000
--- a/src/tty.c
+++ /dev/null
@@ -1,294 +0,0 @@
-/*
- * Copyright © 2010 Intel Corporation
- *
- * Permission to use, copy, modify, distribute, and sell this software and
- * its documentation for any purpose is hereby granted without fee, provided
- * that the above copyright notice appear in all copies and that both that
- * copyright notice and this permission notice appear in supporting
- * documentation, and that the name of the copyright holders not be used in
- * advertising or publicity pertaining to distribution of the software
- * without specific, written prior permission.  The copyright holders make
- * no representations about the suitability of this software for any
- * purpose.  It is provided "as is" without express or implied warranty.
- *
- * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
- * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
- * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
- * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
- * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
- * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
- * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include "config.h"
-
-#include <termios.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <linux/kd.h>
-#include <linux/vt.h>
-#include <linux/major.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-
-#include "compositor.h"
-
-/* Introduced in 2.6.38 */
-#ifndef K_OFF
-#define K_OFF 0x04
-#endif
-
-struct tty {
-	struct weston_compositor *compositor;
-	int fd;
-	struct termios terminal_attributes;
-
-	struct wl_event_source *input_source;
-	struct wl_event_source *vt_source;
-	int vt, starting_vt;
-	int kb_mode;
-};
-
-static int vt_handler(int signal_number, void *data)
-{
-	struct tty *tty = data;
-	struct weston_compositor *compositor = tty->compositor;
-
-	if (compositor->session_active) {
-		compositor->session_active = 0;
-		wl_signal_emit(&tty->compositor->session_signal,
-			       tty->compositor);
-
-		ioctl(tty->fd, VT_RELDISP, 1);
-	} else {
-
-		ioctl(tty->fd, VT_RELDISP, VT_ACKACQ);
-
-		compositor->session_active = 1;
-		wl_signal_emit(&tty->compositor->session_signal,
-			       tty->compositor);
-	}
-
-	return 1;
-}
-
-static int
-on_tty_input(int fd, uint32_t mask, void *data)
-{
-	struct tty *tty = data;
-
-	/* Ignore input to tty.  We get keyboard events from evdev */
-	tcflush(tty->fd, TCIFLUSH);
-
-	return 1;
-}
-
-static int
-try_open_vt(struct tty *tty)
-{
-	int tty0, fd;
-	char filename[16];
-
-	tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
-	if (tty0 < 0) {
-		weston_log("could not open tty0: %m\n");
-		return -1;
-	}
-
-	if (ioctl(tty0, VT_OPENQRY, &tty->vt) < 0 || tty->vt == -1) {
-		weston_log("could not open tty0: %m\n");
-		close(tty0);
-		return -1;
-	}
-
-	close(tty0);
-	snprintf(filename, sizeof filename, "/dev/tty%d", tty->vt);
-	weston_log("compositor: using new vt %s\n", filename);
-	fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
-	if (fd < 0)
-		return fd;
-
-	return fd;
-}
-
-int
-tty_activate_vt(struct tty *tty, int vt)
-{
-	return ioctl(tty->fd, VT_ACTIVATE, vt);
-}
-
-struct tty *
-tty_create(struct weston_compositor *compositor, int tty_nr)
-{
-	struct termios raw_attributes;
-	struct vt_mode mode = { 0 };
-	int ret;
-	struct tty *tty;
-	struct wl_event_loop *loop;
-	struct stat buf;
-	char filename[16];
-	struct vt_stat vts;
-
-	tty = zalloc(sizeof *tty);
-	if (tty == NULL)
-		return NULL;
-
-	tty->compositor = compositor;
-
-	tty->fd = weston_environment_get_fd("WESTON_TTY_FD");
-	if (tty->fd < 0)
-		tty->fd = STDIN_FILENO;
-
-	if (tty_nr > 0) {
-		snprintf(filename, sizeof filename, "/dev/tty%d", tty_nr);
-		weston_log("compositor: using %s\n", filename);
-		tty->fd = open(filename, O_RDWR | O_NOCTTY | O_CLOEXEC);
-		tty->vt = tty_nr;
-	} else if (fstat(tty->fd, &buf) == 0 &&
-		   major(buf.st_rdev) == TTY_MAJOR &&
-		   minor(buf.st_rdev) > 0) {
-		if (tty->fd == STDIN_FILENO)
-			tty->fd = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 0);
-		tty->vt = minor(buf.st_rdev);
-	} else {
-		/* Fall back to try opening a new VT.  This typically
-		 * requires root. */
-		tty->fd = try_open_vt(tty);
-	}
-
-	if (tty->fd < 0) {
-		weston_log("failed to open tty: %m\n");
-		free(tty);
-		return NULL;
-	}
-
-	if (ioctl(tty->fd, VT_GETSTATE, &vts) == 0)
-		tty->starting_vt = vts.v_active;
-	else
-		tty->starting_vt = tty->vt;
-
-	if (tty->starting_vt != tty->vt) {
-		if (ioctl(tty->fd, VT_ACTIVATE, tty->vt) < 0 ||
-		    ioctl(tty->fd, VT_WAITACTIVE, tty->vt) < 0) {
-			weston_log("failed to switch to new vt\n");
-			goto err;
-		}
-	}
-
-	if (tcgetattr(tty->fd, &tty->terminal_attributes) < 0) {
-		weston_log("could not get terminal attributes: %m\n");
-		goto err;
-	}
-
-	/* Ignore control characters and disable echo */
-	raw_attributes = tty->terminal_attributes;
-	cfmakeraw(&raw_attributes);
-
-	/* Fix up line endings to be normal (cfmakeraw hoses them) */
-	raw_attributes.c_oflag |= OPOST | OCRNL;
-
-	if (tcsetattr(tty->fd, TCSANOW, &raw_attributes) < 0)
-		weston_log("could not put terminal into raw mode: %m\n");
-
-	loop = wl_display_get_event_loop(compositor->wl_display);
-
-	ioctl(tty->fd, KDGKBMODE, &tty->kb_mode);
-	ret = ioctl(tty->fd, KDSKBMODE, K_OFF);
-	if (ret) {
-		ret = ioctl(tty->fd, KDSKBMODE, K_RAW);
-		if (ret) {
-			weston_log("failed to set keyboard mode on tty: %m\n");
-			goto err_attr;
-		}
-
-		tty->input_source = wl_event_loop_add_fd(loop, tty->fd,
-							 WL_EVENT_READABLE,
-							 on_tty_input, tty);
-		if (!tty->input_source)
-			goto err_kdkbmode;
-	}
-
-	ret = ioctl(tty->fd, KDSETMODE, KD_GRAPHICS);
-	if (ret) {
-		weston_log("failed to set KD_GRAPHICS mode on tty: %m\n");
-		goto err_kdkbmode;
-	}
-
-	mode.mode = VT_PROCESS;
-	mode.relsig = SIGUSR1;
-	mode.acqsig = SIGUSR1;
-	if (ioctl(tty->fd, VT_SETMODE, &mode) < 0) {
-		weston_log("failed to take control of vt handling\n");
-		goto err_kdmode;
-	}
-
-	tty->vt_source =
-		wl_event_loop_add_signal(loop, SIGUSR1, vt_handler, tty);
-	if (!tty->vt_source)
-		goto err_vtmode;
-
-	return tty;
-
-err_vtmode:
-	ioctl(tty->fd, VT_SETMODE, &mode);
-
-err_kdmode:
-	ioctl(tty->fd, KDSETMODE, KD_TEXT);
-
-err_kdkbmode:
-	if (tty->input_source)
-		wl_event_source_remove(tty->input_source);
-	ioctl(tty->fd, KDSKBMODE, tty->kb_mode);
-
-err_attr:
-	tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes);
-
-err:
-	close(tty->fd);
-	free(tty);
-	return NULL;
-}
-
-void tty_reset(struct tty *tty)
-{
-	struct vt_mode mode = { 0 };
-
-	if (ioctl(tty->fd, KDSKBMODE, tty->kb_mode))
-		weston_log("failed to restore keyboard mode: %m\n");
-
-	if (ioctl(tty->fd, KDSETMODE, KD_TEXT))
-		weston_log("failed to set KD_TEXT mode on tty: %m\n");
-
-	if (tcsetattr(tty->fd, TCSANOW, &tty->terminal_attributes) < 0)
-		weston_log("could not restore terminal to canonical mode\n");
-
-	mode.mode = VT_AUTO;
-	if (ioctl(tty->fd, VT_SETMODE, &mode) < 0)
-		weston_log("could not reset vt handling\n");
-
-	if (tty->compositor->session_active && tty->vt != tty->starting_vt) {
-		ioctl(tty->fd, VT_ACTIVATE, tty->starting_vt);
-		ioctl(tty->fd, VT_WAITACTIVE, tty->starting_vt);
-	}
-}
-
-void
-tty_destroy(struct tty *tty)
-{
-	if (tty->input_source)
-		wl_event_source_remove(tty->input_source);
-
-	if (tty->vt_source)
-		wl_event_source_remove(tty->vt_source);
-
-	tty_reset(tty);
-
-	close(tty->fd);
-
-	free(tty);
-}
diff --git a/src/weston-launch.c b/src/weston-launch.c
index 05f15c1..525c61c 100644
--- a/src/weston-launch.c
+++ b/src/weston-launch.c
@@ -28,7 +28,6 @@
 #include <assert.h>
 #include <poll.h>
 #include <errno.h>
-#include <termios.h>
 
 #include <error.h>
 #include <getopt.h>
@@ -43,7 +42,6 @@
 #include <unistd.h>
 #include <fcntl.h>
 
-#include <termios.h>
 #include <linux/vt.h>
 #include <linux/major.h>
 #include <linux/kd.h>
@@ -62,6 +60,10 @@
 
 #define DRM_MAJOR 226
 
+#ifndef KDSKBMUTE
+#define KDSKBMUTE	0x4B51
+#endif
+
 #define MAX_ARGV_SIZE 256
 
 struct weston_launch {
@@ -71,7 +73,6 @@
 	int ttynr;
 	int sock[2];
 	int drm_fd;
-	struct termios terminal_attributes;
 	int kb_mode;
 	struct passwd *pw;
 
@@ -391,16 +392,13 @@
 		pam_end(wl->ph, err);
 	}
 
-	if (ioctl(wl->tty, KDSKBMODE, wl->kb_mode))
+	if (ioctl(wl->tty, KDSKBMUTE, 0) &&
+	    ioctl(wl->tty, KDSKBMODE, wl->kb_mode))
 		fprintf(stderr, "failed to restore keyboard mode: %m\n");
 
 	if (ioctl(wl->tty, KDSETMODE, KD_TEXT))
 		fprintf(stderr, "failed to set KD_TEXT mode on tty: %m\n");
 
-	if (tcsetattr(wl->tty, TCSANOW, &wl->terminal_attributes) < 0)
-		fprintf(stderr,
-			"could not restore terminal to canonical mode\n");
-
 	mode.mode = VT_AUTO;
 	if (ioctl(wl->tty, VT_SETMODE, &mode) < 0)
 		fprintf(stderr, "could not reset vt handling\n");
@@ -464,11 +462,9 @@
 static int
 setup_tty(struct weston_launch *wl, const char *tty)
 {
-	struct termios raw_attributes;
 	struct stat buf;
 	struct vt_mode mode = { 0 };
 	char *t;
-	int ret;
 
 	if (!wl->new_user) {
 		wl->tty = STDIN_FILENO;
@@ -506,28 +502,14 @@
 		wl->ttynr = minor(buf.st_rdev);
 	}
 
-	if (tcgetattr(wl->tty, &wl->terminal_attributes) < 0)
-		error(1, errno, "could not get terminal attributes: %m\n");
+	if (ioctl(wl->tty, KDGKBMODE, &wl->kb_mode))
+		error(1, errno, "failed to get current keyboard mode: %m\n");
 
-	/* Ignore control characters and disable echo */
-	raw_attributes = wl->terminal_attributes;
-	cfmakeraw(&raw_attributes);
+	if (ioctl(wl->tty, KDSKBMUTE, 1) &&
+	    ioctl(wl->tty, KDSKBMODE, K_OFF))
+		error(1, errno, "failed to set K_OFF keyboard mode: %m\n");
 
-	/* Fix up line endings to be normal (cfmakeraw hoses them) */
-	raw_attributes.c_oflag |= OPOST | OCRNL;
-
-	if (tcsetattr(wl->tty, TCSANOW, &raw_attributes) < 0)
-		error(1, errno, "could not put terminal into raw mode: %m\n");
-
-	ioctl(wl->tty, KDGKBMODE, &wl->kb_mode);
-	ret = ioctl(wl->tty, KDSKBMODE, K_OFF);
-	if (ret)
-		ret = ioctl(wl->tty, KDSKBMODE, K_RAW);
-	if (ret)
-		error(1, errno, "failed to set keyboard mode on tty: %m\n");
-
-	ret = ioctl(wl->tty, KDSETMODE, KD_GRAPHICS);
-	if (ret)
+	if (ioctl(wl->tty, KDSETMODE, KD_GRAPHICS))
 		error(1, errno, "failed to set KD_GRAPHICS mode on tty: %m\n");
 
 	mode.mode = VT_PROCESS;
@@ -597,9 +579,7 @@
 
 	drop_privileges(wl);
 
-	if (wl->tty != STDIN_FILENO)
-		setenv_fd("WESTON_TTY_FD", wl->tty);
-
+	setenv_fd("WESTON_TTY_FD", wl->tty);
 	setenv_fd("WESTON_LAUNCHER_SOCK", wl->sock[1]);
 
 	unsetenv("DISPLAY");