Skip to content

Commit 9284bb1

Browse files
committed
Refactor native_display_t to use raw pointer handle
Replaces the native_display_handle struct and shared_ptr with a raw pointer to the Display object for native_display_t, aligning with the window handle design. Updates all usages to cast and manage the raw pointer appropriately, simplifying the handle implementation.
1 parent c6ec50e commit 9284bb1

File tree

2 files changed

+37
-42
lines changed

2 files changed

+37
-42
lines changed

src/capi/display_c.cpp

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55

66
using namespace nativeapi;
77

8-
// Internal structure to hold the actual Display pointer
9-
struct native_display_handle {
10-
std::shared_ptr<Display> display;
11-
explicit native_display_handle(std::shared_ptr<Display> d)
12-
: display(std::move(d)) {}
13-
};
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+
}
1412

1513
// Helper function to safely copy C++ string to C string
1614
static char* copy_string(const std::string& str) {
@@ -28,26 +26,26 @@ static char* copy_string(const std::string& str) {
2826
// Basic identification getters
2927
FFI_PLUGIN_EXPORT
3028
char* native_display_get_id(native_display_t display) {
31-
if (!display || !display->display)
29+
if (!display)
3230
return nullptr;
33-
return copy_string(display->display->GetId());
31+
return copy_string(to_display(display)->GetId());
3432
}
3533

3634
FFI_PLUGIN_EXPORT
3735
char* native_display_get_name(native_display_t display) {
38-
if (!display || !display->display)
36+
if (!display)
3937
return nullptr;
40-
return copy_string(display->display->GetName());
38+
return copy_string(to_display(display)->GetName());
4139
}
4240

4341
// Physical properties getters
4442
FFI_PLUGIN_EXPORT
4543
native_point_t native_display_get_position(native_display_t display) {
4644
native_point_t result = {0.0, 0.0};
47-
if (!display || !display->display)
45+
if (!display)
4846
return result;
4947

50-
auto pos = display->display->GetPosition();
48+
auto pos = to_display(display)->GetPosition();
5149
result.x = pos.x;
5250
result.y = pos.y;
5351
return result;
@@ -56,10 +54,10 @@ native_point_t native_display_get_position(native_display_t display) {
5654
FFI_PLUGIN_EXPORT
5755
native_size_t native_display_get_size(native_display_t display) {
5856
native_size_t result = {0.0, 0.0};
59-
if (!display || !display->display)
57+
if (!display)
6058
return result;
6159

62-
auto size = display->display->GetSize();
60+
auto size = to_display(display)->GetSize();
6361
result.width = size.width;
6462
result.height = size.height;
6563
return result;
@@ -68,10 +66,10 @@ native_size_t native_display_get_size(native_display_t display) {
6866
FFI_PLUGIN_EXPORT
6967
native_rectangle_t native_display_get_work_area(native_display_t display) {
7068
native_rectangle_t result = {0.0, 0.0, 0.0, 0.0};
71-
if (!display || !display->display)
69+
if (!display)
7270
return result;
7371

74-
Rectangle work_area = display->display->GetWorkArea();
72+
Rectangle work_area = to_display(display)->GetWorkArea();
7573
result.x = work_area.x;
7674
result.y = work_area.y;
7775
result.width = work_area.width;
@@ -81,26 +79,26 @@ native_rectangle_t native_display_get_work_area(native_display_t display) {
8179

8280
FFI_PLUGIN_EXPORT
8381
double native_display_get_scale_factor(native_display_t display) {
84-
if (!display || !display->display)
82+
if (!display)
8583
return 1.0;
86-
return display->display->GetScaleFactor();
84+
return to_display(display)->GetScaleFactor();
8785
}
8886

8987
// Additional properties getters
9088
FFI_PLUGIN_EXPORT
9189
bool native_display_is_primary(native_display_t display) {
92-
if (!display || !display->display)
90+
if (!display)
9391
return false;
94-
return display->display->IsPrimary();
92+
return to_display(display)->IsPrimary();
9593
}
9694

9795
FFI_PLUGIN_EXPORT
9896
native_display_orientation_t native_display_get_orientation(
9997
native_display_t display) {
100-
if (!display || !display->display)
98+
if (!display)
10199
return NATIVE_DISPLAY_ORIENTATION_PORTRAIT;
102100

103-
DisplayOrientation orientation = display->display->GetOrientation();
101+
DisplayOrientation orientation = to_display(display)->GetOrientation();
104102
switch (orientation) {
105103
case DisplayOrientation::kPortrait:
106104
return NATIVE_DISPLAY_ORIENTATION_PORTRAIT;
@@ -117,46 +115,46 @@ native_display_orientation_t native_display_get_orientation(
117115

118116
FFI_PLUGIN_EXPORT
119117
int native_display_get_refresh_rate(native_display_t display) {
120-
if (!display || !display->display)
118+
if (!display)
121119
return 0;
122-
return display->display->GetRefreshRate();
120+
return to_display(display)->GetRefreshRate();
123121
}
124122

125123
FFI_PLUGIN_EXPORT
126124
int native_display_get_bit_depth(native_display_t display) {
127-
if (!display || !display->display)
125+
if (!display)
128126
return 0;
129-
return display->display->GetBitDepth();
127+
return to_display(display)->GetBitDepth();
130128
}
131129

132130
// Hardware information getters
133131
FFI_PLUGIN_EXPORT
134132
char* native_display_get_manufacturer(native_display_t display) {
135-
if (!display || !display->display)
133+
if (!display)
136134
return nullptr;
137-
return copy_string(display->display->GetManufacturer());
135+
return copy_string(to_display(display)->GetManufacturer());
138136
}
139137

140138
FFI_PLUGIN_EXPORT
141139
char* native_display_get_model(native_display_t display) {
142-
if (!display || !display->display)
140+
if (!display)
143141
return nullptr;
144-
return copy_string(display->display->GetModel());
142+
return copy_string(to_display(display)->GetModel());
145143
}
146144

147145
FFI_PLUGIN_EXPORT
148146
char* native_display_get_serial_number(native_display_t display) {
149-
if (!display || !display->display)
147+
if (!display)
150148
return nullptr;
151-
return copy_string(display->display->GetSerialNumber());
149+
return copy_string(to_display(display)->GetSerialNumber());
152150
}
153151

154152
// Platform-specific functions
155153
FFI_PLUGIN_EXPORT
156154
void* native_display_get_native_object(native_display_t display) {
157-
if (!display || !display->display)
155+
if (!display)
158156
return nullptr;
159-
return display->display->GetNativeObject();
157+
return to_display(display)->GetNativeObject();
160158
}
161159

162160
// Memory management
@@ -170,7 +168,7 @@ void native_display_free_string(char* str) {
170168
FFI_PLUGIN_EXPORT
171169
void native_display_free(native_display_t display) {
172170
if (display) {
173-
delete display;
171+
delete to_display(display);
174172
}
175173
}
176174

@@ -182,7 +180,7 @@ void native_display_list_free(native_display_list_t* list) {
182180
// Free individual display handles
183181
for (long i = 0; i < list->count; i++) {
184182
if (list->displays[i]) {
185-
delete list->displays[i];
183+
delete to_display(list->displays[i]);
186184
}
187185
}
188186

@@ -196,11 +194,7 @@ void native_display_list_free(native_display_list_t* list) {
196194
native_display_t native_display_create_handle(
197195
const nativeapi::Display& cpp_display) {
198196
try {
199-
// Create a shared_ptr from the Display object (copy constructor)
200-
auto display_ptr = std::make_shared<Display>(cpp_display);
201-
202-
// Create the native handle
203-
auto* handle = new (std::nothrow) native_display_handle(display_ptr);
197+
auto* handle = new (std::nothrow) Display(cpp_display);
204198
return handle;
205199
} catch (const std::exception&) {
206200
return nullptr;

src/capi/display_c.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ typedef enum : int {
2727

2828
/**
2929
* Opaque display handle
30+
* Align with window handle design: use a raw pointer to underlying C++ type
3031
*/
31-
typedef struct native_display_handle* native_display_t;
32+
typedef void* native_display_t;
3233

3334
/**
3435
* Display list structure

0 commit comments

Comments
 (0)