[PATCH libinput 4/6] path: add libinput_path_create_context instead of libinput_create_from_path

Peter Hutterer peter.hutterer at who-t.net
Wed Feb 5 20:13:08 PST 2014


Creates an empty context that is not hooked up to a device. Callers can then
add and remove devices to this context using libinput_path_add_device() and
libinput_path_remove_device().

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 src/libinput.h      | 19 ++++++++++---------
 src/path.c          | 29 +++--------------------------
 test/litest.c       |  7 ++++++-
 test/litest.h       |  1 +
 test/path.c         | 50 ++++++++++++++++++++++++++++++++++----------------
 tools/event-debug.c | 12 +++++++++++-
 6 files changed, 65 insertions(+), 53 deletions(-)

diff --git a/src/libinput.h b/src/libinput.h
index e1d1ffb..38a5a52 100644
--- a/src/libinput.h
+++ b/src/libinput.h
@@ -706,25 +706,26 @@ libinput_create_from_udev(const struct libinput_interface *interface,
 			  void *user_data,
 			  struct udev *udev,
 			  const char *seat_id);
+
 /**
  * @ingroup base
  *
- * Create a new libinput context from the given path. This context
- * represents one single device only, it will not respond to new devices
- * being added and reading from the device after it was removed will fail.
+ * Create a new libinput context that requires the caller to manually add or
+ * remove devices with libinput_path_add_device() and
+ * libinput_path_remove_device().
+ *
+ * The context is fully initialized but will not generate events until at
+ * least one device has been added.
  *
  * @param interface The callback interface
  * @param user_data Caller-specific data passed to the various callback
  * interfaces.
- * @param path Path to an input device
  *
- * @return An initialized libinput context, ready to handle events or NULL on
- * error.
+ * @return An initialized, empty libinput context.
  */
 struct libinput *
