• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_event_handler.h"
17 
18 #include <vector>
19 #include "base/log/log.h"
20 #include "core/event/touch_event.h"
21 #include "flutter/shell/platform/glfw/public/flutter_glfw.h"
22 #include "flutter/shell/platform/embedder/embedder.h"
23 #include "core/pipeline/layers/flutter_scene_builder.h"
24 #include "adapter/preview/entrance/event_dispatcher.h"
25 
26 namespace OHOS::Ace::Platform {
27 namespace {
28 
ConvertTouchPoint(flutter::PointerData * pointerItem)29 TouchPoint ConvertTouchPoint(flutter::PointerData* pointerItem)
30 {
31     TouchPoint touchPoint;
32     // just get the max of width and height
33     touchPoint.size = pointerItem->size;
34     touchPoint.id = pointerItem->device;
35     touchPoint.force = pointerItem->pressure;
36     touchPoint.x = pointerItem->physical_x;
37     touchPoint.y = pointerItem->physical_y;
38     touchPoint.screenX = pointerItem->physical_x;
39     touchPoint.screenY = pointerItem->physical_y;
40     return touchPoint;
41 }
42 
ConvertTouchEvent(const std::vector<uint8_t> & data,std::vector<TouchEvent> & events)43 void ConvertTouchEvent(const std::vector<uint8_t>& data, std::vector<TouchEvent>& events)
44 {
45     constexpr int32_t DEFAULT_ACTION_ID = 0;
46     const auto* origin = reinterpret_cast<const flutter::PointerData*>(data.data());
47     size_t size = data.size() / sizeof(flutter::PointerData);
48     auto current = const_cast<flutter::PointerData*>(origin);
49     auto end = current + size;
50 
51     while (current < end) {
52         std::chrono::microseconds micros(current->time_stamp);
53         TimeStamp time(micros);
54         TouchEvent point {
55             static_cast<int32_t>(DEFAULT_ACTION_ID), static_cast<float>(current->physical_x),
56             static_cast<float>(current->physical_y), static_cast<float>(current->physical_x),
57             static_cast<float>(current->physical_y), TouchType::UNKNOWN, time, current->size,
58             static_cast<float>(current->pressure), static_cast<int64_t>(current->device)
59         };
60         point.pointers.emplace_back(ConvertTouchPoint(current));
61         switch (current->change) {
62             case flutter::PointerData::Change::kCancel:
63                 point.type = TouchType::CANCEL;
64                 events.push_back(point);
65                 break;
66             case flutter::PointerData::Change::kAdd:
67             case flutter::PointerData::Change::kRemove:
68             case flutter::PointerData::Change::kHover:
69                 break;
70             case flutter::PointerData::Change::kDown:
71                 point.type = TouchType::DOWN;
72                 events.push_back(point);
73                 break;
74             case flutter::PointerData::Change::kMove:
75                 point.type = TouchType::MOVE;
76                 events.push_back(point);
77                 break;
78             case flutter::PointerData::Change::kUp:
79                 point.type = TouchType::UP;
80                 events.push_back(point);
81                 break;
82         }
83         current++;
84     }
85 }
86 
HandleTouchEvent(const std::vector<uint8_t> & data)87 bool HandleTouchEvent(const std::vector<uint8_t>& data)
88 {
89     std::vector<TouchEvent> touchEvents;
90     ConvertTouchEvent(data, touchEvents);
91     for (const auto& event : touchEvents) {
92         EventDispatcher::GetInstance().DispatchTouchEvent(event);
93     }
94     return true;
95 }
96 
97 }
98 
InitialTouchEventCallback(const GlfwController & controller)99 void TouchEventHandler::InitialTouchEventCallback(const GlfwController &controller)
100 {
101     LOGI("Initial callback function of touch event.");
102 #ifdef USE_GLFW_WINDOW
103     auto touchEventCallback = [](std::unique_ptr<flutter::PointerDataPacket>& packet)->bool {
104         return packet && HandleTouchEvent(packet->data());
105     };
106     FlutterEngineRegisterHandleTouchEventCallback(std::move(touchEventCallback));
107 #endif
108 }
109 
110 } // namespace OHOS::Ace::Platform
111