Skip to content

About

Desktop screen and window capture for HuxerUI on Windows, macOS, and Linux, with live previews and region capture.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Lib-DesktopCapture

Lib-DesktopCapture is a C++20 library for display and window capture on Windows, macOS, and Linux. It retains the latest full frame so a HuxerUI ExternalTexture preview and on-demand Bitmap reads can share one capture session. Applications link HuxerUI::DesktopCapture and use the huxerui::desktop_capture namespace. HuxerUI · Public API

Integration

Use CMake 3.20 or later and load the HuxerUI SDK with find_package(HuxerUI CONFIG REQUIRED). After creating your application target, add one of the following declarations. Replace my_app with the target name.

Git tag

Replace <release-tag> with the plain name of a published Git tag that contains the library sources. Git tags

huxerui_use_library(my_app
    TARGET HuxerUI::DesktopCapture
    URL "https://github.com/HuxerUI/Lib-DesktopCapture.git"
    TAG "<release-tag>"
)

Git commit

Replace <full-commit-sha> with the full 40-character SHA of a commit that contains the library sources. Commit history

huxerui_use_library(my_app
    TARGET HuxerUI::DesktopCapture
    URL "https://github.com/HuxerUI/Lib-DesktopCapture.git"
    COMMIT "<full-commit-sha>"
)

Local checkout

Use PATH to build against a local checkout, such as a library directory beside your application.

huxerui_use_library(my_app
    TARGET HuxerUI::DesktopCapture
    PATH "${CMAKE_CURRENT_SOURCE_DIR}/../Lib-DesktopCapture"
)

Quick start

List sources with EnumerateSources() and pass the user's selection to DesktopCaptureSession::Start(). On Wayland, the entry returned by enumeration opens the system source picker. In GTK applications, query capabilities and enumerate sources on the application thread.

#include <optional>

#include <huxerui/desktop_capture.h>
#include <huxerui/huxerui.h>

namespace capture = huxerui::desktop_capture;

const auto center_crop = capture::CaptureRegion::Normalized({0.25F, 0.25F, 0.5F, 0.5F});

auto StartCapture(const capture::CaptureSource& source) {
    return capture::DesktopCaptureSession::Start(source, {.region = center_crop, .preview = true});
}

huxerui::View MakePreview(const capture::DesktopCaptureSession& session) {
    if (const auto preview = session.Preview(); preview.texture) {
        return huxerui::Image(preview.texture).Fit(huxerui::ImageFit::Contain);
    }
    return huxerui::Spacer();
}

std::optional<huxerui::Bitmap> ReadCrop(const capture::DesktopCaptureSession& session) {
    if (const auto frame = session.LatestFrame()) {
        return frame->ReadBitmap(center_crop);
    }
    return std::nullopt;
}

Keep the session returned by StartCapture() alive while sampling. Capture starts asynchronously. Read GetStatus() to track startup, picker cancellation, permission errors, and source loss. Stop() joins the worker and releases capture resources. A retained CapturedFrame remains readable after the session stops.

Frames and regions

LatestFrame() retains the latest full frame as immutable, top-down RGBA8 pixels at scale 1. Hold a single CapturedFrame to read several regions from the same revision. A full-frame ReadBitmap() shares pixel storage, and reading a smaller region copies the selected pixels. Filters and QR decoding run in the application.

SetRegion() changes the crop published to the preview. The preview revision advances when the crop moves, its pixels change, or the crop is cleared. Rebind the ExternalTexture when its identity changes, such as when the crop changes size or re-enters the frame. A live texture receives subsequent pixels at the same size.

Region Coordinates
CaptureRegion{} Full source frame.
CaptureRegion::InPixels() Pixels measured from the top left of the full frame.
CaptureRegion::Normalized() Fractions of the full frame width and height. Map pointer positions relative to the fitted image when the preview has margins.
CaptureRegion::InDesktop() Desktop coordinates supplied by the selected backend. Available when the frame carries desktop bounds.

Region bounds are rounded outward and clipped to the captured frame. An empty intersection yields no crop. A desktop region spanning displays is clipped to the selected display.

Following a desktop window

