focus-tracker 1.1.0

Cross-platform focus tracker for Linux (X11), macOS and Windows
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use focus_tracker_core::{FocusTrackerError, FocusTrackerResult, FocusedWindow, IconConfig};
use objc2::AnyThread;
use objc2::rc::autoreleasepool;
use objc2::runtime::AnyObject;
use objc2_app_kit::{
    NSBitmapImageFileType, NSBitmapImageRep, NSCalibratedRGBColorSpace, NSGraphicsContext, NSImage,
    NSRunningApplication, NSWorkspace,
};
use objc2_foundation::{NSDictionary, NSPoint, NSRect, NSSize, NSString, ns_string};
use std::ffi::c_void;

#[link(name = "CoreGraphics", kind = "framework")]
unsafe extern "C" {
    fn CGWindowListCopyWindowInfo(option: u32, relative_to_window: u32) -> *const c_void;
}

const K_CG_WINDOW_LIST_OPTION_ON_SCREEN_ONLY: u32 = 1;
const K_CG_WINDOW_LIST_EXCLUDE_DESKTOP_ELEMENTS: u32 = 1 << 4;
const K_CG_NULL_WINDOW_ID: u32 = 0;

#[link(name = "CoreFoundation", kind = "framework")]
unsafe extern "C" {
    fn CFRelease(cf: *const c_void);

    fn CFArrayGetCount(the_array: *const c_void) -> isize;
    fn CFArrayGetValueAtIndex(the_array: *const c_void, idx: isize) -> *const c_void;

    fn CFDictionaryGetValue(the_dict: *const c_void, key: *const c_void) -> *const c_void;

    fn CFNumberGetValue(number: *const c_void, the_type: i32, value_ptr: *mut c_void) -> bool;

    fn CFStringGetLength(the_string: *const c_void) -> isize;
    fn CFStringGetCString(
        the_string: *const c_void,
        buffer: *mut i8,
        buffer_size: isize,
        encoding: u32,
    ) -> bool;
}

const K_CF_NUMBER_SINT32_TYPE: i32 = 3;
const K_CF_STRING_ENCODING_UTF8: u32 = 0x0800_0100;

#[link(name = "ApplicationServices", kind = "framework")]
unsafe extern "C" {
    fn AXUIElementCreateApplication(pid: i32) -> *mut c_void;
    fn AXUIElementCopyAttributeValue(
        element: *const c_void,
        attribute: *const c_void,
        value: *mut *mut c_void,
    ) -> i32;
}

const K_AX_ERROR_SUCCESS: i32 = 0;
const K_AX_ERROR_API_DISABLED: i32 = -25211;

/// Returns information about the currently focused (frontmost) window.
///
/// Uses [`CGWindowListCopyWindowInfo`] to query the window server directly
/// (reliable from any thread), then resolves the process name via
/// [`NSRunningApplication`] and the window title via the Accessibility API.
///
/// # Errors
///
/// Returns an error if no on-screen window is found, the process name cannot
/// be determined, or the Accessibility API denies permission.
pub fn get_frontmost_window_basic_info() -> FocusTrackerResult<FocusedWindow> {
    autoreleasepool(|_pool| {
        let pid = get_frontmost_window_pid()?;

        let app = NSRunningApplication::runningApplicationWithProcessIdentifier(pid);

        let process_name = app
            .and_then(|a| a.localizedName().map(|n| n.to_string()))
            .ok_or_else(|| {
                FocusTrackerError::platform(format!("failed to get process name for pid {pid}"))
            })?;

        let window_title = get_window_title_via_accessibility(pid)?;

        Ok(FocusedWindow {
            process_id: u32::try_from(pid).unwrap_or(0),
            window_title,
            process_name,
            icon: None,
        })
    })
}

