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 #ifndef FOUNDATION_ACE_ADAPTER_PREVIEW_ENTRANCE_SAMPLES_EVENT_ADAPTER_H
17 #define FOUNDATION_ACE_ADAPTER_PREVIEW_ENTRANCE_SAMPLES_EVENT_ADAPTER_H
18 
19 #include <mutex>
20 
21 #include "glfw_render_context.h"
22 
23 #include "adapter/preview/external/multimodalinput/key_event.h"
24 #include "adapter/preview/external/multimodalinput/pointer_event.h"
25 #include "frameworks/base/utils/singleton.h"
26 
27 using OHOS::MMI::KeyAction;
28 using OHOS::MMI::KeyCode;
29 using OHOS::MMI::KeyEvent;
30 using OHOS::MMI::PointerEvent;
31 using OHOS::MMI::SourceTool;
32 using OHOS::MMI::SourceType;
33 using OHOS::MMI::TouchPoint;
34 using OHOS::MMI::TouchType;
35 using OHOS::Rosen::GlfwRenderContext;
36 
37 using MMIKeyEventCallback = std::function<void(const std::shared_ptr<KeyEvent>&)>;
38 using MMIPointerEventCallback = std::function<void(const std::shared_ptr<PointerEvent>&)>;
39 using InspectorCallback = std::function<void(void)>;
40 
41 namespace OHOS::Ace::Sample {
42 
43 class EventAdapter : public Singleton<EventAdapter> {
44     DECLARE_SINGLETON(EventAdapter);
45 
46 public:
47     void Initialize(std::shared_ptr<GlfwRenderContext>& glfwRenderContext);
48     bool RecognizeKeyEvent(int key, int action, int mods);
49     void RecognizePointerEvent(const TouchType type);
50     void RegisterKeyEventCallback(MMIKeyEventCallback&& callback);
51     void RegisterPointerEventCallback(MMIPointerEventCallback&& callback);
52     // Register the callback for inspector
53     void RegisterInspectorCallback(InspectorCallback&& callback);
54     // This function is used to perform specific operations, by recognize some special shortcut keys.
55     bool RunSpecialOperations(int key, int action, int mods);
56 
57 private:
58     std::shared_ptr<KeyEvent> keyEvent_;
59     std::shared_ptr<PointerEvent> pointerEvent_;
60     MMIKeyEventCallback keyEventCallback_;
61     MMIPointerEventCallback pointerEventCallback_;
62     InspectorCallback inspectorCallback_;
63 
64     // for mouse event
65     std::mutex mouseMutex_;
66     bool isMousePressed_ = false;
67     double posX_ = 0;
68     double posY_ = 0;
69 };
70 
71 } // namespace OHOS::Ace::Sample
72 
73 #endif // FOUNDATION_ACE_ADAPTER_PREVIEW_ENTRANCE_SAMPLES_EVENT_ADAPTER_H
74