Skip to content

Commit ecdf94e

Browse files
committed
Add screenshot and screen recording APIs for macOS and Windows
1 parent 5086bdb commit ecdf94e

33 files changed

Lines changed: 3585 additions & 0 deletions

‎include/nativeapi.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../src/accessibility_manager.h"
99
#include "../src/app_info.h"
1010
#include "../src/application.h"
11+
#include "../src/capture_source.h"
1112
#include "../src/clipboard.h"
1213
#include "../src/device_info.h"
1314
#include "../src/dialog.h"
@@ -29,6 +30,8 @@
2930
#include "../src/placement.h"
3031
#include "../src/positioning_strategy.h"
3132
#include "../src/preferences.h"
33+
#include "../src/screen_capturer.h"
34+
#include "../src/screen_recorder.h"
3235
#include "../src/secure_storage.h"
3336
#include "../src/shortcut.h"
3437
#include "../src/shortcut_manager.h"
@@ -47,6 +50,7 @@
4750
#include "../src/capi/accessibility_manager_c.h"
4851
#include "../src/capi/app_info_c.h"
4952
#include "../src/capi/application_c.h"
53+
#include "../src/capi/capture_source_c.h"
5054
#include "../src/capi/clipboard_c.h"
5155
#include "../src/capi/color_c.h"
5256
#include "../src/capi/common_c.h"
@@ -69,6 +73,8 @@
6973
#include "../src/capi/placement_c.h"
7074
#include "../src/capi/positioning_strategy_c.h"
7175
#include "../src/capi/preferences_c.h"
76+
#include "../src/capi/screen_capturer_c.h"
77+
#include "../src/capi/screen_recorder_c.h"
7278
#include "../src/capi/secure_storage_c.h"
7379
#include "../src/capi/shortcut_c.h"
7480
#include "../src/capi/shortcut_manager_c.h"

‎src/CMakeLists.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
9898
target_link_libraries(nativeapi PUBLIC PkgConfig::GTK PkgConfig::X11 PkgConfig::XI pthread)
9999
elseif(APPLE)
100100
target_link_libraries(nativeapi PUBLIC "-framework Cocoa")
101+
target_link_libraries(nativeapi PUBLIC "-weak_framework ScreenCaptureKit" "-framework AVFoundation"
102+
"-framework CoreMedia" "-framework CoreVideo" "-framework VideoToolbox")
101103
target_link_libraries(nativeapi PUBLIC "-framework QuartzCore")
102104
target_link_libraries(nativeapi PUBLIC "-framework Carbon")
103105
target_link_libraries(nativeapi PUBLIC "-framework ServiceManagement")
@@ -111,4 +113,5 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "OHOS")
111113
set_target_properties(nativeapi PROPERTIES POSITION_INDEPENDENT_CODE ON)
112114
elseif(WIN32)
113115
target_link_libraries(nativeapi PUBLIC user32 shell32 ole32 uuid comctl32 dwmapi gdiplus crypt32 advapi32 version)
116+
target_link_libraries(nativeapi PUBLIC d3d11 dxgi windowsapp runtimeobject mfplat mfreadwrite mfuuid windowscodecs)
114117
endif ()

