blob: 3af1d04073e9d524a90657161a5a144a26262978 [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
Arpit Singh8e6fb252023-04-06 11:49:17 +000026ExternalStylusInputMapper::ExternalStylusInputMapper(InputDeviceContext& deviceContext,
27 const InputReaderConfiguration& readerConfig)
28 : InputMapper(deviceContext, readerConfig), mTouchButtonAccumulator(deviceContext) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070029
Philip Junker4af3b3d2021-12-14 10:36:55 +010030uint32_t ExternalStylusInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070031 return AINPUT_SOURCE_STYLUS;
32}
33
Harry Cuttsd02ea102023-03-17 18:21:30 +000034void ExternalStylusInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070035 InputMapper::populateDeviceInfo(info);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +000036 if (mRawPressureAxis.valid) {
Harry Cuttsd02ea102023-03-17 18:21:30 +000037 info.addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, AINPUT_SOURCE_STYLUS, 0.0f, 1.0f, 0.0f,
38 0.0f, 0.0f);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +000039 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070040}
41
42void ExternalStylusInputMapper::dump(std::string& dump) {
43 dump += INDENT2 "External Stylus Input Mapper:\n";
44 dump += INDENT3 "Raw Stylus Axes:\n";
45 dumpRawAbsoluteAxisInfo(dump, mRawPressureAxis, "Pressure");
46 dump += INDENT3 "Stylus State:\n";
47 dumpStylusState(dump, mStylusState);
48}
49
Arpit Singh4be4eef2023-03-28 14:26:01 +000050std::list<NotifyArgs> ExternalStylusInputMapper::reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +000051 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000052 ConfigurationChanges changes) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070053 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPressureAxis);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +000054 mTouchButtonAccumulator.configure();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070055 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056}
57
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070058std::list<NotifyArgs> ExternalStylusInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080059 mSingleTouchMotionAccumulator.reset(getDeviceContext());
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +000060 mTouchButtonAccumulator.reset();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070061 return InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070062}
63
Harry Cuttsa32a1192024-06-04 15:10:31 +000064std::list<NotifyArgs> ExternalStylusInputMapper::process(const RawEvent& rawEvent) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070065 std::list<NotifyArgs> out;
Harry Cuttsa32a1192024-06-04 15:10:31 +000066 mSingleTouchMotionAccumulator.process(rawEvent);
67 mTouchButtonAccumulator.process(rawEvent);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070068
Harry Cuttsa32a1192024-06-04 15:10:31 +000069 if (rawEvent.type == EV_SYN && rawEvent.code == SYN_REPORT) {
70 out += sync(rawEvent.when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070071 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070072 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070073}
74
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070075std::list<NotifyArgs> ExternalStylusInputMapper::sync(nsecs_t when) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070076 mStylusState.clear();
77
78 mStylusState.when = when;
79
80 mStylusState.toolType = mTouchButtonAccumulator.getToolType();
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070081 if (mStylusState.toolType == ToolType::UNKNOWN) {
82 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070083 }
84
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070085 if (mRawPressureAxis.valid) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +000086 auto rawPressure = static_cast<float>(mSingleTouchMotionAccumulator.getAbsolutePressure());
87 mStylusState.pressure = (rawPressure - mRawPressureAxis.minValue) /
88 static_cast<float>(mRawPressureAxis.maxValue - mRawPressureAxis.minValue);
89 } else if (mTouchButtonAccumulator.hasButtonTouch()) {
90 mStylusState.pressure = mTouchButtonAccumulator.isHovering() ? 0.0f : 1.0f;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070091 }
92
93 mStylusState.buttons = mTouchButtonAccumulator.getButtonState();
94
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070095 return getContext()->dispatchExternalStylusState(mStylusState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096}
97
98} // namespace android