[PATCH libinput v2 3/9] core: Add internal event notification mechanism

Hans de Goede hdegoede at redhat.com
Sun Sep 28 04:21:02 PDT 2014


For features like e.g. disable-touchpad-while-typing, it is necessary for one
device to be able to listen into another device's events.

It is tempting to use the existing device_added / device_removed mechanism
to give e.g. the keyboard a link to the touchpad, and make the keyboard code
disable / re-enable the touchpad but this wrong. This needs to be a setting
of the touchpad, and the policy for things like which events to count as
activity, and what sort of timeout to use to consider the device idle, belongs
in the touchpad code not in the keyboard code.

Add an event listeners mechanism so that the touchpad can listen for (e.g.)
keyboard events, and respond to these itself.

Signed-off-by: Hans de Goede <hdegoede at redhat.com>
Reviewed-by: Peter Hutterer <peter.hutterer at who-t.net>

--
Changes in v2: s/eventlistener/event_listener/
---
 src/libinput-private.h | 19 +++++++++++++++++++
 src/libinput.c         | 28 ++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/src/libinput-private.h b/src/libinput-private.h
index 3794520..c8b65d9 100644
--- a/src/libinput-private.h
+++ b/src/libinput-private.h
@@ -144,12 +144,19 @@ struct libinput_device_config {
 struct libinput_device {
 	struct libinput_seat *seat;
 	struct list link;
+	struct list event_listeners;
 	void *user_data;
 	int terminated;
 	int refcount;
 	struct libinput_device_config config;
 };
 
+struct libinput_event_listener {
+	struct list link;
+	void (*notify_func)(uint64_t time, struct libinput_event *ev, void *notify_func_data);
+	void *notify_func_data;
+};
+
 typedef void (*libinput_source_dispatch_t)(void *data);
 
 
@@ -206,6 +213,18 @@ libinput_device_init(struct libinput_device *device,
 		     struct libinput_seat *seat);
 
 void
+libinput_device_add_event_listener(struct libinput_device *device,
+				   struct libinput_event_listener *listener,
+				   void (*notify_func)(
+						uint64_t time,
+						struct libinput_event *event,
+						void *notify_func_data),
+				   void *notify_func_data);
+
+void
+libinput_device_remove_event_listener(struct libinput_event_listener *listener);
+
+void
 notify_added_device(struct libinput_device *device);
 
 void
diff --git a/src/libinput.c b/src/libinput.c
index 2f172ef..f55dfad 100644
--- a/src/libinput.c
+++ b/src/libinput.c
@@ -674,6 +674,7 @@ libinput_device_init(struct libinput_device *device,
 {
 	device->seat = seat;
 	device->refcount = 1;
+	list_init(&device->event_listeners);
 }
 
 LIBINPUT_EXPORT struct libinput_device *
@@ -686,6 +687,7 @@ libinput_device_ref(struct libinput_device *device)
 static void
 libinput_device_destroy(struct libinput_device *device)
 {
+	assert(list_empty(&device->event_listeners));
 	evdev_device_destroy((struct evdev_device *) device);
 }
 
@@ -732,6 +734,26 @@ libinput_dispatch(struct libinput *libinput)
 	return 0;
 }
 
+void
+libinput_device_add_event_listener(struct libinput_device *device,
+				   struct libinput_event_listener *listener,
+				   void (*notify_func)(
+						uint64_t time,
+						struct libinput_event *event,
+						void *notify_func_data),
+				   void *notify_func_data)
+{
+	listener->notify_func = notify_func;
+	listener->notify_func_data = notify_func_data;
+	list_insert(&device->event_listeners, &listener->link);
+}
+
+void
+libinput_device_remove_event_listener(struct libinput_event_listener *listener)
+{
+	list_remove(&listener->link);
+}
+
 static uint32_t
 update_seat_key_count(struct libinput_seat *seat,
 		      int32_t key,
@@ -799,7 +821,13 @@ post_device_event(struct libinput_device *device,
 		  enum libinput_event_type type,
 		  struct libinput_event *event)
 {
+	struct libinput_event_listener *listener, *tmp;
+
 	init_event_base(event, device, type);
+
+	list_for_each_safe(listener, tmp, &device->event_listeners, link)
+		listener->notify_func(time, event, listener->notify_func_data);
+
 	libinput_post_event(device->seat->libinput, event);
 }
 
-- 
2.1.0



More information about the wayland-devel mailing list