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