• 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_pointer_manager.h"
17 
18 #include "napi_constants.h"
19 
20 namespace OHOS {
21 namespace MMI {
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "JsPointerManager" };
24 
25 enum class ReturnType {
26     VOID,
27     BOOL,
28     NUMBER,
29 };
30 }
31 
TypeOf(napi_env env,napi_value value,napi_valuetype type)32 bool JsCommon::TypeOf(napi_env env, napi_value value, napi_valuetype type)
33 {
34     napi_valuetype valueType = napi_undefined;
35     CHKRF(napi_typeof(env, value, &valueType), TYPEOF);
36     if (valueType != type) {
37         return false;
38     }
39     return true;
40 }
41 
~AsyncContext()42 AsyncContext::~AsyncContext()
43 {
44     CALL_DEBUG_ENTER;
45     if (work != nullptr) {
46         CHKRV(napi_delete_async_work(env, work), DELETE_ASYNC_WORK);
47     }
48     if (callback != nullptr && env != nullptr) {
49         CHKRV(napi_delete_reference(env, callback), DELETE_REFERENCE);
50         env = nullptr;
51     }
52 }
53 
getResult(sptr<AsyncContext> asyncContext,napi_value * results,int32_t size)54 bool getResult(sptr<AsyncContext> asyncContext, napi_value *results, int32_t size)
55 {
56     CALL_DEBUG_ENTER;
57     int32_t length = 2;
58     if (size < length) {
59         MMI_HILOGE("results size less than 2");
60         return false;
61     }
62     napi_env env = asyncContext->env;
63     if (asyncContext->errorCode != RET_OK) {
64         if (asyncContext->errorCode == RET_ERR) {
65             MMI_HILOGE("Other errors");
66             return false;
67         }
68         NapiError codeMsg;
69         if (!UtilNapiError::GetApiError(asyncContext->errorCode, codeMsg)) {
70             MMI_HILOGE("ErrorCode not found, errCode:%{public}d", asyncContext->errorCode);
71             return false;
72         }
73         napi_value errCode = nullptr;
74         napi_value errMsg = nullptr;
75         napi_value businessError = nullptr;
76         CHKRF(napi_create_int32(env, asyncContext->errorCode, &errCode), CREATE_INT32);
77         CHKRF(napi_create_string_utf8(env, codeMsg.msg.c_str(),
78             NAPI_AUTO_LENGTH, &errMsg), CREATE_STRING_UTF8);
79         CHKRF(napi_create_error(env, nullptr, errMsg, &businessError), CREATE_ERROR);
80         CHKRF(napi_set_named_property(env, businessError, ERR_CODE.c_str(), errCode), SET_NAMED_PROPERTY);
81         results[0] = businessError;
82     } else {
83         CHKRF(napi_get_undefined(env, &results[0]), GET_UNDEFINED);
84     }
85 
86     ReturnType resultType;
87     asyncContext->reserve >> resultType;
88     if (resultType == ReturnType::BOOL) {
89         bool temp;
90         asyncContext->reserve >> temp;
91         CHKRF(napi_get_boolean(env, temp, &results[1]), GET_BOOLEAN);
92     } else if (resultType == ReturnType::NUMBER) {
93         int32_t temp;
94         asyncContext->reserve >> temp;
95         CHKRF(napi_create_int32(env, temp, &results[1]), CREATE_INT32);
96     } else {
97         CHKRF(napi_get_undefined(env, &results[1]), GET_UNDEFINED);
98     }
99     return true;
100 }
101 
AsyncCallbackWork(sptr<AsyncContext> asyncContext)102 void AsyncCallbackWork(sptr<AsyncContext> asyncContext)
103 {
104     CALL_DEBUG_ENTER;
105     CHKPV(asyncContext);
106     CHKPV(asyncContext->env);
107     napi_env env = asyncContext->env;
108     napi_value resource = nullptr;
109     CHKRV(napi_create_string_utf8(env, "AsyncCallbackWork", NAPI_AUTO_LENGTH, &resource), CREATE_STRING_UTF8);
110     asyncContext->IncStrongRef(nullptr);
111     napi_status status = napi_create_async_work(env, nullptr, resource, [](napi_env env, void* data) {},
112         [](napi_env env, napi_status status, void* data) {
113             sptr<AsyncContext> asyncContext(static_cast<AsyncContext *>(data));
114             /**
115              * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced
116              * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the
117              * asynchronous task is created, and subtract 1 from the reference count after the naked
118              * pointer is converted to a pointer when the asynchronous task is executed, the reference
119              * count of the smart pointer is guaranteed to be 1.
120              */
121             asyncContext->DecStrongRef(nullptr);
122             napi_value results[2] = { 0 };
123             int32_t size = 2;
124             if (!getResult(asyncContext, results, size)) {
125                 MMI_HILOGE("Failed to create napi data");
126                 return;
127             }
128             if (asyncContext->deferred) {
129                 if (asyncContext->errorCode == RET_OK) {
130                     CHKRV(napi_resolve_deferred(env, asyncContext->deferred, results[1]), RESOLVE_DEFERRED);
131                 } else {
132                     CHKRV(napi_reject_deferred(env, asyncContext->deferred, results[0]), REJECT_DEFERRED);
133                 }
134             } else {
135                 napi_value callback = nullptr;
136                 CHKRV(napi_get_reference_value(env, asyncContext->callback, &callback), GET_REFERENCE_VALUE);
137                 napi_value callResult = nullptr;
138                 CHKRV(napi_call_function(env, nullptr, callback, size, results, &callResult), CALL_FUNCTION);
139             }
140         },
141         asyncContext.GetRefPtr(), &asyncContext->work);
142     if (status != napi_ok || napi_queue_async_work(env, asyncContext->work) != napi_ok) {
143         MMI_HILOGE("Create async work failed");
144         asyncContext->DecStrongRef(nullptr);
145     }
146 }
147 
SetPointerVisible(napi_env env,bool visible,napi_value handle)148 napi_value JsPointerManager::SetPointerVisible(napi_env env, bool visible, napi_value handle)
149 {
150     CALL_DEBUG_ENTER;
151     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
152     CHKPP(asyncContext);
153 
154     asyncContext->errorCode = InputManager::GetInstance()->SetPointerVisible(visible);
155     asyncContext->reserve << ReturnType::VOID;
156 
157     napi_value promise = nullptr;
158     if (handle != nullptr) {
159         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
160         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
161     } else {
162         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
163     }
164     AsyncCallbackWork(asyncContext);
165     return promise;
166 }
167 
SetPointerVisibleSync(napi_env env,bool visible)168 napi_value JsPointerManager::SetPointerVisibleSync(napi_env env, bool visible)
169 {
170     CALL_DEBUG_ENTER;
171     InputManager::GetInstance()->SetPointerVisible(visible);
172     napi_value result = nullptr;
173     if (napi_get_undefined(env, &result) != napi_ok) {
174         MMI_HILOGE("Get undefined result is failed");
175         return nullptr;
176     }
177     return result;
178 }
179 
IsPointerVisible(napi_env env,napi_value handle)180 napi_value JsPointerManager::IsPointerVisible(napi_env env, napi_value handle)
181 {
182     CALL_DEBUG_ENTER;
183     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
184     CHKPP(asyncContext);
185 
186     bool visible = InputManager::GetInstance()->IsPointerVisible();
187     asyncContext->errorCode = ERR_OK;
188     asyncContext->reserve << ReturnType::BOOL << visible;
189 
190     napi_value promise = nullptr;
191     if (handle != nullptr) {
192         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
193         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
194     } else {
195         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
196     }
197     AsyncCallbackWork(asyncContext);
198     return promise;
199 }
200 
IsPointerVisibleSync(napi_env env)201 napi_value JsPointerManager::IsPointerVisibleSync(napi_env env)
202 {
203     CALL_DEBUG_ENTER;
204     bool visible = InputManager::GetInstance()->IsPointerVisible();
205     napi_value result = nullptr;
206     NAPI_CALL(env, napi_get_boolean(env, visible, &result));
207     return result;
208 }
209 
SetPointerColor(napi_env env,int32_t color,napi_value handle)210 napi_value JsPointerManager::SetPointerColor(napi_env env, int32_t color, napi_value handle)
211 {
212     CALL_DEBUG_ENTER;
213     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
214     CHKPP(asyncContext);
215     asyncContext->errorCode = InputManager::GetInstance()->SetPointerColor(color);
216     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
217         MMI_HILOGE("Non system applications use system API");
218         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
219         return nullptr;
220     }
221     asyncContext->reserve << ReturnType::VOID;
222     napi_value promise = nullptr;
223     if (handle != nullptr) {
224         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
225         if (napi_get_undefined(env, &promise) != napi_ok) {
226             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
227             return nullptr;
228         }
229     } else {
230         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
231     }
232     AsyncCallbackWork(asyncContext);
233     return promise;
234 }
235 
GetPointerColor(napi_env env,napi_value handle)236 napi_value JsPointerManager::GetPointerColor(napi_env env, napi_value handle)
237 {
238     CALL_DEBUG_ENTER;
239     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
240     CHKPP(asyncContext);
241     int32_t color = 1;
242     asyncContext->errorCode = InputManager::GetInstance()->GetPointerColor(color);
243     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
244         MMI_HILOGE("Non system applications use system API");
245         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
246         return nullptr;
247     }
248     asyncContext->reserve << ReturnType::NUMBER << color;
249     napi_value promise = nullptr;
250     uint32_t initialRefCount = 1;
251     if (handle != nullptr) {
252         CHKRP(napi_create_reference(env, handle, initialRefCount, &asyncContext->callback), CREATE_REFERENCE);
253         if (napi_get_undefined(env, &promise) != napi_ok) {
254             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
255             return nullptr;
256         }
257     } else {
258         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
259     }
260     AsyncCallbackWork(asyncContext);
261     return promise;
262 }
263 
SetPointerColorSync(napi_env env,int32_t color)264 napi_value JsPointerManager::SetPointerColorSync(napi_env env, int32_t color)
265 {
266     CALL_DEBUG_ENTER;
267     auto errorCode = InputManager::GetInstance()->SetPointerColor(color);
268     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
269         MMI_HILOGE("Non system applications use system API");
270         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
271         return nullptr;
272     }
273 
274     napi_value result = nullptr;
275     if (napi_get_undefined(env, &result) != napi_ok) {
276         MMI_HILOGE("Get undefined result is failed");
277         return nullptr;
278     }
279     return result;
280 }
281 
GetPointerColorSync(napi_env env)282 napi_value JsPointerManager::GetPointerColorSync(napi_env env)
283 {
284     CALL_DEBUG_ENTER;
285     int32_t color = 1;
286     auto errorCode = InputManager::GetInstance()->GetPointerColor(color);
287     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
288         MMI_HILOGE("Non system applications use system API");
289         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
290         return nullptr;
291     }
292     napi_value result = nullptr;
293     NAPI_CALL(env, napi_create_int32(env, color, &result));
294     return result;
295 }
296 
SetPointerSpeed(napi_env env,int32_t pointerSpeed,napi_value handle)297 napi_value JsPointerManager::SetPointerSpeed(napi_env env, int32_t pointerSpeed, napi_value handle)
298 {
299     CALL_DEBUG_ENTER;
300     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
301     CHKPP(asyncContext);
302     asyncContext->errorCode = InputManager::GetInstance()->SetPointerSpeed(pointerSpeed);
303     asyncContext->reserve << ReturnType::VOID;
304     napi_value promise = nullptr;
305     if (handle != nullptr) {
306         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
307         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
308     } else {
309         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
310     }
311     AsyncCallbackWork(asyncContext);
312     return promise;
313 }
314 
SetPointerSpeedSync(napi_env env,int32_t pointerSpeed)315 napi_value JsPointerManager::SetPointerSpeedSync(napi_env env, int32_t pointerSpeed)
316 {
317     CALL_DEBUG_ENTER;
318     InputManager::GetInstance()->SetPointerSpeed(pointerSpeed);
319     napi_value result = nullptr;
320     if (napi_get_undefined(env, &result) != napi_ok) {
321         MMI_HILOGE("Get undefined result is failed");
322         return nullptr;
323     }
324     return result;
325 }
326 
GetPointerSpeed(napi_env env,napi_value handle)327 napi_value JsPointerManager::GetPointerSpeed(napi_env env, napi_value handle)
328 {
329     CALL_DEBUG_ENTER;
330     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
331     CHKPP(asyncContext);
332     int32_t pointerSpeed = 0;
333     asyncContext->errorCode = InputManager::GetInstance()->GetPointerSpeed(pointerSpeed);
334     asyncContext->reserve << ReturnType::NUMBER << pointerSpeed;
335     napi_value promise = nullptr;
336     uint32_t initial_refcount = 1;
337     if (handle != nullptr) {
338         CHKRP(napi_create_reference(env, handle, initial_refcount, &asyncContext->callback), CREATE_REFERENCE);
339         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
340     } else {
341         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
342     }
343     AsyncCallbackWork(asyncContext);
344     return promise;
345 }
346 
GetPointerSpeedSync(napi_env env)347 napi_value JsPointerManager::GetPointerSpeedSync(napi_env env)
348 {
349     CALL_DEBUG_ENTER;
350     int32_t pointerSpeed = 0;
351     InputManager::GetInstance()->GetPointerSpeed(pointerSpeed);
352     napi_value result = nullptr;
353     NAPI_CALL(env, napi_create_int32(env, pointerSpeed, &result));
354     return result;
355 }
356 
SetMouseScrollRows(napi_env env,int32_t rows,napi_value handle)357 napi_value JsPointerManager::SetMouseScrollRows(napi_env env, int32_t rows, napi_value handle)
358 {
359     CALL_DEBUG_ENTER;
360     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
361     CHKPP(asyncContext);
362     asyncContext->errorCode = InputManager::GetInstance()->SetMouseScrollRows(rows);
363     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
364         MMI_HILOGE("Non system applications use system API");
365         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
366         return nullptr;
367     }
368     asyncContext->reserve << ReturnType::VOID;
369     napi_value promise = nullptr;
370     if (handle != nullptr) {
371         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
372         if (napi_get_undefined(env, &promise) != napi_ok) {
373             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
374             return nullptr;
375         }
376     } else {
377         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
378     }
379     AsyncCallbackWork(asyncContext);
380     return promise;
381 }
382 
GetMouseScrollRows(napi_env env,napi_value handle)383 napi_value JsPointerManager::GetMouseScrollRows(napi_env env, napi_value handle)
384 {
385     CALL_DEBUG_ENTER;
386     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
387     CHKPP(asyncContext);
388     int32_t rows = 3;
389     asyncContext->errorCode = InputManager::GetInstance()->GetMouseScrollRows(rows);
390     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
391         MMI_HILOGE("Non system applications use system API");
392         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
393         return nullptr;
394     }
395     asyncContext->reserve << ReturnType::NUMBER << rows;
396     napi_value promise = nullptr;
397     uint32_t initialRefCount = 1;
398     if (handle != nullptr) {
399         CHKRP(napi_create_reference(env, handle, initialRefCount, &asyncContext->callback), CREATE_REFERENCE);
400         if (napi_get_undefined(env, &promise) != napi_ok) {
401             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
402             return nullptr;
403         }
404     } else {
405         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
406     }
407     AsyncCallbackWork(asyncContext);
408     return promise;
409 }
410 
SetPointerSize(napi_env env,int32_t size,napi_value handle)411 napi_value JsPointerManager::SetPointerSize(napi_env env, int32_t size, napi_value handle)
412 {
413     CALL_DEBUG_ENTER;
414     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
415     CHKPP(asyncContext);
416     asyncContext->errorCode = InputManager::GetInstance()->SetPointerSize(size);
417     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
418         MMI_HILOGE("Non system applications use system API");
419         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
420         return nullptr;
421     }
422     asyncContext->reserve << ReturnType::VOID;
423     napi_value promise = nullptr;
424     if (handle != nullptr) {
425         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
426         if (napi_get_undefined(env, &promise) != napi_ok) {
427             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
428             return nullptr;
429         }
430     } else {
431         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
432     }
433     AsyncCallbackWork(asyncContext);
434     return promise;
435 }
436 
GetPointerSize(napi_env env,napi_value handle)437 napi_value JsPointerManager::GetPointerSize(napi_env env, napi_value handle)
438 {
439     CALL_DEBUG_ENTER;
440     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
441     CHKPP(asyncContext);
442     int32_t size = 1;
443     asyncContext->errorCode = InputManager::GetInstance()->GetPointerSize(size);
444     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
445         MMI_HILOGE("Non system applications use system API");
446         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
447         return nullptr;
448     }
449     asyncContext->reserve << ReturnType::NUMBER << size;
450     napi_value promise = nullptr;
451     uint32_t initialRefCount = 1;
452     if (handle != nullptr) {
453         CHKRP(napi_create_reference(env, handle, initialRefCount, &asyncContext->callback), CREATE_REFERENCE);
454         if (napi_get_undefined(env, &promise) != napi_ok) {
455             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
456             return nullptr;
457         }
458     } else {
459         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
460     }
461     AsyncCallbackWork(asyncContext);
462     return promise;
463 }
464 
SetPointerSizeSync(napi_env env,int32_t size)465 napi_value JsPointerManager::SetPointerSizeSync(napi_env env, int32_t size)
466 {
467     CALL_DEBUG_ENTER;
468     auto errorCode = InputManager::GetInstance()->SetPointerSize(size);
469     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
470         MMI_HILOGE("Non system applications use system API");
471         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
472         return nullptr;
473     }
474 
475     napi_value result = nullptr;
476     if (napi_get_undefined(env, &result) != napi_ok) {
477         MMI_HILOGE("Get undefined result is failed");
478         return nullptr;
479     }
480     return result;
481 }
482 
GetPointerSizeSync(napi_env env)483 napi_value JsPointerManager::GetPointerSizeSync(napi_env env)
484 {
485     CALL_DEBUG_ENTER;
486     int32_t size = 1;
487     auto errorCode = InputManager::GetInstance()->GetPointerSize(size);
488     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
489         MMI_HILOGE("Non system applications use system API");
490         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
491         return nullptr;
492     }
493     napi_value result = nullptr;
494     NAPI_CALL(env, napi_create_int32(env, size, &result));
495     return result;
496 }
497 
SetPointerStyle(napi_env env,int32_t windowid,int32_t pointerStyle,napi_value handle)498 napi_value JsPointerManager::SetPointerStyle(napi_env env, int32_t windowid, int32_t pointerStyle, napi_value handle)
499 {
500     CALL_DEBUG_ENTER;
501     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
502     CHKPP(asyncContext);
503     PointerStyle style;
504     style.id = pointerStyle;
505     asyncContext->errorCode = InputManager::GetInstance()->SetPointerStyle(windowid, style);
506     asyncContext->reserve << ReturnType::VOID;
507 
508     napi_value promise = nullptr;
509     if (handle != nullptr) {
510         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
511         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
512     } else {
513         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
514     }
515     AsyncCallbackWork(asyncContext);
516     return promise;
517 }
518 
SetPointerStyleSync(napi_env env,int32_t windowid,int32_t pointerStyle)519 napi_value JsPointerManager::SetPointerStyleSync(napi_env env, int32_t windowid, int32_t pointerStyle)
520 {
521     CALL_DEBUG_ENTER;
522     PointerStyle style;
523     style.id = pointerStyle;
524     InputManager::GetInstance()->SetPointerStyle(windowid, style);
525     napi_value result = nullptr;
526     if (napi_get_undefined(env, &result) != napi_ok) {
527         MMI_HILOGE("Get undefined result is failed");
528         return nullptr;
529     }
530     return result;
531 }
532 
GetPointerStyle(napi_env env,int32_t windowid,napi_value handle)533 napi_value JsPointerManager::GetPointerStyle(napi_env env, int32_t windowid, napi_value handle)
534 {
535     CALL_DEBUG_ENTER;
536     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
537     CHKPP(asyncContext);
538     PointerStyle pointerStyle;
539     asyncContext->errorCode = InputManager::GetInstance()->GetPointerStyle(windowid, pointerStyle);
540     asyncContext->reserve << ReturnType::NUMBER << pointerStyle.id;
541     napi_value promise = nullptr;
542     if (handle != nullptr) {
543         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
544         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
545     } else {
546         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
547     }
548     AsyncCallbackWork(asyncContext);
549     return promise;
550 }
551 
GetPointerStyleSync(napi_env env,int32_t windowid)552 napi_value JsPointerManager::GetPointerStyleSync(napi_env env, int32_t windowid)
553 {
554     CALL_DEBUG_ENTER;
555     PointerStyle pointerStyle;
556     InputManager::GetInstance()->GetPointerStyle(windowid, pointerStyle);
557     napi_value result = nullptr;
558     NAPI_CALL(env, napi_create_int32(env, pointerStyle.id, &result));
559     return result;
560 }
561 
EnterCaptureMode(napi_env env,int32_t windowId,napi_value handle)562 napi_value JsPointerManager::EnterCaptureMode(napi_env env, int32_t windowId, napi_value handle)
563 {
564     CALL_DEBUG_ENTER;
565     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
566     if (asyncContext == nullptr) {
567         THROWERR(env, "Create AsyncContext failed");
568         return nullptr;
569     }
570     asyncContext->errorCode = InputManager::GetInstance()->EnterCaptureMode(windowId);
571     asyncContext->reserve << ReturnType::VOID;
572 
573     napi_value promise = nullptr;
574     if (handle != nullptr) {
575         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
576         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
577     } else {
578         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
579     }
580     AsyncCallbackWork(asyncContext);
581     return promise;
582 }
583 
LeaveCaptureMode(napi_env env,int32_t windowId,napi_value handle)584 napi_value JsPointerManager::LeaveCaptureMode(napi_env env, int32_t windowId, napi_value handle)
585 {
586     CALL_DEBUG_ENTER;
587     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
588     if (asyncContext == nullptr) {
589         THROWERR(env, "Create AsyncContext failed");
590         return nullptr;
591     }
592 
593     asyncContext->errorCode = InputManager::GetInstance()->LeaveCaptureMode(windowId);
594     asyncContext->reserve << ReturnType::VOID;
595 
596     napi_value promise = nullptr;
597     if (handle != nullptr) {
598         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
599         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
600     } else {
601         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
602     }
603     AsyncCallbackWork(asyncContext);
604     return promise;
605 }
606 
SetMousePrimaryButton(napi_env env,int32_t primaryButton,napi_value handle)607 napi_value JsPointerManager::SetMousePrimaryButton(napi_env env, int32_t primaryButton, napi_value handle)
608 {
609     CALL_DEBUG_ENTER;
610     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
611     CHKPP(asyncContext);
612 
613     asyncContext->errorCode = InputManager::GetInstance()->SetMousePrimaryButton(primaryButton);
614     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
615         MMI_HILOGE("Non system applications use system API");
616         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
617         return nullptr;
618     }
619     asyncContext->reserve << ReturnType::VOID;
620 
621     napi_value promise = nullptr;
622     if (handle != nullptr) {
623         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
624         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
625     } else {
626         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
627     }
628     AsyncCallbackWork(asyncContext);
629     return promise;
630 }
631 
GetMousePrimaryButton(napi_env env,napi_value handle)632 napi_value JsPointerManager::GetMousePrimaryButton(napi_env env, napi_value handle)
633 {
634     CALL_DEBUG_ENTER;
635     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
636     CHKPP(asyncContext);
637     int32_t primaryButton;
638     asyncContext->errorCode = InputManager::GetInstance()->GetMousePrimaryButton(primaryButton);
639     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
640         MMI_HILOGE("Non system applications use system API");
641         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
642         return nullptr;
643     }
644     asyncContext->reserve << ReturnType::NUMBER << primaryButton;
645     napi_value promise = nullptr;
646     if (handle != nullptr) {
647         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
648         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
649     } else {
650         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
651     }
652     AsyncCallbackWork(asyncContext);
653     return promise;
654 }
655 
SetHoverScrollState(napi_env env,bool state,napi_value handle)656 napi_value JsPointerManager::SetHoverScrollState(napi_env env, bool state, napi_value handle)
657 {
658     CALL_DEBUG_ENTER;
659     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
660     CHKPP(asyncContext);
661 
662     asyncContext->errorCode = InputManager::GetInstance()->SetHoverScrollState(state);
663     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
664         MMI_HILOGE("Non system applications use system API");
665         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
666         return nullptr;
667     }
668     asyncContext->reserve << ReturnType::VOID;
669 
670     napi_value promise = nullptr;
671     if (handle != nullptr) {
672         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
673         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
674     } else {
675         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
676     }
677     AsyncCallbackWork(asyncContext);
678     return promise;
679 }
680 
GetHoverScrollState(napi_env env,napi_value handle)681 napi_value JsPointerManager::GetHoverScrollState(napi_env env, napi_value handle)
682 {
683     CALL_DEBUG_ENTER;
684     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
685     CHKPP(asyncContext);
686 
687     bool state;
688     asyncContext->errorCode = InputManager::GetInstance()->GetHoverScrollState(state);
689     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
690         MMI_HILOGE("Non system applications use system API");
691         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
692         return nullptr;
693     }
694     asyncContext->reserve << ReturnType::BOOL << state;
695 
696     napi_value promise = nullptr;
697     if (handle != nullptr) {
698         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
699         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
700     } else {
701         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
702     }
703     AsyncCallbackWork(asyncContext);
704     return promise;
705 }
706 
SetTouchpadData(napi_env env,napi_value handle,int32_t errorCode)707 napi_value JsPointerManager::SetTouchpadData(napi_env env, napi_value handle, int32_t errorCode)
708 {
709     CALL_DEBUG_ENTER;
710     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
711         MMI_HILOGE("Non system applications use system API");
712         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
713         return nullptr;
714     }
715 
716     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
717     CHKPP(asyncContext);
718 
719     asyncContext->errorCode = errorCode;
720     asyncContext->reserve << ReturnType::VOID;
721 
722     napi_value promise = nullptr;
723     if (handle != nullptr) {
724         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
725         if (napi_get_undefined(env, &promise) != napi_ok) {
726             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
727             return nullptr;
728         }
729     } else {
730         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
731     }
732     AsyncCallbackWork(asyncContext);
733     return promise;
734 }
735 
GetTouchpadBoolData(napi_env env,napi_value handle,bool data,int32_t errorCode)736 napi_value JsPointerManager::GetTouchpadBoolData(napi_env env, napi_value handle, bool data, int32_t errorCode)
737 {
738     CALL_DEBUG_ENTER;
739     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
740         MMI_HILOGE("Non system applications use system API");
741         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
742         return nullptr;
743     }
744 
745     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
746     CHKPP(asyncContext);
747 
748     asyncContext->errorCode = errorCode;
749     asyncContext->reserve << ReturnType::BOOL << data;
750 
751     napi_value promise = nullptr;
752     if (handle != nullptr) {
753         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
754         if (napi_get_undefined(env, &promise) != napi_ok) {
755             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
756             return nullptr;
757         }
758     } else {
759         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
760     }
761     AsyncCallbackWork(asyncContext);
762     return promise;
763 }
764 
GetTouchpadInt32Data(napi_env env,napi_value handle,int32_t data,int32_t errorCode)765 napi_value JsPointerManager::GetTouchpadInt32Data(napi_env env, napi_value handle, int32_t data, int32_t errorCode)
766 {
767     CALL_DEBUG_ENTER;
768     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
769         MMI_HILOGE("Non system applications use system API");
770         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
771         return nullptr;
772     }
773 
774     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
775     CHKPP(asyncContext);
776 
777     asyncContext->errorCode = errorCode;
778     asyncContext->reserve << ReturnType::NUMBER << data;
779 
780     napi_value promise = nullptr;
781     if (handle != nullptr) {
782         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
783         if (napi_get_undefined(env, &promise) != napi_ok) {
784             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
785             return nullptr;
786         }
787     } else {
788         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
789     }
790     AsyncCallbackWork(asyncContext);
791     return promise;
792 }
793 
SetTouchpadScrollSwitch(napi_env env,bool switchFlag,napi_value handle)794 napi_value JsPointerManager::SetTouchpadScrollSwitch(napi_env env, bool switchFlag, napi_value handle)
795 {
796     CALL_DEBUG_ENTER;
797     int32_t ret = InputManager::GetInstance()->SetTouchpadScrollSwitch(switchFlag);
798     return SetTouchpadData(env, handle, ret);
799 }
800 
GetTouchpadScrollSwitch(napi_env env,napi_value handle)801 napi_value JsPointerManager::GetTouchpadScrollSwitch(napi_env env, napi_value handle)
802 {
803     CALL_DEBUG_ENTER;
804     bool switchFlag = true;
805     int32_t ret = InputManager::GetInstance()->GetTouchpadScrollSwitch(switchFlag);
806     return GetTouchpadBoolData(env, handle, switchFlag, ret);
807 }
808 
SetTouchpadScrollDirection(napi_env env,bool state,napi_value handle)809 napi_value JsPointerManager::SetTouchpadScrollDirection(napi_env env, bool state, napi_value handle)
810 {
811     CALL_DEBUG_ENTER;
812     int32_t ret = InputManager::GetInstance()->SetTouchpadScrollDirection(state);
813     return SetTouchpadData(env, handle, ret);
814 }
815 
GetTouchpadScrollDirection(napi_env env,napi_value handle)816 napi_value JsPointerManager::GetTouchpadScrollDirection(napi_env env, napi_value handle)
817 {
818     CALL_DEBUG_ENTER;
819     bool state = true;
820     int32_t ret = InputManager::GetInstance()->GetTouchpadScrollDirection(state);
821     return GetTouchpadBoolData(env, handle, state, ret);
822 }
823 
SetTouchpadTapSwitch(napi_env env,bool switchFlag,napi_value handle)824 napi_value JsPointerManager::SetTouchpadTapSwitch(napi_env env, bool switchFlag, napi_value handle)
825 {
826     CALL_DEBUG_ENTER;
827     int32_t ret = InputManager::GetInstance()->SetTouchpadTapSwitch(switchFlag);
828     return SetTouchpadData(env, handle, ret);
829 }
830 
GetTouchpadTapSwitch(napi_env env,napi_value handle)831 napi_value JsPointerManager::GetTouchpadTapSwitch(napi_env env, napi_value handle)
832 {
833     CALL_DEBUG_ENTER;
834     bool switchFlag = true;
835     int32_t ret = InputManager::GetInstance()->GetTouchpadTapSwitch(switchFlag);
836     return GetTouchpadBoolData(env, handle, switchFlag, ret);
837 }
SetTouchpadPointerSpeed(napi_env env,int32_t speed,napi_value handle)838 napi_value JsPointerManager::SetTouchpadPointerSpeed(napi_env env, int32_t speed, napi_value handle)
839 {
840     CALL_DEBUG_ENTER;
841     int32_t ret = InputManager::GetInstance()->SetTouchpadPointerSpeed(speed);
842     return SetTouchpadData(env, handle, ret);
843 }
844 
GetTouchpadPointerSpeed(napi_env env,napi_value handle)845 napi_value JsPointerManager::GetTouchpadPointerSpeed(napi_env env, napi_value handle)
846 {
847     CALL_DEBUG_ENTER;
848     int32_t speed;
849     int32_t ret = InputManager::GetInstance()->GetTouchpadPointerSpeed(speed);
850     return GetTouchpadInt32Data(env, handle, speed, ret);
851 }
852 
SetTouchpadPinchSwitch(napi_env env,bool switchFlag,napi_value handle)853 napi_value JsPointerManager::SetTouchpadPinchSwitch(napi_env env, bool switchFlag, napi_value handle)
854 {
855     CALL_DEBUG_ENTER;
856     int32_t ret = InputManager::GetInstance()->SetTouchpadPinchSwitch(switchFlag);
857     return SetTouchpadData(env, handle, ret);
858 }
859 
GetTouchpadPinchSwitch(napi_env env,napi_value handle)860 napi_value JsPointerManager::GetTouchpadPinchSwitch(napi_env env, napi_value handle)
861 {
862     CALL_DEBUG_ENTER;
863     bool switchFlag = true;
864     int32_t ret = InputManager::GetInstance()->GetTouchpadPinchSwitch(switchFlag);
865     return GetTouchpadBoolData(env, handle, switchFlag, ret);
866 }
867 
SetTouchpadSwipeSwitch(napi_env env,bool switchFlag,napi_value handle)868 napi_value JsPointerManager::SetTouchpadSwipeSwitch(napi_env env, bool switchFlag, napi_value handle)
869 {
870     CALL_DEBUG_ENTER;
871     int32_t ret = InputManager::GetInstance()->SetTouchpadSwipeSwitch(switchFlag);
872     return SetTouchpadData(env, handle, ret);
873 }
874 
GetTouchpadSwipeSwitch(napi_env env,napi_value handle)875 napi_value JsPointerManager::GetTouchpadSwipeSwitch(napi_env env, napi_value handle)
876 {
877     CALL_DEBUG_ENTER;
878     bool switchFlag = true;
879     int32_t ret = InputManager::GetInstance()->GetTouchpadSwipeSwitch(switchFlag);
880     return GetTouchpadBoolData(env, handle, switchFlag, ret);
881 }
882 
SetTouchpadRightClickType(napi_env env,int32_t type,napi_value handle)883 napi_value JsPointerManager::SetTouchpadRightClickType(napi_env env, int32_t type, napi_value handle)
884 {
885     CALL_DEBUG_ENTER;
886     int32_t ret = InputManager::GetInstance()->SetTouchpadRightClickType(type);
887     return SetTouchpadData(env, handle, ret);
888 }
889 
GetTouchpadRightClickType(napi_env env,napi_value handle)890 napi_value JsPointerManager::GetTouchpadRightClickType(napi_env env, napi_value handle)
891 {
892     CALL_DEBUG_ENTER;
893     int32_t type = 1;
894     int32_t ret = InputManager::GetInstance()->GetTouchpadRightClickType(type);
895     return GetTouchpadInt32Data(env, handle, type, ret);
896 }
897 } // namespace MMI
898 } // namespace OHOS