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 "ace_extra_input_data.h"
17 
18 #include <memory>
19 
20 #include "pointer_event.h"
21 
22 #define CHECK_FALSE_RETURN CHECK_NULL_RETURN
23 
24 namespace OHOS::Ace {
WriteToInput(const std::shared_ptr<MMI::InputEvent> & inputEvent,AceExtraInputData & data)25 void AceExtraInputData::WriteToInput(const std::shared_ptr<MMI::InputEvent>& inputEvent, AceExtraInputData& data)
26 {
27     std::shared_ptr<uint8_t[]> dst(new uint8_t[sizeof(AceExtraInputData)], [](uint8_t* ptr) { delete[] ptr; });
28     auto* src = reinterpret_cast<uint8_t*>(&data);
29     std::copy(src, src + sizeof(AceExtraInputData), dst.get());
30     inputEvent->SetExtraData(dst, sizeof(AceExtraInputData));
31 }
32 
ReadToTouchEvent(const std::shared_ptr<MMI::InputEvent> & inputEvent,TouchEvent & event)33 void AceExtraInputData::ReadToTouchEvent(const std::shared_ptr<MMI::InputEvent>& inputEvent, TouchEvent& event)
34 {
35     std::shared_ptr<const uint8_t[]> raw;
36     uint32_t length = 0;
37     inputEvent->GetExtraData(raw, length);
38     if (length == 0 || !raw) {
39         return;
40     }
41 
42     const AceExtraInputData* data = reinterpret_cast<const AceExtraInputData*>(raw.get());
43     event.x = data->interpolatedX;
44     event.y = data->interpolatedY;
45     event.screenX = data->interpolatedDisplayX;
46     event.screenY = data->interpolatedDisplayY;
47     uint64_t stampCnt = data->msSinceEpoch;
48     auto msSinceEpoch = std::chrono::milliseconds(stampCnt);
49     std::chrono::high_resolution_clock::time_point timeStamp(msSinceEpoch);
50     event.time = timeStamp;
51 }
52 
InsertInterpolatePoints(const TouchEventInfo & info)53 void AceExtraInputData::InsertInterpolatePoints(const TouchEventInfo& info)
54 {
55     const auto pointerEvent = info.GetPointerEvent();
56     MMI::PointerEvent::PointerItem item;
57     if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), item)) {
58         return;
59     }
60 
61     const auto changedInfo = info.GetChangedTouches();
62     if (changedInfo.empty()) {
63         return;
64     }
65     const auto& point = changedInfo.front();
66     const Offset& location = point.GetLocalLocation();
67     const auto& screenLocation = point.GetScreenLocation();
68     AceExtraInputData aceData(
69         location.GetX(), location.GetY(), screenLocation.GetX(), screenLocation.GetY(), info.GetTimeStamp());
70     aceData.WriteToInput(pointerEvent, aceData);
71 }
72 } // namespace OHOS::Ace
73