• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "adapter/preview/entrance/event_dispatcher.h"
22 
23 namespace OHOS::Ace::Platform {
24 namespace {
25 std::mutex lastMouseMutex;
26 double lastMouseX = 0;
27 double lastMouseY = 0;
28 bool lastMousePressed = false;
29 } // namespace
30 
CreateMockTouchEvent(const TouchType & type)31 struct TouchEvent CreateMockTouchEvent(const TouchType &type)
32 {
33     std::lock_guard lock(lastMouseMutex);
34     return {
35         .id = 1,
36         .x = lastMouseX,
37         .y = lastMouseY,
38         .screenX = 0,
39         .screenY = 0,
40         .type = type,
41         .time = std::chrono::high_resolution_clock::now(),
42         .size = 96.0,
43         .force = 0,
44         .deviceId = 0,
45         .sourceType = SourceType::TOUCH,
46         .pointers = {},
47     };
48 }
49 
HandleMouseButton(int button,bool pressed,int mods)50 void HandleMouseButton(int button, bool pressed, int mods)
51 {
52     {
53         std::lock_guard lock(lastMouseMutex);
54         lastMousePressed = pressed;
55     }
56 
57     auto event = CreateMockTouchEvent(pressed ? TouchType::DOWN : TouchType::UP);
58     EventDispatcher::GetInstance().DispatchTouchEvent(event);
59 }
60 
HandleCursorPos(double x,double y)61 void HandleCursorPos(double x, double y) {
62     {
63         std::lock_guard lock(lastMouseMutex);
64         lastMouseX = x;
65         lastMouseY = y;
66     }
67 
68     if (lastMousePressed) {
69         auto event = CreateMockTouchEvent(TouchType::MOVE);
70         EventDispatcher::GetInstance().DispatchTouchEvent(event);
71     }
72 }
73 
InitialTouchEventCallback(const GlfwController & controller)74 void TouchEventHandler::InitialTouchEventCallback(const GlfwController &controller)
75 {
76     controller->OnMouseButton(HandleMouseButton);
77     controller->OnCursorPos(HandleCursorPos);
78 }
79 
80 } // namespace OHOS::Ace::Platform
81