Skip to content

Commit 35abc5b

Browse files
committed
Refactor event callback management for C API
Renamed event callback structs, mutexes, and maps for shortcut and window managers to improve clarity and avoid naming conflicts. Updated related code to use new names and refactored event listener class names for consistency.
1 parent 9ea73b5 commit 35abc5b

File tree

6 files changed

+45
-51
lines changed

6 files changed

+45
-51
lines changed

examples/shortcut_c_example/main.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ int main(void) {
5858
printf("Global shortcuts are supported\n\n");
5959

6060
// Register event callback
61-
int event_callback_id =
62-
native_shortcut_manager_register_event_callback(on_shortcut_event, NULL);
61+
int event_callback_id = native_shortcut_manager_register_event_callback(on_shortcut_event, NULL);
6362
if (event_callback_id < 0) {
6463
printf("Failed to register event callback\n");
6564
return 1;
@@ -71,8 +70,8 @@ int main(void) {
7170
printf("Registering shortcuts...\n");
7271

7372
// Simple registration
74-
native_shortcut_t shortcut1 = native_shortcut_manager_register(
75-
"Ctrl+Shift+A", on_shortcut_activated, (void*)"Shortcut 1");
73+
native_shortcut_t shortcut1 =
74+
native_shortcut_manager_register("Ctrl+Shift+A", on_shortcut_activated, (void*)"Shortcut 1");
7675

7776
if (!shortcut1) {
7877
printf("Failed to register shortcut 1\n");
@@ -110,7 +109,7 @@ int main(void) {
110109
} else {
111110
printf("Registered shortcut 3: %s (scope: %s)\n", native_shortcut_get_accelerator(shortcut3),
112111
native_shortcut_get_scope(shortcut3) == NATIVE_SHORTCUT_SCOPE_GLOBAL ? "Global"
113-
: "Application");
112+
: "Application");
114113
}
115114

116115
printf("\n");
@@ -218,4 +217,3 @@ int main(void) {
218217
printf("\nDone!\n");
219218
return 0;
220219
}
221-

src/capi/shortcut_c.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,3 @@ void native_shortcut_invoke(native_shortcut_t shortcut) {
8080
return;
8181
GetShortcut(shortcut)->Invoke();
8282
}
83-

src/capi/shortcut_c.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,3 @@ void native_shortcut_invoke(native_shortcut_t shortcut);
112112
#ifdef __cplusplus
113113
}
114114
#endif
115-

src/capi/shortcut_manager_c.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ static std::mutex g_shortcut_callback_mutex;
1818
static std::unordered_map<native_shortcut_id_t, ShortcutCallbackInfo> g_shortcut_callbacks;
1919

2020
// Global state for event callbacks
21-
struct EventCallbackInfo {
21+
struct ShortcutEventCallbackInfo {
2222
native_shortcut_event_callback_t callback;
2323
void* user_data;
2424
int id;
2525
};
2626

27-
static std::mutex g_event_callback_mutex;
28-
static std::unordered_map<int, EventCallbackInfo> g_event_callbacks;
29-
static int g_next_callback_id = 1;
27+
static std::mutex g_shortcut_event_callback_mutex;
28+
static std::unordered_map<int, ShortcutEventCallbackInfo> g_shortcut_event_callbacks;
29+
static int g_shortcut_next_callback_id = 1;
3030

3131
// Helper function to create native_shortcut_t from shared_ptr<Shortcut>
3232
static native_shortcut_t CreateNativeShortcutHandle(std::shared_ptr<Shortcut> shortcut) {
@@ -38,9 +38,9 @@ static native_shortcut_t CreateNativeShortcutHandle(std::shared_ptr<Shortcut> sh
3838

3939
// Helper function to dispatch events to registered callbacks
4040
static void DispatchEvent(const native_shortcut_event_t& event) {
41-
std::lock_guard<std::mutex> lock(g_event_callback_mutex);
41+
std::lock_guard<std::mutex> lock(g_shortcut_event_callback_mutex);
4242

43-
for (const auto& [id, callback_info] : g_event_callbacks) {
43+
for (const auto& [id, callback_info] : g_shortcut_event_callbacks) {
4444
try {
4545
callback_info.callback(&event, callback_info.user_data);
4646
} catch (...) {
@@ -50,9 +50,9 @@ static void DispatchEvent(const native_shortcut_event_t& event) {
5050
}
5151

5252
// Event listener class to bridge C++ events to C callbacks
53-
class CEventListener {
53+
class ShortcutCEventListener {
5454
public:
55-
CEventListener() {
55+
ShortcutCEventListener() {
5656
auto& manager = ShortcutManager::GetInstance();
5757

5858
// Register for shortcut events
@@ -93,8 +93,8 @@ class CEventListener {
9393
};
9494

9595
// Singleton event listener
96-
static CEventListener* GetEventListener() {
97-
static CEventListener listener;
96+
static ShortcutCEventListener* GetEventListener() {
97+
static ShortcutCEventListener listener;
9898
return &listener;
9999
}
100100

@@ -162,7 +162,7 @@ native_shortcut_t native_shortcut_manager_register_with_options(
162162
cpp_options.accelerator = options->accelerator;
163163
cpp_options.description = options->description ? options->description : "";
164164
cpp_options.scope = options->scope == NATIVE_SHORTCUT_SCOPE_GLOBAL ? ShortcutScope::Global
165-
: ShortcutScope::Application;
165+
: ShortcutScope::Application;
166166
cpp_options.enabled = options->enabled;
167167

168168
// Placeholder callback - will be replaced below
@@ -320,23 +320,22 @@ int native_shortcut_manager_register_event_callback(native_shortcut_event_callba
320320
// Ensure event listener is initialized
321321
GetEventListener();
322322

323-
std::lock_guard<std::mutex> lock(g_event_callback_mutex);
323+
std::lock_guard<std::mutex> lock(g_shortcut_event_callback_mutex);
324324

325-
int id = g_next_callback_id++;
326-
g_event_callbacks[id] = {callback, user_data, id};
325+
int id = g_shortcut_next_callback_id++;
326+
g_shortcut_event_callbacks[id] = {callback, user_data, id};
327327

328328
return id;
329329
}
330330

331331
bool native_shortcut_manager_unregister_event_callback(int registration_id) {
332-
std::lock_guard<std::mutex> lock(g_event_callback_mutex);
332+
std::lock_guard<std::mutex> lock(g_shortcut_event_callback_mutex);
333333

334-
auto it = g_event_callbacks.find(registration_id);
335-
if (it == g_event_callbacks.end()) {
334+
auto it = g_shortcut_event_callbacks.find(registration_id);
335+
if (it == g_shortcut_event_callbacks.end()) {
336336
return false;
337337
}
338338

339-
g_event_callbacks.erase(it);
339+
g_shortcut_event_callbacks.erase(it);
340340
return true;
341341
}
342-

src/capi/shortcut_manager_c.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ typedef struct {
4444
* Shortcut event callback function type
4545
*/
4646
typedef void (*native_shortcut_event_callback_t)(const native_shortcut_event_t* event,
47-
void* user_data);
47+
void* user_data);
4848

4949
/**
5050
* Shortcut list structure
@@ -198,4 +198,3 @@ bool native_shortcut_manager_unregister_event_callback(int registration_id);
198198
#ifdef __cplusplus
199199
}
200200
#endif
201-

src/capi/window_manager_c.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
using namespace nativeapi;
1010

1111
// Global state for event callbacks
12-
struct EventCallbackInfo {
12+
struct WindowEventCallbackInfo {
1313
native_window_event_callback_t callback;
1414
void* user_data;
1515
int id;
1616
};
1717

18-
static std::mutex g_callback_mutex;
19-
static std::unordered_map<int, EventCallbackInfo> g_event_callbacks;
20-
static int g_next_callback_id = 1;
18+
static std::mutex g_window_callback_mutex;
19+
static std::unordered_map<int, WindowEventCallbackInfo> g_window_event_callbacks;
20+
static int g_window_next_callback_id = 1;
2121

2222
// Helper function to create native_window_t from shared_ptr<Window>
2323
static native_window_t CreateNativeWindowHandle(std::shared_ptr<Window> window) {
@@ -30,9 +30,9 @@ static native_window_t CreateNativeWindowHandle(std::shared_ptr<Window> window)
3030

3131
// Helper function to dispatch events to registered callbacks
3232
static void DispatchEvent(const native_window_event_t& event) {
33-
std::lock_guard<std::mutex> lock(g_callback_mutex);
33+
std::lock_guard<std::mutex> lock(g_window_callback_mutex);
3434

35-
for (const auto& [id, callback_info] : g_event_callbacks) {
35+
for (const auto& [id, callback_info] : g_window_event_callbacks) {
3636
try {
3737
callback_info.callback(&event, callback_info.user_data);
3838
} catch (...) {
@@ -42,9 +42,9 @@ static void DispatchEvent(const native_window_event_t& event) {
4242
}
4343

4444
// Event listener class to bridge C++ events to C callbacks
45-
class CEventListener {
45+
class WindowCEventListener {
4646
public:
47-
CEventListener() {
47+
WindowCEventListener() {
4848
auto& manager = WindowManager::GetInstance();
4949

5050
// Register for various window events
@@ -103,7 +103,7 @@ class CEventListener {
103103
}
104104
};
105105

106-
static std::unique_ptr<CEventListener> g_event_listener;
106+
static std::unique_ptr<WindowCEventListener> g_event_listener;
107107

108108
// Hook callbacks (C API)
109109
static std::mutex g_hook_mutex;
@@ -177,22 +177,22 @@ int native_window_manager_register_event_callback(native_window_event_callback_t
177177
if (!callback)
178178
return -1;
179179

180-
std::lock_guard<std::mutex> lock(g_callback_mutex);
180+
std::lock_guard<std::mutex> lock(g_window_callback_mutex);
181181

182-
int callback_id = g_next_callback_id++;
183-
EventCallbackInfo info;
182+
int callback_id = g_window_next_callback_id++;
183+
WindowEventCallbackInfo info;
184184
info.callback = callback;
185185
info.user_data = user_data;
186186
info.id = callback_id;
187187

188-
g_event_callbacks[callback_id] = info;
188+
g_window_event_callbacks[callback_id] = info;
189189

190190
// Initialize event listener if this is the first callback
191191
if (!g_event_listener) {
192192
try {
193-
g_event_listener = std::make_unique<CEventListener>();
193+
g_event_listener = std::make_unique<WindowCEventListener>();
194194
} catch (...) {
195-
g_event_callbacks.erase(callback_id);
195+
g_window_event_callbacks.erase(callback_id);
196196
return -1;
197197
}
198198
}
@@ -202,17 +202,17 @@ int native_window_manager_register_event_callback(native_window_event_callback_t
202202

203203
FFI_PLUGIN_EXPORT
204204
bool native_window_manager_unregister_event_callback(int registration_id) {
205-
std::lock_guard<std::mutex> lock(g_callback_mutex);
205+
std::lock_guard<std::mutex> lock(g_window_callback_mutex);
206206

207-
auto it = g_event_callbacks.find(registration_id);
208-
if (it == g_event_callbacks.end()) {
207+
auto it = g_window_event_callbacks.find(registration_id);
208+
if (it == g_window_event_callbacks.end()) {
209209
return false;
210210
}
211211

212-
g_event_callbacks.erase(it);
212+
g_window_event_callbacks.erase(it);
213213

214214
// Clean up event listener if no callbacks remain
215-
if (g_event_callbacks.empty()) {
215+
if (g_window_event_callbacks.empty()) {
216216
g_event_listener.reset();
217217
}
218218

@@ -221,10 +221,10 @@ bool native_window_manager_unregister_event_callback(int registration_id) {
221221

222222
FFI_PLUGIN_EXPORT
223223
void native_window_manager_shutdown(void) {
224-
std::lock_guard<std::mutex> lock(g_callback_mutex);
224+
std::lock_guard<std::mutex> lock(g_window_callback_mutex);
225225

226226
// Clear all callbacks
227-
g_event_callbacks.clear();
227+
g_window_event_callbacks.clear();
228228

229229
// Clean up event listener
230230
g_event_listener.reset();

0 commit comments

Comments
 (0)