• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     };
46     struct ReportData : RefBase {
47         napi_ref ref { nullptr };
48         int32_t deviceId { 0 };
49     };
50     struct CallbackInfo : RefBase {
51         napi_env env { nullptr };
52         napi_ref ref { nullptr };
53         napi_deferred deferred { nullptr };
54         int32_t errCode { -1 };
55         CallbackData data;
56         UserData uData;
57         bool isApi9 { false };
58     };
59     struct DeviceType {
60         std::string sourceTypeName;
61         uint32_t typeBit { 0 };
62     };
63 
64     static bool IsSameHandle(napi_env env, napi_value handle, napi_ref ref);
65     static napi_value GetDeviceInfo(sptr<CallbackInfo> cb);
66     static bool GetDeviceAxisInfo(sptr<CallbackInfo> cb, napi_value &object);
67     static bool GetDeviceSourceType(sptr<CallbackInfo> cb, napi_value &object);
68     static bool TypeOf(napi_env env, napi_value value, napi_valuetype type);
69     static void DeleteCallbackInfo(std::unique_ptr<CallbackInfo> callback);
70     template <typename T>
DeletePtr(T & ptr)71     static void DeletePtr(T &ptr)
72     {
73         if (ptr != nullptr) {
74             delete ptr;
75             ptr = nullptr;
76         }
77     }
78 };
79 } // namespace MMI
80 } // namespace OHOS
81 #endif // JS_UTIL_H