Skip to content

Commit 62945ad

Browse files
committed
feat: Implement screen retriever for macOs, and add screen_info example.
1 parent d315057 commit 62945ad

15 files changed

+465
-0
lines changed

.clang-format

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Defines the Chromium style for automatic reformatting.
2+
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html
3+
BasedOnStyle: Chromium
4+
# This defaults to 'Auto'. Explicitly set it for a while, so that
5+
# 'vector<vector<int> >' in existing files gets formatted to
6+
# 'vector<vector<int>>'. ('Auto' means that clang-format will only use
7+
# 'int>>' if the file already contains at least one such instance.)
8+
Standard: Cpp11
9+
SortIncludes: true
10+
---
11+
Language: ObjC
12+
ColumnLimit: 100

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(libnativeapi_library VERSION 0.0.1 LANGUAGES CXX)
4+
5+
# Add library subdirectory
6+
add_subdirectory(src)
7+
8+
# Add example programs subdirectory
9+
add_subdirectory(examples/screen_info)

examples/screen_info/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(screen_info_example VERSION 0.0.1 LANGUAGES CXX)
4+
5+
# Set C++ standard
6+
set(CMAKE_CXX_STANDARD 11)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
9+
# Add example program
10+
add_executable(screen_info_example
11+
"main.cpp"
12+
)
13+
14+
# Link main library
15+
target_link_libraries(screen_info_example PRIVATE libnativeapi)
16+
17+
# Set example program properties
18+
set_target_properties(screen_info_example PROPERTIES
19+
OUTPUT_NAME "screen_info_example"
20+
)
21+
22+
# Set example program compile options (macOS only)
23+
if(APPLE)
24+
set_source_files_properties("main.cpp"
25+
PROPERTIES
26+
COMPILE_FLAGS "-x objective-c++"
27+
)
28+
endif()

examples/screen_info/main.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <iostream>
2+
#include "libnativeapi.h"
3+
4+
int main() {
5+
auto screenRetriever = ScreenRetriever::Create();
6+
7+
// Get primary display information
8+
Display primaryDisplay = screenRetriever->GetPrimaryDisplay();
9+
std::cout << "Primary Display Information:" << std::endl;
10+
std::cout << "ID: " << primaryDisplay.id << std::endl;
11+
std::cout << "Name: " << primaryDisplay.name << std::endl;
12+
std::cout << "Resolution: " << primaryDisplay.width << "x"
13+
<< primaryDisplay.height << std::endl;
14+
std::cout << "Scale Factor: " << primaryDisplay.scaleFactor << std::endl;
15+
std::cout << "Visible Position: (" << primaryDisplay.visiblePositionX << ", "
16+
<< primaryDisplay.visiblePositionY << ")" << std::endl;
17+
std::cout << "Visible Size: " << primaryDisplay.visibleSizeWidth << "x"
18+
<< primaryDisplay.visibleSizeHeight << std::endl;
19+
std::cout << std::endl;
20+
21+
// Get all displays information
22+
DisplayList allDisplays = screenRetriever->GetAllDisplays();
23+
std::cout << "All Displays Information:" << std::endl;
24+
for (int i = 0; i < allDisplays.count; i++) {
25+
Display& display = allDisplays.displays[i];
26+
std::cout << "Display " << (i + 1) << ":" << std::endl;
27+
std::cout << "ID: " << display.id << std::endl;
28+
std::cout << "Name: " << display.name << std::endl;
29+
std::cout << "Resolution: " << display.width << "x" << display.height
30+
<< std::endl;
31+
std::cout << "Scale Factor: " << display.scaleFactor << std::endl;
32+
std::cout << "Visible Position: (" << display.visiblePositionX << ", "
33+
<< display.visiblePositionY << ")" << std::endl;
34+
std::cout << "Visible Size: " << display.visibleSizeWidth << "x"
35+
<< display.visibleSizeHeight << std::endl;
36+
std::cout << std::endl;
37+
}
38+
39+
// Get cursor position
40+
CursorPoint cursorPoint = screenRetriever->GetCursorScreenPoint();
41+
std::cout << "Current Cursor Position: (" << cursorPoint.x << ", "
42+
<< cursorPoint.y << ")" << std::endl;
43+
44+
// Clean up memory
45+
delete[] allDisplays.displays;
46+
delete[] primaryDisplay.id;
47+
delete[] primaryDisplay.name;
48+
49+
return 0;
50+
}

