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 | f1ed705 | 2023-06-16 10:01:53 -0700 | [diff] [blame] | 23 | #include <utils/Timers.h> |
Harry Cutts | f13161a | 2023-03-08 14:15:49 +0000 | [diff] [blame] | 24 | #include <optional> |
| 25 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 26 | #include "CursorScrollAccumulator.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
Biswarup Pal | ba27d1d | 2024-07-09 19:57:33 +0000 | [diff] [blame] | 30 | constexpr float kDefaultScaleFactor = 1.0f; |
| 31 | |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 32 | RotaryEncoderInputMapper::RotaryEncoderInputMapper(InputDeviceContext& deviceContext, |
| 33 | const InputReaderConfiguration& readerConfig) |
Biswarup Pal | ba27d1d | 2024-07-09 19:57:33 +0000 | [diff] [blame] | 34 | : InputMapper(deviceContext, readerConfig), |
| 35 | mSource(AINPUT_SOURCE_ROTARY_ENCODER), |
| 36 | mScalingFactor(kDefaultScaleFactor), |
| 37 | mOrientation(ui::ROTATION_0) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 38 | |
| 39 | RotaryEncoderInputMapper::~RotaryEncoderInputMapper() {} |
| 40 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 41 | uint32_t RotaryEncoderInputMapper::getSources() const { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 42 | return mSource; |
| 43 | } |
| 44 | |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 45 | void RotaryEncoderInputMapper::populateDeviceInfo(InputDeviceInfo& info) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 46 | InputMapper::populateDeviceInfo(info); |
| 47 | |
| 48 | if (mRotaryEncoderScrollAccumulator.haveRelativeVWheel()) { |
Harry Cutts | f13161a | 2023-03-08 14:15:49 +0000 | [diff] [blame] | 49 | const PropertyMap& config = getDeviceContext().getConfiguration(); |
| 50 | std::optional<float> res = config.getFloat("device.res"); |
| 51 | if (!res.has_value()) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 52 | ALOGW("Rotary Encoder device configuration file didn't specify resolution!\n"); |
| 53 | } |
Harry Cutts | f13161a | 2023-03-08 14:15:49 +0000 | [diff] [blame] | 54 | std::optional<float> scalingFactor = config.getFloat("device.scalingFactor"); |
| 55 | if (!scalingFactor.has_value()) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 56 | ALOGW("Rotary Encoder device configuration file didn't specify scaling factor," |
Biswarup Pal | ba27d1d | 2024-07-09 19:57:33 +0000 | [diff] [blame] | 57 | "default to %f!\n", |
| 58 | kDefaultScaleFactor); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 59 | } |
Biswarup Pal | ba27d1d | 2024-07-09 19:57:33 +0000 | [diff] [blame] | 60 | mScalingFactor = scalingFactor.value_or(kDefaultScaleFactor); |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 61 | info.addMotionRange(AMOTION_EVENT_AXIS_SCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, |
| 62 | res.value_or(0.0f) * mScalingFactor); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | void 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 Wubshit | f1ed705 | 2023-06-16 10:01:53 -0700 | [diff] [blame] | 70 | dump += StringPrintf(INDENT3 "HaveSlopController: %s\n", toString(mSlopController != nullptr)); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 73 | std::list<NotifyArgs> RotaryEncoderInputMapper::reconfigure(nsecs_t when, |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 74 | const InputReaderConfiguration& config, |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 75 | ConfigurationChanges changes) { |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 76 | std::list<NotifyArgs> out = InputMapper::reconfigure(when, config, changes); |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 77 | if (!changes.any()) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 78 | mRotaryEncoderScrollAccumulator.configure(getDeviceContext()); |
Yeabkal Wubshit | f1ed705 | 2023-06-16 10:01:53 -0700 | [diff] [blame] | 79 | |
| 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 Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 89 | } |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 90 | if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) { |
Vladimir Komsiyski | dd438e2 | 2024-02-13 11:47:54 +0100 | [diff] [blame] | 91 | if (getDeviceContext().getAssociatedViewport()) { |
| 92 | mDisplayId = getDeviceContext().getAssociatedViewport()->displayId; |
| 93 | mOrientation = getDeviceContext().getAssociatedViewport()->orientation; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 94 | } else { |
Vladimir Komsiyski | dd438e2 | 2024-02-13 11:47:54 +0100 | [diff] [blame] | 95 | 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 Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 105 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 108 | std::list<NotifyArgs> RotaryEncoderInputMapper::reset(nsecs_t when) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 109 | mRotaryEncoderScrollAccumulator.reset(getDeviceContext()); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 110 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 111 | return InputMapper::reset(when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 114 | std::list<NotifyArgs> RotaryEncoderInputMapper::process(const RawEvent& rawEvent) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 115 | std::list<NotifyArgs> out; |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 116 | mRotaryEncoderScrollAccumulator.process(rawEvent); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 117 | |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 118 | if (rawEvent.type == EV_SYN && rawEvent.code == SYN_REPORT) { |
| 119 | out += sync(rawEvent.when, rawEvent.readTime); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 120 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 121 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 124 | std::list<NotifyArgs> RotaryEncoderInputMapper::sync(nsecs_t when, nsecs_t readTime) { |
| 125 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 126 | |
| 127 | float scroll = mRotaryEncoderScrollAccumulator.getRelativeVWheel(); |
Yeabkal Wubshit | 6b66276 | 2023-05-22 23:07:31 -0700 | [diff] [blame] | 128 | if (mSlopController) { |
| 129 | scroll = mSlopController->consumeEvent(when, scroll); |
| 130 | } |
| 131 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 132 | bool scrolled = scroll != 0; |
| 133 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 134 | // Send motion event. |
| 135 | if (scrolled) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 136 | int32_t metaState = getContext()->getGlobalMetaState(); |
Yeabkal Wubshit | b8c3548 | 2022-11-09 17:49:22 -0800 | [diff] [blame] | 137 | |
Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame] | 138 | if (mOrientation == ui::ROTATION_180) { |
Yeabkal Wubshit | b8c3548 | 2022-11-09 17:49:22 -0800 | [diff] [blame] | 139 | scroll = -scroll; |
| 140 | } |
| 141 | |
| 142 | PointerCoords pointerCoords; |
| 143 | pointerCoords.clear(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 144 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor); |
| 145 | |
Yeabkal Wubshit | b8c3548 | 2022-11-09 17:49:22 -0800 | [diff] [blame] | 146 | PointerProperties pointerProperties; |
| 147 | pointerProperties.clear(); |
| 148 | pointerProperties.id = 0; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 149 | pointerProperties.toolType = ToolType::UNKNOWN; |
Yeabkal Wubshit | b8c3548 | 2022-11-09 17:49:22 -0800 | [diff] [blame] | 150 | |
| 151 | uint32_t policyFlags = 0; |
| 152 | if (getDeviceContext().isExternal()) { |
| 153 | policyFlags |= POLICY_FLAG_WAKE; |
| 154 | } |
| 155 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 156 | out.push_back( |
| 157 | NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource, |
Vladimir Komsiyski | dd438e2 | 2024-02-13 11:47:54 +0100 | [diff] [blame] | 158 | mDisplayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 159 | metaState, /*buttonState=*/0, MotionClassification::NONE, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 160 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, |
| 161 | &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, |
Harry Cutts | 101ee9b | 2023-07-06 18:04:14 +0000 | [diff] [blame] | 162 | AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /*videoFrames=*/{})); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | mRotaryEncoderScrollAccumulator.finishSync(); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 166 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | } // namespace android |