-libinput_create_from_path(const struct libinput_interface *interface,
-			  void *user_data,
-			  const char *path);
+libinput_path_create_context(const struct libinput_interface *interface,
+			     void *user_data);
 
 /**
  * @ingroup base
diff --git a/src/path.c b/src/path.c
index a5b3338..a0870c2 100644
--- a/src/path.c
+++ b/src/path.c
@@ -242,49 +242,26 @@ static const struct libinput_interface_backend interface_backend = {
 };
 
 LIBINPUT_EXPORT struct libinput *
-libinput_create_from_path(const struct libinput_interface *interface,
-			  void *user_data,
-			  const char *path)
+libinput_path_create_context(const struct libinput_interface *interface,
+			     void *user_data)
 {
 	struct path_input *input;
-	struct path_device *dev;
 
-	if (!interface || !path)
+	if (!interface)
 		return NULL;
 
 	input = zalloc(sizeof *input);
 	if (!input)
 		return NULL;
 
-	dev = zalloc(sizeof *dev);
-	if (!dev) {
-		free(input);
-		return NULL;
-	}
-
 	if (libinput_init(&input->base, interface,
 			  &interface_backend, user_data) != 0) {
 		free(input);
-		free(dev);
 		return NULL;
 	}
 
 	list_init(&input->path_list);
 
-	dev->path = strdup(path);
-	if (!dev->path) {
-		free(input);
-		free(dev);
-		return NULL;
-	}
-
-	list_insert(&input->path_list, &dev->link);
-
-	if (path_input_enable(&input->base) < 0) {
-		libinput_destroy(&input->base);
-		return NULL;
-	}
-
 	return &input->base;
 }
 
diff --git a/test/litest.c b/test/litest.c
index 216e1a0..d64974a 100644
--- a/test/litest.c
+++ b/test/litest.c
@@ -356,9 +356,13 @@ litest_create_device(enum litest_device_type which)
 	rc = libevdev_new_from_fd(fd, &d->evdev);
 	ck_assert_int_eq(rc, 0);
 
-	d->libinput = libinput_create_from_path(&interface, NULL, path);
+	d->libinput = libinput_path_create_context(&interface, NULL);
 	ck_assert(d->libinput != NULL);
 
+	d->libinput_device = libinput_path_add_device(d->libinput, path);
+	ck_assert(d->libinput_device != NULL);
+	libinput_device_ref(d->libinput_device);
+
 	d->interface->min[ABS_X] = libevdev_get_abs_minimum(d->evdev, ABS_X);
 	d->interface->max[ABS_X] = libevdev_get_abs_maximum(d->evdev, ABS_X);
 	d->interface->min[ABS_Y] = libevdev_get_abs_minimum(d->evdev, ABS_Y);
@@ -386,6 +390,7 @@ litest_delete_device(struct litest_device *d)
 	if (!d)
 		return;
 
+	libinput_device_unref(d->libinput_device);
 	libinput_destroy(d->libinput);
 	libevdev_free(d->evdev);
 	libevdev_uinput_destroy(d->uinput);
diff --git a/test/litest.h b/test/litest.h
index 67c95e8..e3f599f 100644
--- a/test/litest.h
+++ b/test/litest.h
@@ -59,6 +59,7 @@ struct litest_device {
 	struct libevdev *evdev;
 	struct libevdev_uinput *uinput;
 	struct libinput *libinput;
+	struct libinput_device *libinput_device;
 	struct litest_device_interface *interface;
 };
 
diff --git a/test/path.c b/test/path.c
index be47175..3aade76 100644
--- a/test/path.c
+++ b/test/path.c
@@ -58,17 +58,15 @@ START_TEST(path_create_NULL)
 {
 	struct libinput *li;
 	const struct libinput_interface interface;
-	const char *path = "foo";
 
 	open_func_count = 0;
 	close_func_count = 0;
 
-	li = libinput_create_from_path(NULL, NULL, NULL);
-	ck_assert(li == NULL);
-	li = libinput_create_from_path(&interface, NULL, NULL);
-	ck_assert(li == NULL);
-	li = libinput_create_from_path(NULL, NULL, path);
+	li = libinput_path_create_context(NULL, NULL);
 	ck_assert(li == NULL);
+	li = libinput_path_create_context(&interface, NULL);
+	ck_assert(li != NULL);
+	libinput_destroy(li);
 
 	ck_assert_int_eq(open_func_count, 0);
 	ck_assert_int_eq(close_func_count, 0);
@@ -81,13 +79,16 @@ END_TEST
 START_TEST(path_create_invalid)
 {
 	struct libinput *li;
+	struct libinput_device *device;
 	const char *path = "/tmp";
 
 	open_func_count = 0;
 	close_func_count = 0;
 
-	li = libinput_create_from_path(&simple_interface, NULL, path);
-	ck_assert(li == NULL);
+	li = libinput_path_create_context(&simple_interface, NULL);
+	ck_assert(li != NULL);
+	device = libinput_path_add_device(li, path);
+	ck_assert(device == NULL);
 
 	ck_assert_int_eq(open_func_count, 0);
 	ck_assert_int_eq(close_func_count, 0);
@@ -103,6 +104,7 @@ END_TEST
 START_TEST(path_create_destroy)
 {
 	struct libinput *li;
+	struct libinput_device *device;
 	struct libevdev *evdev;
 	struct libevdev_uinput *uinput;
 	int rc;
@@ -123,10 +125,14 @@ START_TEST(path_create_destroy)
 	ck_assert_int_eq(rc, 0);
 	libevdev_free(evdev);
 
-	li = libinput_create_from_path(&simple_interface, userdata,
-				       libevdev_uinput_get_devnode(uinput));
+	li = libinput_path_create_context(&simple_interface, userdata);
 	ck_assert(li != NULL);
 	ck_assert(libinput_get_user_data(li) == userdata);
+
+	device = libinput_path_add_device(li,
+					  libevdev_uinput_get_devnode(uinput));
+	ck_assert(device != NULL);
+
 	ck_assert_int_eq(open_func_count, 1);
 
 	libevdev_uinput_destroy(uinput);
@@ -334,6 +340,7 @@ END_TEST
 START_TEST(path_suspend)
 {
 	struct libinput *li;
+	struct libinput_device *device;
 	struct libevdev *evdev;
 	struct libevdev_uinput *uinput;
 	int rc;
@@ -354,10 +361,13 @@ START_TEST(path_suspend)
 	ck_assert_int_eq(rc, 0);
 	libevdev_free(evdev);
 
-	li = libinput_create_from_path(&simple_interface, userdata,
-				       libevdev_uinput_get_devnode(uinput));
+	li = libinput_path_create_context(&simple_interface, userdata);
 	ck_assert(li != NULL);
 
+	device = libinput_path_add_device(li,
+					  libevdev_uinput_get_devnode(uinput));
+	ck_assert(device != NULL);
+
 	libinput_suspend(li);
 	libinput_resume(li);
 
@@ -372,6 +382,7 @@ END_TEST
 START_TEST(path_double_suspend)
 {
 	struct libinput *li;
+	struct libinput_device *device;
 	struct libevdev *evdev;
 	struct libevdev_uinput *uinput;
 	int rc;
@@ -392,10 +403,13 @@ START_TEST(path_double_suspend)
 	ck_assert_int_eq(rc, 0);
 	libevdev_free(evdev);
 
-	li = libinput_create_from_path(&simple_interface, userdata,
-				       libevdev_uinput_get_devnode(uinput));
+	li = libinput_path_create_context(&simple_interface, userdata);
 	ck_assert(li != NULL);
 
+	device = libinput_path_add_device(li,
+					  libevdev_uinput_get_devnode(uinput));
+	ck_assert(device != NULL);
+
 	libinput_suspend(li);
 	libinput_suspend(li);
 	libinput_resume(li);
@@ -411,6 +425,7 @@ END_TEST
 START_TEST(path_double_resume)
 {
 	struct libinput *li;
+	struct libinput_device *device;
 	struct libevdev *evdev;
 	struct libevdev_uinput *uinput;
 	int rc;
@@ -431,10 +446,13 @@ START_TEST(path_double_resume)
 	ck_assert_int_eq(rc, 0);
 	libevdev_free(evdev);
 
-	li = libinput_create_from_path(&simple_interface, userdata,
-				       libevdev_uinput_get_devnode(uinput));
+	li = libinput_path_create_context(&simple_interface, userdata);
 	ck_assert(li != NULL);
 
+	device = libinput_path_add_device(li,
+					  libevdev_uinput_get_devnode(uinput));
+	ck_assert(device != NULL);
+
 	libinput_suspend(li);
 	libinput_resume(li);
 	libinput_resume(li);
diff --git a/tools/event-debug.c b/tools/event-debug.c
index bbb358b..3e89bf1 100644
--- a/tools/event-debug.c
+++ b/tools/event-debug.c
@@ -151,11 +151,21 @@ open_udev(struct libinput **li)
 static int
 open_device(struct libinput **li, const char *path)
 {
-	*li = libinput_create_from_path(&interface, NULL, path);
+	struct libinput_device *device;
+
+	*li = libinput_path_create_context(&interface, NULL);
 	if (!*li) {
 		fprintf(stderr, "Failed to initialize context from %s\n", path);
 		return 1;
 	}
+
+	device = libinput_path_add_device(*li, path);
+	if (!device) {
+		fprintf(stderr, "Failed to initialized device %s\n", path);
+		libinput_destroy(*li);
+		return 1;
+	}
+
 	return 0;
 }
 
-- 
1.8.4.2



More information about the wayland-devel mailing list