blob: 29a1bda3ee39aa126152a8e9c02df08619b3d17b [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
23#include "CursorScrollAccumulator.h"
24
25namespace android {
26
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080027RotaryEncoderInputMapper::RotaryEncoderInputMapper(InputDeviceContext& deviceContext)
28 : InputMapper(deviceContext), mOrientation(DISPLAY_ORIENTATION_0) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070029 mSource = AINPUT_SOURCE_ROTARY_ENCODER;
30}
31
32RotaryEncoderInputMapper::~RotaryEncoderInputMapper() {}
33
Philip Junker4af3b3d2021-12-14 10:36:55 +010034uint32_t RotaryEncoderInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070035 return mSource;
36}
37
38void RotaryEncoderInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
39 InputMapper::populateDeviceInfo(info);
40
41 if (mRotaryEncoderScrollAccumulator.haveRelativeVWheel()) {
42 float res = 0.0f;
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070043 if (!getDeviceContext().getConfiguration().tryGetProperty("device.res", res)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070044 ALOGW("Rotary Encoder device configuration file didn't specify resolution!\n");
45 }
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -070046 if (!getDeviceContext().getConfiguration().tryGetProperty("device.scalingFactor",
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080047 mScalingFactor)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070048 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
57void 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 Vishniakou2935db72022-09-22 13:35:22 -070063std::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 Pradhanbaa5c822019-08-30 15:27:05 -070067 if (!changes) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080068 mRotaryEncoderScrollAccumulator.configure(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070069 }
70 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
71 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +010072 config->getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070073 if (internalViewport) {
74 mOrientation = internalViewport->orientation;
75 } else {
76 mOrientation = DISPLAY_ORIENTATION_0;
77 }
78 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070079 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070080}
81
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070082std::list<NotifyArgs> RotaryEncoderInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080083 mRotaryEncoderScrollAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070084
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070085 return InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070086}
87
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070088std::list<NotifyArgs> RotaryEncoderInputMapper::process(const RawEvent* rawEvent) {
89 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090 mRotaryEncoderScrollAccumulator.process(rawEvent);
91
92 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070093 out += sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094 }
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::sync(nsecs_t when, nsecs_t readTime) {
99 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700100 PointerCoords pointerCoords;
101 pointerCoords.clear();
102
103 PointerProperties pointerProperties;
104 pointerProperties.clear();
105 pointerProperties.id = 0;
106 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
107
108 float scroll = mRotaryEncoderScrollAccumulator.getRelativeVWheel();
109 bool scrolled = scroll != 0;
110
111 // This is not a pointer, so it's not associated with a display.
112 int32_t displayId = ADISPLAY_ID_NONE;
113
114 // Moving the rotary encoder should wake the device (if specified).
115 uint32_t policyFlags = 0;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800116 if (scrolled && getDeviceContext().isExternal()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700117 policyFlags |= POLICY_FLAG_WAKE;
118 }
119
120 if (mOrientation == DISPLAY_ORIENTATION_180) {
121 scroll = -scroll;
122 }
123
124 // Send motion event.
125 if (scrolled) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800126 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700127 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor);
128
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700129 out.push_back(
130 NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
131 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0,
132 metaState, /* buttonState */ 0, MotionClassification::NONE,
133 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties,
134 &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
135 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700136 }
137
138 mRotaryEncoderScrollAccumulator.finishSync();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700139 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700140}
141
142} // namespace android