blob: 13f2e59db909bf58251b071c57bd26f110fcfc66 [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
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 Wrightfe3de7d2020-07-02 19:05:30 +010017// clang-format off
Prabir Pradhan9244aea2020-02-05 20:31:40 -080018#include "../Macros.h"
Michael Wrightfe3de7d2020-07-02 19:05:30 +010019// clang-format on
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070020
21#include "RotaryEncoderInputMapper.h"
22
Harry Cuttsf13161a2023-03-08 14:15:49 +000023#include <optional>
24
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070025#include "CursorScrollAccumulator.h"
26
27namespace android {
28
Arpit Singh8e6fb252023-04-06 11:49:17 +000029RotaryEncoderInputMapper::RotaryEncoderInputMapper(InputDeviceContext& deviceContext,
30 const InputReaderConfiguration& readerConfig)
31 : InputMapper(deviceContext, readerConfig), mOrientation(ui::ROTATION_0) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070032 mSource = AINPUT_SOURCE_ROTARY_ENCODER;
33}
34
35RotaryEncoderInputMapper::~RotaryEncoderInputMapper() {}
36
Philip Junker4af3b3d2021-12-14 10:36:55 +010037uint32_t RotaryEncoderInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070038 return mSource;
39}
40
Harry Cuttsd02ea102023-03-17 18:21:30 +000041void RotaryEncoderInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070042 InputMapper::populateDeviceInfo(info);
43
44 if (mRotaryEncoderScrollAccumulator.haveRelativeVWheel()) {
Harry Cuttsf13161a2023-03-08 14:15:49 +000045 const PropertyMap& config = getDeviceContext().getConfiguration();
46 std::optional<float> res = config.getFloat("device.res");
47 if (!res.has_value()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070048 ALOGW("Rotary Encoder device configuration file didn't specify resolution!\n");
49 }
Harry Cuttsf13161a2023-03-08 14:15:49 +000050 std::optional<float> scalingFactor = config.getFloat("device.scalingFactor");
51 if (!scalingFactor.has_value()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052 ALOGW("Rotary Encoder device configuration file didn't specify scaling factor,"
53 "default to 1.0!\n");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070054 }
Harry Cuttsf13161a2023-03-08 14:15:49 +000055 mScalingFactor = scalingFactor.value_or(1.0f);
Harry Cuttsd02ea102023-03-17 18:21:30 +000056 info.addMotionRange(AMOTION_EVENT_AXIS_SCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
57 res.value_or(0.0f) * mScalingFactor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070058 }
59}
60
61void RotaryEncoderInputMapper::dump(std::string& dump) {
62 dump += INDENT2 "Rotary Encoder Input Mapper:\n";
63 dump += StringPrintf(INDENT3 "HaveWheel: %s\n",
64 toString(mRotaryEncoderScrollAccumulator.haveRelativeVWheel()));
65}
66
Arpit Singh4be4eef2023-03-28 14:26:01 +000067std::list<NotifyArgs> RotaryEncoderInputMapper::reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +000068 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000069 ConfigurationChanges changes) {
Arpit Singh4be4eef2023-03-28 14:26:01 +000070 std::list<NotifyArgs> out = InputMapper::reconfigure(when, config, changes);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000071 if (!changes.any()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080072 mRotaryEncoderScrollAccumulator.configure(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070073 }
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000074 if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070075 std::optional<DisplayViewport> internalViewport =
Arpit Singhed6c3de2023-04-05 19:24:37 +000076 config.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070077 if (internalViewport) {
78 mOrientation = internalViewport->orientation;
79 } else {
Michael Wrighta9cf4192022-12-01 23:46:39 +000080 mOrientation = ui::ROTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070081 }
82 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070083 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070084}
85
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070086std::list<NotifyArgs> RotaryEncoderInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080087 mRotaryEncoderScrollAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070088
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070089 return InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090}
91
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070092std::list<NotifyArgs> RotaryEncoderInputMapper::process(const RawEvent* rawEvent) {
93 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094 mRotaryEncoderScrollAccumulator.process(rawEvent);
95
96 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070097 out += sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070099 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700100}
101
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700102std::list<NotifyArgs> RotaryEncoderInputMapper::sync(nsecs_t when, nsecs_t readTime) {
103 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700104
105 float scroll = mRotaryEncoderScrollAccumulator.getRelativeVWheel();
106 bool scrolled = scroll != 0;
107
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700108 // Send motion event.
109 if (scrolled) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800110 int32_t metaState = getContext()->getGlobalMetaState();
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800111 // This is not a pointer, so it's not associated with a display.
112 int32_t displayId = ADISPLAY_ID_NONE;
113
Michael Wrighta9cf4192022-12-01 23:46:39 +0000114 if (mOrientation == ui::ROTATION_180) {
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800115 scroll = -scroll;
116 }
117
118 PointerCoords pointerCoords;
119 pointerCoords.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700120 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor);
121
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800122 PointerProperties pointerProperties;
123 pointerProperties.clear();
124 pointerProperties.id = 0;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700125 pointerProperties.toolType = ToolType::UNKNOWN;
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800126
127 uint32_t policyFlags = 0;
128 if (getDeviceContext().isExternal()) {
129 policyFlags |= POLICY_FLAG_WAKE;
130 }
131
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700132 out.push_back(
133 NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
134 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0,
135 metaState, /* buttonState */ 0, MotionClassification::NONE,
136 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties,
137 &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
138 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700139 }
140
141 mRotaryEncoderScrollAccumulator.finishSync();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700142 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700143}
144
145} // namespace android