Skip to content

Commit 1ff2e1a

Browse files
HarryCuttsJiri Kosina
authored andcommitted
HID: input: Create a utility class for counting scroll events
To avoid code duplication, this class counts high-resolution scroll movements and emits the legacy low-resolution events when appropriate. Drivers should be able to create one instance for each scroll wheel that they need to handle. Signed-off-by: Harry Cutts <hcutts@chromium.org> Reviewed-by: Benjamin Tissoires <benjamin.tissoires@redhat.com> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent aaf9978 commit 1ff2e1a

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

drivers/hid/hid-input.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,3 +1826,48 @@ void hidinput_disconnect(struct hid_device *hid)
18261826
}
18271827
EXPORT_SYMBOL_GPL(hidinput_disconnect);
18281828

1829+
/**
1830+
* hid_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
1831+
* events given a high-resolution wheel
1832+
* movement.
1833+
* @counter: a hid_scroll_counter struct describing the wheel.
1834+
* @hi_res_value: the movement of the wheel, in the mouse's high-resolution
1835+
* units.
1836+
*
1837+
* Given a high-resolution movement, this function converts the movement into
1838+
* microns and emits high-resolution scroll events for the input device. It also
1839+
* uses the multiplier from &struct hid_scroll_counter to emit low-resolution
1840+
* scroll events when appropriate for backwards-compatibility with userspace
1841+
* input libraries.
1842+
*/
1843+
void hid_scroll_counter_handle_scroll(struct hid_scroll_counter *counter,
1844+
int hi_res_value)
1845+
{
1846+
int low_res_scroll_amount;
1847+
/* Some wheels will rest 7/8ths of a notch from the previous notch
1848+
* after slow movement, so we want the threshold for low-res events to
1849+
* be in the middle of the notches (e.g. after 4/8ths) as opposed to on
1850+
* the notches themselves (8/8ths).
1851+
*/
1852+
int threshold = counter->resolution_multiplier / 2;
1853+
1854+
input_report_rel(counter->dev, REL_WHEEL_HI_RES,
1855+
hi_res_value * counter->microns_per_hi_res_unit);
1856+
1857+
counter->remainder += hi_res_value;
1858+
if (abs(counter->remainder) >= threshold) {
1859+
/* Add (or subtract) 1 because we want to trigger when the wheel
1860+
* is half-way to the next notch (i.e. scroll 1 notch after a
1861+
* 1/2 notch movement, 2 notches after a 1 1/2 notch movement,
1862+
* etc.).
1863+
*/
1864+
low_res_scroll_amount =
1865+
counter->remainder / counter->resolution_multiplier
1866+
+ (hi_res_value > 0 ? 1 : -1);
1867+
input_report_rel(counter->dev, REL_WHEEL,
1868+
low_res_scroll_amount);
1869+
counter->remainder -=
1870+
low_res_scroll_amount * counter->resolution_multiplier;
1871+
}
1872+
}
1873+
EXPORT_SYMBOL_GPL(hid_scroll_counter_handle_scroll);

include/linux/hid.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,34 @@ static inline u32 hid_report_len(struct hid_report *report)
11381138
int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,
11391139
int interrupt);
11401140

1141+
1142+
/**
1143+
* struct hid_scroll_counter - Utility class for processing high-resolution
1144+
* scroll events.
1145+
* @dev: the input device for which events should be reported.
1146+
* @microns_per_hi_res_unit: the amount moved by the user's finger for each
1147+
* high-resolution unit reported by the mouse, in
1148+
* microns.
1149+
* @resolution_multiplier: the wheel's resolution in high-resolution mode as a
1150+
* multiple of its lower resolution. For example, if
1151+
* moving the wheel by one "notch" would result in a
1152+
* value of 1 in low-resolution mode but 8 in
1153+
* high-resolution, the multiplier is 8.
1154+
* @remainder: counts the number of high-resolution units moved since the last
1155+
* low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
1156+
* only be used by class methods.
1157+
*/
1158+
struct hid_scroll_counter {
1159+
struct input_dev *dev;
1160+
int microns_per_hi_res_unit;
1161+
int resolution_multiplier;
1162+
1163+
int remainder;
1164+
};
1165+
1166+
void hid_scroll_counter_handle_scroll(struct hid_scroll_counter *counter,
1167+
int hi_res_value);
1168+
11411169
/* HID quirks API */
11421170
unsigned long hid_lookup_quirk(const struct hid_device *hdev);
11431171
int hid_quirks_init(char **quirks_param, __u16 bus, int count);

0 commit comments

Comments
 (0)