blob: b72cc6e060e8006a8d848a1df5bffccdc6bfd61b [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Michael Wrightfe3de7d2020-07-02 19:05:30 +010017// clang-format off
Prabir Pradhan9244aea2020-02-05 20:31:40 -080018#include "../Macros.h"
Michael Wrightfe3de7d2020-07-02 19:05:30 +010019// clang-format on
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070020
21#include "RotaryEncoderInputMapper.h"
22
Yeabkal Wubshitf1ed7052023-06-16 10:01:53 -070023#include <utils/Timers.h>
Harry Cuttsf13161a2023-03-08 14:15:49 +000024#include <optional>
25
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070026#include "CursorScrollAccumulator.h"
27
28namespace android {
29
Biswarup Palba27d1d2024-07-09 19:57:33 +000030constexpr float kDefaultScaleFactor = 1.0f;
31
Arpit Singh8e6fb252023-04-06 11:49:17 +000032RotaryEncoderInputMapper::RotaryEncoderInputMapper(InputDeviceContext& deviceContext,
33 const InputReaderConfiguration& readerConfig)
Biswarup Palba27d1d2024-07-09 19:57:33 +000034 : InputMapper(deviceContext, readerConfig),
35 mSource(AINPUT_SOURCE_ROTARY_ENCODER),
36 mScalingFactor(kDefaultScaleFactor),
37 mOrientation(ui::ROTATION_0) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070038
39RotaryEncoderInputMapper::~RotaryEncoderInputMapper() {}
40
Philip Junker4af3b3d2021-12-14 10:36:55 +010041uint32_t RotaryEncoderInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070042 return mSource;
43}
44
Harry Cuttsd02ea102023-03-17 18:21:30 +000045void RotaryEncoderInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070046 InputMapper::populateDeviceInfo(info);
47
48 if (mRotaryEncoderScrollAccumulator.haveRelativeVWheel()) {
Harry Cuttsf13161a2023-03-08 14:15:49 +000049 const PropertyMap& config = getDeviceContext().getConfiguration();
50 std::optional<float> res = config.getFloat("device.res");
51 if (!res.has_value()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052 ALOGW("Rotary Encoder device configuration file didn't specify resolution!\n");
53 }
Harry Cuttsf13161a2023-03-08 14:15:49 +000054 std::optional<float> scalingFactor = config.getFloat("device.scalingFactor");
55 if (!scalingFactor.has_value()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056 ALOGW("Rotary Encoder device configuration file didn't specify scaling factor,"
Biswarup Palba27d1d2024-07-09 19:57:33 +000057 "default to %f!\n",
58 kDefaultScaleFactor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070059 }
Biswarup Palba27d1d2024-07-09 19:57:33 +000060 mScalingFactor = scalingFactor.value_or(kDefaultScaleFactor);
Harry Cuttsd02ea102023-03-17 18:21:30 +000061 info.addMotionRange(AMOTION_EVENT_AXIS_SCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
62 res.value_or(0.0f) * mScalingFactor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070063 }
64}
65
66void RotaryEncoderInputMapper::dump(std::string& dump) {
67 dump += INDENT2 "Rotary Encoder Input Mapper:\n";
68 dump += StringPrintf(INDENT3 "HaveWheel: %s\n",
69 toString(mRotaryEncoderScrollAccumulator.haveRelativeVWheel()));
Yeabkal Wubshitf1ed7052023-06-16 10:01:53 -070070 dump += StringPrintf(INDENT3 "HaveSlopController: %s\n", toString(mSlopController != nullptr));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070071}
72
Arpit Singh4be4eef2023-03-28 14:26:01 +000073std::list<NotifyArgs> RotaryEncoderInputMapper::reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +000074 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000075 ConfigurationChanges changes) {
Arpit Singh4be4eef2023-03-28 14:26:01 +000076 std::list<NotifyArgs> out = InputMapper::reconfigure(when, config, changes);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000077 if (!changes.any()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080078 mRotaryEncoderScrollAccumulator.configure(getDeviceContext());
Yeabkal Wubshitf1ed7052023-06-16 10:01:53 -070079
80 const PropertyMap& propertyMap = getDeviceContext().getConfiguration();
81 float slopThreshold = propertyMap.getInt("rotary_encoder.slop_threshold").value_or(0);
82 int32_t slopDurationNs = milliseconds_to_nanoseconds(
83 propertyMap.getInt("rotary_encoder.slop_duration_ms").value_or(0));
84 if (slopThreshold > 0 && slopDurationNs > 0) {
85 mSlopController = std::make_unique<SlopController>(slopThreshold, slopDurationNs);
86 } else {
87 mSlopController = nullptr;
88 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089 }
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000090 if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Vladimir Komsiyskidd438e22024-02-13 11:47:54 +010091 if (getDeviceContext().getAssociatedViewport()) {
92 mDisplayId = getDeviceContext().getAssociatedViewport()->displayId;
93 mOrientation = getDeviceContext().getAssociatedViewport()->orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094 } else {
Vladimir Komsiyskidd438e22024-02-13 11:47:54 +010095 mDisplayId = ui::LogicalDisplayId::INVALID;
96 std::optional<DisplayViewport> internalViewport =
97 config.getDisplayViewportByType(ViewportType::INTERNAL);
98 if (internalViewport) {
99 mOrientation = internalViewport->orientation;
100 } else {
101 mOrientation = ui::ROTATION_0;
102 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700103 }
104 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700105 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700106}
107
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700108std::list<NotifyArgs> RotaryEncoderInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800109 mRotaryEncoderScrollAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700110
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700111 return InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700112}
113
Harry Cuttsa32a1192024-06-04 15:10:31 +0000114std::list<NotifyArgs> RotaryEncoderInputMapper::process(const RawEvent& rawEvent) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700115 std::list<NotifyArgs> out;
Harry Cuttsa32a1192024-06-04 15:10:31 +0000116 mRotaryEncoderScrollAccumulator.process(rawEvent);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700117
Harry Cuttsa32a1192024-06-04 15:10:31 +0000118 if (rawEvent.type == EV_SYN && rawEvent.code == SYN_REPORT) {
119 out += sync(rawEvent.when, rawEvent.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700120 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700121 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700122}
123
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700124std::list<NotifyArgs> RotaryEncoderInputMapper::sync(nsecs_t when, nsecs_t readTime) {
125 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700126
127 float scroll = mRotaryEncoderScrollAccumulator.getRelativeVWheel();
Yeabkal Wubshit6b662762023-05-22 23:07:31 -0700128 if (mSlopController) {
129 scroll = mSlopController->consumeEvent(when, scroll);
130 }
131
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700132 bool scrolled = scroll != 0;
133
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700134 // Send motion event.
135 if (scrolled) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800136 int32_t metaState = getContext()->getGlobalMetaState();
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800137
Michael Wrighta9cf4192022-12-01 23:46:39 +0000138 if (mOrientation == ui::ROTATION_180) {
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800139 scroll = -scroll;
140 }
141
142 PointerCoords pointerCoords;
143 pointerCoords.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700144 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor);
145
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800146 PointerProperties pointerProperties;
147 pointerProperties.clear();
148 pointerProperties.id = 0;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700149 pointerProperties.toolType = ToolType::UNKNOWN;
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800150
151 uint32_t policyFlags = 0;
152 if (getDeviceContext().isExternal()) {
153 policyFlags |= POLICY_FLAG_WAKE;
154 }
155
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700156 out.push_back(
157 NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Vladimir Komsiyskidd438e22024-02-13 11:47:54 +0100158 mDisplayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000159 metaState, /*buttonState=*/0, MotionClassification::NONE,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700160 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties,
161 &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000162 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /*videoFrames=*/{}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700163 }
164
165 mRotaryEncoderScrollAccumulator.finishSync();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700166 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700167}
168
169} // namespace android