Skip to content

Commit acd8194

Browse files
committed
feat: Implement screen retriever part methods for Linux
1 parent d08de3b commit acd8194

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

src/CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ list(FILTER COMMON_SOURCES EXCLUDE REGEX "linux.*|macos.*|windows.*")
2121
# Platform-specific source files
2222
if(LINUX)
2323
file(GLOB PLATFORM_SOURCES "*_linux.cpp")
24+
# Find GTK package for Linux
25+
find_package(PkgConfig REQUIRED)
26+
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)
2427
elseif(APPLE)
2528
file(GLOB PLATFORM_SOURCES "*_macos.mm")
2629
elseif(WIN32)
@@ -46,8 +49,14 @@ target_include_directories(libnativeapi PUBLIC
4649
$<INSTALL_INTERFACE:include>
4750
)
4851

49-
# Link required frameworks (macOS only)
52+
if (LINUX)
53+
target_include_directories(libnativeapi PUBLIC ${GTK_INCLUDE_DIRS})
54+
endif ()
55+
56+
# Link required frameworks and libraries
5057
if(APPLE)
5158
target_link_libraries(libnativeapi PUBLIC "-framework Cocoa")
5259
target_compile_options(libnativeapi PRIVATE "-x" "objective-c++")
53-
endif()
60+
elseif (LINUX)
61+
target_link_libraries(libnativeapi PUBLIC PkgConfig::GTK)
62+
endif ()

src/screen_retriever_linux.cpp

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
1+
#include <gtk/gtk.h>
12
#include <iostream>
23
#include "screen_retriever.h"
34

45
namespace nativeapi {
56

7+
static Display CreateDisplayFromGdkMonitor(GdkMonitor* monitor,
8+
bool isFirstScreen) {
9+
Display display;
10+
11+
display.id = "";
12+
display.name = gdk_monitor_get_model(monitor);
13+
14+
GdkRectangle frame;
15+
gdk_monitor_get_geometry(monitor, &frame);
16+
17+
display.width = frame.width;
18+
display.height = frame.height;
19+
20+
GdkRectangle workarea_rect;
21+
gdk_monitor_get_workarea(monitor, &workarea_rect);
22+
23+
display.visibleSizeWidth = workarea_rect.width;
24+
display.visibleSizeHeight = workarea_rect.height;
25+
display.visiblePositionX = workarea_rect.x;
26+
display.visiblePositionY = workarea_rect.y;
27+
28+
display.scaleFactor = gdk_monitor_get_scale_factor(monitor);
29+
30+
return display;
31+
}
32+
633
ScreenRetriever::ScreenRetriever() {
34+
gtk_init(nullptr, nullptr);
735
// Constructor implementation
836
std::cout << "ScreenRetriever initialized" << std::endl;
937
}
@@ -14,26 +42,33 @@ ScreenRetriever::~ScreenRetriever() {
1442
}
1543

1644
Point ScreenRetriever::GetCursorScreenPoint() {
45+
GdkDisplay* display = gdk_display_get_default();
46+
GdkSeat* seat = gdk_display_get_default_seat(display);
47+
GdkDevice* pointer = gdk_seat_get_pointer(seat);
48+
49+
int x, y;
50+
gdk_device_get_position(pointer, NULL, &x, &y);
51+
1752
// Empty implementation
1853
Point point;
19-
point.x = 0.0;
20-
point.y = 0.0;
54+
point.x = x;
55+
point.y = y;
2156
return point;
2257
}
2358

2459
Display ScreenRetriever::GetPrimaryDisplay() {
25-
// Empty implementation
26-
Display display;
27-
display.id = "display-1";
28-
display.name = "Linux Display";
29-
display.width = 1920.0;
30-
display.height = 1080.0;
31-
display.visiblePositionX = 0.0;
32-
display.visiblePositionY = 0.0;
33-
display.visibleSizeWidth = 1920.0;
34-
display.visibleSizeHeight = 1080.0;
35-
display.scaleFactor = 1.0;
36-
return display;
60+
GdkDisplay* display = gdk_display_get_default();
61+
GdkMonitor* monitor = gdk_display_get_primary_monitor(display);
62+
63+
// opt: fallback if there's no primary monitor
64+
if (monitor == nullptr) {
65+
int monitor_count = gdk_display_get_n_monitors(display);
66+
if (monitor_count > 0) {
67+
monitor = gdk_display_get_monitor(display, 0);
68+
}
69+
}
70+
71+
return CreateDisplayFromGdkMonitor(monitor, true);
3772
}
3873

3974
std::vector<Display> ScreenRetriever::GetAllDisplays() {

0 commit comments

Comments
 (0)