• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     static void JsCallback(uv_work_t *work, int32_t status);
79     static void JsPreCallback(uv_work_t *work, int32_t status);
80     JsInputMonitor(napi_env jsEnv, const std::string &typeName, std::vector<Rect> hotRectArea,
81         int32_t rectTotal, napi_value callback, int32_t id, int32_t fingers);
82     JsInputMonitor(napi_env jsEnv, const std::string &typeName, napi_value callback, int32_t id, int32_t fingers);
83     JsInputMonitor(napi_env jsEnv, const std::string &typeName, napi_value callback,
84         int32_t id, std::vector<int32_t> keys);
85     ~JsInputMonitor();
86 
87     int32_t Start(const std::string &typeName);
88     void Stop();
89     void MarkConsumed(const int32_t eventId);
90     int32_t IsMatch(const napi_env jsEnv, napi_value callback);
91     int32_t IsMatch(napi_env jsEnv);
92     int32_t GetId() const;
93     int32_t GetFingers() const;
94     void OnPointerEventInJsThread(const std::string &typeName, const int32_t fingers);
95     void OnKeyEventInJsThread(const std::string &typeName);
96     void CheckConsumed(bool retValue, std::shared_ptr<PointerEvent> pointerEvent);
97     void OnPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent);
98     void OnKeyEvent(const std::shared_ptr<KeyEvent> keyEvent);
99     std::string GetTypeName() const;
100     bool IsLocaledWithinRect(napi_env env, napi_value napiPointer, uint32_t rectTotal, std::vector<Rect> hotRectArea);
101 private:
102     void SetCallback(napi_value callback);
103     MapFun GetInputEventFunc(const std::shared_ptr<InputEvent> inputEvent);
104     int32_t SetInputEventProperty(const std::shared_ptr<InputEvent> inputEvent, napi_value result);
105     int32_t TransformPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
106     int32_t GetAction(int32_t action) const;
107     int32_t GetSourceType(int32_t sourceType) const;
108     int32_t GetPinchAction(int32_t action) const;
109     int32_t GetSwipeAction(int32_t action) const;
110     int32_t GetRotateAction(int32_t action) const;
111     int32_t GetMultiTapAction(int32_t action) const;
112     int32_t GetGestureAction(int32_t action) const;
113 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
114     int32_t GetFingerprintAction(int32_t action) const;
115 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
116     int32_t GetJsPointerItem(const PointerEvent::PointerItem &item, napi_value value) const;
117     int32_t TransformTsActionValue(int32_t pointerAction);
118     int32_t TransformMousePointerEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
119     int32_t TransformPinchEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
120     int32_t TransformSwipeEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
121     int32_t TransformRotateEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
122     int32_t TransformMultiTapEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
123     int32_t TransformSwipeInwardEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
124     int32_t TransformJoystickPointerEvent(std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
125     int32_t TransformGestureEvent(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
126 #ifdef OHOS_BUILD_ENABLE_FINGERPRINT
127     int32_t TransformFingerprintEvent(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
128 #endif // OHOS_BUILD_ENABLE_FINGERPRINT
129     int32_t GetMousePointerItem(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
130     std::optional<int32_t> GetJoystickAction(int32_t action);
131     int32_t GetJoystickButton(int32_t button);
132     int32_t GetJoystickPointerItem(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
133     bool SetMouseProperty(const std::shared_ptr<PointerEvent> pointerEvent,
134         const PointerEvent::PointerItem& item, napi_value result);
135     bool GetAxesValue(const std::shared_ptr<PointerEvent> pointerEvent, napi_value element);
136     bool GetPressedKeys(const std::vector<int32_t>& pressedKeys, napi_value result);
137     bool GetJoystickPressedButtons(const std::set<int32_t>& pressedButtons, napi_value result);
138     bool GetPressedButtons(const std::set<int32_t>& pressedButtons, napi_value result);
139     bool HasKeyCode(const std::vector<int32_t>& pressedKeys, int32_t keyCode);
140     bool GetPressedKey(const std::vector<int32_t>& pressedKeys, napi_value result);
141     bool IsPinch(std::shared_ptr<PointerEvent> pointerEvent, const int32_t fingers);
142     bool IsRotate(std::shared_ptr<PointerEvent> pointerEvent);
143     bool IsThreeFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent);
144     bool IsFourFingersSwipe(std::shared_ptr<PointerEvent> pointerEvent);
145     bool IsBeginAndEnd(std::shared_ptr<PointerEvent> pointerEvent);
146     bool IsThreeFingersTap(std::shared_ptr<PointerEvent> pointerEvent);
147     bool IsSwipeInward(std::shared_ptr<PointerEvent> pointerEvent);
148     bool IsJoystick(std::shared_ptr<PointerEvent> pointerEvent);
149     bool IsFingerprint(std::shared_ptr<PointerEvent> pointerEvent);
150     MapFun GetFuns(const std::shared_ptr<PointerEvent> pointerEvent, const PointerEvent::PointerItem& item);
151     int32_t TransformKeyEvent(const std::shared_ptr<KeyEvent> keyEvent, napi_value result);
152     int32_t GetKeyEventAction(int32_t action) const;
153     int32_t GetJsKeyItem(const std::optional<MMI::KeyEvent::KeyItem> keyItem, napi_value value) const;
154     bool GetFunctionKeyState(const std::shared_ptr<KeyEvent> keyEvent, napi_value value) const;
155     bool GetKeys(const std::shared_ptr<KeyEvent> keyEvent,
156         const std::vector<int32_t> &pressedKeys, napi_value value) const;
157 
158 private:
159     std::shared_ptr<InputMonitor> monitor_ { nullptr };
160     std::queue<std::shared_ptr<PointerEvent>> evQueue_;
161     std::queue<std::shared_ptr<PointerEvent>> pointerQueue_;
162     ThreadSafeQueue<std::shared_ptr<KeyEvent>> preEvQueue_;
163     napi_ref receiver_ { nullptr };
164     napi_env jsEnv_ { nullptr };
165     std::string typeName_;
166     int32_t monitorId_ { 0 };
167     int32_t fingers_ { 0 };
168     bool isMonitoring_ { false };
169     std::mutex mutex_;
170     std::mutex resourcemutex_;
171     std::vector<int32_t> keys_;
172 };
173 } // namespace MMI
174 } // namespace OHOS
175 #endif // JS_INPUT_MONITOR_H
176