Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

feat(vision): use sck-rs for macOS screen capture

Louis Beaumontd3c82bf8c640
diff --git a/Cargo.lock b/Cargo.lock
index 9c7cf1a30..05505a2bd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -628,7 +628,7 @@ dependencies = [
"bitflags 2.9.0",
"cexpr",
"clang-sys",
- "itertools 0.12.1",
+ "itertools 0.10.5",
"lazy_static",
"lazycell",
"log",
@@ -3932,7 +3932,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34"
dependencies = [
"cfg-if",
- "windows-targets 0.52.6",
+ "windows-targets 0.48.5",
]
[[package]]
@@ -6553,6 +6553,19 @@ dependencies = [
"windows-sys 0.59.0",
]
+[[package]]
+name = "sck-rs"
+version = "0.1.0"
+source = "git+https://github.com/louis030195/sck-rs#e09225902d7ea977ca0ebda336bb01fbac3e7948"
+dependencies = [
+ "cidre 0.7.0",
+ "image",
+ "once_cell",
+ "thiserror 1.0.69",
+ "tokio",
+ "tracing",
+]
+
[[package]]
name = "scoped-tls"
version = "1.0.1"
@@ -6804,6 +6817,7 @@ dependencies = [
"once_cell",
"reqwest 0.12.12",
"rusty-tesseract",
+ "sck-rs",
"screenpipe-core",
"screenpipe-db",
"screenpipe-integrations",
diff --git a/screenpipe-vision/Cargo.toml b/screenpipe-vision/Cargo.toml
index a3495871a..8fda0bda8 100644
--- a/screenpipe-vision/Cargo.toml
+++ b/screenpipe-vision/Cargo.toml
@@ -39,13 +39,17 @@ tracing-subscriber = { workspace = true }
tracing = { workspace = true }
serde = "1.0.200"
-xcap = "0.8.1"
-
once_cell = { workspace = true }
base64 = "0.22.1"
-
reqwest = { workspace = true }
+# Screen capture - platform specific
+[target.'cfg(target_os = "macos")'.dependencies.sck-rs]
+git = "https://github.com/louis030195/sck-rs"
+
+[target.'cfg(not(target_os = "macos"))'.dependencies]
+xcap = "0.8.1"
+
[dev-dependencies]
tempfile = "3.3.0"
criterion = { workspace = true }
diff --git a/screenpipe-vision/examples/screenpipe-vision.rs b/screenpipe-vision/examples/screenpipe-vision.rs
index fdcb6d785..55a813be9 100644
--- a/screenpipe-vision/examples/screenpipe-vision.rs
+++ b/screenpipe-vision/examples/screenpipe-vision.rs
@@ -6,6 +6,10 @@ use screenpipe_vision::{
use std::{sync::Arc, time::Duration};
use tokio::sync::mpsc::channel;
use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter};
+#[cfg(target_os = "macos")]
+use sck_rs::Monitor;
+
+#[cfg(not(target_os = "macos"))]
use xcap::Monitor;
#[derive(Parser)]
@@ -35,17 +39,23 @@ async fn main() {
let languages = cli.language;
// Get monitor ID before spawning the task
+ #[cfg(target_os = "macos")]
let monitor_id = tokio::task::spawn_blocking(|| Monitor::all().unwrap().first().unwrap().id())
.await
.unwrap();
+ #[cfg(not(target_os = "macos"))]
+ let monitor_id = tokio::task::spawn_blocking(|| Monitor::all().unwrap().first().unwrap().id().unwrap())
+ .await
+ .unwrap();
+
let window_filters = Arc::new(WindowFilters::new(&[], &[]));
let _ = continuous_capture(
result_tx,
Duration::from_secs_f32(1.0 / cli.fps),
OcrEngine::AppleNative,
- monitor_id.unwrap(),
+ monitor_id,
window_filters,
languages.clone(),
false,
diff --git a/screenpipe-vision/src/capture_screenshot_by_window.rs b/screenpipe-vision/src/capture_screenshot_by_window.rs
index 9491f4477..e36c504a7 100644
--- a/screenpipe-vision/src/capture_screenshot_by_window.rs
+++ b/screenpipe-vision/src/capture_screenshot_by_window.rs
@@ -5,6 +5,10 @@ use std::error::Error;
use std::fmt;
use tracing::debug;
+#[cfg(target_os = "macos")]
+use sck_rs::{Window, XCapError};
+
+#[cfg(not(target_os = "macos"))]
use xcap::{Window, XCapError};
use crate::browser_utils::create_url_detector;
diff --git a/screenpipe-vision/src/monitor.rs b/screenpipe-vision/src/monitor.rs
index d04454b97..c8a8a67e9 100644
--- a/screenpipe-vision/src/monitor.rs
+++ b/screenpipe-vision/src/monitor.rs
@@ -2,6 +2,11 @@ use anyhow::{Error, Result};
use image::DynamicImage;
use std::sync::Arc;
use tracing;
+
+#[cfg(target_os = "macos")]
+use sck_rs::Monitor;
+
+#[cfg(not(target_os = "macos"))]
use xcap::Monitor;
#[derive(Clone)]
@@ -19,7 +24,26 @@ pub struct MonitorData {
}
impl SafeMonitor {
+ #[cfg(target_os = "macos")]
+ pub fn new(monitor: Monitor) -> Self {
+ // sck-rs API: id(), name(), is_primary() return values directly
+ let monitor_id = monitor.id();
+ let monitor_data = Arc::new(MonitorData {
+ width: monitor.width().unwrap_or(0),
+ height: monitor.height().unwrap_or(0),
+ name: monitor.name().to_string(),
+ is_primary: monitor.is_primary(),
+ });
+
+ Self {
+ monitor_id,
+ monitor_data,
+ }
+ }
+
+ #[cfg(not(target_os = "macos"))]
pub fn new(monitor: Monitor) -> Self {
+ // xcap API: id(), name(), is_primary() return Result
let monitor_id = monitor.id().unwrap();
let monitor_data = Arc::new(MonitorData {
width: monitor.width().unwrap(),
@@ -34,6 +58,33 @@ impl SafeMonitor {
}
}
+ #[cfg(target_os = "macos")]
+ pub async fn capture_image(&self) -> Result<DynamicImage> {
+ let monitor_id = self.monitor_id;
+
+ let image = std::thread::spawn(move || -> Result<DynamicImage> {
+ let monitor = Monitor::all()
+ .map_err(Error::from)?
+ .into_iter()
+ .find(|m| m.id() == monitor_id)
+ .ok_or_else(|| anyhow::anyhow!("Monitor not found"))?;
+
+ if monitor.width().unwrap_or(0) == 0 || monitor.height().unwrap_or(0) == 0 {
+ return Err(anyhow::anyhow!("Invalid monitor dimensions"));
+ }
+
+ monitor
+ .capture_image()
+ .map_err(|e| anyhow::anyhow!("{}", e))
+ .map(DynamicImage::ImageRgba8)
+ })
+ .join()
+ .unwrap()?;
+
+ Ok(image)
+ }
+
+ #[cfg(not(target_os = "macos"))]
pub async fn capture_image(&self) -> Result<DynamicImage> {
let monitor_id = self.monitor_id;
@@ -108,6 +159,37 @@ pub async fn get_default_monitor() -> SafeMonitor {
.unwrap()
}
+#[cfg(target_os = "macos")]
+pub async fn get_monitor_by_id(id: u32) -> Option<SafeMonitor> {
+ tokio::task::spawn_blocking(move || match Monitor::all() {
+ Ok(monitors) => {
+ let monitor_count = monitors.len();
+ let monitor_ids: Vec<u32> = monitors.iter().map(|m| m.id()).collect();
+
+ tracing::debug!(
+ "Found {} monitors with IDs: {:?}",
+ monitor_count,
+ monitor_ids
+ );
+
+ monitors
+ .into_iter()
+ .find(|m| m.id() == id)
+ .map(SafeMonitor::new)
+ }
+ Err(e) => {
+ tracing::error!("Failed to list monitors: {}", e);
+ None
+ }
+ })
+ .await
+ .unwrap_or_else(|e| {
+ tracing::error!("Task to get monitor by ID {} panicked: {}", id, e);
+ None
+ })
+}
+
+#[cfg(not(target_os = "macos"))]
pub async fn get_monitor_by_id(id: u32) -> Option<SafeMonitor> {
tokio::task::spawn_blocking(move || match Monitor::all() {
Ok(monitors) => {