blob: 37d1d7499ce724c1ea922b4506ef4645367f65a8 [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
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 Pradhan9244aea2020-02-05 20:31:40 -080017#include "../Macros.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070018
19#include "ExternalStylusInputMapper.h"
20
21#include "SingleTouchMotionAccumulator.h"
22#include "TouchButtonAccumulator.h"
23
24namespace android {
25
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080026ExternalStylusInputMapper::ExternalStylusInputMapper(InputDeviceContext& deviceContext)
27 : InputMapper(deviceContext) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070028
29uint32_t ExternalStylusInputMapper::getSources() {
30 return AINPUT_SOURCE_STYLUS;
31}
32
33void 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
39void 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
47void ExternalStylusInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config,
48 uint32_t changes) {
49 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPressureAxis);
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080050 mTouchButtonAccumulator.configure(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070051}
52
53void ExternalStylusInputMapper::reset(nsecs_t when) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080054 mSingleTouchMotionAccumulator.reset(getDeviceContext());
55 mTouchButtonAccumulator.reset(getDeviceContext());
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056 InputMapper::reset(when);
57}
58
59void 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
68void 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
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080089 getContext()->dispatchExternalStylusState(mStylusState);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090}
91
92} // namespace android