1 /* 2 * Copyright (c) 2021-2022 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 <cinttypes> 20 #include <map> 21 #include <mutex> 22 #include <queue> 23 #include <uv.h> 24 25 #include "napi/native_api.h" 26 #include "napi/native_node_api.h" 27 #include "nocopyable.h" 28 #include "util_napi.h" 29 30 #include "i_input_event_consumer.h" 31 32 namespace OHOS { 33 namespace MMI { 34 using MapFun = std::map<std::string, std::function<int64_t()>>; 35 36 37 class InputMonitor : public IInputEventConsumer, 38 public std::enable_shared_from_this<InputMonitor> { 39 public: 40 InputMonitor() = default; 41 DISALLOW_COPY_AND_MOVE(InputMonitor); 42 virtual ~InputMonitor() = default; 43 44 int32_t Start(); 45 void Stop(); 46 void MarkConsumed(int32_t eventId); 47 void SetCallback(std::function<void(std::shared_ptr<PointerEvent>)> callback); 48 void SetId(int32_t id); 49 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override; 50 virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override; 51 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override; 52 53 private: 54 std::function<void(std::shared_ptr<PointerEvent>)> callback_; 55 int32_t id_ { -1 }; 56 int32_t monitorId_ { -1 }; 57 mutable bool consumed_ { false }; 58 mutable std::mutex mutex_; 59 }; 60 61 62 class JsInputMonitor { 63 public: 64 static void JsCallback(uv_work_t *work, int32_t status); 65 JsInputMonitor(napi_env jsEnv, const std::string &typeName, napi_value callback, int32_t id); 66 ~JsInputMonitor(); 67 68 int32_t Start(); 69 void Stop(); 70 void MarkConsumed(const int32_t eventId); 71 int32_t IsMatch(const napi_env jsEnv, napi_value callback); 72 int32_t IsMatch(napi_env jsEnv); 73 int32_t GetId() const; 74 void OnPointerEventInJsThread(const std::string &typeName); 75 void OnPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent); 76 std::string GetTypeName() const; 77 private: 78 void SetCallback(napi_value callback); 79 int32_t TransformPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 80 std::string GetAction(int32_t action) const; 81 int32_t GetJsPointerItem(const PointerEvent::PointerItem &item, napi_value value) const; 82 int32_t TransformTsActionValue(int32_t pointerAction); 83 int32_t TransformMousePointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 84 int32_t GetMousePointerItem(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result); 85 bool SetMouseProperty(const std::shared_ptr<PointerEvent> pointerEvent, 86 const PointerEvent::PointerItem& item, napi_value result); 87 bool GetAxesValue(const std::shared_ptr<PointerEvent> pointerEvent, napi_value element); 88 bool GetPressedKeys(const std::vector<int32_t>& pressedKeys, napi_value result); 89 bool GetPressedButtons(const std::set<int32_t>& pressedButtons, napi_value result); 90 bool HasKeyCode(const std::vector<int32_t>& pressedKeys, int32_t keyCode); 91 bool GetPressedKey(const std::vector<int32_t>& pressedKeys, napi_value result); 92 MapFun GetFuns(const std::shared_ptr<PointerEvent> pointerEvent, const PointerEvent::PointerItem& item); 93 private: 94 std::shared_ptr<InputMonitor> monitor_ { nullptr }; 95 std::queue<std::shared_ptr<PointerEvent>> evQueue_; 96 napi_ref receiver_ { nullptr }; 97 napi_env jsEnv_ { nullptr }; 98 std::string typeName_; 99 int32_t monitorId_ { 0 }; 100 int32_t jsTaskNum_ = { 0 }; 101 bool isMonitoring_ = { false }; 102 std::mutex mutex_; 103 }; 104 } // namespace MMI 105 } // namespace OHOS 106 #endif // JS_INPUT_MONITOR_H 107