Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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 | |
Michael Wright | fe3de7d | 2020-07-02 19:05:30 +0100 | [diff] [blame] | 17 | // clang-format off |
Prabir Pradhan | 9244aea | 2020-02-05 20:31:40 -0800 | [diff] [blame] | 18 | #include "../Macros.h" |
Michael Wright | fe3de7d | 2020-07-02 19:05:30 +0100 | [diff] [blame] | 19 | // clang-format on |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 20 | |
| 21 | #include "RotaryEncoderInputMapper.h" |
| 22 | |
Yeabkal Wubshit | 58bda65 | 2024-09-24 20:22:18 -0700 | [diff] [blame] | 23 | #include <Counter.h> |
| 24 | #include <com_android_input_flags.h> |
Yeabkal Wubshit | f1ed705 | 2023-06-16 10:01:53 -0700 | [diff] [blame] | 25 | #include <utils/Timers.h> |
Harry Cutts | f13161a | 2023-03-08 14:15:49 +0000 | [diff] [blame] | 26 | #include <optional> |
| 27 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 28 | #include "CursorScrollAccumulator.h" |
| 29 | |
| 30 | namespace android { |
| 31 | |
Yeabkal Wubshit | 58bda65 | 2024-09-24 20:22:18 -0700 | [diff] [blame] | 32 | using android::expresslog::Counter; |
| 33 | |
| 34 | constexpr float kDefaultResolution = 0; |
Biswarup Pal | ba27d1d | 2024-07-09 19:57:33 +0000 | [diff] [blame] | 35 | constexpr float kDefaultScaleFactor = 1.0f; |
Yeabkal Wubshit | 58bda65 | 2024-09-24 20:22:18 -0700 | [diff] [blame] | 36 | constexpr int32_t kDefaultMinRotationsToLog = 3; |
Biswarup Pal | ba27d1d | 2024-07-09 19:57:33 +0000 | [diff] [blame] | 37 | |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 38 | RotaryEncoderInputMapper::RotaryEncoderInputMapper(InputDeviceContext& deviceContext, |
| 39 | const InputReaderConfiguration& readerConfig) |
Yeabkal Wubshit | 58bda65 | 2024-09-24 20:22:18 -0700 | [diff] [blame] | 40 | : RotaryEncoderInputMapper(deviceContext, readerConfig, |
| 41 | Counter::logIncrement /* telemetryLogCounter */) {} |
| 42 | |
| 43 | RotaryEncoderInputMapper::RotaryEncoderInputMapper( |
| 44 | InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig, |
| 45 | std::function<void(const char*, int64_t)> telemetryLogCounter) |
Biswarup Pal | ba27d1d | 2024-07-09 19:57:33 +0000 | [diff] [blame] | 46 | : InputMapper(deviceContext, readerConfig), |
| 47 | mSource(AINPUT_SOURCE_ROTARY_ENCODER), |
| 48 | mScalingFactor(kDefaultScaleFactor), |
Yeabkal Wubshit | 58bda65 | 2024-09-24 20:22:18 -0700 | [diff] [blame] | 49 | mResolution(kDefaultResolution), |
| 50 | mOrientation(ui::ROTATION_0), |
| 51 | mTelemetryLogCounter(telemetryLogCounter) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 52 | |
| 53 | RotaryEncoderInputMapper::~RotaryEncoderInputMapper() {} |
| 54 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 55 | uint32_t RotaryEncoderInputMapper::getSources() const { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 56 | return mSource; |
| 57 | } |
| 58 | |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 59 | void RotaryEncoderInputMapper::populateDeviceInfo(InputDeviceInfo& info) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 60 | InputMapper::populateDeviceInfo(info); |
| 61 | |
| 62 | if (mRotaryEncoderScrollAccumulator.haveRelativeVWheel()) { |
Harry Cutts | f13161a | 2023-03-08 14:15:49 +0000 | [diff] [blame] | 63 | const PropertyMap& config = getDeviceContext().getConfiguration(); |
| 64 | std::optional<float> res = config.getFloat("device.res"); |
| 65 | if (!res.has_value()) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 66 | ALOGW("Rotary Encoder device configuration file didn't specify resolution!\n"); |
| 67 | } |
Yeabkal Wubshit | 58bda65 | 2024-09-24 20:22:18 -0700 | [diff] [blame] | 68 | mResolution = res.value_or(kDefaultResolution); |
Harry Cutts | f13161a | 2023-03-08 14:15:49 +0000 | [diff] [blame] | 69 | std::optional<float> scalingFactor = config.getFloat("device.scalingFactor"); |
| 70 | if (!scalingFactor.has_value()) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 71 | ALOGW("Rotary Encoder device configuration file didn't specify scaling factor," |
Biswarup Pal | ba27d1d | 2024-07-09 19:57:33 +0000 | [diff] [blame] | 72 | "default to %f!\n", |
| 73 | kDefaultScaleFactor); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 74 | } |
Biswarup Pal | ba27d1d | 2024-07-09 19:57:33 +0000 | [diff] [blame] | 75 | mScalingFactor = scalingFactor.value_or(kDefaultScaleFactor); |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 76 | info.addMotionRange(AMOTION_EVENT_AXIS_SCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, |
Yeabkal Wubshit | 58bda65 | 2024-09-24 20:22:18 -0700 | [diff] [blame] | 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 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 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())); |
Yeabkal Wubshit | f1ed705 | 2023-06-16 10:01:53 -0700 | [diff] [blame] | 100 | dump += StringPrintf(INDENT3 "HaveSlopController: %s\n", toString(mSlopController != nullptr)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 103 | std::list<NotifyArgs> RotaryEncoderInputMapper::reconfigure(nsecs_t when, |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 104 | const InputReaderConfiguration& config, |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 105 | ConfigurationChanges changes) { |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 106 | std::list<NotifyArgs> out = InputMapper::reconfigure(when, config, changes); |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 107 | if (!changes.any()) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 108 | mRotaryEncoderScrollAccumulator.configure(getDeviceContext()); |
Yeabkal Wubshit | f1ed705 | 2023-06-16 10:01:53 -0700 | [diff] [blame] | 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 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 119 | } |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 120 | if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) { |
Vladimir Komsiyski | dd438e2 | 2024-02-13 11:47:54 +0100 | [diff] [blame] | 121 | if (getDeviceContext().getAssociatedViewport()) { |
| 122 | mDisplayId = getDeviceContext().getAssociatedViewport()->displayId; |
| 123 | mOrientation = getDeviceContext().getAssociatedViewport()->orientation; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 124 | } else { |
Vladimir Komsiyski | dd438e2 | 2024-02-13 11:47:54 +0100 | [diff] [blame] | 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 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 135 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 138 | std::list<NotifyArgs> RotaryEncoderInputMapper::reset(nsecs_t when) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 139 | mRotaryEncoderScrollAccumulator.reset(getDeviceContext()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 140 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 141 | return InputMapper::reset(when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 144 | std::list<NotifyArgs> RotaryEncoderInputMapper::process(const RawEvent& rawEvent) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 145 | std::list<NotifyArgs> out; |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 146 | mRotaryEncoderScrollAccumulator.process(rawEvent); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 147 | |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 148 | if (rawEvent.type == EV_SYN && rawEvent.code == SYN_REPORT) { |
| 149 | out += sync(rawEvent.when, rawEvent.readTime); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 150 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 151 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Yeabkal Wubshit | 58bda65 | 2024-09-24 20:22:18 -0700 | [diff] [blame] | 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 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 171 | std::list<NotifyArgs> RotaryEncoderInputMapper::sync(nsecs_t when, nsecs_t readTime) { |
| 172 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 173 | |
| 174 | float scroll = mRotaryEncoderScrollAccumulator.getRelativeVWheel(); |
Yeabkal Wubshit | 58bda65 | 2024-09-24 20:22:18 -0700 | [diff] [blame] | 175 | logScroll(scroll); |
| 176 | |
Yeabkal Wubshit | 6b66276 | 2023-05-22 23:07:31 -0700 | [diff] [blame] | 177 | if (mSlopController) { |
| 178 | scroll = mSlopController->consumeEvent(when, scroll); |
| 179 | } |
| 180 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 181 | bool scrolled = scroll != 0; |
| 182 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 183 | // Send motion event. |
| 184 | if (scrolled) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 185 | int32_t metaState = getContext()->getGlobalMetaState(); |
Yeabkal Wubshit | b8c3548 | 2022-11-09 17:49:22 -0800 | [diff] [blame] | 186 | |
Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame] | 187 | if (mOrientation == ui::ROTATION_180) { |
Yeabkal Wubshit | b8c3548 | 2022-11-09 17:49:22 -0800 | [diff] [blame] | 188 | scroll = -scroll; |
| 189 | } |
| 190 | |
| 191 | PointerCoords pointerCoords; |
| 192 | pointerCoords.clear(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 193 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor); |
| 194 | |
Yeabkal Wubshit | b8c3548 | 2022-11-09 17:49:22 -0800 | [diff] [blame] | 195 | PointerProperties pointerProperties; |
| 196 | pointerProperties.clear(); |
| 197 | pointerProperties.id = 0; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 198 | pointerProperties.toolType = ToolType::UNKNOWN; |
Yeabkal Wubshit | b8c3548 | 2022-11-09 17:49:22 -0800 | [diff] [blame] | 199 | |
| 200 | uint32_t policyFlags = 0; |
| 201 | if (getDeviceContext().isExternal()) { |
| 202 | policyFlags |= POLICY_FLAG_WAKE; |
| 203 | } |
| 204 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 205 | out.push_back( |
| 206 | NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource, |
Vladimir Komsiyski | dd438e2 | 2024-02-13 11:47:54 +0100 | [diff] [blame] | 207 | mDisplayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 208 | metaState, /*buttonState=*/0, MotionClassification::NONE, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 209 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, |
| 210 | &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 211 | AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /*videoFrames=*/{})); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | mRotaryEncoderScrollAccumulator.finishSync(); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 215 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | } // namespace android |