blob: 2809939c86d31ddc6f9b22403ddc2b06d44286cd [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)
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +000027 : InputMapper(deviceContext), mTouchButtonAccumulator(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);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +000035 if (mRawPressureAxis.valid) {
36 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, AINPUT_SOURCE_STYLUS, 0.0f, 1.0f, 0.0f,
37 0.0f, 0.0f);
38 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070039}
40
41void ExternalStylusInputMapper::dump(std::string& dump) {
42 dump += INDENT2 "External Stylus Input Mapper:\n";
43 dump += INDENT3 "Raw Stylus Axes:\n";
44 dumpRawAbsoluteAxisInfo(dump, mRawPressureAxis, "Pressure");
45 dump += INDENT3 "Stylus State:\n";
46 dumpStylusState(dump, mStylusState);
47}
48
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070049std::list<NotifyArgs> ExternalStylusInputMapper::configure(nsecs_t when,
50 const InputReaderConfiguration* config,
51 uint32_t changes) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPressureAxis);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +000053 mTouchButtonAccumulator.configure();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070054 return {};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070055}
56
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070057std::list<NotifyArgs> ExternalStylusInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080058 mSingleTouchMotionAccumulator.reset(getDeviceContext());
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +000059 mTouchButtonAccumulator.reset();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070060 return InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070061}
62
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070063std::list<NotifyArgs> ExternalStylusInputMapper::process(const RawEvent* rawEvent) {
64 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070065 mSingleTouchMotionAccumulator.process(rawEvent);
66 mTouchButtonAccumulator.process(rawEvent);
67
68 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070069 out += sync(rawEvent->when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070070 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070071 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070072}
73
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070074std::list<NotifyArgs> ExternalStylusInputMapper::sync(nsecs_t when) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070075 mStylusState.clear();
76
77 mStylusState.when = when;
78
79 mStylusState.toolType = mTouchButtonAccumulator.getToolType();
80 if (mStylusState.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
81 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
82 }
83
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070084 if (mRawPressureAxis.valid) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +000085 auto rawPressure = static_cast<float>(mSingleTouchMotionAccumulator.getAbsolutePressure());
86 mStylusState.pressure = (rawPressure - mRawPressureAxis.minValue) /
87 static_cast<float>(mRawPressureAxis.maxValue - mRawPressureAxis.minValue);
88 } else if (mTouchButtonAccumulator.hasButtonTouch()) {
89 mStylusState.pressure = mTouchButtonAccumulator.isHovering() ? 0.0f : 1.0f;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090 }
91
92 mStylusState.buttons = mTouchButtonAccumulator.getButtonState();
93
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070094 return getContext()->dispatchExternalStylusState(mStylusState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070095}
96
97} // namespace android