xref: /aosp_15_r20/frameworks/native/services/inputflinger/reader/mapper/CapturedTouchpadEventConverter.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright 2023 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 #pragma once
18 
19 #include <bitset>
20 #include <list>
21 #include <map>
22 #include <set>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include <android/input.h>
28 #include <input/Input.h>
29 #include <utils/Timers.h>
30 
31 #include "EventHub.h"
32 #include "InputDevice.h"
33 #include "accumulator/CursorButtonAccumulator.h"
34 #include "accumulator/MultiTouchMotionAccumulator.h"
35 #include "accumulator/TouchButtonAccumulator.h"
36 
37 namespace android {
38 
39 class CapturedTouchpadEventConverter {
40 public:
41     explicit CapturedTouchpadEventConverter(InputReaderContext& readerContext,
42                                             const InputDeviceContext& deviceContext,
43                                             MultiTouchMotionAccumulator& motionAccumulator,
44                                             int32_t deviceId);
45     std::string dump() const;
46     void populateMotionRanges(InputDeviceInfo& info) const;
47     void reset();
48     [[nodiscard]] std::list<NotifyArgs> process(const RawEvent& rawEvent);
49 
50 private:
51     void tryAddRawMotionRange(InputDeviceInfo& deviceInfo, int32_t androidAxis,
52                               int32_t evdevAxis) const;
53     void tryAddRawMotionRangeWithRelative(InputDeviceInfo& deviceInfo, int32_t androidAxis,
54                                           int32_t androidRelativeAxis, int32_t evdevAxis) const;
55     [[nodiscard]] std::list<NotifyArgs> sync(nsecs_t when, nsecs_t readTime);
56     [[nodiscard]] NotifyMotionArgs makeMotionArgs(nsecs_t when, nsecs_t readTime, int32_t action,
57                                                   const std::vector<PointerCoords>& coords,
58                                                   const std::vector<PointerProperties>& properties,
59                                                   int32_t actionButton = 0, int32_t flags = 0);
60     PointerCoords makePointerCoordsForSlot(size_t slotNumber);
61     int32_t allocatePointerIdToSlot(size_t slotNumber);
62     void freePointerIdForSlot(size_t slotNumber);
63 
64     const int32_t mDeviceId;
65     InputReaderContext& mReaderContext;
66     const InputDeviceContext& mDeviceContext;
67     CursorButtonAccumulator mCursorButtonAccumulator;
68     MultiTouchMotionAccumulator& mMotionAccumulator;
69 
70     float mOrientationScale = 0;
71     float mPressureScale = 1;
72     float mSizeScale = 0;
73     bool mHasTouchMajor;
74     const bool mHasTouchMinor;
75     bool mHasToolMajor;
76     const bool mHasToolMinor;
77     nsecs_t mDownTime = 0;
78     uint32_t mButtonState = 0;
79 
80     std::bitset<MAX_POINTER_ID + 1> mPointerIdsInUse;
81     std::map<size_t, int32_t> mPointerIdForSlotNumber;
82     std::map<size_t, std::pair<float, float>> mPreviousCoordsForSlotNumber;
83 };
84 
85 } // namespace android
86