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