Input filters
Subsystems (4)
Dmitry Torokhov
maintainercover Input filters
Hi,
The following 2 patches implement a limited version of input filters which allow suppressing certain events from reaching regular input handlers/user space.
The name inspired by the recent patch from Matthew, although implementation and intended usage quite different - Matthew needs to filter non-input (battery, wlan, AC adapter state) from the raw data stream emitted by i8042 whereas input filters are supposed to filter (and maybe replace) _input_ events, most likely without regard as to which device emitted them.
The first (and quite possible only) user of the filter facility is Mac mouse button emulation which user to be hacked into the keyboard driver. The setup worked until keyboard was the sole source of key events but now that X is switching to evdev (which is completely ignorant of mac emulation) user space started getting the original key presses along with the emulated buttons. Input filters solve the issue while encapsulating everything in drivers/macintosh and removing dependencies between mac_hid, keyboard and evdev.
Signature · 1 line
Dmitry
Dmitry Torokhov
maintainerPATCH 1/2 Input: implement input filters
Sometimes it is desirable to suppress certain events from reaching input handlers and thus user space. One such example is Mac mouse button emulation code which catches certain key presses and converts them into button clicks as if they were emitted by a virtual mouse. The original key press events should be completely suppressed, otherwise user space will be confused, and while keyboard driver does it on its own evdev is blissfully unaware of this arrangement.
This patch adds notion of 'filter' to the standard input handlers, which may flag event as filtered thus preventing it from reaching other input handlers. Filters don't (nor will they ever) have a notion of priority relative to each other, input core will run all of them first and any one of them may mark event as filtered.
This patch is inspired by similar patch by Matthew Garret but the implementation and intended usage are quite different.
---
drivers/input/input.c | 39 ++++++++++++++++++++++++++++++++------- include/linux/input.h | 8 ++++++++ 2 files changed, 40 insertions(+), 7 deletions(-)
drivers/input/input.c +32 -7
@@ -83,12 +83,14 @@ static int input_defuzz_abs_event(int value, int old_val, int fuzz) }/* -* Pass event through all open handles. This function is called with +* Pass event first through all filters and then, if event has not been +* filtered out, through all open handles. This function is called with * dev->event_lock held and interrupts disabled. */ static void input_pass_event (struct input_dev *dev,unsigned int type,unsigned int code,int value) { +struct input_handler *handler;struct input_handle *handle;rcu_read_lock ();
@@ -96,11 +98,25 @@ static void input_pass_event(struct input_dev *dev, handle =rcu_dereference (dev->grab);if (handle) handle->handler->event (handle, type, code, value); -else -list_for_each_entry_rcu (handle, &dev->h_list, d_node) -if (handle->open) - handle->handler->event (handle, - type, code, value); +else { +bool filtered = false; + +list_for_each_entry_rcu (handle, &dev->h_list, d_node) { +if (!handle->open) +continue ; + + handler = handle->handler; +if (!handler->filter) { +if (filtered) +break ; + + handler->event (handle, type, code, value); + + }else if (handler->filter (handle, type, code, value)) + filtered = true; + } + } +rcu_read_unlock (); }
@@ -903,12 +919,15 @@ static int input_handlers_seq_show(struct seq_file *seq, void *v)seq_printf (seq,"N: Number=%ld Name=%s" , (unsigned long )seq->private, handler->name); +if (handler->filter) +seq_puts (seq,"(filter)" );if (handler->fops)seq_printf (seq," Minor=%d" , handler->minor);seq_putc (seq,'\n' );return 0 ; } +static const struct seq_operations input_handlers_seq_ops = { .start = input_handlers_seq_start, .next = input_handlers_seq_next,
@@ -1588,8 +1607,14 @@ int input_register_handle(struct input_handle *handle)* which is mutually exclusive with ->disconnect() * we can't be racing with input_unregister_handle() * and so separate lock is not needed here. +* +* Filters go to the head of the list, normal handlers +* to the tail. */ -list_add_tail (&handle->h_node, &handler->h_list); +if (handler->filter) +list_add (&handle->h_node, &handler->h_list); +else +list_add_tail (&handle->h_node, &handler->h_list);if (handler->start) handler->start (handle);
include/linux/input.h +8 -0
@@ -1189,6 +1189,8 @@ struct input_handle;* @event: event handler. This method is being called by input core with * interrupts disabled and dev->event_lock spinlock held and so * it may not sleep +* @filter: similar to @event; separates normal event handlers from +* "filters". * @connect: called when attaching a handler to an input device * @disconnect: disconnects a handler from input device * @start: starts handler for given handle. This function is called by
@@ -1210,6 +1212,11 @@ struct input_handle;* same time. All of them will get their copy of input event generated by * the device. * +* The very same structure is used to implement input filters. Input core +* allows filters to run first and will not pass event to regular handlers +* if any of the filters indicate that the event should be filtered (by +* returning %true from their filter() method). +* * Note that input core serializes calls to connect() and disconnect() * methods. */
@@ -1218,6 +1225,7 @@ struct input_handler {void *private;void (*event)(struct input_handle *handle,unsigned int type,unsigned int code,int value); +bool (*filter)(struct input_handle *handle,unsigned int type,unsigned int code,int value);int (*connect)(struct input_handler *handler,struct input_dev *dev,const struct input_device_id *id);void (*disconnect)(struct input_handle *handle);void (*start)(struct input_handle *handle);
Dmitry Torokhov
maintainerPATCH 2/2 Input: Mac button emulation - implement as input filter
In reply to Dmitry TorokhovCurrent implementation of Mac mouse button emulation plugs into legacy keyboard driver, converts certain keys into button events on a separate device and suppresses the real events from reaching tty. This worked well enough until user space started using evdev which was completely unaware of this arrangement and kept sending original key presses to its users. Change the implementation to use newly added input filter framework so that original key presses are not transmitted to any handlers.
As a bonus remove SYSCTL dependencies from the code and use Kconfig instead; also do not create the emulated mouse device until user activates emulation.
---
drivers/char/keyboard.c | 5 - drivers/macintosh/Kconfig | 1 drivers/macintosh/mac_hid.c | 257 +++++++++++++++++++++++++++++++------------ include/linux/kbd_kern.h | 3 - 4 files changed, 186 insertions(+), 80 deletions(-)
drivers/char/keyboard.c +0 -5
@@ -1168,11 +1168,6 @@ static void kbd_keycode(unsigned int keycode, int down, int hw_raw) rep = (down ==2 ); -#ifdef CONFIG_MAC_EMUMOUSEBTN -if (mac_hid_mouse_emulate_buttons (1 , keycode, down)) -return ; -#endif /* CONFIG_MAC_EMUMOUSEBTN */ -if ((raw_mode = (kbd->kbdmode ==VC_RAW )) && !hw_raw)if (emulate_raw (vc, keycode, !down <<7 ))if (keycode <BTN_MISC &&printk_ratelimit ())
drivers/macintosh/Kconfig +1 -0
@@ -172,6 +172,7 @@ config INPUT_ADBHID config MAC_EMUMOUSEBTN bool "Support for mouse button 2+3 emulation" + depends on SYSCTL select INPUT help This provides generic support for emulating the 2nd and 3rd mouse
drivers/macintosh/mac_hid.c +185 -72
296 lines not on this page · raw message
include/linux/kbd_kern.h +0 -3
@@ -161,7 +161,4 @@ static inline void con_schedule_flip(struct tty_struct *t) schedule_delayed_work(&t->buf.work, 0); } -/* mac_hid.c */ -extern int mac_hid_mouse_emulate_buttons (int ,unsigned int ,int ); -#endif