blob: c633b495e41004b417e0ead4c258c59b11b5c011 [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 Wubshit58bda652024-09-24 20:22:18 -070023#include <Counter.h>
24#include <com_android_input_flags.h>
Yeabkal Wubshitf1ed7052023-06-16 10:01:53 -070025#include <utils/Timers.h>
Harry Cuttsf13161a2023-03-08 14:15:49 +000026#include <optional>
27
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070028#include "CursorScrollAccumulator.h"
29
30namespace android {
31
Yeabkal Wubshit58bda652024-09-24 20:22:18 -070032using android::expresslog::Counter;
33
34constexpr float kDefaultResolution = 0;
Biswarup Palba27d1d2024-07-09 19:57:33 +000035constexpr float kDefaultScaleFactor = 1.0f;
Yeabkal Wubshit58bda652024-09-24 20:22:18 -070036constexpr int32_t kDefaultMinRotationsToLog = 3;
Biswarup Palba27d1d2024-07-09 19:57:33 +000037
Arpit Singh8e6fb252023-04-06 11:49:17 +000038RotaryEncoderInputMapper::RotaryEncoderInputMapper(InputDeviceContext& deviceContext,
39 const InputReaderConfiguration& readerConfig)
Yeabkal Wubshit58bda652024-09-24 20:22:18 -070040 : RotaryEncoderInputMapper(deviceContext, readerConfig,
41 Counter::logIncrement /* telemetryLogCounter */) {}
42
43RotaryEncoderInputMapper::RotaryEncoderInputMapper(
44 InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
45 std::function<void(const char*, int64_t)> telemetryLogCounter)
Biswarup Palba27d1d2024-07-09 19:57:33 +000046 : InputMapper(deviceContext, readerConfig),
47 mSource(AINPUT_SOURCE_ROTARY_ENCODER),
48 mScalingFactor(kDefaultScaleFactor),
Yeabkal Wubshit58bda652024-09-24 20:22:18 -070049 mResolution(kDefaultResolution),
50 mOrientation(ui::ROTATION_0),
51 mTelemetryLogCounter(telemetryLogCounter) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052
53RotaryEncoderInputMapper::~RotaryEncoderInputMapper() {}
54
Philip Junker4af3b3d2021-12-14 10:36:55 +010055uint32_t RotaryEncoderInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056 return mSource;
57}
58
Harry Cuttsd02ea102023-03-17 18:21:30 +000059void RotaryEncoderInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070060 InputMapper::populateDeviceInfo(info);
61
62 if (mRotaryEncoderScrollAccumulator.haveRelativeVWheel()) {
Harry Cuttsf13161a2023-03-08 14:15:49 +000063 const PropertyMap& config = getDeviceContext().getConfiguration();
64 std::optional<float> res = config.getFloat("device.res");
65 if (!res.has_value()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070066 ALOGW("Rotary Encoder device configuration file didn't specify resolution!\n");
67 }
Yeabkal Wubshit58bda652024-09-24 20:22:18 -070068 mResolution = res.value_or(kDefaultResolution);
Harry Cuttsf13161a2023-03-08 14:15:49 +000069 std::optional<float> scalingFactor = config.getFloat("device.scalingFactor");
70 if (!scalingFactor.has_value()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070071 ALOGW("Rotary Encoder device configuration file didn't specify scaling factor,"
Biswarup Palba27d1d2024-07-09 19:57:33 +000072 "default to %f!\n",
73 kDefaultScaleFactor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070074 }
Biswarup Palba27d1d2024-07-09 19:57:33 +000075 mScalingFactor = scalingFactor.value_or(kDefaultScaleFactor);
Harry Cuttsd02ea102023-03-17 18:21:30 +000076 info.addMotionRange(AMOTION_EVENT_AXIS_SCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
Yeabkal Wubshit58bda652024-09-24 20:22:18 -070077 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 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070093 }
94}
95
96void RotaryEncoderInputMapper::dump(std::string& dump) {
97 dump += INDENT2 "Rotary Encoder Input Mapper:\n";
98 dump += StringPrintf(INDENT3 "HaveWheel: %s\n",
99 toString(mRotaryEncoderScrollAccumulator.haveRelativeVWheel()));
Yeabkal Wubshitf1ed7052023-06-16 10:01:53 -0700100 dump += StringPrintf(INDENT3 "HaveSlopController: %s\n", toString(mSlopController != nullptr));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700101}
102
Arpit Singh4be4eef2023-03-28 14:26:01 +0000103std::list<NotifyArgs> RotaryEncoderInputMapper::reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +0000104 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000105 ConfigurationChanges changes) {
Arpit Singh4be4eef2023-03-28 14:26:01 +0000106 std::list<NotifyArgs> out = InputMapper::reconfigure(when, config, changes);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000107 if (!changes.any()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800108 mRotaryEncoderScrollAccumulator.configure(getDeviceContext());
Yeabkal Wubshitf1ed7052023-06-16 10:01:53 -0700109
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 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700119 }
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000120 if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Vladimir Komsiyskidd438e22024-02-13 11:47:54 +0100121 if (getDeviceContext().getAssociatedViewport()) {
122 mDisplayId = getDeviceContext().getAssociatedViewport()->displayId;
123 mOrientation = getDeviceContext().getAssociatedViewport()->orientation;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700124 } else {
Vladimir Komsiyskidd438e22024-02-13 11:47:54 +0100125 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 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700133 }
134 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700135 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700136}
137
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700138std::list<NotifyArgs> RotaryEncoderInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800139 mRotaryEncoderScrollAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700140
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700141 return InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700142}
143
Harry Cuttsa32a1192024-06-04 15:10:31 +0000144std::list<NotifyArgs> RotaryEncoderInputMapper::process(const RawEvent& rawEvent) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700145 std::list<NotifyArgs> out;
Harry Cuttsa32a1192024-06-04 15:10:31 +0000146 mRotaryEncoderScrollAccumulator.process(rawEvent);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700147
Harry Cuttsa32a1192024-06-04 15:10:31 +0000148 if (rawEvent.type == EV_SYN && rawEvent.code == SYN_REPORT) {
149 out += sync(rawEvent.when, rawEvent.readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700150 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700151 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700152}
153
Yeabkal Wubshit58bda652024-09-24 20:22:18 -0700154void 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
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700171std::list<NotifyArgs> RotaryEncoderInputMapper::sync(nsecs_t when, nsecs_t readTime) {
172 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700173
174 float scroll = mRotaryEncoderScrollAccumulator.getRelativeVWheel();
Yeabkal Wubshit58bda652024-09-24 20:22:18 -0700175 logScroll(scroll);
176
Yeabkal Wubshit6b662762023-05-22 23:07:31 -0700177 if (mSlopController) {
178 scroll = mSlopController->consumeEvent(when, scroll);
179 }
180
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700181 bool scrolled = scroll != 0;
182
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700183 // Send motion event.
184 if (scrolled) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800185 int32_t metaState = getContext()->getGlobalMetaState();
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800186
Michael Wrighta9cf4192022-12-01 23:46:39 +0000187 if (mOrientation == ui::ROTATION_180) {
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800188 scroll = -scroll;
189 }
190
191 PointerCoords pointerCoords;
192 pointerCoords.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700193 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor);
194
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800195 PointerProperties pointerProperties;
196 pointerProperties.clear();
197 pointerProperties.id = 0;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700198 pointerProperties.toolType = ToolType::UNKNOWN;
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800199
200 uint32_t policyFlags = 0;
201 if (getDeviceContext().isExternal()) {
202 policyFlags |= POLICY_FLAG_WAKE;
203 }
204
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700205 out.push_back(
206 NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
Vladimir Komsiyskidd438e22024-02-13 11:47:54 +0100207 mDisplayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000208 metaState, /*buttonState=*/0, MotionClassification::NONE,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700209 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties,
210 &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000211 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /*videoFrames=*/{}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700212 }
213
214 mRotaryEncoderScrollAccumulator.finishSync();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700215 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700216}
217
218} // namespace android