nashaofu / xcap

XCap is a cross-platform screen capture library written in Rust. It supports Linux (X11, Wayland), MacOS, and Windows. XCap supports screenshot and video recording (to be implemented).
https://docs.rs/xcap
Apache License 2.0
455 stars 54 forks source link

Is there a way to check this before the screen capture permission pop up on macos? #138

Open gusxodnjs opened 1 month ago

gusxodnjs commented 1 month ago

When I try to screen capture in macos, I get a pop-up if I don't have permission. I want to replace this pop-up with my custom pop-up. Is there a way to look up the system's settings before this pop-up pops up? I already know that I can access tcc.db to verify permissions, but I couldn't access this db in my application.

louis030195 commented 1 month ago

@gusxodnjs interested about it too, do you know what to put in entitlements.plist and Info.plist also for permissions?

codesoda commented 2 weeks ago

Use this module


// screen_capture.rs

// Use the objc crate to work with Objective-C objects and runtime
extern crate objc;

// Import necessary parts from the objc crate
use objc::runtime::Object;
use std::os::raw::c_void;

// Declare the external functions from CoreGraphics framework
extern "C" {
    // CGPreflightScreenCaptureAccess doesn't take parameters and returns a bool.
    // True if the app either already has screen capture access or if the system
    // version is earlier than 10.15. False otherwise.
    fn CGPreflightScreenCaptureAccess() -> bool;

    // CGRequestScreenCaptureAccess doesn't take parameters and returns a bool.
    // True if the user grants permission or if the app already has permission.
    // False if the user denies permission or if an error occurs.
    fn CGRequestScreenCaptureAccess() -> bool;
}

/// Check if the user has already granted screen capture access or if the system
/// version is earlier than 10.15.
pub fn preflight_access() -> bool {
    unsafe {
        // Safety: Calling an external C function, considered unsafe in Rust
        CGPreflightScreenCaptureAccess()
    }
}

/// Request screen capture access from the user.
pub fn request_access() -> bool {
    unsafe {
        // Safety: Calling an external C function, considered unsafe in Rust
        CGRequestScreenCaptureAccess()
    }
}