• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <mutex>
21 #include <queue>
22 #include <uv.h>
23 #include "i_input_event_consumer.h"
24 #include "napi/native_api.h"
25 #include "napi/native_node_api.h"
26 
27 namespace OHOS {
28 namespace MMI {
29 class InputMonitor : public IInputEventConsumer,
30                      public std::enable_shared_from_this<InputMonitor> {
31 public:
32     InputMonitor() = default;
33     virtual ~InputMonitor() = default;
34 
35     bool Start();
36 
37     void Stop();
38 
39     void MarkConsumed(int32_t eventId);
40 
41     void SetCallback(std::function<void(std::shared_ptr<PointerEvent>)> callback);
42 
43     void SetId(int32_t id);
44 
45     virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override;
46 
47     virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override;
48 
49     virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override;
50 
51 private:
52     InputMonitor(const InputMonitor&) = delete;
53 
54     InputMonitor(InputMonitor&&) = delete;
55 
56     InputMonitor& operator=(const InputMonitor&) = delete;
57 
58 private:
59     int32_t id_ {-1};
60     mutable std::mutex mutex_;
61     int32_t monitorId_ {-1};
62     std::function<void(std::shared_ptr<PointerEvent>)> callback_;
63     mutable bool consumed_ {false};
64 };
65 
66 
67 class JsInputMonitor {
68 public:
69     JsInputMonitor(napi_env jsEnv, napi_value receiver, int32_t id);
70 
71     ~JsInputMonitor();
72 
73     bool Start();
74 
75     void Stop();
76 
77     void MarkConsumed(const int32_t eventId);
78 
79     int32_t IsMatch(const napi_env jsEnv, napi_value receiver);
80 
81     int32_t IsMatch(napi_env jsEnv);
82 
83     int32_t GetId();
84 
85     void OnPointerEventInJsThread();
86 
87     void OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent);
88 
89     static void JsCallback(uv_work_t *work, int32_t status);
90 private:
91     void OnPointerEventInJsThread(std::shared_ptr<PointerEvent> pointerEvent);
92 
93     void SetReceiver(napi_value receiver);
94 
95     void printfPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent) const;
96 
97     int32_t TransformPointerEvent(const std::shared_ptr<PointerEvent> pointerEvent, napi_value result);
98 
99     std::string GetAction(int32_t action);
100 
101 private:
102     std::shared_ptr<InputMonitor> monitor_ {nullptr};
103     napi_ref receiver_ {nullptr};
104     napi_env jsEnv_ {nullptr};
105     uv_loop_s *loop_ = nullptr;
106     int32_t id_ = 0;
107     bool isMonitoring_ = false;
108     std::queue<std::shared_ptr<PointerEvent>> evQueue_;
109     std::mutex mutex_;
110     int32_t jsThreadNum_ = 0;
111 };
112 } // namespace MMI
113 } // namespace OHOS
114 #endif // JS_INPUT_MONITOR_H
115