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