1 /*
2  * Copyright (c) 2022-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_UTIL_H
17 #define JS_UTIL_H
18 
19 #include <uv.h>
20 
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 #include "refbase.h"
24 
25 #include "input_device.h"
26 
27 namespace OHOS {
28 namespace MMI {
29 class JsUtil {
30 public:
31     struct UserData {
32         int32_t userData { 0 };
33         int32_t deviceId { 0 };
34         napi_value handle { nullptr };
35         std::vector<int32_t> keys;
36     };
37     struct CallbackData {
38         std::vector<int32_t> ids;
39         std::shared_ptr<InputDevice> device { nullptr };
40         std::vector<bool> keystrokeAbility;
41         int32_t deviceId { 0 };
42         int32_t keyboardType { 0 };
43         int32_t keyboardRepeatDelay { 0 };
44         int32_t keyboardRepeatRate { 0 };
45         int64_t IntervalSinceLastInput { 0 };
46     };
47     struct ReportData : RefBase {
48         napi_ref ref { nullptr };
49         int32_t deviceId { 0 };
50     };
51     struct CallbackInfo : RefBase {
52         napi_env env { nullptr };
53         napi_ref ref { nullptr };
54         napi_deferred deferred { nullptr };
55         int32_t errCode { -1 };
56         CallbackData data;
57         UserData uData;
58         bool isApi9 { false };
59     };
60     struct DeviceType {
61         std::string sourceTypeName;
62         uint32_t typeBit { 0 };
63     };
64 
65     static bool IsSameHandle(napi_env env, napi_value handle, napi_ref ref);
66     static napi_value GetDeviceInfo(sptr<CallbackInfo> cb);
67     static bool GetDeviceAxisInfo(sptr<CallbackInfo> cb, napi_value &object);
68     static bool GetDeviceSourceType(sptr<CallbackInfo> cb, napi_value &object);
69     static bool TypeOf(napi_env env, napi_value value, napi_valuetype type);
70     static void DeleteCallbackInfo(std::unique_ptr<CallbackInfo> callback);
71     template <typename T>
DeletePtr(T & ptr)72     static void DeletePtr(T &ptr)
73     {
74         if (ptr != nullptr) {
75             delete ptr;
76             ptr = nullptr;
77         }
78     }
79 };
80 } // namespace MMI
81 } // namespace OHOS
82 #endif // JS_UTIL_H