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