‎src/capi/capture_source_c.cpp‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// AUTO-GENERATED. DO NOT EDIT.
2+
// Any manual changes WILL BE LOST when this file is regenerated.
3+
4+
#include "capture_source_c.h"
5+
6+
#include <cstdio>
7+
#include <memory>
8+
#include <new>
9+
#include <optional>
10+
#include <string>
11+
#include <utility>
12+
#include <vector>
13+
14+
#include "string_utils_c.h"
15+
#include "user_data.h"
16+
#include "../foundation/handle_table.h"
17+
#include "../display.h"
18+
#include "display_c.h"
19+
#include "../window.h"
20+
#include "window_c.h"
21+
#include "../capture_source.h"
22+
23+
native_capture_source_t native_capture_source_from_display(native_display_t display) {
24+
try {
25+
auto display_cpp = nativeapi::HandleTable::GetInstance().Resolve<nativeapi::Display>(display);
26+
return nativeapi::HandleTable::GetInstance().Insert(nativeapi::CaptureSource::FromDisplay(display_cpp));
27+
} catch (...) {
28+
fprintf(stderr, "[nativeapi] %s: unexpected exception\n", "native_capture_source_from_display");
29+
return 0;
30+
}
31+
}
32+
33+
native_capture_source_t native_capture_source_from_window(native_window_t window) {
34+
try {
35+
auto window_cpp = nativeapi::HandleTable::GetInstance().Resolve<nativeapi::Window>(window);
36+
return nativeapi::HandleTable::GetInstance().Insert(nativeapi::CaptureSource::FromWindow(window_cpp));
37+
} catch (...) {
38+
fprintf(stderr, "[nativeapi] %s: unexpected exception\n", "native_capture_source_from_window");
39+
return 0;
40+
}
41+
}
42+
43+
native_capture_source_type_t native_capture_source_get_type(native_capture_source_t capture_source) {
44+
auto self = nativeapi::HandleTable::GetInstance().Resolve<nativeapi::CaptureSource>(capture_source);
45+
if (!self) {
46+
return (native_capture_source_type_t)NATIVE_CAPTURE_SOURCE_TYPE_NONE;
47+
}
48+
try {
49+
return to_c_capture_source_type(self->GetType());
50+
} catch (...) {
51+
fprintf(stderr, "[nativeapi] %s: unexpected exception\n", "native_capture_source_get_type");
52+
return (native_capture_source_type_t)NATIVE_CAPTURE_SOURCE_TYPE_NONE;
53+
}
54+
}
55+
56+
char* native_capture_source_get_name(native_capture_source_t capture_source) {
57+
auto self = nativeapi::HandleTable::GetInstance().Resolve<nativeapi::CaptureSource>(capture_source);
58+
if (!self) {
59+
return nullptr;
60+
}
61+
try {
62+
return to_c_str(self->GetName());
63+
} catch (...) {
64+
fprintf(stderr, "[nativeapi] %s: unexpected exception\n", "native_capture_source_get_name");
65+
return nullptr;
66+
}
67+
}
68+
69+
void native_capture_source_free(native_capture_source_t capture_source) {
70+
// The table invalidates the handle itself, so releasing an unknown or
71+
// already-released one is a no-op rather than a double free.
72+
nativeapi::HandleTable::GetInstance().Release(capture_source);
73+
}
74+

‎src/capi/capture_source_c.h‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// AUTO-GENERATED. DO NOT EDIT.
2+
// Any manual changes WILL BE LOST when this file is regenerated.
3+
4+
#pragma once
5+
6+
#include <stdbool.h>
7+
#include <stdint.h>
8+
9+
#include "common_c.h"
10+
typedef uint64_t native_display_t;
11+
typedef uint64_t native_window_t;
12+
13+
#include "display_c.h"
14+
#include "window_c.h"
15+
16+
#if _WIN32
17+
#define FFI_PLUGIN_EXPORT __declspec(dllexport)
18+
#else
19+
#define FFI_PLUGIN_EXPORT
20+
#endif
21+
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
26+
typedef enum {
27+
NATIVE_CAPTURE_SOURCE_TYPE_NONE = 0,
28+
NATIVE_CAPTURE_SOURCE_TYPE_DISPLAY = 1,
29+
NATIVE_CAPTURE_SOURCE_TYPE_WINDOW = 2,
30+
NATIVE_CAPTURE_SOURCE_TYPE_UNKNOWN = 3,
31+
} native_capture_source_type_t;
32+
33+
/// Opaque CaptureSource handle.
34+
///
35+
/// A generational index into the library's handle table, NOT a pointer:
36+
/// never dereference it, and compare it against NATIVE_INVALID_CAPTURE_SOURCE rather than NULL.
37+
/// Releasing a handle invalidates it; later calls fail safely instead of
38+
/// touching freed memory.
39+
typedef uint64_t native_capture_source_t;
40+
41+
/// Never refers to a live CaptureSource.
42+
#define NATIVE_INVALID_CAPTURE_SOURCE ((native_capture_source_t)0)
43+
44+
/// Caller owns the returned handle; release it with native_capture_source_free().
45+
FFI_PLUGIN_EXPORT
46+
native_capture_source_t native_capture_source_from_display(native_display_t display);
47+
48+
/// Caller owns the returned handle; release it with native_capture_source_free().
49+
FFI_PLUGIN_EXPORT
50+
native_capture_source_t native_capture_source_from_window(native_window_t window);
51+
52+
FFI_PLUGIN_EXPORT
53+
native_capture_source_type_t native_capture_source_get_type(native_capture_source_t capture_source);
54+
55+
/// Caller owns the returned string; free it with free_c_str().
56+
FFI_PLUGIN_EXPORT
57+
char* native_capture_source_get_name(native_capture_source_t capture_source);
58+
59+
/// Releases the caller's reference. Safe to call with an invalid or
60+
/// already-released handle.
61+
FFI_PLUGIN_EXPORT
62+
void native_capture_source_free(native_capture_source_t capture_source);
63+
64+
#ifdef __cplusplus
65+
}
66+
#endif
67+
68+
#ifdef __cplusplus
69+
#include "../capture_source.h"
70+
#include "string_utils_c.h"
71+
#include "user_data.h"
72+
73+
// Conversion helpers between these C types and their C++ originals.
74+
75+
inline native_capture_source_type_t to_c_capture_source_type(nativeapi::CaptureSourceType value);
76+
inline nativeapi::CaptureSourceType to_cpp_capture_source_type(native_capture_source_type_t value);
77+
78+
inline native_capture_source_type_t to_c_capture_source_type(nativeapi::CaptureSourceType value) {
79+
switch (value) {
80+
case nativeapi::CaptureSourceType::None:
81+
return NATIVE_CAPTURE_SOURCE_TYPE_NONE;
82+
case nativeapi::CaptureSourceType::Display:
83+
return NATIVE_CAPTURE_SOURCE_TYPE_DISPLAY;
84+
case nativeapi::CaptureSourceType::Window:
85+
return NATIVE_CAPTURE_SOURCE_TYPE_WINDOW;
86+
case nativeapi::CaptureSourceType::Unknown:
87+
return NATIVE_CAPTURE_SOURCE_TYPE_UNKNOWN;
88+
default:
89+
return NATIVE_CAPTURE_SOURCE_TYPE_NONE;
90+
}
91+
}
92+
93+
inline nativeapi::CaptureSourceType to_cpp_capture_source_type(native_capture_source_type_t value) {
94+
switch (value) {
95+
case NATIVE_CAPTURE_SOURCE_TYPE_NONE:
96+
return nativeapi::CaptureSourceType::None;
97+
case NATIVE_CAPTURE_SOURCE_TYPE_DISPLAY:
98+
return nativeapi::CaptureSourceType::Display;
99+
case NATIVE_CAPTURE_SOURCE_TYPE_WINDOW:
100+
return nativeapi::CaptureSourceType::Window;
101+
case NATIVE_CAPTURE_SOURCE_TYPE_UNKNOWN:
102+
return nativeapi::CaptureSourceType::Unknown;
103+
default:
104+
return static_cast<nativeapi::CaptureSourceType>(value);
105+
}
106+
}
107+
108+
#endif // __cplusplus

