linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] HID: magicmouse: enable high-resolution scroll
@ 2021-07-07 15:58 José Expósito
  2021-07-07 15:58 ` [PATCH 2/2] HID: magicmouse: high-resolution scroll threshold José Expósito
  2021-07-15 19:17 ` [PATCH 1/2] HID: magicmouse: enable high-resolution scroll Jiri Kosina
  0 siblings, 2 replies; 3+ messages in thread
From: José Expósito @ 2021-07-07 15:58 UTC (permalink / raw)
  To: jikos
  Cc: benjamin.tissoires, linux-input, linux-kernel, José Expósito

The Magic Mouse, generations 1 and 2, doesn't have a physical scroll
wheel, instead, the REL_WHEEL and REL_HWHEEL events are emulated
when sliding a finger on the surface of the mouse.
However, the smooth movement of the finger is transformed into a step
based scroll on the screen, leading to a suboptimal user experience.

Emulate high-resolution scroll by sending REL_WHEEL_HI_RES and
REL_HWHEEL_HI_RES events so the scroll on the screen is closer to the
finger movement.

Signed-off-by: José Expósito <jose.exposito89@gmail.com>
---
 drivers/hid/hid-magicmouse.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c
index 8bcaee4ccae0..e95f46dab4ad 100644
--- a/drivers/hid/hid-magicmouse.c
+++ b/drivers/hid/hid-magicmouse.c
@@ -68,6 +68,9 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
 #define TOUCH_STATE_START 0x30
 #define TOUCH_STATE_DRAG  0x40
 
+/* Number of high-resolution events for each low-resolution detent. */
+#define SCROLL_HR_STEPS 10
+#define SCROLL_HR_MULT (120 / SCROLL_HR_STEPS)
 #define SCROLL_ACCEL_DEFAULT 7
 
 /* Touch surface information. Dimension is in hundredths of a mm, min and max
@@ -126,6 +129,8 @@ struct magicmouse_sc {
 		short y;
 		short scroll_x;
 		short scroll_y;
+		short scroll_x_hr;
+		short scroll_y_hr;
 		u8 size;
 	} touches[16];
 	int tracking_ids[16];
@@ -248,12 +253,18 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
 		unsigned long now = jiffies;
 		int step_x = msc->touches[id].scroll_x - x;
 		int step_y = msc->touches[id].scroll_y - y;
+		int step_hr = ((64 - (int)scroll_speed) * msc->scroll_accel) /
+			      SCROLL_HR_STEPS;
+		int step_x_hr = msc->touches[id].scroll_x_hr - x;
+		int step_y_hr = msc->touches[id].scroll_y_hr - y;
 
 		/* Calculate and apply the scroll motion. */
 		switch (state) {
 		case TOUCH_STATE_START:
 			msc->touches[id].scroll_x = x;
 			msc->touches[id].scroll_y = y;
+			msc->touches[id].scroll_x_hr = x;
+			msc->touches[id].scroll_y_hr = y;
 
 			/* Reset acceleration after half a second. */
 			if (scroll_acceleration && time_before(now,
@@ -280,6 +291,24 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
 				msc->scroll_jiffies = now;
 				input_report_rel(input, REL_WHEEL, step_y);
 			}
+
+			step_x_hr /= step_hr;
+			if (step_x_hr != 0) {
+				msc->touches[id].scroll_x_hr -= step_x_hr *
+					step_hr;
+				input_report_rel(input,
+						 REL_HWHEEL_HI_RES,
+						 -step_x_hr * SCROLL_HR_MULT);
+			}
+
+			step_y_hr /= step_hr;
+			if (step_y_hr != 0) {
+				msc->touches[id].scroll_y_hr -= step_y_hr *
+					step_hr;
+				input_report_rel(input,
+						 REL_WHEEL_HI_RES,
+						 step_y_hr * SCROLL_HR_MULT);
+			}
 			break;
 		}
 	}
@@ -481,6 +510,8 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd
 		if (emulate_scroll_wheel) {
 			__set_bit(REL_WHEEL, input->relbit);
 			__set_bit(REL_HWHEEL, input->relbit);
+			__set_bit(REL_WHEEL_HI_RES, input->relbit);
+			__set_bit(REL_HWHEEL_HI_RES, input->relbit);
 		}
 	} else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) {
 		/* setting the device name to ensure the same driver settings
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] HID: magicmouse: high-resolution scroll threshold
  2021-07-07 15:58 [PATCH 1/2] HID: magicmouse: enable high-resolution scroll José Expósito
@ 2021-07-07 15:58 ` José Expósito
  2021-07-15 19:17 ` [PATCH 1/2] HID: magicmouse: enable high-resolution scroll Jiri Kosina
  1 sibling, 0 replies; 3+ messages in thread
From: José Expósito @ 2021-07-07 15:58 UTC (permalink / raw)
  To: jikos
  Cc: benjamin.tissoires, linux-input, linux-kernel, José Expósito

In order to avoid triggering involuntary high-resolution scroll events
due to tiny touch movement deltas, add a movement threshold.

The value chosen for the threshold, about 1.5 ~ 2 mm, is similar to the
threshold used on touchpads by libinput (see libinput
evdev-mt-touchpad-gestures.c) to try to keep the scroll experience
consistent.

Signed-off-by: José Expósito <jose.exposito89@gmail.com>
---
 drivers/hid/hid-magicmouse.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c
index e95f46dab4ad..686788ebf3e1 100644
--- a/drivers/hid/hid-magicmouse.c
+++ b/drivers/hid/hid-magicmouse.c
@@ -71,6 +71,7 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
 /* Number of high-resolution events for each low-resolution detent. */
 #define SCROLL_HR_STEPS 10
 #define SCROLL_HR_MULT (120 / SCROLL_HR_STEPS)
+#define SCROLL_HR_THRESHOLD 90 /* units */
 #define SCROLL_ACCEL_DEFAULT 7
 
 /* Touch surface information. Dimension is in hundredths of a mm, min and max
@@ -132,6 +133,8 @@ struct magicmouse_sc {
 		short scroll_x_hr;
 		short scroll_y_hr;
 		u8 size;
+		bool scroll_x_active;
+		bool scroll_y_active;
 	} touches[16];
 	int tracking_ids[16];
 
@@ -265,6 +268,8 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
 			msc->touches[id].scroll_y = y;
 			msc->touches[id].scroll_x_hr = x;
 			msc->touches[id].scroll_y_hr = y;
+			msc->touches[id].scroll_x_active = false;
+			msc->touches[id].scroll_y_active = false;
 
 			/* Reset acceleration after half a second. */
 			if (scroll_acceleration && time_before(now,
@@ -292,8 +297,16 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
 				input_report_rel(input, REL_WHEEL, step_y);
 			}
 
+			if (!msc->touches[id].scroll_x_active &&
+			    abs(step_x_hr) > SCROLL_HR_THRESHOLD) {
+				msc->touches[id].scroll_x_active = true;
+				msc->touches[id].scroll_x_hr = x;
+				step_x_hr = 0;
+			}
+
 			step_x_hr /= step_hr;
-			if (step_x_hr != 0) {
+			if (step_x_hr != 0 &&
+			    msc->touches[id].scroll_x_active) {
 				msc->touches[id].scroll_x_hr -= step_x_hr *
 					step_hr;
 				input_report_rel(input,
@@ -301,8 +314,16 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
 						 -step_x_hr * SCROLL_HR_MULT);
 			}
 
+			if (!msc->touches[id].scroll_y_active &&
+			    abs(step_y_hr) > SCROLL_HR_THRESHOLD) {
+				msc->touches[id].scroll_y_active = true;
+				msc->touches[id].scroll_y_hr = y;
+				step_y_hr = 0;
+			}
+
 			step_y_hr /= step_hr;
-			if (step_y_hr != 0) {
+			if (step_y_hr != 0 &&
+			    msc->touches[id].scroll_y_active) {
 				msc->touches[id].scroll_y_hr -= step_y_hr *
 					step_hr;
 				input_report_rel(input,
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] HID: magicmouse: enable high-resolution scroll
  2021-07-07 15:58 [PATCH 1/2] HID: magicmouse: enable high-resolution scroll José Expósito
  2021-07-07 15:58 ` [PATCH 2/2] HID: magicmouse: high-resolution scroll threshold José Expósito
@ 2021-07-15 19:17 ` Jiri Kosina
  1 sibling, 0 replies; 3+ messages in thread
From: Jiri Kosina @ 2021-07-15 19:17 UTC (permalink / raw)
  To: José Expósito; +Cc: benjamin.tissoires, linux-input, linux-kernel

On Wed, 7 Jul 2021, José Expósito wrote:

> The Magic Mouse, generations 1 and 2, doesn't have a physical scroll
> wheel, instead, the REL_WHEEL and REL_HWHEEL events are emulated
> when sliding a finger on the surface of the mouse.
> However, the smooth movement of the finger is transformed into a step
> based scroll on the screen, leading to a suboptimal user experience.
> 
> Emulate high-resolution scroll by sending REL_WHEEL_HI_RES and
> REL_HWHEEL_HI_RES events so the scroll on the screen is closer to the
> finger movement.
> 
> Signed-off-by: José Expósito <jose.exposito89@gmail.com>

I have queued the series for 5.15. Thanks José,

-- 
Jiri Kosina
SUSE Labs


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-07-15 19:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-07 15:58 [PATCH 1/2] HID: magicmouse: enable high-resolution scroll José Expósito
2021-07-07 15:58 ` [PATCH 2/2] HID: magicmouse: high-resolution scroll threshold José Expósito
2021-07-15 19:17 ` [PATCH 1/2] HID: magicmouse: enable high-resolution scroll Jiri Kosina

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).