• 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 "pixel_map.h"
19 
20 #undef MMI_LOG_TAG
21 #define MMI_LOG_TAG "JsPointerManager"
22 
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 enum class ReturnType {
27     VOID,
28     BOOL,
29     NUMBER,
30 };
31 constexpr int32_t TOUCHPAD_SCROLL_ROWS { 3 };
32 }
33 
TypeOf(napi_env env,napi_value value,napi_valuetype type)34 bool JsCommon::TypeOf(napi_env env, napi_value value, napi_valuetype type)
35 {
36     napi_valuetype valueType = napi_undefined;
37     CHKRF(napi_typeof(env, value, &valueType), TYPEOF);
38     if (valueType != type) {
39         return false;
40     }
41     return true;
42 }
43 
~AsyncContext()44 AsyncContext::~AsyncContext()
45 {
46     CALL_DEBUG_ENTER;
47     if (work != nullptr) {
48         CHKRV(napi_delete_async_work(env, work), DELETE_ASYNC_WORK);
49     }
50     if (callback != nullptr && env != nullptr) {
51         CHKRV(napi_delete_reference(env, callback), DELETE_REFERENCE);
52         env = nullptr;
53     }
54 }
55 
GetResult(sptr<AsyncContext> asyncContext,napi_value * results,int32_t size)56 static bool GetResult(sptr<AsyncContext> asyncContext, napi_value *results, int32_t size)
57 {
58     CALL_DEBUG_ENTER;
59     int32_t length = 2;
60     if (size < length) {
61         MMI_HILOGE("Results size less than 2");
62         return false;
63     }
64     CHKPF(asyncContext);
65     napi_env env = asyncContext->env;
66     if (asyncContext->errorCode != RET_OK) {
67         if (asyncContext->errorCode == RET_ERR) {
68             MMI_HILOGE("Other errors");
69             return false;
70         }
71         NapiError codeMsg;
72         if (!UtilNapiError::GetApiError(asyncContext->errorCode, codeMsg)) {
73             MMI_HILOGE("ErrorCode not found, errCode:%{public}d", asyncContext->errorCode);
74             return false;
75         }
76         napi_value errCode = nullptr;
77         napi_value errMsg = nullptr;
78         napi_value businessError = nullptr;
79         CHKRF(napi_create_int32(env, asyncContext->errorCode, &errCode), CREATE_INT32);
80         CHKRF(napi_create_string_utf8(env, codeMsg.msg.c_str(),
81             NAPI_AUTO_LENGTH, &errMsg), CREATE_STRING_UTF8);
82         CHKRF(napi_create_error(env, nullptr, errMsg, &businessError), CREATE_ERROR);
83         CHKRF(napi_set_named_property(env, businessError, ERR_CODE.c_str(), errCode), SET_NAMED_PROPERTY);
84         results[0] = businessError;
85     } else {
86         CHKRF(napi_get_undefined(env, &results[0]), GET_UNDEFINED);
87     }
88 
89     ReturnType resultType;
90     asyncContext->reserve >> resultType;
91     if (resultType == ReturnType::BOOL) {
92         bool temp;
93         asyncContext->reserve >> temp;
94         CHKRF(napi_get_boolean(env, temp, &results[1]), GET_BOOLEAN);
95     } else if (resultType == ReturnType::NUMBER) {
96         int32_t temp = 0;
97         asyncContext->reserve >> temp;
98         CHKRF(napi_create_int32(env, temp, &results[1]), CREATE_INT32);
99     } else {
100         CHKRF(napi_get_undefined(env, &results[1]), GET_UNDEFINED);
101     }
102     return true;
103 }
104 
AsyncCallbackWork(sptr<AsyncContext> asyncContext)105 void AsyncCallbackWork(sptr<AsyncContext> asyncContext)
106 {
107     CALL_DEBUG_ENTER;
108     CHKPV(asyncContext);
109     CHKPV(asyncContext->env);
110     napi_env env = asyncContext->env;
111     napi_value resource = nullptr;
112     CHKRV(napi_create_string_utf8(env, "AsyncCallbackWork", NAPI_AUTO_LENGTH, &resource), CREATE_STRING_UTF8);
113     asyncContext->IncStrongRef(nullptr);
114     napi_status status = napi_create_async_work(
115         env, nullptr, resource,
116         [](napi_env env, void* data) {
117             MMI_HILOGD("async_work callback function is called");
118         },
119         [](napi_env env, napi_status status, void* data) {
120             sptr<AsyncContext> asyncContext(static_cast<AsyncContext *>(data));
121             /**
122              * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced
123              * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the
124              * asynchronous task is created, and subtract 1 from the reference count after the naked
125              * pointer is converted to a pointer when the asynchronous task is executed, the reference
126              * count of the smart pointer is guaranteed to be 1.
127              */
128             asyncContext->DecStrongRef(nullptr);
129             napi_value results[2] = { 0 };
130             int32_t size = 2;
131             if (!GetResult(asyncContext, results, size)) {
132                 MMI_HILOGE("Failed to create napi data");
133                 return;
134             }
135             if (asyncContext->deferred) {
136                 if (asyncContext->errorCode == RET_OK) {
137                     CHKRV(napi_resolve_deferred(env, asyncContext->deferred, results[1]), RESOLVE_DEFERRED);
138                 } else {
139                     CHKRV(napi_reject_deferred(env, asyncContext->deferred, results[0]), REJECT_DEFERRED);
140                 }
141             } else {
142                 napi_value callback = nullptr;
143                 CHKRV(napi_get_reference_value(env, asyncContext->callback, &callback), GET_REFERENCE_VALUE);
144                 napi_value callResult = nullptr;
145                 CHKRV(napi_call_function(env, nullptr, callback, size, results, &callResult), CALL_FUNCTION);
146             }
147         },
148         asyncContext.GetRefPtr(), &asyncContext->work);
149     if (status != napi_ok ||
150         napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) {
151         MMI_HILOGE("Create async work failed");
152         asyncContext->DecStrongRef(nullptr);
153     }
154 }
155 
SetPointerVisible(napi_env env,bool visible,napi_value handle)156 napi_value JsPointerManager::SetPointerVisible(napi_env env, bool visible, napi_value handle)
157 {
158     CALL_DEBUG_ENTER;
159     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
160     CHKPP(asyncContext);
161 
162     asyncContext->errorCode = InputManager::GetInstance()->SetPointerVisible(visible);
163     asyncContext->reserve << ReturnType::VOID;
164 
165     napi_value promise = nullptr;
166     if (handle != nullptr) {
167         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
168         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
169     } else {
170         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
171     }
172     AsyncCallbackWork(asyncContext);
173     return promise;
174 }
175 
SetPointerVisibleSync(napi_env env,bool visible)176 napi_value JsPointerManager::SetPointerVisibleSync(napi_env env, bool visible)
177 {
178     CALL_DEBUG_ENTER;
179     InputManager::GetInstance()->SetPointerVisible(visible);
180     napi_value result = nullptr;
181     if (napi_get_undefined(env, &result) != napi_ok) {
182         MMI_HILOGE("Get undefined result is failed");
183         return nullptr;
184     }
185     return result;
186 }
187 
IsPointerVisible(napi_env env,napi_value handle)188 napi_value JsPointerManager::IsPointerVisible(napi_env env, napi_value handle)
189 {
190     CALL_DEBUG_ENTER;
191     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
192     CHKPP(asyncContext);
193 
194     bool visible = InputManager::GetInstance()->IsPointerVisible();
195     asyncContext->errorCode = ERR_OK;
196     asyncContext->reserve << ReturnType::BOOL << visible;
197 
198     napi_value promise = nullptr;
199     if (handle != nullptr) {
200         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
201         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
202     } else {
203         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
204     }
205     AsyncCallbackWork(asyncContext);
206     return promise;
207 }
208 
IsPointerVisibleSync(napi_env env)209 napi_value JsPointerManager::IsPointerVisibleSync(napi_env env)
210 {
211     CALL_DEBUG_ENTER;
212     bool visible = InputManager::GetInstance()->IsPointerVisible();
213     napi_value result = nullptr;
214     NAPI_CALL(env, napi_get_boolean(env, visible, &result));
215     return result;
216 }
217 
SetPointerColor(napi_env env,int32_t color,napi_value handle)218 napi_value JsPointerManager::SetPointerColor(napi_env env, int32_t color, napi_value handle)
219 {
220     CALL_DEBUG_ENTER;
221     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
222     CHKPP(asyncContext);
223     asyncContext->errorCode = InputManager::GetInstance()->SetPointerColor(color);
224     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
225         MMI_HILOGE("Non system applications use system API");
226         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
227         return nullptr;
228     }
229     asyncContext->reserve << ReturnType::VOID;
230     napi_value promise = nullptr;
231     if (handle != nullptr) {
232         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
233         if (napi_get_undefined(env, &promise) != napi_ok) {
234             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
235             return nullptr;
236         }
237     } else {
238         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
239     }
240     AsyncCallbackWork(asyncContext);
241     return promise;
242 }
243 
GetPointerColor(napi_env env,napi_value handle)244 napi_value JsPointerManager::GetPointerColor(napi_env env, napi_value handle)
245 {
246     CALL_DEBUG_ENTER;
247     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
248     CHKPP(asyncContext);
249     int32_t color = 1;
250     asyncContext->errorCode = InputManager::GetInstance()->GetPointerColor(color);
251     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
252         MMI_HILOGE("Non system applications use system API");
253         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
254         return nullptr;
255     }
256     asyncContext->reserve << ReturnType::NUMBER << color;
257     napi_value promise = nullptr;
258     uint32_t initialRefCount = 1;
259     if (handle != nullptr) {
260         CHKRP(napi_create_reference(env, handle, initialRefCount, &asyncContext->callback), CREATE_REFERENCE);
261         if (napi_get_undefined(env, &promise) != napi_ok) {
262             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
263             return nullptr;
264         }
265     } else {
266         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
267     }
268     AsyncCallbackWork(asyncContext);
269     return promise;
270 }
271 
SetPointerColorSync(napi_env env,int32_t color)272 napi_value JsPointerManager::SetPointerColorSync(napi_env env, int32_t color)
273 {
274     CALL_DEBUG_ENTER;
275     auto errorCode = InputManager::GetInstance()->SetPointerColor(color);
276     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
277         MMI_HILOGE("Non system applications use system API");
278         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
279         return nullptr;
280     }
281 
282     napi_value result = nullptr;
283     if (napi_get_undefined(env, &result) != napi_ok) {
284         MMI_HILOGE("Get undefined result is failed");
285         return nullptr;
286     }
287     return result;
288 }
289 
GetPointerColorSync(napi_env env)290 napi_value JsPointerManager::GetPointerColorSync(napi_env env)
291 {
292     CALL_DEBUG_ENTER;
293     int32_t color = 1;
294     auto errorCode = InputManager::GetInstance()->GetPointerColor(color);
295     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
296         MMI_HILOGE("Non system applications use system API");
297         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
298         return nullptr;
299     }
300     napi_value result = nullptr;
301     NAPI_CALL(env, napi_create_int32(env, color, &result));
302     return result;
303 }
304 
SetPointerSpeed(napi_env env,int32_t pointerSpeed,napi_value handle)305 napi_value JsPointerManager::SetPointerSpeed(napi_env env, int32_t pointerSpeed, napi_value handle)
306 {
307     CALL_DEBUG_ENTER;
308     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
309     CHKPP(asyncContext);
310     asyncContext->errorCode = InputManager::GetInstance()->SetPointerSpeed(pointerSpeed);
311     asyncContext->reserve << ReturnType::VOID;
312     napi_value promise = nullptr;
313     if (handle != nullptr) {
314         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
315         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
316     } else {
317         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
318     }
319     AsyncCallbackWork(asyncContext);
320     return promise;
321 }
322 
SetPointerSpeedSync(napi_env env,int32_t pointerSpeed)323 napi_value JsPointerManager::SetPointerSpeedSync(napi_env env, int32_t pointerSpeed)
324 {
325     CALL_DEBUG_ENTER;
326     InputManager::GetInstance()->SetPointerSpeed(pointerSpeed);
327     napi_value result = nullptr;
328     if (napi_get_undefined(env, &result) != napi_ok) {
329         MMI_HILOGE("Get undefined result is failed");
330         return nullptr;
331     }
332     return result;
333 }
334 
GetPointerSpeed(napi_env env,napi_value handle)335 napi_value JsPointerManager::GetPointerSpeed(napi_env env, napi_value handle)
336 {
337     CALL_DEBUG_ENTER;
338     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
339     CHKPP(asyncContext);
340     int32_t pointerSpeed = 0;
341     asyncContext->errorCode = InputManager::GetInstance()->GetPointerSpeed(pointerSpeed);
342     asyncContext->reserve << ReturnType::NUMBER << pointerSpeed;
343     napi_value promise = nullptr;
344     uint32_t initial_refcount = 1;
345     if (handle != nullptr) {
346         CHKRP(napi_create_reference(env, handle, initial_refcount, &asyncContext->callback), CREATE_REFERENCE);
347         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
348     } else {
349         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
350     }
351     AsyncCallbackWork(asyncContext);
352     return promise;
353 }
354 
GetPointerSpeedSync(napi_env env)355 napi_value JsPointerManager::GetPointerSpeedSync(napi_env env)
356 {
357     CALL_DEBUG_ENTER;
358     int32_t pointerSpeed = 0;
359     InputManager::GetInstance()->GetPointerSpeed(pointerSpeed);
360     napi_value result = nullptr;
361     NAPI_CALL(env, napi_create_int32(env, pointerSpeed, &result));
362     return result;
363 }
364 
SetMouseScrollRows(napi_env env,int32_t rows,napi_value handle)365 napi_value JsPointerManager::SetMouseScrollRows(napi_env env, int32_t rows, napi_value handle)
366 {
367     CALL_DEBUG_ENTER;
368     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
369     CHKPP(asyncContext);
370     asyncContext->errorCode = InputManager::GetInstance()->SetMouseScrollRows(rows);
371     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
372         MMI_HILOGE("Non system applications use system API");
373         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
374         return nullptr;
375     }
376     asyncContext->reserve << ReturnType::VOID;
377     napi_value promise = nullptr;
378     if (handle != nullptr) {
379         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
380         if (napi_get_undefined(env, &promise) != napi_ok) {
381             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
382             return nullptr;
383         }
384     } else {
385         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
386     }
387     AsyncCallbackWork(asyncContext);
388     return promise;
389 }
390 
GetMouseScrollRows(napi_env env,napi_value handle)391 napi_value JsPointerManager::GetMouseScrollRows(napi_env env, napi_value handle)
392 {
393     CALL_DEBUG_ENTER;
394     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
395     CHKPP(asyncContext);
396     int32_t rows = 3;
397     asyncContext->errorCode = InputManager::GetInstance()->GetMouseScrollRows(rows);
398     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
399         MMI_HILOGE("Non system applications use system API");
400         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
401         return nullptr;
402     }
403     asyncContext->reserve << ReturnType::NUMBER << rows;
404     napi_value promise = nullptr;
405     uint32_t initialRefCount = 1;
406     if (handle != nullptr) {
407         CHKRP(napi_create_reference(env, handle, initialRefCount, &asyncContext->callback), CREATE_REFERENCE);
408         if (napi_get_undefined(env, &promise) != napi_ok) {
409             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
410             return nullptr;
411         }
412     } else {
413         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
414     }
415     AsyncCallbackWork(asyncContext);
416     return promise;
417 }
418 
SetPointerLocation(napi_env env,int32_t x,int32_t y,napi_value handle)419 napi_value JsPointerManager::SetPointerLocation(napi_env env, int32_t x, int32_t y, napi_value handle)
420 {
421     CALL_DEBUG_ENTER;
422     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
423     CHKPP(asyncContext);
424     asyncContext->errorCode = InputManager::GetInstance()->SetPointerLocation(x, y);
425     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
426         MMI_HILOGE("Non system applications use system API");
427         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
428         return nullptr;
429     }
430     asyncContext->reserve << ReturnType::VOID;
431     napi_value promise = nullptr;
432     if (handle != nullptr) {
433         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
434         if (napi_get_undefined(env, &promise) != napi_ok) {
435             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
436             return nullptr;
437         }
438     } else {
439         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
440     }
441     AsyncCallbackWork(asyncContext);
442     return promise;
443 }
444 
SetCustomCursor(napi_env env,int32_t windowId,void * pixelMap,CursorFocus focus)445 napi_value JsPointerManager::SetCustomCursor(napi_env env, int32_t windowId, void* pixelMap, CursorFocus focus)
446 {
447     CALL_DEBUG_ENTER;
448     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
449     CHKPP(asyncContext);
450     asyncContext->errorCode = InputManager::GetInstance()->SetCustomCursor(windowId, pixelMap, focus.x, focus.y);
451     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
452         MMI_HILOGE("Non system applications use system API");
453         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
454         return nullptr;
455     }
456     asyncContext->reserve << ReturnType::VOID;
457     napi_value promise = nullptr;
458     CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
459     AsyncCallbackWork(asyncContext);
460     return promise;
461 }
462 
SetCustomCursorSync(napi_env env,int32_t windowId,void * pixelMap,CursorFocus focus)463 napi_value JsPointerManager::SetCustomCursorSync(napi_env env, int32_t windowId, void* pixelMap, CursorFocus focus)
464 {
465     CALL_DEBUG_ENTER;
466     InputManager::GetInstance()->SetCustomCursor(windowId, pixelMap, focus.x, focus.y);
467     napi_value result = nullptr;
468     if (napi_get_undefined(env, &result) != napi_ok) {
469         MMI_HILOGE("Get undefined result is failed");
470         return nullptr;
471     }
472     return result;
473 }
474 
SetPointerSize(napi_env env,int32_t size,napi_value handle)475 napi_value JsPointerManager::SetPointerSize(napi_env env, int32_t size, napi_value handle)
476 {
477     CALL_DEBUG_ENTER;
478     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
479     CHKPP(asyncContext);
480     asyncContext->errorCode = InputManager::GetInstance()->SetPointerSize(size);
481     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
482         MMI_HILOGE("Non system applications use system API");
483         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
484         return nullptr;
485     }
486     asyncContext->reserve << ReturnType::VOID;
487     napi_value promise = nullptr;
488     if (handle != nullptr) {
489         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
490         if (napi_get_undefined(env, &promise) != napi_ok) {
491             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
492             return nullptr;
493         }
494     } else {
495         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
496     }
497     AsyncCallbackWork(asyncContext);
498     return promise;
499 }
500 
GetPointerSize(napi_env env,napi_value handle)501 napi_value JsPointerManager::GetPointerSize(napi_env env, napi_value handle)
502 {
503     CALL_DEBUG_ENTER;
504     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
505     CHKPP(asyncContext);
506     int32_t size = 1;
507     asyncContext->errorCode = InputManager::GetInstance()->GetPointerSize(size);
508     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
509         MMI_HILOGE("Non system applications use system API");
510         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
511         return nullptr;
512     }
513     asyncContext->reserve << ReturnType::NUMBER << size;
514     napi_value promise = nullptr;
515     uint32_t initialRefCount = 1;
516     if (handle != nullptr) {
517         CHKRP(napi_create_reference(env, handle, initialRefCount, &asyncContext->callback), CREATE_REFERENCE);
518         if (napi_get_undefined(env, &promise) != napi_ok) {
519             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
520             return nullptr;
521         }
522     } else {
523         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
524     }
525     AsyncCallbackWork(asyncContext);
526     return promise;
527 }
528 
SetPointerSizeSync(napi_env env,int32_t size)529 napi_value JsPointerManager::SetPointerSizeSync(napi_env env, int32_t size)
530 {
531     CALL_DEBUG_ENTER;
532     auto errorCode = InputManager::GetInstance()->SetPointerSize(size);
533     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
534         MMI_HILOGE("Non system applications use system API");
535         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
536         return nullptr;
537     }
538 
539     napi_value result = nullptr;
540     if (napi_get_undefined(env, &result) != napi_ok) {
541         MMI_HILOGE("Get undefined result is failed");
542         return nullptr;
543     }
544     return result;
545 }
546 
GetPointerSizeSync(napi_env env)547 napi_value JsPointerManager::GetPointerSizeSync(napi_env env)
548 {
549     CALL_DEBUG_ENTER;
550     int32_t size = 1;
551     auto errorCode = InputManager::GetInstance()->GetPointerSize(size);
552     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
553         MMI_HILOGE("Non system applications use system API");
554         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
555         return nullptr;
556     }
557     napi_value result = nullptr;
558     NAPI_CALL(env, napi_create_int32(env, size, &result));
559     return result;
560 }
561 
SetPointerStyle(napi_env env,int32_t windowid,int32_t pointerStyle,napi_value handle)562 napi_value JsPointerManager::SetPointerStyle(napi_env env, int32_t windowid, int32_t pointerStyle, napi_value handle)
563 {
564     CALL_DEBUG_ENTER;
565     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
566     CHKPP(asyncContext);
567     PointerStyle style;
568     style.id = pointerStyle;
569     asyncContext->errorCode = InputManager::GetInstance()->SetPointerStyle(windowid, style);
570     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
571         MMI_HILOGE("The windowId is negative number and no system applications use system API");
572         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR,
573             "windowId is negative number and no system applications use system API");
574         return nullptr;
575     }
576     asyncContext->reserve << ReturnType::VOID;
577 
578     napi_value promise = nullptr;
579     if (handle != nullptr) {
580         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
581         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
582     } else {
583         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
584     }
585     AsyncCallbackWork(asyncContext);
586     return promise;
587 }
588 
SetPointerStyleSync(napi_env env,int32_t windowid,int32_t pointerStyle)589 napi_value JsPointerManager::SetPointerStyleSync(napi_env env, int32_t windowid, int32_t pointerStyle)
590 {
591     CALL_DEBUG_ENTER;
592     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
593     CHKPP(asyncContext);
594     PointerStyle style;
595     style.id = pointerStyle;
596     asyncContext->errorCode = InputManager::GetInstance()->SetPointerStyle(windowid, style);
597     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
598         MMI_HILOGE("The WindowId is negative number and no system applications use system API");
599         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR,
600             "WindowId is negative number and no system applications use system API");
601         return nullptr;
602     }
603     napi_value result = nullptr;
604     if (napi_get_undefined(env, &result) != napi_ok) {
605         MMI_HILOGE("Get undefined result is failed");
606         return nullptr;
607     }
608     return result;
609 }
610 
GetPointerStyle(napi_env env,int32_t windowid,napi_value handle)611 napi_value JsPointerManager::GetPointerStyle(napi_env env, int32_t windowid, napi_value handle)
612 {
613     CALL_DEBUG_ENTER;
614     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
615     CHKPP(asyncContext);
616     PointerStyle pointerStyle;
617     asyncContext->errorCode = InputManager::GetInstance()->GetPointerStyle(windowid, pointerStyle);
618     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
619         MMI_HILOGE("WindowId is negative number and no system applications use system API");
620         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR,
621             "WindowId is negative number and no system applications use system API");
622         return nullptr;
623     }
624     asyncContext->reserve << ReturnType::NUMBER << pointerStyle.id;
625     napi_value promise = nullptr;
626     if (handle != nullptr) {
627         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
628         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
629     } else {
630         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
631     }
632     AsyncCallbackWork(asyncContext);
633     return promise;
634 }
635 
GetPointerStyleSync(napi_env env,int32_t windowid)636 napi_value JsPointerManager::GetPointerStyleSync(napi_env env, int32_t windowid)
637 {
638     CALL_DEBUG_ENTER;
639     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
640     CHKPP(asyncContext);
641     PointerStyle pointerStyle;
642     asyncContext->errorCode = InputManager::GetInstance()->GetPointerStyle(windowid, pointerStyle);
643     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
644         MMI_HILOGE("WindowId is negative number and no system applications use system API");
645         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR,
646             "WindowId is negative number and no system applications use system API");
647         return nullptr;
648     }
649     napi_value result = nullptr;
650     NAPI_CALL(env, napi_create_int32(env, pointerStyle.id, &result));
651     return result;
652 }
653 
EnterCaptureMode(napi_env env,int32_t windowId,napi_value handle)654 napi_value JsPointerManager::EnterCaptureMode(napi_env env, int32_t windowId, napi_value handle)
655 {
656     CALL_DEBUG_ENTER;
657     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
658     if (asyncContext == nullptr) {
659         THROWERR(env, "Create AsyncContext failed");
660         return nullptr;
661     }
662     asyncContext->errorCode = InputManager::GetInstance()->EnterCaptureMode(windowId);
663     asyncContext->reserve << ReturnType::VOID;
664 
665     napi_value promise = nullptr;
666     if (handle != nullptr) {
667         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
668         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
669     } else {
670         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
671     }
672     AsyncCallbackWork(asyncContext);
673     return promise;
674 }
675 
LeaveCaptureMode(napi_env env,int32_t windowId,napi_value handle)676 napi_value JsPointerManager::LeaveCaptureMode(napi_env env, int32_t windowId, napi_value handle)
677 {
678     CALL_DEBUG_ENTER;
679     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
680     if (asyncContext == nullptr) {
681         THROWERR(env, "Create AsyncContext failed");
682         return nullptr;
683     }
684 
685     asyncContext->errorCode = InputManager::GetInstance()->LeaveCaptureMode(windowId);
686     asyncContext->reserve << ReturnType::VOID;
687 
688     napi_value promise = nullptr;
689     if (handle != nullptr) {
690         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
691         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
692     } else {
693         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
694     }
695     AsyncCallbackWork(asyncContext);
696     return promise;
697 }
698 
SetMousePrimaryButton(napi_env env,int32_t primaryButton,napi_value handle)699 napi_value JsPointerManager::SetMousePrimaryButton(napi_env env, int32_t primaryButton, napi_value handle)
700 {
701     CALL_DEBUG_ENTER;
702     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
703     CHKPP(asyncContext);
704 
705     asyncContext->errorCode = InputManager::GetInstance()->SetMousePrimaryButton(primaryButton);
706     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
707         MMI_HILOGE("Non system applications use system API");
708         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
709         return nullptr;
710     }
711     asyncContext->reserve << ReturnType::VOID;
712 
713     napi_value promise = nullptr;
714     if (handle != nullptr) {
715         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
716         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
717     } else {
718         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
719     }
720     AsyncCallbackWork(asyncContext);
721     return promise;
722 }
723 
GetMousePrimaryButton(napi_env env,napi_value handle)724 napi_value JsPointerManager::GetMousePrimaryButton(napi_env env, napi_value handle)
725 {
726     CALL_DEBUG_ENTER;
727     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
728     CHKPP(asyncContext);
729     int32_t primaryButton = 0;
730     asyncContext->errorCode = InputManager::GetInstance()->GetMousePrimaryButton(primaryButton);
731     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
732         MMI_HILOGE("Non system applications use system API");
733         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
734         return nullptr;
735     }
736     asyncContext->reserve << ReturnType::NUMBER << primaryButton;
737     napi_value promise = nullptr;
738     if (handle != nullptr) {
739         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
740         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
741     } else {
742         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
743     }
744     AsyncCallbackWork(asyncContext);
745     return promise;
746 }
747 
SetHoverScrollState(napi_env env,bool state,napi_value handle)748 napi_value JsPointerManager::SetHoverScrollState(napi_env env, bool state, napi_value handle)
749 {
750     CALL_DEBUG_ENTER;
751     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
752     CHKPP(asyncContext);
753 
754     asyncContext->errorCode = InputManager::GetInstance()->SetHoverScrollState(state);
755     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
756         MMI_HILOGE("Non system applications use system API");
757         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
758         return nullptr;
759     }
760     asyncContext->reserve << ReturnType::VOID;
761 
762     napi_value promise = nullptr;
763     if (handle != nullptr) {
764         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
765         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
766     } else {
767         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
768     }
769     AsyncCallbackWork(asyncContext);
770     return promise;
771 }
772 
GetHoverScrollState(napi_env env,napi_value handle)773 napi_value JsPointerManager::GetHoverScrollState(napi_env env, napi_value handle)
774 {
775     CALL_DEBUG_ENTER;
776     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
777     CHKPP(asyncContext);
778 
779     bool state;
780     asyncContext->errorCode = InputManager::GetInstance()->GetHoverScrollState(state);
781     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
782         MMI_HILOGE("Non system applications use system API");
783         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
784         return nullptr;
785     }
786     asyncContext->reserve << ReturnType::BOOL << state;
787 
788     napi_value promise = nullptr;
789     if (handle != nullptr) {
790         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
791         CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
792     } else {
793         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
794     }
795     AsyncCallbackWork(asyncContext);
796     return promise;
797 }
798 
SetTouchpadData(napi_env env,napi_value handle,int32_t errorCode)799 napi_value JsPointerManager::SetTouchpadData(napi_env env, napi_value handle, int32_t errorCode)
800 {
801     CALL_DEBUG_ENTER;
802     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
803         MMI_HILOGE("Non system applications use system API");
804         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
805         return nullptr;
806     }
807 
808     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
809     CHKPP(asyncContext);
810 
811     asyncContext->errorCode = errorCode;
812     asyncContext->reserve << ReturnType::VOID;
813 
814     napi_value promise = nullptr;
815     if (handle != nullptr) {
816         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
817         if (napi_get_undefined(env, &promise) != napi_ok) {
818             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
819             return nullptr;
820         }
821     } else {
822         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
823     }
824     AsyncCallbackWork(asyncContext);
825     return promise;
826 }
827 
GetTouchpadBoolData(napi_env env,napi_value handle,bool data,int32_t errorCode)828 napi_value JsPointerManager::GetTouchpadBoolData(napi_env env, napi_value handle, bool data, int32_t errorCode)
829 {
830     CALL_DEBUG_ENTER;
831     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
832         MMI_HILOGE("Non system applications use system API");
833         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
834         return nullptr;
835     }
836 
837     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
838     CHKPP(asyncContext);
839 
840     asyncContext->errorCode = errorCode;
841     asyncContext->reserve << ReturnType::BOOL << data;
842 
843     napi_value promise = nullptr;
844     if (handle != nullptr) {
845         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
846         if (napi_get_undefined(env, &promise) != napi_ok) {
847             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
848             return nullptr;
849         }
850     } else {
851         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
852     }
853     AsyncCallbackWork(asyncContext);
854     return promise;
855 }
856 
GetTouchpadInt32Data(napi_env env,napi_value handle,int32_t data,int32_t errorCode)857 napi_value JsPointerManager::GetTouchpadInt32Data(napi_env env, napi_value handle, int32_t data, int32_t errorCode)
858 {
859     CALL_DEBUG_ENTER;
860     if (errorCode == COMMON_USE_SYSAPI_ERROR) {
861         MMI_HILOGE("Non system applications use system API");
862         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
863         return nullptr;
864     }
865 
866     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
867     CHKPP(asyncContext);
868 
869     asyncContext->errorCode = errorCode;
870     asyncContext->reserve << ReturnType::NUMBER << data;
871 
872     napi_value promise = nullptr;
873     if (handle != nullptr) {
874         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
875         if (napi_get_undefined(env, &promise) != napi_ok) {
876             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
877             return nullptr;
878         }
879     } else {
880         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
881     }
882     AsyncCallbackWork(asyncContext);
883     return promise;
884 }
885 
SetTouchpadScrollSwitch(napi_env env,bool switchFlag,napi_value handle)886 napi_value JsPointerManager::SetTouchpadScrollSwitch(napi_env env, bool switchFlag, napi_value handle)
887 {
888     CALL_DEBUG_ENTER;
889     int32_t ret = InputManager::GetInstance()->SetTouchpadScrollSwitch(switchFlag);
890     return SetTouchpadData(env, handle, ret);
891 }
892 
GetTouchpadScrollSwitch(napi_env env,napi_value handle)893 napi_value JsPointerManager::GetTouchpadScrollSwitch(napi_env env, napi_value handle)
894 {
895     CALL_DEBUG_ENTER;
896     bool switchFlag = true;
897     int32_t ret = InputManager::GetInstance()->GetTouchpadScrollSwitch(switchFlag);
898     return GetTouchpadBoolData(env, handle, switchFlag, ret);
899 }
900 
SetTouchpadScrollDirection(napi_env env,bool state,napi_value handle)901 napi_value JsPointerManager::SetTouchpadScrollDirection(napi_env env, bool state, napi_value handle)
902 {
903     CALL_DEBUG_ENTER;
904     int32_t ret = InputManager::GetInstance()->SetTouchpadScrollDirection(state);
905     return SetTouchpadData(env, handle, ret);
906 }
907 
GetTouchpadScrollDirection(napi_env env,napi_value handle)908 napi_value JsPointerManager::GetTouchpadScrollDirection(napi_env env, napi_value handle)
909 {
910     CALL_DEBUG_ENTER;
911     bool state = true;
912     int32_t ret = InputManager::GetInstance()->GetTouchpadScrollDirection(state);
913     return GetTouchpadBoolData(env, handle, state, ret);
914 }
915 
SetTouchpadTapSwitch(napi_env env,bool switchFlag,napi_value handle)916 napi_value JsPointerManager::SetTouchpadTapSwitch(napi_env env, bool switchFlag, napi_value handle)
917 {
918     CALL_DEBUG_ENTER;
919     int32_t ret = InputManager::GetInstance()->SetTouchpadTapSwitch(switchFlag);
920     return SetTouchpadData(env, handle, ret);
921 }
922 
GetTouchpadTapSwitch(napi_env env,napi_value handle)923 napi_value JsPointerManager::GetTouchpadTapSwitch(napi_env env, napi_value handle)
924 {
925     CALL_DEBUG_ENTER;
926     bool switchFlag = true;
927     int32_t ret = InputManager::GetInstance()->GetTouchpadTapSwitch(switchFlag);
928     return GetTouchpadBoolData(env, handle, switchFlag, ret);
929 }
SetTouchpadPointerSpeed(napi_env env,int32_t speed,napi_value handle)930 napi_value JsPointerManager::SetTouchpadPointerSpeed(napi_env env, int32_t speed, napi_value handle)
931 {
932     CALL_DEBUG_ENTER;
933     int32_t ret = InputManager::GetInstance()->SetTouchpadPointerSpeed(speed);
934     return SetTouchpadData(env, handle, ret);
935 }
936 
GetTouchpadPointerSpeed(napi_env env,napi_value handle)937 napi_value JsPointerManager::GetTouchpadPointerSpeed(napi_env env, napi_value handle)
938 {
939     CALL_DEBUG_ENTER;
940     int32_t speed = 0;
941     int32_t ret = InputManager::GetInstance()->GetTouchpadPointerSpeed(speed);
942     return GetTouchpadInt32Data(env, handle, speed, ret);
943 }
944 
SetTouchpadPinchSwitch(napi_env env,bool switchFlag,napi_value handle)945 napi_value JsPointerManager::SetTouchpadPinchSwitch(napi_env env, bool switchFlag, napi_value handle)
946 {
947     CALL_DEBUG_ENTER;
948     int32_t ret = InputManager::GetInstance()->SetTouchpadPinchSwitch(switchFlag);
949     return SetTouchpadData(env, handle, ret);
950 }
951 
GetTouchpadPinchSwitch(napi_env env,napi_value handle)952 napi_value JsPointerManager::GetTouchpadPinchSwitch(napi_env env, napi_value handle)
953 {
954     CALL_DEBUG_ENTER;
955     bool switchFlag = true;
956     int32_t ret = InputManager::GetInstance()->GetTouchpadPinchSwitch(switchFlag);
957     return GetTouchpadBoolData(env, handle, switchFlag, ret);
958 }
959 
SetTouchpadSwipeSwitch(napi_env env,bool switchFlag,napi_value handle)960 napi_value JsPointerManager::SetTouchpadSwipeSwitch(napi_env env, bool switchFlag, napi_value handle)
961 {
962     CALL_DEBUG_ENTER;
963     int32_t ret = InputManager::GetInstance()->SetTouchpadSwipeSwitch(switchFlag);
964     return SetTouchpadData(env, handle, ret);
965 }
966 
GetTouchpadSwipeSwitch(napi_env env,napi_value handle)967 napi_value JsPointerManager::GetTouchpadSwipeSwitch(napi_env env, napi_value handle)
968 {
969     CALL_DEBUG_ENTER;
970     bool switchFlag = true;
971     int32_t ret = InputManager::GetInstance()->GetTouchpadSwipeSwitch(switchFlag);
972     return GetTouchpadBoolData(env, handle, switchFlag, ret);
973 }
974 
SetTouchpadRightClickType(napi_env env,int32_t type,napi_value handle)975 napi_value JsPointerManager::SetTouchpadRightClickType(napi_env env, int32_t type, napi_value handle)
976 {
977     CALL_DEBUG_ENTER;
978     int32_t ret = InputManager::GetInstance()->SetTouchpadRightClickType(type);
979     return SetTouchpadData(env, handle, ret);
980 }
981 
GetTouchpadRightClickType(napi_env env,napi_value handle)982 napi_value JsPointerManager::GetTouchpadRightClickType(napi_env env, napi_value handle)
983 {
984     CALL_DEBUG_ENTER;
985     int32_t type = 1;
986     int32_t ret = InputManager::GetInstance()->GetTouchpadRightClickType(type);
987     return GetTouchpadInt32Data(env, handle, type, ret);
988 }
989 
SetTouchpadRotateSwitch(napi_env env,bool rotateSwitch,napi_value handle)990 napi_value JsPointerManager::SetTouchpadRotateSwitch(napi_env env, bool rotateSwitch, napi_value handle)
991 {
992     CALL_DEBUG_ENTER;
993     int32_t ret = InputManager::GetInstance()->SetTouchpadRotateSwitch(rotateSwitch);
994     return SetTouchpadData(env, handle, ret);
995 }
996 
GetTouchpadRotateSwitch(napi_env env,napi_value handle)997 napi_value JsPointerManager::GetTouchpadRotateSwitch(napi_env env, napi_value handle)
998 {
999     CALL_DEBUG_ENTER;
1000     bool rotateSwitch = true;
1001     int32_t ret = InputManager::GetInstance()->GetTouchpadRotateSwitch(rotateSwitch);
1002     return GetTouchpadBoolData(env, handle, rotateSwitch, ret);
1003 }
1004 
SetTouchpadDoubleTapAndDragState(napi_env env,bool switchFlag,napi_value handle)1005 napi_value JsPointerManager::SetTouchpadDoubleTapAndDragState(napi_env env, bool switchFlag, napi_value handle)
1006 {
1007     CALL_DEBUG_ENTER;
1008     int32_t ret = InputManager::GetInstance()->SetTouchpadDoubleTapAndDragState(switchFlag);
1009     return SetTouchpadData(env, handle, ret);
1010 }
1011 
GetTouchpadDoubleTapAndDragState(napi_env env,napi_value handle)1012 napi_value JsPointerManager::GetTouchpadDoubleTapAndDragState(napi_env env, napi_value handle)
1013 {
1014     CALL_DEBUG_ENTER;
1015     bool switchFlag = true;
1016     int32_t ret = InputManager::GetInstance()->GetTouchpadDoubleTapAndDragState(switchFlag);
1017     return GetTouchpadBoolData(env, handle, switchFlag, ret);
1018 }
1019 
SetMoveEventFilters(napi_env env,bool flag)1020 napi_value JsPointerManager::SetMoveEventFilters(napi_env env, bool flag)
1021 {
1022     CALL_DEBUG_ENTER;
1023     int32_t ret = InputManager::GetInstance()->SetMoveEventFilters(flag);
1024     napi_value result = nullptr;
1025     CHKRP(napi_create_int32(env, ret, &result), CREATE_INT32);
1026     return result;
1027 }
1028 
SetTouchpadThreeFingersTapSwitch(napi_env env,bool switchFlag,napi_value handle)1029 napi_value JsPointerManager::SetTouchpadThreeFingersTapSwitch(napi_env env, bool switchFlag, napi_value handle)
1030 {
1031     CALL_DEBUG_ENTER;
1032     int32_t ret = InputManager::GetInstance()->SetTouchpadThreeFingersTapSwitch(switchFlag);
1033     return SetTouchpadData(env, handle, ret);
1034 }
1035 
GetTouchpadThreeFingersTapSwitch(napi_env env,napi_value handle)1036 napi_value JsPointerManager::GetTouchpadThreeFingersTapSwitch(napi_env env, napi_value handle)
1037 {
1038     CALL_DEBUG_ENTER;
1039     bool switchFlag = true;
1040     int32_t ret = InputManager::GetInstance()->GetTouchpadThreeFingersTapSwitch(switchFlag);
1041     return GetTouchpadBoolData(env, handle, switchFlag, ret);
1042 }
1043 
EnableHardwareCursorStats(napi_env env,bool enable)1044 napi_value JsPointerManager::EnableHardwareCursorStats(napi_env env, bool enable)
1045 {
1046     CALL_DEBUG_ENTER;
1047     InputManager::GetInstance()->EnableHardwareCursorStats(enable);
1048     napi_value result = nullptr;
1049     if (napi_get_undefined(env, &result) != napi_ok) {
1050         MMI_HILOGE("Get undefined result is failed");
1051         return nullptr;
1052     }
1053     return result;
1054 }
1055 
GetHardwareCursorStats(napi_env env)1056 napi_value JsPointerManager::GetHardwareCursorStats(napi_env env)
1057 {
1058     CALL_DEBUG_ENTER;
1059     uint32_t frameCount = 0;
1060     uint32_t vsyncCount = 0;
1061     InputManager::GetInstance()->GetHardwareCursorStats(frameCount, vsyncCount);
1062     napi_value result = nullptr;
1063     napi_status status = napi_create_object(env, &result);
1064     if (status != napi_ok) {
1065         MMI_HILOGE("Napi create object is failed");
1066         return nullptr;
1067     }
1068     MMI_HILOGD("GetHardwareCursorStats, frameCount:%{public}d, vsyncCount:%{public}d",
1069         frameCount, vsyncCount);
1070     napi_value frameNapiCount;
1071     CHKRP(napi_create_uint32(env, frameCount, &frameNapiCount), CREATE_UINT32);
1072     napi_value vsyncNapiCount;
1073     CHKRP(napi_create_uint32(env, vsyncCount, &vsyncNapiCount), CREATE_UINT32);
1074     status = napi_set_named_property(env, result, "frameCount", frameNapiCount);
1075     if (status != napi_ok) {
1076         MMI_HILOGE("Napi set frameCount named property is failed");
1077         return nullptr;
1078     }
1079     status = napi_set_named_property(env, result, "vsyncCount", vsyncNapiCount);
1080     if (status != napi_ok) {
1081         MMI_HILOGE("Napi set vsyncCount named property is failed");
1082         return nullptr;
1083     }
1084     return result;
1085 }
1086 
SetTouchpadScrollRows(napi_env env,int32_t rows,napi_value handle)1087 napi_value JsPointerManager::SetTouchpadScrollRows(napi_env env, int32_t rows, napi_value handle)
1088 {
1089     CALL_DEBUG_ENTER;
1090     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
1091     CHKPP(asyncContext);
1092     asyncContext->errorCode = InputManager::GetInstance()->SetTouchpadScrollRows(rows);
1093     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
1094         MMI_HILOGE("Non system applications use system API");
1095         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
1096         return nullptr;
1097     }
1098     asyncContext->reserve << ReturnType::VOID;
1099     napi_value promise = nullptr;
1100     if (handle != nullptr) {
1101         CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
1102         if (napi_get_undefined(env, &promise) != napi_ok) {
1103             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
1104             return nullptr;
1105         }
1106     } else {
1107         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
1108     }
1109     AsyncCallbackWork(asyncContext);
1110     return promise;
1111 }
1112 
GetTouchpadScrollRows(napi_env env,napi_value handle)1113 napi_value JsPointerManager::GetTouchpadScrollRows(napi_env env, napi_value handle)
1114 {
1115     CALL_DEBUG_ENTER;
1116     sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
1117     CHKPP(asyncContext);
1118     int32_t rows = TOUCHPAD_SCROLL_ROWS;
1119     asyncContext->errorCode = InputManager::GetInstance()->GetTouchpadScrollRows(rows);
1120     if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
1121         MMI_HILOGE("Non system applications use system API");
1122         THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
1123         return nullptr;
1124     }
1125     asyncContext->reserve << ReturnType::NUMBER << rows;
1126     napi_value promise = nullptr;
1127     uint32_t initialRefCount = 1;
1128     if (handle != nullptr) {
1129         CHKRP(napi_create_reference(env, handle, initialRefCount, &asyncContext->callback), CREATE_REFERENCE);
1130         if (napi_get_undefined(env, &promise) != napi_ok) {
1131             CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
1132             return nullptr;
1133         }
1134     } else {
1135         CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
1136     }
1137     AsyncCallbackWork(asyncContext);
1138     return promise;
1139 }
1140 
SetCustomCursor(napi_env env,int32_t windowId,CustomCursor cursor,CursorOptions options)1141 napi_value JsPointerManager::SetCustomCursor(napi_env env, int32_t windowId, CustomCursor cursor,
1142     CursorOptions options)
1143 {
1144     CALL_DEBUG_ENTER;
1145     sptr<CustomCursorAsyncContext> asyncContext = new (std::nothrow) CustomCursorAsyncContext(env);
1146     CHKPP(asyncContext);
1147     asyncContext->windowId = windowId;
1148     CHKPP(cursor.pixelMap);
1149     MessageParcel *pData = static_cast<MessageParcel*>(cursor.pixelMap);
1150     CHKPP(pData);
1151     Media::PixelMap* pixelMapPtr =  Media::PixelMap::Unmarshalling(*pData);
1152     asyncContext->cursor.pixelMap = (void*)pixelMapPtr;
1153     asyncContext->cursor.focusX = cursor.focusX;
1154     asyncContext->cursor.focusY = cursor.focusY;
1155     asyncContext->options = options;
1156     napi_value promise = nullptr;
1157     CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
1158     ExecuteSetCustomCursorAsync(asyncContext);
1159     return promise;
1160 }
1161 
ExecuteSetCustomCursorAsync(sptr<CustomCursorAsyncContext> asyncContext)1162 napi_value JsPointerManager::ExecuteSetCustomCursorAsync(sptr<CustomCursorAsyncContext> asyncContext)
1163 {
1164     CALL_DEBUG_ENTER;
1165     CHKPP(asyncContext);
1166     napi_env env = asyncContext->env;
1167     napi_value resource = nullptr;
1168     CHKRP(napi_create_string_utf8(env, "ExecuteSetCustomCursor", NAPI_AUTO_LENGTH, &resource), CREATE_STRING_UTF8);
1169     asyncContext->IncStrongRef(nullptr);
1170     if (!CreateAsyncWork(env, asyncContext, resource)) {
1171         MMI_HILOGE("Create async work failed");
1172         asyncContext->DecStrongRef(nullptr);
1173         return nullptr;
1174     }
1175 
1176     napi_value result;
1177     napi_get_undefined(env, &result);
1178     return result;
1179 }
1180 
CreateAsyncWork(napi_env env,sptr<CustomCursorAsyncContext> asyncContext,napi_value resource)1181 bool JsPointerManager::CreateAsyncWork(napi_env env, sptr<CustomCursorAsyncContext> asyncContext, napi_value resource)
1182 {
1183     napi_status status = napi_create_async_work(
1184         env,
1185         nullptr,
1186         resource,
1187         ExecuteSetCustomCursorWork,
1188         HandleSetCustomCursorCompletion,
1189         asyncContext.GetRefPtr(), &asyncContext->work);
1190     if (status != napi_ok ||
1191         napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) {
1192         return false;
1193     }
1194     return true;
1195 }
1196 
ExecuteSetCustomCursorWork(napi_env env,void * data)1197 void JsPointerManager::ExecuteSetCustomCursorWork(napi_env env, void* data)
1198 {
1199     CHKPV(data);
1200     sptr<CustomCursorAsyncContext> asyncContext(static_cast<CustomCursorAsyncContext*>(data));
1201     asyncContext->errorCode = InputManager::GetInstance()->SetCustomCursor(
1202         asyncContext->windowId,
1203         asyncContext->cursor,
1204         asyncContext->options);
1205 }
1206 
HandleSetCustomCursorCompletion(napi_env env,napi_status status,void * data)1207 void JsPointerManager::HandleSetCustomCursorCompletion(napi_env env, napi_status status, void* data)
1208 {
1209     napi_value results[2] = { 0 };
1210     int32_t size = 2;
1211 
1212     CHKPV(data);
1213     sptr<CustomCursorAsyncContext> asyncContext(static_cast<CustomCursorAsyncContext*>(data));
1214     CHKPV(asyncContext);
1215     napi_create_int32(env, asyncContext->errorCode, &asyncContext->resultValue);
1216 
1217     if (!GetResult(asyncContext, results, size)) {
1218         asyncContext->DecStrongRef(nullptr);
1219         return;
1220     }
1221     results[1] = asyncContext->resultValue;
1222     if (asyncContext->deferred) {
1223         if (asyncContext->errorCode == RET_OK) {
1224             CHKRV(napi_resolve_deferred(env, asyncContext->deferred, results[1]), RESOLVE_DEFERRED);
1225         } else {
1226             CHKRV(napi_reject_deferred(env, asyncContext->deferred, results[0]), REJECT_DEFERRED);
1227         }
1228     }
1229     asyncContext->DecStrongRef(nullptr);
1230 }
1231 } // namespace MMI
1232 } // namespace OHOS