/// Fetches the application icon for the given PID and returns it as an RGBA
/// image.
///
/// The icon is extracted via [`NSWorkspace::iconForFile`] using the app's
/// bundle path, then rendered at the target size through
/// [`NSGraphicsContext`] into an [`NSBitmapImageRep`] and encoded as PNG.
///
/// # Errors
///
/// Returns an error if the icon cannot be rendered or if the PNG data
/// cannot be decoded.
pub fn fetch_icon_for_pid(
    pid: i32,
    icon_config: &IconConfig,
) -> FocusTrackerResult<Option<image::RgbaImage>> {
    autoreleasepool(|_pool| {
        let app = NSRunningApplication::runningApplicationWithProcessIdentifier(pid);
        match app {
            Some(app) => get_app_icon(&app, icon_config),
            None => Ok(None),
        }
    })
}

/// Queries the window server for the PID of the frontmost normal application
/// window.
///
/// The window list returned by `CGWindowListCopyWindowInfo` is ordered
/// front-to-back.  We pick the first entry at layer 0 (normal windows),
/// which corresponds to the currently focused application.  Status-bar items,
/// menus, and other chrome live on higher layers and are skipped.
fn get_frontmost_window_pid() -> FocusTrackerResult<i32> {
    unsafe {
        let options =
            K_CG_WINDOW_LIST_OPTION_ON_SCREEN_ONLY | K_CG_WINDOW_LIST_EXCLUDE_DESKTOP_ELEMENTS;
        let window_list = CGWindowListCopyWindowInfo(options, K_CG_NULL_WINDOW_ID);

        if window_list.is_null() {
            return Err(FocusTrackerError::platform("failed to get window list"));
        }

        let count = CFArrayGetCount(window_list);
        if count <= 0 {
            CFRelease(window_list);
            return Err(FocusTrackerError::platform("no windows found"));
        }

        let layer_key: *const c_void =
            std::ptr::from_ref::<NSString>(ns_string!("kCGWindowLayer")).cast();
        let pid_key: *const c_void =
            std::ptr::from_ref::<NSString>(ns_string!("kCGWindowOwnerPID")).cast();

        for i in 0..count {
            let dict = CFArrayGetValueAtIndex(window_list, i);
            if dict.is_null() {
                continue;
            }

            let layer_val = CFDictionaryGetValue(dict, layer_key);
            if !layer_val.is_null() {
                let mut layer: i32 = 0;
                let ok = CFNumberGetValue(
                    layer_val,
                    K_CF_NUMBER_SINT32_TYPE,
                    std::ptr::from_mut(&mut layer).cast(),
                );
                if ok && layer != 0 {
                    continue;
                }
            }

            let pid_val = CFDictionaryGetValue(dict, pid_key);
            if pid_val.is_null() {
                continue;
            }
            let mut pid: i32 = 0;
            if !CFNumberGetValue(
                pid_val,
                K_CF_NUMBER_SINT32_TYPE,
                std::ptr::from_mut(&mut pid).cast(),
            ) {
                continue;
            }

            CFRelease(window_list);
            return Ok(pid);
        }

        CFRelease(window_list);
        Err(FocusTrackerError::platform(
            "no normal application window found",
        ))
    }
}

fn get_window_title_via_accessibility(pid: i32) -> FocusTrackerResult<Option<String>> {
    let Some(focused_window) = copy_focused_window(pid)? else {
        return Ok(None);
    };
    // SAFETY: `focused_window` is a non-null `AXUIElementRef` returned by
    // `AXUIElementCopyAttributeValue`; ownership is transferred to us so we
    // must release it once we've finished reading attributes off it.
    let title = unsafe { copy_string_attribute(focused_window, ns_string!("AXTitle")) };
    unsafe { CFRelease(focused_window) };
    Ok(title)
}

