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