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(InputDevice * device)26 ExternalStylusInputMapper::ExternalStylusInputMapper(InputDevice* device) : InputMapper(device) {}
27
getSources()28 uint32_t ExternalStylusInputMapper::getSources() {
29 return AINPUT_SOURCE_STYLUS;
30 }
31
populateDeviceInfo(InputDeviceInfo * info)32 void ExternalStylusInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
33 InputMapper::populateDeviceInfo(info);
34 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, AINPUT_SOURCE_STYLUS, 0.0f, 1.0f, 0.0f, 0.0f,
35 0.0f);
36 }
37
dump(std::string & dump)38 void ExternalStylusInputMapper::dump(std::string& dump) {
39 dump += INDENT2 "External Stylus Input Mapper:\n";
40 dump += INDENT3 "Raw Stylus Axes:\n";
41 dumpRawAbsoluteAxisInfo(dump, mRawPressureAxis, "Pressure");
42 dump += INDENT3 "Stylus State:\n";
43 dumpStylusState(dump, mStylusState);
44 }
45
configure(nsecs_t when,const InputReaderConfiguration * config,uint32_t changes)46 void ExternalStylusInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config,
47 uint32_t changes) {
48 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPressureAxis);
49 mTouchButtonAccumulator.configure(getDevice());
50 }
51
reset(nsecs_t when)52 void ExternalStylusInputMapper::reset(nsecs_t when) {
53 InputDevice* device = getDevice();
54 mSingleTouchMotionAccumulator.reset(device);
55 mTouchButtonAccumulator.reset(device);
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 mContext->dispatchExternalStylusState(mStylusState);
90 }
91
92 } // namespace android
93