/// Returns the document URL of the focused window for the given PID, when the
/// window exposes one via the Accessibility API.
///
/// Apps that follow Apple's `NSDocument`-based architecture (Preview, Pages,
/// TextEdit, …) populate `AXDocument` on their focused window with a
/// `file://` URL pointing at the open document.
///
/// Returns:
///
/// - `Ok(Some(url))` when the focused window exposes `AXDocument`.
/// - `Ok(None)` when there is no focused window or it does not expose the
///   attribute (e.g. an unsaved document, an app that does not implement it,
///   or the user is looking at a non-document window).
///
/// # Errors
///
/// Returns [`FocusTrackerError::PermissionDenied`] when macOS denies
/// Accessibility access — callers should treat this as "we can't see what
/// the user is reading" and back off, identical to the title path.
pub fn focused_document_url(pid: u32) -> FocusTrackerResult<Option<String>> {
    let pid = i32::try_from(pid).map_err(|_| {
        FocusTrackerError::platform(format!("pid {pid} does not fit into a macOS pid_t"))
    })?;
    let Some(focused_window) = copy_focused_window(pid)? else {
        return Ok(None);
    };
    // SAFETY: see `get_window_title_via_accessibility`.
    let url = unsafe { copy_string_attribute(focused_window, ns_string!("AXDocument")) };
    unsafe { CFRelease(focused_window) };
    Ok(url)
}

/// Returns an owned `AXUIElementRef` for the focused window of the given PID.
///
/// The caller is responsible for `CFRelease`ing the returned pointer.
fn copy_focused_window(pid: i32) -> FocusTrackerResult<Option<*mut c_void>> {
    let app_element = unsafe { AXUIElementCreateApplication(pid) };
    if app_element.is_null() {
        return Ok(None);
    }

    let focused_window_attr = ns_string!("AXFocusedWindow");
    let mut focused_window: *mut c_void = std::ptr::null_mut();
    let result = unsafe {
        AXUIElementCopyAttributeValue(
            app_element,
            std::ptr::from_ref::<NSString>(focused_window_attr).cast::<c_void>(),
            &raw mut focused_window,
        )
    };

    unsafe { CFRelease(app_element) };

    if result == K_AX_ERROR_API_DISABLED {
        return Err(FocusTrackerError::PermissionDenied {
            context: "macOS accessibility API denied (AXUIElement)".into(),
        });
    }

    if result != K_AX_ERROR_SUCCESS || focused_window.is_null() {
        return Ok(None);
    }

    Ok(Some(focused_window))
}

/// Reads a CFString-valued attribute off an `AXUIElementRef` and returns it
/// as an owned [`String`].
///
/// # Safety
///
/// `element` must be a valid, non-null `AXUIElementRef`. `attribute` must be
/// a non-null `NSString` (typically constructed via [`ns_string!`]).
unsafe fn copy_string_attribute(element: *mut c_void, attribute: &NSString) -> Option<String> {
    let mut value: *mut c_void = std::ptr::null_mut();
    let result = unsafe {
        AXUIElementCopyAttributeValue(
            element,
            std::ptr::from_ref::<NSString>(attribute).cast::<c_void>(),
            &raw mut value,
        )
    };

    if result != K_AX_ERROR_SUCCESS || value.is_null() {
        return None;
    }

    let s = unsafe { cfstring_to_string(value) };
    unsafe { CFRelease(value) };
    s
}

/// Converts a `CFStringRef` (passed as `*const c_void`) to a Rust [`String`].
///
/// # Safety
///
/// `cf_string` must point to a valid `CFString` instance, or be null.
unsafe fn cfstring_to_string(cf_string: *const c_void) -> Option<String> {
    if cf_string.is_null() {
        return None;
    }

    let length = unsafe { CFStringGetLength(cf_string) };
    if length <= 0 {
        return Some(String::new());
    }

    let buffer_size = (length * 4 + 1).cast_unsigned();
    let mut buffer: Vec<i8> = vec![0; buffer_size];

    let success = unsafe {
        CFStringGetCString(
            cf_string,
            buffer.as_mut_ptr(),
            buffer_size.cast_signed(),
            K_CF_STRING_ENCODING_UTF8,
        )
    };

    if success {
        let c_str = unsafe { std::ffi::CStr::from_ptr(buffer.as_ptr()) };
        c_str.to_str().ok().map(std::string::ToString::to_string)
    } else {
        None
    }
}