Check QueryCapabilities() for desktop coordinates and window exclusion. On the application thread, call LocateWindow() for an attached HuxerUI window. Use the returned DesktopWindowPlacement::ToDesktop() to map the sampling rectangle from client DIPs to the backend's desktop coordinates. Query the placement again as the window moves, resizes, or changes DPI. Pass the NativeWindowId of each owned window to omit through CaptureSessionOptions::exclude when starting display capture.

Platform support

Platform Capture sources Desktop coordinates Own-window exclusion from display capture
Windows 10 version 1903 or later Displays and windows through Windows Graphics Capture. Physical pixels with per-window DPI for client DIPs. Windows 10 version 2004 or later, when the operating system accepts display affinity for the owned top-level window. The library restores the previous affinity on stop.
macOS 12.3 or later Displays and windows through ScreenCaptureKit. Screen Recording permission is required. Quartz points mapped to frame pixels. Supported through the ScreenCaptureKit window filter.
Linux X11 XRandR displays and XComposite windows. The root screen is used when XRandR monitor enumeration is unavailable. X11 root pixels with the GDK surface scale for client DIPs. Unavailable. Window capture isolates the selected window.
Linux Wayland Displays or windows offered by the XDG Desktop Portal ScreenCast interface. Use normalized coordinates or frame pixels. Unavailable.

Windows builds require a Windows SDK with C++/WinRT. macOS builds use ScreenCaptureKit from the host SDK. Linux builds require GTK 4.14 or later and development packages for XCB, XCB RandR, XCB Composite, GIO, and PipeWire 0.3. Wayland capture requires a running ScreenCast portal and a CPU-mappable PipeWire video stream in BGRx, BGRA, RGBx, or RGBA format.

On Windows, the display affinity set for excluded windows also applies to other compatible capture tools while the session is active.

Preview

The preview application demonstrates live capture, region selection, Bitmap readback, and a transparent QR scanner. Run it from a library checkout with the HuxerUI CLI. Use the platform name for your host.

huxerui run windows

On supported desktops, choose Open test background and Open QR scanner. Move the transparent square over the sample code. The main window displays the live crop and its revision. Read Bitmap reports the pixel dimensions of that crop. The example follows the display containing the largest part of the sampling region. On Wayland, open the system picker with Choose source. On X11, select a source and choose Start source. Then draw a rectangle around the code in the fitted preview. The example generates the sample and decodes the captured region with Lib-QRCode.

Verification

Build the example and the Windows or Linux verification executable by running these commands from the library root.

cmake -S . -B build/verify -G Ninja \
    -DHUXERUI_HOME=/path/to/huxerui-sdk \
    -DHUXERUI_DESKTOP_CAPTURE_BUILD_EXAMPLE=ON \
    -DCMAKE_BUILD_TYPE=Release
cmake --build build/verify

Run build/verify/tests/desktop_capture_verify.exe on Windows. On Linux, use an isolated display to run the X11 checks.

xvfb-run -a env GDK_BACKEND=x11 build/verify/tests/desktop_capture_verify
Environment Verification
Windows 11, build 26200 Display and isolated window capture, stationary deduplication, moving crop, background-only updates, and pixel-verified exclusion with display affinity restored on stop.
Ubuntu 24.04, X11 under Xvfb Display and isolated window capture, stationary deduplication, moving crop, background-only updates, and an unsupported result for display exclusions. The preview also scanned a sample code from a window and read back the selected Bitmap.
Ubuntu 24.04, headless Wayland on WSL Portal-selected display, PipeWire frames, normalized crop, stationary deduplication, and background-only updates. The fixture used a preselected output and an SHM-enabled wlroots portal build. Interactive picker UI and window selection remain unverified.
macOS ScreenCaptureKit build and runtime verification are pending.

Mixed-DPI checks cover simulated scale factors of 1.25 and 2, negative origins, and clipping. Verification on a physical mixed-DPI display setup is pending.

License

Lib-DesktopCapture is distributed under the MIT License.

About

Desktop screen and window capture for HuxerUI on Windows, macOS, and Linux, with live previews and region capture.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages