1 /*
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
17 // clang-format off
18 #include "../Macros.h"
19 // clang-format on
20
21 #include "RotaryEncoderInputMapper.h"
22
23 #include <Counter.h>
24 #include <com_android_input_flags.h>
25 #include <utils/Timers.h>
26 #include <optional>
27
28 #include "CursorScrollAccumulator.h"
29
30 namespace android {
31
32 using android::expresslog::Counter;
33
34 constexpr float kDefaultResolution = 0;
35 constexpr float kDefaultScaleFactor = 1.0f;
36 constexpr int32_t kDefaultMinRotationsToLog = 3;
37
RotaryEncoderInputMapper(InputDeviceContext & deviceContext,const InputReaderConfiguration & readerConfig)38 RotaryEncoderInputMapper::RotaryEncoderInputMapper(InputDeviceContext& deviceContext,
39 const InputReaderConfiguration& readerConfig)
40 : RotaryEncoderInputMapper(deviceContext, readerConfig,
41 Counter::logIncrement /* telemetryLogCounter */) {}
42
RotaryEncoderInputMapper(InputDeviceContext & deviceContext,const InputReaderConfiguration & readerConfig,std::function<void (const char *,int64_t)> telemetryLogCounter)43 RotaryEncoderInputMapper::RotaryEncoderInputMapper(
44 InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
45 std::function<void(const char*, int64_t)> telemetryLogCounter)
46 : InputMapper(deviceContext, readerConfig),
47 mSource(AINPUT_SOURCE_ROTARY_ENCODER),
48 mScalingFactor(kDefaultScaleFactor),
49 mResolution(kDefaultResolution),
50 mOrientation(ui::ROTATION_0),
51 mTelemetryLogCounter(telemetryLogCounter) {}
52
~RotaryEncoderInputMapper()53 RotaryEncoderInputMapper::~RotaryEncoderInputMapper() {}
54
getSources() const55 uint32_t RotaryEncoderInputMapper::getSources() const {
56 return mSource;
57 }
58
populateDeviceInfo(InputDeviceInfo & info)59 void RotaryEncoderInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
60 InputMapper::populateDeviceInfo(info);
61
62 if (mRotaryEncoderScrollAccumulator.haveRelativeVWheel()) {
63 const PropertyMap& config = getDeviceContext().getConfiguration();
64 std::optional<float> res = config.getFloat("device.res");
65 if (!res.has_value()) {
66 ALOGW("Rotary Encoder device configuration file didn't specify resolution!\n");
67 }
68 mResolution = res.value_or(kDefaultResolution);
69 std::optional<float> scalingFactor = config.getFloat("device.scalingFactor");
70 if (!scalingFactor.has_value()) {
71 ALOGW("Rotary Encoder device configuration file didn't specify scaling factor,"
72 "default to %f!\n",
73 kDefaultScaleFactor);
74 }
75 mScalingFactor = scalingFactor.value_or(kDefaultScaleFactor);
76 info.addMotionRange(AMOTION_EVENT_AXIS_SCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
77 mResolution * mScalingFactor);
78
79 if (com::android::input::flags::rotary_input_telemetry()) {
80 mMinRotationsToLog = config.getInt("rotary_encoder.min_rotations_to_log");
81 if (!mMinRotationsToLog.has_value()) {
82 ALOGI("Rotary Encoder device configuration file didn't specify min log rotation.");
83 } else if (*mMinRotationsToLog <= 0) {
84 ALOGE("Rotary Encoder device configuration specified non-positive min log rotation "
85 ": %d. Telemetry logging of rotations disabled.",
86 *mMinRotationsToLog);
87 mMinRotationsToLog = {};
88 } else {
89 ALOGD("Rotary Encoder telemetry enabled. mMinRotationsToLog=%d",
90 *mMinRotationsToLog);
91 }
92 }
93 }
94 }
95
dump(std::string & dump)96 void RotaryEncoderInputMapper::dump(std::string& dump) {
97 dump += INDENT2 "Rotary Encoder Input Mapper:\n";
98 dump += StringPrintf(INDENT3 "HaveWheel: %s\n",
99 toString(mRotaryEncoderScrollAccumulator.haveRelativeVWheel()));
100 dump += StringPrintf(INDENT3 "HaveSlopController: %s\n", toString(mSlopController != nullptr));
101 }
102
reconfigure(nsecs_t when,const InputReaderConfiguration & config,ConfigurationChanges changes)103 std::list<NotifyArgs> RotaryEncoderInputMapper::reconfigure(nsecs_t when,
104 const InputReaderConfiguration& config,
105 ConfigurationChanges changes) {
106 std::list<NotifyArgs> out = InputMapper::reconfigure(when, config, changes);
107 if (!changes.any()) {
108 mRotaryEncoderScrollAccumulator.configure(getDeviceContext());
109
110 const PropertyMap& propertyMap = getDeviceContext().getConfiguration();
111 float slopThreshold = propertyMap.getInt("rotary_encoder.slop_threshold").value_or(0);
112 int32_t slopDurationNs = milliseconds_to_nanoseconds(
113 propertyMap.getInt("rotary_encoder.slop_duration_ms").value_or(0));
114 if (slopThreshold > 0 && slopDurationNs > 0) {
115 mSlopController = std::make_unique<SlopController>(slopThreshold, slopDurationNs);
116 } else {
117 mSlopController = nullptr;
118 }
119 }
120 if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
121 if (getDeviceContext().getAssociatedViewport()) {
122 mDisplayId = getDeviceContext().getAssociatedViewport()->displayId;
123 mOrientation = getDeviceContext().getAssociatedViewport()->orientation;
124 } else {
125 mDisplayId = ui::LogicalDisplayId::INVALID;
126 std::optional<DisplayViewport> internalViewport =
127 config.getDisplayViewportByType(ViewportType::INTERNAL);
128 if (internalViewport) {
129 mOrientation = internalViewport->orientation;
130 } else {
131 mOrientation = ui::ROTATION_0;
132 }
133 }
134 }
135 return out;
136 }
137
reset(nsecs_t when)138 std::list<NotifyArgs> RotaryEncoderInputMapper::reset(nsecs_t when) {
139 mRotaryEncoderScrollAccumulator.reset(getDeviceContext());
140
141 return InputMapper::reset(when);
142 }
143
process(const RawEvent & rawEvent)144 std::list<NotifyArgs> RotaryEncoderInputMapper::process(const RawEvent& rawEvent) {
145 std::list<NotifyArgs> out;
146 mRotaryEncoderScrollAccumulator.process(rawEvent);
147
148 if (rawEvent.type == EV_SYN && rawEvent.code == SYN_REPORT) {
149 out += sync(rawEvent.when, rawEvent.readTime);
150 }
151 return out;
152 }
153
logScroll(float scroll)154 void RotaryEncoderInputMapper::logScroll(float scroll) {
155 if (mResolution <= 0 || !mMinRotationsToLog) return;
156
157 mUnloggedScrolls += fabs(scroll);
158
159 // unitsPerRotation = (2 * PI * radians) * (units per radian (i.e. resolution))
160 const float unitsPerRotation = 2 * M_PI * mResolution;
161 const float scrollsPerMinRotationsToLog = *mMinRotationsToLog * unitsPerRotation;
162 const int32_t numMinRotationsToLog =
163 static_cast<int32_t>(mUnloggedScrolls / scrollsPerMinRotationsToLog);
164 mUnloggedScrolls = std::fmod(mUnloggedScrolls, scrollsPerMinRotationsToLog);
165 if (numMinRotationsToLog) {
166 mTelemetryLogCounter("input.value_rotary_input_device_full_rotation_count",
167 numMinRotationsToLog * (*mMinRotationsToLog));
168 }
169 }
170
sync(nsecs_t when,nsecs_t readTime)171 std::list<NotifyArgs> RotaryEncoderInputMapper::sync(nsecs_t when, nsecs_t readTime) {
172 std::list<NotifyArgs> out;
173
174 float scroll = mRotaryEncoderScrollAccumulator.getRelativeVWheel();
175 logScroll(scroll);
176
177 if (mSlopController) {
178 scroll = mSlopController->consumeEvent(when, scroll);
179 }
180
181 bool scrolled = scroll != 0;
182
183 // Send motion event.
184 if (scrolled) {
185 int32_t metaState = getContext()->getGlobalMetaState();
186
187 if (mOrientation == ui::ROTATION_180) {
188 scroll = -scroll;
189 }
190
191 PointerCoords pointerCoords;
192 pointerCoords.clear();
193 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor);
194
195 PointerProperties pointerProperties;
196 pointerProperties.clear();
197 pointerProperties.id = 0;
198 pointerProperties.toolType = ToolType::UNKNOWN;
199
200 uint32_t policyFlags = 0;
201 if (getDeviceContext().isExternal()) {
202 policyFlags |= POLICY_FLAG_WAKE;
203 }
204
205 out.push_back(
206 NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
207 mDisplayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0,
208 metaState, /*buttonState=*/0, MotionClassification::NONE,
209 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties,
210 &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
211 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /*videoFrames=*/{}));
212 }
213
214 mRotaryEncoderScrollAccumulator.finishSync();
215 return out;
216 }
217
218 } // namespace android
219