#pragma once #include #include #include #include #include #include namespace piik::capture { // Sharing defaults and source previews request borderless capture. Windows owns // consent and visibility; an optional access request must not delay frames/stop. class CaptureBorder final { public: CaptureBorder() = default; CaptureBorder(const CaptureBorder&) = delete; CaptureBorder& operator=(const CaptureBorder&) = delete; ~CaptureBorder() { Close(); } static bool Supported() noexcept { using winrt::Windows::Foundation::Metadata::ApiInformation; try { return ApiInformation::IsPropertyPresent( L"Windows.Graphics.Capture.GraphicsCaptureSession", L"IsBorderRequired") && ApiInformation::IsMethodPresent( L"Windows.Graphics.Capture.GraphicsCaptureAccess", L"RequestAccessAsync"); } catch (const winrt::hresult_error&) { return false; } } void Start(const winrt::Windows::Graphics::Capture::GraphicsCaptureSession& session) { using namespace winrt::Windows::Graphics::Capture; if (!Supported()) return; try { access_ = GraphicsCaptureAccess::RequestAccessAsync(GraphicsCaptureAccessKind::Borderless); Apply(session); } catch (const winrt::hresult_error& error) { std::osyncstream(std::cerr) << "capture-border-access-failed: " << winrt::to_string(error.message()) << '\n'; } } void Apply(const winrt::Windows::Graphics::Capture::GraphicsCaptureSession& session) { if (!access_) return; try { if (access_.Status() == winrt::Windows::Foundation::AsyncStatus::Started) return; const auto access = access_.GetResults(); // Store the request even when consent is denied. Windows enforces current // permission and can honor this preference if consent changes mid-share. session.IsBorderRequired(false); std::osyncstream(std::cerr) << "capture-border-access=" << (access == AccessStatus::Allowed ? "allowed" : "not-allowed") << " status=" << static_cast(access) << '\n'; } catch (const winrt::hresult_error& error) { std::osyncstream(std::cerr) << "capture-border-access-failed: " << winrt::to_string(error.message()) << '\n'; } access_ = nullptr; } void Close() noexcept { try { if (access_) access_.Cancel(); } catch (...) { } access_ = nullptr; } private: using AccessStatus = winrt::Windows::Security::Authorization::AppCapabilityAccess::AppCapabilityAccessStatus; winrt::Windows::Foundation::IAsyncOperation access_{nullptr}; }; } // namespace piik::capture