1 /*
2  * Copyright (c) 2021-2023 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 #include "window_info.h"
30 
31 #include "i_input_event_consumer.h"
32 #include "js_touch_event.h"
33 
34 namespace OHOS {
35 namespace MMI {
36 using MapFun = std::map<std::string, std::function<int64_t()>>;
37 
38 class InputMonitor final : public IInputEventConsumer, public std::enable_shared_from_this<InputMonitor> {
39 public:
40     InputMonitor() = default;
41     DISALLOW_COPY_AND_MOVE(InputMonitor);
42     ~InputMonitor() override = default;
43 
44     int32_t Start(const std::string &typeName);
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     void SetFingers(int32_t fingers);
50     void SetHotRectArea(std::vector<Rect> hotRectArea);
51     std::vector<Rect> GetHotRectArea();
52     void SetRectTotal(uint32_t rectTotal);
53     uint32_t GetRectTotal();
54     void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override;
55     void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override;
56     void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override;
57     std::string GetTypeName() const;
58     void SetTypeName(const std::string &typeName);
59 private:
60     bool IsGestureEvent(std::shared_ptr<PointerEvent> pointerEvent) const;
61     void SetConsumeState(std::shared_ptr<PointerEvent> pointerEvent) const;
62 
63 private:
64     std::function<void(std::shared_ptr<PointerEvent>)> callback_;
65     int32_t id_ { -1 };
66     int32_t monitorId_ { -1 };
67     int32_t fingers_ { 0 };
68     std::vector<Rect> hotRectArea_;
69     uint32_t rectTotal_ { 0 };
70     mutable bool consumed_ { false };
71     mutable std::mutex mutex_;
72     mutable int32_t flowCtrl_ { 0 };
73     std::string typeName_;
74 };
75 
76 class JsInputMonitor final {
77 public:
78     static void JsCallback(uv_work_t *work, int32_t status);
79     JsInputMonitor(napi_env jsEnv, const std::string &typeName, std::vector<Rect> hotRectArea,
80         int32_t rectTotal, napi_value callback, int32_t id, int32_t fingers);
81     JsInputMonitor(napi_env jsEnv, const std::string &typeName, napi_value callback, int32_t id, int32_t fingers);
82     ~JsInputMonitor();
83 
84     int32_t Start(const std::string &typeName);
85     void Stop();
86     void MarkConsumed(const int32_t eventId);
87     int32_t IsMatch(const napi_env jsEnv, napi_value callback);
88     int32_t IsMatch(napi_env jsEnv);
89     int32_t GetId() const;
90     int32_t GetFingers() const;
91     void OnPointerEventInJsThread(const std::string &typeName, const int32_t fingers);
92     void CheckConsumed(bool retValue, std::shared_ptr<PointerEvent> pointerEvent);
93     void OnPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent);
94     std::string GetTypeName() const;
95     bool IsLocaledWithinRect(napi_env env, napi_value napiPointer, uint32_t rectTotal, std::vector<Rect> hotRectArea);
96 private:
97     void SetCallback(napi_value callback);
98     MapFun GetInputEventFunc(const std::shared_ptr<InputEvent> inputEvent);
99     int32_t SetInputEventProperty(const std::shared_ptr<InputEvent> inputEvent, napi_value result);
100     int32_t TransformPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
101     int32_t GetAction(int32_t action) const;
102     int32_t GetSourceType(int32_t sourceType) const;
103     int32_t GetPinchAction(int32_t action) const;
104     int32_t GetSwipeAction(int32_t action) const;
105     int32_t GetRotateAction(int32_t action) const;
106     int32_t GetMultiTapAction(int32_t action) const;
107 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
108     int32_t GetFingerprintAction(int32_t action) const;
109 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
110     int32_t GetJsPointerItem(const PointerEvent::PointerItem &item, napi_value value) const;
111     int32_t TransformTsActionValue(int32_t pointerAction);
112     int32_t TransformMousePointerEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
113     int32_t TransformPinchEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
114     int32_t TransformSwipeEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
115     int32_t TransformRotateEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
116     int32_t TransformMultiTapEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
117     int32_t TransformSwipeInwardEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
118 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
119     int32_t TransformFingerprintEvent(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
120 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
121     int32_t GetMousePointerItem(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
122     bool SetMouseProperty(const std::shared_ptr<PointerEvent> pointerEvent,
123         const PointerEvent::PointerItem& item, napi_value result);
124     bool GetAxesValue(const std::shared_ptr<PointerEvent> pointerEvent, napi_value element);
125     bool GetPressedKeys(const std::vector<int32_t>& pressedKeys, napi_value result);
126     bool GetPressedButtons(const std::set<int32_t>& pressedButtons, napi_value result);
127     bool HasKeyCode(const std::vector<int32_t>& pressedKeys, int32_t keyCode);
128     bool GetPressedKey(const std::vector<int32_t>& pressedKeys, napi_value result);
129     bool IsPinch(std::shared_ptr<PointerEvent> pointerEvent, const int32_t fingers);
130     bool IsRotate(std::shared_ptr<PointerEvent> pointerEvent);
131     bool IsThreeFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent);
132     bool IsFourFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent);
133     bool IsBeginAndEnd(std::shared_ptr<PointerEvent> pointerEvent);
134     bool IsThreeFingersTap(std::shared_ptr<PointerEvent> pointerEvent);
135     bool IsSwipeInward(std::shared_ptr<PointerEvent> pointerEvent);
136 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
137     bool IsFingerprint(std::shared_ptr<PointerEvent> pointerEvent);
138 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
139     MapFun GetFuns(const std::shared_ptr<PointerEvent> pointerEvent, const PointerEvent::PointerItem& item);
140 private:
141     std::shared_ptr<InputMonitor> monitor_ { nullptr };
142     std::queue<std::shared_ptr<PointerEvent>> evQueue_;
143     std::queue<std::shared_ptr<PointerEvent>> pointerQueue_;
144     napi_ref receiver_ { nullptr };
145     napi_env jsEnv_ { nullptr };
146     std::string typeName_;
147     int32_t monitorId_ { 0 };
148     int32_t fingers_ { 0 };
149     bool isMonitoring_ { false };
150     std::mutex mutex_;
151     std::mutex resourcesmutex_;
152 };
153 } // namespace MMI
154 } // namespace OHOS
155 #endif // JS_INPUT_MONITOR_H
156