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_node_api.h" 22 #include "refbase.h" 23 24 #include "input_device.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 int32_t deviceId { 0 }; 41 int32_t keyboardType { 0 }; 42 int32_t keyboardRepeatDelay { 0 }; 43 int32_t keyboardRepeatRate { 0 }; 44 int64_t IntervalSinceLastInput { 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 bool setFuncKeyType { false }; 59 bool getFuncKeyType { false }; 60 }; 61 struct DeviceType { 62 std::string sourceTypeName; 63 uint32_t typeBit { 0 }; 64 }; 65 66 static bool IsSameHandle(napi_env env, napi_value handle, napi_ref ref); 67 static napi_value GetDeviceInfo(sptr<CallbackInfo> cb); 68 static bool GetDeviceAxisInfo(sptr<CallbackInfo> cb, napi_value &object); 69 static bool GetDeviceSourceType(sptr<CallbackInfo> cb, napi_value &object); 70 static bool TypeOf(napi_env env, napi_value value, napi_valuetype type); 71 static void DeleteCallbackInfo(std::unique_ptr<CallbackInfo> callback); 72 template <typename T> DeletePtr(T & ptr)73 static void DeletePtr(T &ptr) 74 { 75 if (ptr != nullptr) { 76 delete ptr; 77 ptr = nullptr; 78 } 79 } 80 }; 81 } // namespace MMI 82 } // namespace OHOS 83 #endif // JS_UTIL_H