• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef LIBINPUT_ADAPTER_H
17 #define LIBINPUT_ADAPTER_H
18 
19 #include "hotplug_detector.h"
20 #include "libinput.h"
21 
22 namespace OHOS {
23 namespace MMI {
24 typedef std::function<void(void *event, int64_t frameTime)> FunInputEvent;
25 typedef std::function<int32_t(double screenX,
26                               double screenY,
27                               int touchId,
28                               int32_t eventType,
29                               double touchPressure,
30                               int32_t longAxis,
31                               int32_t shortAxis)> HandleTouchPoint;
32 typedef std::function<int32_t(int& toggleCodeFirst, int& toggleCodeSecond, int& keyCode)> GetMessage;
33 typedef std::function<void(std::vector<std::vector<int32_t>>& retMsgList)> GetAllTouchMessage;
34 typedef std::function<void()> ClearTouchMessage;
35 typedef std::function<void(std::vector<std::vector<int32_t>>& retMsgList)> GetAllKeyMessage;
36 typedef std::function<void()> ClearKeyMessage;
37 typedef std::function<void()> HardwareKeyEventDetected;
38 typedef std::function<int32_t()> GetKeyboardActivationState;
39 
40 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
41 enum VKeyboardMessageType {
42     VNoMessage = -1,
43     VKeyPressed = 0,
44     VCombinationKeyPressed = 1,
45     VStartLongPressControl = 16,
46     VStopLongPressControl = 17,
47     VSwitchLayout = 18,
48 };
49 enum class VTPStateMachineMessageType : int32_t {
50     UNKNOWN = 0,
51     POINTER_MOVE = 1,
52     LEFT_CLICK_DOWN = 2,
53     LEFT_CLICK_UP = 3,
54     RIGHT_CLICK_DOWN = 4,
55     RIGHT_CLICK_UP = 5,
56     SCROLL_BEGIN = 6,
57     SCROLL_UPDATE = 7,
58     SCROLL_END = 8,
59     PINCH_BEGIN = 9,
60     PINCH_UPDATE = 10,
61     PINCH_END = 11,
62     PAN_BEGIN = 12,
63     PAN_UPDATE = 13,
64     PAN_END = 14,
65     ROT_BEGIN = 15,
66     ROT_UPDATE = 16,
67     ROT_END = 17,
68     LEFT_TOUCH_DOWN = 18,
69     LEFT_TOUCH_UP = 19,
70 };
71 
72 enum class VKeyboardActivation : int32_t {
73     INACTIVE = 0,
74     ACTIVATED = 1,
75     TOUCH_CANCEL = 2,
76     TOUCH_DROP = 3,
77     EIGHT_FINGERS_UP = 4,
78 };
79 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
80 class LibinputAdapter final {
81 public:
82     static int32_t DeviceLedUpdate(struct libinput_device *device, int32_t funcKey, bool isEnable);
83     LibinputAdapter() = default;
84     DISALLOW_COPY_AND_MOVE(LibinputAdapter);
85     ~LibinputAdapter() = default;
86     bool Init(FunInputEvent funInputEvent);
87     void EventDispatch(int32_t fd);
88     void Stop();
89     void ProcessPendingEvents();
90     void ReloadDevice();
91 
GetInputFds()92     auto GetInputFds() const
93     {
94         return std::array{fd_, hotplugDetector_.GetFd()};
95     }
96 
97     void InitVKeyboard(HandleTouchPoint handleTouchPoint,
98         GetMessage getMessage,
99         GetAllTouchMessage getAllTouchMessage,
100         ClearTouchMessage clearTouchMessage,
101         GetAllKeyMessage getAllKeyMessage,
102         ClearKeyMessage clearKeyMessage,
103         HardwareKeyEventDetected hardwareKeyEventDetected,
104         GetKeyboardActivationState getKeyboardActivationState);
105 
106 private:
107     void MultiKeyboardSetLedState(bool oldCapsLockState);
108     void MultiKeyboardSetFuncState(libinput_event* event);
109     void OnEventHandler();
110     void OnDeviceAdded(std::string path);
111     void OnDeviceRemoved(std::string path);
112     void InitRightButtonAreaConfig();
113     void InjectKeyEvent(libinput_event_touch* touch, int32_t keyCode, libinput_key_state state, int64_t frameTime);
114 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
115     void HandleVFullKeyboardMessages(
116         libinput_event *event, int64_t frameTime, libinput_event_type eventType, libinput_event_touch *touch);
117     void HandleVKeyTouchpadMessages(libinput_event_touch* touch);
118     void OnVKeyTrackPadMessage(libinput_event_touch* touch,
119         const std::vector<std::vector<int32_t>>& msgList);
120     void OnVKeyTrackPadGestureMessage(libinput_event_touch* touch,
121         VTPStateMachineMessageType msgType, const std::vector<int32_t>& msgItem);
122     void OnVKeyTrackPadGestureTwoMessage(libinput_event_touch* touch,
123         VTPStateMachineMessageType msgType, const std::vector<int32_t>& msgItem);
124     bool HandleVKeyTrackPadPointerMove(libinput_event_touch* touch,
125         const std::vector<int32_t>& msgItem);
126     bool HandleVKeyTrackPadLeftBtnDown(libinput_event_touch* touch,
127         const std::vector<int32_t>& msgItem);
128     bool HandleVKeyTrackPadLeftBtnUp(libinput_event_touch* touch,
129         const std::vector<int32_t>& msgItem);
130     bool HandleVKeyTrackPadRightBtnDown(libinput_event_touch* touch,
131         const std::vector<int32_t>& msgItem);
132     bool HandleVKeyTrackPadRightBtnUp(libinput_event_touch* touch,
133         const std::vector<int32_t>& msgItem);
134     bool HandleVKeyTrackPadScrollBegin(libinput_event_touch* touch,
135         const std::vector<int32_t>& msgItem);
136     bool HandleVKeyTrackPadScrollUpdate(libinput_event_touch* touch,
137         const std::vector<int32_t>& msgItem);
138     bool HandleVKeyTrackPadScrollEnd(libinput_event_touch* touch,
139         const std::vector<int32_t>& msgItem);
140     bool HandleVKeyTrackPadPinchBegin(libinput_event_touch* touch,
141         const std::vector<int32_t>& msgItem);
142     bool HandleVKeyTrackPadPinchUpdate(libinput_event_touch* touch,
143         const std::vector<int32_t>& msgItem);
144     bool HandleVKeyTrackPadPinchEnd(libinput_event_touch* touch,
145         const std::vector<int32_t>& msgItem);
146     bool HandleVKeyTrackPadPanBegin(libinput_event_touch* touch,
147         const std::vector<int32_t>& msgItem);
148     bool HandleVKeyTrackPadPanUpdate(libinput_event_touch* touch,
149         const std::vector<int32_t>& msgItem);
150     bool HandleVKeyTrackPadPanEnd(libinput_event_touch* touch,
151         const std::vector<int32_t>& msgItem);
152     bool HandleVKeyTrackPadRotateBegin(libinput_event_touch* touch,
153         const std::vector<int32_t>& msgItem);
154     bool HandleVKeyTrackPadRotateUpdate(libinput_event_touch* touch,
155         const std::vector<int32_t>& msgItem);
156     bool HandleVKeyTrackPadRotateEnd(libinput_event_touch* touch,
157         const std::vector<int32_t>& msgItem);
158     int32_t ConvertToTouchEventType(libinput_event_type eventType);
159     void PrintVKeyTPPointerLog(event_pointer &pEvent);
160     void PrintVKeyTPGestureLog(event_gesture &gEvent);
161     void HandleHWKeyEventForVKeyboard(libinput_event* event);
162     void ShowMouseCursor();
163     void HideMouseCursorTemporary();
164     double GetAccumulatedPressure(int touchId, int32_t eventType, double touchPressure);
165     bool SkipTouchMove(int touchId, int32_t eventType); // compress touch move events in consecutive two frame
166 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
167     int32_t fd_ { -1 };
168     libinput *input_ { nullptr };
169 
170     FunInputEvent funInputEvent_;
171     HandleTouchPoint handleTouchPoint_ { nullptr };
172     GetMessage getMessage_ { nullptr };
173     GetAllTouchMessage getAllTouchMessage_ { nullptr };
174     ClearTouchMessage clearTouchMessage_ { nullptr };
175     GetAllKeyMessage getAllKeyMessage_ { nullptr };
176     ClearKeyMessage clearKeyMessage_ { nullptr };
177     HardwareKeyEventDetected hardwareKeyEventDetected_ { nullptr };
178     GetKeyboardActivationState getKeyboardActivationState_ { nullptr };
179     int32_t deviceId;
180     std::unordered_map<int32_t, std::pair<double, double>> touchPoints_;
181     static std::unordered_map<std::string, int32_t> keyCodes_;
182     std::unordered_map<int32_t, double> touchPointPressureCache_;
183     std::unordered_map<int32_t, bool> skipTouchMoveCache_;
184 
185     HotplugDetector hotplugDetector_;
186     std::unordered_map<std::string, libinput_device*> devices_;
187 };
188 } // namespace MMI
189 } // namespace OHOS
190 #endif // S_INPUT_H
191