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)26 ExternalStylusInputMapper::ExternalStylusInputMapper(InputDeviceContext& deviceContext)
27 : InputMapper(deviceContext) {}
28
getSources()29 uint32_t ExternalStylusInputMapper::getSources() {
30 return AINPUT_SOURCE_STYLUS;
31 }
32
populateDeviceInfo(InputDeviceInfo * info)33 void ExternalStylusInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
34 InputMapper::populateDeviceInfo(info);
35 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, AINPUT_SOURCE_STYLUS, 0.0f, 1.0f, 0.0f, 0.0f,
36 0.0f);
37 }
38
dump(std::string & dump)39 void ExternalStylusInputMapper::dump(std::string& dump) {
40 dump += INDENT2 "External Stylus Input Mapper:\n";
41 dump += INDENT3 "Raw Stylus Axes:\n";
42 dumpRawAbsoluteAxisInfo(dump, mRawPressureAxis, "Pressure");
43 dump += INDENT3 "Stylus State:\n";
44 dumpStylusState(dump, mStylusState);
45 }
46
configure(nsecs_t when,const InputReaderConfiguration * config,uint32_t changes)47 void ExternalStylusInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config,
48 uint32_t changes) {
49 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPressureAxis);
50 mTouchButtonAccumulator.configure(getDeviceContext());
51 }
52
reset(nsecs_t when)53 void ExternalStylusInputMapper::reset(nsecs_t when) {
54 mSingleTouchMotionAccumulator.reset(getDeviceContext());
55 mTouchButtonAccumulator.reset(getDeviceContext());
56 InputMapper::reset(when);
57 }
58
process(const RawEvent * rawEvent)59 void ExternalStylusInputMapper::process(const RawEvent* rawEvent) {
60 mSingleTouchMotionAccumulator.process(rawEvent);
61 mTouchButtonAccumulator.process(rawEvent);
62
63 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
64 sync(rawEvent->when);
65 }
66 }
67
sync(nsecs_t when)68 void ExternalStylusInputMapper::sync(nsecs_t when) {
69 mStylusState.clear();
70
71 mStylusState.when = when;
72
73 mStylusState.toolType = mTouchButtonAccumulator.getToolType();
74 if (mStylusState.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
75 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
76 }
77
78 int32_t pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
79 if (mRawPressureAxis.valid) {
80 mStylusState.pressure = float(pressure) / mRawPressureAxis.maxValue;
81 } else if (mTouchButtonAccumulator.isToolActive()) {
82 mStylusState.pressure = 1.0f;
83 } else {
84 mStylusState.pressure = 0.0f;
85 }
86
87 mStylusState.buttons = mTouchButtonAccumulator.getButtonState();
88
89 getContext()->dispatchExternalStylusState(mStylusState);
90 }
91
92 } // namespace android
93