Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Pradhan | 9244aea | 2020-02-05 20:31:40 -0800 | [diff] [blame] | 17 | #include "../Macros.h" |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 18 | |
| 19 | #include "ExternalStylusInputMapper.h" |
| 20 | |
| 21 | #include "SingleTouchMotionAccumulator.h" |
| 22 | #include "TouchButtonAccumulator.h" |
| 23 | |
| 24 | namespace android { |
| 25 | |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 26 | ExternalStylusInputMapper::ExternalStylusInputMapper(InputDeviceContext& deviceContext, |
| 27 | const InputReaderConfiguration& readerConfig) |
| 28 | : InputMapper(deviceContext, readerConfig), mTouchButtonAccumulator(deviceContext) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 29 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 30 | uint32_t ExternalStylusInputMapper::getSources() const { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 31 | return AINPUT_SOURCE_STYLUS; |
| 32 | } |
| 33 | |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 34 | void ExternalStylusInputMapper::populateDeviceInfo(InputDeviceInfo& info) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 35 | InputMapper::populateDeviceInfo(info); |
Prabir Pradhan | 7ff19a1 | 2024-07-29 22:40:31 +0000 | [diff] [blame] | 36 | if (mRawPressureAxis || mTouchButtonAccumulator.hasButtonTouch()) { |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 37 | info.addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, AINPUT_SOURCE_STYLUS, 0.0f, 1.0f, 0.0f, |
| 38 | 0.0f, 0.0f); |
Prabir Pradhan | 3f7545f | 2022-10-19 16:56:39 +0000 | [diff] [blame] | 39 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void 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 Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 50 | std::list<NotifyArgs> ExternalStylusInputMapper::reconfigure(nsecs_t when, |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 51 | const InputReaderConfiguration& config, |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 52 | ConfigurationChanges changes) { |
Prabir Pradhan | 132f21c | 2024-07-25 16:48:30 +0000 | [diff] [blame] | 53 | mRawPressureAxis = getAbsoluteAxisInfo(ABS_PRESSURE); |
Prabir Pradhan | 4f05b5f | 2022-10-11 21:24:07 +0000 | [diff] [blame] | 54 | mTouchButtonAccumulator.configure(); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 55 | return {}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 58 | std::list<NotifyArgs> ExternalStylusInputMapper::reset(nsecs_t when) { |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 59 | mSingleTouchMotionAccumulator.reset(getDeviceContext()); |
Prabir Pradhan | 4f05b5f | 2022-10-11 21:24:07 +0000 | [diff] [blame] | 60 | mTouchButtonAccumulator.reset(); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 61 | return InputMapper::reset(when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 64 | std::list<NotifyArgs> ExternalStylusInputMapper::process(const RawEvent& rawEvent) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 65 | std::list<NotifyArgs> out; |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 66 | mSingleTouchMotionAccumulator.process(rawEvent); |
| 67 | mTouchButtonAccumulator.process(rawEvent); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 68 | |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 69 | if (rawEvent.type == EV_SYN && rawEvent.code == SYN_REPORT) { |
| 70 | out += sync(rawEvent.when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 71 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 72 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 75 | std::list<NotifyArgs> ExternalStylusInputMapper::sync(nsecs_t when) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 76 | mStylusState.clear(); |
| 77 | |
| 78 | mStylusState.when = when; |
| 79 | |
| 80 | mStylusState.toolType = mTouchButtonAccumulator.getToolType(); |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 81 | if (mStylusState.toolType == ToolType::UNKNOWN) { |
| 82 | mStylusState.toolType = ToolType::STYLUS; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Prabir Pradhan | 132f21c | 2024-07-25 16:48:30 +0000 | [diff] [blame] | 85 | if (mRawPressureAxis) { |
Prabir Pradhan | 3f7545f | 2022-10-19 16:56:39 +0000 | [diff] [blame] | 86 | auto rawPressure = static_cast<float>(mSingleTouchMotionAccumulator.getAbsolutePressure()); |
Prabir Pradhan | 132f21c | 2024-07-25 16:48:30 +0000 | [diff] [blame] | 87 | mStylusState.pressure = (rawPressure - mRawPressureAxis->minValue) / |
| 88 | static_cast<float>(mRawPressureAxis->maxValue - mRawPressureAxis->minValue); |
Prabir Pradhan | 3f7545f | 2022-10-19 16:56:39 +0000 | [diff] [blame] | 89 | } else if (mTouchButtonAccumulator.hasButtonTouch()) { |
| 90 | mStylusState.pressure = mTouchButtonAccumulator.isHovering() ? 0.0f : 1.0f; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | mStylusState.buttons = mTouchButtonAccumulator.getButtonState(); |
| 94 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 95 | return getContext()->dispatchExternalStylusState(mStylusState); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | } // namespace android |