Skip to content

Commit 90974b5

Browse files
committed
refactor: restructure cross-platform backend architecture
1 parent ce2faf4 commit 90974b5

110 files changed

Lines changed: 6306 additions & 2601 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212

1313
/.opencode
1414
/AGENTS.md
15+
/tmp

‎CMakeLists.txt‎

Lines changed: 188 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,209 @@
1-
cmake_minimum_required(VERSION 3.21)
1+
cmake_minimum_required(VERSION 3.24)
22

3-
project(RobotCPP)
3+
project(robot-cpp
4+
VERSION 0.2.0
5+
DESCRIPTION "Cross-platform keyboard, mouse, and screen automation"
6+
LANGUAGES CXX
7+
)
48

9+
# Honour CMAKE_CXX_STANDARD on targets and keep this a subproject-friendly build.
510
set(CMAKE_CXX_STANDARD 23)
611
set(CMAKE_CXX_STANDARD_REQUIRED ON)
712
set(CMAKE_CXX_EXTENSIONS OFF)
813

9-
# Library name
10-
set(LIB_NAME RobotCPP)
11-
12-
# Option to build tests (OFF by default)
13-
option(BUILD_ROBOT_TESTS "Build the RobotCPP tests" OFF)
14-
15-
# Only find dependencies and add GoogleTest if tests are enabled
16-
if(BUILD_ROBOT_TESTS)
17-
# Add GoogleTest
18-
add_subdirectory(externals/googletest)
19-
enable_testing()
14+
if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
15+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
16+
endif()
2017

21-
# Find SDL2 for tests
22-
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/sdl2/")
23-
find_package(SDL2 REQUIRED)
18+
# Detect whether we are the top-level project so options don't leak into parents.
19+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
20+
set(ROBOT_IS_TOPLEVEL ON)
21+
else()
22+
set(ROBOT_IS_TOPLEVEL OFF)
2423
endif()
2524

26-
# Compiler-specific options
25+
option(ROBOT_BUILD_TESTS "Build robot-cpp tests" ${ROBOT_IS_TOPLEVEL})
26+
option(ROBOT_BUILD_INTERACTIVE_TESTS
27+
"Build the SDL-driven interactive injection tests (needs a live display)" OFF)
28+
option(ROBOT_BUILD_EXAMPLES "Build robot-cpp examples" ${ROBOT_IS_TOPLEVEL})
29+
option(ROBOT_WERROR "Treat warnings as errors" OFF)
30+
option(ROBOT_LINUX_ENABLE_UINPUT "Build the Linux uinput backend" ON)
31+
32+
# ── Warnings interface ────────────────────────────────────────────────────────
33+
# A dedicated interface target carries warning flags so they attach per-target
34+
# instead of polluting the whole directory via add_compile_options.
35+
add_library(robot_warnings INTERFACE)
2736
if(MSVC)
28-
# MSVC flags
29-
add_compile_options(/W4 /MP)
37+
target_compile_options(robot_warnings INTERFACE /W4 /permissive- /MP)
38+
if(ROBOT_WERROR)
39+
target_compile_options(robot_warnings INTERFACE /WX)
40+
endif()
3041
else()
31-
# GCC/Clang flags
32-
add_compile_options(-Wall -Wextra -Wpedantic)
42+
target_compile_options(robot_warnings INTERFACE
43+
-Wall -Wextra -Wpedantic -Wconversion -Wshadow)
44+
if(ROBOT_WERROR)
45+
target_compile_options(robot_warnings INTERFACE -Werror)
46+
endif()
3347
endif()
3448

