[PATCH weston v2] compositor: allow to control the vt switching

Giulio Camuffo giuliocamuffo at gmail.com
Sat Jun 4 17:48:31 UTC 2016


A compositor may want to control the vt switching, for example to
ensure to have a lock screen before it. To enable that add a vfunc
that will be called when CTRL+ALT+FN is pressed. The default behavior
is to do the switching, but the user can change it by using the new
weston_compositor_set_vt_switcher() function, so that it can delay the
switching to later, by calling weston_compositor_activate_vt().

Signed-off-by: Giulio Camuffo <giuliocamuffo at gmail.com>
---
 Makefile.am                  |  2 +-
 src/compositor.c             | 18 ++++++++++++++++++
 src/compositor.h             | 39 +++++++++++++++++++++++++++++++++++++++
 src/launcher-direct.c        | 12 ++++++++++++
 src/launcher-impl.h          |  2 ++
 src/launcher-logind.c        |  8 ++++++++
 src/launcher-util.c          | 11 ++++++++++-
 src/launcher-weston-launch.c | 12 ++++++++++++
 src/main.c                   |  1 +
 9 files changed, 103 insertions(+), 2 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 2358000..f6e0ee9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -66,7 +66,7 @@ libweston_la_CPPFLAGS = $(AM_CPPFLAGS) -DIN_WESTON
 libweston_la_CFLAGS = $(AM_CFLAGS) $(COMPOSITOR_CFLAGS) $(LIBUNWIND_CFLAGS)
 libweston_la_LIBADD = $(COMPOSITOR_LIBS) $(LIBUNWIND_LIBS) \
 	$(DLOPEN_LIBS) -lm $(CLOCK_GETTIME_LIBS) \
-	$(LIBINPUT_BACKEND_LIBS) libshared.la
+	$(LIBINPUT_BACKEND_LIBS) libshared.la libsession-helper.la
 libweston_la_LDFLAGS = -release ${LIBWESTON_ABI_VERSION}
 
 libweston_la_SOURCES =					\
diff --git a/src/compositor.c b/src/compositor.c
index bf8e5aa..938894b 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -54,6 +54,8 @@
 #include "timeline.h"
 
 #include "compositor.h"
+#include "launcher-impl.h"
+#include "launcher-util.h"
 #include "scaler-server-protocol.h"
 #include "presentation-time-server-protocol.h"
 #include "shared/helpers.h"
@@ -4492,6 +4494,22 @@ compositor_bind(struct wl_client *client,
 }
 
 WL_EXPORT int
+weston_compositor_activate_vt(struct weston_compositor *compositor, int vt)
+{
+	if (compositor->launcher)
+		return weston_launcher_activate_vt(compositor->launcher, vt);
+	return -1;
+}
+
+WL_EXPORT void
+weston_compositor_set_vt_switcher(struct weston_compositor *compositor,
+				  weston_compositor_vt_switcher_func_t switcher)
+{
+	if (compositor->launcher)
+		compositor->launcher->vt_switcher = switcher;
+}
+
+WL_EXPORT int
 weston_environment_get_fd(const char *env)
 {
 	char *e, *end;
diff --git a/src/compositor.h b/src/compositor.h
index f934aab..472e482 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -1369,6 +1369,45 @@ void
 weston_compositor_set_default_pointer_grab(struct weston_compositor *compositor,
 			const struct weston_pointer_grab_interface *interface);
 
+/**
+ * Request a vt switch for the compositor.
+ *
+ * This will only work if the compositor is running as the owner of
+ * the session.
+ *
+ * \param launcher The compositor instance.
+ * \param vt The vt to switch to.
+ *
+ * Returns 0 on success, -1 otherwise.
+ *
+ * \sa weston_compositor_set_vt_switcher
+ */
+int
+weston_compositor_activate_vt(struct weston_compositor *compositor, int vt);
+
+typedef void (*weston_compositor_vt_switcher_func_t)(
+		struct weston_compositor *compositor, int vt);
+/**
+ * Set the vt switcher for the compositor.
+ *
+ * If the compositor is the owner of the session, the CTRL+ALT+FN key
+ * combinations will trigger a vt switch. The default behavior is to do
+ * the switching immediately, but some compositors may want to make sure to
+ * e.g. draw a lock screen before doing the switch.
+ * This function allows to register a custom vt switcher so that the actual
+ * vt switch can be controlled by calling \a weston_compositor_activate_vt.
+ *
+ * \param compositor The compositor instance.
+ * \param switcher The vt switcher function, which will be called when a
+ *                 CTRL+ALT+FN key combination is pressed, carrying the
+ *                 requested vt.
+ *
+ * \sa weston_compositor_activate_vt
+ */
+void
+weston_compositor_set_vt_switcher(struct weston_compositor *compositor,
+				  weston_compositor_vt_switcher_func_t switcher);
+
 int
 weston_environment_get_fd(const char *env);
 
diff --git a/src/launcher-direct.c b/src/launcher-direct.c
index 29d9c28..0ff99a4 100644
--- a/src/launcher-direct.c
+++ b/src/launcher-direct.c
@@ -305,6 +305,17 @@ launcher_direct_destroy(struct weston_launcher *launcher_base)
 	free(launcher);
 }
 
