blob: 594ff42f8709d68eb6a26213ea366b52cabcb2af [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
34uint32_t RotaryEncoderInputMapper::getSources() {
35 return mSource;
36}
37
38void RotaryEncoderInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
39 InputMapper::populateDeviceInfo(info);
40
41 if (mRotaryEncoderScrollAccumulator.haveRelativeVWheel()) {
42 float res = 0.0f;
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080043 if (!getDeviceContext().getConfiguration().tryGetProperty(String8("device.res"), res)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070044 ALOGW("Rotary Encoder device configuration file didn't specify resolution!\n");
45 }
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080046 if (!getDeviceContext().getConfiguration().tryGetProperty(String8("device.scalingFactor"),
47 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
63void RotaryEncoderInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config,
64 uint32_t changes) {
65 InputMapper::configure(when, config, changes);
66 if (!changes) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080067 mRotaryEncoderScrollAccumulator.configure(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070068 }
69 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
70 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +010071 config->getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070072 if (internalViewport) {
73 mOrientation = internalViewport->orientation;
74 } else {
75 mOrientation = DISPLAY_ORIENTATION_0;
76 }
77 }
78}
79
80void RotaryEncoderInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080081 mRotaryEncoderScrollAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070082
83 InputMapper::reset(when);
84}
85
86void 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
94void 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. Lewis26ec2222020-01-10 16:30:54 -0800111 if (scrolled && getDeviceContext().isExternal()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700112 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. Lewis26ec2222020-01-10 16:30:54 -0800121 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700122 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor);
123
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800124 NotifyMotionArgs scrollArgs(getContext()->getNextId(), when, getDeviceId(), mSource,
125 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0,
126 metaState, /* buttonState */ 0, MotionClassification::NONE,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700127 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties,
128 &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
129 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /* videoFrames */ {});
130 getListener()->notifyMotion(&scrollArgs);
131 }
132
133 mRotaryEncoderScrollAccumulator.finishSync();
134}
135
136} // namespace android