Skip to content

Commit 6379dc0

Browse files
committed
Add C API for display manager and geometry structs
- Introduce `geometry_c.h` for point, size, and rectangle structs - Add `display_manager_c.h` and implementation for C API functions: - `native_display_manager_get_all` - `native_display_manager_get_primary` - `native_display_manager_get_cursor_position` - Refactor C display API to use new types and naming conventions - Update example to show usage of new C API functions
1 parent ccfbdae commit 6379dc0

File tree

7 files changed

+390
-245
lines changed

7 files changed

+390
-245
lines changed

examples/display_example/main.cpp

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#include <iostream>
2-
#include <iomanip>
3-
#include <thread>
41
#include <chrono>
2+
#include <iomanip>
3+
#include <iostream>
54
#include <sstream>
5+
#include <thread>
66
#include "nativeapi.h"
7+
#include "nativeapi_c.h"
78

89
using nativeapi::Display;
910
using nativeapi::DisplayManager;
@@ -36,26 +37,39 @@ std::string truncateString(const std::string& str, size_t maxLength) {
3637

3738
// Helper function to format a table row with proper alignment
3839
std::string formatTableRow(const std::string& content, int totalWidth = 70) {
39-
std::string truncated = truncateString(content, totalWidth - 4); // Leave space for "│ " and " │"
40+
std::string truncated =
41+
truncateString(content, totalWidth - 4); // Leave space for "│ " and " │"
4042
std::ostringstream oss;
4143
oss << "" << std::left << std::setw(totalWidth - 4) << truncated << "";
4244
return oss.str();
4345
}
4446

45-
// Helper function to create table border - using ASCII characters for better compatibility
46-
std::string createTableBorder(const std::string& leftChar, const std::string& rightChar, const std::string& fillChar, int width = 70) {
47+
// Helper function to create table border - using ASCII characters for better
48+
// compatibility
49+
std::string createTableBorder(const std::string& leftChar,
50+
const std::string& rightChar,
51+
const std::string& fillChar,
52+
int width = 70) {
4753
std::string border;
48-
if (leftChar == "") border = "+";
49-
else if (leftChar == "") border = "+";
50-
else if (leftChar == "") border = "+";
51-
else border = leftChar;
54+
if (leftChar == "")
55+
border = "+";
56+
else if (leftChar == "")
57+
border = "+";
58+
else if (leftChar == "")
59+
border = "+";
60+
else
61+
border = leftChar;
5262

5363
border += std::string(width - 2, '-');
5464

55-
if (rightChar == "") border += "+";
56-
else if (rightChar == "") border += "+";
57-
else if (rightChar == "") border += "+";
58-
else border += rightChar;
65+
if (rightChar == "")
66+
border += "+";
67+
else if (rightChar == "")
68+
border += "+";
69+
else if (rightChar == "")
70+
border += "+";
71+
else
72+
border += rightChar;
5973

6074
return border;
6175
}
@@ -65,44 +79,54 @@ void printDisplayInfo(const Display& display, bool isPrimary = false) {
6579
const int tableWidth = 70;
6680

6781
std::cout << createTableBorder("", "", "", tableWidth) << std::endl;
68-
std::cout << formatTableRow("Display: " + display.name, tableWidth) << std::endl;
82+
std::cout << formatTableRow("Display: " + display.name, tableWidth)
83+
<< std::endl;
6984
std::cout << createTableBorder("", "", "", tableWidth) << std::endl;
7085
std::cout << formatTableRow("ID: " + display.id, tableWidth) << std::endl;
7186

7287
// Format position string
73-
std::string positionStr = "Position: (" + std::to_string((int)display.position.x) + ", " + std::to_string((int)display.position.y) + ")";
88+
std::string positionStr = "Position: (" +
89+
std::to_string((int)display.position.x) + ", " +
90+
std::to_string((int)display.position.y) + ")";
7491
std::cout << formatTableRow(positionStr, tableWidth) << std::endl;
7592

7693
// Format size string with proper separator
77-
std::string sizeStr = "Size: " + std::to_string((int)display.size.width) + " x " + std::to_string((int)display.size.height);
94+
std::string sizeStr = "Size: " + std::to_string((int)display.size.width) +
95+
" x " + std::to_string((int)display.size.height);
7896
std::cout << formatTableRow(sizeStr, tableWidth) << std::endl;
7997

8098
// Format work area string with proper formatting
81-
std::string workAreaStr = "Work Area: (" + std::to_string((int)display.workArea.x) + ", " +
82-
std::to_string((int)display.workArea.y) + ") " +
83-
std::to_string((int)display.workArea.width) + " x " +
84-
std::to_string((int)display.workArea.height);
99+
std::string workAreaStr =
100+
"Work Area: (" + std::to_string((int)display.workArea.x) + ", " +
101+
std::to_string((int)display.workArea.y) + ") " +
102+
std::to_string((int)display.workArea.width) + " x " +
103+
std::to_string((int)display.workArea.height);
85104
std::cout << formatTableRow(workAreaStr, tableWidth) << std::endl;
86105

87106
// Format scale factor string
88107
std::stringstream scaleStream;
89-
scaleStream << "Scale Factor: " << std::fixed << std::setprecision(2) << display.scaleFactor;
108+
scaleStream << "Scale Factor: " << std::fixed << std::setprecision(2)
109+
<< display.scaleFactor;
90110
std::cout << formatTableRow(scaleStream.str(), tableWidth) << std::endl;
91111

92112
// Format primary status
93-
std::string primaryStr = "Primary: " + std::string(display.isPrimary ? "Yes" : "No");
113+
std::string primaryStr =
114+
"Primary: " + std::string(display.isPrimary ? "Yes" : "No");
94115
std::cout << formatTableRow(primaryStr, tableWidth) << std::endl;
95116

96117
// Format orientation
97-
std::string orientationStr = "Orientation: " + orientationToString(display.orientation);
118+
std::string orientationStr =
119+
"Orientation: " + orientationToString(display.orientation);
98120
std::cout << formatTableRow(orientationStr, tableWidth) << std::endl;
99121

100122
// Format refresh rate
101-
std::string refreshStr = "Refresh Rate: " + std::to_string(display.refreshRate) + " Hz";
123+
std::string refreshStr =
124+
"Refresh Rate: " + std::to_string(display.refreshRate) + " Hz";
102125
std::cout << formatTableRow(refreshStr, tableWidth) << std::endl;
103126

104127
if (display.bitDepth > 0) {
105-
std::string bitDepthStr = "Bit Depth: " + std::to_string(display.bitDepth) + " bits";
128+
std::string bitDepthStr =
129+
"Bit Depth: " + std::to_string(display.bitDepth) + " bits";
106130
std::cout << formatTableRow(bitDepthStr, tableWidth) << std::endl;
107131
}
108132

@@ -156,7 +180,8 @@ int main() {
156180

157181
// Display cursor position
158182
Point cursorPos = displayManager.GetCursorPosition();
159-
std::cout << "🖱️ Current Cursor Position: (" << cursorPos.x << ", " << cursorPos.y << ")" << std::endl;
183+
std::cout << "🖱️ Current Cursor Position: (" << cursorPos.x << ", "
184+
<< cursorPos.y << ")" << std::endl;
160185
std::cout << std::endl;
161186

162187
// Display summary statistics
@@ -176,13 +201,16 @@ int main() {
176201
std::cout << createTableBorder("", "", "", summaryWidth) << std::endl;
177202

178203
// Format each summary line properly
179-
std::string totalDisplaysStr = "Total Displays: " + std::to_string(displays.size());
204+
std::string totalDisplaysStr =
205+
"Total Displays: " + std::to_string(displays.size());
180206
std::cout << formatTableRow(totalDisplaysStr, summaryWidth) << std::endl;
181207

182-
std::string combinedWidthStr = "Combined Width: " + std::to_string((int)totalWidth);
208+
std::string combinedWidthStr =
209+
"Combined Width: " + std::to_string((int)totalWidth);
183210
std::cout << formatTableRow(combinedWidthStr, summaryWidth) << std::endl;
184211

185-
std::string maxHeightStr = "Max Height: " + std::to_string((int)totalHeight);
212+
std::string maxHeightStr =
213+
"Max Height: " + std::to_string((int)totalHeight);
186214
std::cout << formatTableRow(maxHeightStr, summaryWidth) << std::endl;
187215

188216
std::stringstream scaleRangeStream;
@@ -202,5 +230,15 @@ int main() {
202230
}
203231

204232
std::cout << "\n✅ Display information retrieved successfully!" << std::endl;
233+
234+
native_display_list_t display_list = native_display_manager_get_all();
235+
236+
std::cout << "C API - Found " << display_list.count
237+
<< " display(s):" << std::endl;
238+
for (size_t i = 0; i < display_list.count; ++i) {
239+
std::cout << "Display " << (i + 1)
240+
<< " Name: " << display_list.displays[i].name << std::endl;
241+
}
242+
205243
return 0;
206-
}
244+
}

include/nativeapi_c.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#pragma once
22

33
#include "../src/capi/display_c.h"
4+
#include "../src/capi/display_manager_c.h"
5+
#include "../src/capi/geometry_c.h"

0 commit comments

Comments
 (0)