From ad2acca72d501c57a19380f4d73e05c47396f88b Mon Sep 17 00:00:00 2001 From: Hans de Goede Date: Sun, 28 Sep 2014 13:21:02 +0200 Subject: [PATCH] core: Add internal event notification mechanism 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 is 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 Reviewed-by: Peter Hutterer Signed-off-by: Peter Hutterer --- 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 37945202..c8b65d9b 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); @@ -205,6 +212,18 @@ void 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); diff --git a/src/libinput.c b/src/libinput.c index 2f172efb..f55dfad6 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); }