1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "js_pointer_manager.h"
17
18 #include "napi_constants.h"
19 #include "pixel_map.h"
20
21 #undef MMI_LOG_TAG
22 #define MMI_LOG_TAG "JsPointerManager"
23
24 namespace OHOS {
25 namespace MMI {
26 namespace {
27 enum class ReturnType {
28 VOID,
29 BOOL,
30 NUMBER,
31 };
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 asyncContext->reserve << ReturnType::VOID;
570
571 napi_value promise = nullptr;
572 if (handle != nullptr) {
573 CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
574 CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
575 } else {
576 CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
577 }
578 AsyncCallbackWork(asyncContext);
579 return promise;
580 }
581
SetPointerStyleSync(napi_env env,int32_t windowid,int32_t pointerStyle)582 napi_value JsPointerManager::SetPointerStyleSync(napi_env env, int32_t windowid, int32_t pointerStyle)
583 {
584 CALL_DEBUG_ENTER;
585 PointerStyle style;
586 style.id = pointerStyle;
587 InputManager::GetInstance()->SetPointerStyle(windowid, style);
588 napi_value result = nullptr;
589 if (napi_get_undefined(env, &result) != napi_ok) {
590 MMI_HILOGE("Get undefined result is failed");
591 return nullptr;
592 }
593 return result;
594 }
595
GetPointerStyle(napi_env env,int32_t windowid,napi_value handle)596 napi_value JsPointerManager::GetPointerStyle(napi_env env, int32_t windowid, napi_value handle)
597 {
598 CALL_DEBUG_ENTER;
599 sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
600 CHKPP(asyncContext);
601 PointerStyle pointerStyle;
602 asyncContext->errorCode = InputManager::GetInstance()->GetPointerStyle(windowid, pointerStyle);
603 asyncContext->reserve << ReturnType::NUMBER << pointerStyle.id;
604 napi_value promise = nullptr;
605 if (handle != nullptr) {
606 CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
607 CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
608 } else {
609 CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
610 }
611 AsyncCallbackWork(asyncContext);
612 return promise;
613 }
614
GetPointerStyleSync(napi_env env,int32_t windowid)615 napi_value JsPointerManager::GetPointerStyleSync(napi_env env, int32_t windowid)
616 {
617 CALL_DEBUG_ENTER;
618 PointerStyle pointerStyle;
619 InputManager::GetInstance()->GetPointerStyle(windowid, pointerStyle);
620 napi_value result = nullptr;
621 NAPI_CALL(env, napi_create_int32(env, pointerStyle.id, &result));
622 return result;
623 }
624
EnterCaptureMode(napi_env env,int32_t windowId,napi_value handle)625 napi_value JsPointerManager::EnterCaptureMode(napi_env env, int32_t windowId, napi_value handle)
626 {
627 CALL_DEBUG_ENTER;
628 sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
629 if (asyncContext == nullptr) {
630 THROWERR(env, "Create AsyncContext failed");
631 return nullptr;
632 }
633 asyncContext->errorCode = InputManager::GetInstance()->EnterCaptureMode(windowId);
634 asyncContext->reserve << ReturnType::VOID;
635
636 napi_value promise = nullptr;
637 if (handle != nullptr) {
638 CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
639 CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
640 } else {
641 CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
642 }
643 AsyncCallbackWork(asyncContext);
644 return promise;
645 }
646
LeaveCaptureMode(napi_env env,int32_t windowId,napi_value handle)647 napi_value JsPointerManager::LeaveCaptureMode(napi_env env, int32_t windowId, napi_value handle)
648 {
649 CALL_DEBUG_ENTER;
650 sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
651 if (asyncContext == nullptr) {
652 THROWERR(env, "Create AsyncContext failed");
653 return nullptr;
654 }
655
656 asyncContext->errorCode = InputManager::GetInstance()->LeaveCaptureMode(windowId);
657 asyncContext->reserve << ReturnType::VOID;
658
659 napi_value promise = nullptr;
660 if (handle != nullptr) {
661 CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
662 CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
663 } else {
664 CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
665 }
666 AsyncCallbackWork(asyncContext);
667 return promise;
668 }
669
SetMousePrimaryButton(napi_env env,int32_t primaryButton,napi_value handle)670 napi_value JsPointerManager::SetMousePrimaryButton(napi_env env, int32_t primaryButton, napi_value handle)
671 {
672 CALL_DEBUG_ENTER;
673 sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
674 CHKPP(asyncContext);
675
676 asyncContext->errorCode = InputManager::GetInstance()->SetMousePrimaryButton(primaryButton);
677 if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
678 MMI_HILOGE("Non system applications use system API");
679 THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
680 return nullptr;
681 }
682 asyncContext->reserve << ReturnType::VOID;
683
684 napi_value promise = nullptr;
685 if (handle != nullptr) {
686 CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
687 CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
688 } else {
689 CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
690 }
691 AsyncCallbackWork(asyncContext);
692 return promise;
693 }
694
GetMousePrimaryButton(napi_env env,napi_value handle)695 napi_value JsPointerManager::GetMousePrimaryButton(napi_env env, napi_value handle)
696 {
697 CALL_DEBUG_ENTER;
698 sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
699 CHKPP(asyncContext);
700 int32_t primaryButton = 0;
701 asyncContext->errorCode = InputManager::GetInstance()->GetMousePrimaryButton(primaryButton);
702 if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
703 MMI_HILOGE("Non system applications use system API");
704 THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
705 return nullptr;
706 }
707 asyncContext->reserve << ReturnType::NUMBER << primaryButton;
708 napi_value promise = nullptr;
709 if (handle != nullptr) {
710 CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
711 CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
712 } else {
713 CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
714 }
715 AsyncCallbackWork(asyncContext);
716 return promise;
717 }
718
SetHoverScrollState(napi_env env,bool state,napi_value handle)719 napi_value JsPointerManager::SetHoverScrollState(napi_env env, bool state, napi_value handle)
720 {
721 CALL_DEBUG_ENTER;
722 sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
723 CHKPP(asyncContext);
724
725 asyncContext->errorCode = InputManager::GetInstance()->SetHoverScrollState(state);
726 if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
727 MMI_HILOGE("Non system applications use system API");
728 THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
729 return nullptr;
730 }
731 asyncContext->reserve << ReturnType::VOID;
732
733 napi_value promise = nullptr;
734 if (handle != nullptr) {
735 CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
736 CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
737 } else {
738 CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
739 }
740 AsyncCallbackWork(asyncContext);
741 return promise;
742 }
743
GetHoverScrollState(napi_env env,napi_value handle)744 napi_value JsPointerManager::GetHoverScrollState(napi_env env, napi_value handle)
745 {
746 CALL_DEBUG_ENTER;
747 sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
748 CHKPP(asyncContext);
749
750 bool state;
751 asyncContext->errorCode = InputManager::GetInstance()->GetHoverScrollState(state);
752 if (asyncContext->errorCode == COMMON_USE_SYSAPI_ERROR) {
753 MMI_HILOGE("Non system applications use system API");
754 THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
755 return nullptr;
756 }
757 asyncContext->reserve << ReturnType::BOOL << state;
758
759 napi_value promise = nullptr;
760 if (handle != nullptr) {
761 CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
762 CHKRP(napi_get_undefined(env, &promise), GET_UNDEFINED);
763 } else {
764 CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
765 }
766 AsyncCallbackWork(asyncContext);
767 return promise;
768 }
769
SetTouchpadData(napi_env env,napi_value handle,int32_t errorCode)770 napi_value JsPointerManager::SetTouchpadData(napi_env env, napi_value handle, int32_t errorCode)
771 {
772 CALL_DEBUG_ENTER;
773 if (errorCode == COMMON_USE_SYSAPI_ERROR) {
774 MMI_HILOGE("Non system applications use system API");
775 THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
776 return nullptr;
777 }
778
779 sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
780 CHKPP(asyncContext);
781
782 asyncContext->errorCode = errorCode;
783 asyncContext->reserve << ReturnType::VOID;
784
785 napi_value promise = nullptr;
786 if (handle != nullptr) {
787 CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
788 if (napi_get_undefined(env, &promise) != napi_ok) {
789 CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
790 return nullptr;
791 }
792 } else {
793 CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
794 }
795 AsyncCallbackWork(asyncContext);
796 return promise;
797 }
798
GetTouchpadBoolData(napi_env env,napi_value handle,bool data,int32_t errorCode)799 napi_value JsPointerManager::GetTouchpadBoolData(napi_env env, napi_value handle, bool data, 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::BOOL << data;
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
GetTouchpadInt32Data(napi_env env,napi_value handle,int32_t data,int32_t errorCode)828 napi_value JsPointerManager::GetTouchpadInt32Data(napi_env env, napi_value handle, int32_t 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::NUMBER << 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
SetTouchpadScrollSwitch(napi_env env,bool switchFlag,napi_value handle)857 napi_value JsPointerManager::SetTouchpadScrollSwitch(napi_env env, bool switchFlag, napi_value handle)
858 {
859 CALL_DEBUG_ENTER;
860 int32_t ret = InputManager::GetInstance()->SetTouchpadScrollSwitch(switchFlag);
861 return SetTouchpadData(env, handle, ret);
862 }
863
GetTouchpadScrollSwitch(napi_env env,napi_value handle)864 napi_value JsPointerManager::GetTouchpadScrollSwitch(napi_env env, napi_value handle)
865 {
866 CALL_DEBUG_ENTER;
867 bool switchFlag = true;
868 int32_t ret = InputManager::GetInstance()->GetTouchpadScrollSwitch(switchFlag);
869 return GetTouchpadBoolData(env, handle, switchFlag, ret);
870 }
871
SetTouchpadScrollDirection(napi_env env,bool state,napi_value handle)872 napi_value JsPointerManager::SetTouchpadScrollDirection(napi_env env, bool state, napi_value handle)
873 {
874 CALL_DEBUG_ENTER;
875 int32_t ret = InputManager::GetInstance()->SetTouchpadScrollDirection(state);
876 return SetTouchpadData(env, handle, ret);
877 }
878
GetTouchpadScrollDirection(napi_env env,napi_value handle)879 napi_value JsPointerManager::GetTouchpadScrollDirection(napi_env env, napi_value handle)
880 {
881 CALL_DEBUG_ENTER;
882 bool state = true;
883 int32_t ret = InputManager::GetInstance()->GetTouchpadScrollDirection(state);
884 return GetTouchpadBoolData(env, handle, state, ret);
885 }
886
SetTouchpadTapSwitch(napi_env env,bool switchFlag,napi_value handle)887 napi_value JsPointerManager::SetTouchpadTapSwitch(napi_env env, bool switchFlag, napi_value handle)
888 {
889 CALL_DEBUG_ENTER;
890 int32_t ret = InputManager::GetInstance()->SetTouchpadTapSwitch(switchFlag);
891 return SetTouchpadData(env, handle, ret);
892 }
893
GetTouchpadTapSwitch(napi_env env,napi_value handle)894 napi_value JsPointerManager::GetTouchpadTapSwitch(napi_env env, napi_value handle)
895 {
896 CALL_DEBUG_ENTER;
897 bool switchFlag = true;
898 int32_t ret = InputManager::GetInstance()->GetTouchpadTapSwitch(switchFlag);
899 return GetTouchpadBoolData(env, handle, switchFlag, ret);
900 }
SetTouchpadPointerSpeed(napi_env env,int32_t speed,napi_value handle)901 napi_value JsPointerManager::SetTouchpadPointerSpeed(napi_env env, int32_t speed, napi_value handle)
902 {
903 CALL_DEBUG_ENTER;
904 int32_t ret = InputManager::GetInstance()->SetTouchpadPointerSpeed(speed);
905 return SetTouchpadData(env, handle, ret);
906 }
907
GetTouchpadPointerSpeed(napi_env env,napi_value handle)908 napi_value JsPointerManager::GetTouchpadPointerSpeed(napi_env env, napi_value handle)
909 {
910 CALL_DEBUG_ENTER;
911 int32_t speed = 0;
912 int32_t ret = InputManager::GetInstance()->GetTouchpadPointerSpeed(speed);
913 return GetTouchpadInt32Data(env, handle, speed, ret);
914 }
915
SetTouchpadPinchSwitch(napi_env env,bool switchFlag,napi_value handle)916 napi_value JsPointerManager::SetTouchpadPinchSwitch(napi_env env, bool switchFlag, napi_value handle)
917 {
918 CALL_DEBUG_ENTER;
919 int32_t ret = InputManager::GetInstance()->SetTouchpadPinchSwitch(switchFlag);
920 return SetTouchpadData(env, handle, ret);
921 }
922
GetTouchpadPinchSwitch(napi_env env,napi_value handle)923 napi_value JsPointerManager::GetTouchpadPinchSwitch(napi_env env, napi_value handle)
924 {
925 CALL_DEBUG_ENTER;
926 bool switchFlag = true;
927 int32_t ret = InputManager::GetInstance()->GetTouchpadPinchSwitch(switchFlag);
928 return GetTouchpadBoolData(env, handle, switchFlag, ret);
929 }
930
SetTouchpadSwipeSwitch(napi_env env,bool switchFlag,napi_value handle)931 napi_value JsPointerManager::SetTouchpadSwipeSwitch(napi_env env, bool switchFlag, napi_value handle)
932 {
933 CALL_DEBUG_ENTER;
934 int32_t ret = InputManager::GetInstance()->SetTouchpadSwipeSwitch(switchFlag);
935 return SetTouchpadData(env, handle, ret);
936 }
937
GetTouchpadSwipeSwitch(napi_env env,napi_value handle)938 napi_value JsPointerManager::GetTouchpadSwipeSwitch(napi_env env, napi_value handle)
939 {
940 CALL_DEBUG_ENTER;
941 bool switchFlag = true;
942 int32_t ret = InputManager::GetInstance()->GetTouchpadSwipeSwitch(switchFlag);
943 return GetTouchpadBoolData(env, handle, switchFlag, ret);
944 }
945
SetTouchpadRightClickType(napi_env env,int32_t type,napi_value handle)946 napi_value JsPointerManager::SetTouchpadRightClickType(napi_env env, int32_t type, napi_value handle)
947 {
948 CALL_DEBUG_ENTER;
949 int32_t ret = InputManager::GetInstance()->SetTouchpadRightClickType(type);
950 return SetTouchpadData(env, handle, ret);
951 }
952
GetTouchpadRightClickType(napi_env env,napi_value handle)953 napi_value JsPointerManager::GetTouchpadRightClickType(napi_env env, napi_value handle)
954 {
955 CALL_DEBUG_ENTER;
956 int32_t type = 1;
957 int32_t ret = InputManager::GetInstance()->GetTouchpadRightClickType(type);
958 return GetTouchpadInt32Data(env, handle, type, ret);
959 }
960
SetTouchpadRotateSwitch(napi_env env,bool rotateSwitch,napi_value handle)961 napi_value JsPointerManager::SetTouchpadRotateSwitch(napi_env env, bool rotateSwitch, napi_value handle)
962 {
963 CALL_DEBUG_ENTER;
964 int32_t ret = InputManager::GetInstance()->SetTouchpadRotateSwitch(rotateSwitch);
965 return SetTouchpadData(env, handle, ret);
966 }
967
GetTouchpadRotateSwitch(napi_env env,napi_value handle)968 napi_value JsPointerManager::GetTouchpadRotateSwitch(napi_env env, napi_value handle)
969 {
970 CALL_DEBUG_ENTER;
971 bool rotateSwitch = true;
972 int32_t ret = InputManager::GetInstance()->GetTouchpadRotateSwitch(rotateSwitch);
973 return GetTouchpadBoolData(env, handle, rotateSwitch, ret);
974 }
975
SetTouchpadDoubleTapAndDragState(napi_env env,bool switchFlag,napi_value handle)976 napi_value JsPointerManager::SetTouchpadDoubleTapAndDragState(napi_env env, bool switchFlag, napi_value handle)
977 {
978 CALL_DEBUG_ENTER;
979 int32_t ret = InputManager::GetInstance()->SetTouchpadDoubleTapAndDragState(switchFlag);
980 return SetTouchpadData(env, handle, ret);
981 }
982
GetTouchpadDoubleTapAndDragState(napi_env env,napi_value handle)983 napi_value JsPointerManager::GetTouchpadDoubleTapAndDragState(napi_env env, napi_value handle)
984 {
985 CALL_DEBUG_ENTER;
986 bool switchFlag = true;
987 int32_t ret = InputManager::GetInstance()->GetTouchpadDoubleTapAndDragState(switchFlag);
988 return GetTouchpadBoolData(env, handle, switchFlag, ret);
989 }
990
EnableHardwareCursorStats(napi_env env,bool enable)991 napi_value JsPointerManager::EnableHardwareCursorStats(napi_env env, bool enable)
992 {
993 CALL_DEBUG_ENTER;
994 InputManager::GetInstance()->EnableHardwareCursorStats(enable);
995 napi_value result = nullptr;
996 if (napi_get_undefined(env, &result) != napi_ok) {
997 MMI_HILOGE("Get undefined result is failed");
998 return nullptr;
999 }
1000 return result;
1001 }
1002
GetHardwareCursorStats(napi_env env)1003 napi_value JsPointerManager::GetHardwareCursorStats(napi_env env)
1004 {
1005 CALL_DEBUG_ENTER;
1006 uint32_t frameCount = 0;
1007 uint32_t vsyncCount = 0;
1008 InputManager::GetInstance()->GetHardwareCursorStats(frameCount, vsyncCount);
1009 napi_value result = nullptr;
1010 napi_status status = napi_create_object(env, &result);
1011 if (status != napi_ok) {
1012 MMI_HILOGE("Napi create object is failed");
1013 return nullptr;
1014 }
1015 MMI_HILOGD("GetHardwareCursorStats, frameCount:%{public}d, vsyncCount:%{public}d",
1016 frameCount, vsyncCount);
1017 napi_value frameNapiCount;
1018 NAPI_CALL(env, napi_create_uint32(env, frameCount, &frameNapiCount));
1019 napi_value vsyncNapiCount;
1020 NAPI_CALL(env, napi_create_uint32(env, vsyncCount, &vsyncNapiCount));
1021 status = napi_set_named_property(env, result, "frameCount", frameNapiCount);
1022 if (status != napi_ok) {
1023 MMI_HILOGE("Napi set frameCount named property is failed");
1024 return nullptr;
1025 }
1026 status = napi_set_named_property(env, result, "vsyncCount", vsyncNapiCount);
1027 if (status != napi_ok) {
1028 MMI_HILOGE("Napi set vsyncCount named property is failed");
1029 return nullptr;
1030 }
1031 return result;
1032 }
1033
SetCustomCursor(napi_env env,int32_t windowId,CustomCursor cursor,CursorOptions options)1034 napi_value JsPointerManager::SetCustomCursor(napi_env env, int32_t windowId, CustomCursor cursor,
1035 CursorOptions options)
1036 {
1037 CALL_DEBUG_ENTER;
1038 sptr<CustomCursorAsyncContext> asyncContext = new (std::nothrow) CustomCursorAsyncContext(env);
1039 CHKPP(asyncContext);
1040 asyncContext->windowId = windowId;
1041 CHKPP(cursor.pixelMap);
1042 Parcel *pData = static_cast<Parcel*>(cursor.pixelMap);
1043 CHKPP(pData);
1044 Media::PixelMap* pixelMapPtr = Media::PixelMap::Unmarshalling(*pData);
1045 asyncContext->cursor.pixelMap = (void*)pixelMapPtr;
1046 asyncContext->cursor.focusX = cursor.focusX;
1047 asyncContext->cursor.focusY = cursor.focusY;
1048 asyncContext->options = options;
1049 napi_value promise = nullptr;
1050 CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
1051 ExecuteSetCustomCursorAsync(asyncContext);
1052 return promise;
1053 }
1054
ExecuteSetCustomCursorAsync(sptr<CustomCursorAsyncContext> asyncContext)1055 napi_value JsPointerManager::ExecuteSetCustomCursorAsync(sptr<CustomCursorAsyncContext> asyncContext)
1056 {
1057 CALL_DEBUG_ENTER;
1058 CHKPP(asyncContext);
1059 napi_env env = asyncContext->env;
1060 napi_value resource = nullptr;
1061 CHKRP(napi_create_string_utf8(env, "ExecuteSetCustomCursor", NAPI_AUTO_LENGTH, &resource), CREATE_STRING_UTF8);
1062 asyncContext->IncStrongRef(nullptr);
1063 if (!CreateAsyncWork(env, asyncContext, resource)) {
1064 MMI_HILOGE("Create async work failed");
1065 asyncContext->DecStrongRef(nullptr);
1066 return nullptr;
1067 }
1068
1069 napi_value result;
1070 napi_get_undefined(env, &result);
1071 return result;
1072 }
1073
CreateAsyncWork(napi_env env,sptr<CustomCursorAsyncContext> asyncContext,napi_value resource)1074 bool JsPointerManager::CreateAsyncWork(napi_env env, sptr<CustomCursorAsyncContext> asyncContext, napi_value resource)
1075 {
1076 napi_status status = napi_create_async_work(
1077 env,
1078 nullptr,
1079 resource,
1080 ExecuteSetCustomCursorWork,
1081 HandleSetCustomCursorCompletion,
1082 asyncContext.GetRefPtr(), &asyncContext->work);
1083 if (status != napi_ok ||
1084 napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) {
1085 return false;
1086 }
1087 return true;
1088 }
1089
ExecuteSetCustomCursorWork(napi_env env,void * data)1090 void JsPointerManager::ExecuteSetCustomCursorWork(napi_env env, void* data)
1091 {
1092 CHKPV(data);
1093 sptr<CustomCursorAsyncContext> asyncContext(static_cast<CustomCursorAsyncContext*>(data));
1094 asyncContext->errorCode = InputManager::GetInstance()->SetCustomCursor(
1095 asyncContext->windowId,
1096 asyncContext->cursor,
1097 asyncContext->options);
1098 napi_create_int32(env, asyncContext->errorCode, &asyncContext->resultValue);
1099 }
1100
HandleSetCustomCursorCompletion(napi_env env,napi_status status,void * data)1101 void JsPointerManager::HandleSetCustomCursorCompletion(napi_env env, napi_status status, void* data)
1102 {
1103 CHKPV(data);
1104 sptr<CustomCursorAsyncContext> asyncContext(static_cast<CustomCursorAsyncContext*>(data));
1105 CHKPV(asyncContext);
1106 napi_value results[2] = { 0 };
1107 int32_t size = 2;
1108 if (!GetResult(asyncContext, results, size)) {
1109 asyncContext->DecStrongRef(nullptr);
1110 return;
1111 }
1112 results[1] = asyncContext->resultValue;
1113 if (asyncContext->deferred) {
1114 if (asyncContext->errorCode == RET_OK) {
1115 CHKRV(napi_resolve_deferred(env, asyncContext->deferred, results[1]), RESOLVE_DEFERRED);
1116 } else {
1117 CHKRV(napi_reject_deferred(env, asyncContext->deferred, results[0]), REJECT_DEFERRED);
1118 }
1119 }
1120 asyncContext->DecStrongRef(nullptr);
1121 }
1122 } // namespace MMI
1123 } // namespace OHOS