fn get_app_icon(
    app: &NSRunningApplication,
    icon_config: &IconConfig,
) -> FocusTrackerResult<Option<image::RgbaImage>> {
    let Some(bundle_url) = app.bundleURL() else {
        return Ok(None);
    };

    let Some(path) = bundle_url.path() else {
        return Ok(None);
    };

    let workspace = NSWorkspace::sharedWorkspace();
    let ns_image = workspace.iconForFile(&path);

    nsimage_to_rgba(&ns_image, icon_config)
}

/// Converts an [`NSImage`] to an [`image::RgbaImage`] at the configured icon
/// dimensions.
///
/// Instead of decoding the raw multi-resolution TIFF that `NSImage` produces,
/// we draw the image at the target size into a fresh [`NSBitmapImageRep`] via
/// [`NSGraphicsContext`].  This lets AppKit handle resolution selection and
/// colour-profile normalisation in one step and produces a small bitmap that
/// is fast to encode as PNG and decode with the [`image`] crate.
///
/// # Thread safety
///
/// `NSGraphicsContext::graphicsContextWithBitmapImageRep:` creates a purely
/// off-screen context that is safe to use from any thread (per Apple
/// documentation).
fn nsimage_to_rgba(
    ns_image: &NSImage,
    icon_config: &IconConfig,
) -> FocusTrackerResult<Option<image::RgbaImage>> {
    let icon_size = icon_config.get_size_or_default();

    let png_bytes = render_nsimage_to_png(ns_image, icon_size)?;

    let dynamic_image = image::load_from_memory(&png_bytes).map_err(|e| {
        FocusTrackerError::platform_with_source("failed to decode icon image data", e)
    })?;

    Ok(Some(dynamic_image.to_rgba8()))
}

/// Draws an [`NSImage`] at `size × size` pixels into a new RGBA
/// [`NSBitmapImageRep`] and returns the result encoded as PNG.
///
/// By rendering through [`NSGraphicsContext`] AppKit picks the best resolution
/// variant from the (potentially multi-resolution) source image and applies
/// any necessary colour-space conversions.  The output is a plain
/// `size × size` RGBA PNG that the [`image`] crate can decode without issues.
fn render_nsimage_to_png(ns_image: &NSImage, size: u32) -> FocusTrackerResult<Vec<u8>> {
    let size_i = size as isize;
    let size_f = size as f64;

    let bitmap_rep = unsafe {
        NSBitmapImageRep::initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel(
            NSBitmapImageRep::alloc(),
            std::ptr::null_mut(), // planes — let AppKit allocate
            size_i,               // pixelsWide
            size_i,               // pixelsHigh
            8,                    // bitsPerSample
            4,                    // samplesPerPixel (RGBA)
            true,                 // hasAlpha
            false,                // isPlanar
            NSCalibratedRGBColorSpace,
            0,                    // bytesPerRow  (0 = auto-calculate)
            0,                    // bitsPerPixel (0 = auto-calculate)
        )
    }
    .ok_or_else(|| FocusTrackerError::platform("failed to create target NSBitmapImageRep"))?;

    let context =
        NSGraphicsContext::graphicsContextWithBitmapImageRep(&bitmap_rep).ok_or_else(|| {
            FocusTrackerError::platform("failed to create NSGraphicsContext for icon rendering")
        })?;

    NSGraphicsContext::saveGraphicsState_class();
    NSGraphicsContext::setCurrentContext(Some(&context));

    let target_rect = NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(size_f, size_f));
    ns_image.drawInRect(target_rect);

    NSGraphicsContext::restoreGraphicsState_class();

    let empty_props = NSDictionary::<NSString, AnyObject>::new();

    let png_data = unsafe {
        bitmap_rep.representationUsingType_properties(NSBitmapImageFileType::PNG, &empty_props)
    }
    .ok_or_else(|| FocusTrackerError::platform("failed to encode rendered icon as PNG"))?;

    Ok(png_data.to_vec())
}