• 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 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
22 #include "folding_area_toast.h"
23 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
24 
25 namespace OHOS {
26 namespace MMI {
27 typedef std::function<void(void *event, int64_t frameTime)> FunInputEvent;
28 typedef std::function<int32_t(double screenX,
29                               double screenY,
30                               int touchId,
31                               int32_t eventType,
32                               double touchPressure,
33                               int32_t longAxis,
34                               int32_t shortAxis)> HandleTouchPoint;
35 typedef std::function<void(const std::string &keyName)> HardwareKeyEventDetected;
36 typedef std::function<int32_t()> GetKeyboardActivationState;
37 typedef std::function<bool()> IsFloatingKeyboard;
38 typedef std::function<bool()> IsVKeyboardShown;
39 typedef std::function<int32_t(libinput_event_touch *touch, int32_t& delayMs,
40                               std::vector<libinput_event*>& events)> GetLibinputEventForVKeyboard;
41 typedef std::function<int32_t(libinput_event_touch *touch,
42                               std::vector<libinput_event*>& events)> GetLibinputEventForVTrackpad;
43 
44 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
45 enum VTPSwipeStateType {
46     SWIPE_BEGIN = 1,
47     SWIPE_UPDATE = 2,
48     SWIPE_END = 3,
49 };
50 enum VKeyboardMessageType {
51     VNoMessage = -1,
52     VKeyPressed = 0,
53     VCombinationKeyPressed = 1,
54     VStartLongPressControl = 16,
55     VStopLongPressControl = 17,
56     VSwitchLayout = 18,
57 };
58 enum VKeyboardEventType {
59     NoKeyboardEvent = -1,
60     NormalKeyboardEvent = 0,
61     HideCursor = 1,
62     UpdateCaps = 2,
63     StopLongpress = 3
64 };
65 
66 enum VTrackpadEventType {
67     NoTrackpadEvent = -1,
68     NormalTrackpadEvent = 0,
69     PinchBegin = 1,
70     PinchUpdate = 2,
71     PinchEnd = 3
72 };
73 enum class VTPStateMachineMessageType : int32_t {
74     UNKNOWN = 0,
75     POINTER_MOVE = 1,
76     LEFT_CLICK_DOWN = 2,
77     LEFT_CLICK_UP = 3,
78     RIGHT_CLICK_DOWN = 4,
79     RIGHT_CLICK_UP = 5,
80     SCROLL_BEGIN = 6,
81     SCROLL_UPDATE = 7,
82     SCROLL_END = 8,
83     PINCH_BEGIN = 9,
84     PINCH_UPDATE = 10,
85     PINCH_END = 11,
86     PAN_BEGIN = 12,
87     PAN_UPDATE = 13,
88     PAN_END = 14,
89     ROT_BEGIN = 15,
90     ROT_UPDATE = 16,
91     ROT_END = 17,
92     LEFT_TOUCH_DOWN = 18,
93     LEFT_TOUCH_UP = 19,
94     TWO_FINGER_TAP = 20,
95     LEFT_TOUCH_UP_CANCEL = 21,
96     SWIPE_BEGIN = 22,
97     SWIPE_UPDATE = 23,
98     SWIPE_END = 24,
99     SWIPE_FOUR_BEGIN = 25,
100     SWIPE_FOUR_UPDATE = 26,
101     SWIPE_FOUR_END = 27,
102 };
103 
104 enum class VKeyboardActivation : int32_t {
105     INACTIVE = 0,
106     ACTIVATED = 1,
107     TOUCH_CANCEL = 2,
108     TOUCH_DROP = 3,
109     EIGHT_FINGERS_UP = 4,
110 };
111 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
112 class LibinputAdapter final {
113 public:
114     static int32_t DeviceLedUpdate(struct libinput_device *device, int32_t funcKey, bool isEnable);
115     LibinputAdapter() = default;
116     DISALLOW_COPY_AND_MOVE(LibinputAdapter);
117     ~LibinputAdapter() = default;
118     bool Init(FunInputEvent funInputEvent);
119     void EventDispatch(int32_t fd);
120     void Stop();
121     void ProcessPendingEvents();
122     void ReloadDevice();
123 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
124     static void SetBootCompleted();
125     void RegisterBootStatusReceiver();
126 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
127 
GetInputFds()128     auto GetInputFds() const
129     {
130         return std::array{fd_, hotplugDetector_.GetFd()};
131     }
132 
133     void InitVKeyboard(HandleTouchPoint handleTouchPoint,
134         HardwareKeyEventDetected hardwareKeyEventDetected,
135         GetKeyboardActivationState getKeyboardActivationState,
136         IsFloatingKeyboard isFloatingKeyboard,
137         IsVKeyboardShown isVKeyboardShown,
138 		GetLibinputEventForVKeyboard getLibinputEventForVKeyboard,
139         GetLibinputEventForVTrackpad getLibinputEventForVTrackpad
140         );
141 
142 private:
143     void MultiKeyboardSetLedState(bool oldCapsLockState);
144     void MultiKeyboardSetFuncState(libinput_event* event);
145     void OnEventHandler();
146     void OnDeviceAdded(std::string path);
147     void OnDeviceRemoved(std::string path);
148     void InitRightButtonAreaConfig();
149     void InjectKeyEvent(libinput_event_touch* touch, int32_t keyCode, libinput_key_state state, int64_t frameTime);
150 #ifdef OHOS_BUILD_ENABLE_VKEYBOARD
151     void HandleVFullKeyboardMessages(
152         libinput_event *event, int64_t frameTime, libinput_event_type eventType, libinput_event_touch *touch);
153     void HandleVKeyboardMessage(VKeyboardEventType eventType, std::vector<libinput_event*> keyEvents,
154                                 int64_t frameTime);
155     void HandleVTrackpadMessage(VTrackpadEventType eventType, std::vector<libinput_event*> events,
156                                 int64_t frameTime, libinput_event_touch *touch);
157     bool IsVKeyboardActivationDropEvent(libinput_event_touch* touch, libinput_event_type eventType);
158     void InjectEventForTwoFingerOnTouchpad(libinput_event_touch* touch,
159         libinput_event_type eventType, int64_t frameTime);
160     void InjectEventForCastWindow(libinput_event_touch* touch);
161     bool IsCursorInCastWindow();
162     int32_t ConvertToTouchEventType(libinput_event_type eventType);
163     void HandleHWKeyEventForVKeyboard(libinput_event* event);
164     void ShowMouseCursor();
165     void HideMouseCursorTemporary();
166     double GetAccumulatedPressure(int touchId, int32_t eventType, double touchPressure);
167     bool SkipTouchMove(int touchId, int32_t eventType); // compress touch move events in consecutive two frame
168     void DelayInjectKeyEventCallback();
169     bool CreateVKeyboardDelayTimer(libinput_event *event, int32_t delayMs, libinput_event *keyEvent);
170     void StartVKeyboardDelayTimer(int32_t delayMs);
171     bool GetIsCaptureMode();
172     void UpdateBootFlag();
173     VTPSwipeStateType vtpSwipeState_ = VTPSwipeStateType::SWIPE_END;
174 
175     libinput_event *vkbDelayedEvent_ = nullptr;
176     libinput_event *vkbDelayedKeyEvent_ = nullptr;
177     int32_t vkbDelayedKeyCode_ = 0;
178     std::chrono::system_clock::time_point vtpSingleTapDownTime;
179     const double vtpSingleTapThreshold = 0.18; // s
180     // set as true once subscriber succeeded.
181     std::atomic_bool hasInitSubscriber_ { false };
182     static std::atomic_bool isBootCompleted_;
183     FoldingAreaToast foldingAreaToast_;
184 #endif // OHOS_BUILD_ENABLE_VKEYBOARD
185     int32_t fd_ { -1 };
186     libinput *input_ { nullptr };
187 
188     FunInputEvent funInputEvent_;
189     HandleTouchPoint handleTouchPoint_ { nullptr };
190     HardwareKeyEventDetected hardwareKeyEventDetected_ { nullptr };
191     GetKeyboardActivationState getKeyboardActivationState_ { nullptr };
192     IsFloatingKeyboard isFloatingKeyboard_ { nullptr };
193     IsVKeyboardShown isVKeyboardShown_ { nullptr };
194     GetLibinputEventForVKeyboard getLibinputEventForVKeyboard_ { nullptr };
195     GetLibinputEventForVTrackpad getLibinputEventForVTrackpad_ { nullptr };
196     int32_t deviceId;
197     std::unordered_map<int32_t, std::pair<double, double>> touchPoints_;
198     static std::unordered_map<std::string, int32_t> keyCodes_;
199     std::unordered_map<int32_t, double> touchPointPressureCache_;
200 
201     HotplugDetector hotplugDetector_;
202     std::unordered_map<std::string, libinput_device*> devices_;
203 };
204 } // namespace MMI
205 } // namespace OHOS
206 #endif // S_INPUT_H
207