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