[PATCH weston 3/3] Add libinput based input device backend

Jonas Ådahl jadahl at gmail.com
Wed Mar 12 14:08:42 PDT 2014


While disable by default, passing --enable-libinput-backend to
./configure switches the input backend in weston's drm, fbdev and rpi
compositing backends to use libinput instead of udev-seat.c, evdev.c and
friends.

When enabled, weston now also depends on libinput >= 0.1.0.

Signed-off-by: Jonas Ådahl <jadahl at gmail.com>
---

Hi,

These patches enables the use of libinput 0.1 in weston. While libinput
originates from the weston code base, so does the new input backend in
this patch. More specifically, libinput-device.* and libinput-seat.* are
based on evdev.* and udev-seat.* respectively, so its possible to see the
changes by calling "diff -u src/evdev.c src/libinput-device.c" etc.

Jonas

 Makefile.am            |  43 +++---
 configure.ac           |  12 ++
 src/compositor-drm.c   |   2 +-
 src/compositor-fbdev.c |   2 +-
 src/compositor-rpi.c   |   5 +-
 src/libinput-device.c  | 364 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/libinput-device.h  |  75 ++++++++++
 src/libinput-seat.c    | 353 +++++++++++++++++++++++++++++++++++++++++++++++
 src/libinput-seat.h    |  62 +++++++++
 src/udev-input.h       |  34 +++++
 10 files changed, 931 insertions(+), 21 deletions(-)
 create mode 100644 src/libinput-device.c
 create mode 100644 src/libinput-device.h
 create mode 100644 src/libinput-seat.c
 create mode 100644 src/libinput-seat.h
 create mode 100644 src/udev-input.h

diff --git a/Makefile.am b/Makefile.am
index 64d0743..5ff4f83 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -50,8 +50,6 @@ weston_SOURCES =					\
 	src/compositor.h				\
 	src/input.c					\
 	src/data-device.c				\
-	src/filter.c					\
-	src/filter.h					\
 	src/screenshooter.c				\
 	src/clipboard.c					\
 	src/zoom.c					\
@@ -181,12 +179,33 @@ x11_backend_la_CFLAGS =				\
 x11_backend_la_SOURCES = src/compositor-x11.c
 endif
 
+INPUT_BACKEND_SOURCES = src/udev-input.h
+
+if ENABLE_LIBINPUT_BACKEND
+INPUT_BACKEND_LIBS = $(LIBINPUT_BACKEND_LIBS)
+INPUT_BACKEND_SOURCES +=			\
+	src/libinput-seat.c			\
+	src/libinput-seat.h			\
+	src/libinput-device.c			\
+	src/libinput-device.h
+else
+INPUT_BACKEND_SOURCES +=			\
+	src/filter.c				\
+	src/filter.h				\
+	src/udev-seat.c				\
+	src/udev-seat.h				\
+	src/evdev.c				\
+	src/evdev.h				\
+	src/evdev-touchpad.c
+endif
+
 if ENABLE_DRM_COMPOSITOR
 module_LTLIBRARIES += drm-backend.la
 drm_backend_la_LDFLAGS = -module -avoid-version
 drm_backend_la_LIBADD =				\
 	$(COMPOSITOR_LIBS)			\
 	$(DRM_COMPOSITOR_LIBS)			\
+	$(INPUT_BACKEND_LIBS)			\
 	libshared.la -lrt			\
 	libsession-helper.la
 drm_backend_la_CFLAGS =				\
@@ -196,11 +215,7 @@ drm_backend_la_CFLAGS =				\
 	$(GCC_CFLAGS)
 drm_backend_la_SOURCES =			\
 	src/compositor-drm.c			\
-	src/udev-seat.c				\
-	src/udev-seat.h				\
-	src/evdev.c				\
-	src/evdev.h				\
-	src/evdev-touchpad.c			\
+	$(INPUT_BACKEND_SOURCES)		\
 	src/libbacklight.c			\
 	src/libbacklight.h
 
@@ -239,6 +254,7 @@ rpi_backend_la_LDFLAGS = -module -avoid-version
 rpi_backend_la_LIBADD = $(COMPOSITOR_LIBS)	\
 	$(RPI_COMPOSITOR_LIBS)			\
 	$(RPI_BCM_HOST_LIBS)			\
