From 518e1afa584f671ca32037c9f997a4bb5243b516 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Sun, 22 Mar 2026 21:33:50 +0800 Subject: [PATCH] [feat] add Linux screen capture fallback support for DRM and Wayland --- scripts/linux/pkg_amd64.sh | 6 +- scripts/linux/pkg_arm64.sh | 6 +- .../linux/screen_capturer_drm.cpp | 573 ++++++++++++++ .../linux/screen_capturer_drm.h | 87 +++ .../linux/screen_capturer_linux.cpp | 475 ++++++++++++ .../linux/screen_capturer_linux.h | 65 ++ .../linux/screen_capturer_wayland.cpp | 149 ++++ .../linux/screen_capturer_wayland.h | 99 +++ .../linux/screen_capturer_wayland_build.h | 30 + .../screen_capturer_wayland_pipewire.cpp | 340 +++++++++ .../linux/screen_capturer_wayland_portal.cpp | 721 ++++++++++++++++++ .../linux/screen_capturer_x11.cpp | 146 +++- .../linux/screen_capturer_x11.h | 5 +- src/screen_capturer/screen_capturer_factory.h | 4 +- xmake.lua | 229 +----- xmake/options.lua | 50 ++ xmake/platform.lua | 81 ++ xmake/targets.lua | 177 +++++ 18 files changed, 3001 insertions(+), 242 deletions(-) create mode 100644 src/screen_capturer/linux/screen_capturer_drm.cpp create mode 100644 src/screen_capturer/linux/screen_capturer_drm.h create mode 100644 src/screen_capturer/linux/screen_capturer_linux.cpp create mode 100644 src/screen_capturer/linux/screen_capturer_linux.h create mode 100644 src/screen_capturer/linux/screen_capturer_wayland.cpp create mode 100644 src/screen_capturer/linux/screen_capturer_wayland.h create mode 100644 src/screen_capturer/linux/screen_capturer_wayland_build.h create mode 100644 src/screen_capturer/linux/screen_capturer_wayland_pipewire.cpp create mode 100644 src/screen_capturer/linux/screen_capturer_wayland_portal.cpp create mode 100644 xmake/options.lua create mode 100644 xmake/platform.lua create mode 100644 xmake/targets.lua diff --git a/scripts/linux/pkg_amd64.sh b/scripts/linux/pkg_amd64.sh index bee1871..85618f4 100644 --- a/scripts/linux/pkg_amd64.sh +++ b/scripts/linux/pkg_amd64.sh @@ -42,7 +42,9 @@ Description: $DESCRIPTION Depends: libc6 (>= 2.29), libstdc++6 (>= 9), libx11-6, libxcb1, libxcb-randr0, libxcb-xtest0, libxcb-xinerama0, libxcb-shape0, libxcb-xkb1, libxcb-xfixes0, libxv1, libxtst6, libasound2, - libsndio7.0, libxcb-shm0, libpulse0 + libsndio7.0, libxcb-shm0, libpulse0, libdrm2, libdbus-1-3, + libpipewire-0.3-0, xdg-desktop-portal, + xdg-desktop-portal-gtk | xdg-desktop-portal-kde | xdg-desktop-portal-wlr Recommends: nvidia-cuda-toolkit Priority: optional Section: utils @@ -93,4 +95,4 @@ mv "$DEB_DIR.deb" "$OUTPUT_FILE" rm -rf "$DEB_DIR" -echo "✅ Deb package created: $OUTPUT_FILE" \ No newline at end of file +echo "✅ Deb package created: $OUTPUT_FILE" diff --git a/scripts/linux/pkg_arm64.sh b/scripts/linux/pkg_arm64.sh index 3e14cff..c608b85 100644 --- a/scripts/linux/pkg_arm64.sh +++ b/scripts/linux/pkg_arm64.sh @@ -42,7 +42,9 @@ Description: $DESCRIPTION Depends: libc6 (>= 2.29), libstdc++6 (>= 9), libx11-6, libxcb1, libxcb-randr0, libxcb-xtest0, libxcb-xinerama0, libxcb-shape0, libxcb-xkb1, libxcb-xfixes0, libxv1, libxtst6, libasound2, - libsndio7.0, libxcb-shm0, libpulse0 + libsndio7.0, libxcb-shm0, libpulse0, libdrm2, libdbus-1-3, + libpipewire-0.3-0, xdg-desktop-portal, + xdg-desktop-portal-gtk | xdg-desktop-portal-kde | xdg-desktop-portal-wlr Priority: optional Section: utils EOF @@ -92,4 +94,4 @@ mv "$DEB_DIR.deb" "$OUTPUT_FILE" rm -rf "$DEB_DIR" -echo "✅ Deb package created: $OUTPUT_FILE" \ No newline at end of file +echo "✅ Deb package created: $OUTPUT_FILE" diff --git a/src/screen_capturer/linux/screen_capturer_drm.cpp b/src/screen_capturer/linux/screen_capturer_drm.cpp new file mode 100644 index 0000000..9a5ce45 --- /dev/null +++ b/src/screen_capturer/linux/screen_capturer_drm.cpp @@ -0,0 +1,573 @@ +#include "screen_capturer_drm.h" + +#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM && \ + defined(__has_include) && __has_include() && \ + __has_include() +#define CROSSDESK_DRM_BUILD_ENABLED 1 +#include +#include +#elif defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM && \ + defined(__has_include) && __has_include() && \ + __has_include() +#define CROSSDESK_DRM_BUILD_ENABLED 1 +#include +#include +#else +#define CROSSDESK_DRM_BUILD_ENABLED 0 +#endif + +#if CROSSDESK_DRM_BUILD_ENABLED + +#include +#include +#include + +#include +#include +#include + +#include "libyuv.h" +#include "rd_log.h" + +namespace crossdesk { + +namespace { + +constexpr int kMaxDrmCards = 16; + +const char* ConnectorTypeName(uint32_t type) { + switch (type) { + case DRM_MODE_CONNECTOR_VGA: + return "VGA"; + case DRM_MODE_CONNECTOR_DVII: + return "DVI-I"; + case DRM_MODE_CONNECTOR_DVID: + return "DVI-D"; + case DRM_MODE_CONNECTOR_DVIA: + return "DVI-A"; + case DRM_MODE_CONNECTOR_HDMIA: + return "HDMI-A"; + case DRM_MODE_CONNECTOR_HDMIB: + return "HDMI-B"; + case DRM_MODE_CONNECTOR_DisplayPort: + return "DP"; + case DRM_MODE_CONNECTOR_eDP: + return "eDP"; + case DRM_MODE_CONNECTOR_LVDS: + return "LVDS"; +#ifdef DRM_MODE_CONNECTOR_VIRTUAL + case DRM_MODE_CONNECTOR_VIRTUAL: + return "Virtual"; +#endif + default: + return "Display"; + } +} + +} // namespace + +ScreenCapturerDrm::ScreenCapturerDrm() {} + +ScreenCapturerDrm::~ScreenCapturerDrm() { Destroy(); } + +int ScreenCapturerDrm::Init(const int fps, cb_desktop_data cb) { + Destroy(); + + if (!cb) { + LOG_ERROR("DRM screen capturer callback is null"); + return -1; + } + + fps_ = std::max(1, fps); + callback_ = cb; + monitor_index_ = 0; + initial_monitor_index_ = 0; + consecutive_failures_ = 0; + display_info_list_.clear(); + outputs_.clear(); + y_plane_.clear(); + uv_plane_.clear(); + + if (!DiscoverOutputs()) { + LOG_ERROR("DRM screen capturer could not find active outputs"); + callback_ = nullptr; + CloseDevices(); + return -1; + } + + return 0; +} + +int ScreenCapturerDrm::Destroy() { + Stop(); + callback_ = nullptr; + display_info_list_.clear(); + outputs_.clear(); + y_plane_.clear(); + uv_plane_.clear(); + CloseDevices(); + return 0; +} + +int ScreenCapturerDrm::Start(bool show_cursor) { + if (running_) { + return 0; + } + + if (outputs_.empty()) { + LOG_ERROR("DRM screen capturer has no output to capture"); + return -1; + } + + show_cursor_ = show_cursor; + paused_ = false; + + int probe_index = monitor_index_.load(); + if (probe_index < 0 || probe_index >= static_cast(outputs_.size())) { + probe_index = 0; + } + + if (!CaptureOutputFrame(outputs_[probe_index], false)) { + LOG_ERROR("DRM start probe failed on output {}", outputs_[probe_index].name); + return -1; + } + + running_ = true; + thread_ = std::thread([this]() { CaptureLoop(); }); + return 0; +} + +int ScreenCapturerDrm::Stop() { + if (!running_) { + return 0; + } + + running_ = false; + if (thread_.joinable()) { + thread_.join(); + } + return 0; +} + +int ScreenCapturerDrm::Pause([[maybe_unused]] int monitor_index) { + paused_ = true; + return 0; +} + +int ScreenCapturerDrm::Resume([[maybe_unused]] int monitor_index) { + paused_ = false; + return 0; +} + +int ScreenCapturerDrm::SwitchTo(int monitor_index) { + if (monitor_index < 0 || + monitor_index >= static_cast(display_info_list_.size())) { + LOG_ERROR("Invalid DRM monitor index: {}", monitor_index); + return -1; + } + + monitor_index_ = monitor_index; + return 0; +} + +int ScreenCapturerDrm::ResetToInitialMonitor() { + monitor_index_ = initial_monitor_index_; + return 0; +} + +std::vector ScreenCapturerDrm::GetDisplayInfoList() { + return display_info_list_; +} + +bool ScreenCapturerDrm::DiscoverOutputs() { + for (int card_index = 0; card_index < kMaxDrmCards; ++card_index) { + const std::string card_path = "/dev/dri/card" + std::to_string(card_index); + const int fd = open(card_path.c_str(), O_RDWR | O_CLOEXEC); + if (fd < 0) { + continue; + } + + drmModeRes* resources = drmModeGetResources(fd); + if (!resources) { + close(fd); + continue; + } + + DrmDevice device; + device.fd = fd; + device.path = card_path; + devices_.push_back(device); + const int device_slot = static_cast(devices_.size()) - 1; + const size_t output_count_before = outputs_.size(); + + for (int i = 0; i < resources->count_connectors; ++i) { + drmModeConnector* connector = + drmModeGetConnector(fd, resources->connectors[i]); + if (!connector) { + continue; + } + + if (connector->connection != DRM_MODE_CONNECTED || + connector->count_modes <= 0) { + drmModeFreeConnector(connector); + continue; + } + + uint32_t crtc_id = 0; + if (connector->encoder_id != 0) { + drmModeEncoder* encoder = drmModeGetEncoder(fd, connector->encoder_id); + if (encoder) { + crtc_id = encoder->crtc_id; + drmModeFreeEncoder(encoder); + } + } + + if (crtc_id == 0) { + for (int enc_idx = 0; enc_idx < connector->count_encoders; ++enc_idx) { + drmModeEncoder* encoder = + drmModeGetEncoder(fd, connector->encoders[enc_idx]); + if (!encoder) { + continue; + } + if (encoder->crtc_id != 0) { + crtc_id = encoder->crtc_id; + drmModeFreeEncoder(encoder); + break; + } + drmModeFreeEncoder(encoder); + } + } + + if (crtc_id == 0) { + drmModeFreeConnector(connector); + continue; + } + + drmModeCrtc* crtc = drmModeGetCrtc(fd, crtc_id); + if (!crtc || !crtc->mode_valid || crtc->width <= 0 || crtc->height <= 0) { + if (crtc) { + drmModeFreeCrtc(crtc); + } + drmModeFreeConnector(connector); + continue; + } + + DrmOutput output; + output.device_index = device_slot; + output.connector_id = connector->connector_id; + output.crtc_id = crtc_id; + output.left = crtc->x; + output.top = crtc->y; + output.width = static_cast(crtc->width); + output.height = static_cast(crtc->height); + output.name = std::string(ConnectorTypeName(connector->connector_type)) + + std::to_string(connector->connector_type_id); + + outputs_.push_back(output); + display_info_list_.push_back( + DisplayInfo(output.name, output.left, output.top, + output.left + output.width, output.top + output.height)); + + LOG_INFO("DRM output found: {} on {}, {}x{} @ ({}, {})", output.name, + card_path, output.width, output.height, output.left, output.top); + + drmModeFreeCrtc(crtc); + drmModeFreeConnector(connector); + } + + drmModeFreeResources(resources); + + if (outputs_.size() == output_count_before) { + close(fd); + devices_.pop_back(); + } + } + + if (outputs_.empty()) { + return false; + } + + LOG_INFO("DRM screen capturer discovered {} output(s)", outputs_.size()); + return true; +} + +void ScreenCapturerDrm::CloseDevices() { + for (auto& device : devices_) { + if (device.fd >= 0) { + close(device.fd); + device.fd = -1; + } + } + devices_.clear(); +} + +void ScreenCapturerDrm::CaptureLoop() { + using clock = std::chrono::steady_clock; + const auto frame_interval = + std::chrono::milliseconds(std::max(1, 1000 / std::max(1, fps_))); + + while (running_) { + const auto frame_start = clock::now(); + if (!paused_) { + int index = monitor_index_.load(); + if (index >= 0 && index < static_cast(outputs_.size())) { + const bool ok = CaptureOutputFrame(outputs_[index], true); + if (!ok) { + ++consecutive_failures_; + if (consecutive_failures_ == 1 || consecutive_failures_ % 60 == 0) { + LOG_WARN("DRM capture failed (consecutive={})", + consecutive_failures_); + } + } else { + consecutive_failures_ = 0; + } + } + } + + const auto elapsed = std::chrono::duration_cast( + clock::now() - frame_start); + if (elapsed < frame_interval) { + std::this_thread::sleep_for(frame_interval - elapsed); + } + } +} + +bool ScreenCapturerDrm::CaptureOutputFrame(const DrmOutput& output, + bool emit_callback) { + if (output.device_index < 0 || + output.device_index >= static_cast(devices_.size())) { + return false; + } + + const int fd = devices_[output.device_index].fd; + if (fd < 0) { + return false; + } + + drmModeCrtc* crtc = drmModeGetCrtc(fd, output.crtc_id); + if (!crtc) { + return false; + } + + const uint32_t fb_id = crtc->buffer_id; + drmModeFreeCrtc(crtc); + if (fb_id == 0) { + return false; + } + + drmModeFB* fb = drmModeGetFB(fd, fb_id); + if (!fb) { + return false; + } + + const uint32_t handle = fb->handle; + const uint32_t pitch = fb->pitch; + const int src_width = static_cast(fb->width); + const int src_height = static_cast(fb->height); + const int bpp = static_cast(fb->bpp); + drmModeFreeFB(fb); + + if (handle == 0 || pitch == 0 || src_width <= 1 || src_height <= 1) { + return false; + } + + if (bpp != 32) { + LOG_WARN("DRM capture unsupported bpp: {}", bpp); + return false; + } + + const size_t map_size = + static_cast(pitch) * static_cast(src_height); + uint8_t* mapped_ptr = nullptr; + size_t mapped_size = 0; + int prime_fd = -1; + if (!MapFramebuffer(fd, handle, map_size, &mapped_ptr, &mapped_size, + &prime_fd)) { + return false; + } + + int capture_width = std::min(src_width, output.width); + int capture_height = std::min(src_height, output.height); + if (capture_width <= 0 || capture_height <= 0) { + capture_width = src_width; + capture_height = src_height; + } + + capture_width &= ~1; + capture_height &= ~1; + if (capture_width <= 1 || capture_height <= 1) { + UnmapFramebuffer(mapped_ptr, mapped_size, prime_fd); + return false; + } + + const size_t y_size = + static_cast(capture_width) * static_cast(capture_height); + const size_t uv_size = y_size / 2; + if (y_plane_.size() != y_size) { + y_plane_.resize(y_size); + } + if (uv_plane_.size() != uv_size) { + uv_plane_.resize(uv_size); + } + + const int convert_ret = + libyuv::ARGBToNV12(mapped_ptr, static_cast(pitch), y_plane_.data(), + capture_width, uv_plane_.data(), capture_width, + capture_width, capture_height); + + if (convert_ret != 0) { + UnmapFramebuffer(mapped_ptr, mapped_size, prime_fd); + return false; + } + + std::vector nv12; + nv12.reserve(y_plane_.size() + uv_plane_.size()); + nv12.insert(nv12.end(), y_plane_.begin(), y_plane_.end()); + nv12.insert(nv12.end(), uv_plane_.begin(), uv_plane_.end()); + + if (emit_callback && callback_) { + callback_(nv12.data(), static_cast(nv12.size()), capture_width, + capture_height, output.name.c_str()); + } + + UnmapFramebuffer(mapped_ptr, mapped_size, prime_fd); + return true; +} + +bool ScreenCapturerDrm::MapFramebuffer(int fd, uint32_t handle, size_t map_size, + uint8_t** mapped_ptr, + size_t* mapped_size, + int* prime_fd) const { + if (!mapped_ptr || !mapped_size || !prime_fd || map_size == 0) { + return false; + } + + *mapped_ptr = nullptr; + *mapped_size = 0; + *prime_fd = -1; + + drm_mode_map_dumb map_arg{}; + map_arg.handle = handle; + if (drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg) == 0) { + void* mapped = mmap(nullptr, map_size, PROT_READ, MAP_SHARED, fd, + static_cast(map_arg.offset)); + if (mapped != MAP_FAILED) { + *mapped_ptr = static_cast(mapped); + *mapped_size = map_size; + return true; + } + } + + int dma_fd = -1; + if (drmPrimeHandleToFD(fd, handle, DRM_CLOEXEC, &dma_fd) == 0) { + size_t dma_map_size = map_size; + const off_t fd_size = lseek(dma_fd, 0, SEEK_END); + if (fd_size > 0) { + dma_map_size = std::min(map_size, static_cast(fd_size)); + } + + void* mapped = + mmap(nullptr, dma_map_size, PROT_READ, MAP_SHARED, dma_fd, 0); + if (mapped != MAP_FAILED) { + *mapped_ptr = static_cast(mapped); + *mapped_size = dma_map_size; + *prime_fd = dma_fd; + return true; + } + + close(dma_fd); + } + + return false; +} + +void ScreenCapturerDrm::UnmapFramebuffer(uint8_t* mapped_ptr, size_t mapped_size, + int prime_fd) const { + if (mapped_ptr && mapped_size > 0) { + munmap(mapped_ptr, mapped_size); + } + + if (prime_fd >= 0) { + close(prime_fd); + } +} + +} // namespace crossdesk + +#else + +#include "rd_log.h" + +namespace crossdesk { + +ScreenCapturerDrm::ScreenCapturerDrm() {} + +ScreenCapturerDrm::~ScreenCapturerDrm() { Destroy(); } + +int ScreenCapturerDrm::Init([[maybe_unused]] const int fps, cb_desktop_data cb) { + Destroy(); + callback_ = cb; + LOG_WARN("DRM screen capturer disabled: libdrm headers not available"); + return -1; +} + +int ScreenCapturerDrm::Destroy() { + Stop(); + callback_ = nullptr; + display_info_list_.clear(); + outputs_.clear(); + return 0; +} + +int ScreenCapturerDrm::Start([[maybe_unused]] bool show_cursor) { return -1; } + +int ScreenCapturerDrm::Stop() { + running_ = false; + if (thread_.joinable()) { + thread_.join(); + } + return 0; +} + +int ScreenCapturerDrm::Pause([[maybe_unused]] int monitor_index) { return 0; } + +int ScreenCapturerDrm::Resume([[maybe_unused]] int monitor_index) { return 0; } + +int ScreenCapturerDrm::SwitchTo([[maybe_unused]] int monitor_index) { + return -1; +} + +int ScreenCapturerDrm::ResetToInitialMonitor() { return 0; } + +std::vector ScreenCapturerDrm::GetDisplayInfoList() { + return display_info_list_; +} + +bool ScreenCapturerDrm::DiscoverOutputs() { return false; } + +void ScreenCapturerDrm::CloseDevices() {} + +void ScreenCapturerDrm::CaptureLoop() {} + +bool ScreenCapturerDrm::CaptureOutputFrame( + [[maybe_unused]] const DrmOutput& output, + [[maybe_unused]] bool emit_callback) { + return false; +} + +bool ScreenCapturerDrm::MapFramebuffer([[maybe_unused]] int fd, + [[maybe_unused]] uint32_t handle, + [[maybe_unused]] size_t map_size, + [[maybe_unused]] uint8_t** mapped_ptr, + [[maybe_unused]] size_t* mapped_size, + [[maybe_unused]] int* prime_fd) const { + return false; +} + +void ScreenCapturerDrm::UnmapFramebuffer([[maybe_unused]] uint8_t* mapped_ptr, + [[maybe_unused]] size_t mapped_size, + [[maybe_unused]] int prime_fd) const {} + +} // namespace crossdesk + +#endif diff --git a/src/screen_capturer/linux/screen_capturer_drm.h b/src/screen_capturer/linux/screen_capturer_drm.h new file mode 100644 index 0000000..641edde --- /dev/null +++ b/src/screen_capturer/linux/screen_capturer_drm.h @@ -0,0 +1,87 @@ +/* + * @Author: DI JUNKUN + * @Date: 2026-03-22 + * Copyright (c) 2026 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _SCREEN_CAPTURER_DRM_H_ +#define _SCREEN_CAPTURER_DRM_H_ + +#include +#include +#include +#include +#include + +#include "screen_capturer.h" + +namespace crossdesk { + +class ScreenCapturerDrm : public ScreenCapturer { + public: + ScreenCapturerDrm(); + ~ScreenCapturerDrm(); + + public: + int Init(const int fps, cb_desktop_data cb) override; + int Destroy() override; + int Start(bool show_cursor) override; + int Stop() override; + + int Pause(int monitor_index) override; + int Resume(int monitor_index) override; + + int SwitchTo(int monitor_index) override; + int ResetToInitialMonitor() override; + + std::vector GetDisplayInfoList() override; + + private: + struct DrmDevice { + int fd = -1; + std::string path; + }; + + struct DrmOutput { + int device_index = -1; + uint32_t connector_id = 0; + uint32_t crtc_id = 0; + std::string name; + int left = 0; + int top = 0; + int width = 0; + int height = 0; + }; + + private: + bool DiscoverOutputs(); + void CloseDevices(); + void CaptureLoop(); + bool CaptureOutputFrame(const DrmOutput& output, bool emit_callback = true); + bool MapFramebuffer(int fd, uint32_t handle, size_t map_size, + uint8_t** mapped_ptr, size_t* mapped_size, + int* prime_fd) const; + void UnmapFramebuffer(uint8_t* mapped_ptr, size_t mapped_size, + int prime_fd) const; + + private: + std::vector devices_; + std::vector outputs_; + std::vector display_info_list_; + std::thread thread_; + std::atomic running_{false}; + std::atomic paused_{false}; + std::atomic monitor_index_{0}; + int initial_monitor_index_ = 0; + std::atomic show_cursor_{true}; + int fps_ = 60; + cb_desktop_data callback_; + int consecutive_failures_ = 0; + + std::vector y_plane_; + std::vector uv_plane_; +}; + +} // namespace crossdesk + +#endif diff --git a/src/screen_capturer/linux/screen_capturer_linux.cpp b/src/screen_capturer/linux/screen_capturer_linux.cpp new file mode 100644 index 0000000..5fca5a1 --- /dev/null +++ b/src/screen_capturer/linux/screen_capturer_linux.cpp @@ -0,0 +1,475 @@ +#include "screen_capturer_linux.h" + +#include +#include +#include +#include +#include + +#include "rd_log.h" +#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM +#include "screen_capturer_drm.h" +#endif +#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER +#include "screen_capturer_wayland.h" +#endif +#include "screen_capturer_x11.h" + +namespace crossdesk { + +namespace { + +bool IsWaylandSession() { + const char* session_type = getenv("XDG_SESSION_TYPE"); + if (session_type && strcmp(session_type, "wayland") == 0) { + return true; + } + + const char* wayland_display = getenv("WAYLAND_DISPLAY"); + return wayland_display && wayland_display[0] != '\0'; +} + +#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM +constexpr bool kDrmBuildEnabled = true; +#else +constexpr bool kDrmBuildEnabled = false; +#endif + +#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER +constexpr bool kWaylandBuildEnabled = true; +#else +constexpr bool kWaylandBuildEnabled = false; +#endif + +} // namespace + +ScreenCapturerLinux::ScreenCapturerLinux() {} + +ScreenCapturerLinux::~ScreenCapturerLinux() { Destroy(); } + +int ScreenCapturerLinux::Init(const int fps, cb_desktop_data cb) { + Destroy(); + + if (!cb) { + LOG_ERROR("Linux screen capturer callback is null"); + return -1; + } + + fps_ = fps; + callback_orig_ = std::move(cb); + callback_ = [this](unsigned char* data, int size, int width, int height, + const char* display_name) { + const std::string mapped_name = MapDisplayName(display_name); + if (callback_orig_) { + callback_orig_(data, size, width, height, mapped_name.c_str()); + } + }; + + const char* force_backend = getenv("CROSSDESK_SCREEN_BACKEND"); + if (force_backend && force_backend[0] != '\0') { + if (strcmp(force_backend, "drm") == 0) { +#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM + LOG_INFO("Linux screen capturer forced backend: DRM"); + return InitDrm(); +#else + LOG_ERROR( + "Linux screen capturer forced backend DRM is disabled at build time"); + return -1; +#endif + } + + if (strcmp(force_backend, "x11") == 0) { + LOG_INFO("Linux screen capturer forced backend: X11"); + return InitX11(); + } + if (strcmp(force_backend, "wayland") == 0) { +#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER + LOG_INFO("Linux screen capturer forced backend: Wayland"); + return InitWayland(); +#else + LOG_ERROR( + "Linux screen capturer forced backend Wayland is disabled at build " + "time"); + return -1; +#endif + } + + LOG_WARN("Unknown CROSSDESK_SCREEN_BACKEND={}, using auto strategy", + force_backend); + } + + const bool wayland_session = IsWaylandSession(); + if (wayland_session) { + if (kDrmBuildEnabled) { + LOG_INFO("Wayland session detected, prefer DRM -> X11 -> Wayland"); + if (InitDrm() == 0) { + return 0; + } + } else { + LOG_INFO("Wayland session detected, DRM disabled, prefer X11 -> Wayland"); + } + + if (InitX11() == 0) { + return 0; + } + + if (kDrmBuildEnabled) { + LOG_WARN( + "DRM and X11 init failed in Wayland session, trying Wayland portal"); + } else { + LOG_WARN("X11 init failed in Wayland session, trying Wayland portal"); + } + if (kWaylandBuildEnabled) { + return InitWayland(); + } + LOG_ERROR("Wayland session detected but Wayland backend is disabled"); + return -1; + } + + if (InitX11() == 0) { + return 0; + } + + if (kDrmBuildEnabled) { + LOG_WARN("X11 init failed, trying DRM fallback"); + return InitDrm(); + } + + LOG_ERROR("X11 init failed and DRM backend is disabled"); + return -1; +} + +int ScreenCapturerLinux::Destroy() { + if (impl_) { + impl_->Destroy(); + impl_.reset(); + } + + backend_ = BackendType::kNone; + callback_ = nullptr; + callback_orig_ = nullptr; + { + std::lock_guard lock(alias_mutex_); + canonical_displays_.clear(); + label_alias_.clear(); + } + return 0; +} + +int ScreenCapturerLinux::Start(bool show_cursor) { + if (!impl_) { + LOG_ERROR("Linux screen capturer backend is not initialized"); + return -1; + } + + const int ret = impl_->Start(show_cursor); + if (ret == 0) { + return 0; + } + + const char* backend_name = "None"; + if (backend_ == BackendType::kX11) { + backend_name = "X11"; + } else if (backend_ == BackendType::kDrm) { + backend_name = "DRM"; + } else if (backend_ == BackendType::kWayland) { + backend_name = "Wayland"; + } + + LOG_WARN("Linux screen capturer backend {} start failed: {}", + backend_name, ret); + + if (backend_ == BackendType::kX11 && kDrmBuildEnabled && + TryFallbackToDrm(show_cursor)) { + return 0; + } + if (backend_ == BackendType::kX11 && kWaylandBuildEnabled && + TryFallbackToWayland(show_cursor)) { + return 0; + } + if (backend_ == BackendType::kDrm && kDrmBuildEnabled) { + if (TryFallbackToX11(show_cursor)) { + return 0; + } + if (kWaylandBuildEnabled && TryFallbackToWayland(show_cursor)) { + return 0; + } + } + if (backend_ == BackendType::kWayland && kWaylandBuildEnabled) { + if (kDrmBuildEnabled && TryFallbackToDrm(show_cursor)) { + return 0; + } + if (TryFallbackToX11(show_cursor)) { + return 0; + } + } + + return ret; +} + +int ScreenCapturerLinux::Stop() { + if (!impl_) { + return 0; + } + return impl_->Stop(); +} + +int ScreenCapturerLinux::Pause(int monitor_index) { + if (!impl_) { + return -1; + } + return impl_->Pause(monitor_index); +} + +int ScreenCapturerLinux::Resume(int monitor_index) { + if (!impl_) { + return -1; + } + return impl_->Resume(monitor_index); +} + +int ScreenCapturerLinux::SwitchTo(int monitor_index) { + if (!impl_) { + return -1; + } + return impl_->SwitchTo(monitor_index); +} + +int ScreenCapturerLinux::ResetToInitialMonitor() { + if (!impl_) { + return -1; + } + return impl_->ResetToInitialMonitor(); +} + +std::vector ScreenCapturerLinux::GetDisplayInfoList() { + { + std::lock_guard lock(alias_mutex_); + if (!canonical_displays_.empty()) { + return canonical_displays_; + } + } + + if (!impl_) { + return std::vector(); + } + return impl_->GetDisplayInfoList(); +} + +int ScreenCapturerLinux::InitX11() { + auto backend = std::make_unique(); + const int ret = backend->Init(fps_, callback_); + if (ret != 0) { + backend->Destroy(); + LOG_WARN("Linux screen capturer X11 init failed: {}", ret); + return ret; + } + + UpdateAliasesFromBackend(backend.get()); + impl_ = std::move(backend); + backend_ = BackendType::kX11; + LOG_INFO("Linux screen capturer backend selected: X11"); + return 0; +} + +int ScreenCapturerLinux::InitDrm() { +#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM + auto backend = std::make_unique(); + const int ret = backend->Init(fps_, callback_); + if (ret != 0) { + backend->Destroy(); + LOG_WARN("Linux screen capturer DRM init failed: {}", ret); + return ret; + } + + UpdateAliasesFromBackend(backend.get()); + impl_ = std::move(backend); + backend_ = BackendType::kDrm; + LOG_INFO("Linux screen capturer backend selected: DRM"); + return 0; +#else + LOG_WARN("Linux screen capturer DRM backend is disabled at build time"); + return -1; +#endif +} + +int ScreenCapturerLinux::InitWayland() { +#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER + auto backend = std::make_unique(); + const int ret = backend->Init(fps_, callback_); + if (ret != 0) { + backend->Destroy(); + LOG_WARN("Linux screen capturer Wayland init failed: {}", ret); + return ret; + } + + UpdateAliasesFromBackend(backend.get()); + impl_ = std::move(backend); + backend_ = BackendType::kWayland; + LOG_INFO("Linux screen capturer backend selected: Wayland"); + return 0; +#else + LOG_WARN("Linux screen capturer Wayland backend is disabled at build time"); + return -1; +#endif +} + +bool ScreenCapturerLinux::TryFallbackToDrm(bool show_cursor) { +#if defined(CROSSDESK_HAS_DRM) && CROSSDESK_HAS_DRM + auto drm_backend = std::make_unique(); + int ret = drm_backend->Init(fps_, callback_); + if (ret != 0) { + LOG_ERROR("Linux screen capturer fallback DRM init failed: {}", ret); + return false; + } + + UpdateAliasesFromBackend(drm_backend.get()); + ret = drm_backend->Start(show_cursor); + if (ret != 0) { + drm_backend->Destroy(); + LOG_ERROR("Linux screen capturer fallback DRM start failed: {}", ret); + return false; + } + + if (impl_) { + impl_->Stop(); + impl_->Destroy(); + } + + impl_ = std::move(drm_backend); + backend_ = BackendType::kDrm; + LOG_INFO("Linux screen capturer fallback switched to DRM"); + return true; +#else + (void)show_cursor; + LOG_WARN("Linux screen capturer DRM fallback is disabled at build time"); + return false; +#endif +} + +bool ScreenCapturerLinux::TryFallbackToX11(bool show_cursor) { + auto x11_backend = std::make_unique(); + int ret = x11_backend->Init(fps_, callback_); + if (ret != 0) { + LOG_ERROR("Linux screen capturer fallback X11 init failed: {}", ret); + return false; + } + + UpdateAliasesFromBackend(x11_backend.get()); + ret = x11_backend->Start(show_cursor); + if (ret != 0) { + x11_backend->Destroy(); + LOG_ERROR("Linux screen capturer fallback X11 start failed: {}", ret); + return false; + } + + if (impl_) { + impl_->Stop(); + impl_->Destroy(); + } + + impl_ = std::move(x11_backend); + backend_ = BackendType::kX11; + LOG_INFO("Linux screen capturer fallback switched to X11"); + return true; +} + +bool ScreenCapturerLinux::TryFallbackToWayland(bool show_cursor) { +#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER + auto wayland_backend = std::make_unique(); + int ret = wayland_backend->Init(fps_, callback_); + if (ret != 0) { + LOG_ERROR("Linux screen capturer fallback Wayland init failed: {}", ret); + return false; + } + + UpdateAliasesFromBackend(wayland_backend.get()); + ret = wayland_backend->Start(show_cursor); + if (ret != 0) { + wayland_backend->Destroy(); + LOG_ERROR("Linux screen capturer fallback Wayland start failed: {}", ret); + return false; + } + + if (impl_) { + impl_->Stop(); + impl_->Destroy(); + } + + impl_ = std::move(wayland_backend); + backend_ = BackendType::kWayland; + LOG_INFO("Linux screen capturer fallback switched to Wayland"); + return true; +#else + (void)show_cursor; + LOG_WARN("Linux screen capturer Wayland fallback is disabled at build time"); + return false; +#endif +} + +void ScreenCapturerLinux::UpdateAliasesFromBackend(ScreenCapturer* backend) { + if (!backend) { + return; + } + + const auto backend_displays = backend->GetDisplayInfoList(); + if (backend_displays.empty()) { + return; + } + + std::lock_guard lock(alias_mutex_); + label_alias_.clear(); + + if (canonical_displays_.empty()) { + canonical_displays_ = backend_displays; + for (const auto& display : backend_displays) { + label_alias_[display.name] = display.name; + } + return; + } + + if (canonical_displays_.size() < backend_displays.size()) { + for (size_t i = canonical_displays_.size(); i < backend_displays.size(); + ++i) { + canonical_displays_.push_back(backend_displays[i]); + } + } + + for (size_t i = 0; i < backend_displays.size(); ++i) { + const std::string mapped_name = i < canonical_displays_.size() + ? canonical_displays_[i].name + : backend_displays[i].name; + label_alias_[backend_displays[i].name] = mapped_name; + + if (i < canonical_displays_.size()) { + // Keep original stable names, but refresh geometry from active backend. + canonical_displays_[i].left = backend_displays[i].left; + canonical_displays_[i].top = backend_displays[i].top; + canonical_displays_[i].right = backend_displays[i].right; + canonical_displays_[i].bottom = backend_displays[i].bottom; + canonical_displays_[i].width = backend_displays[i].width; + canonical_displays_[i].height = backend_displays[i].height; + } + } +} + +std::string ScreenCapturerLinux::MapDisplayName(const char* display_name) const { + std::string input_name = display_name ? display_name : ""; + if (input_name.empty()) { + return input_name; + } + + std::lock_guard lock(alias_mutex_); + auto it = label_alias_.find(input_name); + if (it != label_alias_.end()) { + return it->second; + } + + if (canonical_displays_.size() == 1) { + return canonical_displays_[0].name; + } + + return input_name; +} + +} // namespace crossdesk diff --git a/src/screen_capturer/linux/screen_capturer_linux.h b/src/screen_capturer/linux/screen_capturer_linux.h new file mode 100644 index 0000000..b3d76e9 --- /dev/null +++ b/src/screen_capturer/linux/screen_capturer_linux.h @@ -0,0 +1,65 @@ +/* + * @Author: DI JUNKUN + * @Date: 2026-03-22 + * Copyright (c) 2026 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _SCREEN_CAPTURER_LINUX_H_ +#define _SCREEN_CAPTURER_LINUX_H_ + +#include +#include +#include +#include +#include + +#include "screen_capturer.h" + +namespace crossdesk { + +class ScreenCapturerLinux : public ScreenCapturer { + public: + ScreenCapturerLinux(); + ~ScreenCapturerLinux(); + + public: + int Init(const int fps, cb_desktop_data cb) override; + int Destroy() override; + int Start(bool show_cursor) override; + int Stop() override; + + int Pause(int monitor_index) override; + int Resume(int monitor_index) override; + + int SwitchTo(int monitor_index) override; + int ResetToInitialMonitor() override; + + std::vector GetDisplayInfoList() override; + + private: + enum class BackendType { kNone, kX11, kDrm, kWayland }; + + private: + int InitX11(); + int InitDrm(); + int InitWayland(); + bool TryFallbackToDrm(bool show_cursor); + bool TryFallbackToX11(bool show_cursor); + bool TryFallbackToWayland(bool show_cursor); + void UpdateAliasesFromBackend(ScreenCapturer* backend); + std::string MapDisplayName(const char* display_name) const; + + private: + std::unique_ptr impl_; + BackendType backend_ = BackendType::kNone; + int fps_ = 60; + cb_desktop_data callback_; + cb_desktop_data callback_orig_; + std::vector canonical_displays_; + mutable std::mutex alias_mutex_; + std::unordered_map label_alias_; +}; + +} // namespace crossdesk + +#endif diff --git a/src/screen_capturer/linux/screen_capturer_wayland.cpp b/src/screen_capturer/linux/screen_capturer_wayland.cpp new file mode 100644 index 0000000..fc3fb80 --- /dev/null +++ b/src/screen_capturer/linux/screen_capturer_wayland.cpp @@ -0,0 +1,149 @@ +#include "screen_capturer_wayland.h" + +#include "screen_capturer_wayland_build.h" + +#if !CROSSDESK_WAYLAND_BUILD_ENABLED +#error "Wayland capturer requires USE_WAYLAND=true and Wayland development headers" +#endif + +#include +#include + +#include +#include + +#include "rd_log.h" + +namespace crossdesk { + +namespace { + +bool IsWaylandSession() { + const char* session_type = getenv("XDG_SESSION_TYPE"); + if (session_type && strcmp(session_type, "wayland") == 0) { + return true; + } + + const char* wayland_display = getenv("WAYLAND_DISPLAY"); + return wayland_display && wayland_display[0] != '\0'; +} + +} // namespace + +ScreenCapturerWayland::ScreenCapturerWayland() {} + +ScreenCapturerWayland::~ScreenCapturerWayland() { Destroy(); } + +int ScreenCapturerWayland::Init(const int fps, cb_desktop_data cb) { + Destroy(); + + if (!IsWaylandSession()) { + LOG_ERROR("Wayland screen capturer requires a Wayland session"); + return -1; + } + + if (!cb) { + LOG_ERROR("Wayland screen capturer callback is null"); + return -1; + } + + if (!CheckPortalAvailability()) { + LOG_ERROR("xdg-desktop-portal screencast service is unavailable"); + return -1; + } + + fps_ = fps; + callback_ = cb; + display_info_list_.clear(); + display_info_list_.push_back( + DisplayInfo(display_name_, 0, 0, kFallbackWidth, kFallbackHeight)); + monitor_index_ = 0; + initial_monitor_index_ = 0; + frame_width_ = kFallbackWidth; + frame_height_ = kFallbackHeight; + frame_stride_ = kFallbackWidth * 4; + y_plane_.resize(kFallbackWidth * kFallbackHeight); + uv_plane_.resize((kFallbackWidth / 2) * (kFallbackHeight / 2) * 2); + + return 0; +} + +int ScreenCapturerWayland::Destroy() { + Stop(); + y_plane_.clear(); + uv_plane_.clear(); + display_info_list_.clear(); + callback_ = nullptr; + return 0; +} + +int ScreenCapturerWayland::Start(bool show_cursor) { + if (running_) { + return 0; + } + + show_cursor_ = show_cursor; + paused_ = false; + running_ = true; + thread_ = std::thread([this]() { Run(); }); + return 0; +} + +int ScreenCapturerWayland::Stop() { + running_ = false; + if (thread_.joinable()) { + thread_.join(); + } + return 0; +} + +int ScreenCapturerWayland::Pause([[maybe_unused]] int monitor_index) { + paused_ = true; + return 0; +} + +int ScreenCapturerWayland::Resume([[maybe_unused]] int monitor_index) { + paused_ = false; + return 0; +} + +int ScreenCapturerWayland::SwitchTo(int monitor_index) { + if (monitor_index != 0) { + LOG_WARN("Wayland screencast currently supports one logical display"); + return -1; + } + + monitor_index_ = 0; + return 0; +} + +int ScreenCapturerWayland::ResetToInitialMonitor() { + monitor_index_ = initial_monitor_index_; + return 0; +} + +std::vector ScreenCapturerWayland::GetDisplayInfoList() { + return display_info_list_; +} + +void ScreenCapturerWayland::Run() { + if (!ConnectSessionBus() || !CreatePortalSession() || !SelectPortalSource() || + !StartPortalSession() || !OpenPipeWireRemote() || + !SetupPipeWireStream()) { + running_ = false; + CleanupPipeWire(); + ClosePortalSession(); + CleanupDbus(); + return; + } + + while (running_) { + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + } + + CleanupPipeWire(); + ClosePortalSession(); + CleanupDbus(); +} + +} // namespace crossdesk \ No newline at end of file diff --git a/src/screen_capturer/linux/screen_capturer_wayland.h b/src/screen_capturer/linux/screen_capturer_wayland.h new file mode 100644 index 0000000..ce4616c --- /dev/null +++ b/src/screen_capturer/linux/screen_capturer_wayland.h @@ -0,0 +1,99 @@ +/* + * @Author: DI JUNKUN + * @Date: 2026-03-22 + * Copyright (c) 2026 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _SCREEN_CAPTURER_WAYLAND_H_ +#define _SCREEN_CAPTURER_WAYLAND_H_ + +struct DBusConnection; +struct pw_context; +struct pw_core; +struct pw_stream; +struct pw_thread_loop; + +#include +#include +#include +#include +#include + +#include "screen_capturer.h" + +namespace crossdesk { + +class ScreenCapturerWayland : public ScreenCapturer { + public: + ScreenCapturerWayland(); + ~ScreenCapturerWayland(); + + public: + int Init(const int fps, cb_desktop_data cb) override; + int Destroy() override; + int Start(bool show_cursor) override; + int Stop() override; + + int Pause(int monitor_index) override; + int Resume(int monitor_index) override; + + int SwitchTo(int monitor_index) override; + int ResetToInitialMonitor() override; + + std::vector GetDisplayInfoList() override; + + private: + bool CheckPortalAvailability() const; + bool ConnectSessionBus(); + bool CreatePortalSession(); + bool SelectPortalSource(); + bool StartPortalSession(); + bool OpenPipeWireRemote(); + bool SetupPipeWireStream(); + + void Run(); + void CleanupPipeWire(); + void CleanupDbus(); + void ClosePortalSession(); + void HandlePipeWireBuffer(); + void UpdateDisplayGeometry(int width, int height); + + private: + static constexpr int kFallbackWidth = 1920; + static constexpr int kFallbackHeight = 1080; + + std::thread thread_; + std::atomic running_{false}; + std::atomic paused_{false}; + std::atomic monitor_index_{0}; + int initial_monitor_index_ = 0; + std::atomic show_cursor_{true}; + int fps_ = 60; + cb_desktop_data callback_ = nullptr; + std::vector display_info_list_; + + DBusConnection* dbus_connection_ = nullptr; + std::string session_handle_; + std::string display_name_ = "WAYLAND0"; + uint32_t pipewire_node_id_ = 0; + int pipewire_fd_ = -1; + + pw_thread_loop* pw_thread_loop_ = nullptr; + pw_context* pw_context_ = nullptr; + pw_core* pw_core_ = nullptr; + pw_stream* pw_stream_ = nullptr; + void* stream_listener_ = nullptr; + bool pipewire_initialized_ = false; + bool pipewire_thread_loop_started_ = false; + uint32_t spa_video_format_ = 0; + int frame_width_ = 0; + int frame_height_ = 0; + int frame_stride_ = 0; + + std::vector y_plane_; + std::vector uv_plane_; +}; + +} // namespace crossdesk + +#endif diff --git a/src/screen_capturer/linux/screen_capturer_wayland_build.h b/src/screen_capturer/linux/screen_capturer_wayland_build.h new file mode 100644 index 0000000..69bfb6a --- /dev/null +++ b/src/screen_capturer/linux/screen_capturer_wayland_build.h @@ -0,0 +1,30 @@ +/* + * @Author: DI JUNKUN + * @Date: 2026-03-22 + * Copyright (c) 2026 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _SCREEN_CAPTURER_WAYLAND_BUILD_H_ +#define _SCREEN_CAPTURER_WAYLAND_BUILD_H_ + +#if defined(CROSSDESK_HAS_WAYLAND_CAPTURER) && CROSSDESK_HAS_WAYLAND_CAPTURER + +#define CROSSDESK_WAYLAND_BUILD_ENABLED 1 + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#else + +#define CROSSDESK_WAYLAND_BUILD_ENABLED 0 + +#endif + +#endif diff --git a/src/screen_capturer/linux/screen_capturer_wayland_pipewire.cpp b/src/screen_capturer/linux/screen_capturer_wayland_pipewire.cpp new file mode 100644 index 0000000..2e31325 --- /dev/null +++ b/src/screen_capturer/linux/screen_capturer_wayland_pipewire.cpp @@ -0,0 +1,340 @@ +#include "screen_capturer_wayland.h" + +#include "screen_capturer_wayland_build.h" + +#if CROSSDESK_WAYLAND_BUILD_ENABLED + +#include +#include +#include + +#include + +#include "libyuv.h" +#include "rd_log.h" + +namespace crossdesk { + +namespace { + +const char* PipeWireFormatName(uint32_t spa_format) { + switch (spa_format) { + case SPA_VIDEO_FORMAT_BGRx: + return "BGRx"; + case SPA_VIDEO_FORMAT_BGRA: + return "BGRA"; + default: + return "unsupported"; + } +} + +} // namespace + +bool ScreenCapturerWayland::SetupPipeWireStream() { + if (pipewire_fd_ < 0 || pipewire_node_id_ == 0) { + return false; + } + + if (!pipewire_initialized_) { + pw_init(nullptr, nullptr); + pipewire_initialized_ = true; + } + + pw_thread_loop_ = pw_thread_loop_new("crossdesk-wayland-capture", nullptr); + if (!pw_thread_loop_) { + LOG_ERROR("Failed to create PipeWire thread loop"); + return false; + } + + if (pw_thread_loop_start(pw_thread_loop_) < 0) { + LOG_ERROR("Failed to start PipeWire thread loop"); + CleanupPipeWire(); + return false; + } + pipewire_thread_loop_started_ = true; + + pw_thread_loop_lock(pw_thread_loop_); + + pw_context_ = + pw_context_new(pw_thread_loop_get_loop(pw_thread_loop_), nullptr, 0); + if (!pw_context_) { + LOG_ERROR("Failed to create PipeWire context"); + pw_thread_loop_unlock(pw_thread_loop_); + CleanupPipeWire(); + return false; + } + + pw_core_ = pw_context_connect_fd(pw_context_, pipewire_fd_, nullptr, 0); + if (!pw_core_) { + LOG_ERROR("Failed to connect to PipeWire remote"); + pw_thread_loop_unlock(pw_thread_loop_); + CleanupPipeWire(); + return false; + } + pipewire_fd_ = -1; + + pw_stream_ = pw_stream_new( + pw_core_, "CrossDesk Wayland Capture", + pw_properties_new(PW_KEY_MEDIA_TYPE, "Video", PW_KEY_MEDIA_CATEGORY, + "Capture", PW_KEY_MEDIA_ROLE, "Screen", nullptr)); + if (!pw_stream_) { + LOG_ERROR("Failed to create PipeWire stream"); + pw_thread_loop_unlock(pw_thread_loop_); + CleanupPipeWire(); + return false; + } + + auto* listener = new spa_hook(); + stream_listener_ = listener; + + static const pw_stream_events stream_events = [] { + pw_stream_events events{}; + events.version = PW_VERSION_STREAM_EVENTS; + events.state_changed = + [](void* userdata, enum pw_stream_state old_state, + enum pw_stream_state state, const char* error_message) { + auto* self = static_cast(userdata); + if (!self) { + return; + } + + if (state == PW_STREAM_STATE_ERROR) { + LOG_ERROR("PipeWire stream error: {}", + error_message ? error_message : "unknown"); + self->running_ = false; + return; + } + + LOG_INFO("PipeWire stream state: {} -> {}", + pw_stream_state_as_string(old_state), + pw_stream_state_as_string(state)); + }; + events.param_changed = + [](void* userdata, uint32_t id, const struct spa_pod* param) { + auto* self = static_cast(userdata); + if (!self || id != SPA_PARAM_Format || !param) { + return; + } + + spa_video_info_raw info{}; + if (spa_format_video_raw_parse(param, &info) < 0) { + LOG_ERROR("Failed to parse PipeWire video format"); + return; + } + + self->spa_video_format_ = info.format; + self->frame_width_ = static_cast(info.size.width); + self->frame_height_ = static_cast(info.size.height); + self->frame_stride_ = static_cast(info.size.width) * 4; + + if (self->spa_video_format_ != SPA_VIDEO_FORMAT_BGRx && + self->spa_video_format_ != SPA_VIDEO_FORMAT_BGRA) { + LOG_ERROR("Unsupported PipeWire pixel format: {}", + PipeWireFormatName(self->spa_video_format_)); + self->running_ = false; + return; + } + + self->UpdateDisplayGeometry(self->frame_width_, self->frame_height_); + LOG_INFO("PipeWire video format: {}, {}x{}", + PipeWireFormatName(self->spa_video_format_), + self->frame_width_, self->frame_height_); + }; + events.process = [](void* userdata) { + auto* self = static_cast(userdata); + if (self) { + self->HandlePipeWireBuffer(); + } + }; + return events; + }(); + + pw_stream_add_listener(pw_stream_, listener, &stream_events, this); + + uint8_t buffer[1024]; + spa_pod_builder builder = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer)); + const spa_pod* params[1]; + const spa_rectangle min_size{1, 1}; + const spa_rectangle max_size{8192, 8192}; + const spa_rectangle default_size{kFallbackWidth, kFallbackHeight}; + const spa_fraction any_rate{0, 1}; + + params[0] = reinterpret_cast(spa_pod_builder_add_object( + &builder, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat, + SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video), + SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw), + SPA_FORMAT_VIDEO_format, SPA_POD_Id(SPA_VIDEO_FORMAT_BGRx), + SPA_FORMAT_VIDEO_size, + SPA_POD_CHOICE_RANGE_Rectangle(&default_size, &min_size, &max_size), + SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&any_rate))); + + const int ret = pw_stream_connect( + pw_stream_, PW_DIRECTION_INPUT, pipewire_node_id_, + static_cast(PW_STREAM_FLAG_AUTOCONNECT | + PW_STREAM_FLAG_MAP_BUFFERS), + params, 1); + pw_thread_loop_unlock(pw_thread_loop_); + + if (ret < 0) { + LOG_ERROR("pw_stream_connect failed: {}", spa_strerror(ret)); + CleanupPipeWire(); + return false; + } + + return true; +} + +void ScreenCapturerWayland::CleanupPipeWire() { + const bool need_lock = pw_thread_loop_ && + (pw_stream_ != nullptr || pw_core_ != nullptr || + pw_context_ != nullptr); + if (need_lock) { + pw_thread_loop_lock(pw_thread_loop_); + } + + if (pw_stream_) { + pw_stream_disconnect(pw_stream_); + pw_stream_destroy(pw_stream_); + pw_stream_ = nullptr; + } + + if (stream_listener_) { + delete static_cast(stream_listener_); + stream_listener_ = nullptr; + } + + if (pw_core_) { + pw_core_disconnect(pw_core_); + pw_core_ = nullptr; + } + + if (pw_context_) { + pw_context_destroy(pw_context_); + pw_context_ = nullptr; + } + + if (need_lock) { + pw_thread_loop_unlock(pw_thread_loop_); + } + + if (pw_thread_loop_) { + if (pipewire_thread_loop_started_) { + pw_thread_loop_stop(pw_thread_loop_); + pipewire_thread_loop_started_ = false; + } + pw_thread_loop_destroy(pw_thread_loop_); + pw_thread_loop_ = nullptr; + } + + if (pipewire_fd_ >= 0) { + close(pipewire_fd_); + pipewire_fd_ = -1; + } + + if (pipewire_initialized_) { + pw_deinit(); + pipewire_initialized_ = false; + } +} + +void ScreenCapturerWayland::HandlePipeWireBuffer() { + if (!pw_stream_) { + return; + } + + pw_buffer* buffer = pw_stream_dequeue_buffer(pw_stream_); + if (!buffer) { + return; + } + + auto requeue = [&]() { pw_stream_queue_buffer(pw_stream_, buffer); }; + + if (paused_) { + requeue(); + return; + } + + spa_buffer* spa_buffer = buffer->buffer; + if (!spa_buffer || spa_buffer->n_datas == 0 || !spa_buffer->datas[0].data) { + requeue(); + return; + } + + const spa_data& data = spa_buffer->datas[0]; + if (!data.chunk) { + requeue(); + return; + } + + if (frame_width_ <= 1 || frame_height_ <= 1) { + requeue(); + return; + } + + uint8_t* src = static_cast(data.data); + src += data.chunk->offset; + + int stride = frame_stride_; + if (data.chunk->stride > 0) { + stride = data.chunk->stride; + } else if (stride <= 0) { + stride = frame_width_ * 4; + } + + int even_width = frame_width_ & ~1; + int even_height = frame_height_ & ~1; + if (even_width <= 0 || even_height <= 0) { + requeue(); + return; + } + + const size_t y_size = static_cast(even_width) * even_height; + const size_t uv_size = y_size / 2; + if (y_plane_.size() != y_size) { + y_plane_.resize(y_size); + } + if (uv_plane_.size() != uv_size) { + uv_plane_.resize(uv_size); + } + + libyuv::ARGBToNV12(src, stride, y_plane_.data(), even_width, + uv_plane_.data(), even_width, even_width, even_height); + + std::vector nv12; + nv12.reserve(y_plane_.size() + uv_plane_.size()); + nv12.insert(nv12.end(), y_plane_.begin(), y_plane_.end()); + nv12.insert(nv12.end(), uv_plane_.begin(), uv_plane_.end()); + + if (callback_) { + callback_(nv12.data(), static_cast(nv12.size()), even_width, + even_height, display_name_.c_str()); + } + + requeue(); +} + +void ScreenCapturerWayland::UpdateDisplayGeometry(int width, int height) { + if (width <= 0 || height <= 0) { + return; + } + + frame_width_ = width; + frame_height_ = height; + + if (display_info_list_.empty()) { + display_info_list_.push_back(DisplayInfo(display_name_, 0, 0, width, height)); + return; + } + + auto& display = display_info_list_[0]; + display.left = 0; + display.top = 0; + display.right = width; + display.bottom = height; + display.width = width; + display.height = height; +} + +} // namespace crossdesk + +#endif diff --git a/src/screen_capturer/linux/screen_capturer_wayland_portal.cpp b/src/screen_capturer/linux/screen_capturer_wayland_portal.cpp new file mode 100644 index 0000000..720601a --- /dev/null +++ b/src/screen_capturer/linux/screen_capturer_wayland_portal.cpp @@ -0,0 +1,721 @@ +#include "screen_capturer_wayland.h" + +#include "screen_capturer_wayland_build.h" + +#if CROSSDESK_WAYLAND_BUILD_ENABLED + +#include +#include +#include +#include +#include + +#include "rd_log.h" + +namespace crossdesk { + +namespace { + +constexpr const char* kPortalBusName = "org.freedesktop.portal.Desktop"; +constexpr const char* kPortalObjectPath = "/org/freedesktop/portal/desktop"; +constexpr const char* kPortalScreenCastInterface = + "org.freedesktop.portal.ScreenCast"; +constexpr const char* kPortalRequestInterface = + "org.freedesktop.portal.Request"; +constexpr const char* kPortalSessionInterface = + "org.freedesktop.portal.Session"; +constexpr const char* kPortalRequestPathPrefix = + "/org/freedesktop/portal/desktop/request/"; +constexpr const char* kPortalSessionPathPrefix = + "/org/freedesktop/portal/desktop/session/"; + +constexpr uint32_t kScreenCastSourceMonitor = 1u; +constexpr uint32_t kCursorModeHidden = 1u; +constexpr uint32_t kCursorModeEmbedded = 2u; + +std::string MakeToken(const char* prefix) { + const auto now = std::chrono::steady_clock::now().time_since_epoch().count(); + return std::string(prefix) + "_" + std::to_string(now); +} + +void LogDbusError(const char* action, DBusError* error) { + if (error && dbus_error_is_set(error)) { + LOG_ERROR("{} failed: {} ({})", action, + error->message ? error->message : "unknown", + error->name ? error->name : "unknown"); + } else { + LOG_ERROR("{} failed", action); + } +} + +void AppendDictEntryString(DBusMessageIter* dict, const char* key, + const std::string& value) { + DBusMessageIter entry; + DBusMessageIter variant; + const char* key_cstr = key; + const char* value_cstr = value.c_str(); + + dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, nullptr, &entry); + dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_cstr); + dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, "s", &variant); + dbus_message_iter_append_basic(&variant, DBUS_TYPE_STRING, &value_cstr); + dbus_message_iter_close_container(&entry, &variant); + dbus_message_iter_close_container(dict, &entry); +} + +void AppendDictEntryUint32(DBusMessageIter* dict, const char* key, + uint32_t value) { + DBusMessageIter entry; + DBusMessageIter variant; + const char* key_cstr = key; + + dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, nullptr, &entry); + dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_cstr); + dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, "u", &variant); + dbus_message_iter_append_basic(&variant, DBUS_TYPE_UINT32, &value); + dbus_message_iter_close_container(&entry, &variant); + dbus_message_iter_close_container(dict, &entry); +} + +void AppendDictEntryBool(DBusMessageIter* dict, const char* key, bool value) { + DBusMessageIter entry; + DBusMessageIter variant; + const char* key_cstr = key; + dbus_bool_t bool_value = value ? TRUE : FALSE; + + dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY, nullptr, &entry); + dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key_cstr); + dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT, "b", &variant); + dbus_message_iter_append_basic(&variant, DBUS_TYPE_BOOLEAN, &bool_value); + dbus_message_iter_close_container(&entry, &variant); + dbus_message_iter_close_container(dict, &entry); +} + +bool ReadIntLike(DBusMessageIter* iter, int* value) { + if (!iter || !value) { + return false; + } + + const int type = dbus_message_iter_get_arg_type(iter); + if (type == DBUS_TYPE_INT32) { + int32_t temp = 0; + dbus_message_iter_get_basic(iter, &temp); + *value = static_cast(temp); + return true; + } + + if (type == DBUS_TYPE_UINT32) { + uint32_t temp = 0; + dbus_message_iter_get_basic(iter, &temp); + *value = static_cast(temp); + return true; + } + + return false; +} + +bool ReadPathLikeVariant(DBusMessageIter* variant, std::string* value) { + if (!variant || !value) { + return false; + } + + const int type = dbus_message_iter_get_arg_type(variant); + if (type == DBUS_TYPE_OBJECT_PATH || type == DBUS_TYPE_STRING) { + const char* temp = nullptr; + dbus_message_iter_get_basic(variant, &temp); + if (temp && temp[0] != '\0') { + *value = temp; + return true; + } + } + + return false; +} + +std::string BuildSessionHandleFromRequestPath( + const std::string& request_path, const std::string& session_handle_token) { + if (request_path.rfind(kPortalRequestPathPrefix, 0) != 0 || + session_handle_token.empty()) { + return ""; + } + + const size_t sender_start = strlen(kPortalRequestPathPrefix); + const size_t token_sep = request_path.find('/', sender_start); + if (token_sep == std::string::npos || token_sep <= sender_start) { + return ""; + } + + const std::string sender = request_path.substr(sender_start, + token_sep - sender_start); + if (sender.empty()) { + return ""; + } + + return std::string(kPortalSessionPathPrefix) + sender + "/" + + session_handle_token; +} + +struct PortalResponseState { + std::string request_path; + bool received = false; + DBusMessage* message = nullptr; +}; + +DBusHandlerResult HandlePortalResponseSignal(DBusConnection* connection, + DBusMessage* message, + void* user_data) { + auto* state = static_cast(user_data); + if (!state || !message) { + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + if (!dbus_message_is_signal(message, kPortalRequestInterface, "Response")) { + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + const char* path = dbus_message_get_path(message); + if (!path || state->request_path != path) { + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + if (state->message) { + dbus_message_unref(state->message); + state->message = nullptr; + } + + state->message = dbus_message_ref(message); + state->received = true; + return DBUS_HANDLER_RESULT_HANDLED; +} + +DBusMessage* WaitForPortalResponse(DBusConnection* connection, + const std::string& request_path, + const std::atomic& running, + int timeout_ms = 120000) { + if (!connection || request_path.empty()) { + return nullptr; + } + + PortalResponseState state; + state.request_path = request_path; + + DBusError error; + dbus_error_init(&error); + + const std::string match_rule = + "type='signal',interface='" + std::string(kPortalRequestInterface) + + "',member='Response',path='" + request_path + "'"; + dbus_bus_add_match(connection, match_rule.c_str(), &error); + if (dbus_error_is_set(&error)) { + LogDbusError("dbus_bus_add_match", &error); + dbus_error_free(&error); + return nullptr; + } + + dbus_connection_add_filter(connection, HandlePortalResponseSignal, &state, + nullptr); + + auto deadline = + std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms); + while (running.load() && !state.received && + std::chrono::steady_clock::now() < deadline) { + dbus_connection_read_write(connection, 100); + while (dbus_connection_dispatch(connection) == DBUS_DISPATCH_DATA_REMAINS) { + } + } + + dbus_connection_remove_filter(connection, HandlePortalResponseSignal, &state); + + DBusError remove_error; + dbus_error_init(&remove_error); + dbus_bus_remove_match(connection, match_rule.c_str(), &remove_error); + if (dbus_error_is_set(&remove_error)) { + dbus_error_free(&remove_error); + } + + return state.message; +} + +bool ExtractRequestPath(DBusMessage* reply, std::string* request_path) { + if (!reply || !request_path) { + return false; + } + + const char* path = nullptr; + DBusError error; + dbus_error_init(&error); + const dbus_bool_t ok = dbus_message_get_args( + reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID); + if (!ok || !path) { + LogDbusError("dbus_message_get_args(request_path)", &error); + dbus_error_free(&error); + return false; + } + + *request_path = path; + return true; +} + +bool ExtractPortalResponse(DBusMessage* message, uint32_t* response_code, + DBusMessageIter* results_array) { + if (!message || !response_code || !results_array) { + return false; + } + + DBusMessageIter iter; + if (!dbus_message_iter_init(message, &iter) || + dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) { + return false; + } + + dbus_message_iter_get_basic(&iter, response_code); + if (!dbus_message_iter_next(&iter) || + dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) { + return false; + } + + *results_array = iter; + return true; +} + +bool SendPortalRequestAndHandleResponse( + DBusConnection* connection, const char* method_name, + const char* action_name, + const std::function& append_message_args, + const std::atomic& running, + const std::function& handle_results, + std::string* request_path_out = nullptr) { + if (!connection || !method_name || method_name[0] == '\0') { + return false; + } + + DBusMessage* message = + dbus_message_new_method_call(kPortalBusName, kPortalObjectPath, + kPortalScreenCastInterface, method_name); + if (!message) { + LOG_ERROR("Failed to allocate {} message", method_name); + return false; + } + + if (append_message_args && !append_message_args(message)) { + dbus_message_unref(message); + LOG_ERROR("{} arguments are malformed", method_name); + return false; + } + + DBusError error; + dbus_error_init(&error); + DBusMessage* reply = + dbus_connection_send_with_reply_and_block(connection, message, -1, &error); + dbus_message_unref(message); + if (!reply) { + LogDbusError(action_name ? action_name : method_name, &error); + dbus_error_free(&error); + return false; + } + + std::string request_path; + const bool got_request_path = ExtractRequestPath(reply, &request_path); + dbus_message_unref(reply); + if (!got_request_path) { + return false; + } + if (request_path_out) { + *request_path_out = request_path; + } + + DBusMessage* response = + WaitForPortalResponse(connection, request_path, running); + if (!response) { + LOG_ERROR("Timed out waiting for {} response", method_name); + return false; + } + + uint32_t response_code = 1; + DBusMessageIter results; + const bool parsed = ExtractPortalResponse(response, &response_code, &results); + if (!parsed) { + dbus_message_unref(response); + LOG_ERROR("{} response was malformed", method_name); + return false; + } + + const bool ok = handle_results ? handle_results(response_code, &results) + : (response_code == 0); + dbus_message_unref(response); + return ok; +} + +} // namespace + +bool ScreenCapturerWayland::CheckPortalAvailability() const { + DBusError error; + dbus_error_init(&error); + + DBusConnection* connection = dbus_bus_get(DBUS_BUS_SESSION, &error); + if (!connection) { + LogDbusError("dbus_bus_get", &error); + dbus_error_free(&error); + return false; + } + + const dbus_bool_t has_owner = dbus_bus_name_has_owner( + connection, kPortalBusName, &error); + if (dbus_error_is_set(&error)) { + LogDbusError("dbus_bus_name_has_owner", &error); + dbus_error_free(&error); + dbus_connection_unref(connection); + return false; + } + + dbus_connection_unref(connection); + return has_owner == TRUE; +} + +bool ScreenCapturerWayland::ConnectSessionBus() { + if (dbus_connection_) { + return true; + } + + DBusError error; + dbus_error_init(&error); + + dbus_connection_ = dbus_bus_get_private(DBUS_BUS_SESSION, &error); + if (!dbus_connection_) { + LogDbusError("dbus_bus_get_private", &error); + dbus_error_free(&error); + return false; + } + + dbus_connection_set_exit_on_disconnect(dbus_connection_, FALSE); + return true; +} + +bool ScreenCapturerWayland::CreatePortalSession() { + if (!dbus_connection_) { + return false; + } + + const std::string session_handle_token = MakeToken("crossdesk_session"); + std::string request_path; + const bool ok = SendPortalRequestAndHandleResponse( + dbus_connection_, "CreateSession", "CreateSession", + [&](DBusMessage* message) { + DBusMessageIter iter; + DBusMessageIter options; + dbus_message_iter_init_append(message, &iter); + dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", + &options); + AppendDictEntryString(&options, "session_handle_token", + session_handle_token); + AppendDictEntryString(&options, "handle_token", MakeToken("crossdesk_req")); + dbus_message_iter_close_container(&iter, &options); + return true; + }, + running_, + [&](uint32_t response_code, DBusMessageIter* results) { + if (response_code != 0) { + LOG_ERROR("CreateSession was denied or malformed, response={}", + response_code); + return false; + } + + DBusMessageIter dict; + dbus_message_iter_recurse(results, &dict); + while (dbus_message_iter_get_arg_type(&dict) != DBUS_TYPE_INVALID) { + if (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) { + DBusMessageIter entry; + dbus_message_iter_recurse(&dict, &entry); + + const char* key = nullptr; + dbus_message_iter_get_basic(&entry, &key); + if (key && dbus_message_iter_next(&entry) && + dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_VARIANT && + strcmp(key, "session_handle") == 0) { + DBusMessageIter variant; + std::string parsed_handle; + dbus_message_iter_recurse(&entry, &variant); + if (ReadPathLikeVariant(&variant, &parsed_handle) && + !parsed_handle.empty()) { + session_handle_ = parsed_handle; + break; + } + } + } + dbus_message_iter_next(&dict); + } + return true; + }, + &request_path); + if (!ok) { + return false; + } + + if (session_handle_.empty()) { + const std::string fallback_handle = BuildSessionHandleFromRequestPath( + request_path, session_handle_token); + if (!fallback_handle.empty()) { + LOG_WARN( + "CreateSession response missing session_handle, using derived handle " + "{}", + fallback_handle); + session_handle_ = fallback_handle; + } + } + + if (session_handle_.empty()) { + LOG_ERROR("CreateSession response did not include a session handle"); + return false; + } + + return true; +} + +bool ScreenCapturerWayland::SelectPortalSource() { + if (!dbus_connection_ || session_handle_.empty()) { + return false; + } + + const char* session_handle = session_handle_.c_str(); + return SendPortalRequestAndHandleResponse( + dbus_connection_, "SelectSources", "SelectSources", + [&](DBusMessage* message) { + DBusMessageIter iter; + DBusMessageIter options; + dbus_message_iter_init_append(message, &iter); + dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, + &session_handle); + dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", + &options); + AppendDictEntryUint32(&options, "types", kScreenCastSourceMonitor); + AppendDictEntryBool(&options, "multiple", false); + AppendDictEntryUint32( + &options, "cursor_mode", + show_cursor_ ? kCursorModeEmbedded : kCursorModeHidden); + AppendDictEntryString(&options, "handle_token", + MakeToken("crossdesk_req")); + dbus_message_iter_close_container(&iter, &options); + return true; + }, + running_, [](uint32_t response_code, DBusMessageIter*) { + if (response_code != 0) { + LOG_ERROR("SelectSources was denied or malformed, response={}", + response_code); + return false; + } + return true; + }); +} + +bool ScreenCapturerWayland::StartPortalSession() { + if (!dbus_connection_ || session_handle_.empty()) { + return false; + } + + const char* session_handle = session_handle_.c_str(); + const char* parent_window = ""; + const bool ok = SendPortalRequestAndHandleResponse( + dbus_connection_, "Start", "Start", + [&](DBusMessage* message) { + DBusMessageIter iter; + DBusMessageIter options; + dbus_message_iter_init_append(message, &iter); + dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, + &session_handle); + dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &parent_window); + dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", + &options); + AppendDictEntryString(&options, "handle_token", MakeToken("crossdesk_req")); + dbus_message_iter_close_container(&iter, &options); + return true; + }, + running_, + [&](uint32_t response_code, DBusMessageIter* results) { + if (response_code != 0) { + LOG_ERROR("Start was denied or malformed, response={}", response_code); + return false; + } + + DBusMessageIter dict; + dbus_message_iter_recurse(results, &dict); + while (dbus_message_iter_get_arg_type(&dict) != DBUS_TYPE_INVALID) { + if (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) { + DBusMessageIter entry; + dbus_message_iter_recurse(&dict, &entry); + + const char* key = nullptr; + dbus_message_iter_get_basic(&entry, &key); + if (key && dbus_message_iter_next(&entry) && + dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_VARIANT && + strcmp(key, "streams") == 0) { + DBusMessageIter variant; + DBusMessageIter streams; + dbus_message_iter_recurse(&entry, &variant); + dbus_message_iter_recurse(&variant, &streams); + + if (dbus_message_iter_get_arg_type(&streams) == DBUS_TYPE_STRUCT) { + DBusMessageIter stream; + dbus_message_iter_recurse(&streams, &stream); + + if (dbus_message_iter_get_arg_type(&stream) == DBUS_TYPE_UINT32) { + dbus_message_iter_get_basic(&stream, &pipewire_node_id_); + } + + if (dbus_message_iter_next(&stream) && + dbus_message_iter_get_arg_type(&stream) == DBUS_TYPE_ARRAY) { + DBusMessageIter props; + dbus_message_iter_recurse(&stream, &props); + while (dbus_message_iter_get_arg_type(&props) != + DBUS_TYPE_INVALID) { + if (dbus_message_iter_get_arg_type(&props) == + DBUS_TYPE_DICT_ENTRY) { + DBusMessageIter prop_entry; + dbus_message_iter_recurse(&props, &prop_entry); + + const char* prop_key = nullptr; + dbus_message_iter_get_basic(&prop_entry, &prop_key); + if (prop_key && dbus_message_iter_next(&prop_entry) && + dbus_message_iter_get_arg_type(&prop_entry) == + DBUS_TYPE_VARIANT && + strcmp(prop_key, "size") == 0) { + DBusMessageIter prop_variant; + dbus_message_iter_recurse(&prop_entry, &prop_variant); + if (dbus_message_iter_get_arg_type(&prop_variant) == + DBUS_TYPE_STRUCT) { + DBusMessageIter size_iter; + int width = 0; + int height = 0; + dbus_message_iter_recurse(&prop_variant, &size_iter); + if (ReadIntLike(&size_iter, &width) && + dbus_message_iter_next(&size_iter) && + ReadIntLike(&size_iter, &height)) { + UpdateDisplayGeometry(width, height); + } + } + } + } + dbus_message_iter_next(&props); + } + } + } + } + } + + dbus_message_iter_next(&dict); + } + return true; + }); + if (!ok) { + return false; + } + + if (pipewire_node_id_ == 0) { + LOG_ERROR("Start response did not include a PipeWire node id"); + return false; + } + + LOG_INFO("Wayland screencast ready, node_id={}", pipewire_node_id_); + return true; +} + +bool ScreenCapturerWayland::OpenPipeWireRemote() { + if (!dbus_connection_ || session_handle_.empty()) { + return false; + } + + DBusMessage* message = dbus_message_new_method_call( + kPortalBusName, kPortalObjectPath, kPortalScreenCastInterface, + "OpenPipeWireRemote"); + if (!message) { + LOG_ERROR("Failed to allocate OpenPipeWireRemote message"); + return false; + } + + DBusMessageIter iter; + DBusMessageIter options; + const char* session_handle = session_handle_.c_str(); + dbus_message_iter_init_append(message, &iter); + dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, + &session_handle); + dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &options); + dbus_message_iter_close_container(&iter, &options); + + DBusError error; + dbus_error_init(&error); + DBusMessage* reply = + dbus_connection_send_with_reply_and_block(dbus_connection_, message, -1, + &error); + dbus_message_unref(message); + if (!reply) { + LogDbusError("OpenPipeWireRemote", &error); + dbus_error_free(&error); + return false; + } + + DBusMessageIter reply_iter; + if (!dbus_message_iter_init(reply, &reply_iter) || + dbus_message_iter_get_arg_type(&reply_iter) != DBUS_TYPE_UNIX_FD) { + LOG_ERROR("OpenPipeWireRemote returned an unexpected payload"); + dbus_message_unref(reply); + return false; + } + + int received_fd = -1; + dbus_message_iter_get_basic(&reply_iter, &received_fd); + dbus_message_unref(reply); + + if (received_fd < 0) { + LOG_ERROR("OpenPipeWireRemote returned an invalid fd"); + return false; + } + + pipewire_fd_ = dup(received_fd); + if (pipewire_fd_ < 0) { + LOG_ERROR("Failed to duplicate PipeWire remote fd"); + return false; + } + + return true; +} + +void ScreenCapturerWayland::CleanupDbus() { + if (!dbus_connection_) { + return; + } + + dbus_connection_close(dbus_connection_); + dbus_connection_unref(dbus_connection_); + dbus_connection_ = nullptr; +} + +void ScreenCapturerWayland::ClosePortalSession() { + if (!dbus_connection_ || session_handle_.empty()) { + return; + } + + DBusMessage* message = dbus_message_new_method_call( + kPortalBusName, session_handle_.c_str(), kPortalSessionInterface, + "Close"); + if (message) { + DBusError error; + dbus_error_init(&error); + DBusMessage* reply = + dbus_connection_send_with_reply_and_block(dbus_connection_, message, + 1000, &error); + if (!reply && dbus_error_is_set(&error)) { + LogDbusError("Session.Close", &error); + dbus_error_free(&error); + } + if (reply) { + dbus_message_unref(reply); + } + dbus_message_unref(message); + } + + session_handle_.clear(); + pipewire_node_id_ = 0; +} + +} // namespace crossdesk + +#endif diff --git a/src/screen_capturer/linux/screen_capturer_x11.cpp b/src/screen_capturer/linux/screen_capturer_x11.cpp index 8fabfa7..c660e45 100644 --- a/src/screen_capturer/linux/screen_capturer_x11.cpp +++ b/src/screen_capturer/linux/screen_capturer_x11.cpp @@ -5,7 +5,9 @@ #include #include +#include #include +#include #include #include "libyuv.h" @@ -13,11 +15,58 @@ namespace crossdesk { +namespace { + +std::atomic g_x11_last_error_code{0}; +std::mutex g_x11_error_handler_mutex; + +int CaptureX11ErrorHandler([[maybe_unused]] Display* display, + XErrorEvent* error_event) { + if (error_event) { + g_x11_last_error_code.store(error_event->error_code); + } else { + g_x11_last_error_code.store(-1); + } + return 0; +} + +class ScopedX11ErrorTrap { + public: + explicit ScopedX11ErrorTrap(Display* display) + : display_(display), lock_(g_x11_error_handler_mutex) { + g_x11_last_error_code.store(0); + previous_handler_ = XSetErrorHandler(CaptureX11ErrorHandler); + } + + ~ScopedX11ErrorTrap() { + if (display_) { + XSync(display_, False); + } + XSetErrorHandler(previous_handler_); + } + + int SyncAndGetError() const { + if (display_) { + XSync(display_, False); + } + return g_x11_last_error_code.load(); + } + + private: + Display* display_ = nullptr; + int (*previous_handler_)(Display*, XErrorEvent*) = nullptr; + std::unique_lock lock_; +}; + +} // namespace + ScreenCapturerX11::ScreenCapturerX11() {} ScreenCapturerX11::~ScreenCapturerX11() { Destroy(); } int ScreenCapturerX11::Init(const int fps, cb_desktop_data cb) { + Destroy(); + display_ = XOpenDisplay(nullptr); if (!display_) { LOG_ERROR("Cannot connect to X server"); @@ -29,6 +78,7 @@ int ScreenCapturerX11::Init(const int fps, cb_desktop_data cb) { if (!screen_res_) { LOG_ERROR("Failed to get screen resources"); XCloseDisplay(display_); + display_ = nullptr; return 1; } @@ -82,6 +132,11 @@ int ScreenCapturerX11::Init(const int fps, cb_desktop_data cb) { y_plane_.resize(width_ * height_); uv_plane_.resize((width_ / 2) * (height_ / 2) * 2); + if (!ProbeCapture()) { + LOG_ERROR("X11 backend probe failed, XGetImage is not usable"); + return -3; + } + return 0; } @@ -108,9 +163,23 @@ int ScreenCapturerX11::Start(bool show_cursor) { show_cursor_ = show_cursor; running_ = true; paused_ = false; + capture_error_count_ = 0; thread_ = std::thread([this]() { + using clock = std::chrono::steady_clock; + const auto frame_interval = + std::chrono::milliseconds(std::max(1, 1000 / std::max(1, fps_))); + while (running_) { - if (!paused_) OnFrame(); + const auto frame_start = clock::now(); + if (!paused_) { + OnFrame(); + } + + const auto elapsed = std::chrono::duration_cast( + clock::now() - frame_start); + if (elapsed < frame_interval) { + std::this_thread::sleep_for(frame_interval - elapsed); + } } }); return 0; @@ -152,19 +221,44 @@ void ScreenCapturerX11::OnFrame() { return; } - if (monitor_index_ < 0 || monitor_index_ >= display_info_list_.size()) { - LOG_ERROR("Invalid monitor index: {}", monitor_index_.load()); + const int monitor_index = monitor_index_.load(); + if (monitor_index < 0 || + monitor_index >= static_cast(display_info_list_.size())) { + LOG_ERROR("Invalid monitor index: {}", monitor_index); return; } - left_ = display_info_list_[monitor_index_].left; - top_ = display_info_list_[monitor_index_].top; - width_ = display_info_list_[monitor_index_].width; - height_ = display_info_list_[monitor_index_].height; + left_ = display_info_list_[monitor_index].left; + top_ = display_info_list_[monitor_index].top; + width_ = display_info_list_[monitor_index].width & ~1; + height_ = display_info_list_[monitor_index].height & ~1; - XImage* image = XGetImage(display_, root_, left_, top_, width_, height_, - AllPlanes, ZPixmap); - if (!image) return; + if (width_ <= 1 || height_ <= 1) { + LOG_ERROR("Invalid capture size: {}x{}", width_, height_); + return; + } + + XImage* image = nullptr; + int x11_error = 0; + { + ScopedX11ErrorTrap trap(display_); + image = XGetImage(display_, root_, left_, top_, width_, height_, AllPlanes, + ZPixmap); + x11_error = trap.SyncAndGetError(); + } + + if (x11_error != 0 || !image) { + if (image) { + XDestroyImage(image); + } + ++capture_error_count_; + if (capture_error_count_ == 1 || capture_error_count_ % 120 == 0) { + LOG_WARN("X11 capture failed: x11_error={}, image={}, consecutive={}", + x11_error, image ? "valid" : "null", capture_error_count_); + } + return; + } + capture_error_count_ = 0; // if enable show cursor, draw cursor if (show_cursor_) { @@ -205,7 +299,7 @@ void ScreenCapturerX11::OnFrame() { if (callback_) { callback_(nv12.data(), width_ * height_ * 3 / 2, width_, height_, - display_info_list_[monitor_index_].name.c_str()); + display_info_list_[monitor_index].name.c_str()); } XDestroyImage(image); @@ -288,4 +382,32 @@ void ScreenCapturerX11::DrawCursor(XImage* image, int x, int y) { XFree(cursor_image); } -} // namespace crossdesk \ No newline at end of file + +bool ScreenCapturerX11::ProbeCapture() { + if (!display_ || display_info_list_.empty()) { + return false; + } + + const auto& first_display = display_info_list_[0]; + XImage* probe_image = nullptr; + int x11_error = 0; + { + ScopedX11ErrorTrap trap(display_); + probe_image = XGetImage(display_, root_, first_display.left, + first_display.top, 1, 1, AllPlanes, ZPixmap); + x11_error = trap.SyncAndGetError(); + } + + if (probe_image) { + XDestroyImage(probe_image); + } + + if (x11_error != 0 || !probe_image) { + LOG_WARN("X11 probe XGetImage failed: x11_error={}, image={}", x11_error, + probe_image ? "valid" : "null"); + return false; + } + + return true; +} +} // namespace crossdesk diff --git a/src/screen_capturer/linux/screen_capturer_x11.h b/src/screen_capturer/linux/screen_capturer_x11.h index 75131f4..024b3bc 100644 --- a/src/screen_capturer/linux/screen_capturer_x11.h +++ b/src/screen_capturer/linux/screen_capturer_x11.h @@ -17,6 +17,7 @@ struct _XImage; typedef struct _XImage XImage; #include +#include #include #include #include @@ -50,6 +51,7 @@ class ScreenCapturerX11 : public ScreenCapturer { private: void DrawCursor(XImage* image, int x, int y); + bool ProbeCapture(); private: Display* display_ = nullptr; @@ -68,9 +70,10 @@ class ScreenCapturerX11 : public ScreenCapturer { int fps_ = 60; cb_desktop_data callback_; std::vector display_info_list_; + int capture_error_count_ = 0; std::vector y_plane_; std::vector uv_plane_; }; } // namespace crossdesk -#endif \ No newline at end of file +#endif diff --git a/src/screen_capturer/screen_capturer_factory.h b/src/screen_capturer/screen_capturer_factory.h index d4fdf24..571adda 100644 --- a/src/screen_capturer/screen_capturer_factory.h +++ b/src/screen_capturer/screen_capturer_factory.h @@ -10,7 +10,7 @@ #ifdef _WIN32 #include "screen_capturer_win.h" #elif __linux__ -#include "screen_capturer_x11.h" +#include "screen_capturer_linux.h" #elif __APPLE__ // #include "screen_capturer_avf.h" #include "screen_capturer_sck.h" @@ -27,7 +27,7 @@ class ScreenCapturerFactory { #ifdef _WIN32 return new ScreenCapturerWin(); #elif __linux__ - return new ScreenCapturerX11(); + return new ScreenCapturerLinux(); #elif __APPLE__ // return new ScreenCapturerAvf(); return new ScreenCapturerSck(); diff --git a/xmake.lua b/xmake.lua index 5950603..395a232 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,227 +1,10 @@ set_project("crossdesk") set_license("LGPL-3.0") -option("CROSSDESK_VERSION") - set_default("0.0.0") - set_showmenu(true) - set_description("Set CROSSDESK_VERSION for build") -option_end() +includes("xmake/options.lua") +includes("xmake/platform.lua") +includes("xmake/targets.lua") -option("USE_CUDA") - set_default(false) - set_showmenu(true) - set_description("Use CUDA for hardware codec acceleration") -option_end() - -add_rules("mode.release", "mode.debug") -set_languages("c++17") -set_encodings("utf-8") - --- set_policy("build.warning", true) --- set_warnings("all", "extra") --- add_cxxflags("/W4", "/WX") - -add_defines("UNICODE") -add_defines("USE_CUDA=" .. (is_config("USE_CUDA", true) and "1" or "0")) - -if is_mode("debug") then - add_defines("CROSSDESK_DEBUG") -end - -add_requireconfs("*.python", {version = "3.12", override = true, configs = {pgo = false}}) -add_requires("spdlog 1.14.1", {system = false}) -add_requires("imgui v1.92.1-docking", {configs = {sdl3 = true, sdl3_renderer = true}}) -add_requires("openssl3 3.3.2", {system = false}) -add_requires("nlohmann_json 3.11.3") -add_requires("cpp-httplib v0.26.0", {configs = {ssl = true}}) -add_requires("tinyfiledialogs 3.15.1") - -if is_os("windows") then - add_requires("libyuv", "miniaudio 0.11.21") - add_links("Shell32", "dwmapi", "User32", "kernel32", - "SDL3-static", "gdi32", "winmm", "setupapi", "version", - "Imm32", "iphlpapi", "d3d11", "dxgi") - add_cxflags("/WX") - set_runtimes("MT") -elseif is_os("linux") then - add_links("pulse-simple", "pulse") - add_requires("libyuv") - add_syslinks("pthread", "dl") - add_links("SDL3", "asound", "X11", "Xtst", "Xrandr", "Xfixes") - add_cxflags("-Wno-unused-variable") -elseif is_os("macosx") then - add_links("SDL3") - add_ldflags("-Wl,-ld_classic") - add_cxflags("-Wno-unused-variable") - add_frameworks("OpenGL", "IOSurface", "ScreenCaptureKit", "AVFoundation", - "CoreMedia", "CoreVideo", "CoreAudio", "AudioToolbox") -end - -add_packages("spdlog", "imgui", "nlohmann_json") - -includes("submodules", "thirdparty") - -target("rd_log") - set_kind("object") - add_packages("spdlog") - add_files("src/log/rd_log.cpp") - add_includedirs("src/log", {public = true}) - -target("common") - set_kind("object") - add_deps("rd_log") - add_files("src/common/*.cpp") - if is_os("macosx") then - add_files("src/common/*.mm") - end - add_includedirs("src/common", {public = true}) - -target("path_manager") - set_kind("object") - add_deps("rd_log") - add_includedirs("src/path_manager", {public = true}) - add_files("src/path_manager/*.cpp") - add_includedirs("src/path_manager", {public = true}) - -target("screen_capturer") - set_kind("object") - add_deps("rd_log", "common") - add_includedirs("src/screen_capturer", {public = true}) - if is_os("windows") then - add_packages("libyuv") - add_files("src/screen_capturer/windows/screen_capturer_dxgi.cpp", - "src/screen_capturer/windows/screen_capturer_gdi.cpp", - "src/screen_capturer/windows/screen_capturer_win.cpp") - add_includedirs("src/screen_capturer/windows", {public = true}) - elseif is_os("macosx") then - add_files("src/screen_capturer/macosx/*.cpp", - "src/screen_capturer/macosx/*.mm") - add_includedirs("src/screen_capturer/macosx", {public = true}) - elseif is_os("linux") then - add_packages("libyuv") - add_files("src/screen_capturer/linux/*.cpp") - add_includedirs("src/screen_capturer/linux", {public = true}) - end - -target("speaker_capturer") - set_kind("object") - add_deps("rd_log") - add_includedirs("src/speaker_capturer", {public = true}) - if is_os("windows") then - add_packages("miniaudio") - add_files("src/speaker_capturer/windows/*.cpp") - add_includedirs("src/speaker_capturer/windows", {public = true}) - elseif is_os("macosx") then - add_files("src/speaker_capturer/macosx/*.cpp", - "src/speaker_capturer/macosx/*.mm") - add_includedirs("src/speaker_capturer/macosx", {public = true}) - elseif is_os("linux") then - add_files("src/speaker_capturer/linux/*.cpp") - add_includedirs("src/speaker_capturer/linux", {public = true}) - end - -target("device_controller") - set_kind("object") - add_deps("rd_log", "common") - add_includedirs("src/device_controller", {public = true}) - if is_os("windows") then - add_files("src/device_controller/mouse/windows/*.cpp", - "src/device_controller/keyboard/windows/*.cpp") - add_includedirs("src/device_controller/mouse/windows", - "src/device_controller/keyboard/windows", {public = true}) - elseif is_os("macosx") then - add_files("src/device_controller/mouse/mac/*.cpp", - "src/device_controller/keyboard/mac/*.cpp") - add_includedirs("src/device_controller/mouse/mac", - "src/device_controller/keyboard/mac", {public = true}) - elseif is_os("linux") then - add_files("src/device_controller/mouse/linux/*.cpp", - "src/device_controller/keyboard/linux/*.cpp") - add_includedirs("src/device_controller/mouse/linux", - "src/device_controller/keyboard/linux", {public = true}) - end - -target("thumbnail") - set_kind("object") - add_packages("libyuv", "openssl3") - add_deps("rd_log", "common") - add_files("src/thumbnail/*.cpp") - add_includedirs("src/thumbnail", {public = true}) - -target("autostart") - set_kind("object") - add_deps("rd_log") - add_files("src/autostart/*.cpp") - add_includedirs("src/autostart", {public = true}) - -target("config_center") - set_kind("object") - add_deps("rd_log", "autostart") - add_files("src/config_center/*.cpp") - add_includedirs("src/config_center", {public = true}) - -target("assets") - set_kind("headeronly") - add_includedirs("src/gui/assets/localization", - "src/gui/assets/fonts", - "src/gui/assets/icons", - "src/gui/assets/layouts", {public = true}) - -target("version_checker") - set_kind("object") - add_packages("cpp-httplib") - add_defines("CROSSDESK_VERSION=\"" .. (get_config("CROSSDESK_VERSION") or "Unknown") .. "\"") - add_deps("rd_log") - add_files("src/version_checker/*.cpp") - add_includedirs("src/version_checker", {public = true}) - -target("tools") - set_kind("object") - add_deps("rd_log") - add_files("src/tools/*.cpp") - if is_os("macosx") then - add_files("src/tools/*.mm") - end - add_includedirs("src/tools", {public = true}) - -target("gui") - set_kind("object") - add_packages("libyuv", "tinyfiledialogs") - add_defines("CROSSDESK_VERSION=\"" .. (get_config("CROSSDESK_VERSION") or "Unknown") .. "\"") - add_deps("rd_log", "common", "assets", "config_center", "minirtc", - "path_manager", "screen_capturer", "speaker_capturer", - "device_controller", "thumbnail", "version_checker", "tools") - add_files("src/gui/*.cpp", "src/gui/panels/*.cpp", "src/gui/toolbars/*.cpp", - "src/gui/windows/*.cpp") - add_includedirs("src/gui", "src/gui/panels", "src/gui/toolbars", - "src/gui/windows", {public = true}) - if is_os("windows") then - add_files("src/gui/tray/*.cpp") - add_includedirs("src/gui/tray", {public = true}) - elseif is_os("macosx") then - add_files("src/gui/windows/*.mm") - end - -if is_os("windows") then -target("wgc_plugin") - set_kind("shared") - add_packages("libyuv") - add_deps("rd_log") - add_defines("CROSSDESK_WGC_PLUGIN_BUILD=1") - add_links("windowsapp") - add_files("src/screen_capturer/windows/screen_capturer_wgc.cpp", - "src/screen_capturer/windows/wgc_session_impl.cpp", - "src/screen_capturer/windows/wgc_plugin_entry.cpp") - add_includedirs("src/common", "src/screen_capturer", - "src/screen_capturer/windows") -end - -target("crossdesk") - set_kind("binary") - add_deps("rd_log", "common", "gui") - add_files("src/app/*.cpp") - add_includedirs("src/app", {public = true}) - if is_os("windows") then - add_deps("wgc_plugin") - add_files("scripts/windows/crossdesk.rc") - end +setup_options_and_dependencies() +setup_platform_settings() +setup_targets() diff --git a/xmake/options.lua b/xmake/options.lua new file mode 100644 index 0000000..2a61950 --- /dev/null +++ b/xmake/options.lua @@ -0,0 +1,50 @@ +function setup_options_and_dependencies() + option("CROSSDESK_VERSION") + set_default("0.0.0") + set_showmenu(true) + set_description("Set CROSSDESK_VERSION for build") + option_end() + + option("USE_CUDA") + set_default(false) + set_showmenu(true) + set_description("Use CUDA for hardware codec acceleration") + option_end() + + option("USE_WAYLAND") + set_default(false) + set_showmenu(true) + set_description("Enable Wayland capture on Linux (assumes dependencies are installed)") + option_end() + + option("USE_DRM") + set_default(false) + set_showmenu(true) + set_description("Enable DRM capture on Linux (assumes dependencies are installed)") + option_end() + + add_rules("mode.release", "mode.debug") + set_languages("c++17") + set_encodings("utf-8") + + -- set_policy("build.warning", true) + -- set_warnings("all", "extra") + -- add_cxxflags("/W4", "/WX") + + add_defines("UNICODE") + add_defines("USE_CUDA=" .. (is_config("USE_CUDA", true) and "1" or "0")) + add_defines("USE_WAYLAND=" .. (is_config("USE_WAYLAND", true) and "1" or "0")) + add_defines("USE_DRM=" .. (is_config("USE_DRM", true) and "1" or "0")) + + if is_mode("debug") then + add_defines("CROSSDESK_DEBUG") + end + + add_requireconfs("*.python", {version = "3.12", override = true, configs = {pgo = false}}) + add_requires("spdlog 1.14.1", {system = false}) + add_requires("imgui v1.92.1-docking", {configs = {sdl3 = true, sdl3_renderer = true}}) + add_requires("openssl3 3.3.2", {system = false}) + add_requires("nlohmann_json 3.11.3") + add_requires("cpp-httplib v0.26.0", {configs = {ssl = true}}) + add_requires("tinyfiledialogs 3.15.1") +end \ No newline at end of file diff --git a/xmake/platform.lua b/xmake/platform.lua new file mode 100644 index 0000000..95ac782 --- /dev/null +++ b/xmake/platform.lua @@ -0,0 +1,81 @@ +local function add_existing_include_dirs(paths, opts) + for _, dir in ipairs(paths) do + if os.isdir(dir) then + add_includedirs(dir, opts) + end + end +end + +local function collect_dbus_arch_include_dirs() + local include_dirs = {} + for _, pattern in ipairs({ + "/usr/lib/*/dbus-1.0/include", + "/usr/lib64/dbus-1.0/include", + "/usr/lib/dbus-1.0/include", + "/lib/*/dbus-1.0/include", + "/lib64/dbus-1.0/include", + "/lib/dbus-1.0/include" + }) do + for _, include_dir in ipairs(os.dirs(pattern)) do + table.insert(include_dirs, include_dir) + end + end + return include_dirs +end + +function setup_platform_settings() + if is_os("windows") then + add_requires("libyuv", "miniaudio 0.11.21") + add_links("Shell32", "dwmapi", "User32", "kernel32", + "SDL3-static", "gdi32", "winmm", "setupapi", "version", + "Imm32", "iphlpapi", "d3d11", "dxgi") + add_cxflags("/WX") + set_runtimes("MT") + elseif is_os("linux") then + add_links("pulse-simple", "pulse") + add_requires("libyuv") + add_syslinks("pthread", "dl") + add_links("SDL3", "asound", "X11", "Xtst", "Xrandr", "Xfixes") + + if is_config("USE_DRM", true) then + add_links("drm") + add_defines("CROSSDESK_HAS_DRM=1") + add_existing_include_dirs({ + "/usr/include/libdrm", + "/usr/local/include/libdrm" + }, {system = true}) + else + add_defines("CROSSDESK_HAS_DRM=0") + end + + if is_config("USE_WAYLAND", true) then + add_links("dbus-1", "pipewire-0.3") + add_defines("CROSSDESK_HAS_WAYLAND_CAPTURER=1") + add_existing_include_dirs({ + "/usr/include/dbus-1.0", + "/usr/local/include/dbus-1.0", + "/usr/include/pipewire-0.3", + "/usr/local/include/pipewire-0.3", + "/usr/include/pipewire", + "/usr/local/include/pipewire", + "/usr/include/spa-0.2", + "/usr/local/include/spa-0.2", + "/usr/include/spa", + "/usr/local/include/spa" + }, {system = true}) + for _, include_dir in ipairs(collect_dbus_arch_include_dirs()) do + add_includedirs(include_dir, {system = true}) + end + else + add_defines("CROSSDESK_HAS_WAYLAND_CAPTURER=0") + end + + add_cxflags("-Wno-unused-variable") + elseif is_os("macosx") then + add_links("SDL3") + add_ldflags("-Wl,-ld_classic") + add_cxflags("-Wno-unused-variable") + add_frameworks("OpenGL", "IOSurface", "ScreenCaptureKit", "AVFoundation", + "CoreMedia", "CoreVideo", "CoreAudio", "AudioToolbox") + end +end \ No newline at end of file diff --git a/xmake/targets.lua b/xmake/targets.lua new file mode 100644 index 0000000..58bf90d --- /dev/null +++ b/xmake/targets.lua @@ -0,0 +1,177 @@ +function setup_targets() + add_packages("spdlog", "imgui", "nlohmann_json") + + includes("submodules", "thirdparty") + + target("rd_log") + set_kind("object") + add_packages("spdlog") + add_files("src/log/rd_log.cpp") + add_includedirs("src/log", {public = true}) + + target("common") + set_kind("object") + add_deps("rd_log") + add_files("src/common/*.cpp") + if is_os("macosx") then + add_files("src/common/*.mm") + end + add_includedirs("src/common", {public = true}) + + target("path_manager") + set_kind("object") + add_deps("rd_log") + add_includedirs("src/path_manager", {public = true}) + add_files("src/path_manager/*.cpp") + add_includedirs("src/path_manager", {public = true}) + + target("screen_capturer") + set_kind("object") + add_deps("rd_log", "common") + add_includedirs("src/screen_capturer", {public = true}) + if is_os("windows") then + add_packages("libyuv") + add_files("src/screen_capturer/windows/screen_capturer_dxgi.cpp", + "src/screen_capturer/windows/screen_capturer_gdi.cpp", + "src/screen_capturer/windows/screen_capturer_win.cpp") + add_includedirs("src/screen_capturer/windows", {public = true}) + elseif is_os("macosx") then + add_files("src/screen_capturer/macosx/*.cpp", + "src/screen_capturer/macosx/*.mm") + add_includedirs("src/screen_capturer/macosx", {public = true}) + elseif is_os("linux") then + add_packages("libyuv") + add_files("src/screen_capturer/linux/screen_capturer_linux.cpp") + add_files("src/screen_capturer/linux/screen_capturer_x11.cpp") + add_files("src/screen_capturer/linux/screen_capturer_drm.cpp") + if is_config("USE_WAYLAND", true) then + add_files("src/screen_capturer/linux/screen_capturer_wayland.cpp") + add_files("src/screen_capturer/linux/screen_capturer_wayland_portal.cpp") + add_files("src/screen_capturer/linux/screen_capturer_wayland_pipewire.cpp") + end + add_includedirs("src/screen_capturer/linux", {public = true}) + end + + target("speaker_capturer") + set_kind("object") + add_deps("rd_log") + add_includedirs("src/speaker_capturer", {public = true}) + if is_os("windows") then + add_packages("miniaudio") + add_files("src/speaker_capturer/windows/*.cpp") + add_includedirs("src/speaker_capturer/windows", {public = true}) + elseif is_os("macosx") then + add_files("src/speaker_capturer/macosx/*.cpp", + "src/speaker_capturer/macosx/*.mm") + add_includedirs("src/speaker_capturer/macosx", {public = true}) + elseif is_os("linux") then + add_files("src/speaker_capturer/linux/*.cpp") + add_includedirs("src/speaker_capturer/linux", {public = true}) + end + + target("device_controller") + set_kind("object") + add_deps("rd_log", "common") + add_includedirs("src/device_controller", {public = true}) + if is_os("windows") then + add_files("src/device_controller/mouse/windows/*.cpp", + "src/device_controller/keyboard/windows/*.cpp") + add_includedirs("src/device_controller/mouse/windows", + "src/device_controller/keyboard/windows", {public = true}) + elseif is_os("macosx") then + add_files("src/device_controller/mouse/mac/*.cpp", + "src/device_controller/keyboard/mac/*.cpp") + add_includedirs("src/device_controller/mouse/mac", + "src/device_controller/keyboard/mac", {public = true}) + elseif is_os("linux") then + add_files("src/device_controller/mouse/linux/*.cpp", + "src/device_controller/keyboard/linux/*.cpp") + add_includedirs("src/device_controller/mouse/linux", + "src/device_controller/keyboard/linux", {public = true}) + end + + target("thumbnail") + set_kind("object") + add_packages("libyuv", "openssl3") + add_deps("rd_log", "common") + add_files("src/thumbnail/*.cpp") + add_includedirs("src/thumbnail", {public = true}) + + target("autostart") + set_kind("object") + add_deps("rd_log") + add_files("src/autostart/*.cpp") + add_includedirs("src/autostart", {public = true}) + + target("config_center") + set_kind("object") + add_deps("rd_log", "autostart") + add_files("src/config_center/*.cpp") + add_includedirs("src/config_center", {public = true}) + + target("assets") + set_kind("headeronly") + add_includedirs("src/gui/assets/localization", + "src/gui/assets/fonts", + "src/gui/assets/icons", + "src/gui/assets/layouts", {public = true}) + + target("version_checker") + set_kind("object") + add_packages("cpp-httplib") + add_defines("CROSSDESK_VERSION=\"" .. (get_config("CROSSDESK_VERSION") or "Unknown") .. "\"") + add_deps("rd_log") + add_files("src/version_checker/*.cpp") + add_includedirs("src/version_checker", {public = true}) + + target("tools") + set_kind("object") + add_deps("rd_log") + add_files("src/tools/*.cpp") + if is_os("macosx") then + add_files("src/tools/*.mm") + end + add_includedirs("src/tools", {public = true}) + + target("gui") + set_kind("object") + add_packages("libyuv", "tinyfiledialogs") + add_defines("CROSSDESK_VERSION=\"" .. (get_config("CROSSDESK_VERSION") or "Unknown") .. "\"") + add_deps("rd_log", "common", "assets", "config_center", "minirtc", + "path_manager", "screen_capturer", "speaker_capturer", + "device_controller", "thumbnail", "version_checker", "tools") + add_files("src/gui/*.cpp", "src/gui/panels/*.cpp", "src/gui/toolbars/*.cpp", + "src/gui/windows/*.cpp") + add_includedirs("src/gui", "src/gui/panels", "src/gui/toolbars", + "src/gui/windows", {public = true}) + if is_os("windows") then + add_files("src/gui/tray/*.cpp") + add_includedirs("src/gui/tray", {public = true}) + elseif is_os("macosx") then + add_files("src/gui/windows/*.mm") + end + + if is_os("windows") then + target("wgc_plugin") + set_kind("shared") + add_packages("libyuv") + add_deps("rd_log") + add_defines("CROSSDESK_WGC_PLUGIN_BUILD=1") + add_links("windowsapp") + add_files("src/screen_capturer/windows/screen_capturer_wgc.cpp", + "src/screen_capturer/windows/wgc_session_impl.cpp", + "src/screen_capturer/windows/wgc_plugin_entry.cpp") + add_includedirs("src/common", "src/screen_capturer", + "src/screen_capturer/windows") + end + + target("crossdesk") + set_kind("binary") + add_deps("rd_log", "common", "gui") + add_files("src/app/*.cpp") + add_includedirs("src/app", {public = true}) + if is_os("windows") then + add_deps("wgc_plugin") + add_files("scripts/windows/crossdesk.rc") + end +end \ No newline at end of file