35-
# Common source files
36-
set(COMMON_SOURCES
37-
src/ActionRecorder.h
38-
src/types.h
39-
src/Mouse.h
40-
src/Mouse.cpp
41-
src/Utils.h
42-
src/Utils.cpp
43-
src/Keyboard.cpp
44-
src/Keyboard.h
45-
src/Screen.cpp
46-
src/Screen.h)
47-
48-
# External dependencies
49-
set(SOURCES_LODEPNG
50-
externals/lodepng/lodepng.cpp
51-
externals/lodepng/lodepng.h)
52-
53-
# Platform-specific components
49+
# ── Vendored PNG encoder (private) ────────────────────────────────────────────
50+
add_library(robot_lodepng STATIC externals/lodepng/lodepng.cpp)
51+
target_include_directories(robot_lodepng
52+
PUBLIC
53+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/externals/lodepng>
54+
$<INSTALL_INTERFACE:include/robot/external/lodepng>
55+
)
56+
set_target_properties(robot_lodepng PROPERTIES POSITION_INDEPENDENT_CODE ON)
57+
58+
# ── Portable sources (compiled on every platform) ─────────────────────────────
59+
set(ROBOT_COMMON_SOURCES
60+
src/common/Session.cpp
61+
src/common/Keyboard.cpp
62+
src/common/Mouse.cpp
63+
src/common/Screen.cpp
64+
src/common/Image.cpp
65+
src/common/Recorder.cpp
66+
src/common/EventTap.cpp
67+
)
68+
69+
# ── Platform selection: exactly one directory is compiled ─────────────────────
70+
# This is the single compile boundary. No consumer translation unit contains a
71+
# platform #ifdef; the platform is chosen here, once.
5472
if(APPLE)
55-
list(APPEND PLATFORM_SOURCES src/EventHookMacOS.h)
56-
find_library(CARBON_LIBRARY Carbon)
57-
mark_as_advanced(CARBON_LIBRARY)
58-
list(APPEND PLATFORM_LIBRARIES ${CARBON_LIBRARY})
73+
set(ROBOT_PLATFORM_SOURCES
74+
src/platform/macos/MacBackendFactory.cpp
75+
src/platform/macos/MacKeyMap.cpp
76+
src/platform/macos/MacKeyboardBackend.cpp
77+
src/platform/macos/MacMouseBackend.cpp
78+
src/platform/macos/MacScreenBackend.cpp
79+
src/platform/macos/MacEventTapBackend.cpp
80+
)
5981
elseif(WIN32)
60-
list(APPEND PLATFORM_SOURCES src/EventHookWindows.h)
82+
set(ROBOT_PLATFORM_SOURCES
83+
src/platform/windows/WinBackendFactory.cpp
84+
src/platform/windows/WinDpi.cpp
85+
src/platform/windows/WinKeyMap.cpp
86+
src/platform/windows/WinKeyboardBackend.cpp
87+
src/platform/windows/WinMouseBackend.cpp
88+
src/platform/windows/WinScreenBackend.cpp
89+
src/platform/windows/WinEventTapBackend.cpp
90+
)
91+
elseif(UNIX)
92+
set(ROBOT_PLATFORM_SOURCES
93+
src/platform/linux/LinuxBackendFactory.cpp
94+
src/platform/linux/LinuxPlatformBackend.cpp
95+
src/platform/linux/X11Display.cpp
96+
src/platform/linux/X11KeyMap.cpp
97+
src/platform/linux/X11KeyboardBackend.cpp
98+
src/platform/linux/X11MouseBackend.cpp
99+
src/platform/linux/X11ScreenBackend.cpp
100+
src/platform/linux/X11EventTapBackend.cpp
101+
)
102+
if(ROBOT_LINUX_ENABLE_UINPUT)
103+
list(APPEND ROBOT_PLATFORM_SOURCES
104+
src/platform/linux/UinputBackend.cpp
105+
src/platform/linux/LinuxEvdevKeymap.cpp
106+
)
107+
endif()
108+
else()
109+
message(FATAL_ERROR "robot-cpp: unsupported platform")
61110
endif()
62111

63-
# Define the main library
64-
add_library(${LIB_NAME} STATIC ${COMMON_SOURCES} ${PLATFORM_SOURCES} ${SOURCES_LODEPNG})
65-
target_include_directories(${LIB_NAME} PUBLIC src PRIVATE externals/lodepng)
66-
target_link_libraries(${LIB_NAME} ${PLATFORM_LIBRARIES})
112+
# ── Main library ──────────────────────────────────────────────────────────────
113+
add_library(robot ${ROBOT_COMMON_SOURCES} ${ROBOT_PLATFORM_SOURCES})
114+
add_library(robot::robot ALIAS robot)
115+
116+
target_compile_features(robot PUBLIC cxx_std_23)
117+
118+
# Public headers under include/; implementation-only headers under src/ are
119+
# private. The generator expression keeps install and build-tree includes correct.
120+
target_include_directories(robot
121+
PUBLIC
122+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
123+
$<INSTALL_INTERFACE:include>
124+
PRIVATE
125+
${CMAKE_CURRENT_SOURCE_DIR}/src
126+
)
127+
128+
# lodepng and the warnings flags are implementation details: PRIVATE, so they do
129+
# not propagate to consumers of the library.
130+
target_link_libraries(robot
131+
PRIVATE
132+
robot_lodepng
133+
robot_warnings
134+
)
67135

68-
# Set output directory for all targets
69-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
70-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin)
71-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin)
136+
set_target_properties(robot PROPERTIES POSITION_INDEPENDENT_CODE ON)
72137

