blob: 5220b102110135defdff914815a36ca969437c80 [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;
Yeabkal Wubshit6b662762023-05-22 23:07:31 -070033
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 Pradhanbaa5c822019-08-30 15:27:05 -070041}
42
43RotaryEncoderInputMapper::~RotaryEncoderInputMapper() {}
44
Philip Junker4af3b3d2021-12-14 10:36:55 +010045uint32_t RotaryEncoderInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070046 return mSource;
47}
48
Harry Cuttsd02ea102023-03-17 18:21:30 +000049void RotaryEncoderInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070050 InputMapper::populateDeviceInfo(info);
51
52 if (mRotaryEncoderScrollAccumulator.haveRelativeVWheel()) {
Harry Cuttsf13161a2023-03-08 14:15:49 +000053 const PropertyMap& config = getDeviceContext().getConfiguration();
54 std::optional<float> res = config.getFloat("device.res");
55 if (!res.has_value()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056 ALOGW("Rotary Encoder device configuration file didn't specify resolution!\n");
57 }
Harry Cuttsf13161a2023-03-08 14:15:49 +000058 std::optional<float> scalingFactor = config.getFloat("device.scalingFactor");
59 if (!scalingFactor.has_value()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070060 ALOGW("Rotary Encoder device configuration file didn't specify scaling factor,"
61 "default to 1.0!\n");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070062 }
Harry Cuttsf13161a2023-03-08 14:15:49 +000063 mScalingFactor = scalingFactor.value_or(1.0f);
Harry Cuttsd02ea102023-03-17 18:21:30 +000064 info.addMotionRange(AMOTION_EVENT_AXIS_SCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
65 res.value_or(0.0f) * mScalingFactor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070066 }
67}
68
69void 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 Singh4be4eef2023-03-28 14:26:01 +000075std::list<NotifyArgs> RotaryEncoderInputMapper::reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +000076 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000077 ConfigurationChanges changes) {
Arpit Singh4be4eef2023-03-28 14:26:01 +000078 std::list<NotifyArgs> out = InputMapper::reconfigure(when, config, changes);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000079 if (!changes.any()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080080 mRotaryEncoderScrollAccumulator.configure(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070081 }
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000082 if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070083 std::optional<DisplayViewport> internalViewport =
Arpit Singhed6c3de2023-04-05 19:24:37 +000084 config.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070085 if (internalViewport) {
86 mOrientation = internalViewport->orientation;
87 } else {
Michael Wrighta9cf4192022-12-01 23:46:39 +000088 mOrientation = ui::ROTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089 }
90 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070091 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070092}
93
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070094std::list<NotifyArgs> RotaryEncoderInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080095 mRotaryEncoderScrollAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070097 return InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098}
99
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700100std::list<NotifyArgs> RotaryEncoderInputMapper::process(const RawEvent* rawEvent) {
101 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700102 mRotaryEncoderScrollAccumulator.process(rawEvent);
103
104 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700105 out += sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700106 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700107 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700108}
109
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700110std::list<NotifyArgs> RotaryEncoderInputMapper::sync(nsecs_t when, nsecs_t readTime) {
111 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700112
113 float scroll = mRotaryEncoderScrollAccumulator.getRelativeVWheel();
Yeabkal Wubshit6b662762023-05-22 23:07:31 -0700114 if (mSlopController) {
115 scroll = mSlopController->consumeEvent(when, scroll);
116 }
117
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700118 bool scrolled = scroll != 0;
119
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700120 // Send motion event.
121 if (scrolled) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800122 int32_t metaState = getContext()->getGlobalMetaState();
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800123 // This is not a pointer, so it's not associated with a display.
124 int32_t displayId = ADISPLAY_ID_NONE;
125
Michael Wrighta9cf4192022-12-01 23:46:39 +0000126 if (mOrientation == ui::ROTATION_180) {
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800127 scroll = -scroll;
128 }
129
130 PointerCoords pointerCoords;
131 pointerCoords.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700132 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor);
133
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800134 PointerProperties pointerProperties;
135 pointerProperties.clear();
136 pointerProperties.id = 0;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700137 pointerProperties.toolType = ToolType::UNKNOWN;
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800138
139 uint32_t policyFlags = 0;
140 if (getDeviceContext().isExternal()) {
141 policyFlags |= POLICY_FLAG_WAKE;
142 }
143
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700144 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 Pradhanbaa5c822019-08-30 15:27:05 -0700151 }
152
153 mRotaryEncoderScrollAccumulator.finishSync();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700154 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700155}
156
157} // namespace android