• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "ani_util.h"
17 
18 #include <unordered_map>
19 #include <linux/input.h>
20 
21 #include "mmi_log.h"
22 #include "napi_constants.h"
23 
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "AniUtil"
26 
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30 
31 constexpr uint32_t EVDEV_UDEV_TAG_TOUCHSCREEN = (1 << 4);
32 constexpr uint32_t EVDEV_UDEV_TAG_JOYSTICK = (1 << 6);
33 constexpr uint32_t EVDEV_UDEV_TAG_TRACKBALL = (1 << 10);
34 
35 AniUtil::DeviceType g_deviceType[] = {
36     { "keyboard", EVDEV_UDEV_TAG_KEYBOARD },
37     { "mouse", EVDEV_UDEV_TAG_MOUSE },
38     { "touchpad", EVDEV_UDEV_TAG_TOUCHPAD },
39     { "touchscreen", EVDEV_UDEV_TAG_TOUCHSCREEN },
40     { "joystick", EVDEV_UDEV_TAG_JOYSTICK },
41     { "trackball", EVDEV_UDEV_TAG_TRACKBALL },
42 };
43 } // namespace
44 
SetCallback(ani_object handle)45 bool AniUtil::CallbackInfo::SetCallback(ani_object handle)
46 {
47     if (ANI_OK != env_->GlobalReference_Create(handle, &callback_)) {
48         MMI_HILOGE("%{public}s: Create global callback failed", __func__);
49         return false;
50     }
51     return true;
52 }
53 
~CallbackInfo()54 AniUtil::CallbackInfo::~CallbackInfo()
55 {
56     CALL_DEBUG_ENTER;
57     if (env_ == nullptr) {
58         return;
59     }
60     if (callback_ != nullptr) {
61         env_->GlobalReference_Delete(callback_);
62     }
63     callback_ = nullptr;
64 }
65 
IsSameHandle(ani_env * env,ani_ref handle,ani_env * iterEnv,ani_ref iterhandle)66 bool AniUtil::IsSameHandle(ani_env *env, ani_ref handle, ani_env *iterEnv, ani_ref iterhandle)
67 {
68     if (env != iterEnv) {
69         MMI_HILOGD("%{public}s: not the same env", __func__);
70         return false;
71     }
72     ani_boolean isEquals = false;
73     if (ANI_OK != env->Reference_StrictEquals(handle, iterhandle, &isEquals)) {
74         MMI_HILOGD("%{public}s: check observer equal failed!", __func__);
75         return false;
76     }
77     return isEquals;
78 }
79 
StdStringToANIString(ani_env * env,const std::string & str)80 ani_string AniUtil::StdStringToANIString(ani_env* env, const std::string& str)
81 {
82     ani_string stringAni = nullptr;
83     if (ANI_OK != env->String_NewUTF8(str.c_str(), str.size(), &stringAni)) {
84         MMI_HILOGD("%{public}s: String_NewUTF8 Failed", __func__);
85     }
86     return stringAni;
87 }
88 
IsInstanceOf(ani_env * env,const std::string & cls_name,ani_object obj)89 ani_boolean AniUtil::IsInstanceOf(ani_env *env, const std::string &cls_name, ani_object obj)
90 {
91     ani_class cls;
92     if (ANI_OK != env->FindClass(cls_name.c_str(), &cls)) {
93         MMI_HILOGE("%{public}s: FindClass failed", __func__);
94         return ANI_FALSE;
95     }
96 
97     ani_boolean ret;
98     env->Object_InstanceOf(obj, cls, &ret);
99     return ret;
100 }
101 
CreateAniObject(ani_env * env,const char * nsName,const char * className)102 ani_object AniUtil::CreateAniObject(ani_env *env, const char *nsName, const char *className)
103 {
104     ani_namespace ns;
105     if (ANI_OK != env->FindNamespace(nsName, &ns)) {
106         MMI_HILOGE("%{public}s: Not found %{public}s", __func__, nsName);
107         return nullptr;
108     }
109 
110     ani_class cls;
111     if (ANI_OK != env->Namespace_FindClass(ns, className, &cls)) {
112         MMI_HILOGE("%{public}s: Namespace_FindClass %{public}s failed",  __func__, className);
113         return nullptr;
114     }
115 
116     ani_method ctor;
117     if (ANI_OK != env->Class_FindMethod(cls, "<ctor>", nullptr, &ctor)) {
118         MMI_HILOGE("%{public}s: Class_FindMethod 'constructor' failed",  __func__);
119         return nullptr;
120     }
121 
122     ani_object obj;
123     if (ANI_OK != env->Object_New(cls, ctor, &obj)) {
124         MMI_HILOGE("%{public}s: Object_New ani_object failed",  __func__);
125         return nullptr;
126     }
127     return obj;
128 }
129 
130 } // namespace MMI
131 } // namespace OHOS