• 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 
25 namespace OHOS {
26 namespace MMI {
27 class JsUtil {
28 public:
29     struct UserData {
30         int32_t userData { 0 };
31         int32_t deviceId { 0 };
32         napi_value handle { nullptr };
33         std::vector<int32_t> keys;
34     };
35     struct CallbackData {
36         std::vector<int32_t> ids;
37         std::shared_ptr<InputDevice> device { nullptr };
38         std::vector<bool> keystrokeAbility;
39         int32_t deviceId { 0 };
40         int32_t keyboardType { 0 };
41     };
42     struct CallbackInfo {
43         napi_env env { nullptr };
44         napi_ref ref { nullptr };
45         napi_deferred deferred { nullptr };
46         int32_t errCode { -1 };
47         CallbackData data;
48         UserData uData;
49         bool isApi9 { false };
50     };
51     struct DeviceType {
52         std::string sourceTypeName;
53         uint32_t typeBit { 0 };
54     };
55 
56     static bool IsSameHandle(napi_env env, napi_value handle, napi_ref ref);
57     static napi_value GetDeviceInfo(const std::unique_ptr<CallbackInfo> &cb);
58     static bool GetDeviceAxisInfo(const std::unique_ptr<CallbackInfo> &cb, napi_value &object);
59     static bool GetDeviceSourceType(const std::unique_ptr<CallbackInfo> &cb, napi_value &object);
60     static bool TypeOf(napi_env env, napi_value value, napi_valuetype type);
61     static void DeleteCallbackInfo(std::unique_ptr<CallbackInfo> callback);
62     template <typename T>
DeletePtr(T & ptr)63     static void DeletePtr(T &ptr)
64     {
65         if (ptr != nullptr) {
66             delete ptr;
67             ptr = nullptr;
68         }
69     }
70 };
71 } // namespace MMI
72 } // namespace OHOS
73 
74 #endif // JS_UTIL_H