+	$(INPUT_BACKEND_LIBS)			\
 	libsession-helper.la			\
 	libshared.la
 rpi_backend_la_CFLAGS =				\
@@ -251,11 +267,7 @@ rpi_backend_la_SOURCES =			\
 	src/rpi-renderer.c			\
 	src/rpi-renderer.h			\
 	src/rpi-bcm-stubs.h			\
-	src/udev-seat.c				\
-	src/udev-seat.h				\
-	src/evdev.c				\
-	src/evdev.h				\
-	src/evdev-touchpad.c
+	$(INPUT_BACKEND_SOURCES)
 
 if ENABLE_EGL
 rpi_backend_la_LIBADD += $(EGL_LIBS)
@@ -278,6 +290,7 @@ fbdev_backend_la_LDFLAGS = -module -avoid-version
 fbdev_backend_la_LIBADD =			\
 	$(COMPOSITOR_LIBS)			\
 	$(FBDEV_COMPOSITOR_LIBS)		\
+	$(INPUT_BACKEND_LIBS)			\
 	libsession-helper.la			\
 	libshared.la
 fbdev_backend_la_CFLAGS =			\
@@ -288,11 +301,7 @@ fbdev_backend_la_CFLAGS =			\
 	$(GCC_CFLAGS)
 fbdev_backend_la_SOURCES =			\
 	src/compositor-fbdev.c			\
-	src/udev-seat.c				\
-	src/udev-seat.h				\
-	src/evdev.c				\
-	src/evdev.h				\
-	src/evdev-touchpad.c
+	$(INPUT_BACKEND_SOURCES)
 endif
 
 if ENABLE_RDP_COMPOSITOR
diff --git a/configure.ac b/configure.ac
index 00d7123..4db0ed1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -153,6 +153,16 @@ if test x$enable_drm_compositor = xyes; then
   PKG_CHECK_MODULES(DRM_COMPOSITOR, [libudev >= 136 libdrm >= 2.4.30 gbm mtdev >= 1.1.0])
 fi
 
