blob: a3206c2844c040867c7d5021ba1d01f320f254fd [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
Yeabkal Wubshitf1ed7052023-06-16 10:01:53 -070023#include <utils/Timers.h>
Harry Cuttsf13161a2023-03-08 14:15:49 +000024#include <optional>
25
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070026#include "CursorScrollAccumulator.h"
27
28namespace android {
29
Arpit Singh8e6fb252023-04-06 11:49:17 +000030RotaryEncoderInputMapper::RotaryEncoderInputMapper(InputDeviceContext& deviceContext,
31 const InputReaderConfiguration& readerConfig)
32 : InputMapper(deviceContext, readerConfig), mOrientation(ui::ROTATION_0) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070033 mSource = AINPUT_SOURCE_ROTARY_ENCODER;
34}
35
36RotaryEncoderInputMapper::~RotaryEncoderInputMapper() {}
37
Philip Junker4af3b3d2021-12-14 10:36:55 +010038uint32_t RotaryEncoderInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070039 return mSource;
40}
41
Harry Cuttsd02ea102023-03-17 18:21:30 +000042void RotaryEncoderInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070043 InputMapper::populateDeviceInfo(info);
44
45 if (mRotaryEncoderScrollAccumulator.haveRelativeVWheel()) {
Harry Cuttsf13161a2023-03-08 14:15:49 +000046 const PropertyMap& config = getDeviceContext().getConfiguration();
47 std::optional<float> res = config.getFloat("device.res");
48 if (!res.has_value()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070049 ALOGW("Rotary Encoder device configuration file didn't specify resolution!\n");
50 }
Harry Cuttsf13161a2023-03-08 14:15:49 +000051 std::optional<float> scalingFactor = config.getFloat("device.scalingFactor");
52 if (!scalingFactor.has_value()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070053 ALOGW("Rotary Encoder device configuration file didn't specify scaling factor,"
54 "default to 1.0!\n");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070055 }
Harry Cuttsf13161a2023-03-08 14:15:49 +000056 mScalingFactor = scalingFactor.value_or(1.0f);
Harry Cuttsd02ea102023-03-17 18:21:30 +000057 info.addMotionRange(AMOTION_EVENT_AXIS_SCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
58 res.value_or(0.0f) * mScalingFactor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070059 }
60}
61
62void RotaryEncoderInputMapper::dump(std::string& dump) {
63 dump += INDENT2 "Rotary Encoder Input Mapper:\n";
64 dump += StringPrintf(INDENT3 "HaveWheel: %s\n",
65 toString(mRotaryEncoderScrollAccumulator.haveRelativeVWheel()));
Yeabkal Wubshitf1ed7052023-06-16 10:01:53 -070066 dump += StringPrintf(INDENT3 "HaveSlopController: %s\n", toString(mSlopController != nullptr));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070067}
68
Arpit Singh4be4eef2023-03-28 14:26:01 +000069std::list<NotifyArgs> RotaryEncoderInputMapper::reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +000070 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000071 ConfigurationChanges changes) {
Arpit Singh4be4eef2023-03-28 14:26:01 +000072 std::list<NotifyArgs> out = InputMapper::reconfigure(when, config, changes);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000073 if (!changes.any()) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080074 mRotaryEncoderScrollAccumulator.configure(getDeviceContext());
Yeabkal Wubshitf1ed7052023-06-16 10:01:53 -070075
76 const PropertyMap& propertyMap = getDeviceContext().getConfiguration();
77 float slopThreshold = propertyMap.getInt("rotary_encoder.slop_threshold").value_or(0);
78 int32_t slopDurationNs = milliseconds_to_nanoseconds(
79 propertyMap.getInt("rotary_encoder.slop_duration_ms").value_or(0));
80 if (slopThreshold > 0 && slopDurationNs > 0) {
81 mSlopController = std::make_unique<SlopController>(slopThreshold, slopDurationNs);
82 } else {
83 mSlopController = nullptr;
84 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070085 }
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000086 if (!changes.any() || changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070087 std::optional<DisplayViewport> internalViewport =
Arpit Singhed6c3de2023-04-05 19:24:37 +000088 config.getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089 if (internalViewport) {
90 mOrientation = internalViewport->orientation;
91 } else {
Michael Wrighta9cf4192022-12-01 23:46:39 +000092 mOrientation = ui::ROTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070093 }
94 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070095 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096}
97
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070098std::list<NotifyArgs> RotaryEncoderInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080099 mRotaryEncoderScrollAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700100
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700101 return InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700102}
103
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700104std::list<NotifyArgs> RotaryEncoderInputMapper::process(const RawEvent* rawEvent) {
105 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700106 mRotaryEncoderScrollAccumulator.process(rawEvent);
107
108 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700109 out += sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700110 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700111 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700112}
113
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700114std::list<NotifyArgs> RotaryEncoderInputMapper::sync(nsecs_t when, nsecs_t readTime) {
115 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700116
117 float scroll = mRotaryEncoderScrollAccumulator.getRelativeVWheel();
Yeabkal Wubshit6b662762023-05-22 23:07:31 -0700118 if (mSlopController) {
119 scroll = mSlopController->consumeEvent(when, scroll);
120 }
121
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700122 bool scrolled = scroll != 0;
123
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700124 // Send motion event.
125 if (scrolled) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800126 int32_t metaState = getContext()->getGlobalMetaState();
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800127 // This is not a pointer, so it's not associated with a display.
Linnan Li13bf76a2024-05-05 19:18:02 +0800128 ui::LogicalDisplayId displayId = ui::ADISPLAY_ID_NONE;
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800129
Michael Wrighta9cf4192022-12-01 23:46:39 +0000130 if (mOrientation == ui::ROTATION_180) {
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800131 scroll = -scroll;
132 }
133
134 PointerCoords pointerCoords;
135 pointerCoords.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700136 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor);
137
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800138 PointerProperties pointerProperties;
139 pointerProperties.clear();
140 pointerProperties.id = 0;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700141 pointerProperties.toolType = ToolType::UNKNOWN;
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800142
143 uint32_t policyFlags = 0;
144 if (getDeviceContext().isExternal()) {
145 policyFlags |= POLICY_FLAG_WAKE;
146 }
147
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700148 out.push_back(
149 NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
150 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000151 metaState, /*buttonState=*/0, MotionClassification::NONE,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700152 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties,
153 &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
Harry Cutts101ee9b2023-07-06 18:04:14 +0000154 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /*videoFrames=*/{}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700155 }
156
157 mRotaryEncoderScrollAccumulator.finishSync();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700158 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700159}
160
161} // namespace android