73-
# Add the tests directory only if tests are enabled
74-
if(BUILD_ROBOT_TESTS)
138+
# ── Platform dependencies, scoped per target ──────────────────────────────────
139+
if(APPLE)
140+
find_library(APPLICATION_SERVICES ApplicationServices REQUIRED)
141+
# ApplicationServices types appear only in .cpp files (behind the backend
142+
# interfaces), so the dependency is PRIVATE. Carbon is intentionally NOT used.
143+
target_link_libraries(robot PRIVATE ${APPLICATION_SERVICES})
144+
elseif(WIN32)
145+
# user32/gdi32 for SendInput, hooks, GDI capture. Shcore and the DPI context
146+
# API are resolved at runtime (see WinDpi.cpp / WinScreenBackend.cpp), so they
147+
# are not linked here.
148+
target_link_libraries(robot PRIVATE user32 gdi32)
149+
target_compile_definitions(robot PRIVATE
150+
WIN32_LEAN_AND_MEAN NOMINMAX UNICODE _UNICODE)
151+
elseif(UNIX)
152+
find_package(X11 REQUIRED)
153+
# X11 headers are used only in platform .cpp files -> PRIVATE. XTest and
154+
# Xrandr components ship with X11; XRecord lives in libXtst as well.
155+
target_link_libraries(robot PRIVATE
156+
X11::X11 X11::Xtst X11::Xrandr)
157+
endif()
158+
159+
# ── Tests ─────────────────────────────────────────────────────────────────────
160+
if(ROBOT_BUILD_TESTS)
161+
enable_testing()
75162
add_subdirectory(tests)
76163
endif()
164+
165+
# ── Examples ──────────────────────────────────────────────────────────────────
166+
if(ROBOT_BUILD_EXAMPLES)
167+
add_subdirectory(examples)
168+
endif()
169+
170+
# ── Install / export ──────────────────────────────────────────────────────────
171+
include(GNUInstallDirs)
172+
include(CMakePackageConfigHelpers)
173+
174+
install(TARGETS robot robot_lodepng robot_warnings
175+
EXPORT robotTargets
176+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
177+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
178+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
179+
)
180+
install(DIRECTORY include/robot DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
181+
install(FILES
182+
externals/lodepng/lodepng.h
183+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/robot/external/lodepng
184+
)
185+
186+
install(EXPORT robotTargets
187+
FILE robotTargets.cmake
188+
NAMESPACE robot::
189+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/robot
190+
)
191+
192+
write_basic_package_version_file(
193+
"${CMAKE_CURRENT_BINARY_DIR}/robotConfigVersion.cmake"
194+
VERSION ${PROJECT_VERSION}
195+
COMPATIBILITY SameMajorVersion
196+
)
197+
# A minimal package config that re-finds the platform deps on the consumer side.
198+
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/robotConfig.cmake"
199+
"include(CMakeFindDependencyMacro)\n\
200+
if(UNIX AND NOT APPLE)\n\
201+
find_dependency(X11)\n\
202+
endif()\n\
203+
include(\"\${CMAKE_CURRENT_LIST_DIR}/robotTargets.cmake\")\n")
204+
205+
install(FILES
206+
"${CMAKE_CURRENT_BINARY_DIR}/robotConfig.cmake"
207+
"${CMAKE_CURRENT_BINARY_DIR}/robotConfigVersion.cmake"
208+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/robot
209+
)

‎examples/CMakeLists.txt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Small runnable programs that double as manual smoke tests. They perform real
2+
# injection, so they are examples rather than automated tests.
3+
add_executable(example_type_text type_text.cpp)
4+
target_link_libraries(example_type_text PRIVATE robot::robot)
5+
6+
add_executable(example_capture_screen capture_screen.cpp)
7+
target_link_libraries(example_capture_screen PRIVATE robot::robot)
8+
9+
add_executable(example_record_replay record_replay.cpp)
10+
target_link_libraries(example_record_replay PRIVATE robot::robot)

‎examples/capture_screen.cpp‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <print>
2+
3+
#include "robot/Robot.h"
4+
5+
// Captures the primary monitor at native pixel resolution and writes a PNG,
6+
// showing the density-correct capture model: the image dimensions are physical
7+
// pixels, so a Retina/HiDPI display yields a larger-than-logical image.
8+
int main() {
9+
auto session = robot::Session::create();
10+
if (!session) {
11+
std::println("Cannot start: {}", session.error().message);
12+
return 1;
13+
}
14+
15+
robot::Screen& screen = (*session)->screen();
16+
17+
auto primary = screen.primaryMonitor();
18+
if (!primary) {
19+
std::println("No primary monitor: {}", primary.error().message);
20+
return 1;
21+
}
22+
std::println(
23+
"Primary: {}x{} logical, scale {:.2f}",
24+
primary->logicalBounds.size.width, primary->logicalBounds.size.height,
25+
primary->scaleFactor
26+
);
27+
28+
auto image = screen.captureMonitor(primary->id);
29+
if (!image) {
30+
std::println("Capture failed: {}", image.error().message);
31+
return 1;
32+
}
33+
std::println("Captured {}x{} physical pixels", image->width(), image->height());
34+
35+
if (auto r = image->savePng("primary.png"); !r) {
36+
std::println("Save failed: {}", r.error().message);
37+
return 1;
38+
}
39+
std::println("Wrote primary.png");
40+
return 0;
41+
}

‎examples/record_replay.cpp‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <chrono>
2+
#include <print>
3+
#include <thread>
4+
5+
#include "robot/Robot.h"
6+
7+
// Records global input for a few seconds, then replays it. Recording is a
8+
// privileged, platform-limited capability, so the example checks canRecordEvents
9+
// and surfaces the exact error (permission, or Wayland unsupported) instead of
10+
// hanging or failing opaquely.
11+
int main() {
12+
auto session = robot::Session::create();
13+
if (!session) {
14+
std::println("Cannot start: {}", session.error().message);
15+
return 1;
16+
}
17+
18+
if (!(*session)->capabilities().canRecordEvents) {
19+
std::println("Event recording is not available on this backend.");
20+
return 1;
21+
}
22+
23+
robot::Recorder recorder;
24+
robot::EventTap& tap = (*session)->eventTap();
25+
26+
std::println("Recording for 5 seconds...");
27+
std::thread recordThread([&] {
28+
auto r = tap.start([&](const robot::InputEvent& e) { recorder.capture(e); });
29+
if (!r) std::println("Tap error: {}", r.error().message);
30+
});
31+
32+
std::this_thread::sleep_for(std::chrono::seconds(5));
33+
tap.stop();
34+
recordThread.join();
35+
36+
std::println("Captured {} events. Replaying in 2s...", recorder.events().size());
37+
std::this_thread::sleep_for(std::chrono::seconds(2));
38+
39+
if (auto r = recorder.replay(**session); !r) {
40+
std::println("Replay failed: {}", r.error().message);
41+
return 1;
42+
}
43+
std::println("Replay complete.");
44+
return 0;
45+
}

0 commit comments

Comments
 (0)