• 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 #include "js_util.h"
17 
18 #include <linux/input.h>
19 
20 #include "mmi_log.h"
21 #include "napi_constants.h"
22 #include "util_napi.h"
23 
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "JsUtil"
26 
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30 std::unordered_map<int32_t, std::string> axisType = {
31     { ABS_MT_TOUCH_MAJOR, "touchmajor" },
32     { ABS_MT_TOUCH_MINOR, "touchminor" },
33     { ABS_MT_ORIENTATION, "orientation" },
34     { ABS_MT_POSITION_X, "x" },
35     { ABS_MT_POSITION_Y, "y" },
36     { ABS_MT_PRESSURE, "pressure" },
37     { ABS_MT_WIDTH_MAJOR, "toolmajor" },
38     { ABS_MT_WIDTH_MINOR, "toolminor" },
39 };
40 
41 constexpr uint32_t EVDEV_UDEV_TAG_TOUCHSCREEN = (1 << 4);
42 constexpr uint32_t EVDEV_UDEV_TAG_JOYSTICK = (1 << 6);
43 constexpr uint32_t EVDEV_UDEV_TAG_TRACKBALL = (1 << 10);
44 
45 JsUtil::DeviceType g_deviceType[] = {
46     { "keyboard", EVDEV_UDEV_TAG_KEYBOARD },
47     { "mouse", EVDEV_UDEV_TAG_MOUSE },
48     { "touchpad", EVDEV_UDEV_TAG_TOUCHPAD },
49     { "touchscreen", EVDEV_UDEV_TAG_TOUCHSCREEN },
50     { "joystick", EVDEV_UDEV_TAG_JOYSTICK },
51     { "trackball", EVDEV_UDEV_TAG_TRACKBALL },
52 };
53 } // namespace
IsSameHandle(napi_env env,napi_value handle,napi_ref ref)54 bool JsUtil::IsSameHandle(napi_env env, napi_value handle, napi_ref ref)
55 {
56     napi_handle_scope scope = nullptr;
57     napi_open_handle_scope(env, &scope);
58     napi_value handlerTemp = nullptr;
59     if (napi_get_reference_value(env, ref, &handlerTemp) != napi_ok) {
60         MMI_HILOGE("%{public}s failed", std::string(GET_REFERENCE_VALUE).c_str());
61         napi_close_handle_scope(env, scope);
62         return false;
63     }
64     bool isEqual = false;
65     if (napi_strict_equals(env, handle, handlerTemp, &isEqual) != napi_ok) {
66         MMI_HILOGE("%{public}s failed", std::string(STRICT_EQUALS).c_str());
67         napi_close_handle_scope(env, scope);
68         return false;
69     }
70     napi_close_handle_scope(env, scope);
71     return isEqual;
72 }
73 
GetDeviceInfo(sptr<CallbackInfo> cb)74 napi_value JsUtil::GetDeviceInfo(sptr<CallbackInfo> cb)
75 {
76     CHKPP(cb);
77     CHKPP(cb->env);
78     CHKPP(cb->data.device);
79     napi_value object = nullptr;
80     CHKRP(napi_create_object(cb->env, &object), CREATE_OBJECT);
81     napi_value id = nullptr;
82     CHKRP(napi_create_int32(cb->env, cb->data.device->GetId(), &id), CREATE_INT32);
83     napi_value name = nullptr;
84     CHKRP(napi_create_string_utf8(cb->env, (cb->data.device->GetName()).c_str(),
85         NAPI_AUTO_LENGTH, &name), CREATE_STRING_UTF8);
86     CHKRP(napi_set_named_property(cb->env, object, "id", id), SET_NAMED_PROPERTY);
87     CHKRP(napi_set_named_property(cb->env, object, "name", name), SET_NAMED_PROPERTY);
88     napi_value busType = nullptr;
89     CHKRP(napi_create_int32(cb->env, cb->data.device->GetBus(), &busType), CREATE_INT32);
90     CHKRP(napi_set_named_property(cb->env, object, "bus", busType), SET_NAMED_PROPERTY);
91     napi_value product = nullptr;
92     CHKRP(napi_create_int32(cb->env, cb->data.device->GetProduct(), &product), CREATE_INT32);
93     CHKRP(napi_set_named_property(cb->env, object, "product", product), SET_NAMED_PROPERTY);
94     napi_value vendor = nullptr;
95     CHKRP(napi_create_int32(cb->env, cb->data.device->GetVendor(), &vendor), CREATE_INT32);
96     CHKRP(napi_set_named_property(cb->env, object, "vendor", vendor), SET_NAMED_PROPERTY);
97     napi_value version = nullptr;
98     CHKRP(napi_create_int32(cb->env, cb->data.device->GetVersion(), &version), CREATE_INT32);
99     CHKRP(napi_set_named_property(cb->env, object, "version", version), SET_NAMED_PROPERTY);
100     napi_value uniq = nullptr;
101     CHKRP(napi_create_string_utf8(cb->env, (cb->data.device->GetUniq()).c_str(),
102         NAPI_AUTO_LENGTH, &uniq), CREATE_STRING_UTF8);
103     CHKRP(napi_set_named_property(cb->env, object, "uniq", uniq), SET_NAMED_PROPERTY);
104     napi_value phys = nullptr;
105     CHKRP(napi_create_string_utf8(cb->env, (cb->data.device->GetPhys()).c_str(),
106         NAPI_AUTO_LENGTH, &phys), CREATE_STRING_UTF8);
107     CHKRP(napi_set_named_property(cb->env, object, "phys", phys), SET_NAMED_PROPERTY);
108 
109     if (!GetDeviceSourceType(cb, object)) {
110         MMI_HILOGE("Get device source type failed");
111         return nullptr;
112     }
113     if (!GetDeviceAxisInfo(cb, object)) {
114         MMI_HILOGE("Get device axis failed");
115         return nullptr;
116     }
117     return object;
118 }
119 
GetDeviceAxisInfo(sptr<CallbackInfo> cb,napi_value & object)120 bool JsUtil::GetDeviceAxisInfo(sptr<CallbackInfo> cb, napi_value &object)
121 {
122     CHKPF(cb);
123     CHKPF(cb->env);
124     CHKPF(cb->data.device);
125     napi_value sourceType = nullptr;
126     uint32_t types = static_cast<uint32_t>(cb->data.device->GetType());
127     for (const auto &item : g_deviceType) {
128         if (types &item.typeBit) {
129             CHKRF(napi_create_string_utf8(cb->env, item.sourceTypeName.c_str(),
130                 NAPI_AUTO_LENGTH, &sourceType), CREATE_STRING_UTF8);
131             break;
132         }
133     }
134     napi_value axisRanges = nullptr;
135     CHKRF(napi_create_array(cb->env, &axisRanges), CREATE_ARRAY);
136     if (sourceType == nullptr) {
137         CHKRF(napi_set_named_property(cb->env, object, "axisRanges", axisRanges), SET_NAMED_PROPERTY);
138         MMI_HILOGD("SourceType not found");
139         return true;
140     }
141     napi_value axisRange = nullptr;
142     uint32_t i = 0;
143     for (const auto &item : cb->data.device->GetAxisInfo()) {
144         auto iter = axisType.find(item.GetAxisType());
145         if (iter == axisType.end()) {
146             MMI_HILOGD("Find axisType failed");
147             continue;
148         }
149         CHKRF(napi_create_object(cb->env, &axisRange), CREATE_OBJECT);
150         CHKRF(napi_set_named_property(cb->env, axisRange, "source", sourceType), SET_NAMED_PROPERTY);
151         napi_value axisType = nullptr;
152         CHKRF(napi_create_string_utf8(cb->env, iter->second.c_str(),
153             NAPI_AUTO_LENGTH, &axisType), CREATE_STRING_UTF8);
154         CHKRF(napi_set_named_property(cb->env, axisRange, "axis", axisType), SET_NAMED_PROPERTY);
155         napi_value min = nullptr;
156         CHKRF(napi_create_int32(cb->env, item.GetMinimum(), &min), CREATE_INT32);
157         CHKRF(napi_set_named_property(cb->env, axisRange, "min", min), SET_NAMED_PROPERTY);
158         napi_value max = nullptr;
159         CHKRF(napi_create_int32(cb->env, item.GetMaximum(), &max), CREATE_INT32);
160         CHKRF(napi_set_named_property(cb->env, axisRange, "max", max), SET_NAMED_PROPERTY);
161         napi_value fuzz = nullptr;
162         CHKRF(napi_create_int32(cb->env, item.GetFuzz(), &fuzz), CREATE_INT32);
163         CHKRF(napi_set_named_property(cb->env, axisRange, "fuzz", fuzz), SET_NAMED_PROPERTY);
164         napi_value flat = nullptr;
165         CHKRF(napi_create_int32(cb->env, item.GetFlat(), &flat), CREATE_INT32);
166         CHKRF(napi_set_named_property(cb->env, axisRange, "flat", flat), SET_NAMED_PROPERTY);
167         napi_value resolution = nullptr;
168         CHKRF(napi_create_int32(cb->env, item.GetResolution(), &resolution), CREATE_INT32);
169         CHKRF(napi_set_named_property(cb->env, axisRange, "resolution", resolution), SET_NAMED_PROPERTY);
170         CHKRF(napi_set_element(cb->env, axisRanges, i, axisRange), SET_ELEMENT);
171         ++i;
172     }
173     CHKRF(napi_set_named_property(cb->env, object, "axisRanges", axisRanges), SET_NAMED_PROPERTY);
174     return true;
175 }
176 
GetDeviceSourceType(sptr<CallbackInfo> cb,napi_value & object)177 bool JsUtil::GetDeviceSourceType(sptr<CallbackInfo> cb, napi_value &object)
178 {
179     CHKPF(cb);
180     CHKPF(cb->env);
181     CHKPF(cb->data.device);
182     uint32_t types = static_cast<uint32_t>(cb->data.device->GetType());
183     std::vector<std::string> sources;
184     for (const auto &item : g_deviceType) {
185         if (types &item.typeBit) {
186             sources.push_back(item.sourceTypeName);
187         }
188     }
189     napi_value devSources = nullptr;
190     CHKRF(napi_create_array(cb->env, &devSources), CREATE_ARRAY);
191     napi_value value = nullptr;
192     for (size_t i = 0; i < sources.size(); ++i) {
193         CHKRF(napi_create_string_utf8(cb->env, sources[i].c_str(), NAPI_AUTO_LENGTH, &value),
194             CREATE_STRING_UTF8);
195         CHKRF(napi_set_element(cb->env, devSources, i, value), SET_ELEMENT);
196     }
197     CHKRF(napi_set_named_property(cb->env, object, "sources", devSources), SET_NAMED_PROPERTY);
198     return true;
199 }
200 
TypeOf(napi_env env,napi_value value,napi_valuetype type)201 bool JsUtil::TypeOf(napi_env env, napi_value value, napi_valuetype type)
202 {
203     napi_valuetype valueType = napi_undefined;
204     CHKRF(napi_typeof(env, value, &valueType), TYPEOF);
205     if (valueType != type) {
206         return false;
207     }
208     return true;
209 }
210 
DeleteCallbackInfo(std::unique_ptr<CallbackInfo> callback)211 void JsUtil::DeleteCallbackInfo(std::unique_ptr<CallbackInfo> callback)
212 {
213     CALL_DEBUG_ENTER;
214     CHKPV(callback);
215     if (callback->ref != nullptr && callback->env != nullptr) {
216         CHKRV(napi_delete_reference(callback->env, callback->ref), DELETE_REFERENCE);
217         callback->env = nullptr;
218     }
219 }
220 } // namespace MMI
221 } // namespace OHOS