Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/cdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ fn endpoint_owner_process_ids(port: u16, _process_id: u32) -> Result<BTreeSet<u3
if written >= descriptors.len() || written % 8 != 0 {
return Err(CdpError::StaleTarget);
}
for descriptor in descriptors[..written].chunks_exact(8) {
for descriptor in descriptors[..written].chunks(8) {
let file_descriptor =
i32::from_ne_bytes(descriptor[..4].try_into().map_err(|_| CdpError::Protocol)?);
let descriptor_type = u32::from_ne_bytes(
Expand Down Expand Up @@ -1851,7 +1851,7 @@ fn endpoint_owner_process_ids(port: u16, _process_id: u32) -> Result<BTreeSet<u3
return Err(CdpError::Protocol);
}
let mut owners = BTreeSet::new();
for row in table[4..].chunks_exact(TCP_ROW_BYTES).take(row_count) {
for row in table[4..].chunks(TCP_ROW_BYTES).take(row_count) {
if u32::from_ne_bytes(row[..4].try_into().map_err(|_| CdpError::Protocol)?) == TCP_LISTEN
&& row[4..8] == Ipv4Addr::LOCALHOST.octets()
&& u16::from_be_bytes(row[8..10].try_into().map_err(|_| CdpError::Protocol)?) == port
Expand Down
176 changes: 93 additions & 83 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1963,95 +1963,105 @@ fn native_drag(
}
}

#[cfg(windows)]
fn native_click_windows(point: &NativePoint, button: &str) -> Result<(), NativeError> {
use windows::Win32::UI::Input::KeyboardAndMouse::*;
use windows::Win32::UI::WindowsAndMessaging::SetCursorPos;

if unsafe { SetCursorPos(point.x as i32, point.y as i32) }.is_err() {
return Err(NativeError);
}
let (down_flags, up_flags) = match button {
"left" => (MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP),
"right" => (MOUSEEVENTF_RIGHTDOWN, MOUSEEVENTF_RIGHTUP),
"middle" => (MOUSEEVENTF_MIDDLEDOWN, MOUSEEVENTF_MIDDLEUP),
_ => return Err(NativeError),
};
let down = INPUT {
r#type: INPUT_MOUSE,
Anonymous: INPUT_0 {
mi: MOUSEINPUT {
dx: 0,
dy: 0,
mouseData: 0,
dwFlags: down_flags,
time: 0,
dwExtraInfo: 0,
},
},
};
let up = INPUT {
r#type: INPUT_MOUSE,
Anonymous: INPUT_0 {
mi: MOUSEINPUT {
dx: 0,
dy: 0,
mouseData: 0,
dwFlags: up_flags,
time: 0,
dwExtraInfo: 0,
},
},
};
if unsafe { SendInput(&[down, up], std::mem::size_of::<INPUT>() as i32) } != 2 {
return Err(NativeError);
}
return Ok(());
}

#[cfg(target_os = "macos")]
fn native_click_macos(point: &NativePoint, button: &str) -> Result<(), NativeError> {
use core_graphics::event::{CGEvent, CGEventType, CGMouseButton};
use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};
use core_graphics::geometry::CGPoint;

if !native_permissions()
.get("accessibility")
.and_then(Value::as_bool)
.unwrap_or(false)
{
return Err(NativeError);
}
let (down, up, mouse_button) = match button {
"left" => (
CGEventType::LeftMouseDown,
CGEventType::LeftMouseUp,
CGMouseButton::Left,
),
"right" => (
CGEventType::RightMouseDown,
CGEventType::RightMouseUp,
CGMouseButton::Right,
),
"middle" => (
CGEventType::OtherMouseDown,
CGEventType::OtherMouseUp,
CGMouseButton::Center,
),
_ => return Err(NativeError),
};
let position = CGPoint::new(point.x as f64, point.y as f64);
let down_source =
CGEventSource::new(CGEventSourceStateID::CombinedSessionState).map_err(|_| NativeError)?;
let down_event = CGEvent::new_mouse_event(down_source, down, position, mouse_button)
.map_err(|_| NativeError)?;
let up_source =
CGEventSource::new(CGEventSourceStateID::CombinedSessionState).map_err(|_| NativeError)?;
let up_event =
CGEvent::new_mouse_event(up_source, up, position, mouse_button).map_err(|_| NativeError)?;
mac_post_event(&down_event)?;
mac_post_event(&up_event)?;
Ok(())
}

fn native_click(point: &NativePoint, button: &str) -> Result<(), NativeError> {
#[cfg(target_os = "macos")]
{
use core_graphics::event::{CGEvent, CGEventType, CGMouseButton};
use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};
use core_graphics::geometry::CGPoint;

if !native_permissions()
.get("accessibility")
.and_then(Value::as_bool)
.unwrap_or(false)
{
return Err(NativeError);
}
let (down, up, mouse_button) = match button {
"left" => (
CGEventType::LeftMouseDown,
CGEventType::LeftMouseUp,
CGMouseButton::Left,
),
"right" => (
CGEventType::RightMouseDown,
CGEventType::RightMouseUp,
CGMouseButton::Right,
),
"middle" => (
CGEventType::OtherMouseDown,
CGEventType::OtherMouseUp,
CGMouseButton::Center,
),
_ => return Err(NativeError),
};
let position = CGPoint::new(point.x as f64, point.y as f64);
let down_source = CGEventSource::new(CGEventSourceStateID::CombinedSessionState)
.map_err(|_| NativeError)?;
let down_event = CGEvent::new_mouse_event(down_source, down, position, mouse_button)
.map_err(|_| NativeError)?;
let up_source = CGEventSource::new(CGEventSourceStateID::CombinedSessionState)
.map_err(|_| NativeError)?;
let up_event = CGEvent::new_mouse_event(up_source, up, position, mouse_button)
.map_err(|_| NativeError)?;
mac_post_event(&down_event)?;
mac_post_event(&up_event)?;
Ok(())
return native_click_macos(point, button);
}
#[cfg(windows)]
{
use windows::Win32::UI::Input::KeyboardAndMouse::*;
use windows::Win32::UI::WindowsAndMessaging::SetCursorPos;

if unsafe { SetCursorPos(point.x as i32, point.y as i32) }.is_err() {
return Err(NativeError);
}
let (down_flags, up_flags) = match button {
"left" => (MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP),
"right" => (MOUSEEVENTF_RIGHTDOWN, MOUSEEVENTF_RIGHTUP),
"middle" => (MOUSEEVENTF_MIDDLEDOWN, MOUSEEVENTF_MIDDLEUP),
_ => return Err(NativeError),
};
let down = INPUT {
r#type: INPUT_MOUSE,
Anonymous: INPUT_0 {
mi: MOUSEINPUT {
dx: 0,
dy: 0,
mouseData: 0,
dwFlags: down_flags,
time: 0,
dwExtraInfo: 0,
},
},
};
let up = INPUT {
r#type: INPUT_MOUSE,
Anonymous: INPUT_0 {
mi: MOUSEINPUT {
dx: 0,
dy: 0,
mouseData: 0,
dwFlags: up_flags,
time: 0,
dwExtraInfo: 0,
},
},
};
if unsafe { SendInput(&[down, up], std::mem::size_of::<INPUT>() as i32) } != 2 {
return Err(NativeError);
}
return Ok(());
return native_click_windows(point, button);
}
#[cfg(target_os = "linux")]
{
Expand Down
2 changes: 1 addition & 1 deletion tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn run(arguments: &[&str], stdin: &str) -> std::process::Output {
.stderr(Stdio::piped())
.spawn()
.expect("spawn CLI");
if let Some(mut child_stdin) = child.stdin.take() {
if let Some(mut child_stdin) = child.stdin.take() {
let _ = child_stdin.write_all(stdin.as_bytes());
}
child.wait_with_output().expect("CLI output")
Expand Down
Loading