1 /*
2 * Copyright (c) 2024 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 "core/event/touch_event.h"
17
18 #include "base/input_manager/input_manager.h"
19 #include "core/common/ace_application_info.h"
20 #include "core/event/mouse_event.h"
21 #include "core/event/key_event.h"
22
23 namespace OHOS::Ace {
CovertId()24 void TouchPoint::CovertId()
25 {
26 if (sourceTool == SourceTool::PEN) {
27 originalId = TOUCH_TOOL_BASE_ID + static_cast<int32_t>(sourceTool);
28 id = id + originalId;
29 }
30 }
31
GetOriginalReCovertId() const32 int32_t TouchPoint::GetOriginalReCovertId() const
33 {
34 if (!AceApplicationInfo::GetInstance().GetTouchPadIdChanged()) {
35 return originalId;
36 }
37 if (sourceTool == SourceTool::PEN) {
38 return originalId - TOUCH_TOOL_BASE_ID - static_cast<int32_t>(sourceTool);
39 } else if (sourceTool == SourceTool::MOUSE) {
40 return originalId - MOUSE_BASE_ID - static_cast<int32_t>(MouseButton::LEFT_BUTTON);
41 } else {
42 return originalId;
43 }
44 }
45
SetId(int32_t id)46 TouchEvent& TouchEvent::SetId(int32_t id)
47 {
48 this->id = id;
49 return *this;
50 }
51
SetX(float x)52 TouchEvent& TouchEvent::SetX(float x)
53 {
54 this->x = x;
55 return *this;
56 }
57
SetY(float y)58 TouchEvent& TouchEvent::SetY(float y)
59 {
60 this->y = y;
61 return *this;
62 }
63
SetScreenX(float screenX)64 TouchEvent& TouchEvent::SetScreenX(float screenX)
65 {
66 this->screenX = screenX;
67 return *this;
68 }
69
SetScreenY(float screenY)70 TouchEvent& TouchEvent::SetScreenY(float screenY)
71 {
72 this->screenY = screenY;
73 return *this;
74 }
75
SetGlobalDisplayX(double globalDisplayX)76 TouchEvent& TouchEvent::SetGlobalDisplayX(double globalDisplayX)
77 {
78 this->globalDisplayX = globalDisplayX;
79 return *this;
80 }
81
SetGlobalDisplayY(double globalDisplayY)82 TouchEvent& TouchEvent::SetGlobalDisplayY(double globalDisplayY)
83 {
84 this->globalDisplayY = globalDisplayY;
85 return *this;
86 }
87
SetTime(TimeStamp time)88 TouchEvent& TouchEvent::SetTime(TimeStamp time)
89 {
90 this->time = time;
91 return *this;
92 }
93
GetTimeStamp() const94 TimeStamp TouchEvent::GetTimeStamp() const
95 {
96 return this->time;
97 }
98
SetType(TouchType type)99 TouchEvent& TouchEvent::SetType(TouchType type)
100 {
101 this->type = type;
102 return *this;
103 }
104
SetPullType(TouchType pullType)105 TouchEvent& TouchEvent::SetPullType(TouchType pullType)
106 {
107 this->pullType = pullType;
108 return *this;
109 }
110
SetSize(double size)111 TouchEvent& TouchEvent::SetSize(double size)
112 {
113 this->size = size;
114 return *this;
115 }
116
SetForce(float force)117 TouchEvent& TouchEvent::SetForce(float force)
118 {
119 this->force = force;
120 return *this;
121 }
122
SetTiltX(std::optional<float> tiltX)123 TouchEvent& TouchEvent::SetTiltX(std::optional<float> tiltX)
124 {
125 this->tiltX = tiltX;
126 return *this;
127 }
128
SetTiltY(std::optional<float> tiltY)129 TouchEvent& TouchEvent::SetTiltY(std::optional<float> tiltY)
130 {
131 this->tiltY = tiltY;
132 return *this;
133 }
134
SetRollAngle(std::optional<float> rollAngle)135 TouchEvent& TouchEvent::SetRollAngle(std::optional<float> rollAngle)
136 {
137 this->rollAngle = rollAngle;
138 return *this;
139 }
140
SetDeviceId(int64_t deviceId)141 TouchEvent& TouchEvent::SetDeviceId(int64_t deviceId)
142 {
143 this->deviceId = deviceId;
144 return *this;
145 }
146
SetTargetDisplayId(int32_t targetDisplayId)147 TouchEvent& TouchEvent::SetTargetDisplayId(int32_t targetDisplayId)
148 {
149 this->targetDisplayId = targetDisplayId;
150 return *this;
151 }
152
SetSourceType(SourceType sourceType)153 TouchEvent& TouchEvent::SetSourceType(SourceType sourceType)
154 {
155 this->sourceType = sourceType;
156 return *this;
157 }
158
SetSourceTool(SourceTool sourceTool)159 TouchEvent& TouchEvent::SetSourceTool(SourceTool sourceTool)
160 {
161 this->sourceTool = sourceTool;
162 return *this;
163 }
164
SetTouchEventId(int32_t touchEventId)165 TouchEvent& TouchEvent::SetTouchEventId(int32_t touchEventId)
166 {
167 this->touchEventId = touchEventId;
168 return *this;
169 }
170
SetIsInterpolated(bool isInterpolated)171 TouchEvent& TouchEvent::SetIsInterpolated(bool isInterpolated)
172 {
173 this->isInterpolated = isInterpolated;
174 return *this;
175 }
176
SetPointers(std::vector<TouchPoint> pointers)177 TouchEvent& TouchEvent::SetPointers(std::vector<TouchPoint> pointers)
178 {
179 this->pointers = std::move(pointers);
180 return *this;
181 }
182
SetPointerEvent(std::shared_ptr<const MMI::PointerEvent> pointerEvent)183 TouchEvent& TouchEvent::SetPointerEvent(std::shared_ptr<const MMI::PointerEvent> pointerEvent)
184 {
185 this->pointerEvent = std::move(pointerEvent);
186 return *this;
187 }
188
GetTouchEventPointerEvent() const189 std::shared_ptr<MMI::PointerEvent> TouchEvent::GetTouchEventPointerEvent() const
190 {
191 return InputManager::CreatePointerEvent(pointerEvent);
192 }
193
SetOriginalId(int32_t originalId)194 TouchEvent& TouchEvent::SetOriginalId(int32_t originalId)
195 {
196 this->originalId = originalId;
197 return *this;
198 }
199
SetIsInjected(bool isInjected)200 TouchEvent& TouchEvent::SetIsInjected(bool isInjected)
201 {
202 this->isInjected = isInjected;
203 return *this;
204 }
205
SetInputXDeltaSlope(float inputXDeltaSlope)206 TouchEvent& TouchEvent::SetInputXDeltaSlope(float inputXDeltaSlope)
207 {
208 this->inputXDeltaSlope = inputXDeltaSlope;
209 return *this;
210 }
211
SetInputYDeltaSlope(float inputYDeltaSlope)212 TouchEvent& TouchEvent::SetInputYDeltaSlope(float inputYDeltaSlope)
213 {
214 this->inputYDeltaSlope = inputYDeltaSlope;
215 return *this;
216 }
217
SetPressedKeyCodes(const std::vector<KeyCode> & pressedKeyCodes)218 TouchEvent& TouchEvent::SetPressedKeyCodes(const std::vector<KeyCode>& pressedKeyCodes)
219 {
220 this->pressedKeyCodes_ = pressedKeyCodes;
221 return *this;
222 }
223
SetIsPassThroughMode(bool isPassThroughMode)224 TouchEvent& TouchEvent::SetIsPassThroughMode(bool isPassThroughMode)
225 {
226 this->isPassThroughMode = isPassThroughMode;
227 return *this;
228 }
229
SetOperatingHand(int32_t operatingHand)230 TouchEvent& TouchEvent::SetOperatingHand(int32_t operatingHand)
231 {
232 this->operatingHand = operatingHand;
233 return *this;
234 }
235
SetPressedTime(TimeStamp pressedTime)236 TouchEvent& TouchEvent::SetPressedTime(TimeStamp pressedTime)
237 {
238 this->pressedTime = pressedTime;
239 return *this;
240 }
241
SetWidth(int32_t width)242 TouchEvent& TouchEvent::SetWidth(int32_t width)
243 {
244 this->width = width;
245 return *this;
246 }
247
SetHeight(int32_t height)248 TouchEvent& TouchEvent::SetHeight(int32_t height)
249 {
250 this->height = height;
251 return *this;
252 }
253
CloneWith(float scale) const254 TouchEvent TouchEvent::CloneWith(float scale) const
255 {
256 return CloneWith(scale, 0.0f, 0.0f, std::nullopt);
257 }
258
CloneWith(float scale,float offsetX,float offsetY,std::optional<int32_t> pointId) const259 TouchEvent TouchEvent::CloneWith(float scale, float offsetX, float offsetY, std::optional<int32_t> pointId) const
260 {
261 TouchEvent event;
262 event.id = pointId.has_value() ? pointId.value() : id;
263 event.x = (x - offsetX) / scale;
264 event.y = (y - offsetY) / scale;
265 event.screenX = (screenX - offsetX) / scale;
266 event.screenY = (screenY - offsetY) / scale;
267 event.globalDisplayX = (globalDisplayX - offsetX) / scale;
268 event.globalDisplayY = (globalDisplayY - offsetY) / scale;
269 event.type = type;
270 event.pullType = pullType;
271 event.time = time;
272 event.size = size;
273 event.force = force;
274 event.tiltX = tiltX;
275 event.tiltY = tiltY;
276 event.rollAngle = rollAngle;
277 event.deviceId = deviceId;
278 event.targetDisplayId = targetDisplayId;
279 event.sourceType = sourceType;
280 event.sourceTool = sourceTool;
281 event.touchEventId = touchEventId;
282 event.isInterpolated = isInterpolated;
283 event.pointers = std::move(pointers);
284 event.pointerEvent = std::move(pointerEvent);
285 event.originalId = originalId;
286 event.pressedKeyCodes_ = pressedKeyCodes_;
287 event.isInjected = isInjected;
288 event.isPrivacyMode = isPrivacyMode;
289 event.inputXDeltaSlope = inputXDeltaSlope;
290 event.inputYDeltaSlope = inputYDeltaSlope;
291 event.eventType = UIInputEventType::TOUCH;
292 event.isPassThroughMode = isPassThroughMode;
293 event.operatingHand = operatingHand;
294 event.width = width;
295 event.height = height;
296 event.pressedTime = pressedTime;
297 event.convertInfo = convertInfo;
298 event.passThrough = passThrough;
299 // Only set postEventNodeId when the event supports passThrough
300 if (passThrough) {
301 event.postEventNodeId = postEventNodeId;
302 }
303 return event;
304 }
305
ToJsonValue(std::unique_ptr<JsonValue> & json) const306 void TouchEvent::ToJsonValue(std::unique_ptr<JsonValue>& json) const
307 {
308 json->Put("id", id);
309 json->Put("x", x);
310 json->Put("y", y);
311 json->Put("sx", screenX);
312 json->Put("sy", screenY);
313 json->Put("ty", static_cast<int32_t>(type));
314 int64_t timeValue = std::chrono::duration_cast<std::chrono::nanoseconds>(time.time_since_epoch()).count();
315 json->Put("ti", timeValue);
316 json->Put("si", size);
317 json->Put("f", force);
318 int32_t hasTiltX = tiltX.has_value() ? 1 : 0;
319 json->Put("hx", hasTiltX);
320 if (hasTiltX) {
321 json->Put("tx", tiltX.value());
322 }
323 int32_t hasTiltY = tiltY.has_value() ? 1 : 0;
324 json->Put("hy", hasTiltY);
325 if (tiltY.has_value()) {
326 json->Put("ty", tiltY.value());
327 }
328 int32_t hasRollAngle = rollAngle.has_value() ? 1 : 0;
329 json->Put("ha", hasRollAngle);
330 if (rollAngle.has_value()) {
331 json->Put("ta", rollAngle.value());
332 }
333 json->Put("d", deviceId);
334 json->Put("sty", static_cast<int32_t>(sourceType));
335 json->Put("sto", static_cast<int32_t>(sourceTool));
336 }
337
FromJson(const std::unique_ptr<JsonValue> & json)338 void TouchEvent::FromJson(const std::unique_ptr<JsonValue>& json)
339 {
340 CHECK_NULL_VOID(json);
341 id = json->GetInt("id");
342 x = json->GetDouble("x");
343 y = json->GetDouble("y");
344 screenX = json->GetDouble("sx");
345 screenY = json->GetDouble("sy");
346 type = static_cast<TouchType>(json->GetInt("ty"));
347 int64_t timeValue = json->GetInt64("ti");
348 time = TimeStamp(std::chrono::nanoseconds(timeValue));
349 size = json->GetDouble("si");
350 force = json->GetDouble("f");
351 int32_t hasTiltX = json->GetInt("hx");
352 int32_t hasTiltY = json->GetInt("hy");
353 if (hasTiltX) {
354 tiltX = json->GetDouble("tx");
355 }
356 if (hasTiltY) {
357 tiltY = json->GetDouble("ty");
358 }
359 int32_t hasRollAngle = json->GetInt("ha");
360 if (hasRollAngle) {
361 rollAngle = json->GetDouble("ta");
362 }
363 deviceId = json->GetInt64("d");
364 sourceType = static_cast<SourceType>(json->GetInt("sty"));
365 sourceTool = static_cast<SourceTool>(json->GetInt("sto"));
366 }
367
GetOffset() const368 Offset TouchEvent::GetOffset() const
369 {
370 return Offset(x, y);
371 }
372
GetScreenOffset() const373 Offset TouchEvent::GetScreenOffset() const
374 {
375 return Offset(screenX, screenY);
376 }
377
GetGlobalDisplayOffset() const378 Offset TouchEvent::GetGlobalDisplayOffset() const
379 {
380 return Offset(globalDisplayX, globalDisplayY);
381 }
382
CovertId()383 void TouchEvent::CovertId()
384 {
385 if ((sourceType == SourceType::TOUCH) && (sourceTool == SourceTool::PEN)) {
386 id = id + TOUCH_TOOL_BASE_ID + static_cast<int32_t>(sourceTool);
387 originalId = TOUCH_TOOL_BASE_ID + static_cast<int32_t>(sourceTool);
388 }
389 }
390
GetOriginalReCovertId() const391 int32_t TouchEvent::GetOriginalReCovertId() const
392 {
393 if (!AceApplicationInfo::GetInstance().GetTouchPadIdChanged()) {
394 return originalId;
395 }
396 if ((sourceType == SourceType::TOUCH) && (sourceTool == SourceTool::PEN)) {
397 return originalId - TOUCH_TOOL_BASE_ID - static_cast<int32_t>(sourceTool);
398 } else if (sourceType == SourceType::MOUSE) {
399 return originalId - MOUSE_BASE_ID - static_cast<int32_t>(MouseButton::LEFT_BUTTON);
400 } else {
401 return originalId;
402 }
403 }
404
CreateScalePoint(float scale) const405 TouchEvent TouchEvent::CreateScalePoint(float scale) const
406 {
407 if (NearZero(scale)) {
408 return CloneWith(1);
409 }
410 auto temp = pointers;
411 std::for_each(temp.begin(), temp.end(), [scale](auto&& point) {
412 point.x = point.x / scale;
413 point.y = point.y / scale;
414 point.screenX = point.screenX / scale;
415 point.screenY = point.screenY / scale;
416 point.globalDisplayX = point.globalDisplayX / scale;
417 point.globalDisplayY = point.globalDisplayY / scale;
418 });
419 return CloneWith(scale);
420 }
421
UpdateScalePoint(float scale,float offsetX,float offsetY,int32_t pointId) const422 TouchEvent TouchEvent::UpdateScalePoint(float scale, float offsetX, float offsetY, int32_t pointId) const
423 {
424 auto temp = pointers;
425 if (NearZero(scale)) {
426 std::for_each(temp.begin(), temp.end(), [offsetX, offsetY](auto&& point) {
427 point.x = point.x - offsetX;
428 point.y = point.y - offsetY;
429 point.screenX = point.screenX - offsetX;
430 point.screenY = point.screenY - offsetY;
431 point.globalDisplayX = point.globalDisplayX - offsetX;
432 point.globalDisplayY = point.globalDisplayY - offsetY;
433 });
434 return CloneWith(1, offsetX, offsetY, pointId);
435 }
436
437 std::for_each(temp.begin(), temp.end(), [scale, offsetX, offsetY](auto&& point) {
438 point.x = (point.x - offsetX) / scale;
439 point.y = (point.y - offsetY) / scale;
440 point.screenX = (point.screenX - offsetX) / scale;
441 point.screenY = (point.screenY - offsetY) / scale;
442 point.globalDisplayX = (point.globalDisplayX - offsetX) / scale;
443 point.globalDisplayY = (point.globalDisplayY - offsetY) / scale;
444 });
445 return CloneWith(scale, offsetX, offsetY, pointId);
446 }
447
UpdatePointers() const448 TouchEvent TouchEvent::UpdatePointers() const
449 {
450 TouchPoint point { .id = id,
451 .x = x,
452 .y = y,
453 .screenX = screenX,
454 .screenY = screenY,
455 .globalDisplayX = globalDisplayX,
456 .globalDisplayY = globalDisplayY,
457 .downTime = time,
458 .size = size,
459 .force = force,
460 .isPressed = (type == TouchType::DOWN),
461 .operatingHand = operatingHand };
462 TouchEvent event;
463 event.SetId(id)
464 .SetX(x)
465 .SetY(y)
466 .SetScreenX(screenX)
467 .SetScreenY(screenY)
468 .SetGlobalDisplayX(globalDisplayX)
469 .SetGlobalDisplayY(globalDisplayY)
470 .SetType(type)
471 .SetTime(time)
472 .SetSize(size)
473 .SetForce(force)
474 .SetDeviceId(deviceId)
475 .SetTargetDisplayId(targetDisplayId)
476 .SetSourceType(sourceType)
477 .SetIsInterpolated(isInterpolated)
478 .SetPointerEvent(pointerEvent)
479 .SetOriginalId(originalId)
480 .SetIsPassThroughMode(isPassThroughMode)
481 .SetOperatingHand(operatingHand);
482 event.pointers.emplace_back(std::move(point));
483 return event;
484 }
485
IsPenHoverEvent() const486 bool TouchEvent::IsPenHoverEvent() const
487 {
488 return sourceTool == SourceTool::PEN && (type == TouchType::PROXIMITY_IN || type == TouchType::PROXIMITY_OUT ||
489 (type == TouchType::MOVE && NearZero(force)));
490 }
491
GetTargetDisplayId() const492 int32_t TouchEvent::GetTargetDisplayId() const
493 {
494 return targetDisplayId;
495 }
496
GetEventIdentity() const497 int32_t TouchEvent::GetEventIdentity() const
498 {
499 if (passThrough) {
500 return id;
501 }
502 return originalId;
503 }
504
SetScreenX(float screenX)505 void TouchCallBackInfo::SetScreenX(float screenX)
506 {
507 screenX_ = screenX;
508 }
509
GetScreenX() const510 float TouchCallBackInfo::GetScreenX() const
511 {
512 return screenX_;
513 }
514
SetScreenY(float screenY)515 void TouchCallBackInfo::SetScreenY(float screenY)
516 {
517 screenY_ = screenY;
518 }
519
GetScreenY() const520 float TouchCallBackInfo::GetScreenY() const
521 {
522 return screenY_;
523 }
524
SetGlobalDisplayX(double globalDisplayX)525 void TouchCallBackInfo::SetGlobalDisplayX(double globalDisplayX)
526 {
527 globalDisplayX_ = globalDisplayX;
528 }
529
GetGlobalDisplayX() const530 double TouchCallBackInfo::GetGlobalDisplayX() const
531 {
532 return globalDisplayX_;
533 }
534
SetGlobalDisplayY(double globalDisplayY)535 void TouchCallBackInfo::SetGlobalDisplayY(double globalDisplayY)
536 {
537 globalDisplayY_ = globalDisplayY;
538 }
539
GetGlobalDisplayY() const540 double TouchCallBackInfo::GetGlobalDisplayY() const
541 {
542 return globalDisplayY_;
543 }
544
SetLocalX(float localX)545 void TouchCallBackInfo::SetLocalX(float localX)
546 {
547 localX_ = localX;
548 }
549
GetLocalX() const550 float TouchCallBackInfo::GetLocalX() const
551 {
552 return localX_;
553 }
554
SetLocalY(float localY)555 void TouchCallBackInfo::SetLocalY(float localY)
556 {
557 localY_ = localY;
558 }
559
GetLocalY() const560 float TouchCallBackInfo::GetLocalY() const
561 {
562 return localY_;
563 }
564
SetTouchType(TouchType type)565 void TouchCallBackInfo::SetTouchType(TouchType type)
566 {
567 touchType_ = type;
568 }
569
GetTouchType() const570 TouchType TouchCallBackInfo::GetTouchType() const
571 {
572 return touchType_;
573 }
574
SetTimeStamp(const TimeStamp & time)575 void TouchCallBackInfo::SetTimeStamp(const TimeStamp& time)
576 {
577 time_ = time;
578 }
579
GetTimeStamp() const580 TimeStamp TouchCallBackInfo::GetTimeStamp() const
581 {
582 return time_;
583 }
584
SetGlobalLocation(const Offset & globalLocation)585 TouchLocationInfo& TouchLocationInfo::SetGlobalLocation(const Offset& globalLocation)
586 {
587 globalLocation_ = globalLocation;
588 return *this;
589 }
590
SetLocalLocation(const Offset & localLocation)591 TouchLocationInfo& TouchLocationInfo::SetLocalLocation(const Offset& localLocation)
592 {
593 localLocation_ = localLocation;
594 return *this;
595 }
596
SetScreenLocation(const Offset & screenLocation)597 TouchLocationInfo& TouchLocationInfo::SetScreenLocation(const Offset& screenLocation)
598 {
599 screenLocation_ = screenLocation;
600 return *this;
601 }
602
SetGlobalDisplayLocation(const Offset & globalDisplayLocation)603 TouchLocationInfo& TouchLocationInfo::SetGlobalDisplayLocation(const Offset& globalDisplayLocation)
604 {
605 globalDisplayLocation_ = globalDisplayLocation;
606 return *this;
607 }
608
SetSize(double size)609 void TouchLocationInfo::SetSize(double size)
610 {
611 size_ = size;
612 }
613
GetSize() const614 double TouchLocationInfo::GetSize() const
615 {
616 return size_;
617 }
618
SetTouchDeviceId(int64_t deviceId)619 void TouchLocationInfo::SetTouchDeviceId(int64_t deviceId)
620 {
621 touchDeviceId_ = deviceId;
622 }
623
GetTouchDeviceId() const624 int64_t TouchLocationInfo::GetTouchDeviceId() const
625 {
626 return touchDeviceId_;
627 }
628
SetTouchType(TouchType type)629 void TouchLocationInfo::SetTouchType(TouchType type)
630 {
631 touchType_ = type;
632 }
633
SetPressedTime(TimeStamp pressedTime)634 void TouchLocationInfo::SetPressedTime(TimeStamp pressedTime)
635 {
636 pressedTime_ = pressedTime;
637 }
638
GetPressedTime() const639 TimeStamp TouchLocationInfo::GetPressedTime() const
640 {
641 return pressedTime_;
642 }
643
SetWidth(int32_t width)644 void TouchLocationInfo::SetWidth(int32_t width)
645 {
646 width_ = width;
647 }
648
GetWidth() const649 int32_t TouchLocationInfo::GetWidth() const
650 {
651 return width_;
652 }
653
SetHeight(int32_t height)654 void TouchLocationInfo::SetHeight(int32_t height)
655 {
656 height_ = height;
657 }
658
GetHeight() const659 int32_t TouchLocationInfo::GetHeight() const
660 {
661 return height_;
662 }
663
Dump(std::list<std::pair<int32_t,std::string>> & dumpList,int32_t depth) const664 void StateRecord::Dump(std::list<std::pair<int32_t, std::string>>& dumpList, int32_t depth) const
665 {
666 std::stringstream oss;
667 oss << "procedure: " << procedure;
668 if (!state.empty()) {
669 oss << ", "
670 << "state: " << state << ", "
671 << "disposal: " << disposal << ", "
672 << "extraInfo: " << extraInfo;
673 }
674 oss << ", "
675 << "timestamp: " << ConvertTimestampToStr(timestamp);
676 dumpList.emplace_back(std::make_pair(depth, oss.str()));
677 }
678
Dump(std::unique_ptr<JsonValue> & json) const679 void StateRecord::Dump(std::unique_ptr<JsonValue>& json) const
680 {
681 json->Put("procedure", procedure.c_str());
682 if (!state.empty()) {
683 json->Put("state", state.c_str());
684 json->Put("disposal", disposal.c_str());
685 json->Put("extraInfo", extraInfo.c_str());
686 }
687 json->Put("timestamp", ConvertTimestampToStr(timestamp).c_str());
688 }
689
AddProcedure(const std::string & procedure,const std::string & extraInfo,const std::string & state,const std::string & disposal,int64_t timestamp)690 void GestureSnapshot::AddProcedure(const std::string& procedure, const std::string& extraInfo,
691 const std::string& state, const std::string& disposal, int64_t timestamp)
692 {
693 if (timestamp == 0) {
694 timestamp = GetCurrentTimestamp();
695 }
696 stateHistory.emplace_back(StateRecord(procedure, extraInfo, state, disposal, timestamp));
697 }
698
CheckNeedAddMove(const std::string & state,const std::string & disposal)699 bool GestureSnapshot::CheckNeedAddMove(const std::string& state, const std::string& disposal)
700 {
701 return stateHistory.empty() || stateHistory.back().state != state || stateHistory.back().disposal != disposal;
702 }
703
Dump(std::list<std::pair<int32_t,std::string>> & dumpList,int32_t depth) const704 void GestureSnapshot::Dump(std::list<std::pair<int32_t, std::string>>& dumpList, int32_t depth) const
705 {
706 std::stringstream oss;
707 oss << "frameNodeId: " << nodeId << ", "
708 << "type: " << type << ", "
709 << "depth: " << this->depth << ", " << std::hex << "id: 0x" << id << ", "
710 << "parentId: 0x" << parentId;
711 if (!customInfo.empty()) {
712 oss << ", "
713 << "customInfo: " << customInfo;
714 }
715 dumpList.emplace_back(std::make_pair(depth + this->depth, oss.str()));
716 dumpList.emplace_back(std::make_pair(depth + 1 + this->depth, "stateHistory:"));
717 for (const auto& state : stateHistory) {
718 state.Dump(dumpList, depth + 1 + 1 + this->depth);
719 }
720 }
721
TransTouchType(TouchType type)722 std::string GestureSnapshot::TransTouchType(TouchType type)
723 {
724 switch (type) {
725 case TouchType::DOWN:
726 return "TouchDown";
727 case TouchType::MOVE:
728 return "TouchMove";
729 case TouchType::UP:
730 return "TouchUp";
731 case TouchType::CANCEL:
732 return "TouchCancel";
733 default:
734 return std::string("Type:").append(std::to_string(static_cast<int32_t>(type)));
735 }
736 }
737
TransAxisType(AxisAction action)738 std::string GestureSnapshot::TransAxisType(AxisAction action)
739 {
740 switch (action) {
741 case AxisAction::BEGIN:
742 return "AxisBegin";
743 case AxisAction::UPDATE:
744 return "AxisUpdate";
745 case AxisAction::END:
746 return "AxisEnd";
747 case AxisAction::CANCEL:
748 return "AxisCancel";
749 default:
750 return std::string("Type:").append(std::to_string(static_cast<int32_t>(action)));
751 }
752 }
753
GetIds() const754 std::tuple<std::string, std::string> GestureSnapshot::GetIds() const
755 {
756 std::stringstream oss;
757 oss << "0x" << std::hex << id;
758 std::string idStr = oss.str();
759 oss.str("");
760 oss << "0x" << std::hex << parentId;
761 std::string parentIdStr = oss.str();
762 return std::make_tuple(idStr, parentIdStr);
763 }
764
Dump(std::unique_ptr<JsonValue> & json) const765 void GestureSnapshot::Dump(std::unique_ptr<JsonValue>& json) const
766 {
767 json->Put("frameNodeId", nodeId);
768 json->Put("type", type.c_str());
769 auto result = GetIds();
770 json->Put("id", std::get<0>(result).c_str());
771 json->Put("parentId", std::get<1>(result).c_str());
772 json->Put("depth", this->depth);
773 if (!customInfo.empty()) {
774 json->Put("customInfo", customInfo.c_str());
775 }
776 std::unique_ptr<JsonValue> children = JsonUtil::CreateArray(true);
777 for (const auto& state : stateHistory) {
778 std::unique_ptr<JsonValue> child = JsonUtil::Create(true);
779 state.Dump(child);
780 children->Put(child);
781 }
782 json->Put("stateHistory", children);
783 }
784
SetTouchRestrict(const TouchRestrict & touchRestrict)785 void TouchEventTarget::SetTouchRestrict(const TouchRestrict& touchRestrict)
786 {
787 touchRestrict_ = touchRestrict;
788 }
789
SetGetEventTargetImpl(const GetEventTargetImpl & getEventTargetImpl)790 void TouchEventTarget::SetGetEventTargetImpl(const GetEventTargetImpl& getEventTargetImpl)
791 {
792 getEventTargetImpl_ = getEventTargetImpl;
793 }
794
GetEventTarget() const795 std::optional<EventTarget> TouchEventTarget::GetEventTarget() const
796 {
797 if (getEventTargetImpl_) {
798 return getEventTargetImpl_();
799 }
800 return std::nullopt;
801 }
802
SetCoordinateOffset(const Offset & coordinateOffset)803 void TouchEventTarget::SetCoordinateOffset(const Offset& coordinateOffset)
804 {
805 coordinateOffset_ = coordinateOffset;
806 }
807
GetCoordinateOffset() const808 const Offset& TouchEventTarget::GetCoordinateOffset() const
809 {
810 return coordinateOffset_;
811 }
812
SetSubPipelineGlobalOffset(const Offset & subPipelineGlobalOffset,float viewScale)813 void TouchEventTarget::SetSubPipelineGlobalOffset(const Offset& subPipelineGlobalOffset, float viewScale)
814 {
815 subPipelineGlobalOffset_ = subPipelineGlobalOffset;
816 viewScale_ = viewScale;
817 }
818
DispatchMultiContainerEvent(const TouchEvent & point)819 bool TouchEventTarget::DispatchMultiContainerEvent(const TouchEvent& point)
820 {
821 #ifdef OHOS_STANDARD_SYSTEM
822 if (!subPipelineGlobalOffset_.IsZero()) {
823 auto multiContainerPoint = point.UpdateScalePoint(
824 viewScale_, subPipelineGlobalOffset_.GetX(), subPipelineGlobalOffset_.GetY(), point.id);
825 return DispatchEvent(multiContainerPoint);
826 }
827 #endif
828 return DispatchEvent(point);
829 }
830
HandleMultiContainerEvent(const TouchEvent & point)831 bool TouchEventTarget::HandleMultiContainerEvent(const TouchEvent& point)
832 {
833 #ifdef OHOS_STANDARD_SYSTEM
834 if (!subPipelineGlobalOffset_.IsZero()) {
835 auto multiContainerPoint = point.UpdateScalePoint(
836 viewScale_, subPipelineGlobalOffset_.GetX(), subPipelineGlobalOffset_.GetY(), point.id);
837 return HandleEvent(multiContainerPoint);
838 }
839 #endif
840 return HandleEvent(point);
841 }
842
GetNodeName() const843 std::string TouchEventTarget::GetNodeName() const
844 {
845 return nodeName_;
846 }
847
SetNodeId(int id)848 void TouchEventTarget::SetNodeId(int id)
849 {
850 if (nodeId_ != -1) {
851 return;
852 }
853 nodeId_ = id;
854 }
855
GetNodeId() const856 int32_t TouchEventTarget::GetNodeId() const
857 {
858 return nodeId_;
859 }
860
AttachFrameNode(const WeakPtr<NG::FrameNode> & node)861 void TouchEventTarget::AttachFrameNode(const WeakPtr<NG::FrameNode>& node)
862 {
863 if (!(node_.Invalid())) {
864 return;
865 }
866 node_ = node;
867 }
868
GetAttachedNode() const869 WeakPtr<NG::FrameNode> TouchEventTarget::GetAttachedNode() const
870 {
871 return node_;
872 }
873
Dump() const874 RefPtr<GestureSnapshot> TouchEventTarget::Dump() const
875 {
876 RefPtr<GestureSnapshot> info = AceType::MakeRefPtr<GestureSnapshot>();
877 info->type = GetTypeName();
878 info->id = reinterpret_cast<uintptr_t>(this);
879 return info;
880 }
881
SetTargetComponent(const RefPtr<NG::TargetComponent> & targetComponent)882 void TouchEventTarget::SetTargetComponent(const RefPtr<NG::TargetComponent>& targetComponent)
883 {
884 if (!targetComponent_) {
885 targetComponent_ = targetComponent;
886 }
887 }
888
GetTargetComponent()889 RefPtr<NG::TargetComponent> TouchEventTarget::GetTargetComponent()
890 {
891 return targetComponent_;
892 }
893
SetIsPostEventResult(bool isPostEventResult)894 void TouchEventTarget::SetIsPostEventResult(bool isPostEventResult)
895 {
896 isPostEventResult_ = isPostEventResult;
897 }
898
IsPostEventResult() const899 bool TouchEventTarget::IsPostEventResult() const
900 {
901 return isPostEventResult_;
902 }
903
AddTouchLocationInfo(TouchLocationInfo && info)904 void TouchEventInfo::AddTouchLocationInfo(TouchLocationInfo&& info)
905 {
906 touches_.emplace_back(info);
907 }
908
AddChangedTouchLocationInfo(TouchLocationInfo && info)909 void TouchEventInfo::AddChangedTouchLocationInfo(TouchLocationInfo&& info)
910 {
911 changedTouches_.emplace_back(info);
912 }
913
AddHistoryLocationInfo(TouchLocationInfo && info)914 void TouchEventInfo::AddHistoryLocationInfo(TouchLocationInfo&& info)
915 {
916 history_.emplace_back(std::move(info));
917 }
918
GetHistory() const919 const std::list<TouchLocationInfo>& TouchEventInfo::GetHistory() const
920 {
921 return history_;
922 }
923
AddHistoryPointerEvent(const std::shared_ptr<MMI::PointerEvent> & info)924 void TouchEventInfo::AddHistoryPointerEvent(const std::shared_ptr<MMI::PointerEvent>& info)
925 {
926 historyPointerEvent_.emplace_back(info);
927 }
928
GetHistoryPointerEvent() const929 const std::list<std::shared_ptr<MMI::PointerEvent>>& TouchEventInfo::GetHistoryPointerEvent() const
930 {
931 return historyPointerEvent_;
932 }
933
SetPointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)934 void TouchEventInfo::SetPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
935 {
936 pointerEvent_ = pointerEvent;
937 }
938
GetPointerEvent() const939 const std::shared_ptr<MMI::PointerEvent>& TouchEventInfo::GetPointerEvent() const
940 {
941 return pointerEvent_;
942 }
943
SetTouchEventsEnd(bool isTouchEventsEnd)944 void TouchEventInfo::SetTouchEventsEnd(bool isTouchEventsEnd)
945 {
946 isTouchEventsEnd_ = isTouchEventsEnd;
947 }
948
GetTouchEventsEnd() const949 bool TouchEventInfo::GetTouchEventsEnd() const
950 {
951 return isTouchEventsEnd_;
952 }
953
ConvertToTouchEvent() const954 TouchEvent TouchEventInfo::ConvertToTouchEvent() const
955 {
956 TouchEvent touchEvent;
957 if (!changedTouches_.empty()) {
958 touchEvent.x = static_cast<float>(changedTouches_.front().GetGlobalLocation().GetX());
959 touchEvent.y = static_cast<float>(changedTouches_.front().GetGlobalLocation().GetY());
960 touchEvent.screenX = static_cast<float>(changedTouches_.front().GetScreenLocation().GetX());
961 touchEvent.screenY = static_cast<float>(changedTouches_.front().GetScreenLocation().GetY());
962 touchEvent.localX = static_cast<float>(changedTouches_.front().GetLocalLocation().GetX());
963 touchEvent.localY = static_cast<float>(changedTouches_.front().GetLocalLocation().GetY());
964 touchEvent.globalDisplayX = static_cast<double>(changedTouches_.front().GetGlobalDisplayLocation().GetX());
965 touchEvent.globalDisplayY = static_cast<double>(changedTouches_.front().GetGlobalDisplayLocation().GetY());
966 touchEvent.id = changedTouches_.front().GetFingerId();
967 touchEvent.force = changedTouches_.front().GetForce();
968 touchEvent.type = changedTouches_.front().GetTouchType();
969 touchEvent.tiltX = changedTouches_.front().GetTiltX();
970 touchEvent.tiltY = changedTouches_.front().GetTiltY();
971 touchEvent.rollAngle = changedTouches_.front().GetRollAngle();
972 touchEvent.width = changedTouches_.front().GetWidth();
973 touchEvent.height = changedTouches_.front().GetHeight();
974 touchEvent.pressedTime = changedTouches_.front().GetPressedTime();
975 const auto& targetLocalOffset = changedTouches_.front().GetTarget().area.GetOffset();
976 const auto& targetOrigin = changedTouches_.front().GetTarget().origin;
977 // width height x y globalx globaly
978 touchEvent.targetPositionX = targetLocalOffset.GetX().ConvertToPx();
979 touchEvent.targetPositionY = targetLocalOffset.GetY().ConvertToPx();
980 touchEvent.targetGlobalPositionX = targetOrigin.GetX().ConvertToPx() + targetLocalOffset.GetX().ConvertToPx();
981 touchEvent.targetGlobalPositionY = targetOrigin.GetY().ConvertToPx() + targetLocalOffset.GetY().ConvertToPx();
982 touchEvent.widthArea = changedTouches_.front().GetTarget().area.GetWidth().ConvertToPx();
983 touchEvent.heightArea = changedTouches_.front().GetTarget().area.GetHeight().ConvertToPx();
984 // deviceid
985 touchEvent.deviceId = changedTouches_.front().GetDeviceId();
986 // modifierkeystates
987 touchEvent.modifierKeyState = CalculateModifierKeyState(changedTouches_.front().GetPressedKeyCodes());
988 }
989 touchEvent.time = timeStamp_;
990 return touchEvent;
991 }
992
GetEmbedId() const993 const std::string& NativeEmbeadTouchInfo::GetEmbedId() const
994 {
995 return embedId_;
996 }
997
GetTouchEventInfo() const998 const TouchEventInfo& NativeEmbeadTouchInfo::GetTouchEventInfo() const
999 {
1000 return touchEvent_;
1001 }
GetResult() const1002 const RefPtr<GestureEventResult>& NativeEmbeadTouchInfo::GetResult() const
1003 {
1004 return result_;
1005 }
1006
1007 } // namespace OHOS::Ace
1008