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 
17 #include "../Macros.h"
18 
19 #include "ExternalStylusInputMapper.h"
20 
21 #include "SingleTouchMotionAccumulator.h"
22 #include "TouchButtonAccumulator.h"
23 
24 namespace android {
25 
ExternalStylusInputMapper(InputDeviceContext & deviceContext,const InputReaderConfiguration & readerConfig)26 ExternalStylusInputMapper::ExternalStylusInputMapper(InputDeviceContext& deviceContext,
27                                                      const InputReaderConfiguration& readerConfig)
28       : InputMapper(deviceContext, readerConfig), mTouchButtonAccumulator(deviceContext) {}
29 
getSources() const30 uint32_t ExternalStylusInputMapper::getSources() const {
31     return AINPUT_SOURCE_STYLUS;
32 }
33 
populateDeviceInfo(InputDeviceInfo & info)34 void ExternalStylusInputMapper::populateDeviceInfo(InputDeviceInfo& info) {
35     InputMapper::populateDeviceInfo(info);
36     if (mRawPressureAxis.valid) {
37         info.addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, AINPUT_SOURCE_STYLUS, 0.0f, 1.0f, 0.0f,
38                             0.0f, 0.0f);
39     }
40 }
41 
dump(std::string & dump)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 
reconfigure(nsecs_t when,const InputReaderConfiguration & config,ConfigurationChanges changes)50 std::list<NotifyArgs> ExternalStylusInputMapper::reconfigure(nsecs_t when,
51                                                              const InputReaderConfiguration& config,
52                                                              ConfigurationChanges changes) {
53     getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPressureAxis);
54     mTouchButtonAccumulator.configure();
55     return {};
56 }
57 
reset(nsecs_t when)58 std::list<NotifyArgs> ExternalStylusInputMapper::reset(nsecs_t when) {
59     mSingleTouchMotionAccumulator.reset(getDeviceContext());
60     mTouchButtonAccumulator.reset();
61     return InputMapper::reset(when);
62 }
63 
process(const RawEvent & rawEvent)64 std::list<NotifyArgs> ExternalStylusInputMapper::process(const RawEvent& rawEvent) {
65     std::list<NotifyArgs> out;
66     mSingleTouchMotionAccumulator.process(rawEvent);
67     mTouchButtonAccumulator.process(rawEvent);
68 
69     if (rawEvent.type == EV_SYN && rawEvent.code == SYN_REPORT) {
70         out += sync(rawEvent.when);
71     }
72     return out;
73 }
74 
sync(nsecs_t when)75 std::list<NotifyArgs> ExternalStylusInputMapper::sync(nsecs_t when) {
76     mStylusState.clear();
77 
78     mStylusState.when = when;
79 
80     mStylusState.toolType = mTouchButtonAccumulator.getToolType();
81     if (mStylusState.toolType == ToolType::UNKNOWN) {
82         mStylusState.toolType = ToolType::STYLUS;
83     }
84 
85     if (mRawPressureAxis.valid) {
86         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;
91     }
92 
93     mStylusState.buttons = mTouchButtonAccumulator.getButtonState();
94 
95     return getContext()->dispatchExternalStylusState(mStylusState);
96 }
97 
98 } // namespace android
99