+
+AC_ARG_ENABLE(libinput-backend, [  --enable-libinput-backend],,
+	      enable_libinput_backend=no)
+AM_CONDITIONAL([ENABLE_LIBINPUT_BACKEND], [test x$enable_libinput_backend = xyes])
+if test x$enable_libinput_backend = xyes; then
+  AC_DEFINE([BUILD_LIBINPUT_BACKEND], [1], [Build the libinput input device backend])
+  PKG_CHECK_MODULES(LIBINPUT_BACKEND, [libinput >= 0.1.0])
+fi
+
+
 PKG_CHECK_MODULES(COMPOSITOR, [$COMPOSITOR_MODULES])
 
 AC_ARG_ENABLE(wayland-compositor, [  --enable-wayland-compositor],,
@@ -506,6 +516,8 @@ AC_MSG_RESULT([
 	FBDEV Compositor		${enable_fbdev_compositor}
 	RDP Compositor			${enable_rdp_compositor}
 
+	libinput Backend		${enable_libinput_backend}
+
 	Raspberry Pi BCM headers	${have_bcm_host}
 
 	Build Clients			${enable_clients}
diff --git a/src/compositor-drm.c b/src/compositor-drm.c
index e489957..ed30168 100644
--- a/src/compositor-drm.c
+++ b/src/compositor-drm.c
@@ -47,7 +47,7 @@
 #include "compositor.h"
 #include "gl-renderer.h"
 #include "pixman-renderer.h"
-#include "udev-seat.h"
+#include "udev-input.h"
 #include "launcher-util.h"
 #include "vaapi-recorder.h"
 
diff --git a/src/compositor-fbdev.c b/src/compositor-fbdev.c
index ca38c42..4700c89 100644
--- a/src/compositor-fbdev.c
+++ b/src/compositor-fbdev.c
@@ -42,7 +42,7 @@
 #include "compositor.h"
 #include "launcher-util.h"
 #include "pixman-renderer.h"
-#include "udev-seat.h"
+#include "udev-input.h"
 #include "gl-renderer.h"
 
 struct fbdev_compositor {
diff --git a/src/compositor-rpi.c b/src/compositor-rpi.c
index 78221a0..287451d 100644
--- a/src/compositor-rpi.c
+++ b/src/compositor-rpi.c
@@ -29,10 +29,12 @@
 #include <stdio.h>
 #include <string.h>
 #include <math.h>
+#include <sys/time.h>
 #include <sys/types.h>
 #include <fcntl.h>
 #include <unistd.h>
 
+#include <linux/input.h>
 #include <libudev.h>
 
 #ifdef HAVE_BCM_HOST
@@ -43,9 +45,8 @@
 
 #include "compositor.h"
 #include "rpi-renderer.h"
-#include "evdev.h"
 #include "launcher-util.h"
-#include "udev-seat.h"
+#include "udev-input.h"
 
 #if 0
 #define DBG(...) \
diff --git a/src/libinput-device.c b/src/libinput-device.c
new file mode 100644
index 0000000..c8a6443
--- /dev/null
+++ b/src/libinput-device.c
@@ -0,0 +1,364 @@
+/*
+ * Copyright © 2010 Intel Corporation
+ * Copyright © 2013 Jonas Ådahl
+ *
+ * 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 <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <linux/input.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <mtdev.h>
+#include <assert.h>
+#include <libinput.h>
+
+#include "compositor.h"
+#include "libinput-device.h"
+
+#define DEFAULT_AXIS_STEP_DISTANCE wl_fixed_from_int(10)
+
+void
+evdev_led_update(struct evdev_device *device, enum weston_led weston_leds)
+{
+	enum libinput_led leds = 0;
+
+	if (weston_leds & LED_NUM_LOCK)
+		leds |= LIBINPUT_LED_NUM_LOCK;
+	if (weston_leds & LED_CAPS_LOCK)
+		leds |= LIBINPUT_LED_CAPS_LOCK;
+	if (weston_leds & LED_SCROLL_LOCK)
+		leds |= LIBINPUT_LED_SCROLL_LOCK;
+
+	libinput_device_led_update(device->device, leds);
+}
+
+static void
+handle_keyboard_key(struct libinput_device *libinput_device,
+		    struct libinput_event_keyboard *keyboard_event)
+{
+	struct evdev_device *device =
+		libinput_device_get_user_data(libinput_device);
+
+	notify_key(device->seat,
+		   libinput_event_keyboard_get_time(keyboard_event),
+		   libinput_event_keyboard_get_key(keyboard_event),
+		   libinput_event_keyboard_get_key_state(keyboard_event),
+		   STATE_UPDATE_AUTOMATIC);
+}
+
+static void
+handle_pointer_motion(struct libinput_device *libinput_device,
+		      struct libinput_event_pointer *pointer_event)
+{
+	struct evdev_device *device =
+		libinput_device_get_user_data(libinput_device);
+
+	notify_motion(device->seat,
+		      libinput_event_pointer_get_time(pointer_event),
+		      libinput_event_pointer_get_dx(pointer_event),
+		      libinput_event_pointer_get_dy(pointer_event));
+}
+
+static void
+handle_pointer_motion_absolute(
+	struct libinput_device *libinput_device,
+	struct libinput_event_pointer *pointer_event)
+{
+	struct evdev_device *device =
+		libinput_device_get_user_data(libinput_device);
+	struct weston_output *output = device->output;
+	uint32_t time;
+	wl_fixed_t x, y;
+	uint32_t width, height;
+
+	if (!output)
+		return;
+
+	time = libinput_event_pointer_get_time(pointer_event);
+	width = device->output->current_mode->width;
+	height = device->output->current_mode->height;
+
+	x = libinput_event_pointer_get_absolute_x_transformed(pointer_event,
+							      width);
+	y = libinput_event_pointer_get_absolute_y_transformed(pointer_event,
+							      height);
+
+	weston_output_transform_coordinate(device->output, x, y, &x, &y);
+	notify_motion_absolute(device->seat, time, x, y);
+}
+
+static void
+handle_pointer_button(struct libinput_device *libinput_device,
+		      struct libinput_event_pointer *pointer_event)
+{
+	struct evdev_device *device =
+		libinput_device_get_user_data(libinput_device);
+
+	notify_button(device->seat,
+		      libinput_event_pointer_get_time(pointer_event),
+		      libinput_event_pointer_get_button(pointer_event),
+		      libinput_event_pointer_get_button_state(pointer_event));
+}
+
+static void
+handle_pointer_axis(struct libinput_device *libinput_device,
+		    struct libinput_event_pointer *pointer_event)
+{
+	struct evdev_device *device =
+		libinput_device_get_user_data(libinput_device);
+
+	notify_axis(device->seat,
+		    libinput_event_pointer_get_time(pointer_event),
+		    libinput_event_pointer_get_axis(pointer_event),
+		    libinput_event_pointer_get_axis_value(pointer_event));
+}
+
+static void
+handle_touch_with_coords(struct libinput_device *libinput_device,
+			 struct libinput_event_touch *touch_event,
+			 int touch_type)
+{
+	struct evdev_device *device =
+		libinput_device_get_user_data(libinput_device);
+	wl_fixed_t x;
+	wl_fixed_t y;
+	uint32_t width, height;
+	uint32_t time;
+	int32_t slot;
+
+	time = libinput_event_touch_get_time(touch_event);
+	slot = libinput_event_touch_get_seat_slot(touch_event);
+
+	width = device->output->current_mode->width;
+	height = device->output->current_mode->height;
+	x = libinput_event_touch_get_x_transformed(touch_event, width);
+	y = libinput_event_touch_get_y_transformed(touch_event, height);
+
+	weston_output_transform_coordinate(device->output,
+					   x, y, &x, &y);
+
+	notify_touch(device->seat, time, slot, x, y, touch_type);
+}
+
+static void
+handle_touch_down(struct libinput_device *device,
+		  struct libinput_event_touch *touch_event)
+{
+	handle_touch_with_coords(device, touch_event, WL_TOUCH_DOWN);
+}
+
+static void
+handle_touch_motion(struct libinput_device *device,
+		    struct libinput_event_touch *touch_event)
+{
+	handle_touch_with_coords(device, touch_event, WL_TOUCH_MOTION);
+}
+
+static void
+handle_touch_up(struct libinput_device *libinput_device,
+		struct libinput_event_touch *touch_event)
+{
+	struct evdev_device *device =
+		libinput_device_get_user_data(libinput_device);
+	uint32_t time = libinput_event_touch_get_time(touch_event);
+	int32_t slot = libinput_event_touch_get_seat_slot(touch_event);
+
+	notify_touch(device->seat, time, slot, 0, 0, WL_TOUCH_UP);
+}
+
+int
+evdev_device_process_event(struct libinput_event *event)
+{
+	struct libinput_device *libinput_device =
+		libinput_event_get_device(event);
+	int handled = 1;
+
+	switch (libinput_event_get_type(event)) {
+	case LIBINPUT_EVENT_KEYBOARD_KEY:
+		handle_keyboard_key(libinput_device,
+				    libinput_event_get_keyboard_event(event));
+		break;
+	case LIBINPUT_EVENT_POINTER_MOTION:
+		handle_pointer_motion(libinput_device,
+				      libinput_event_get_pointer_event(event));
+		break;
+	case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
+		handle_pointer_motion_absolute(
+			libinput_device,
+			libinput_event_get_pointer_event(event));
+		break;
+	case LIBINPUT_EVENT_POINTER_BUTTON:
+		handle_pointer_button(libinput_device,
+				      libinput_event_get_pointer_event(event));
+		break;
+	case LIBINPUT_EVENT_POINTER_AXIS:
+		handle_pointer_axis(libinput_device,
+				    libinput_event_get_pointer_event(event));
+		break;
+	case LIBINPUT_EVENT_TOUCH_DOWN:
+		handle_touch_down(libinput_device,
+				  libinput_event_get_touch_event(event));
+		break;
+	case LIBINPUT_EVENT_TOUCH_MOTION:
+		handle_touch_motion(libinput_device,
+				    libinput_event_get_touch_event(event));
+		break;
+	case LIBINPUT_EVENT_TOUCH_UP:
+		handle_touch_up(libinput_device,
+				libinput_event_get_touch_event(event));
+	default:
+		handled = 0;
+		weston_log("unknown libinput event %d\n",
+			   libinput_event_get_type(event));
+	}
+
+	return handled;
+}
+
+static void
+notify_output_destroy(struct wl_listener *listener, void *data)
+{
+	struct evdev_device *device =
+		container_of(listener,
+			     struct evdev_device, output_destroy_listener);
+	struct weston_compositor *c = device->seat->compositor;
+	struct weston_output *output;
+
+	if (device->output_name) {
+		output = container_of(c->output_list.next,
+				      struct weston_output, link);
+		evdev_device_set_output(device, output);
+	} else {
+		device->output = NULL;
+	}
+}
+
+void
+evdev_device_set_output(struct evdev_device *device,
+			struct weston_output *output)
+{
+	device->output = output;
+	device->output_destroy_listener.notify = notify_output_destroy;
+	wl_signal_add(&output->destroy_signal,
+		      &device->output_destroy_listener);
+}
+
+struct evdev_device *
+evdev_device_create(struct libinput_device *libinput_device,
+		    struct weston_seat *seat)
+{
+	struct evdev_device *device;
+
+	device = zalloc(sizeof *device);
+	if (device == NULL)
+		return NULL;
+
+	device->seat = seat;
+	wl_list_init(&device->link);
+	device->device = libinput_device;
+
+	if (libinput_device_has_capability(libinput_device,
+					   LIBINPUT_DEVICE_CAP_KEYBOARD)) {
+		weston_seat_init_keyboard(seat, NULL);
+		device->seat_caps |= EVDEV_SEAT_KEYBOARD;
+	}
+	if (libinput_device_has_capability(libinput_device,
+					   LIBINPUT_DEVICE_CAP_POINTER)) {
+		weston_seat_init_pointer(seat);
+		device->seat_caps |= EVDEV_SEAT_POINTER;
+	}
+	if (libinput_device_has_capability(libinput_device,
+					   LIBINPUT_DEVICE_CAP_TOUCH)) {
+		weston_seat_init_touch(seat);
+		device->seat_caps |= EVDEV_SEAT_TOUCH;
+	}
+
+	libinput_device_set_user_data(libinput_device, device);
+	libinput_device_ref(libinput_device);
+
+	return device;
+}
+
+void
+evdev_device_destroy(struct evdev_device *device)
+{
+	if (device->seat_caps & EVDEV_SEAT_POINTER)
+		weston_seat_release_pointer(device->seat);
+	if (device->seat_caps & EVDEV_SEAT_KEYBOARD)
+		weston_seat_release_keyboard(device->seat);
+	if (device->seat_caps & EVDEV_SEAT_TOUCH)
+		weston_seat_release_touch(device->seat);
+
+	if (device->output)
+		wl_list_remove(&device->output_destroy_listener.link);
+	wl_list_remove(&device->link);
+	libinput_device_unref(device->device);
+	free(device->devnode);
+	free(device->output_name);
+	free(device);
+}
+
+void
+evdev_notify_keyboard_focus(struct weston_seat *seat,
+			    struct wl_list *evdev_devices)
+{
+	struct evdev_device *device;
+	struct wl_array keys;
+	unsigned int i, set;
+	char evdev_keys[(KEY_CNT + 7) / 8];
+	char all_keys[(KEY_CNT + 7) / 8];
+	uint32_t *k;
+	int ret;
+
+	if (!seat->keyboard_device_count > 0)
+		return;
+
+	memset(all_keys, 0, sizeof all_keys);
+	wl_list_for_each(device, evdev_devices, link) {
+		memset(evdev_keys, 0, sizeof evdev_keys);
+		ret = libinput_device_get_keys(device->device,
+					       evdev_keys,
+					       sizeof evdev_keys);
+		if (ret < 0) {
+			weston_log("failed to get keys for device %s\n",
+				device->devnode);
+			continue;
+		}
+		for (i = 0; i < ARRAY_LENGTH(evdev_keys); i++)
+			all_keys[i] |= evdev_keys[i];
+	}
+
+	wl_array_init(&keys);
+	for (i = 0; i < KEY_CNT; i++) {
+		set = all_keys[i >> 3] & (1 << (i & 7));
+		if (set) {
+			k = wl_array_add(&keys, sizeof *k);
+			*k = i;
+		}
+	}
+
+	notify_keyboard_focus_in(seat, &keys, STATE_UPDATE_AUTOMATIC);
+
+	wl_array_release(&keys);
+}
diff --git a/src/libinput-device.h b/src/libinput-device.h
new file mode 100644
index 0000000..0775743
--- /dev/null
+++ b/src/libinput-device.h
@@ -0,0 +1,75 @@
+/*
+ * Copyright © 2011, 2012 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.
+ */
+
+#ifndef _LIBINPUT_DEVICE_H_
+#define _LIBINPUT_DEVICE_H_
+
+#include "config.h"
+
+#include <linux/input.h>
+#include <wayland-util.h>
+#include <libinput.h>
+
+#include "compositor.h"
+
+enum evdev_device_seat_capability {
+	EVDEV_SEAT_POINTER = (1 << 0),
+	EVDEV_SEAT_KEYBOARD = (1 << 1),
+	EVDEV_SEAT_TOUCH = (1 << 2)
+};
+
+struct evdev_device {
+	struct weston_seat *seat;
+	enum evdev_device_seat_capability seat_caps;
+	struct libinput_device *device;
+	struct wl_list link;
+	struct weston_output *output;
+	struct wl_listener output_destroy_listener;
+	char *devnode;
+	char *output_name;
+	int fd;
+};
+
+void
+evdev_led_update(struct evdev_device *device, enum weston_led leds);
+
+struct evdev_device *
+evdev_device_create(struct libinput_device *libinput_device,
+		    struct weston_seat *seat);
+
+int
+evdev_device_process_event(struct libinput_event *event);
+
+void
+evdev_device_set_output(struct evdev_device *device,
+			struct weston_output *output);
+void
+evdev_device_destroy(struct evdev_device *device);
+
+void
+evdev_notify_keyboard_focus(struct weston_seat *seat,
+			    struct wl_list *evdev_devices);
+
+int
+dispatch_libinput(struct libinput *libinput);
+
+#endif /* _LIBINPUT_DEVICE_H_ */
diff --git a/src/libinput-seat.c b/src/libinput-seat.c
new file mode 100644
index 0000000..b6adc76
--- /dev/null
+++ b/src/libinput-seat.c
@@ -0,0 +1,353 @@
+/*
+ * Copyright © 2013 Intel Corporation
+ * Copyright © 2013 Jonas Ådahl
+ *
+ * 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 <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <libinput.h>
+#include <libudev.h>
+
+#include "compositor.h"
+#include "launcher-util.h"
+#include "libinput-seat.h"
+#include "libinput-device.h"
+
+static const char default_seat[] = "seat0";
+static const char default_seat_name[] = "default";
+
+static void
+process_events(struct udev_input *input);
+static struct udev_seat *
+udev_seat_create(struct udev_input *input, const char *seat_name);
+static void
+udev_seat_destroy(struct udev_seat *seat);
+
+static void
+device_added(struct udev_input *input, struct libinput_device *libinput_device)
+{
+	struct weston_compositor *c;
+	struct evdev_device *device;
+	struct weston_output *output;
+	const char *seat_name;
+	const char *output_name;
+	struct libinput_seat *libinput_seat;
+	struct weston_seat *seat;
+	struct udev_seat *udev_seat;
+
+	c = input->compositor;
+	libinput_seat = libinput_device_get_seat(libinput_device);
+
+	seat_name = libinput_seat_get_logical_name(libinput_seat);
+	udev_seat = udev_seat_get_named(input, seat_name);
+	if (!udev_seat)
+		return;
+
+	seat = &udev_seat->base;
+	device = evdev_device_create(libinput_device, seat);
+	if (device == NULL)
+		return;
+
+	udev_seat = (struct udev_seat *) seat;
+	wl_list_insert(udev_seat->devices_list.prev, &device->link);
+
+	if (seat->output && seat->pointer)
+		weston_pointer_clamp(seat->pointer,
+				     &seat->pointer->x,
+				     &seat->pointer->y);
+
+	output_name = libinput_device_get_output_name(libinput_device);
+	if (output_name) {
+		device->output_name = strdup(output_name);
+		wl_list_for_each(output, &c->output_list, link)
+			if (strcmp(output->name, device->output_name) == 0)
+				evdev_device_set_output(device, output);
+	} else if (device->output == NULL) {
+		output = container_of(c->output_list.next,
+				      struct weston_output, link);
+		evdev_device_set_output(device, output);
+	}
+
+	if (!input->suspended)
+		weston_seat_repick(seat);
+}
+
+static void
+udev_seat_remove_devices(struct udev_seat *seat)
+{
+	struct evdev_device *device, *next;
+
+	wl_list_for_each_safe(device, next, &seat->devices_list, link) {
+		evdev_device_destroy(device);
+	}
+}
+
+void
+udev_input_disable(struct udev_input *input)
+{
+	if (input->suspended)
+		return;
+
+	libinput_suspend(input->libinput);
+	process_events(input);
+	input->suspended = 1;
+}
+
+static int
+udev_input_process_event(struct libinput_event *event)
+{
+	struct libinput *libinput = libinput_event_get_context(event);
+	struct libinput_device *libinput_device =
+		libinput_event_get_device(event);
+	struct udev_input *input = libinput_get_user_data(libinput);
+	struct evdev_device *device;
+	int handled = 1;
+
+	switch (libinput_event_get_type(event)) {
+	case LIBINPUT_EVENT_DEVICE_ADDED:
+		device_added(input, libinput_device);
+		break;
+	case LIBINPUT_EVENT_DEVICE_REMOVED:
+		device = libinput_device_get_user_data(libinput_device);
+		evdev_device_destroy(device);
+		break;
+	default:
+		handled = 0;
+	}
+
+	return handled;
+}
+
+static void
+process_event(struct libinput_event *event)
+{
+	if (udev_input_process_event(event))
+		return;
+	if (evdev_device_process_event(event))
+		return;
+}
+
+static void
+process_events(struct udev_input *input)
+{
+	struct libinput_event *event;
+
+	while ((event = libinput_get_event(input->libinput))) {
+		process_event(event);
+		libinput_event_destroy(event);
+	}
+}
+
+static int
+udev_input_dispatch(struct udev_input *input)
+{
+	if (libinput_dispatch(input->libinput) != 0)
+		weston_log("libinput: Failed to dispatch libinput\n");
+
+	process_events(input);
+
+	return 0;
+}
+
+static int
+libinput_source_dispatch(int fd, uint32_t mask, void *data)
+{
+	struct udev_input *input = data;
+
+	return udev_input_dispatch(input) != 0;
+}
+
+static int
+open_restricted(const char *path, int flags, void *user_data)
+{
+	struct udev_input *input = user_data;
+	struct weston_launcher *launcher = input->compositor->launcher;
+
+	return weston_launcher_open(launcher, path, flags);
+}
+
+static void
+close_restricted(int fd, void *user_data)
+{
+	struct udev_input *input = user_data;
+	struct weston_launcher *launcher = input->compositor->launcher;
+
+	weston_launcher_close(launcher, fd);
+}
+
+const struct libinput_interface libinput_interface = {
+	open_restricted,
+	close_restricted,
+};
+
+int
+udev_input_enable(struct udev_input *input)
+{
+	struct wl_event_loop *loop;
+	struct weston_compositor *c = input->compositor;
+	int fd;
+	struct udev_seat *seat;
+	int devices_found = 0;
+
+	loop = wl_display_get_event_loop(c->wl_display);
+	fd = libinput_get_fd(input->libinput);
+	input->libinput_source =
+		wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
+				     libinput_source_dispatch, input);
+	if (!input->libinput_source) {
+		return -1;
+	}
+
+	if (input->suspended) {
+		if (libinput_resume(input->libinput) != 0) {
+			wl_event_source_remove(input->libinput_source);
+			input->libinput_source = NULL;
+			return -1;
+		}
+		input->suspended = 0;
+		process_events(input);
+	}
+
+	wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
+		evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
+
+		if (!wl_list_empty(&seat->devices_list))
+			devices_found = 1;
+	}
+
+	if (devices_found == 0) {
+		weston_log(
+			"warning: no input devices on entering Weston. "
+			"Possible causes:\n"
+			"\t- no permissions to read /dev/input/event*\n"
+			"\t- seats misconfigured "
+			"(Weston backend option 'seat', "
+			"udev device property ID_SEAT)\n");
+		return -1;
+	}
+
+	return 0;
+}
+
+int
+udev_input_init(struct udev_input *input, struct weston_compositor *c, struct udev *udev,
+		const char *seat_id)
+{
+	memset(input, 0, sizeof *input);
+
+	input->compositor = c;
+
+	input->libinput = libinput_udev_create_for_seat(&libinput_interface, input,
+							udev, seat_id);
+	if (!input->libinput) {
+		return -1;
+	}
+	process_events(input);
+
+	return udev_input_enable(input);
+}
+
+void
+udev_input_destroy(struct udev_input *input)
+{
+	struct udev_seat *seat, *next;
+
+	wl_event_source_remove(input->libinput_source);
+	wl_list_for_each_safe(seat, next, &input->compositor->seat_list, base.link)
+		udev_seat_destroy(seat);
+	libinput_destroy(input->libinput);
+}
+
+static void
+udev_seat_led_update(struct weston_seat *seat_base, enum weston_led leds)
+{
+	struct udev_seat *seat = (struct udev_seat *) seat_base;
+	struct evdev_device *device;
+
+	wl_list_for_each(device, &seat->devices_list, link)
+		evdev_led_update(device, leds);
+}
+
+static void
+notify_output_create(struct wl_listener *listener, void *data)
+{
+	struct udev_seat *seat = container_of(listener, struct udev_seat,
+					      output_create_listener);
+	struct evdev_device *device;
+	struct weston_output *output = data;
+
+	wl_list_for_each(device, &seat->devices_list, link)
+		if (device->output_name &&
+		    strcmp(output->name, device->output_name) == 0) {
+			evdev_device_set_output(device, output);
+			break;
+		}
+}
+
+static struct udev_seat *
+udev_seat_create(struct udev_input *input, const char *seat_name)
+{
+	struct weston_compositor *c = input->compositor;
+	struct udev_seat *seat;
+
+	seat = zalloc(sizeof *seat);
+
+	if (!seat)
+		return NULL;
+	weston_seat_init(&seat->base, c, seat_name);
+	seat->base.led_update = udev_seat_led_update;
+
+	seat->output_create_listener.notify = notify_output_create;
+	wl_signal_add(&c->output_created_signal,
+		      &seat->output_create_listener);
+
+	wl_list_init(&seat->devices_list);
+
+	return seat;
+}
+
+static void
+udev_seat_destroy(struct udev_seat *seat)
+{
+	udev_seat_remove_devices(seat);
+	if (seat->base.keyboard)
+		notify_keyboard_focus_out(&seat->base);
+	weston_seat_release(&seat->base);
+	wl_list_remove(&seat->output_create_listener.link);
+	free(seat);
+}
+
+struct udev_seat *
+udev_seat_get_named(struct udev_input *input, const char *seat_name)
+{
+	struct udev_seat *seat;
+
+	wl_list_for_each(seat, &input->compositor->seat_list, base.link) {
+		if (strcmp(seat->base.seat_name, seat_name) == 0)
+			return seat;
+	}
+
+	return udev_seat_create(input, seat_name);
+}
diff --git a/src/libinput-seat.h b/src/libinput-seat.h
new file mode 100644
index 0000000..c448da0
--- /dev/null
+++ b/src/libinput-seat.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright © 2013 Intel Corporation
+ * Copyright © 2013 Jonas Ådahl
+ *
+ * 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.
+ */
+
+#ifndef _LIBINPUT_SEAT_H_
+#define _LIBINPUT_SEAT_H_
+
+#include "config.h"
+
+#include <libudev.h>
+
+#include "compositor.h"
+
+struct udev_seat {
+	struct weston_seat base;
+	struct wl_list devices_list;
+	struct wl_listener output_create_listener;
+};
+
+struct udev_input {
+	struct libinput *libinput;
+	struct wl_event_source *libinput_source;
+	struct weston_compositor *compositor;
+	int suspended;
+};
+
+int
+udev_input_enable(struct udev_input *input);
+void
+udev_input_disable(struct udev_input *input);
+int
+udev_input_init(struct udev_input *input,
+		struct weston_compositor *c,
+		struct udev *udev,
+		const char *seat_id);
+void
+udev_input_destroy(struct udev_input *input);
+
+struct udev_seat *
+udev_seat_get_named(struct udev_input *u,
+		    const char *seat_name);
+
+#endif
diff --git a/src/udev-input.h b/src/udev-input.h
new file mode 100644
index 0000000..e5f3d9d
--- /dev/null
+++ b/src/udev-input.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright © 2014 Jonas Ådahl
+ *
+ * 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.
+ */
+
+#ifndef _UDEV_INPUT_H_
+#define _UDEV_INPUT_H_
+
+#include "config.h"
+
+#ifdef BUILD_LIBINPUT_BACKEND
+#include "libinput-seat.h"
+#else
+#include "udev-seat.h"
+#endif
+
+#endif
-- 
1.8.3.2



More information about the wayland-devel mailing list