1 /*
2 * Copyright (c) 2025 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 "touch/TouchPoint.h"
17
18 namespace TouchPoint {
19
GetTouchPointWindowX(napi_env env,napi_callback_info info)20 napi_value GetTouchPointWindowX(napi_env env, napi_callback_info info)
21 {
22 if ((env == nullptr) || (info == nullptr)) {
23 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode env or info is null");
24 return nullptr;
25 }
26 size_t argCnt = 1;
27 napi_value args[1] = { nullptr };
28 if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
29 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode napi_get_cb_info failed");
30 }
31 if (argCnt != PARAM_1) {
32 napi_throw_type_error(env, NULL, "Wrong number of arguments");
33 return nullptr;
34 }
35 bool isNullptr;
36 napi_get_value_bool(env, args[0], &isNullptr);
37 float point = 0;
38 int32_t result;
39 if (isNullptr) {
40 result = OH_NativeXComponent_GetTouchPointWindowX(nullptr, 0, &point) ==
41 PARAM_NEGATIVE_2 ? PARAM_NEGATIVE_2 : 0;
42 result = OH_NativeXComponent_GetTouchPointWindowX(XComponent.GetXComponent(), 0, nullptr) == PARAM_NEGATIVE_2 &&
43 result == PARAM_NEGATIVE_2 ? PARAM_NEGATIVE_2 : 0;
44 } else {
45 result = OH_NativeXComponent_GetTouchPointWindowX(XComponent.GetXComponent(), 0, &point);
46 }
47 napi_create_int32(env, result, &args[0]);
48 return args[0];
49 }
50
GetTouchPointWindowY(napi_env env,napi_callback_info info)51 napi_value GetTouchPointWindowY(napi_env env, napi_callback_info info)
52 {
53 if ((env == nullptr) || (info == nullptr)) {
54 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode env or info is null");
55 return nullptr;
56 }
57 size_t argCnt = 1;
58 napi_value args[1] = { nullptr };
59 if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
60 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode napi_get_cb_info failed");
61 }
62 if (argCnt != PARAM_1) {
63 napi_throw_type_error(env, NULL, "Wrong number of arguments");
64 return nullptr;
65 }
66 bool isNullptr;
67 napi_get_value_bool(env, args[0], &isNullptr);
68 float point = 0;
69 int32_t result;
70 if (isNullptr) {
71 result = OH_NativeXComponent_GetTouchPointWindowY(nullptr, 0, &point) ==
72 PARAM_NEGATIVE_2 ? PARAM_NEGATIVE_2 : 0;
73 result = OH_NativeXComponent_GetTouchPointWindowY(XComponent.GetXComponent(), 0, nullptr) == PARAM_NEGATIVE_2 &&
74 result == PARAM_NEGATIVE_2 ? PARAM_NEGATIVE_2 : 0;
75 } else {
76 result = OH_NativeXComponent_GetTouchPointWindowY(XComponent.GetXComponent(), 0, &point);
77 }
78 napi_create_int32(env, result, &args[0]);
79 return args[0];
80 }
81
GetTouchPointDisplayX(napi_env env,napi_callback_info info)82 napi_value GetTouchPointDisplayX(napi_env env, napi_callback_info info)
83 {
84 if ((env == nullptr) || (info == nullptr)) {
85 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode env or info is null");
86 return nullptr;
87 }
88 size_t argCnt = 1;
89 napi_value args[1] = { nullptr };
90 if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
91 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode napi_get_cb_info failed");
92 }
93 if (argCnt != PARAM_1) {
94 napi_throw_type_error(env, NULL, "Wrong number of arguments");
95 return nullptr;
96 }
97 bool isNullptr;
98 napi_get_value_bool(env, args[0], &isNullptr);
99 float point = 0;
100 int32_t result;
101 if (isNullptr) {
102 result = OH_NativeXComponent_GetTouchPointDisplayX(nullptr, 0, &point) ==
103 PARAM_NEGATIVE_2 ? PARAM_NEGATIVE_2 : 0;
104 result = OH_NativeXComponent_GetTouchPointDisplayX(XComponent.GetXComponent(),
105 0, nullptr) == PARAM_NEGATIVE_2 &&
106 result == PARAM_NEGATIVE_2 ? PARAM_NEGATIVE_2 : 0;
107 } else {
108 result = OH_NativeXComponent_GetTouchPointDisplayX(XComponent.GetXComponent(), 0, &point);
109 }
110 napi_create_int32(env, result, &args[0]);
111 return args[0];
112 }
113
GetTouchPointDisplayY(napi_env env,napi_callback_info info)114 napi_value GetTouchPointDisplayY(napi_env env, napi_callback_info info)
115 {
116 if ((env == nullptr) || (info == nullptr)) {
117 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode env or info is null");
118 return nullptr;
119 }
120 size_t argCnt = 1;
121 napi_value args[1] = { nullptr };
122 if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
123 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode napi_get_cb_info failed");
124 }
125 if (argCnt != PARAM_1) {
126 napi_throw_type_error(env, NULL, "Wrong number of arguments");
127 return nullptr;
128 }
129 bool isNullptr;
130 napi_get_value_bool(env, args[0], &isNullptr);
131 float point = 0;
132 int32_t result;
133 if (isNullptr) {
134 result = OH_NativeXComponent_GetTouchPointDisplayY(nullptr, 0, &point) ==
135 PARAM_NEGATIVE_2 ? PARAM_NEGATIVE_2 : 0;
136 result = OH_NativeXComponent_GetTouchPointDisplayY(XComponent.GetXComponent(),
137 0, nullptr) == PARAM_NEGATIVE_2 &&
138 result == PARAM_NEGATIVE_2 ? PARAM_NEGATIVE_2 : 0;
139 } else {
140 result = OH_NativeXComponent_GetTouchPointDisplayY(XComponent.GetXComponent(), 0, &point);
141 }
142 napi_create_int32(env, result, &args[0]);
143 return args[0];
144 }
145
GetHistoricalPoints(napi_env env,napi_callback_info info)146 napi_value GetHistoricalPoints(napi_env env, napi_callback_info info)
147 {
148 napi_value retValue = 0;
149 napi_create_int32(env, g_getHistoricalPointsResult, &retValue);
150 return retValue;
151 }
152
HistoricalPointId(napi_env env,napi_callback_info info)153 napi_value HistoricalPointId(napi_env env, napi_callback_info info)
154 {
155 napi_value retValue = 0;
156 int32_t result = g_historicalPoint.id != -1;
157 g_historicalPoint = {
158 .id = -1,
159 .screenX = -1,
160 .screenY = -1,
161 .x = -1,
162 .y = -1,
163 .type = OH_NativeXComponent_TouchEventType(),
164 .size = -1,
165 .force = -1,
166 .timeStamp = -1,
167 .titlX = -1,
168 .titlY = -1,
169 .sourceTool = OH_NativeXComponent_TouchEvent_SourceTool(),
170 };
171 napi_create_int32(env, result, &retValue);
172 return retValue;
173 }
174
HistoricalPointScreenX(napi_env env,napi_callback_info info)175 napi_value HistoricalPointScreenX(napi_env env, napi_callback_info info)
176 {
177 napi_value retValue = 0;
178 int32_t result = g_historicalPoint.screenX != -1;
179 g_historicalPoint = {
180 .id = -1,
181 .screenX = -1,
182 .screenY = -1,
183 .x = -1,
184 .y = -1,
185 .type = OH_NativeXComponent_TouchEventType(),
186 .size = -1,
187 .force = -1,
188 .timeStamp = -1,
189 .titlX = -1,
190 .titlY = -1,
191 .sourceTool = OH_NativeXComponent_TouchEvent_SourceTool(),
192 };
193 napi_create_int32(env, result, &retValue);
194 return retValue;
195 }
196
HistoricalPointScreenY(napi_env env,napi_callback_info info)197 napi_value HistoricalPointScreenY(napi_env env, napi_callback_info info)
198 {
199 napi_value retValue = 0;
200 int32_t result = g_historicalPoint.screenY != -1;
201 g_historicalPoint = {
202 .id = -1,
203 .screenX = -1,
204 .screenY = -1,
205 .x = -1,
206 .y = -1,
207 .type = OH_NativeXComponent_TouchEventType(),
208 .size = -1,
209 .force = -1,
210 .timeStamp = -1,
211 .titlX = -1,
212 .titlY = -1,
213 .sourceTool = OH_NativeXComponent_TouchEvent_SourceTool(),
214 };
215 napi_create_int32(env, result, &retValue);
216 return retValue;
217 }
218
HistoricalPointX(napi_env env,napi_callback_info info)219 napi_value HistoricalPointX(napi_env env, napi_callback_info info)
220 {
221 napi_value retValue = 0;
222 int32_t result = g_historicalPoint.x != -1;
223 g_historicalPoint = {
224 .id = -1,
225 .screenX = -1,
226 .screenY = -1,
227 .x = -1,
228 .y = -1,
229 .type = OH_NativeXComponent_TouchEventType(),
230 .size = -1,
231 .force = -1,
232 .timeStamp = -1,
233 .titlX = -1,
234 .titlY = -1,
235 .sourceTool = OH_NativeXComponent_TouchEvent_SourceTool(),
236 };
237 napi_create_int32(env, result, &retValue);
238 return retValue;
239 }
240
HistoricalPointY(napi_env env,napi_callback_info info)241 napi_value HistoricalPointY(napi_env env, napi_callback_info info)
242 {
243 napi_value retValue = 0;
244 int32_t result = g_historicalPoint.y != -1;
245 g_historicalPoint = {
246 .id = -1,
247 .screenX = -1,
248 .screenY = -1,
249 .x = -1,
250 .y = -1,
251 .type = OH_NativeXComponent_TouchEventType(),
252 .size = -1,
253 .force = -1,
254 .timeStamp = -1,
255 .titlX = -1,
256 .titlY = -1,
257 .sourceTool = OH_NativeXComponent_TouchEvent_SourceTool(),
258 };
259 napi_create_int32(env, result, &retValue);
260 return retValue;
261 }
262
HistoricalPointType(napi_env env,napi_callback_info info)263 napi_value HistoricalPointType(napi_env env, napi_callback_info info)
264 {
265 napi_value retValue = 0;
266 int32_t result = g_historicalPoint.type != OH_NativeXComponent_TouchEventType();
267 g_historicalPoint = {
268 .id = -1,
269 .screenX = -1,
270 .screenY = -1,
271 .x = -1,
272 .y = -1,
273 .type = OH_NativeXComponent_TouchEventType(),
274 .size = -1,
275 .force = -1,
276 .timeStamp = -1,
277 .titlX = -1,
278 .titlY = -1,
279 .sourceTool = OH_NativeXComponent_TouchEvent_SourceTool(),
280 };
281 napi_create_int32(env, result, &retValue);
282 return retValue;
283 }
284
HistoricalPointSize(napi_env env,napi_callback_info info)285 napi_value HistoricalPointSize(napi_env env, napi_callback_info info)
286 {
287 napi_value retValue = 0;
288 int32_t result = g_historicalPoint.size != -1;
289 g_historicalPoint = {
290 .id = -1,
291 .screenX = -1,
292 .screenY = -1,
293 .x = -1,
294 .y = -1,
295 .type = OH_NativeXComponent_TouchEventType(),
296 .size = -1,
297 .force = -1,
298 .timeStamp = -1,
299 .titlX = -1,
300 .titlY = -1,
301 .sourceTool = OH_NativeXComponent_TouchEvent_SourceTool(),
302 };
303 napi_create_int32(env, result, &retValue);
304 return retValue;
305 }
306
HistoricalPointForce(napi_env env,napi_callback_info info)307 napi_value HistoricalPointForce(napi_env env, napi_callback_info info)
308 {
309 napi_value retValue = 0;
310 int32_t result = g_historicalPoint.force != -1;
311 g_historicalPoint = {
312 .id = -1,
313 .screenX = -1,
314 .screenY = -1,
315 .x = -1,
316 .y = -1,
317 .type = OH_NativeXComponent_TouchEventType(),
318 .size = -1,
319 .force = -1,
320 .timeStamp = -1,
321 .titlX = -1,
322 .titlY = -1,
323 .sourceTool = OH_NativeXComponent_TouchEvent_SourceTool(),
324 };
325 napi_create_int32(env, result, &retValue);
326 return retValue;
327 }
328
HistoricalPointTimeStamp(napi_env env,napi_callback_info info)329 napi_value HistoricalPointTimeStamp(napi_env env, napi_callback_info info)
330 {
331 napi_value retValue = 0;
332 int32_t result = g_historicalPoint.timeStamp != -1;
333 g_historicalPoint = {
334 .id = -1,
335 .screenX = -1,
336 .screenY = -1,
337 .x = -1,
338 .y = -1,
339 .type = OH_NativeXComponent_TouchEventType(),
340 .size = -1,
341 .force = -1,
342 .timeStamp = -1,
343 .titlX = -1,
344 .titlY = -1,
345 .sourceTool = OH_NativeXComponent_TouchEvent_SourceTool(),
346 };
347 napi_create_int32(env, result, &retValue);
348 return retValue;
349 }
350
HistoricalPointTitlX(napi_env env,napi_callback_info info)351 napi_value HistoricalPointTitlX(napi_env env, napi_callback_info info)
352 {
353 napi_value retValue = 0;
354 int32_t result = g_historicalPoint.titlX != -1;
355 g_historicalPoint = {
356 .id = -1,
357 .screenX = -1,
358 .screenY = -1,
359 .x = -1,
360 .y = -1,
361 .type = OH_NativeXComponent_TouchEventType(),
362 .size = -1,
363 .force = -1,
364 .timeStamp = -1,
365 .titlX = -1,
366 .titlY = -1,
367 .sourceTool = OH_NativeXComponent_TouchEvent_SourceTool(),
368 };
369 napi_create_int32(env, result, &retValue);
370 return retValue;
371 }
372
HistoricalPointTitlY(napi_env env,napi_callback_info info)373 napi_value HistoricalPointTitlY(napi_env env, napi_callback_info info)
374 {
375 napi_value retValue = 0;
376 int32_t result = g_historicalPoint.titlY != -1;
377 g_historicalPoint = {
378 .id = -1,
379 .screenX = -1,
380 .screenY = -1,
381 .x = -1,
382 .y = -1,
383 .type = OH_NativeXComponent_TouchEventType(),
384 .size = -1,
385 .force = -1,
386 .timeStamp = -1,
387 .titlX = -1,
388 .titlY = -1,
389 .sourceTool = OH_NativeXComponent_TouchEvent_SourceTool(),
390 };
391 napi_create_int32(env, result, &retValue);
392 return retValue;
393 }
394
HistoricalPointSourceTool(napi_env env,napi_callback_info info)395 napi_value HistoricalPointSourceTool(napi_env env, napi_callback_info info)
396 {
397 napi_value retValue = 0;
398 int32_t result = g_historicalPoint.sourceTool != OH_NativeXComponent_TouchEvent_SourceTool();
399 g_historicalPoint = {
400 .id = -1,
401 .screenX = -1,
402 .screenY = -1,
403 .x = -1,
404 .y = -1,
405 .type = OH_NativeXComponent_TouchEventType(),
406 .size = -1,
407 .force = -1,
408 .timeStamp = -1,
409 .titlX = -1,
410 .titlY = -1,
411 .sourceTool = OH_NativeXComponent_TouchEvent_SourceTool(),
412 };
413 napi_create_int32(env, result, &retValue);
414 return retValue;
415 }
416
GetTouchEventSourceTool(napi_env env,napi_callback_info info)417 napi_value GetTouchEventSourceTool(napi_env env, napi_callback_info info)
418 {
419 if ((env == nullptr) || (info == nullptr)) {
420 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode env or info is null");
421 return nullptr;
422 }
423 size_t argCnt = 1;
424 napi_value args[1] = { nullptr };
425 if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
426 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode napi_get_cb_info failed");
427 }
428 if (argCnt != PARAM_1) {
429 napi_throw_type_error(env, NULL, "Wrong number of arguments");
430 return nullptr;
431 }
432 int32_t Index;
433 napi_get_value_int32(env, args[0], &Index);
434 napi_value retValue = 0;
435 int32_t result = g_historicalPointMap.count(OH_NativeXComponent_TouchEvent_SourceTool(Index)) != 0 ? Index : 0;
436 g_historicalPoint = {
437 .id = -1,
438 .screenX = -1,
439 .screenY = -1,
440 .x = -1,
441 .y = -1,
442 .type = OH_NativeXComponent_TouchEventType(),
443 .size = -1,
444 .force = -1,
445 .timeStamp = -1,
446 .titlX = -1,
447 .titlY = -1,
448 .sourceTool = OH_NativeXComponent_TouchEvent_SourceTool(),
449 };
450 napi_create_int32(env, result, &retValue);
451 return retValue;
452 }
453
GetTouchPointToolTypeResult(napi_env env,napi_callback_info info)454 napi_value GetTouchPointToolTypeResult(napi_env env, napi_callback_info info)
455 {
456 if ((env == nullptr) || (info == nullptr)) {
457 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode env or info is null");
458 return nullptr;
459 }
460 size_t argCnt = 1;
461 napi_value args[1] = { nullptr };
462 if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
463 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode napi_get_cb_info failed");
464 }
465 if (argCnt != PARAM_1) {
466 napi_throw_type_error(env, NULL, "Wrong number of arguments");
467 return nullptr;
468 }
469 int32_t Index;
470 napi_get_value_int32(env, args[0], &Index);
471 napi_value retValue = 0;
472 int32_t result = g_touchPointToolMap.count(OH_NativeXComponent_TouchPointToolType(Index)) != 0 ? Index : 0;
473 napi_create_int32(env, result, &retValue);
474 return retValue;
475 }
476
GetTouchEventSourceType(napi_env env,napi_callback_info info)477 napi_value GetTouchEventSourceType(napi_env env, napi_callback_info info)
478 {
479 if ((env == nullptr) || (info == nullptr)) {
480 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode env or info is null");
481 return nullptr;
482 }
483 size_t argCnt = 1;
484 napi_value args[1] = { nullptr };
485 if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
486 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode napi_get_cb_info failed");
487 }
488 if (argCnt != PARAM_1) {
489 napi_throw_type_error(env, NULL, "Wrong number of arguments");
490 return nullptr;
491 }
492 int32_t Index;
493 napi_get_value_int32(env, args[0], &Index);
494 napi_value retValue = 0;
495 int32_t result = g_touchEventSourceMap.count(OH_NativeXComponent_EventSourceType(Index)) != 0 ? Index : 0;
496 napi_create_int32(env, result, &retValue);
497 return retValue;
498 }
499
TouchPointType(napi_env env,napi_callback_info info)500 napi_value TouchPointType(napi_env env, napi_callback_info info)
501 {
502 napi_value retValue = 0;
503 int32_t result = 0;
504 OH_NativeXComponent_TouchPoint tempTouchPoint = g_touchPointMap[true];
505 result = tempTouchPoint.type >= OH_NATIVEXCOMPONENT_DOWN &&
506 tempTouchPoint.type <= OH_NATIVEXCOMPONENT_UNKNOWN;
507 tempTouchPoint = g_touchPointMap[false];
508 result = tempTouchPoint.type >= OH_NATIVEXCOMPONENT_DOWN &&
509 tempTouchPoint.type <= OH_NATIVEXCOMPONENT_UNKNOWN && result;
510 g_touchPointMap.clear();
511 napi_create_int32(env, result, &retValue);
512 return retValue;
513 }
514
TouchPointSize(napi_env env,napi_callback_info info)515 napi_value TouchPointSize(napi_env env, napi_callback_info info)
516 {
517 napi_value retValue = 0;
518 int32_t result = 0;
519 OH_NativeXComponent_TouchPoint tempTouchPoint = g_touchPointMap[true];
520 result = tempTouchPoint.size != -1;
521 tempTouchPoint = g_touchPointMap[false];
522 result = tempTouchPoint.size != -1 && result;
523 g_touchPointMap.clear();
524 napi_create_int32(env, result, &retValue);
525 return retValue;
526 }
527
TouchPointForce(napi_env env,napi_callback_info info)528 napi_value TouchPointForce(napi_env env, napi_callback_info info)
529 {
530 napi_value retValue = 0;
531 int32_t result = 0;
532 OH_NativeXComponent_TouchPoint tempTouchPoint = g_touchPointMap[true];
533 result = tempTouchPoint.force != -1;
534 tempTouchPoint = g_touchPointMap[false];
535 result = tempTouchPoint.force != -1 && result;
536 g_touchPointMap.clear();
537 napi_create_int32(env, result, &retValue);
538 return retValue;
539 }
540
TouchPointTimeStamp(napi_env env,napi_callback_info info)541 napi_value TouchPointTimeStamp(napi_env env, napi_callback_info info)
542 {
543 napi_value retValue = 0;
544 int32_t result = 0;
545 OH_NativeXComponent_TouchPoint tempTouchPoint = g_touchPointMap[true];
546 result = tempTouchPoint.timeStamp != -1;
547 tempTouchPoint = g_touchPointMap[false];
548 result = tempTouchPoint.timeStamp != -1 && result;
549 g_touchPointMap.clear();
550 napi_create_int32(env, result, &retValue);
551 return retValue;
552 }
553
TouchPointIsPressed(napi_env env,napi_callback_info info)554 napi_value TouchPointIsPressed(napi_env env, napi_callback_info info)
555 {
556 napi_value retValue = 0;
557 int32_t result = 0;
558 result = g_touchPointMap.size() == PARAM_2;
559 g_touchPointMap.clear();
560 napi_create_int32(env, result, &retValue);
561 return retValue;
562 }
563
TouchEventId(napi_env env,napi_callback_info info)564 napi_value TouchEventId(napi_env env, napi_callback_info info)
565 {
566 napi_value retValue = 0;
567 int32_t result = g_touchEvent.id != -1;
568 g_touchEvent =
569 {
570 .id = -1,
571 .screenX = -1,
572 .screenY = -1,
573 .x = -1,
574 .y = -1,
575 .type = OH_NativeXComponent_TouchEventType(),
576 .size = -1,
577 .force = -1,
578 .timeStamp = -1,
579 .touchPoints = {},
580 .numPoints = 0,
581 };
582 napi_create_int32(env, result, &retValue);
583 return retValue;
584 }
585
TouchEventSize(napi_env env,napi_callback_info info)586 napi_value TouchEventSize(napi_env env, napi_callback_info info)
587 {
588 napi_value retValue = 0;
589 int32_t result = g_touchEvent.size != -1;
590 g_touchEvent =
591 {
592 .id = -1,
593 .screenX = -1,
594 .screenY = -1,
595 .x = -1,
596 .y = -1,
597 .type = OH_NativeXComponent_TouchEventType(),
598 .size = -1,
599 .force = -1,
600 .timeStamp = -1,
601 .touchPoints = {},
602 .numPoints = 0,
603 };
604 napi_create_int32(env, result, &retValue);
605 return retValue;
606 }
607
TouchEventForce(napi_env env,napi_callback_info info)608 napi_value TouchEventForce(napi_env env, napi_callback_info info)
609 {
610 napi_value retValue = 0;
611 int32_t result = g_touchEvent.force != -1;
612 g_touchEvent =
613 {
614 .id = -1,
615 .screenX = -1,
616 .screenY = -1,
617 .x = -1,
618 .y = -1,
619 .type = OH_NativeXComponent_TouchEventType(),
620 .size = -1,
621 .force = -1,
622 .timeStamp = -1,
623 .touchPoints = {},
624 .numPoints = 0,
625 };
626 napi_create_int32(env, result, &retValue);
627 return retValue;
628 }
629
TouchEventDeviceId(napi_env env,napi_callback_info info)630 napi_value TouchEventDeviceId(napi_env env, napi_callback_info info)
631 {
632 napi_value retValue = 0;
633 int32_t result = g_touchEvent.deviceId != -1;
634 g_touchEvent =
635 {
636 .id = -1,
637 .screenX = -1,
638 .screenY = -1,
639 .x = -1,
640 .y = -1,
641 .type = OH_NativeXComponent_TouchEventType(),
642 .size = -1,
643 .force = -1,
644 .timeStamp = -1,
645 .touchPoints = {},
646 .numPoints = 0,
647 };
648 napi_create_int32(env, result, &retValue);
649 return retValue;
650 }
651
TouchEventTimeStamp(napi_env env,napi_callback_info info)652 napi_value TouchEventTimeStamp(napi_env env, napi_callback_info info)
653 {
654 napi_value retValue = 0;
655 int32_t result = g_touchEvent.timeStamp != -1;
656 g_touchEvent =
657 {
658 .id = -1,
659 .screenX = -1,
660 .screenY = -1,
661 .x = -1,
662 .y = -1,
663 .type = OH_NativeXComponent_TouchEventType(),
664 .size = -1,
665 .force = -1,
666 .timeStamp = -1,
667 .touchPoints = {},
668 .numPoints = 0,
669 };
670 napi_create_int32(env, result, &retValue);
671 return retValue;
672 }
673
CreateTouchNativeNode(napi_env env,napi_callback_info info)674 napi_value CreateTouchNativeNode(napi_env env, napi_callback_info info)
675 {
676 if ((env == nullptr) || (info == nullptr)) {
677 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode env or info is null");
678 return nullptr;
679 }
680 size_t argCnt = 1;
681 napi_value args[1] = { nullptr };
682 if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
683 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "CreateNativeNode napi_get_cb_info failed");
684 }
685 if (argCnt != PARAM_1) {
686 napi_throw_type_error(env, NULL, "Wrong number of arguments");
687 return nullptr;
688 }
689 ArkUI_NodeContentHandle nodeContentHandle = nullptr;
690 OH_ArkUI_GetNodeContentFromNapiValue(env, args[0], &nodeContentHandle);
691 XComponentCallback.DispatchTouchEvent = TouchEventCallback;
692 auto nodeContentEvent = [](ArkUI_NodeContentEvent *event) {
693 ArkUI_NodeContentHandle handle = OH_ArkUI_NodeContentEvent_GetNodeContentHandle(event);
694 if (OH_ArkUI_NodeContentEvent_GetEventType(event) == NODE_CONTENT_EVENT_ON_ATTACH_TO_WINDOW) {
695 InitNode();
696 OH_ArkUI_NodeContent_AddNode(handle, ColumnComponent.GetComponent());
697 }
698 };
699 OH_ArkUI_NodeContent_RegisterCallback(nodeContentHandle, nodeContentEvent);
700 return nullptr;
701 }
702
InitNode()703 void InitNode()
704 {
705 int width = 480;
706 ColumnComponent.SetWidth(width);
707 XComponent.SetXComponentType(ARKUI_XCOMPONENT_TYPE_SURFACE);
708 XComponent.SetXComponentId("TestXComponent");
709 XComponent.SetXComponentSurfaceSize(PARAM_15, PARAM_15);
710 XComponent.SetFocusable(true);
711 XComponent.SetFocusDefault(true);
712 XComponent.SetPercentWidth(1.0);
713 XComponent.SetPercentHeight(1.0);
714 XComponent.SetId("ndkxcomponent");
715
716 auto *nativeXComponent = XComponent.GetXComponent();
717 if (!nativeXComponent) {
718 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "GetNativeXComponent error");
719 }
720 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "GetNativeXComponent success");
721 OH_NativeXComponent_RegisterCallback(nativeXComponent, &XComponentCallback);
722 auto typeRet = XComponent.getAttribute(NODE_XCOMPONENT_TYPE);
723 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "xcomponent type: %{public}d",
724 typeRet->value[0].i32);
725 auto idRet = XComponent.getAttribute(NODE_XCOMPONENT_ID);
726 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "xcomponent id: %{public}s",
727 idRet->string);
728 std::shared_ptr<ArkUICapiTest::Component> child = std::make_shared<ArkUICapiTest::XComponent>(XComponent);
729 ColumnComponent.AddChild(child);
730 }
731
TouchEventCallback(OH_NativeXComponent * component,void * window)732 void TouchEventCallback(OH_NativeXComponent* component, void* window)
733 {
734 OH_NativeXComponent_HistoricalPoint* historicalPoints = nullptr;
735 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "TouchEvent", "TouchEventCallback");
736 OH_NativeXComponent_GetTouchEvent(component, window, &g_touchEvent);
737 int32_t size;
738 g_getHistoricalPointsResult = OH_NativeXComponent_GetHistoricalPoints(component, window, &size, &historicalPoints);
739 for (size_t index = 0; index < size; index++) {
740 if (g_historicalPointMap.count(historicalPoints[index].sourceTool) == 0) {
741 g_historicalPointMap.insert({ historicalPoints[index].sourceTool, historicalPoints[index] });
742 }
743 }
744 OH_NativeXComponent_TouchPointToolType toolType;
745 for (size_t index = 0; index < g_touchEvent.numPoints; index++) {
746 OH_NativeXComponent_GetTouchPointToolType(XComponent.GetXComponent(), index, &toolType);
747 if (g_touchPointToolMap.count(toolType) == 0) {
748 g_touchPointToolMap.insert({ toolType, toolType });
749 }
750 }
751 OH_NativeXComponent_EventSourceType sourceType;
752 for (size_t index = 0; index < g_touchEvent.numPoints; index++) {
753 OH_NativeXComponent_GetTouchEventSourceType(XComponent.GetXComponent(), index, &sourceType);
754 if (g_touchEventSourceMap.count(sourceType) == 0) {
755 g_touchEventSourceMap.insert({ sourceType, sourceType });
756 }
757 }
758 bool condition = size > 0 && g_historicalPoint.type == OH_NativeXComponent_TouchEventType() &&
759 g_historicalPoint.sourceTool == OH_NativeXComponent_TouchEvent_SourceTool();
760 if (condition) {
761 g_historicalPoint = historicalPoints[0];
762 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN,
763 "TouchEvent", "g_historicalPoint.sourceTool: %{public}d", g_historicalPoint.sourceTool);
764 }
765 if (g_touchEvent.numPoints > 0 && g_touchPointMap.count(g_touchEvent.touchPoints[0].isPressed) == 0) {
766 g_touchPointMap.insert({g_touchEvent.touchPoints[0].isPressed, g_touchEvent.touchPoints[0]});
767 }
768 return;
769 }
770
771 }
772