From patchwork Thu Aug 30 21:56:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harry Cutts X-Patchwork-Id: 10582939 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7AFA85A4 for ; Thu, 30 Aug 2018 21:56:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6A86D2BE32 for ; Thu, 30 Aug 2018 21:56:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5D9462C24B; Thu, 30 Aug 2018 21:56:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E319B2BE32 for ; Thu, 30 Aug 2018 21:56:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728006AbeHaCAq (ORCPT ); Thu, 30 Aug 2018 22:00:46 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:40886 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727983AbeHaCAp (ORCPT ); Thu, 30 Aug 2018 22:00:45 -0400 Received: by mail-pg1-f196.google.com with SMTP id a13-v6so1788203pgt.7 for ; Thu, 30 Aug 2018 14:56:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QcFL356MAhLkKLgvLnv14Bj/GWDN0Ja5s+Ff/8fRGs0=; b=c/TM2kd09Qd2nmUC/nZ+uotwMWVKab1wh7r2/tBJSLUPxyY4fRvkXE5ZOCLR2/HuGI pbEMjhOySYnYwFY91c+uGCZ4+Q+jPcHttZ+aK/9ynmkL92L0qYaJGvpUhAJi0rOdQ4Si dWVwaqcfDH9Y9Jae9LTQUlKsE8DOrqzXEbpGs= 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=QcFL356MAhLkKLgvLnv14Bj/GWDN0Ja5s+Ff/8fRGs0=; b=AfxEIHhYtXiR7oSbYKDXGX9A80eAtP/G3VfShkRqBoh5RTsxq/AOc5Ipr0ylfK9/yn qhedTpRQS/oN1F1/iWZlZQMxkURX2LBDtrdGIpeFGe4ZVlOZU4clKPUyXSqS0FhVT8Px E8aKoWOrMLBXiGW65PBNYK5vMIV/NJiFCEvXAHPp74p6etwsVgjvCBKZG+QId2CXvZol dn+q/y6tTcmmc3FSlLH37fnyWLIyJs44C8xxyw2AraLN5BDhdxuCBgJg+SlmbAqflIb1 0JntJ5fYU8u80mVijvju/6pzLG7F+LNjRnIdE8DF90XhgNvwKsqbT+E1NcfZdzBC5WV4 KXlw== X-Gm-Message-State: APzg51BYeW8IQWKcLRe1xD02MhIixOq2nAkof9R4/bheJGUe/2yTidso lo+nQR/mlYhLkxfeSOF9t9MjyxJbquQ= X-Google-Smtp-Source: ANB0VdYr2ABZM9jTf/bsUPFpY/IvZnkUmrVnpJO8BC0JtuOf6RzZaHwPZjGfLw2FsZZN0AybaeU35g== X-Received: by 2002:a63:5c10:: with SMTP id q16-v6mr11342336pgb.452.1535666189427; Thu, 30 Aug 2018 14:56:29 -0700 (PDT) Received: from kolhar.mtv.corp.google.com ([2620:15c:202:201:356a:9de2:526a:5bc]) by smtp.gmail.com with ESMTPSA id h132-v6sm13516828pfc.100.2018.08.30.14.56.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Aug 2018 14:56:29 -0700 (PDT) From: Harry Cutts To: linux-input , LKML Cc: Jiri Kosina , Dmitry Torokhov , Benjamin Tissoires , Harry Cutts , Jiri Kosina Subject: [PATCH v2 2/5] Create a utility class for counting scroll events Date: Thu, 30 Aug 2018 14:56:19 -0700 Message-Id: <20180830215622.47550-3-hcutts@chromium.org> X-Mailer: git-send-email 2.19.0.rc0.228.g281dcd1b4d0-goog In-Reply-To: <20180830215622.47550-1-hcutts@chromium.org> References: <20180830215622.47550-1-hcutts@chromium.org> MIME-Version: 1.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- Changes in v2: None drivers/hid/hid-input.c | 45 +++++++++++++++++++++++++++++++++++++++++ include/linux/hid.h | 28 +++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c index 4e94ea3e280a..6e84e7b9afcb 100644 --- a/drivers/hid/hid-input.c +++ b/drivers/hid/hid-input.c @@ -1826,3 +1826,48 @@ void hidinput_disconnect(struct hid_device *hid) } EXPORT_SYMBOL_GPL(hidinput_disconnect); +/** + * hid_scroll_counter_handle_scroll() - Send high- and low-resolution scroll + * events given a high-resolution wheel + * movement. + * @counter: a hid_scroll_counter struct describing the wheel. + * @hi_res_value: the movement of the wheel, in the mouse's high-resolution + * units. + * + * Given a high-resolution movement, this function converts the movement into + * microns and emits high-resolution scroll events for the input device. It also + * uses the multiplier from &struct hid_scroll_counter to emit low-resolution + * scroll events when appropriate for backwards-compatibility with userspace + * input libraries. + */ +void hid_scroll_counter_handle_scroll(struct hid_scroll_counter *counter, + int hi_res_value) +{ + int low_res_scroll_amount; + /* Some wheels will rest 7/8ths of a notch from the previous notch + * after slow movement, so we want the threshold for low-res events to + * be in the middle of the notches (e.g. after 4/8ths) as opposed to on + * the notches themselves (8/8ths). + */ + int threshold = counter->resolution_multiplier / 2; + + input_report_rel(counter->dev, REL_WHEEL_HI_RES, + hi_res_value * counter->microns_per_hi_res_unit); + + counter->remainder += hi_res_value; + if (abs(counter->remainder) >= threshold) { + /* Add (or subtract) 1 because we want to trigger when the wheel + * is half-way to the next notch (i.e. scroll 1 notch after a + * 1/2 notch movement, 2 notches after a 1 1/2 notch movement, + * etc.). + */ + low_res_scroll_amount = + counter->remainder / counter->resolution_multiplier + + (hi_res_value > 0 ? 1 : -1); + input_report_rel(counter->dev, REL_WHEEL, + low_res_scroll_amount); + counter->remainder -= + low_res_scroll_amount * counter->resolution_multiplier; + } +} +EXPORT_SYMBOL_GPL(hid_scroll_counter_handle_scroll); diff --git a/include/linux/hid.h b/include/linux/hid.h index 834e6461a690..037e37b0b0e6 100644 --- a/include/linux/hid.h +++ b/include/linux/hid.h @@ -1138,6 +1138,34 @@ static inline u32 hid_report_len(struct hid_report *report) int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size, int interrupt); + +/** + * struct hid_scroll_counter - Utility class for processing high-resolution + * scroll events. + * @dev: the input device for which events should be reported. + * @microns_per_hi_res_unit: the amount moved by the user's finger for each + * high-resolution unit reported by the mouse, in + * microns. + * @resolution_multiplier: the wheel's resolution in high-resolution mode as a + * multiple of its lower resolution. For example, if + * moving the wheel by one "notch" would result in a + * value of 1 in low-resolution mode but 8 in + * high-resolution, the multiplier is 8. + * @remainder: counts the number of high-resolution units moved since the last + * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should + * only be used by class methods. + */ +struct hid_scroll_counter { + struct input_dev *dev; + int microns_per_hi_res_unit; + int resolution_multiplier; + + int remainder; +}; + +void hid_scroll_counter_handle_scroll(struct hid_scroll_counter *counter, + int hi_res_value); + /* HID quirks API */ unsigned long hid_lookup_quirk(const struct hid_device *hdev); int hid_quirks_init(char **quirks_param, __u16 bus, int count);