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 #ifndef GLFW_RENDER_CONTEXT_EXPORT_GLFW_RENDER_CONTEXT
17 #define GLFW_RENDER_CONTEXT_EXPORT_GLFW_RENDER_CONTEXT
18 
19 #include <functional>
20 #include <memory>
21 #include <string>
22 
23 struct GLFWwindow;
24 namespace OHOS::Rosen {
25 class GlfwRenderContext {
26 public:
27     using OnMouseButtonFunc = std::function<void(int button, bool pressed, int mods)>;
28     using OnCursorPosFunc = std::function<void(double x, double y)>;
29     using OnKeyFunc = std::function<void(int key, int scancode, int action, int mods)>;
30     using OnCharFunc = std::function<void(unsigned int codepoint)>;
31     using OnSizeChangedFunc = std::function<void(int32_t width, int32_t height)>;
32 
33     // GlfwRenderContext isn't a singleton.
34     static std::shared_ptr<GlfwRenderContext> GetGlobal();
35 
36     /* before CreateWindow */
37     int Init();
38     void InitFrom(void *glfwWindow);
39     void Terminate();
40 
41     /* before window operation */
42     int CreateGlfwWindow(int32_t width, int32_t height, bool visible);
43     void DestroyWindow();
44 
45     /* window operation */
46     int WindowShouldClose();
47     void WaitForEvents();
48     void PollEvents();
49     void GetWindowSize(int32_t &width, int32_t &height);
50     void SetWindowSize(int32_t width, int32_t height);
51     void SetWindowTitle(const std::string &title);
52     std::string GetClipboardData();
53     void SetClipboardData(const std::string &data);
54 
55     /* gl operation */
56     void MakeCurrent();
57     void SwapBuffers();
58 
59     /* input event */
60     void OnMouseButton(const OnMouseButtonFunc &onMouseBotton);
61     void OnCursorPos(const OnCursorPosFunc &onCursorPos);
62     void OnKey(const OnKeyFunc &onKey);
63     void OnChar(const OnCharFunc &onChar);
64     void OnSizeChanged(const OnSizeChangedFunc &onSizeChanged);
65 
66 private:
67     static void OnMouseButton(GLFWwindow *window, int button, int action, int mods);
68     static void OnCursorPos(GLFWwindow *window, double x, double y);
69     static void OnKey(GLFWwindow *window, int key, int scancode, int action, int mods);
70     static void OnChar(GLFWwindow *window, unsigned int codepoint);
71     static void OnSizeChanged(GLFWwindow *window, int32_t width, int32_t height);
72 
73     static inline std::shared_ptr<GlfwRenderContext> global_ = nullptr;
74     bool external_ = false;
75     GLFWwindow *window_ = nullptr;
76     OnMouseButtonFunc onMouseBotton_ = nullptr;
77     OnCursorPosFunc onCursorPos_ = nullptr;
78     OnKeyFunc onKey_ = nullptr;
79     OnCharFunc onChar_ = nullptr;
80     OnSizeChangedFunc onSizeChanged_ = nullptr;
81 
82     int32_t width_ = 0;
83     int32_t height_ = 0;
84 };
85 } // namespace OHOS::Rosen
86 
87 #endif // GLFW_RENDER_CONTEXT_EXPORT_GLFW_RENDER_CONTEXT
88