| 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 |  | 
|  | 34 | uint32_t RotaryEncoderInputMapper::getSources() { | 
|  | 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; | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 43 | if (!getDeviceContext().getConfiguration().tryGetProperty(String8("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 | } | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 46 | if (!getDeviceContext().getConfiguration().tryGetProperty(String8("device.scalingFactor"), | 
|  | 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 |  | 
|  | 63 | void RotaryEncoderInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config, | 
|  | 64 | uint32_t changes) { | 
|  | 65 | InputMapper::configure(when, config, changes); | 
|  | 66 | if (!changes) { | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 67 | mRotaryEncoderScrollAccumulator.configure(getDeviceContext()); | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 68 | } | 
|  | 69 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { | 
|  | 70 | std::optional<DisplayViewport> internalViewport = | 
| Michael Wright | fe3de7d | 2020-07-02 19:05:30 +0100 | [diff] [blame] | 71 | config->getDisplayViewportByType(ViewportType::INTERNAL); | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 72 | if (internalViewport) { | 
|  | 73 | mOrientation = internalViewport->orientation; | 
|  | 74 | } else { | 
|  | 75 | mOrientation = DISPLAY_ORIENTATION_0; | 
|  | 76 | } | 
|  | 77 | } | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | void RotaryEncoderInputMapper::reset(nsecs_t when) { | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 81 | mRotaryEncoderScrollAccumulator.reset(getDeviceContext()); | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 82 |  | 
|  | 83 | InputMapper::reset(when); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | void RotaryEncoderInputMapper::process(const RawEvent* rawEvent) { | 
|  | 87 | mRotaryEncoderScrollAccumulator.process(rawEvent); | 
|  | 88 |  | 
|  | 89 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { | 
|  | 90 | sync(rawEvent->when); | 
|  | 91 | } | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | void RotaryEncoderInputMapper::sync(nsecs_t when) { | 
|  | 95 | PointerCoords pointerCoords; | 
|  | 96 | pointerCoords.clear(); | 
|  | 97 |  | 
|  | 98 | PointerProperties pointerProperties; | 
|  | 99 | pointerProperties.clear(); | 
|  | 100 | pointerProperties.id = 0; | 
|  | 101 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN; | 
|  | 102 |  | 
|  | 103 | float scroll = mRotaryEncoderScrollAccumulator.getRelativeVWheel(); | 
|  | 104 | bool scrolled = scroll != 0; | 
|  | 105 |  | 
|  | 106 | // This is not a pointer, so it's not associated with a display. | 
|  | 107 | int32_t displayId = ADISPLAY_ID_NONE; | 
|  | 108 |  | 
|  | 109 | // Moving the rotary encoder should wake the device (if specified). | 
|  | 110 | uint32_t policyFlags = 0; | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 111 | if (scrolled && getDeviceContext().isExternal()) { | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 112 | policyFlags |= POLICY_FLAG_WAKE; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | if (mOrientation == DISPLAY_ORIENTATION_180) { | 
|  | 116 | scroll = -scroll; | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | // Send motion event. | 
|  | 120 | if (scrolled) { | 
| Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 121 | int32_t metaState = getContext()->getGlobalMetaState(); | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 122 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor); | 
|  | 123 |  | 
| Siarhei Vishniakou | cec3f6a | 2020-11-10 15:42:39 -0600 | [diff] [blame] | 124 | getContext()->notifyMotion(when, getDeviceId(), mSource, displayId, policyFlags, | 
|  | 125 | AMOTION_EVENT_ACTION_SCROLL, 0, 0, metaState, | 
|  | 126 | /* buttonState */ 0, MotionClassification::NONE, | 
|  | 127 | AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, | 
|  | 128 | &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, | 
|  | 129 | AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /* videoFrames */ {}); | 
| Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
|  | 132 | mRotaryEncoderScrollAccumulator.finishSync(); | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | } // namespace android |