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