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