MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
GlfwSingleton.cpp
Go to the documentation of this file.
1#include "GlfwSingleton.hpp"
3
4#include "GLFW/glfw3.h"
5
6namespace MayaFlux::Core {
7
10std::function<void(int, const char*)> GLFWSingleton::s_error_callback;
11
13
14GlfwPreInitConfig GLFWSingleton::s_preinit_config {};
15
17{
18 if (s_initialized) {
20 "GLFWSingleton::configure() called after GLFW was initialized — pre-init hints will be ignored");
21 return;
22 }
23
25 int glfw_platform = GLFW_ANY_PLATFORM;
26 switch (config.platform) {
28 glfw_platform = GLFW_PLATFORM_WAYLAND;
29 break;
31 glfw_platform = GLFW_PLATFORM_X11;
32 break;
33 default:
34 break;
35 }
36 glfwInitHint(GLFW_PLATFORM, glfw_platform);
37 }
38
39 glfwInitHint(GLFW_WAYLAND_LIBDECOR, config.disable_libdecor ? GLFW_FALSE : GLFW_TRUE);
40 glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, config.cocoa_chdir_resources ? GLFW_TRUE : GLFW_FALSE);
41 glfwInitHint(GLFW_COCOA_MENUBAR, config.cocoa_menubar ? GLFW_TRUE : GLFW_FALSE);
42
43 s_configured = true;
44
46 "GLFW pre-initialization configured: platform={}, libdecor={}, cocoa_chdir_resources={}, cocoa_menubar={}",
48 ? "default"
50 ? "wayland"
51 : "x11"),
52 config.disable_libdecor ? "disabled" : "enabled",
53 config.cocoa_chdir_resources ? "enabled" : "disabled",
54 config.cocoa_menubar ? "enabled" : "disabled");
55
56 s_preinit_config = config;
57}
58
60{
61 if (s_initialized)
62 return true;
63
64 if (!s_configured) {
66 "GLFWSingleton::initialize() called without prior configure() — using default pre-init hints");
67
69 }
70
71 glfwSetErrorCallback([](int error, const char* description) {
72 MF_ERROR(Journal::Component::Core, Journal::Context::WindowingSubsystem, "GLFW Error {}: {}", error, description);
73 });
74
75 if (!glfwInit()) {
77 return false;
78 }
79
80 s_initialized = true;
82 return true;
83}
84
86{
87 if (s_initialized && s_window_count == 0) {
88 glfwTerminate();
89 s_initialized = false;
90 }
91}
92
93std::vector<MonitorInfo> GLFWSingleton::enumerate_monitors()
94{
95 if (!s_initialized)
96 return {};
97
98 int count {};
99 GLFWmonitor** monitors = glfwGetMonitors(&count);
100
101 std::span<GLFWmonitor*> monitor_span(monitors, count);
102
103 std::vector<MonitorInfo> infos;
104 infos.reserve(count);
105
106 GLFWmonitor* primary = glfwGetPrimaryMonitor();
107
108 for (int i = 0; i < count; ++i) {
109 auto* mode = glfwGetVideoMode(monitor_span[i]);
110 int w_mm {}, h_mm {};
111 glfwGetMonitorPhysicalSize(monitor_span[i], &w_mm, &h_mm);
112
113 infos.push_back({ .id = i,
114 .name = glfwGetMonitorName(monitor_span[i]),
115 .width_mm = w_mm,
116 .height_mm = h_mm,
117 .current_mode = {
118 .width = static_cast<uint32_t>(mode->width),
119 .height = static_cast<uint32_t>(mode->height),
120 .refresh_rate = static_cast<uint32_t>(mode->refreshRate),
121 .red_bits = static_cast<uint8_t>(mode->redBits),
122 .green_bits = static_cast<uint8_t>(mode->greenBits),
123 .blue_bits = static_cast<uint8_t>(mode->blueBits) },
124 .is_primary = (monitor_span[i] == primary) });
125 }
126
127 return infos;
128}
129
131{
132 auto monitors = enumerate_monitors();
133 for (const auto& m : monitors) {
134 if (m.is_primary)
135 return m;
136 }
137 return {};
138}
139
141{
142 if (!s_initialized)
143 return "";
144
145#if GLFW_VERSION_MAJOR >= 3 && GLFW_VERSION_MINOR >= 4
146 int platform = glfwGetPlatform();
147 switch (platform) {
148 case GLFW_PLATFORM_WAYLAND:
149 return "wayland";
150 case GLFW_PLATFORM_X11:
151 return "x11";
152 case GLFW_PLATFORM_WIN32:
153 return "win32";
154 case GLFW_PLATFORM_COCOA:
155 return "cocoa";
156 default:
157 return "unknown";
158 }
159#else
160 const char* platform = glfwGetPlatformName();
161 if (platform)
162 return platform;
163
164#ifdef MAYAFLUX_PLATFORM_LINUX
165#ifdef GLFW_USE_WAYLAND
166 return "wayland";
167#else
168 return "x11";
169#endif
170#elif MAYAFLUX_PLATFORM_WINDOWS
171 return "win32";
172#elif MAYAFLUX_PLATFORM_MACOS
173 return "cocoa";
174#else
175 return "unknown";
176#endif
177#endif
178}
179
181{
182 return get_platform() == "wayland";
183}
184
186{
187 auto monitors = enumerate_monitors();
188 if (id >= 0 && static_cast<size_t>(id) < monitors.size()) {
189 return monitors[id];
190 }
191 return {};
192}
193
194void GLFWSingleton::set_error_callback(std::function<void(int, const char*)> callback)
195{
196 s_error_callback = std::move(callback);
197}
198
200{
201 if (!initialize()) {
203 std::source_location::current(),
204 "GLFW must be initialized before querying required instance extensions");
205 }
206
207 uint32_t count = 0;
208 const char** extensions = glfwGetRequiredInstanceExtensions(&count);
209 if (!extensions || count == 0) {
211 "No required instance extensions reported by GLFW");
212 return {};
213 }
214
215 return { extensions, extensions + count };
216}
217
218}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
static std::string get_platform()
Gets the current GLFW platform (Wayland, X11, etc.)
static uint32_t s_window_count
Number of currently active GLFW windows.
static void set_error_callback(std::function< void(int, const char *)> callback)
Sets a custom error callback for GLFW errors.
static bool initialize()
Initializes the GLFW library if not already initialized.
static void terminate()
Terminates the GLFW library if initialized and no windows remain.
static std::vector< const char * > get_required_instance_extensions()
Retrieves the list of required Vulkan instance extensions for GLFW.
static GlfwPreInitConfig s_preinit_config
static std::function< void(int, const char *)> s_error_callback
Internal GLFW error callback that forwards to the user-defined callback if set.
static MonitorInfo get_monitor(int32_t id)
Retrieves information about a specific monitor by ID.
static void configure(const GlfwPreInitConfig &config)
Configures GLFW with pre-initialization hints.
static MonitorInfo get_primary_monitor()
Retrieves information about the primary monitor.
static std::vector< MonitorInfo > enumerate_monitors()
Enumerates all connected monitors and their information.
static bool is_wayland()
Checks if running on Wayland.
static bool s_initialized
Tracks whether GLFW has been initialized.
@ WindowingSubsystem
Windowing system operations (GLFW, SDL)
@ Core
Core engine, backend, subsystems.
enum MayaFlux::Core::GlfwPreInitConfig::Platform platform
bool disable_libdecor
this prevents crash on some wayland compositors
Configuration hints for GLFW initialization.
Information about a physical display.