‎src/capi/color_c.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,9 @@ unsigned int native_color_to_argb(native_color_t color) {
7676
}
7777
}
7878

79+
void native_color_free(native_color_t* value) {
80+
if (!value) {
81+
return;
82+
}
83+
}
84+

‎src/capi/color_c.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ unsigned int native_color_to_rgba(native_color_t color);
6464
FFI_PLUGIN_EXPORT
6565
unsigned int native_color_to_argb(native_color_t color);
6666

67+
/// Frees everything the struct owns.
68+
FFI_PLUGIN_EXPORT
69+
void native_color_free(native_color_t* value);
70+
6771
#ifdef __cplusplus
6872
}
6973
#endif

‎src/capi/geometry_c.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@
1616
#include "../foundation/handle_table.h"
1717
#include "../foundation/geometry.h"
1818

19+
void native_point_free(native_point_t* value) {
20+
if (!value) {
21+
return;
22+
}
23+
}
24+
25+
void native_size_free(native_size_t* value) {
26+
if (!value) {
27+
return;
28+
}
29+
}
30+
31+
void native_rectangle_free(native_rectangle_t* value) {
32+
if (!value) {
33+
return;
34+
}
35+
}
36+
1937
native_edge_insets_t native_edge_insets_all(double value) {
2038
try {
2139
const auto cpp_result = nativeapi::EdgeInsets::All(value);
@@ -38,3 +56,9 @@ native_edge_insets_t native_edge_insets_symmetric(double vertical, double horizo
3856
}
3957
}
4058

59+
void native_edge_insets_free(native_edge_insets_t* value) {
60+
if (!value) {
61+
return;
62+
}
63+
}
64+

‎src/capi/geometry_c.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,28 @@ typedef struct {
4242
double left;
4343
} native_edge_insets_t;
4444

45+
/// Frees everything the struct owns.
46+
FFI_PLUGIN_EXPORT
47+
void native_point_free(native_point_t* value);
48+
49+
/// Frees everything the struct owns.
50+
FFI_PLUGIN_EXPORT
51+
void native_size_free(native_size_t* value);
52+
53+
/// Frees everything the struct owns.
54+
FFI_PLUGIN_EXPORT
55+
void native_rectangle_free(native_rectangle_t* value);
56+
4557
FFI_PLUGIN_EXPORT
4658
native_edge_insets_t native_edge_insets_all(double value);
4759

4860
FFI_PLUGIN_EXPORT
4961
native_edge_insets_t native_edge_insets_symmetric(double vertical, double horizontal);
5062

63+
/// Frees everything the struct owns.
64+
FFI_PLUGIN_EXPORT
65+
void native_edge_insets_free(native_edge_insets_t* value);
66+
5167
#ifdef __cplusplus
5268
}
5369
#endif

0 commit comments

Comments
 (0)