blob: 19a79d775195c0e691ae1ad85b0f2d02ed554095 [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)
Michael Wrighta9cf4192022-12-01 23:46:39 +000028 : InputMapper(deviceContext), mOrientation(ui::ROTATION_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 {
Michael Wrighta9cf4192022-12-01 23:46:39 +000076 mOrientation = ui::ROTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070077 }
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
101 float scroll = mRotaryEncoderScrollAccumulator.getRelativeVWheel();
102 bool scrolled = scroll != 0;
103
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700104 // Send motion event.
105 if (scrolled) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800106 int32_t metaState = getContext()->getGlobalMetaState();
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800107 // This is not a pointer, so it's not associated with a display.
108 int32_t displayId = ADISPLAY_ID_NONE;
109
Michael Wrighta9cf4192022-12-01 23:46:39 +0000110 if (mOrientation == ui::ROTATION_180) {
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800111 scroll = -scroll;
112 }
113
114 PointerCoords pointerCoords;
115 pointerCoords.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700116 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor);
117
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800118 PointerProperties pointerProperties;
119 pointerProperties.clear();
120 pointerProperties.id = 0;
121 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
122
123 uint32_t policyFlags = 0;
124 if (getDeviceContext().isExternal()) {
125 policyFlags |= POLICY_FLAG_WAKE;
126 }
127
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700128 out.push_back(
129 NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
130 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0,
131 metaState, /* buttonState */ 0, MotionClassification::NONE,
132 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties,
133 &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
134 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700135 }
136
137 mRotaryEncoderScrollAccumulator.finishSync();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700138 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700139}
140
141} // namespace android