-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwindow_manager_c.cpp
More file actions
320 lines (268 loc) · 8.75 KB
/
window_manager_c.cpp
File metadata and controls
320 lines (268 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "window_manager_c.h"
#include <memory>
#include <mutex>
#include <optional>
#include <unordered_map>
#include <vector>
#include "../window_manager.h"
using namespace nativeapi;
// Global state for event callbacks
struct WindowEventCallbackInfo {
native_window_event_callback_t callback;
void* user_data;
int id;
};
static std::mutex g_window_callback_mutex;
static std::unordered_map<int, WindowEventCallbackInfo> g_window_event_callbacks;
static int g_window_next_callback_id = 1;
// Helper function to create native_window_t from shared_ptr<Window>
static native_window_t CreateNativeWindowHandle(std::shared_ptr<Window> window) {
if (!window)
return nullptr;
// Cast shared_ptr to void* - the WindowManager maintains the actual
// shared_ptr
return static_cast<void*>(window.get());
}
// Helper function to dispatch events to registered callbacks
static void DispatchEvent(const native_window_event_t& event) {
std::lock_guard<std::mutex> lock(g_window_callback_mutex);
for (const auto& [id, callback_info] : g_window_event_callbacks) {
try {
callback_info.callback(&event, callback_info.user_data);
} catch (...) {
// Ignore exceptions from callbacks
}
}
}
// Event listener class to bridge C++ events to C callbacks
class WindowCEventListener {
public:
WindowCEventListener() {
auto& manager = WindowManager::GetInstance();
// Register for various window events
manager.AddListener<WindowFocusedEvent>([this](const WindowFocusedEvent& e) {
native_window_event_t event;
event.type = NATIVE_WINDOW_EVENT_FOCUSED;
event.window_id = e.GetWindowId();
DispatchEvent(event);
});
manager.AddListener<WindowBlurredEvent>([this](const WindowBlurredEvent& e) {
native_window_event_t event;
event.type = NATIVE_WINDOW_EVENT_BLURRED;
event.window_id = e.GetWindowId();
DispatchEvent(event);
});
manager.AddListener<WindowMinimizedEvent>([this](const WindowMinimizedEvent& e) {
native_window_event_t event;
event.type = NATIVE_WINDOW_EVENT_MINIMIZED;
event.window_id = e.GetWindowId();
DispatchEvent(event);
});
manager.AddListener<WindowMaximizedEvent>([this](const WindowMaximizedEvent& e) {
native_window_event_t event;
event.type = NATIVE_WINDOW_EVENT_MAXIMIZED;
event.window_id = e.GetWindowId();
DispatchEvent(event);
});
manager.AddListener<WindowRestoredEvent>([this](const WindowRestoredEvent& e) {
native_window_event_t event;
event.type = NATIVE_WINDOW_EVENT_RESTORED;
event.window_id = e.GetWindowId();
DispatchEvent(event);
});
manager.AddListener<WindowMovedEvent>([this](const WindowMovedEvent& e) {
native_window_event_t event;
event.type = NATIVE_WINDOW_EVENT_MOVED;
event.window_id = e.GetWindowId();
event.data.moved.position.x = e.GetNewPosition().x;
event.data.moved.position.y = e.GetNewPosition().y;
DispatchEvent(event);
});
manager.AddListener<WindowResizedEvent>([this](const WindowResizedEvent& e) {
native_window_event_t event;
event.type = NATIVE_WINDOW_EVENT_RESIZED;
event.window_id = e.GetWindowId();
event.data.resized.size.width = e.GetNewSize().width;
event.data.resized.size.height = e.GetNewSize().height;
DispatchEvent(event);
});
}
};
static std::unique_ptr<WindowCEventListener> g_event_listener;
// Hook callbacks (C API)
static std::mutex g_hook_mutex;
static native_window_will_show_callback_t g_will_show_cb = nullptr;
static void* g_will_show_ud = nullptr;
static native_window_will_hide_callback_t g_will_hide_cb = nullptr;
static void* g_will_hide_ud = nullptr;
// Window manager operations
FFI_PLUGIN_EXPORT
native_window_t native_window_manager_get(native_window_id_t window_id) {
try {
auto& manager = WindowManager::GetInstance();
auto window = manager.Get(window_id);
return CreateNativeWindowHandle(window);
} catch (...) {
return nullptr;
}
}
FFI_PLUGIN_EXPORT
native_window_list_t native_window_manager_get_all(void) {
native_window_list_t result = {nullptr, 0};
try {
auto& manager = WindowManager::GetInstance();
auto windows = manager.GetAll();
if (windows.empty()) {
return result;
}
result.windows = new (std::nothrow) native_window_t[windows.size()];
if (!result.windows) {
return result;
}
result.count = static_cast<long>(windows.size());
for (size_t i = 0; i < windows.size(); ++i) {
result.windows[i] = CreateNativeWindowHandle(windows[i]);
}
return result;
} catch (...) {
if (result.windows) {
delete[] result.windows;
result.windows = nullptr;
result.count = 0;
}
return result;
}
}
FFI_PLUGIN_EXPORT
native_window_t native_window_manager_get_current(void) {
try {
auto& manager = WindowManager::GetInstance();
auto current_window = manager.GetCurrent();
return CreateNativeWindowHandle(current_window);
} catch (...) {
return nullptr;
}
}
FFI_PLUGIN_EXPORT
int native_window_manager_register_event_callback(native_window_event_callback_t callback,
void* user_data) {
if (!callback)
return -1;
std::lock_guard<std::mutex> lock(g_window_callback_mutex);
int callback_id = g_window_next_callback_id++;
WindowEventCallbackInfo info;
info.callback = callback;
info.user_data = user_data;
info.id = callback_id;
g_window_event_callbacks[callback_id] = info;
// Initialize event listener if this is the first callback
if (!g_event_listener) {
try {
g_event_listener = std::make_unique<WindowCEventListener>();
} catch (...) {
g_window_event_callbacks.erase(callback_id);
return -1;
}
}
return callback_id;
}
FFI_PLUGIN_EXPORT
bool native_window_manager_unregister_event_callback(int registration_id) {
std::lock_guard<std::mutex> lock(g_window_callback_mutex);
auto it = g_window_event_callbacks.find(registration_id);
if (it == g_window_event_callbacks.end()) {
return false;
}
g_window_event_callbacks.erase(it);
// Clean up event listener if no callbacks remain
if (g_window_event_callbacks.empty()) {
g_event_listener.reset();
}
return true;
}
FFI_PLUGIN_EXPORT
void native_window_manager_shutdown(void) {
std::lock_guard<std::mutex> lock(g_window_callback_mutex);
// Clear all callbacks
g_window_event_callbacks.clear();
// Clean up event listener
g_event_listener.reset();
// Note: We don't explicitly destroy the WindowManager singleton
// as it will be cleaned up automatically when the application exits
}
FFI_PLUGIN_EXPORT
void native_window_manager_set_will_show_hook(native_window_will_show_callback_t callback,
void* user_data) {
std::lock_guard<std::mutex> lock(g_hook_mutex);
g_will_show_cb = callback;
g_will_show_ud = user_data;
auto& manager = WindowManager::GetInstance();
if (callback == nullptr) {
manager.SetWillShowHook(std::nullopt);
return;
}
// Bridge C callback through C++ hook
manager.SetWillShowHook([cb = callback, ud = user_data](WindowId id) {
try {
cb(id, ud);
} catch (...) {
// Swallow exceptions to avoid unwinding across API boundary
}
});
}
FFI_PLUGIN_EXPORT
void native_window_manager_set_will_hide_hook(native_window_will_hide_callback_t callback,
void* user_data) {
std::lock_guard<std::mutex> lock(g_hook_mutex);
g_will_hide_cb = callback;
g_will_hide_ud = user_data;
auto& manager = WindowManager::GetInstance();
if (callback == nullptr) {
manager.SetWillHideHook(std::nullopt);
return;
}
// Bridge C callback through C++ hook
manager.SetWillHideHook([cb = callback, ud = user_data](WindowId id) {
try {
cb(id, ud);
} catch (...) {
// Swallow exceptions to avoid unwinding across API boundary
}
});
}
FFI_PLUGIN_EXPORT
bool native_window_manager_has_will_show_hook(void) {
try {
auto& manager = WindowManager::GetInstance();
return manager.HasWillShowHook();
} catch (...) {
return false;
}
}
FFI_PLUGIN_EXPORT
bool native_window_manager_has_will_hide_hook(void) {
try {
auto& manager = WindowManager::GetInstance();
return manager.HasWillHideHook();
} catch (...) {
return false;
}
}
FFI_PLUGIN_EXPORT
bool native_window_manager_call_original_show(native_window_id_t window_id) {
try {
auto& manager = WindowManager::GetInstance();
return manager.CallOriginalShow(window_id);
} catch (...) {
return false;
}
}
FFI_PLUGIN_EXPORT
bool native_window_manager_call_original_hide(native_window_id_t window_id) {
try {
auto& manager = WindowManager::GetInstance();
return manager.CallOriginalHide(window_id);
} catch (...) {
return false;
}
}