1 /* 2 * Copyright (c) 2021-2025 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 JS_INPUT_MONITOR_H 17 #define JS_INPUT_MONITOR_H 18 19 #include <map> 20 #include <queue> 21 #include <uv.h> 22 23 #include "window_info.h" 24 25 #include "i_input_event_consumer.h" 26 #include "js_touch_event.h" 27 #include "js_joystick_event.h" 28 #include "thread_safety_container.h" 29 30 namespace OHOS { 31 namespace MMI { 32 using MapFun = std::map<std::string, std::function<int64_t()>>; 33 34 class InputMonitor final : public IInputEventConsumer, public std::enable_shared_from_this<InputMonitor> { 35 public: 36 InputMonitor() = default; 37 DISALLOW_COPY_AND_MOVE(InputMonitor); 38 ~InputMonitor() override = default; 39 40 int32_t Start(const std::string &typeName); 41 void Stop(); 42 void MarkConsumed(int32_t eventId); 43 void SetCallback(std::function<void(std::shared_ptr<PointerEvent>)> callback); 44 void SetKeyCallback(std::function<void(std::shared_ptr<KeyEvent>)> callback); 45 void SetKeys(std::vector<int32_t> keys); 46 void SetId(int32_t id); 47 void SetFingers(int32_t fingers); 48 void SetHotRectArea(std::vector<Rect> hotRectArea); 49 std::vector<Rect> GetHotRectArea(); 50 void SetRectTotal(uint32_t rectTotal); 51 uint32_t GetRectTotal(); 52 void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override; 53 void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override; 54 void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override; 55 std::string GetTypeName() const; 56 void SetTypeName(const std::string &typeName); 57 private: 58 bool IsGestureEvent(std::shared_ptr<PointerEvent> pointerEvent) const; 59 void SetConsumeState(std::shared_ptr<PointerEvent> pointerEvent) const; 60 61 private: 62 std::function<void(std::shared_ptr<PointerEvent>)> callback_; 63 std::function<void(std::shared_ptr<KeyEvent>)> keyCallback_; 64 int32_t id_ { -1 }; 65 int32_t monitorId_ { -1 }; 66 int32_t fingers_ { 0 }; 67 std::vector<Rect> hotRectArea_; 68 uint32_t rectTotal_ { 0 }; 69 mutable bool consumed_ { false }; 70 mutable std::mutex mutex_; 71 mutable int32_t flowCtrl_ { 0 }; 72 std::string typeName_; 73 std::vector<int32_t> keys_; 74 }; 75 76 class JsInputMonitor final { 77 public: 78 struct CallbackData { 79 int32_t count { 0 }; 80 std::vector<std::shared_ptr<PointerEvent>> touchEventList; 81 }; 82 83 struct CallbackInfo : RefBase { 84 napi_env env { nullptr }; 85 napi_ref ref { nullptr }; 86 napi_deferred deferred { nullptr }; 87 int32_t errCode { -1 }; 88 CallbackData data; 89 }; 90 91 static void JsCallback(uv_work_t *work, int32_t status); 92 static void JsPreCallback(uv_work_t *work, int32_t status); 93 static void CallJsQueryTouchEventsTask(uv_work_t *work); 94 static void CallJsQueryTouchEventsPromise(uv_work_t *work, int32_t status); 95 static napi_value GreateBusinessError(napi_env env, int32_t errCode, std::string errMessage); 96 static void JsQueryTouchEventsResolveDeferred( 97 sptr<CallbackInfo> cb, napi_handle_scope scope, napi_value callResult); 98 template <typename T> DeletePtr(T & ptr)99 static void DeletePtr(T &ptr) 100 { 101 if (ptr != nullptr) { 102 delete ptr; 103 ptr = nullptr; 104 } 105 } 106 107 JsInputMonitor(napi_env jsEnv, const std::string &typeName, std::vector<Rect> hotRectArea, 108 int32_t rectTotal, napi_value callback, int32_t id, int32_t fingers); 109 JsInputMonitor(napi_env jsEnv, const std::string &typeName, napi_value callback, int32_t id, int32_t fingers); 110 JsInputMonitor(napi_env jsEnv, const std::string &typeName, napi_value callback, 111 int32_t id, std::vector<int32_t> keys); 112 ~JsInputMonitor(); 113 114 int32_t Start(const std::string &typeName); 115 void Stop(); 116 void MarkConsumed(const int32_t eventId); 117 int32_t IsMatch(const napi_env jsEnv, napi_value callback); 118 int32_t IsMatch(napi_env jsEnv); 119 int32_t GetId() const; 120 int32_t GetFingers() const; 121 void OnPointerEventInJsThread(const std::string &typeName, const int32_t fingers); 122 void OnKeyEventInJsThread(const std::string &typeName); 123 void CheckConsumed(bool retValue, std::shared_ptr<PointerEvent> pointerEvent); 124 void OnPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent); 125 void OnKeyEvent(const std::shared_ptr<KeyEvent> keyEvent); 126 std::string GetTypeName() const; 127 bool IsLocaledWithinRect(napi_env env, napi_value napiPointer, uint32_t rectTotal, std::vector<Rect> hotRectArea); 128 private: 129 void SetCallback(napi_value callback); 130 static MapFun GetInputEventFunc(const std::shared_ptr<InputEvent> inputEvent); 131 static int32_t SetInputEventProperty(napi_env env, const std::shared_ptr<InputEvent> inputEvent, napi_value result); 132 static int32_t TransformPointerEvent( 133 napi_env env, const std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 134 static int32_t GetAction(int32_t action); 135 static int32_t GetSourceType(int32_t sourceType); 136 int32_t GetPinchAction(int32_t action) const; 137 int32_t GetSwipeAction(int32_t action) const; 138 int32_t GetRotateAction(int32_t action) const; 139 int32_t GetMultiTapAction(int32_t action) const; 140 int32_t GetGestureAction(int32_t action) const; 141 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT 142 int32_t GetFingerprintAction(int32_t action) const; 143 #endif // OHOS_BUILD_ENABLE_FINGERPRINT 144 static int32_t GetJsPointerItem(napi_env env, const PointerEvent::PointerItem &item, napi_value value); 145 int32_t TransformTsActionValue(int32_t pointerAction); 146 int32_t TransformMousePointerEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 147 int32_t TransformPinchEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 148 int32_t TransformSwipeEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 149 int32_t TransformRotateEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 150 int32_t TransformMultiTapEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 151 int32_t TransformSwipeInwardEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 152 int32_t TransformJoystickPointerEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 153 int32_t TransformGestureEvent(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 154 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT 155 int32_t TransformFingerprintEvent(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 156 #endif // OHOS_BUILD_ENABLE_FINGERPRINT 157 #ifdef OHOS_BUILD_ENABLE_X_KEY 158 int32_t TransformXKeyEvent(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 159 #endif // OHOS_BUILD_ENABLE_X_KEY 160 int32_t GetMousePointerItem(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 161 std::optional<int32_t> GetJoystickAction(int32_t action); 162 int32_t GetJoystickButton(int32_t button); 163 int32_t GetJoystickPointerItem(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 164 bool SetMouseProperty(const std::shared_ptr<PointerEvent> pointerEvent, 165 const PointerEvent::PointerItem& item, napi_value result); 166 bool GetAxesValue(const std::shared_ptr<PointerEvent> pointerEvent, napi_value element); 167 bool GetPressedKeys(const std::vector<int32_t>& pressedKeys, napi_value result); 168 bool GetJoystickPressedButtons(const std::set<int32_t>& pressedButtons, napi_value result); 169 bool GetPressedButtons(const std::set<int32_t>& pressedButtons, napi_value result); 170 bool HasKeyCode(const std::vector<int32_t>& pressedKeys, int32_t keyCode); 171 bool GetPressedKey(const std::vector<int32_t>& pressedKeys, napi_value result); 172 bool IsPinch(std::shared_ptr<PointerEvent> pointerEvent, const int32_t fingers); 173 bool IsRotate(std::shared_ptr<PointerEvent> pointerEvent); 174 bool IsThreeFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent); 175 bool IsFourFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent); 176 bool IsBeginAndEnd(std::shared_ptr<PointerEvent> pointerEvent); 177 bool IsThreeFingersTap(std::shared_ptr<PointerEvent> pointerEvent); 178 bool IsSwipeInward(std::shared_ptr<PointerEvent> pointerEvent); 179 bool IsJoystick(std::shared_ptr<PointerEvent> pointerEvent); 180 bool IsFingerprint(std::shared_ptr<PointerEvent> pointerEvent); 181 #ifdef OHOS_BUILD_ENABLE_X_KEY 182 bool IsXKey(const std::shared_ptr<PointerEvent> pointerEvent); 183 #endif // OHOS_BUILD_ENABLE_X_KEY 184 MapFun GetFuns(const std::shared_ptr<PointerEvent> pointerEvent, const PointerEvent::PointerItem& item); 185 int32_t TransformKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, napi_value result); 186 int32_t GetKeyEventAction(int32_t action) const; 187 int32_t GetJsKeyItem(const std::optional<MMI::KeyEvent::KeyItem> keyItem, napi_value value) const; 188 bool GetFunctionKeyState(const std::shared_ptr<KeyEvent> keyEvent, napi_value value) const; 189 bool GetKeys(const std::shared_ptr<KeyEvent> keyEvent, 190 const std::vector<int32_t> &pressedKeys, napi_value value) const; 191 192 private: 193 std::shared_ptr<InputMonitor> monitor_ { nullptr }; 194 std::queue<std::shared_ptr<PointerEvent>> evQueue_; 195 std::queue<std::shared_ptr<PointerEvent>> pointerQueue_; 196 ThreadSafeQueue<std::shared_ptr<KeyEvent>> preEvQueue_; 197 napi_ref receiver_ { nullptr }; 198 napi_env jsEnv_ { nullptr }; 199 std::string typeName_; 200 int32_t monitorId_ { 0 }; 201 int32_t fingers_ { 0 }; 202 bool isMonitoring_ { false }; 203 std::mutex mutex_; 204 std::mutex resourcemutex_; 205 std::vector<int32_t> keys_; 206 }; 207 } // namespace MMI 208 } // namespace OHOS 209 #endif // JS_INPUT_MONITOR_H 210