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