+static int
+launcher_direct_get_vt(struct weston_launcher *base)
+{
+	struct launcher_direct *launcher = wl_container_of(base, launcher, base);
+	struct stat s;
+	if (fstat(launcher->tty, &s) < 0)
+		return -1;
+
+	return minor(s.st_rdev);
+}
+
 struct launcher_interface launcher_direct_iface = {
 	launcher_direct_connect,
 	launcher_direct_destroy,
@@ -312,4 +323,5 @@ struct launcher_interface launcher_direct_iface = {
 	launcher_direct_close,
 	launcher_direct_activate_vt,
 	launcher_direct_restore,
+	launcher_direct_get_vt,
 };
diff --git a/src/launcher-impl.h b/src/launcher-impl.h
index 742721b..8b0531d 100644
--- a/src/launcher-impl.h
+++ b/src/launcher-impl.h
@@ -34,10 +34,12 @@ struct launcher_interface {
 	void (* close) (struct weston_launcher *launcher, int fd);
 	int (* activate_vt) (struct weston_launcher *launcher, int vt);
 	void (* restore) (struct weston_launcher *launcher);
+	int (* get_vt) (struct weston_launcher *launcher);
 };
 
 struct weston_launcher {
 	struct launcher_interface *iface;
+	weston_compositor_vt_switcher_func_t vt_switcher;
 };
 
 extern struct launcher_interface launcher_logind_iface;
diff --git a/src/launcher-logind.c b/src/launcher-logind.c
index f755ec3..4680402 100644
--- a/src/launcher-logind.c
+++ b/src/launcher-logind.c
@@ -829,6 +829,13 @@ launcher_logind_destroy(struct weston_launcher *launcher)
 	free(wl);
 }
 
+static int
+launcher_logind_get_vt(struct weston_launcher *launcher)
+{
+	struct launcher_logind *wl = wl_container_of(launcher, wl, base);
+	return wl->vtnr;
+}
+
 struct launcher_interface launcher_logind_iface = {
 	launcher_logind_connect,
 	launcher_logind_destroy,
@@ -836,4 +843,5 @@ struct launcher_interface launcher_logind_iface = {
 	launcher_logind_close,
 	launcher_logind_activate_vt,
 	launcher_logind_restore,
+	launcher_logind_get_vt,
 };
diff --git a/src/launcher-util.c b/src/launcher-util.c
index 03b9d63..7bd91ae 100644
--- a/src/launcher-util.c
+++ b/src/launcher-util.c
@@ -97,8 +97,17 @@ switch_vt_binding(struct weston_keyboard *keyboard,
 		  uint32_t time, uint32_t key, void *data)
 {
 	struct weston_compositor *compositor = data;
+	struct weston_launcher *launcher = compositor->launcher;
+	int vt = key - KEY_F1 + 1;
+	weston_compositor_vt_switcher_func_t switcher = launcher->vt_switcher;
 
-	weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
+	if (vt == launcher->iface->get_vt(launcher))
+		return;
+
+	if (switcher)
+		launcher->vt_switcher(compositor, vt);
+	else
+		weston_launcher_activate_vt(launcher, vt);
 }
 
 WL_EXPORT void
diff --git a/src/launcher-weston-launch.c b/src/launcher-weston-launch.c
index ad919f1..b74975a 100644
--- a/src/launcher-weston-launch.c
+++ b/src/launcher-weston-launch.c
@@ -290,6 +290,17 @@ launcher_weston_launch_destroy(struct weston_launcher *launcher_base)
 	free(launcher);
 }
 
+static int
+launcher_weston_launch_get_vt(struct weston_launcher *base)
+{
+	struct launcher_weston_launch *launcher = wl_container_of(base, launcher, base);
+	struct stat s;
+	if (fstat(launcher->tty, &s) < 0)
+		return -1;
+
+	return minor(s.st_rdev);
+}
+
 struct launcher_interface launcher_weston_launch_iface = {
 	launcher_weston_launch_connect,
 	launcher_weston_launch_destroy,
@@ -297,4 +308,5 @@ struct launcher_interface launcher_weston_launch_iface = {
 	launcher_weston_launch_close,
 	launcher_weston_launch_activate_vt,
 	launcher_weston_launch_restore,
+	launcher_weston_launch_get_vt,
 };
diff --git a/src/main.c b/src/main.c
index d17d941..1cba49a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -54,6 +54,7 @@
 #include "git-version.h"
 #include "version.h"
 #include "weston.h"
+#include "launcher-util.h"
 
 #include "compositor-drm.h"
 #include "compositor-headless.h"
-- 
2.8.3



More information about the wayland-devel mailing list