mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
Automated fix from the Hex issue pipeline (run 632-2026-10-31t20-08-00-156z). Fixes #632 Co-authored-by: Hex issue pipeline <noreply@anoma.ly>
239 lines
8.2 KiB
Rust
239 lines
8.2 KiB
Rust
use std::ffi::{CStr, c_char, c_void};
|
||
use std::ptr;
|
||
|
||
type CfTypeRef = *const c_void;
|
||
type CfStringRef = *const c_void;
|
||
type AxUiElementRef = *const c_void;
|
||
|
||
const CF_STRING_ENCODING_UTF8: u32 = 0x0800_0100;
|
||
const MESSAGING_TIMEOUT_SECS: f32 = 0.25;
|
||
|
||
#[link(name = "ApplicationServices", kind = "framework")]
|
||
unsafe extern "C" {
|
||
fn AXUIElementCreateApplication(pid: i32) -> AxUiElementRef;
|
||
fn AXUIElementCopyAttributeValue(
|
||
element: AxUiElementRef,
|
||
attribute: CfStringRef,
|
||
value: *mut CfTypeRef,
|
||
) -> i32;
|
||
fn AXUIElementSetMessagingTimeout(element: AxUiElementRef, timeout_in_seconds: f32) -> i32;
|
||
fn AXUIElementCopyParameterizedAttributeValue(
|
||
element: AxUiElementRef,
|
||
parameterized_attribute: CfStringRef,
|
||
parameter: CfTypeRef,
|
||
result: *mut CfTypeRef,
|
||
) -> i32;
|
||
fn AXValueGetTypeID() -> usize;
|
||
fn AXValueCreate(value_type: u32, value: *const c_void) -> CfTypeRef;
|
||
fn AXValueGetValue(value: CfTypeRef, value_type: u32, value_out: *mut c_void) -> bool;
|
||
}
|
||
|
||
/// `kAXValueTypeCFRange`.
|
||
const AX_VALUE_CF_RANGE: u32 = 4;
|
||
|
||
/// Core Foundation's `CFRange`, in UTF-16 code units.
|
||
#[repr(C)]
|
||
#[derive(Clone, Copy, Default)]
|
||
struct CfRange {
|
||
location: isize,
|
||
length: isize,
|
||
}
|
||
|
||
#[link(name = "CoreFoundation", kind = "framework")]
|
||
unsafe extern "C" {
|
||
fn CFGetTypeID(value: CfTypeRef) -> usize;
|
||
fn CFStringGetTypeID() -> usize;
|
||
fn CFStringGetLength(value: CfStringRef) -> isize;
|
||
fn CFStringGetMaximumSizeForEncoding(length: isize, encoding: u32) -> isize;
|
||
fn CFStringGetCString(
|
||
value: CfStringRef,
|
||
buffer: *mut c_char,
|
||
buffer_size: isize,
|
||
encoding: u32,
|
||
) -> bool;
|
||
fn CFStringCreateWithCString(
|
||
allocator: *const c_void,
|
||
string: *const c_char,
|
||
encoding: u32,
|
||
) -> CfStringRef;
|
||
fn CFURLGetTypeID() -> usize;
|
||
fn CFURLGetString(url: CfTypeRef) -> CfStringRef;
|
||
fn CFRelease(value: CfTypeRef);
|
||
}
|
||
|
||
pub fn focused_window_title(pid: i32) -> Option<String> {
|
||
// SAFETY: AXUIElementCreateApplication returns a retained Core Foundation object.
|
||
let application = bounded_element(unsafe { AXUIElementCreateApplication(pid) })?;
|
||
let window = copy_element(&application, c"AXFocusedWindow")?;
|
||
copy_string(&window, c"AXTitle").filter(|title| !title.is_empty())
|
||
}
|
||
|
||
/// The page address of the web area holding the application's focused
|
||
/// element, for browsers without a scripting dictionary (Firefox, Zen).
|
||
/// The outermost web area wins, so focus inside an iframe still reports
|
||
/// the page itself.
|
||
pub fn focused_web_url(pid: i32) -> Option<String> {
|
||
const MAX_DEPTH: usize = 64;
|
||
// SAFETY: AXUIElementCreateApplication returns a retained Core Foundation object.
|
||
let application = bounded_element(unsafe { AXUIElementCreateApplication(pid) })?;
|
||
let mut element = copy_element(&application, c"AXFocusedUIElement")?;
|
||
let mut url = None;
|
||
for _ in 0..MAX_DEPTH {
|
||
if copy_string(&element, c"AXRole").as_deref() == Some("AXWebArea") {
|
||
url = copy_url(&element, c"AXURL").or(url);
|
||
}
|
||
match copy_element(&element, c"AXParent") {
|
||
Some(parent) => element = parent,
|
||
None => break,
|
||
}
|
||
}
|
||
url
|
||
}
|
||
|
||
/// The character just before the insertion point of the application's
|
||
/// focused text element, for Labs › Smart spacing. `None` when text is
|
||
/// selected, the cursor is at the start, or the app doesn't say.
|
||
pub fn char_before_caret(pid: i32) -> Option<char> {
|
||
// SAFETY: AXUIElementCreateApplication returns a retained Core Foundation object.
|
||
let application = bounded_element(unsafe { AXUIElementCreateApplication(pid) })?;
|
||
let focused = copy_element(&application, c"AXFocusedUIElement")?;
|
||
let selection = copy_range(&focused, c"AXSelectedTextRange")?;
|
||
if selection.length != 0 || selection.location <= 0 {
|
||
return None;
|
||
}
|
||
// One UTF-16 unit, or two when it is half of a surrogate pair.
|
||
(1..=2.min(selection.location)).find_map(|units| {
|
||
let range = CfRange {
|
||
location: selection.location - units,
|
||
length: units,
|
||
};
|
||
copy_string_for_range(&focused, range)?.chars().next_back()
|
||
})
|
||
}
|
||
|
||
fn copy_range(element: &CfObject, name: &CStr) -> Option<CfRange> {
|
||
let value = copy_attribute(element, name)?;
|
||
if unsafe { CFGetTypeID(value.0) } != unsafe { AXValueGetTypeID() } {
|
||
return None;
|
||
}
|
||
let mut range = CfRange::default();
|
||
// SAFETY: `value` is an AXValue and `range` has CFRange's layout.
|
||
unsafe {
|
||
AXValueGetValue(
|
||
value.0,
|
||
AX_VALUE_CF_RANGE,
|
||
(&mut range as *mut CfRange).cast(),
|
||
)
|
||
}
|
||
.then_some(range)
|
||
}
|
||
|
||
fn copy_string_for_range(element: &CfObject, range: CfRange) -> Option<String> {
|
||
// SAFETY: AXValueCreate copies the CFRange and returns a retained object.
|
||
let parameter = unsafe { AXValueCreate(AX_VALUE_CF_RANGE, (&range as *const CfRange).cast()) };
|
||
if parameter.is_null() {
|
||
return None;
|
||
}
|
||
let parameter = CfObject(parameter);
|
||
let attribute = CfObject(cf_string_literal(c"AXStringForRange")?);
|
||
let mut value = ptr::null();
|
||
let status = unsafe {
|
||
AXUIElementCopyParameterizedAttributeValue(element.0, attribute.0, parameter.0, &mut value)
|
||
};
|
||
if status != 0 || value.is_null() {
|
||
return None;
|
||
}
|
||
let value = CfObject(value);
|
||
cf_string(value.0).filter(|text| !text.is_empty())
|
||
}
|
||
|
||
struct CfObject(CfTypeRef);
|
||
|
||
impl Drop for CfObject {
|
||
fn drop(&mut self) {
|
||
unsafe { CFRelease(self.0) };
|
||
}
|
||
}
|
||
|
||
fn bounded_element(element: AxUiElementRef) -> Option<CfObject> {
|
||
if element.is_null() {
|
||
return None;
|
||
}
|
||
let element = CfObject(element);
|
||
(unsafe { AXUIElementSetMessagingTimeout(element.0, MESSAGING_TIMEOUT_SECS) } == 0)
|
||
.then_some(element)
|
||
}
|
||
|
||
fn copy_attribute(element: &CfObject, name: &CStr) -> Option<CfObject> {
|
||
let attribute = CfObject(cf_string_literal(name)?);
|
||
let mut value = ptr::null();
|
||
let status = unsafe { AXUIElementCopyAttributeValue(element.0, attribute.0, &mut value) };
|
||
// `then`, not `then_some`: dropping a `CfObject(null)` would CFRelease(NULL).
|
||
(status == 0 && !value.is_null()).then(|| CfObject(value))
|
||
}
|
||
|
||
fn copy_element(element: &CfObject, name: &CStr) -> Option<CfObject> {
|
||
let child = copy_attribute(element, name)?;
|
||
(unsafe { AXUIElementSetMessagingTimeout(child.0, MESSAGING_TIMEOUT_SECS) } == 0)
|
||
.then_some(child)
|
||
}
|
||
|
||
fn copy_string(element: &CfObject, name: &CStr) -> Option<String> {
|
||
let value = copy_attribute(element, name)?;
|
||
cf_string(value.0)
|
||
}
|
||
|
||
fn copy_url(element: &CfObject, name: &CStr) -> Option<String> {
|
||
let value = copy_attribute(element, name)?;
|
||
if unsafe { CFGetTypeID(value.0) } != unsafe { CFURLGetTypeID() } {
|
||
return cf_string(value.0);
|
||
}
|
||
// SAFETY: CFURLGetString follows the Get rule; `value` keeps it alive.
|
||
let string = unsafe { CFURLGetString(value.0) };
|
||
(!string.is_null()).then(|| cf_string(string)).flatten()
|
||
}
|
||
|
||
fn cf_string_literal(value: &CStr) -> Option<CfStringRef> {
|
||
let string =
|
||
unsafe { CFStringCreateWithCString(ptr::null(), value.as_ptr(), CF_STRING_ENCODING_UTF8) };
|
||
(!string.is_null()).then_some(string)
|
||
}
|
||
|
||
fn cf_string(value: CfTypeRef) -> Option<String> {
|
||
if unsafe { CFGetTypeID(value) } != unsafe { CFStringGetTypeID() } {
|
||
return None;
|
||
}
|
||
let value = value.cast();
|
||
let length = unsafe { CFStringGetLength(value) };
|
||
let capacity = unsafe { CFStringGetMaximumSizeForEncoding(length, CF_STRING_ENCODING_UTF8) }
|
||
.saturating_add(1);
|
||
let mut bytes = vec![0_u8; capacity as usize];
|
||
if !unsafe {
|
||
CFStringGetCString(
|
||
value,
|
||
bytes.as_mut_ptr().cast(),
|
||
capacity,
|
||
CF_STRING_ENCODING_UTF8,
|
||
)
|
||
} {
|
||
return None;
|
||
}
|
||
let length = bytes
|
||
.iter()
|
||
.position(|byte| *byte == 0)
|
||
.unwrap_or(bytes.len());
|
||
bytes.truncate(length);
|
||
String::from_utf8(bytes).ok()
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn a_missing_attribute_is_none() {
|
||
assert_eq!(focused_window_title(i32::MAX), None);
|
||
assert_eq!(focused_web_url(i32::MAX), None);
|
||
assert_eq!(char_before_caret(i32::MAX), None);
|
||
}
|
||
}
|