blob: 0404c9acc1b6876ddc0f98e13533b58abe73a4c7 [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
Prabir Pradhan9244aea2020-02-05 20:31:40 -080017#include "../Macros.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070018
19#include "ExternalStylusInputMapper.h"
20
21#include "SingleTouchMotionAccumulator.h"
22#include "TouchButtonAccumulator.h"
23
24namespace android {
25
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080026ExternalStylusInputMapper::ExternalStylusInputMapper(InputDeviceContext& deviceContext)
27 : InputMapper(deviceContext) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070028
Philip Junker4af3b3d2021-12-14 10:36:55 +010029uint32_t ExternalStylusInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070030 return AINPUT_SOURCE_STYLUS;
31}
32
33void ExternalStylusInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
34 InputMapper::populateDeviceInfo(info);
35 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, AINPUT_SOURCE_STYLUS, 0.0f, 1.0f, 0.0f, 0.0f,
36 0.0f);
37}
38
39void ExternalStylusInputMapper::dump(std::string& dump) {
40 dump += INDENT2 "External Stylus Input Mapper:\n";
41 dump += INDENT3 "Raw Stylus Axes:\n";
42 dumpRawAbsoluteAxisInfo(dump, mRawPressureAxis, "Pressure");
43 dump += INDENT3 "Stylus State:\n";
44 dumpStylusState(dump, mStylusState);
45}
46
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070047std::list<NotifyArgs> ExternalStylusInputMapper::configure(nsecs_t when,
48 const InputReaderConfiguration* config,
49 uint32_t changes) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070050 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPressureAxis);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080051 mTouchButtonAccumulator.configure(getDeviceContext());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070052 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070053}
54
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070055std::list<NotifyArgs> ExternalStylusInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080056 mSingleTouchMotionAccumulator.reset(getDeviceContext());
57 mTouchButtonAccumulator.reset(getDeviceContext());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070058 return InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070059}
60
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070061std::list<NotifyArgs> ExternalStylusInputMapper::process(const RawEvent* rawEvent) {
62 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070063 mSingleTouchMotionAccumulator.process(rawEvent);
64 mTouchButtonAccumulator.process(rawEvent);
65
66 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070067 out += sync(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070068 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070069 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070070}
71
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070072std::list<NotifyArgs> ExternalStylusInputMapper::sync(nsecs_t when) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070073 mStylusState.clear();
74
75 mStylusState.when = when;
76
77 mStylusState.toolType = mTouchButtonAccumulator.getToolType();
78 if (mStylusState.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
79 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
80 }
81
82 int32_t pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
83 if (mRawPressureAxis.valid) {
84 mStylusState.pressure = float(pressure) / mRawPressureAxis.maxValue;
85 } else if (mTouchButtonAccumulator.isToolActive()) {
86 mStylusState.pressure = 1.0f;
87 } else {
88 mStylusState.pressure = 0.0f;
89 }
90
91 mStylusState.buttons = mTouchButtonAccumulator.getButtonState();
92
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070093 return getContext()->dispatchExternalStylusState(mStylusState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070094}
95
96} // namespace android