blob: c0a35b196e28c8dd86f1e024f57a5a3aaa36cb82 [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
Harry Cuttsf13161a2023-03-08 14:15:49 +000023#include <optional>
24
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070025#include "CursorScrollAccumulator.h"
26
27namespace android {
28
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080029RotaryEncoderInputMapper::RotaryEncoderInputMapper(InputDeviceContext& deviceContext)
Michael Wrighta9cf4192022-12-01 23:46:39 +000030 : InputMapper(deviceContext), mOrientation(ui::ROTATION_0) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070031 mSource = AINPUT_SOURCE_ROTARY_ENCODER;
32}
33
34RotaryEncoderInputMapper::~RotaryEncoderInputMapper() {}
35
Philip Junker4af3b3d2021-12-14 10:36:55 +010036uint32_t RotaryEncoderInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070037 return mSource;
38}
39
Harry Cuttsd02ea102023-03-17 18:21:30 +000040void RotaryEncoderInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070041 InputMapper::populateDeviceInfo(info);
42
43 if (mRotaryEncoderScrollAccumulator.haveRelativeVWheel()) {
Harry Cuttsf13161a2023-03-08 14:15:49 +000044 const PropertyMap& config = getDeviceContext().getConfiguration();
45 std::optional<float> res = config.getFloat("device.res");
46 if (!res.has_value()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070047 ALOGW("Rotary Encoder device configuration file didn't specify resolution!\n");
48 }
Harry Cuttsf13161a2023-03-08 14:15:49 +000049 std::optional<float> scalingFactor = config.getFloat("device.scalingFactor");
50 if (!scalingFactor.has_value()) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070051 ALOGW("Rotary Encoder device configuration file didn't specify scaling factor,"
52 "default to 1.0!\n");
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070053 }
Harry Cuttsf13161a2023-03-08 14:15:49 +000054 mScalingFactor = scalingFactor.value_or(1.0f);
Harry Cuttsd02ea102023-03-17 18:21:30 +000055 info.addMotionRange(AMOTION_EVENT_AXIS_SCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
56 res.value_or(0.0f) * mScalingFactor);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070057 }
58}
59
60void RotaryEncoderInputMapper::dump(std::string& dump) {
61 dump += INDENT2 "Rotary Encoder Input Mapper:\n";
62 dump += StringPrintf(INDENT3 "HaveWheel: %s\n",
63 toString(mRotaryEncoderScrollAccumulator.haveRelativeVWheel()));
64}
65
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070066std::list<NotifyArgs> RotaryEncoderInputMapper::configure(nsecs_t when,
67 const InputReaderConfiguration* config,
68 uint32_t changes) {
69 std::list<NotifyArgs> out = InputMapper::configure(when, config, changes);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070070 if (!changes) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080071 mRotaryEncoderScrollAccumulator.configure(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070072 }
73 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
74 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +010075 config->getDisplayViewportByType(ViewportType::INTERNAL);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070076 if (internalViewport) {
77 mOrientation = internalViewport->orientation;
78 } else {
Michael Wrighta9cf4192022-12-01 23:46:39 +000079 mOrientation = ui::ROTATION_0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070080 }
81 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070082 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070083}
84
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070085std::list<NotifyArgs> RotaryEncoderInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080086 mRotaryEncoderScrollAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070087
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070088 return InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089}
90
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070091std::list<NotifyArgs> RotaryEncoderInputMapper::process(const RawEvent* rawEvent) {
92 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070093 mRotaryEncoderScrollAccumulator.process(rawEvent);
94
95 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070096 out += sync(rawEvent->when, rawEvent->readTime);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070097 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070098 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070099}
100
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700101std::list<NotifyArgs> RotaryEncoderInputMapper::sync(nsecs_t when, nsecs_t readTime) {
102 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700103
104 float scroll = mRotaryEncoderScrollAccumulator.getRelativeVWheel();
105 bool scrolled = scroll != 0;
106
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700107 // Send motion event.
108 if (scrolled) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800109 int32_t metaState = getContext()->getGlobalMetaState();
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800110 // This is not a pointer, so it's not associated with a display.
111 int32_t displayId = ADISPLAY_ID_NONE;
112
Michael Wrighta9cf4192022-12-01 23:46:39 +0000113 if (mOrientation == ui::ROTATION_180) {
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800114 scroll = -scroll;
115 }
116
117 PointerCoords pointerCoords;
118 pointerCoords.clear();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700119 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_SCROLL, scroll * mScalingFactor);
120
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800121 PointerProperties pointerProperties;
122 pointerProperties.clear();
123 pointerProperties.id = 0;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700124 pointerProperties.toolType = ToolType::UNKNOWN;
Yeabkal Wubshitb8c35482022-11-09 17:49:22 -0800125
126 uint32_t policyFlags = 0;
127 if (getDeviceContext().isExternal()) {
128 policyFlags |= POLICY_FLAG_WAKE;
129 }
130
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700131 out.push_back(
132 NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), mSource,
133 displayId, policyFlags, AMOTION_EVENT_ACTION_SCROLL, 0, 0,
134 metaState, /* buttonState */ 0, MotionClassification::NONE,
135 AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties,
136 &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION,
137 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /* videoFrames */ {}));
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700138 }
139
140 mRotaryEncoderScrollAccumulator.finishSync();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700141 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700142}
143
144} // namespace android