blob: 212c9c9108067f524db8e0045a181d3ea4d5b512 [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 Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070018
19#include "TouchInputMapper.h"
20
21namespace android {
22
23/* Keeps track of the state of multi-touch protocol. */
24class MultiTouchMotionAccumulator {
25public:
26 class Slot {
27 public:
28 inline bool isInUse() const { return mInUse; }
29 inline int32_t getX() const { return mAbsMTPositionX; }
30 inline int32_t getY() const { return mAbsMTPositionY; }
31 inline int32_t getTouchMajor() const { return mAbsMTTouchMajor; }
32 inline int32_t getTouchMinor() const {
33 return mHaveAbsMTTouchMinor ? mAbsMTTouchMinor : mAbsMTTouchMajor;
34 }
35 inline int32_t getToolMajor() const { return mAbsMTWidthMajor; }
36 inline int32_t getToolMinor() const {
37 return mHaveAbsMTWidthMinor ? mAbsMTWidthMinor : mAbsMTWidthMajor;
38 }
39 inline int32_t getOrientation() const { return mAbsMTOrientation; }
40 inline int32_t getTrackingId() const { return mAbsMTTrackingId; }
41 inline int32_t getPressure() const { return mAbsMTPressure; }
42 inline int32_t getDistance() const { return mAbsMTDistance; }
43 inline int32_t getToolType() const;
44
45 private:
46 friend class MultiTouchMotionAccumulator;
47
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080048 bool mInUse = false;
49 bool mHaveAbsMTTouchMinor = false;
50 bool mHaveAbsMTWidthMinor = false;
51 bool mHaveAbsMTToolType = false;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080053 int32_t mAbsMTPositionX = 0;
54 int32_t mAbsMTPositionY = 0;
55 int32_t mAbsMTTouchMajor = 0;
56 int32_t mAbsMTTouchMinor = 0;
57 int32_t mAbsMTWidthMajor = 0;
58 int32_t mAbsMTWidthMinor = 0;
59 int32_t mAbsMTOrientation = 0;
60 int32_t mAbsMTTrackingId = -1;
61 int32_t mAbsMTPressure = 0;
62 int32_t mAbsMTDistance = 0;
63 int32_t mAbsMTToolType = 0;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070064
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080065 void clear() { *this = Slot(); }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070066 };
67
68 MultiTouchMotionAccumulator();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070069
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080070 void configure(InputDeviceContext& deviceContext, size_t slotCount, bool usingSlotsProtocol);
71 void reset(InputDeviceContext& deviceContext);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070072 void process(const RawEvent* rawEvent);
73 void finishSync();
74 bool hasStylus() const;
75
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080076 inline size_t getSlotCount() const { return mSlots.size(); }
77 inline const Slot& getSlot(size_t index) const {
78 LOG_ALWAYS_FATAL_IF(index < 0 || index >= mSlots.size(), "Invalid index: %zu", index);
79 return mSlots[index];
80 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070081
82private:
83 int32_t mCurrentSlot;
Siarhei Vishniakoubaf0b162022-02-16 11:12:36 -080084 std::vector<Slot> mSlots;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070085 bool mUsingSlotsProtocol;
86 bool mHaveStylus;
87
88 void clearSlots(int32_t initialSlot);
Arthur Hung9ad18942021-06-19 02:04:46 +000089 void warnIfNotInUse(const RawEvent& event, const Slot& slot);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090};
91
92class MultiTouchInputMapper : public TouchInputMapper {
93public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080094 explicit MultiTouchInputMapper(InputDeviceContext& deviceContext);
Michael Wright227c5542020-07-02 18:30:52 +010095 ~MultiTouchInputMapper() override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096
Michael Wright227c5542020-07-02 18:30:52 +010097 void reset(nsecs_t when) override;
98 void process(const RawEvent* rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070099
100protected:
Michael Wright227c5542020-07-02 18:30:52 +0100101 void syncTouch(nsecs_t when, RawState* outState) override;
102 void configureRawPointerAxes() override;
103 bool hasStylus() const override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700104
105private:
Prabir Pradhanf4d65b12022-02-10 07:15:38 -0800106 // simulate_stylus_with_touch is a debug mode that converts all finger pointers reported by this
107 // mapper's touchscreen into stylus pointers, and adds SOURCE_STYLUS to the input device.
108 // It is used to simulate stylus events for debugging and testing on a device that does not
109 // support styluses. It can be enabled using
110 // "adb shell setprop persist.debug.input.simulate_stylus_with_touch true",
111 // and requires a reboot to take effect.
112 inline bool shouldSimulateStylusWithTouch() const;
113
arthurhungcc7f9802020-04-30 17:55:40 +0800114 // If the slot is in use, return the bit id. Return std::nullopt otherwise.
115 std::optional<int32_t> getActiveBitId(const MultiTouchMotionAccumulator::Slot& inSlot);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700116 MultiTouchMotionAccumulator mMultiTouchMotionAccumulator;
117
118 // Specifies the pointer id bits that are in use, and their associated tracking id.
119 BitSet32 mPointerIdBits;
120 int32_t mPointerTrackingIdMap[MAX_POINTER_ID + 1];
121};
122
123} // namespace android