launcher: add logind backend

Instead of connecting to weston-launch from launcher-util, we now try to
connect to logind first. If logind provides session-devices, we use them.
If not, we fall back to the old weston-launch facility.
diff --git a/src/launcher-util.c b/src/launcher-util.c
index 8b496c8..a1ff832 100644
--- a/src/launcher-util.c
+++ b/src/launcher-util.c
@@ -46,6 +46,7 @@
 
 #include "compositor.h"
 #include "launcher-util.h"
+#include "logind-util.h"
 #include "weston-launch.h"
 
 #define DRM_MAJOR 226
@@ -57,6 +58,7 @@
 union cmsg_data { unsigned char b[4]; int fd; };
 
 struct weston_launcher {
+	struct weston_logind *logind;
 	struct weston_compositor *compositor;
 	int fd;
 	struct wl_event_source *source;
@@ -104,6 +106,9 @@
 	struct weston_launcher_open *message;
 	struct stat s;
 
+	if (launcher->logind)
+		return weston_logind_open(launcher->logind, path, flags);
+
 	if (launcher->fd == -1) {
 		fd = open(path, flags | O_CLOEXEC);
 		if (fd == -1)
@@ -176,6 +181,9 @@
 void
 weston_launcher_close(struct weston_launcher *launcher, int fd)
 {
+	if (launcher->logind)
+		return weston_logind_close(launcher->logind, fd);
+
 	close(fd);
 }
 
@@ -184,6 +192,9 @@
 {
 	struct vt_mode mode = { 0 };
 
+	if (launcher->logind)
+		return weston_logind_restore(launcher->logind);
+
 	if (ioctl(launcher->tty, KDSKBMUTE, 0) &&
 	    ioctl(launcher->tty, KDSKBMODE, launcher->kb_mode))
 		weston_log("failed to restore kb mode: %m\n");
@@ -342,19 +353,25 @@
 int
 weston_launcher_activate_vt(struct weston_launcher *launcher, int vt)
 {
+	if (launcher->logind)
+		return weston_logind_activate_vt(launcher->logind, vt);
+
 	return ioctl(launcher->tty, VT_ACTIVATE, vt);
 }
 
 struct weston_launcher *
-weston_launcher_connect(struct weston_compositor *compositor, int tty)
+weston_launcher_connect(struct weston_compositor *compositor, int tty,
+			const char *seat_id)
 {
 	struct weston_launcher *launcher;
 	struct wl_event_loop *loop;
+	int r;
 
 	launcher = malloc(sizeof *launcher);
 	if (launcher == NULL)
 		return NULL;
 
+	launcher->logind = NULL;
 	launcher->compositor = compositor;
 	launcher->drm_fd = -1;
 	launcher->fd = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
@@ -369,14 +386,21 @@
 			free(launcher);
 			return NULL;
 		}
-	} else if (geteuid() == 0) {
-		if (setup_tty(launcher, tty) == -1) {
-			free(launcher);
-			return NULL;
-		}
 	} else {
-		free(launcher);
-		return NULL;
+		r = weston_logind_connect(&launcher->logind, compositor,
+					  seat_id, tty);
+		if (r < 0) {
+			launcher->logind = NULL;
+			if (geteuid() == 0) {
+				if (setup_tty(launcher, tty) == -1) {
+					free(launcher);
+					return NULL;
+				}
+			} else {
+				free(launcher);
+				return NULL;
+			}
+		}
 	}
 
 	return launcher;
@@ -385,6 +409,8 @@
 void
 weston_launcher_destroy(struct weston_launcher *launcher)
 {
+	if (launcher->logind)
+		weston_logind_destroy(launcher->logind);
 	if (launcher->fd != -1) {
 		close(launcher->fd);
 		wl_event_source_remove(launcher->source);
@@ -393,6 +419,8 @@
 		wl_event_source_remove(launcher->vt_source);
 	}
 
-	close(launcher->tty);
+	if (launcher->tty >= 0)
+		close(launcher->tty);
+
 	free(launcher);
 }