|
1 | 1 | #include <string.h> |
2 | 2 | #include <iostream> |
| 3 | +#include <vector> |
3 | 4 |
|
4 | 5 | #include "../display.h" |
5 | 6 | #include "../display_manager.h" |
|
10 | 11 |
|
11 | 12 | using namespace nativeapi; |
12 | 13 |
|
| 14 | +native_display_orientation_t to_native_orientation( |
| 15 | + DisplayOrientation orientation) { |
| 16 | + switch (orientation) { |
| 17 | + case DisplayOrientation::kPortrait: |
| 18 | + return NATIVE_DISPLAY_ORIENTATION_PORTRAIT; |
| 19 | + case DisplayOrientation::kLandscape: |
| 20 | + return NATIVE_DISPLAY_ORIENTATION_LANDSCAPE; |
| 21 | + case DisplayOrientation::kPortraitFlipped: |
| 22 | + return NATIVE_DISPLAY_ORIENTATION_PORTRAIT_FLIPPED; |
| 23 | + case DisplayOrientation::kLandscapeFlipped: |
| 24 | + return NATIVE_DISPLAY_ORIENTATION_LANDSCAPE_FLIPPED; |
| 25 | + default: |
| 26 | + return NATIVE_DISPLAY_ORIENTATION_PORTRAIT; |
| 27 | + } |
| 28 | +} |
| 29 | + |
13 | 30 | native_display_t to_native_display(const Display& raw_display) { |
14 | | - native_display_t display; |
| 31 | + native_display_t display = {}; |
| 32 | + |
| 33 | + // Allocate and copy strings |
15 | 34 | display.id = strdup(raw_display.id.c_str()); |
16 | 35 | display.name = strdup(raw_display.name.c_str()); |
| 36 | + display.manufacturer = raw_display.manufacturer.empty() |
| 37 | + ? nullptr |
| 38 | + : strdup(raw_display.manufacturer.c_str()); |
| 39 | + display.model = |
| 40 | + raw_display.model.empty() ? nullptr : strdup(raw_display.model.c_str()); |
| 41 | + display.serial_number = nullptr; // Not available in the C++ API |
| 42 | + |
| 43 | + // Copy position |
| 44 | + display.position.x = raw_display.position.x; |
| 45 | + display.position.y = raw_display.position.y; |
| 46 | + |
| 47 | + // Copy size |
17 | 48 | display.size.width = raw_display.size.width; |
18 | 49 | display.size.height = raw_display.size.height; |
| 50 | + |
| 51 | + // Copy work area |
19 | 52 | display.work_area.x = raw_display.workArea.x; |
20 | 53 | display.work_area.y = raw_display.workArea.y; |
21 | 54 | display.work_area.width = raw_display.workArea.width; |
22 | 55 | display.work_area.height = raw_display.workArea.height; |
| 56 | + |
| 57 | + // Copy other properties |
23 | 58 | display.scale_factor = raw_display.scaleFactor; |
| 59 | + display.is_primary = raw_display.isPrimary; |
| 60 | + display.orientation = to_native_orientation(raw_display.orientation); |
| 61 | + display.refresh_rate = raw_display.refreshRate; |
| 62 | + display.bit_depth = raw_display.bitDepth; |
| 63 | + |
24 | 64 | return display; |
25 | 65 | } |
26 | 66 |
|
27 | 67 | DisplayManager g_display_manager = DisplayManager(); |
28 | 68 |
|
29 | 69 | FFI_PLUGIN_EXPORT |
30 | 70 | native_display_list_t native_display_manager_get_all() { |
31 | | - auto displays = g_display_manager.GetAll(); |
32 | | - native_display_list_t list; |
33 | | - list.count = displays.size(); |
34 | | - for (size_t i = 0; i < displays.size(); i++) { |
35 | | - list.displays[i] = to_native_display(displays[i]); |
| 71 | + try { |
| 72 | + auto displays = g_display_manager.GetAll(); |
| 73 | + native_display_list_t list = {}; |
| 74 | + |
| 75 | + list.count = displays.size(); |
| 76 | + if (list.count > 0) { |
| 77 | + // Allocate array for displays |
| 78 | + list.displays = |
| 79 | + (native_display_t*)malloc(sizeof(native_display_t) * list.count); |
| 80 | + if (list.displays != nullptr) { |
| 81 | + for (size_t i = 0; i < displays.size(); i++) { |
| 82 | + list.displays[i] = to_native_display(displays[i]); |
| 83 | + } |
| 84 | + } else { |
| 85 | + list.count = 0; |
| 86 | + } |
| 87 | + } else { |
| 88 | + list.displays = nullptr; |
| 89 | + } |
| 90 | + |
| 91 | + return list; |
| 92 | + } catch (const std::exception& e) { |
| 93 | + std::cerr << "Error in native_display_manager_get_all: " << e.what() |
| 94 | + << std::endl; |
| 95 | + native_display_list_t empty_list = {}; |
| 96 | + empty_list.count = 0; |
| 97 | + empty_list.displays = nullptr; |
| 98 | + return empty_list; |
36 | 99 | } |
37 | | - return list; |
38 | 100 | } |
39 | 101 |
|
40 | 102 | FFI_PLUGIN_EXPORT |
41 | 103 | native_display_t native_display_manager_get_primary() { |
42 | | - auto display = g_display_manager.GetPrimary(); |
43 | | - return to_native_display(display); |
| 104 | + try { |
| 105 | + auto display = g_display_manager.GetPrimary(); |
| 106 | + return to_native_display(display); |
| 107 | + } catch (const std::exception& e) { |
| 108 | + std::cerr << "Error in native_display_manager_get_primary: " << e.what() |
| 109 | + << std::endl; |
| 110 | + native_display_t empty_display = {}; |
| 111 | + return empty_display; |
| 112 | + } |
44 | 113 | } |
45 | 114 |
|
46 | 115 | FFI_PLUGIN_EXPORT |
47 | 116 | native_point_t native_display_manager_get_cursor_position() { |
48 | | - auto cursor_position = g_display_manager.GetCursorPosition(); |
49 | | - native_point_t point; |
50 | | - point.x = cursor_position.x; |
51 | | - point.y = cursor_position.y; |
52 | | - return point; |
| 117 | + try { |
| 118 | + auto cursor_position = g_display_manager.GetCursorPosition(); |
| 119 | + native_point_t point; |
| 120 | + point.x = cursor_position.x; |
| 121 | + point.y = cursor_position.y; |
| 122 | + return point; |
| 123 | + } catch (const std::exception& e) { |
| 124 | + std::cerr << "Error in native_display_manager_get_cursor_position: " |
| 125 | + << e.what() << std::endl; |
| 126 | + native_point_t point = {0.0, 0.0}; |
| 127 | + return point; |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +FFI_PLUGIN_EXPORT |
| 132 | +void native_display_list_free(native_display_list_t* list) { |
| 133 | + if (list != nullptr && list->displays != nullptr) { |
| 134 | + // Free individual display strings |
| 135 | + for (size_t i = 0; i < list->count; i++) { |
| 136 | + native_display_t* display = &list->displays[i]; |
| 137 | + if (display->id != nullptr) { |
| 138 | + free(display->id); |
| 139 | + display->id = nullptr; |
| 140 | + } |
| 141 | + if (display->name != nullptr) { |
| 142 | + free(display->name); |
| 143 | + display->name = nullptr; |
| 144 | + } |
| 145 | + if (display->manufacturer != nullptr) { |
| 146 | + free(display->manufacturer); |
| 147 | + display->manufacturer = nullptr; |
| 148 | + } |
| 149 | + if (display->model != nullptr) { |
| 150 | + free(display->model); |
| 151 | + display->model = nullptr; |
| 152 | + } |
| 153 | + if (display->serial_number != nullptr) { |
| 154 | + free(display->serial_number); |
| 155 | + display->serial_number = nullptr; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // Free the displays array |
| 160 | + free(list->displays); |
| 161 | + list->displays = nullptr; |
| 162 | + list->count = 0; |
| 163 | + } |
53 | 164 | } |
0 commit comments