From patchwork Wed Jul 7 15:58:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= X-Patchwork-Id: 12362997 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9DE48C07E95 for ; Wed, 7 Jul 2021 15:58:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7FBE161CC3 for ; Wed, 7 Jul 2021 15:58:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231685AbhGGQBj (ORCPT ); Wed, 7 Jul 2021 12:01:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231533AbhGGQBi (ORCPT ); Wed, 7 Jul 2021 12:01:38 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D799C061574; Wed, 7 Jul 2021 08:58:58 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id j34so2168670wms.5; Wed, 07 Jul 2021 08:58:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ZjJvkP2zSgmCjg+xemLcfykoNEygSi31xcm40krHH7s=; b=fiaQN6kDXJLjqLUXL/UYd1Ht8yG0jtci7XbXnFvpFsA6mNJOZ3oWHG50DWA7EunQhK cUHBqe8kXQBgFAKYKFKOlIxleN8tQnHPKjgp7egqFjhGY+Suz+G+j4qwCKoFeMZho5w/ xfut121ooxamZmKTLjFQCLblcaTNZN5WS0If9DIr7X27YFqJdSwhu0Adwg0zgFG2XFn7 dixfzuj14pX8aC0QSS25y7Tk+EtrWs8uIAeTQkuC/ee/Jh4/ZjSEdnSIHbr2NtWR2Ymg YSconRZaP8sw8uMaXoC5eEp6dRL3IXozCel6k7I8IXsZdDseTGbQ6gLFqhTx978P1WCw bWIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ZjJvkP2zSgmCjg+xemLcfykoNEygSi31xcm40krHH7s=; b=kCURfEOaXUsE+UVCqMnuuoC/j7Be9t06dDBKb4WgzqIdCQLsv6aYj9CUx3eGWY+Z9l q5cMg05b/0FgGtZzZvOW1jLF7piqXARR5g3qEdM0ILrj5gAh9U+jsPBEYrhm883cz+0y U4WjTEX0HLrJ2PhuImMEFFGo5aon9XkDdRIefggutQM4TcXhsLZ7CKnwPY29plTme2Wo fpiFrm9Gkr5c2Thb/JZvwWs/LS1xBFCwQ89QpX8EqxdQww2uJGkB/I3AAQXW4hapb4yB vO3mvCJtyLWBjGlw6VNV+kugXdDWiN+Q2LpmJ7RJOE2a4xepCrImjDr0ok8aNzYSD3QS uNpA== X-Gm-Message-State: AOAM533cGCQFntKtieyFx6Pp3zBpGVTT3kZbz40/MOnRXbQw8NhOJW2U 4SCKw3THNbO4JuKPmt3B/80= X-Google-Smtp-Source: ABdhPJzPNu005i0+zS757F6QauF5cm9MB6FjbFyTt4b5Y2jAAuPSsHAP6CqRxnVzgC6zO5K00fccew== X-Received: by 2002:a1c:c911:: with SMTP id f17mr27649340wmb.60.1625673537018; Wed, 07 Jul 2021 08:58:57 -0700 (PDT) Received: from localhost.localdomain ([94.73.35.100]) by smtp.gmail.com with ESMTPSA id c9sm13942838wrx.22.2021.07.07.08.58.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:58:56 -0700 (PDT) From: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= To: jikos@kernel.org Cc: benjamin.tissoires@redhat.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= Subject: [PATCH 1/2] HID: magicmouse: enable high-resolution scroll Date: Wed, 7 Jul 2021 17:58:21 +0200 Message-Id: <20210707155822.7811-1-jose.exposito89@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org 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 --- 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 From patchwork Wed Jul 7 15:58:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= X-Patchwork-Id: 12362999 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ABE39C07E95 for ; Wed, 7 Jul 2021 15:59:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 935EA61CC0 for ; Wed, 7 Jul 2021 15:59:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231533AbhGGQBm (ORCPT ); Wed, 7 Jul 2021 12:01:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231933AbhGGQBl (ORCPT ); Wed, 7 Jul 2021 12:01:41 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CB05C061574; Wed, 7 Jul 2021 08:58:59 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id d2so3716398wrn.0; Wed, 07 Jul 2021 08:58:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NumtXqCRVYAzFa0yveavuQ/5Uh323OAnb8TqM4/K4UY=; b=KxaHFj4gSebkw76xK7UQrJMoMFCRbJ01khCHJuXvPlflgU+TBz3Yu4rLxUUv1sSuqN BVrdF/Za4jjBFhLu+NwL9+k7Ig8U9MkMBYvl/owsQQzeEYsaunvB/IAbVcVZ0qaA/+i8 XnMlIBDTNSZ7FiwA3EDvX3unQgrYURrOO7+yts5B78f/PM1O6qW2/Pqn5pZCcaFVyPBp 8je81Sma6xgcc+WAb+dePd7VpkfSnP9g7mfvknZlRPOgnqDJQfSQnan3tLB1aENC/4uq As3PQVlRKq/aBYasudrbegkJPTOYXPM5HksfYt/9eeSab6LgDCkaBL2Zo9vG9sbqbxdO PGyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NumtXqCRVYAzFa0yveavuQ/5Uh323OAnb8TqM4/K4UY=; b=PkLJ013dTWwmcfqZGmjbqgkU1QwDu4qpBpby+/pHWISWTnSj42ov4vTzeLt9U/QQ3q Hc3gV3XqrBZqgL96228MlL66le9bXAiqAfUJm43DaTW1g3ud8V1TvodWHcIyw4S2/8pJ RSnCT4R6VtOnxfYrFAbT1q1+UoiUcBY/hRvnLTBNAse+9rHKVb/66CBvVFBdIAcNr4mx O7cUWtrmcMsMOgYq5Dh+YV/KxlB2XZshU+gDJ1ZFOzDvLbJMyyrXha2IsE7ufpjvSITY nzSQceJNN/DF7v+F5SRu0yvLby1LAFWFPSt75wifpAplJA6Q48LULuUi8QECvO20qPus B19g== X-Gm-Message-State: AOAM533Ee2B/uBtdBF43C0f0jUESYk8ElYLQ/yKVizXM15VqHzln9Rc/ +BC0bvks9DysYEwQi+Jt9lg= X-Google-Smtp-Source: ABdhPJw3sISd19GRg4hEPy6GHoZ0l4hCW0ybJOatjne9nSWBztxiuR7mWinbUJfbFyyxcbHb8aPl0A== X-Received: by 2002:adf:e4c3:: with SMTP id v3mr4087082wrm.362.1625673537966; Wed, 07 Jul 2021 08:58:57 -0700 (PDT) Received: from localhost.localdomain ([94.73.35.100]) by smtp.gmail.com with ESMTPSA id c9sm13942838wrx.22.2021.07.07.08.58.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:58:57 -0700 (PDT) From: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= To: jikos@kernel.org Cc: benjamin.tissoires@redhat.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= Subject: [PATCH 2/2] HID: magicmouse: high-resolution scroll threshold Date: Wed, 7 Jul 2021 17:58:22 +0200 Message-Id: <20210707155822.7811-2-jose.exposito89@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707155822.7811-1-jose.exposito89@gmail.com> References: <20210707155822.7811-1-jose.exposito89@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org 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 --- 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,