22#include < cstring>
33#include < memory>
44#include " ../display.h"
5+ #include " string_utils_c.h"
56
67using 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
2710FFI_PLUGIN_EXPORT
2811char * 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
3417FFI_PLUGIN_EXPORT
3518char * 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
8164double 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
8871FFI_PLUGIN_EXPORT
8972bool 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
9578FFI_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
117101int 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
123107FFI_PLUGIN_EXPORT
124108int 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
131115FFI_PLUGIN_EXPORT
132116void * 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
139123FFI_PLUGIN_EXPORT
140124void 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
0 commit comments