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