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