Skip to content

Commit 5d6299e

Browse files
committed
Refactor tray icon C API to use allocated C strings
- Replace buffer-based title/tooltip getters with allocated string return - Add string_utils_c for C++ string to C string conversion and freeing - Update display_c to use string_utils_c for string return - Update example and headers for new tray icon string API
1 parent d017d99 commit 5d6299e

File tree

7 files changed

+203
-131
lines changed

7 files changed

+203
-131
lines changed

examples/window_c_example/main.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "../../src/capi/window_c.h"
1111
#include "../../src/capi/menu_c.h"
1212
#include "../../src/capi/run_example_app_c.h"
13+
#include "../../src/capi/string_utils_c.h"
1314

1415
// Global variables to store handles
1516
static native_window_t g_window = NULL;
@@ -331,10 +332,10 @@ int main() {
331332
native_tray_icon_id_t tray_id = native_tray_icon_get_id(g_tray_icon);
332333
printf("Tray ID: %ld\n", tray_id);
333334

334-
char title_buffer[256];
335-
int title_len = native_tray_icon_get_title(g_tray_icon, title_buffer, sizeof(title_buffer));
336-
if (title_len >= 0) {
337-
printf("Tray Title: %s\n", title_buffer);
335+
char* title = native_tray_icon_get_title(g_tray_icon);
336+
if (title) {
337+
printf("Tray Title: %s\n", title);
338+
free_c_str(title);
338339
}
339340

340341
// Create context menu

include/nativeapi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "../src/capi/keyboard_monitor_c.h"
2828
#include "../src/capi/menu_c.h"
2929
#include "../src/capi/run_example_app_c.h"
30+
#include "../src/capi/string_utils_c.h"
3031
#include "../src/capi/tray_icon_c.h"
3132
#include "../src/capi/tray_manager_c.h"
3233
#include "../src/capi/window_c.h"

src/capi/display_c.cpp

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,23 @@
22
#include <cstring>
33
#include <memory>
44
#include "../display.h"
5+
#include "string_utils_c.h"
56

67
using namespace nativeapi;
78

8-
// Helper to cast opaque handle to C++ Display pointer
9-
static inline Display* to_display(native_display_t handle) {
10-
return static_cast<Display*>(handle);
11-
}
12-
13-
// Helper function to safely copy C++ string to C string
14-
static char* copy_string(const std::string& str) {
15-
if (str.empty())
16-
return nullptr;
17-
18-
size_t len = str.length() + 1;
19-
char* result = new (std::nothrow) char[len];
20-
if (result) {
21-
std::strcpy(result, str.c_str());
22-
}
23-
return result;
24-
}
25-
269
// Basic identification getters
2710
FFI_PLUGIN_EXPORT
2811
char* native_display_get_id(native_display_t display) {
2912
if (!display)
3013
return nullptr;
31-
return copy_string(to_display(display)->GetId());
14+
return to_c_str(static_cast<Display*>(display)->GetId());
3215
}
3316

3417
FFI_PLUGIN_EXPORT
3518
char* native_display_get_name(native_display_t display) {
3619
if (!display)
3720
return nullptr;
38-
return copy_string(to_display(display)->GetName());
21+
return to_c_str(static_cast<Display*>(display)->GetName());
3922
}
4023

4124
// Physical properties getters
@@ -45,7 +28,7 @@ native_point_t native_display_get_position(native_display_t display) {
4528
if (!display)
4629
return result;
4730

48-
auto pos = to_display(display)->GetPosition();
31+
auto pos = static_cast<Display*>(display)->GetPosition();
4932
result.x = pos.x;
5033
result.y = pos.y;
5134
return result;
@@ -57,7 +40,7 @@ native_size_t native_display_get_size(native_display_t display) {
5740
if (!display)
5841
return result;
5942

60-
auto size = to_display(display)->GetSize();
43+
auto size = static_cast<Display*>(display)->GetSize();
6144
result.width = size.width;
6245
result.height = size.height;
6346
return result;
@@ -69,7 +52,7 @@ native_rectangle_t native_display_get_work_area(native_display_t display) {
6952
if (!display)
7053
return result;
7154

72-
Rectangle work_area = to_display(display)->GetWorkArea();
55+
Rectangle work_area = static_cast<Display*>(display)->GetWorkArea();
7356
result.x = work_area.x;
7457
result.y = work_area.y;
7558
result.width = work_area.width;
@@ -81,15 +64,15 @@ FFI_PLUGIN_EXPORT
8164
double native_display_get_scale_factor(native_display_t display) {
8265
if (!display)
8366
return 1.0;
84-
return to_display(display)->GetScaleFactor();
67+
return static_cast<Display*>(display)->GetScaleFactor();
8568
}
8669

8770
// Additional properties getters
8871
FFI_PLUGIN_EXPORT
8972
bool native_display_is_primary(native_display_t display) {
9073
if (!display)
9174
return false;
92-
return to_display(display)->IsPrimary();
75+
return static_cast<Display*>(display)->IsPrimary();
9376
}
9477

9578
FFI_PLUGIN_EXPORT
@@ -98,7 +81,8 @@ native_display_orientation_t native_display_get_orientation(
9881
if (!display)
9982
return NATIVE_DISPLAY_ORIENTATION_PORTRAIT;
10083

101-
DisplayOrientation orientation = to_display(display)->GetOrientation();
84+
DisplayOrientation orientation =
85+
static_cast<Display*>(display)->GetOrientation();
10286
switch (orientation) {
10387
case DisplayOrientation::kPortrait:
10488
return NATIVE_DISPLAY_ORIENTATION_PORTRAIT;
@@ -117,29 +101,29 @@ FFI_PLUGIN_EXPORT
117101
int native_display_get_refresh_rate(native_display_t display) {
118102
if (!display)
119103
return 0;
120-
return to_display(display)->GetRefreshRate();
104+
return static_cast<Display*>(display)->GetRefreshRate();
121105
}
122106

123107
FFI_PLUGIN_EXPORT
124108
int native_display_get_bit_depth(native_display_t display) {
125109
if (!display)
126110
return 0;
127-
return to_display(display)->GetBitDepth();
111+
return static_cast<Display*>(display)->GetBitDepth();
128112
}
129113

130114
// Platform-specific functions
131115
FFI_PLUGIN_EXPORT
132116
void* native_display_get_native_object(native_display_t display) {
133117
if (!display)
134118
return nullptr;
135-
return to_display(display)->GetNativeObject();
119+
return static_cast<Display*>(display)->GetNativeObject();
136120
}
137121

138122
// Memory management
139123
FFI_PLUGIN_EXPORT
140124
void native_display_free(native_display_t display) {
141125
if (display) {
142-
delete to_display(display);
126+
delete static_cast<Display*>(display);
143127
}
144128
}
145129

@@ -151,7 +135,7 @@ void native_display_list_free(native_display_list_t* list) {
151135
// Free individual display handles
152136
for (long i = 0; i < list->count; i++) {
153137
if (list->displays[i]) {
154-
delete to_display(list->displays[i]);
138+
delete static_cast<Display*>(list->displays[i]);
155139
}
156140
}
157141

src/capi/string_utils_c.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "string_utils_c.h"
2+
#include <cstring>
3+
4+
char* to_c_str(const std::string& str) {
5+
if (str.empty())
6+
return nullptr;
7+
8+
size_t len = str.length() + 1;
9+
char* result = new (std::nothrow) char[len];
10+
if (result) {
11+
std::strcpy(result, str.c_str());
12+
}
13+
return result;
14+
}
15+
16+
void free_c_str(char* str) {
17+
if (str) {
18+
delete[] str;
19+
}
20+
}

src/capi/string_utils_c.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
#include <string>
5+
#endif
6+
7+
#if _WIN32
8+
#define FFI_PLUGIN_EXPORT __declspec(dllexport)
9+
#else
10+
#define FFI_PLUGIN_EXPORT
11+
#endif
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
/**
18+
* String utilities for C API interoperability
19+
*/
20+
21+
/**
22+
* Convert a C++ string to a C string with memory allocation
23+
* @param str The C++ string to convert
24+
* @return Allocated C string copy, or nullptr if str is empty or allocation
25+
* failed. Caller must free the returned string with free_c_str().
26+
*/
27+
#ifdef __cplusplus
28+
char* to_c_str(const std::string& str);
29+
#endif
30+
31+
/**
32+
* Free a C string allocated by to_c_str
33+
* @param str The string to free (can be nullptr)
34+
*/
35+
FFI_PLUGIN_EXPORT
36+
void free_c_str(char* str);
37+
38+
#ifdef __cplusplus
39+
}
40+
#endif

0 commit comments

Comments
 (0)