src/CMakeLists.txt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# The Flutter tooling requires that developers have CMake 3.10 or later
2+
# installed. You should not increase this version, as doing so will cause
3+
# the plugin to fail to compile for some customers of the plugin.
4+
cmake_minimum_required(VERSION 3.10)
5+
6+
project(libnativeapi VERSION 0.0.1 LANGUAGES CXX)
7+
8+
# Enable Objective-C++
9+
if(APPLE)
10+
enable_language(OBJCXX)
11+
endif()
12+
13+
# Set C++ standard
14+
set(CMAKE_CXX_STANDARD 11)
15+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
16+
17+
# Platform-specific source files
18+
if(LINUX)
19+
set(PLATFORM_SOURCES "screen_retriever_linux.cpp")
20+
elseif(APPLE)
21+
set(PLATFORM_SOURCES "screen_retriever_macos.mm")
22+
elseif(WIN32)
23+
set(PLATFORM_SOURCES "screen_retriever_windows.cpp")
24+
else()
25+
set(PLATFORM_SOURCES "")
26+
endif()
27+
28+
# Add library target
29+
add_library(libnativeapi STATIC
30+
"screen_retriever.cpp"
31+
${PLATFORM_SOURCES}
32+
)
33+
34+
# Set library properties
35+
set_target_properties(libnativeapi PROPERTIES
36+
PUBLIC_HEADER "libnativeapi.h;screen_retriever.h;screen_retriever_linux.h;screen_retriever_macos.h;screen_retriever_windows.h"
37+
)
38+
39+
# Set library include directories
40+
target_include_directories(libnativeapi PUBLIC
41+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
42+
$<INSTALL_INTERFACE:include>
43+
)
44+
45+
# Link required frameworks (macOS only)
46+
if(APPLE)
47+
target_link_libraries(libnativeapi PUBLIC "-framework Cocoa")
48+
target_compile_options(libnativeapi PRIVATE "-x" "objective-c++")
49+
endif()

src/libnativeapi.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stdint.h>
2+
3+
#include "screen_retriever.h"
4+
5+
#if _WIN32
6+
#include <windows.h>
7+
#endif

src/screen_retriever.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "screen_retriever.h"
2+
3+
// Forward declarations for platform-specific implementations
4+
#ifdef __APPLE__
5+
#include "screen_retriever_macos.h"
6+
#elif defined(_WIN32)
7+
#include "screen_retriever_windows.h"
8+
#else
9+
#include "screen_retriever_linux.h"
10+
#endif
11+
12+
std::unique_ptr<ScreenRetriever> ScreenRetriever::Create() {
13+
#ifdef __APPLE__
14+
return std::unique_ptr<ScreenRetriever>(new ScreenRetrieverMacOS());
15+
#elif defined(_WIN32)
16+
return std::unique_ptr<ScreenRetriever>(new ScreenRetrieverWindows());
17+
#else
18+
return std::unique_ptr<ScreenRetriever>(new ScreenRetrieverLinux());
19+
#endif
20+
}
21+
22+
// This file is left intentionally empty as the implementations
23+
// have been moved to platform-specific files:
24+
// - screen_retriever_linux.cpp
25+
// - screen_retriever_macos.mm
26+
// - screen_retriever_windows.cpp

src/screen_retriever.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <vector>
5+
6+
// Representation of a display
7+
struct Display {
8+
char* id;
9+
char* name;
10+
double width;
11+
double height;
12+
double visiblePositionX;
13+
double visiblePositionY;
14+
double visibleSizeWidth;
15+
double visibleSizeHeight;
16+
double scaleFactor;
17+
};
18+
19+
// Representation of a list of displays
20+
struct DisplayList {
21+
Display* displays;
22+
int count;
23+
};
24+
25+
// Representation of a cursor position
26+
struct CursorPoint {
27+
double x;
28+
double y;
29+
};
30+
31+
// Abstract base class for ScreenRetriever
32+
class ScreenRetriever {
33+
public:
34+
virtual ~ScreenRetriever() = default;
35+
36+
// Static factory method to create platform-specific instance
37+
static std::unique_ptr<ScreenRetriever> Create();
38+
39+
// Get the current cursor screen point
40+
virtual CursorPoint GetCursorScreenPoint() = 0;
41+
42+
// Get the primary display information
43+
virtual Display GetPrimaryDisplay() = 0;
44+
45+
// Get all displays information
46+
virtual DisplayList GetAllDisplays() = 0;
47+
};

src/screen_retriever_linux.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "screen_retriever_linux.h"
2+
#include <iostream>
3+
4+
ScreenRetrieverLinux::ScreenRetrieverLinux() {
5+
// Constructor implementation
6+
std::cout << "ScreenRetrieverLinux initialized" << std::endl;
7+
}
8+
9+
ScreenRetrieverLinux::~ScreenRetrieverLinux() {
10+
// Destructor implementation
11+
std::cout << "ScreenRetrieverLinux destroyed" << std::endl;
12+
}
13+
14+
CursorPoint ScreenRetrieverLinux::GetCursorScreenPoint() {
15+
// Empty implementation
16+
CursorPoint point;
17+
point.x = 0.0;
18+
point.y = 0.0;
19+
return point;
20+
}
21+
22+
Display ScreenRetrieverLinux::GetPrimaryDisplay() {
23+
// Empty implementation
24+
Display display;
25+
display.id = "display-1";
26+
display.name = "Linux Display";
27+
display.width = 1920.0;
28+
display.height = 1080.0;
29+
display.visiblePositionX = 0.0;
30+
display.visiblePositionY = 0.0;
31+
display.visibleSizeWidth = 1920.0;
32+
display.visibleSizeHeight = 1080.0;
33+
display.scaleFactor = 1.0;
34+
return display;
35+
}
36+
37+
DisplayList ScreenRetrieverLinux::GetAllDisplays() {
38+
// Empty implementation
39+
DisplayList displayList;
40+
displayList.displays = new Display[1];
41+
displayList.displays[0] = GetPrimaryDisplay();
42+
displayList.count = 1;
43+
return displayList;
44+
}

0 commit comments

Comments
 (0)