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