Skip to content

Commit d469647

Browse files
committed
Input: simplify event handling logic
Streamline event handling code by providing batch implementations for filtering and event processing and using them in place of the main event handler, as needed, instead of having complex branching logic in the middle of the event processing code. Reviewed-by: Jeff LaBundy <jeff@labundy.com> Reviewed-by: Benjamin Tissoires <bentiss@kernel.org> Link: https://lore.kernel.org/r/20240703213756.3375978-5-dmitry.torokhov@gmail.com Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
1 parent 14498e9 commit d469647

1 file changed

Lines changed: 68 additions & 41 deletions

File tree

drivers/input/input.c

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -99,45 +99,13 @@ static void input_stop_autorepeat(struct input_dev *dev)
9999
del_timer(&dev->timer);
100100
}
101101

102-
/*
103-
* Pass event first through all filters and then, if event has not been
104-
* filtered out, through all open handles. This function is called with
105-
* dev->event_lock held and interrupts disabled.
106-
*/
107-
static unsigned int input_to_handler(struct input_handle *handle,
108-
struct input_value *vals, unsigned int count)
109-
{
110-
struct input_handler *handler = handle->handler;
111-
struct input_value *end = vals;
112-
struct input_value *v;
113-
114-
if (handler->filter) {
115-
for (v = vals; v != vals + count; v++) {
116-
if (handler->filter(handle, v->type, v->code, v->value))
117-
continue;
118-
if (end != v)
119-
*end = *v;
120-
end++;
121-
}
122-
count = end - vals;
123-
}
124-
125-
if (!count)
126-
return 0;
127-
128-
if (handler->events)
129-
handler->events(handle, vals, count);
130-
else if (handler->event)
131-
for (v = vals; v != vals + count; v++)
132-
handler->event(handle, v->type, v->code, v->value);
133-
134-
return count;
135-
}
136-
137102
/*
138103
* Pass values first through all filters and then, if event has not been
139-
* filtered out, through all open handles. This function is called with
140-
* dev->event_lock held and interrupts disabled.
104+
* filtered out, through all open handles. This order is achieved by placing
105+
* filters at the head of the list of handles attached to the device, and
106+
* placing regular handles at the tail of the list.
107+
*
108+
* This function is called with dev->event_lock held and interrupts disabled.
141109
*/
142110
static void input_pass_values(struct input_dev *dev,
143111
struct input_value *vals, unsigned int count)
@@ -154,11 +122,12 @@ static void input_pass_values(struct input_dev *dev,
154122

155123
handle = rcu_dereference(dev->grab);
156124
if (handle) {
157-
count = input_to_handler(handle, vals, count);
125+
count = handle->handler->events(handle, vals, count);
158126
} else {
159127
list_for_each_entry_rcu(handle, &dev->h_list, d_node)
160128
if (handle->open) {
161-
count = input_to_handler(handle, vals, count);
129+
count = handle->handler->events(handle, vals,
130+
count);
162131
if (!count)
163132
break;
164133
}
@@ -2537,6 +2506,57 @@ static int input_handler_check_methods(const struct input_handler *handler)
25372506
return 0;
25382507
}
25392508

2509+
/*
2510+
* An implementation of input_handler's events() method that simply
2511+
* invokes handler->event() method for each event one by one.
2512+
*/
2513+
static unsigned int input_handler_events_default(struct input_handle *handle,
2514+
struct input_value *vals,
2515+
unsigned int count)
2516+
{
2517+
struct input_handler *handler = handle->handler;
2518+
struct input_value *v;
2519+
2520+
for (v = vals; v != vals + count; v++)
2521+
handler->event(handle, v->type, v->code, v->value);
2522+
2523+
return count;
2524+
}
2525+
2526+
/*
2527+
* An implementation of input_handler's events() method that invokes
2528+
* handler->filter() method for each event one by one and removes events
2529+
* that were filtered out from the "vals" array.
2530+
*/
2531+
static unsigned int input_handler_events_filter(struct input_handle *handle,
2532+
struct input_value *vals,
2533+
unsigned int count)
2534+
{
2535+
struct input_handler *handler = handle->handler;
2536+
struct input_value *end = vals;
2537+
struct input_value *v;
2538+
2539+
for (v = vals; v != vals + count; v++) {
2540+
if (handler->filter(handle, v->type, v->code, v->value))
2541+
continue;
2542+
if (end != v)
2543+
*end = *v;
2544+
end++;
2545+
}
2546+
2547+
return end - vals;
2548+
}
2549+
2550+
/*
2551+
* An implementation of input_handler's events() method that does nothing.
2552+
*/
2553+
static unsigned int input_handler_events_null(struct input_handle *handle,
2554+
struct input_value *vals,
2555+
unsigned int count)
2556+
{
2557+
return count;
2558+
}
2559+
25402560
/**
25412561
* input_register_handler - register a new input handler
25422562
* @handler: handler to be registered
@@ -2554,12 +2574,19 @@ int input_register_handler(struct input_handler *handler)
25542574
if (error)
25552575
return error;
25562576

2577+
INIT_LIST_HEAD(&handler->h_list);
2578+
2579+
if (handler->filter)
2580+
handler->events = input_handler_events_filter;
2581+
else if (handler->event)
2582+
handler->events = input_handler_events_default;
2583+
else if (!handler->events)
2584+
handler->events = input_handler_events_null;
2585+
25572586
error = mutex_lock_interruptible(&input_mutex);
25582587
if (error)
25592588
return error;
25602589

2561-
INIT_LIST_HEAD(&handler->h_list);
2562-
25632590
list_add_tail(&handler->node, &input_handler_list);
25642591

25652592
list_for_each_entry(dev, &input_dev_list, node)

0 commit comments

Comments
 (0)