Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

new cursor-capture + displays crates (#455)

Brendan Allan741373493e76
diff --git a/Cargo.lock b/Cargo.lock
index c357ac8dd..62a619a87 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -918,6 +918,15 @@ dependencies = [
"tokio",
]
+[[package]]
+name = "cap-cursor-capture"
+version = "0.1.0"
+dependencies = [
+ "cap-displays",
+ "core-graphics 0.24.0",
+ "device_query 3.0.1",
+]
+
[[package]]
name = "cap-desktop"
version = "0.3.41"
@@ -940,7 +949,7 @@ dependencies = [
"core-foundation 0.10.0",
"core-graphics 0.24.0",
"cpal 0.15.3 (git+https://github.com/RustAudio/cpal?rev=f43d36e55494993bbbde3299af0c53e5cdf4d4cf)",
- "device_query",
+ "device_query 2.1.0",
"dirs",
"dotenvy_macro",
"ffmpeg-next",
@@ -999,6 +1008,15 @@ dependencies = [
"windows-sys 0.59.0",
]
+[[package]]
+name = "cap-displays"
+version = "0.1.0"
+dependencies = [
+ "core-graphics 0.24.0",
+ "windows 0.58.0",
+ "windows-sys 0.59.0",
+]
+
[[package]]
name = "cap-editor"
version = "0.1.0"
@@ -1116,6 +1134,8 @@ dependencies = [
name = "cap-recording"
version = "0.1.0"
dependencies = [
+ "cap-cursor-capture",
+ "cap-displays",
"cap-flags",
"cap-media",
"cap-project",
@@ -1123,7 +1143,7 @@ dependencies = [
"chrono",
"cidre",
"cocoa 0.26.0",
- "device_query",
+ "device_query 2.1.0",
"either",
"ffmpeg-next",
"flume 0.11.0",
@@ -2243,6 +2263,20 @@ dependencies = [
"x11",
]
+[[package]]
+name = "device_query"
+version = "3.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "429b683ad2cf0d2f0f37f510655854baf18d5f108c3790ab5dd1d09a26874436"
+dependencies = [
+ "macos-accessibility-client",
+ "pkg-config",
+ "readkey",
+ "readmouse",
+ "windows 0.48.0",
+ "x11",
+]
+
[[package]]
name = "diff"
version = "0.1.13"
@@ -6013,9 +6047,9 @@ dependencies = [
[[package]]
name = "readkey"
-version = "0.2.1"
+version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7677f98ca49bc9bb26e04c8abf80ba579e2cb98e8a384a0ff8128ad70670d249"
+checksum = "8a36870cefdfcff57edbc0fa62165f42dfd4e5a0d8965117c1ea84c5700e4450"
[[package]]
name = "readmouse"
diff --git a/apps/desktop/src/routes/editor/ConfigSidebar.tsx b/apps/desktop/src/routes/editor/ConfigSidebar.tsx
index 94defab13..94a696ef0 100644
--- a/apps/desktop/src/routes/editor/ConfigSidebar.tsx
+++ b/apps/desktop/src/routes/editor/ConfigSidebar.tsx
@@ -1639,7 +1639,6 @@ function ZoomSegmentConfig(props: {
variant="danger"
onClick={() => {
batch(() => {
- setEditorState("timeline", "selection", null);
setProject(
"timeline",
"zoomSegments",
@@ -1648,6 +1647,7 @@ function ZoomSegmentConfig(props: {
return s.splice(props.segmentIndex, 1);
})
);
+ setEditorState("timeline", "selection", null);
});
}}
leftIcon={<IconCapTrash />}
diff --git a/crates/cursor-capture/Cargo.toml b/crates/cursor-capture/Cargo.toml
new file mode 100644
index 000000000..aca8a17df
--- /dev/null
+++ b/crates/cursor-capture/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "cap-cursor-capture"
+version = "0.1.0"
+edition = "2024"
+
+[lints]
+workspace = true
+
+[dependencies]
+cap-displays = { path = "../displays" }
+
+device_query = "3.0.1"
+
+[target.'cfg(target_os = "macos")'.dependencies]
+core-graphics = "0.24.0"
diff --git a/crates/cursor-capture/src/lib.rs b/crates/cursor-capture/src/lib.rs
new file mode 100644
index 000000000..bc3f21573
--- /dev/null
+++ b/crates/cursor-capture/src/lib.rs
@@ -0,0 +1,2 @@
+mod position;
+pub use position::*;
diff --git a/crates/cursor-capture/src/main.rs b/crates/cursor-capture/src/main.rs
new file mode 100644
index 000000000..4715f2115
--- /dev/null
+++ b/crates/cursor-capture/src/main.rs
@@ -0,0 +1,13 @@
+use cap_cursor_capture::RawCursorPosition;
+use cap_displays::Display;
+
+fn main() {
+ loop {
+ let position = RawCursorPosition::get()
+ .relative_to_display(Display::list()[0])
+ .normalize()
+ .with_crop((0.0, 0.0), (1.0, 1.0));
+
+ println!("{position:?}");
+ }
+}
diff --git a/crates/cursor-capture/src/position.rs b/crates/cursor-capture/src/position.rs
new file mode 100644
index 000000000..137318256
--- /dev/null
+++ b/crates/cursor-capture/src/position.rs
@@ -0,0 +1,159 @@
+use cap_displays::Display;
+use device_query::{DeviceQuery, DeviceState};
+
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct RawCursorPosition {
+ pub(crate) x: i32,
+ pub(crate) y: i32,
+}
+
+impl RawCursorPosition {
+ pub fn get() -> Self {
+ let device_state = DeviceState::new();
+ let position = device_state.get_mouse().coords;
+
+ Self {
+ x: position.0,
+ y: position.1,
+ }
+ }
+
+ pub fn relative_to_display(&self, display: Display) -> RelativeCursorPosition {
+ RelativeCursorPosition::from_raw(*self, display)
+ }
+}
+
+// relative to display using top-left origin
+#[derive(Clone, Copy)]
+pub struct RelativeCursorPosition {
+ pub(crate) x: i32,
+ pub(crate) y: i32,
+ pub(crate) display: Display,
+}
+
+impl RelativeCursorPosition {
+ pub fn from_raw(raw: RawCursorPosition, display: Display) -> Self {
+ #[cfg(target_os = "macos")]
+ {
+ let raw_display = display.raw_handle().inner();
+ let display_bounds = raw_display.bounds();
+
+ return Self {
+ x: raw.x - display_bounds.origin.x as i32,
+ y: raw.y - display_bounds.origin.y as i32,
+ display,
+ };
+ }
+
+ #[cfg(windows)]
+ {
+ todo!()
+ }
+ }
+
+ pub fn x(&self) -> i32 {
+ self.x
+ }
+
+ pub fn y(&self) -> i32 {
+ self.y
+ }
+
+ pub fn display(&self) -> &Display {
+ &self.display
+ }
+
+ pub fn normalize(&self) -> NormalizedCursorPosition {
+ #[cfg(target_os = "macos")]
+ let (x, y) = {
+ let display_bounds = self.display().raw_handle().inner().bounds();
+
+ (
+ self.x as f32 / display_bounds.size.width as f32,
+ self.y as f32 / display_bounds.size.height as f32,
+ )
+ };
+
+ #[cfg(windows)]
+ let (x, y) = {
+ todo!();
+ // let display_bounds = self.display().raw_handle().bounds();
+
+ // (
+ // self.x as f32 / (display_bounds.right - display_bounds.left) as f32,
+ // self.y as f32 / (display_bounds.bottom - display_bounds.top) as f32,
+ // )
+ };
+
+ NormalizedCursorPosition {
+ x,
+ y,
+ crop_position: (0.0, 0.0),
+ crop_size: (1.0, 1.0),
+ display: self.display,
+ }
+ }
+}
+
+impl std::fmt::Debug for RelativeCursorPosition {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("RelativeCursorPosition")
+ .field("x", &self.x)
+ .field("y", &self.y)
+ .finish()
+ }
+}
+
+pub struct NormalizedCursorPosition {
+ pub(crate) x: f32,
+ pub(crate) y: f32,
+ pub(crate) crop_position: (f32, f32),
+ pub(crate) crop_size: (f32, f32),
+ pub(crate) display: Display,
+}
+
+impl NormalizedCursorPosition {
+ pub fn x(&self) -> f32 {
+ self.x
+ }
+
+ pub fn y(&self) -> f32 {
+ self.y
+ }
+
+ pub fn display(&self) -> &Display {
+ &self.display
+ }
+
+ pub fn crop_position(&self) -> (f32, f32) {
+ self.crop_position
+ }
+
+ pub fn crop_size(&self) -> (f32, f32) {
+ self.crop_size
+ }
+
+ pub fn with_crop(&self, position: (f32, f32), size: (f32, f32)) -> Self {
+ let raw_normalized = (
+ self.x / self.crop_size.0 - self.crop_position.0,
+ self.y / self.crop_size.1 - self.crop_position.1,
+ );
+
+ Self {
+ x: raw_normalized.0 / size.0 - position.0,
+ y: raw_normalized.1 / size.1 - position.1,
+ crop_position: position,
+ crop_size: size,
+ display: self.display,
+ }
+ }
+}
+
+impl std::fmt::Debug for NormalizedCursorPosition {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("NormalizedCursorPosition")
+ .field("x", &self.x)
+ .field("y", &self.y)
+ .finish()
+ }
+}
diff --git a/crates/displays/Cargo.toml b/crates/displays/Cargo.toml
new file mode 100644
index 000000000..4b42eed0c
--- /dev/null
+++ b/crates/displays/Cargo.toml
@@ -0,0 +1,21 @@
+[package]
+name = "cap-displays"
+version = "0.1.0"
+edition = "2024"
+
+[lints]
+workspace = true
+
+[dependencies]
+
+[target.'cfg(target_os = "macos")'.dependencies]
+core-graphics = "0.24.0"
+
+[target.'cfg(target_os= "windows")'.dependencies]
+windows = { workspace = true, features = [
+ "Win32_Foundation",
+ "Win32_System",
+ "Win32_UI_WindowsAndMessaging",
+ "Win32_Graphics_Gdi",
+] }
+windows-sys = { workspace = true }
diff --git a/crates/displays/src/lib.rs b/crates/displays/src/lib.rs
new file mode 100644
index 000000000..69572f3e5
--- /dev/null
+++ b/crates/displays/src/lib.rs
@@ -0,0 +1,16 @@
+mod platform;
+
+pub use platform::DisplayImpl;
+
+#[derive(Clone, Copy)]
+pub struct Display(DisplayImpl);
+
+impl Display {
+ pub fn list() -> Vec<Self> {
+ DisplayImpl::list().into_iter().map(Self).collect()
+ }
+
+ pub fn raw_handle(&self) -> &DisplayImpl {
+ &self.0
+ }
+}
diff --git a/crates/displays/src/main.rs b/crates/displays/src/main.rs
new file mode 100644
index 000000000..e7a11a969
--- /dev/null
+++ b/crates/displays/src/main.rs
@@ -0,0 +1,3 @@
+fn main() {
+ println!("Hello, world!");
+}
diff --git a/crates/displays/src/platform/macos.rs b/crates/displays/src/platform/macos.rs
new file mode 100644
index 000000000..4d6d62b65
--- /dev/null
+++ b/crates/displays/src/platform/macos.rs
@@ -0,0 +1,22 @@
+use core_graphics::display::CGDisplay;
+
+#[derive(Clone, Copy)]
+pub struct DisplayImpl(CGDisplay);
+
+impl DisplayImpl {
+ pub fn primary() -> Self {
+ Self(CGDisplay::main())
+ }
+
+ pub fn list() -> Vec<Self> {
+ CGDisplay::active_displays()
+ .into_iter()
+ .flatten()
+ .map(|v| Self(CGDisplay::new(v)))
+ .collect()
+ }
+
+ pub fn inner(&self) -> CGDisplay {
+ self.0
+ }
+}
diff --git a/crates/displays/src/platform/mod.rs b/crates/displays/src/platform/mod.rs
new file mode 100644
index 000000000..c257c96e5
--- /dev/null
+++ b/crates/displays/src/platform/mod.rs
@@ -0,0 +1,9 @@
+#[cfg(target_os = "macos")]
+mod macos;
+#[cfg(target_os = "macos")]
+pub use macos::DisplayImpl;
+
+#[cfg(windows)]
+mod win;
+#[cfg(windows)]
+pub use win::DisplayImpl;
diff --git a/crates/displays/src/platform/win.rs b/crates/displays/src/platform/win.rs
new file mode 100644
index 000000000..abf18038a
--- /dev/null
+++ b/crates/displays/src/platform/win.rs
@@ -0,0 +1,58 @@
+use windows::{
+ Win32::{
+ Foundation::{BOOL, CloseHandle, FALSE, HWND, LPARAM, RECT, TRUE},
+ Graphics::Gdi::{
+ DEVMODEW, DISPLAY_DEVICEW, EnumDisplayDevicesW, EnumDisplayMonitors,
+ EnumDisplaySettingsW, GetMonitorInfoW, HDC, HMONITOR, MONITOR_DEFAULTTONULL,
+ MONITORINFO, MONITORINFOEXW, MonitorFromWindow,
+ },
+ },
+ core::{PCWSTR, PWSTR},
+};
+
+#[derive(Clone, Copy)]
+pub struct DisplayImpl(HMONITOR);
+
+impl DisplayImpl {
+ pub fn list() -> Vec<Self> {
+ unsafe extern "system" fn monitor_enum_proc(
+ hmonitor: HMONITOR,
+ _hdc: HDC,
+ _lprc_clip: *mut RECT,
+ lparam: LPARAM,
+ ) -> BOOL {
+ let list = &mut *(lparam.0 as *mut Vec<DisplayImpl>);
+
+ list.push(DisplayImpl(hmonitor));
+
+ FALSE
+ }
+
+ let mut list = vec![];
+ unsafe {
+ let _ = EnumDisplayMonitors(
+ None,
+ None,
+ Some(monitor_enum_proc),
+ LPARAM(core::ptr::addr_of_mut!(list) as isize),
+ );
+ };
+
+ list
+ }
+
+ pub fn bounds(&self) -> Option<RECT> {
+ let mut minfo = MONITORINFOEXW::default();
+
+ minfo.monitorInfo.cbSize = std::mem::size_of::<MONITORINFOEXW>() as u32;
+ unsafe {
+ GetMonitorInfoW(self.0, &mut minfo as *mut MONITORINFOEXW as *mut _)
+ .as_bool()
+ .then(|| minfo.monitorInfo.rcMonitor)
+ }
+ }
+
+ pub fn id(&self) -> u32 {
+ self.0.0 as u32
+ }
+}
diff --git a/crates/media/src/sources/screen_capture.rs b/crates/media/src/sources/screen_capture.rs
index a44ea5f02..fa6f15af1 100644
--- a/crates/media/src/sources/screen_capture.rs
+++ b/crates/media/src/sources/screen_capture.rs
@@ -134,6 +134,7 @@ pub struct ScreenCaptureSource<TCaptureFormat: ScreenCaptureFormat> {
show_camera: bool,
force_show_cursor: bool,
bounds: Bounds,
+ display_size: (f32, f32),
video_tx: Sender<(TCaptureFormat::VideoFormat, f64)>,
audio_tx: Option<Sender<(ffmpeg::frame::Audio, f64)>>,
_phantom: std::marker::PhantomData<TCaptureFormat>,
@@ -194,6 +195,7 @@ impl<TCaptureFormat: ScreenCaptureFormat> Clone for ScreenCaptureSource<TCapture
show_camera: self.show_camera,
force_show_cursor: self.force_show_cursor,
bounds: self.bounds,
+ display_size: self.display_size,
video_tx: self.video_tx.clone(),
audio_tx: self.audio_tx.clone(),
_phantom: std::marker::PhantomData,
@@ -208,6 +210,12 @@ struct OptionsConfig {
scap_target: scap::Target,
bounds: Bounds,
crop_area: Option<Area>,
+ display_size: (f32, f32),
+}
+
+struct CropRatio {
+ position: (f32, f32),
+ size: (f32, f32),
}
impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureSource<TCaptureFormat> {
@@ -223,7 +231,12 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureSource<TCaptureFormat> {
) -> Result<Self, String> {
cap_fail::fail!("media::screen_capture::init");
- let (scap_target, bounds, crop_area) = Self::get_options_config(&target)?;
+ let OptionsConfig {
+ scap_target,
+ bounds,
+ crop_area,
+ display_size,
+ } = Self::get_options_config(&target)?;
let fps = get_target_fps(&scap_target).map_err(|e| format!("target_fps / {e}"))?;
let fps = fps.min(max_fps);
@@ -242,6 +255,7 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureSource<TCaptureFormat> {
video_info: VideoInfo::from_raw(RawVideoFormat::Bgra, 0, 0, 0),
options: Arc::new(Default::default()),
bounds,
+ display_size,
show_camera,
force_show_cursor,
video_tx,
@@ -268,9 +282,24 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureSource<TCaptureFormat> {
&self.bounds
}
- fn get_options_config(
- target: &ScreenCaptureTarget,
- ) -> Result<(scap::Target, Bounds, Option<Area>), String> {
+ pub fn crop_ratio(&self) -> ((f32, f32), (f32, f32)) {
+ if let Some(crop_area) = &self.options.crop_area {
+ (
+ (
+ crop_area.origin.x as f32 / self.display_size.0,
+ crop_area.origin.y as f32 / self.display_size.1,
+ ),
+ (
+ crop_area.size.width as f32 / self.display_size.0,
+ crop_area.size.height as f32 / self.display_size.1,
+ ),
+ )
+ } else {
+ ((0.0, 0.0), (1.0, 1.0))
+ }
+ }
+
+ fn get_options_config(target: &ScreenCaptureTarget) -> Result<OptionsConfig, String> {
let targets = scap::get_all_targets();
Ok(match target {
@@ -320,16 +349,20 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureSource<TCaptureFormat> {
},
};
- (
- Target::Display(display),
- Bounds {
+ OptionsConfig {
+ scap_target: Target::Display(display),
+ bounds: Bounds {
x: crop.origin.x,
y: crop.origin.y,
width: crop.size.width,
height: crop.size.height,
},
- Some(crop),
- )
+ crop_area: Some(crop),
+ display_size: (
+ monitor_bounds.size.width as f32,
+ monitor_bounds.size.height as f32,
+ ),
+ }
}
ScreenCaptureTarget::Screen { id } => {
let screens = list_screens();
@@ -339,18 +372,28 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureSource<TCaptureFormat> {
.find(|(i, t)| i.id == *id)
.ok_or_else(|| "Target for screen capture not found".to_string())?;
- (target, platform::monitor_bounds(screen_info.id), None)
+ let bounds = platform::monitor_bounds(screen_info.id);
+
+ OptionsConfig {
+ scap_target: target,
+ bounds,
+ crop_area: None,
+ display_size: (bounds.width as f32, bounds.height as f32),
+ }
}
ScreenCaptureTarget::Area { screen, bounds } => {
+ let screen_bounds = platform::monitor_bounds(*screen);
+
let screens = list_screens();
+ let screen = screens
+ .into_iter()
+ .find_map(|(i, t)| (i.id == *screen).then_some(t))
+ .ok_or_else(|| "Target for screen capture not found".to_string())?;
- (
- screens
- .into_iter()
- .find_map(|(i, t)| (i.id == *screen).then_some(t))
- .ok_or_else(|| "Target for screen capture not found".to_string())?,
- *bounds,
- Some(Area {
+ OptionsConfig {
+ scap_target: screen,
+ bounds: *bounds,
+ crop_area: Some(Area {
size: Size {
width: bounds.width,
height: bounds.height,
@@ -360,7 +403,8 @@ impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureSource<TCaptureFormat> {
y: bounds.y,
},
}),
- )
+ display_size: (screen_bounds.width as f32, screen_bounds.height as f32),
+ }
}
})
}
diff --git a/crates/project/src/cursor.rs b/crates/project/src/cursor.rs
index 8763dc949..4f3dddab1 100644
--- a/crates/project/src/cursor.rs
+++ b/crates/project/src/cursor.rs
@@ -28,8 +28,6 @@ pub struct CursorClickEvent {
pub cursor_id: String,
pub time_ms: f64,
pub down: bool,
- pub x: f64,
- pub y: f64,
}
impl PartialOrd for CursorClickEvent {
diff --git a/crates/recording/Cargo.toml b/crates/recording/Cargo.toml
index 5f5491214..d94afcd1f 100644
--- a/crates/recording/Cargo.toml
+++ b/crates/recording/Cargo.toml
@@ -11,6 +11,8 @@ cap-project = { path = "../project" }
cap-media = { path = "../media" }
cap-flags = { path = "../flags" }
cap-utils = { path = "../utils" }
+cap-displays = { path = "../displays" }
+cap-cursor-capture = { path = "../cursor-capture" }
specta.workspace = true
tokio.workspace = true
diff --git a/crates/recording/src/cursor.rs b/crates/recording/src/cursor.rs
index 6efa5839b..ea9524fcf 100644
--- a/crates/recording/src/cursor.rs
+++ b/crates/recording/src/cursor.rs
@@ -6,6 +6,8 @@ use std::{
time::{Duration, Instant, SystemTime},
};
+use cap_cursor_capture::RawCursorPosition;
+use cap_displays::Display;
use cap_media::platform::Bounds;
use cap_project::{CursorClickEvent, CursorMoveEvent, XY};
use cap_utils::spawn_actor;
@@ -44,7 +46,9 @@ impl CursorActor {
#[tracing::instrument(name = "cursor", skip_all)]
pub fn spawn_cursor_recorder(
- screen_bounds: Bounds,
+ #[allow(unused)] screen_bounds: Bounds,
+ #[cfg(target_os = "macos")] display: Display,
+ #[cfg(target_os = "macos")] crop_ratio: ((f32, f32), (f32, f32)),
cursors_dir: PathBuf,
prev_cursors: Cursors,
next_cursor_id: u32,
@@ -59,6 +63,9 @@ pub fn spawn_cursor_recorder(
let device_state = DeviceState::new();
let mut last_mouse_state = device_state.get_mouse();
+ #[cfg(target_os = "macos")]
+ let mut last_position = RawCursorPosition::get();
+
// Create cursors directory if it doesn't exist
std::fs::create_dir_all(&cursors_dir).unwrap();
@@ -117,27 +124,31 @@ pub fn spawn_cursor_recorder(
"default".to_string()
};
- if mouse_state.coords != last_mouse_state.coords {
- // Get the actual mouse coordinates
- let (mouse_x, mouse_y) = mouse_state.coords;
+ // TODO: use this on windows too
+ #[cfg(target_os = "macos")]
+ let position = {
+ let position = RawCursorPosition::get();
- #[cfg(target_os = "macos")]
- let (mouse_x, mouse_y) = {
- let primary_bounds = cap_media::platform::primary_monitor_bounds();
+ if position != last_position {
+ last_position = position;
- let mouse_x = mouse_x - screen_bounds.x as i32;
- let mouse_y = mouse_y
- + (screen_bounds.y + screen_bounds.height - primary_bounds.height)
- as i32;
+ let cropped_position = position
+ .relative_to_display(display)
+ .normalize()
+ .with_crop(crop_ratio.0, crop_ratio.1);
- (mouse_x, mouse_y)
- };
+ Some((cropped_position.x() as f64, cropped_position.y() as f64))
+ } else {
+ None
+ }
+ };
- #[cfg(not(target_os = "macos"))]
+ #[cfg(windows)]
+ let position = if mouse_state.coords != last_mouse_state.coords {
let (mouse_x, mouse_y) = {
(
- mouse_x - screen_bounds.x as i32,
- mouse_y - screen_bounds.y as i32,
+ mouse_state.coords.0 - screen_bounds.x as i32,
+ mouse_state.coords.1 - screen_bounds.y as i32,
)
};
@@ -160,16 +171,22 @@ pub fn spawn_cursor_recorder(
debug!("X coordinate is invalid: {}", x);
0.5
} else {
- x.max(0.0).min(1.0)
+ x
};
let y = if y.is_nan() || y.is_infinite() {
debug!("Y coordinate is invalid: {}", y);
0.5
} else {
- y.max(0.0).min(1.0)
+ y
};
+ Some((x, y))
+ } else {
+ None
+ };
+
+ if let Some((x, y)) = position {
let mouse_event = CursorMoveEvent {
active_modifiers: vec![],
cursor_id: cursor_id.clone(),
@@ -177,6 +194,7 @@ pub fn spawn_cursor_recorder(
x,
y,
};
+
response.moves.push(mouse_event);
}
@@ -189,84 +207,12 @@ pub fn spawn_cursor_recorder(
continue;
}
- // Get the actual mouse coordinates
- let (mouse_x, mouse_y) = mouse_state.coords;
-
- #[cfg(windows)]
- let (mouse_x, mouse_y) = {
- // On Windows, ensure we're using the correct coordinate system
- // by getting the virtual screen metrics
- use windows::Win32::UI::WindowsAndMessaging::GetSystemMetrics;
- use windows::Win32::UI::WindowsAndMessaging::{
- SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN, SM_XVIRTUALSCREEN,
- SM_YVIRTUALSCREEN,
- };
-
- let virtual_screen_x = unsafe { GetSystemMetrics(SM_XVIRTUALSCREEN) };
- let virtual_screen_y = unsafe { GetSystemMetrics(SM_YVIRTUALSCREEN) };
- let virtual_screen_width = unsafe { GetSystemMetrics(SM_CXVIRTUALSCREEN) };
- let virtual_screen_height = unsafe { GetSystemMetrics(SM_CYVIRTUALSCREEN) };
-
- // If screen_bounds doesn't match the virtual screen, adjust the coordinates
- if (screen_bounds.x as i32 != virtual_screen_x
- || screen_bounds.y as i32 != virtual_screen_y
- || screen_bounds.width as i32 != virtual_screen_width
- || screen_bounds.height as i32 != virtual_screen_height)
- && screen_bounds.width > 0.0
- && screen_bounds.height > 0.0
- {
- // Convert to normalized coordinates in the virtual screen space first
- let norm_x = (mouse_x as f64 - virtual_screen_x as f64)
- / virtual_screen_width as f64;
- let norm_y = (mouse_y as f64 - virtual_screen_y as f64)
- / virtual_screen_height as f64;
-
- // Then convert to the target screen coordinates
- let adjusted_x =
- (norm_x * screen_bounds.width + screen_bounds.x) as i32;
- let adjusted_y =
- (norm_y * screen_bounds.height + screen_bounds.y) as i32;
-
- (adjusted_x, adjusted_y)
- } else {
- (mouse_x, mouse_y)
- }
- };
-
- // Calculate normalized coordinates (0.0 to 1.0) within the screen bounds
- // Check if screen_bounds dimensions are valid to avoid division by zero
- let x = if screen_bounds.width > 0.0 {
- (mouse_x as f64 - screen_bounds.x) / screen_bounds.width
- } else {
- 0.5 // Fallback if width is invalid
- };
-
- let y = if screen_bounds.height > 0.0 {
- (mouse_y as f64 - screen_bounds.y) / screen_bounds.height
- } else {
- 0.5 // Fallback if height is invalid
- };
-
- // Clamp values to ensure they're within valid range
- let x = if x.is_nan() || x.is_infinite() {
- 0.5
- } else {
- x.max(0.0).min(1.0)
- };
- let y = if y.is_nan() || y.is_infinite() {
- 0.5
- } else {
- y.max(0.0).min(1.0)
- };
-
let mouse_event = CursorClickEvent {
down: pressed,
active_modifiers: vec![],
cursor_num: num as u8,
cursor_id: cursor_id.clone(),
time_ms: elapsed,
- x,
- y,
};
response.clicks.push(mouse_event);
}
diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs
index 121561fbc..4bb932578 100644
--- a/crates/recording/src/studio_recording.rs
+++ b/crates/recording/src/studio_recording.rs
@@ -520,6 +520,7 @@ async fn create_segment_pipeline(
system_audio.0,
start_time,
)?;
+ let screen_crop_ratio = screen_source.crop_ratio();
let camera_feed = match camera_feed.as_ref() {
Some(camera_feed) => Some(camera_feed.lock().await),
@@ -706,9 +707,25 @@ async fn create_segment_pipeline(
let (mut pipeline, pipeline_done_rx) = pipeline_builder.build().await?;
- let cursor = custom_cursor_capture.then(|| {
+ let cursor = custom_cursor_capture.then(move || {
let cursor = spawn_cursor_recorder(
screen.bounds.clone(),
+ #[cfg(target_os = "macos")]
+ cap_displays::Display::list()
+ .into_iter()
+ .find(|m| match &options.capture_target {
+ ScreenCaptureTarget::Screen { id }
+ | ScreenCaptureTarget::Area { screen: id, .. } => {
+ m.raw_handle().inner().id == *id
+ }
+ ScreenCaptureTarget::Window { id } => {
+ m.raw_handle().inner().id
+ == cap_media::platform::display_for_window(*id).unwrap().id
+ }
+ })
+ .unwrap(),
+ #[cfg(target_os = "macos")]
+ screen_crop_ratio,
cursors_dir.clone(),
prev_cursors,
next_cursors_id,