MayaFlux 0.4.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#ifdef GLFW_BACKEND
5
6#include "GLFW/glfw3.h"
7
8namespace MayaFlux::Core {
9
10bool GLFWSingleton::s_initialized {};
11uint32_t GLFWSingleton::s_window_count {};
12std::function<void(int, const char*)> GLFWSingleton::s_error_callback;
13
14bool GLFWSingleton::s_configured {};
15
16GlfwPreInitConfig GLFWSingleton::s_preinit_config {};
17
18void GLFWSingleton::configure(const GlfwPreInitConfig& config)
19{
20 if (s_initialized) {
22 "GLFWSingleton::configure() called after GLFW was initialized — pre-init hints will be ignored");
23 return;
24 }
25
26 glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, config.cocoa_chdir_resources ? GLFW_TRUE : GLFW_FALSE);
27 glfwInitHint(GLFW_COCOA_MENUBAR, config.cocoa_menubar ? GLFW_TRUE : GLFW_FALSE);
28
29 s_configured = true;
30 s_preinit_config = config;
31
33 "GLFW pre-initialization configured: cocoa_chdir_resources={}, cocoa_menubar={}",
34 config.cocoa_chdir_resources ? "enabled" : "disabled",
35 config.cocoa_menubar ? "enabled" : "disabled");
36}
37
38bool GLFWSingleton::initialize()
39{
40 if (s_initialized)
41 return true;
42
43 if (!s_configured) {
45 "GLFWSingleton::initialize() called without prior configure() — using default pre-init hints");
46
47 configure(s_preinit_config);
48 }
49
50 glfwSetErrorCallback([](int error, const char* description) {
51 MF_ERROR(Journal::Component::Core, Journal::Context::WindowingSubsystem, "GLFW Error {}: {}", error, description);
52 });
53
54 if (!glfwInit()) {
56 return false;
57 }
58
59 s_initialized = true;
60 s_window_count = 0;
61 return true;
62}
63
64void GLFWSingleton::terminate()
65{
66 if (s_initialized && s_window_count == 0) {
67 glfwTerminate();
68 s_initialized = false;
69 }
70}
71
72std::vector<MonitorInfo> GLFWSingleton::enumerate_monitors()
73{
74 if (!s_initialized)
75 return {};
76
77 int count {};
78 GLFWmonitor** monitors = glfwGetMonitors(&count);
79
80 std::span<GLFWmonitor*> monitor_span(monitors, count);
81
82 std::vector<MonitorInfo> infos;
83 infos.reserve(count);
84
85 GLFWmonitor* primary = glfwGetPrimaryMonitor();
86
87 for (int i = 0; i < count; ++i) {
88 auto* mode = glfwGetVideoMode(monitor_span[i]);
89 int w_mm {}, h_mm {};
90 glfwGetMonitorPhysicalSize(monitor_span[i], &w_mm, &h_mm);
91
92 infos.push_back({ .id = i,
93 .name = glfwGetMonitorName(monitor_span[i]),
94 .width_mm = w_mm,
95 .height_mm = h_mm,
96 .current_mode = {
97 .width = static_cast<uint32_t>(mode->width),
98 .height = static_cast<uint32_t>(mode->height),
99 .refresh_rate = static_cast<uint32_t>(mode->refreshRate),
100 .red_bits = static_cast<uint8_t>(mode->redBits),
101 .green_bits = static_cast<uint8_t>(mode->greenBits),
102 .blue_bits = static_cast<uint8_t>(mode->blueBits) },
103 .is_primary = (monitor_span[i] == primary) });
104 }
105
106 return infos;
107}
108
109MonitorInfo GLFWSingleton::get_primary_monitor()
110{
111 auto monitors = enumerate_monitors();
112 for (const auto& m : monitors) {
113 if (m.is_primary)
114 return m;
115 }
116 return {};
117}
118
119std::string GLFWSingleton::get_platform()
120{
121 if (!s_initialized)
122 return "";
123
124 return "cocoa";
125}
126
127MonitorInfo GLFWSingleton::get_monitor(int32_t id)
128{
129 auto monitors = enumerate_monitors();
130 if (id >= 0 && static_cast<size_t>(id) < monitors.size()) {
131 return monitors[id];
132 }
133 return {};
134}
135
136void GLFWSingleton::set_error_callback(std::function<void(int, const char*)> callback)
137{
138 s_error_callback = std::move(callback);
139}
140
141std::vector<const char*> GLFWSingleton::get_required_instance_extensions()
142{
143 if (!initialize()) {
145 std::source_location::current(),
146 "GLFW must be initialized before querying required instance extensions");
147 }
148
149 uint32_t count = 0;
150 const char** extensions = glfwGetRequiredInstanceExtensions(&count);
151 if (!extensions || count == 0) {
153 "No required instance extensions reported by GLFW");
154 return {};
155 }
156
157 return { extensions, extensions + count };
158}
159
160}
161
162#endif // GLFW_BACKEND
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
size_t count
void initialize()
Definition main.cpp:11
@ WindowingSubsystem
Windowing system operations (GLFW, SDL)
@ Core
Core engine, backend, subsystems.
std::vector< double > mode(std::span< const double > data, size_t n_windows, uint32_t hop_size, uint32_t window_size)
Mode per window via tolerance-bucketed frequency count.
Definition Analysis.cpp:559