1 /*
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 
17 // clang-format off
18 #include "../Macros.h"
19 // clang-format on
20 
21 #include "RotaryEncoderInputMapper.h"
22 
23 #include "CursorScrollAccumulator.h"
24 
25 namespace android {
26 
RotaryEncoderInputMapper(InputDeviceContext & deviceContext)27 RotaryEncoderInputMapper::RotaryEncoderInputMapper(InputDeviceContext& deviceContext)
28       : InputMapper(deviceContext), mOrientation(DISPLAY_ORIENTATION_0) {
29     mSource = AINPUT_SOURCE_ROTARY_ENCODER;
30 }
31 
~RotaryEncoderInputMapper()32 RotaryEncoderInputMapper::~RotaryEncoderInputMapper() {}
33 
getSources()34 uint32_t RotaryEncoderInputMapper::getSources() {
35     return mSource;
36 }
37 
populateDeviceInfo(InputDeviceInfo * info)38 void RotaryEncoderInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
39     InputMapper::populateDeviceInfo(info);
40 
41     if (mRotaryEncoderScrollAccumulator.haveRelativeVWheel()) {
42         float res = 0.0f;
43         if (!getDeviceContext().getConfiguration().tryGetProperty(String8("device.res"), res)) {
44             ALOGW("Rotary Encoder device configuration file didn't specify resolution!\n");
45         }
46         if (!getDeviceContext().getConfiguration().tryGetProperty(String8("device.scalingFactor"),
47                                                                   mScalingFactor)) {
48             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 
dump(std::string & dump)57 void 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 
configure(nsecs_t when,const InputReaderConfiguration * config,uint32_t changes)63 void RotaryEncoderInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config,
64                                          uint32_t changes) {
65     InputMapper::configure(when, config, changes);
66     if (!changes) {
67         mRotaryEncoderScrollAccumulator.configure(getDeviceContext());
68     }
69     if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
70         std::optional<DisplayViewport> internalViewport =
71                 config->getDisplayViewportByType(ViewportType::INTERNAL);
72         if (internalViewport) {
73             mOrientation = internalViewport->orientation;
74         } else {
75             mOrientation = DISPLAY_ORIENTATION_0;
76         }
77     }
78 }
79 
reset(nsecs_t when)80 void RotaryEncoderInputMapper::reset(nsecs_t when) {
81     mRotaryEncoderScrollAccumulator.reset(getDeviceContext());
82 
83     InputMapper::reset(when);
84 }
85 
process(const RawEvent * rawEvent)86 void RotaryEncoderInputMapper::process(const RawEvent* rawEvent) {
87     mRotaryEncoderScrollAccumulator.process(rawEvent);
88 
89     if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
90         sync(rawEvent->when, rawEvent->readTime);
91     }
92 }
93 
sync(nsecs_t when,nsecs_t readTime)94 void RotaryEncoderInputMapper::sync(nsecs_t when, nsecs_t readTime) {
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;
111     if (scrolled && getDeviceContext().isExternal()) {
112         policyFlags |= POLICY_FLAG_WAKE;
113     }
114 
115     if (mOrientation == DISPLAY_ORIENTATION_180) {
116         scroll = -scroll;
117     }
118 
119     // Send motion event.
120     if (scrolled) {
121         int32_t metaState = getContext()->getGlobalMetaState();
122         pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor);
123 
124         NotifyMotionArgs scrollArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
125                                     mSource, displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0,
126                                     0, metaState, /* buttonState */ 0, MotionClassification::NONE,
127                                     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
137