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