@@ -2,6 +2,7 @@ |
| 2 | 2 | |
| 3 | 3 | mod events; |
| 4 | 4 | mod skylight; |
| 5 | +mod nswindow_overlay; |
| 5 | 6 | |
| 6 | 7 | use events::Event; |
| 7 | 8 | use skylight::*; |
@@ -15,7 +16,6 @@ use tracing::debug; |
| 15 | 16 | static SIGNAL_STOP_REQUESTED: AtomicBool = AtomicBool::new(false); |
| 16 | 17 | const MIN_TRACKED_WINDOW_SIZE: f64 = 4.0; |
| 17 | 18 | const GEOMETRY_EPSILON: f64 = 0.5; |
| 18 | | -const SCALE_EPSILON: f64 = 0.01; |
| 19 | 19 | const WINDOW_ATTRIBUTE_REAL: u64 = 1 << 1; |
| 20 | 20 | const WINDOW_TAG_DOCUMENT: u64 = 1 << 0; |
| 21 | 21 | const WINDOW_TAG_FLOATING: u64 = 1 << 1; |
@@ -24,12 +24,31 @@ const WINDOW_TAG_IGNORES_CYCLE: u64 = 1 << 18; |
| 24 | 24 | const WINDOW_TAG_MODAL: u64 = 1 << 31; |
| 25 | 25 | const WINDOW_TAG_REAL_SURFACE: u64 = 1 << 58; |
| 26 | 26 | |
| 27 | | -/// Per-overlay state: the connection it was created on + its wid. |
| 27 | +/// Per-overlay state: an NSWindow drawing the rounded-rect border via |
| 28 | +/// CAShapeLayer. Replaces the old SLS-only overlay window — see |
| 29 | +/// nswindow_overlay.rs for the rationale (screencaptureui on Tahoe |
| 30 | +/// only honors NSWindow.sharingType, not SLS sharing-state nor tag |
| 31 | +/// bits, for raw SLS-only windows). |
| 28 | 32 | struct Overlay { |
| 29 | | - cid: CGSConnectionID, |
| 30 | | - wid: u32, |
| 31 | | - bounds: CGRect, |
| 32 | | - scale: f64, |
| 33 | + window: nswindow_overlay::OverlayWindow, |
| 34 | +} |
| 35 | + |
| 36 | +impl Overlay { |
| 37 | + fn wid(&self) -> u32 { |
| 38 | + self.window.wid() |
| 39 | + } |
| 40 | + fn bounds(&self) -> CGRect { |
| 41 | + CGRect { |
| 42 | + origin: CGPoint { |
| 43 | + x: self.window.bounds_cg_x, |
| 44 | + y: self.window.bounds_cg_y, |
| 45 | + }, |
| 46 | + size: CGSize { |
| 47 | + width: self.window.bounds_cg_w, |
| 48 | + height: self.window.bounds_cg_h, |
| 49 | + }, |
| 50 | + } |
| 51 | + } |
| 33 | 52 | } |
| 34 | 53 | |
| 35 | 54 | fn window_area(bounds: CGRect) -> f64 { |
@@ -169,59 +188,6 @@ unsafe extern "C" fn handle_sigint(_: libc::c_int) { |
| 169 | 188 | SIGNAL_STOP_REQUESTED.store(true, Ordering::Relaxed); |
| 170 | 189 | } |
| 171 | 190 | |
| 172 | | -fn display_scale_for_bounds(bounds: CGRect) -> f64 { |
| 173 | | - let point = CGPoint { |
| 174 | | - x: bounds.origin.x + bounds.size.width / 2.0, |
| 175 | | - y: bounds.origin.y + bounds.size.height / 2.0, |
| 176 | | - }; |
| 177 | | - |
| 178 | | - unsafe { |
| 179 | | - let mut display_id = 0u32; |
| 180 | | - let mut count = 0u32; |
| 181 | | - if CGGetDisplaysWithPoint(point, 1, &mut display_id, &mut count) != kCGErrorSuccess |
| 182 | | - || count == 0 |
| 183 | | - { |
| 184 | | - return 2.0; |
| 185 | | - } |
| 186 | | - |
| 187 | | - let mode = CGDisplayCopyDisplayMode(display_id); |
| 188 | | - if mode.is_null() { |
| 189 | | - return 2.0; |
| 190 | | - } |
| 191 | | - |
| 192 | | - let width = CGDisplayModeGetWidth(mode) as f64; |
| 193 | | - let height = CGDisplayModeGetHeight(mode) as f64; |
| 194 | | - let pixel_width = CGDisplayModeGetPixelWidth(mode) as f64; |
| 195 | | - let pixel_height = CGDisplayModeGetPixelHeight(mode) as f64; |
| 196 | | - CFRelease(mode as CFTypeRef); |
| 197 | | - |
| 198 | | - let scale_x = if width > 0.0 { |
| 199 | | - pixel_width / width |
| 200 | | - } else { |
| 201 | | - 0.0 |
| 202 | | - }; |
| 203 | | - let scale_y = if height > 0.0 { |
| 204 | | - pixel_height / height |
| 205 | | - } else { |
| 206 | | - 0.0 |
| 207 | | - }; |
| 208 | | - |
| 209 | | - let scale = match (scale_x.is_finite(), scale_y.is_finite()) { |
| 210 | | - (true, true) if scale_x >= 1.0 && scale_y >= 1.0 => (scale_x + scale_y) / 2.0, |
| 211 | | - (true, _) if scale_x >= 1.0 => scale_x, |
| 212 | | - (_, true) if scale_y >= 1.0 => scale_y, |
| 213 | | - _ => 2.0, |
| 214 | | - }; |
| 215 | | - |
| 216 | | - debug!( |
| 217 | | - "[display_scale] display={} point=({:.1},{:.1}) scale={:.2}", |
| 218 | | - display_id, point.x, point.y, scale |
| 219 | | - ); |
| 220 | | - |
| 221 | | - scale |
| 222 | | - } |
| 223 | | -} |
| 224 | | - |
| 225 | 191 | /// Tracks overlays for target windows. |
| 226 | 192 | struct BorderMap { |
| 227 | 193 | overlays: HashMap<u32, Overlay>, |
@@ -233,12 +199,19 @@ struct BorderMap { |
| 233 | 199 | active_color: (f64, f64, f64, f64), |
| 234 | 200 | inactive_color: (f64, f64, f64, f64), |
| 235 | 201 | active_only: bool, |
| 202 | + mtm: objc2::MainThreadMarker, |
| 236 | 203 | } |
| 237 | 204 | |
| 238 | 205 | impl BorderMap { |
| 239 | | - fn new(cid: CGSConnectionID, own_pid: i32, border_width: f64) -> Self { |
| 206 | + fn new( |
| 207 | + cid: CGSConnectionID, |
| 208 | + own_pid: i32, |
| 209 | + border_width: f64, |
| 210 | + mtm: objc2::MainThreadMarker, |
| 211 | + ) -> Self { |
| 240 | 212 | Self { |
| 241 | 213 | overlays: HashMap::new(), |
| 214 | + mtm, |
| 242 | 215 | main_cid: cid, |
| 243 | 216 | own_pid, |
| 244 | 217 | border_width, |
@@ -259,7 +232,7 @@ impl BorderMap { |
| 259 | 232 | } |
| 260 | 233 | |
| 261 | 234 | fn is_overlay(&self, wid: u32) -> bool { |
| 262 | | - self.overlays.values().any(|o| o.wid == wid) |
| 235 | + self.overlays.values().any(|o| o.wid() == wid) |
| 263 | 236 | } |
| 264 | 237 | |
| 265 | 238 | /// Add border using the standard filtering path. |
@@ -337,65 +310,51 @@ impl BorderMap { |
| 337 | 310 | } |
| 338 | 311 | |
| 339 | 312 | let color = self.color_for(target_wid); |
| 340 | | - if let Some((cid, wid, bounds, scale)) = create_overlay( |
| 341 | | - self.main_cid, |
| 342 | | - target_wid, |
| 313 | + if let Some(window) = nswindow_overlay::OverlayWindow::new( |
| 314 | + bounds.origin.x, |
| 315 | + bounds.origin.y, |
| 316 | + bounds.size.width, |
| 317 | + bounds.size.height, |
| 343 | 318 | self.border_width, |
| 344 | 319 | self.radius, |
| 345 | 320 | color, |
| 321 | + self.mtm, |
| 346 | 322 | ) { |
| 347 | | - self.overlays.insert( |
| 348 | | - target_wid, |
| 349 | | - Overlay { |
| 350 | | - cid, |
| 351 | | - wid, |
| 352 | | - bounds, |
| 353 | | - scale, |
| 354 | | - }, |
| 355 | | - ); |
| 356 | | - } |
| 357 | | - } |
| 358 | | - |
| 359 | | - fn remove_all(&mut self) { |
| 360 | | - let wids: Vec<u32> = self.overlays.keys().copied().collect(); |
| 361 | | - for wid in wids { |
| 362 | | - self.remove(wid); |
| 323 | + window.order_above(target_wid); |
| 324 | + self.overlays.insert(target_wid, Overlay { window }); |
| 363 | 325 | } |
| 364 | 326 | } |
| 365 | 327 | |
| 366 | 328 | fn remove(&mut self, target_wid: u32) { |
| 367 | 329 | if let Some(overlay) = self.overlays.remove(&target_wid) { |
| 368 | | - unsafe { |
| 369 | | - // Move off-screen first (most reliable hide on Tahoe) |
| 370 | | - let offscreen = CGPoint { |
| 371 | | - x: -99999.0, |
| 372 | | - y: -99999.0, |
| 373 | | - }; |
| 374 | | - SLSMoveWindow(overlay.cid, overlay.wid, &offscreen); |
| 375 | | - SLSSetWindowAlpha(overlay.cid, overlay.wid, 0.0); |
| 376 | | - SLSOrderWindow(overlay.cid, overlay.wid, 0, 0); |
| 377 | | - SLSReleaseWindow(overlay.cid, overlay.wid); |
| 378 | | - if overlay.cid != self.main_cid { |
| 379 | | - SLSReleaseConnection(overlay.cid); |
| 380 | | - } |
| 381 | | - } |
| 330 | + debug!( |
| 331 | + "[remove] target={} overlay_wid={} dropping NSWindow", |
| 332 | + target_wid, |
| 333 | + overlay.wid() |
| 334 | + ); |
| 335 | + // OverlayWindow's Drop runs orderOut + close. |
| 336 | + drop(overlay); |
| 337 | + } else { |
| 338 | + debug!("[remove] target={} not tracked", target_wid); |
| 382 | 339 | } |
| 383 | 340 | } |
| 384 | 341 | |
| 385 | 342 | /// Reconcile a tracked overlay against its target window. |
| 386 | 343 | fn sync_overlay(&mut self, target_wid: u32) -> bool { |
| 387 | | - let Some((overlay_cid, overlay_wid, overlay_bounds, overlay_scale)) = self |
| 388 | | - .overlays |
| 389 | | - .get(&target_wid) |
| 390 | | - .map(|overlay| (overlay.cid, overlay.wid, overlay.bounds, overlay.scale)) |
| 391 | | - else { |
| 344 | + if !self.overlays.contains_key(&target_wid) { |
| 392 | 345 | return false; |
| 393 | | - }; |
| 346 | + } |
| 394 | 347 | |
| 348 | + let mut bounds = CGRect::default(); |
| 395 | 349 | unsafe { |
| 396 | | - let mut bounds = CGRect::default(); |
| 397 | 350 | if SLSGetWindowBounds(self.main_cid, target_wid, &mut bounds) != kCGErrorSuccess { |
| 398 | | - return false; |
| 351 | + // Window is gone (destroyed). Reap the overlay. |
| 352 | + debug!( |
| 353 | + "[sync_overlay] target={} SLSGetWindowBounds failed — reaping overlay", |
| 354 | + target_wid |
| 355 | + ); |
| 356 | + self.remove(target_wid); |
| 357 | + return true; |
| 399 | 358 | } |
| 400 | 359 | |
| 401 | 360 | if !is_suitable_window(self.main_cid, target_wid) { |
@@ -407,42 +366,40 @@ impl BorderMap { |
| 407 | 366 | self.remove(target_wid); |
| 408 | 367 | return true; |
| 409 | 368 | } |
| 369 | + } |
| 370 | + |
| 371 | + let active_only = self.active_only; |
| 372 | + let focused = self.focused_wid; |
| 410 | 373 | |
| 411 | | - let scale = display_scale_for_bounds(bounds); |
| 412 | | - if size_changed(overlay_bounds, bounds) || (scale - overlay_scale).abs() > SCALE_EPSILON |
| 413 | | - { |
| 374 | + if let Some(overlay) = self.overlays.get_mut(&target_wid) { |
| 375 | + let prev = overlay.bounds(); |
| 376 | + if size_changed(prev, bounds) || origin_changed(prev, bounds) { |
| 414 | 377 | debug!( |
| 415 | | - "[sync_overlay] target={} geometry changed bounds=({:.1},{:.1},{:.1},{:.1}) -> ({:.1},{:.1},{:.1},{:.1}) scale {:.2} -> {:.2}, recreating", |
| 378 | + "[sync_overlay] target={} geometry ({:.1},{:.1},{:.1},{:.1}) -> ({:.1},{:.1},{:.1},{:.1})", |
| 416 | 379 | target_wid, |
| 417 | | - overlay_bounds.origin.x, |
| 418 | | - overlay_bounds.origin.y, |
| 419 | | - overlay_bounds.size.width, |
| 420 | | - overlay_bounds.size.height, |
| 380 | + prev.origin.x, |
| 381 | + prev.origin.y, |
| 382 | + prev.size.width, |
| 383 | + prev.size.height, |
| 384 | + bounds.origin.x, |
| 385 | + bounds.origin.y, |
| 386 | + bounds.size.width, |
| 387 | + bounds.size.height |
| 388 | + ); |
| 389 | + overlay.window.set_bounds( |
| 421 | 390 | bounds.origin.x, |
| 422 | 391 | bounds.origin.y, |
| 423 | 392 | bounds.size.width, |
| 424 | 393 | bounds.size.height, |
| 425 | | - overlay_scale, |
| 426 | | - scale |
| 427 | 394 | ); |
| 428 | | - self.recreate(target_wid); |
| 429 | | - return true; |
| 430 | | - } |
| 431 | | - |
| 432 | | - if origin_changed(overlay_bounds, bounds) { |
| 433 | | - let bw = self.border_width; |
| 434 | | - let origin = CGPoint { |
| 435 | | - x: bounds.origin.x - bw, |
| 436 | | - y: bounds.origin.y - bw, |
| 437 | | - }; |
| 438 | | - SLSMoveWindow(overlay_cid, overlay_wid, &origin); |
| 439 | | - } |
| 440 | | - |
| 441 | | - if let Some(overlay) = self.overlays.get_mut(&target_wid) { |
| 442 | | - overlay.bounds = bounds; |
| 443 | | - overlay.scale = scale; |
| 444 | | - overlay.cid = overlay_cid; |
| 445 | | - overlay.wid = overlay_wid; |
| 395 | + // orderWindow:relativeTo: re-shows an off-screen window |
| 396 | + // as a side effect. In active_only mode, non-focused |
| 397 | + // overlays must remain hidden — otherwise stack peek |
| 398 | + // positions cause every stacked window's overlay to |
| 399 | + // pop onto the screen as their bounds shift. |
| 400 | + if !active_only || target_wid == focused { |
| 401 | + overlay.window.order_above(target_wid); |
| 402 | + } |
| 446 | 403 | } |
| 447 | 404 | } |
| 448 | 405 | |
@@ -460,33 +417,59 @@ impl BorderMap { |
| 460 | 417 | changed |
| 461 | 418 | } |
| 462 | 419 | |
| 463 | | - /// Recreate overlay at new size. |
| 464 | | - fn recreate(&mut self, target_wid: u32) { |
| 465 | | - if !self.overlays.contains_key(&target_wid) { |
| 466 | | - return; |
| 420 | + /// Re-apply each overlay's CAShapeLayer geometry. Called on a slow |
| 421 | + /// periodic schedule (and on hotplug) to repair layer state that |
| 422 | + /// macOS occasionally resets during display sleep/wake without |
| 423 | + /// changing the NSWindow's frame — sync_overlay won't fix it on |
| 424 | + /// its own because the SLS bounds match what we already stored. |
| 425 | + fn refresh_all_layers(&self) { |
| 426 | + for overlay in self.overlays.values() { |
| 427 | + overlay.window.reapply_layer(); |
| 467 | 428 | } |
| 468 | | - self.remove(target_wid); |
| 469 | | - self.add_fresh(target_wid); |
| 470 | | - if self.active_only && target_wid != self.focused_wid { |
| 471 | | - self.hide(target_wid); |
| 429 | + } |
| 430 | + |
| 431 | + /// Re-apply set_bounds for every tracked overlay even when the |
| 432 | + /// stored CG bounds match the current SLS bounds. After a display |
| 433 | + /// reconfiguration the cocoa frame depends on the (possibly new) |
| 434 | + /// primary screen height, so unchanged CG bounds still need their |
| 435 | + /// cocoa frame recomputed. |
| 436 | + fn reconcile_all_force(&mut self) { |
| 437 | + let tracked: Vec<u32> = self.overlays.keys().copied().collect(); |
| 438 | + let active_only = self.active_only; |
| 439 | + let focused = self.focused_wid; |
| 440 | + for wid in tracked { |
| 441 | + let mut bounds = CGRect::default(); |
| 442 | + unsafe { |
| 443 | + if SLSGetWindowBounds(self.main_cid, wid, &mut bounds) != kCGErrorSuccess { |
| 444 | + self.remove(wid); |
| 445 | + continue; |
| 446 | + } |
| 447 | + } |
| 448 | + if let Some(overlay) = self.overlays.get_mut(&wid) { |
| 449 | + overlay.window.set_bounds( |
| 450 | + bounds.origin.x, |
| 451 | + bounds.origin.y, |
| 452 | + bounds.size.width, |
| 453 | + bounds.size.height, |
| 454 | + ); |
| 455 | + if !active_only || wid == focused { |
| 456 | + overlay.window.order_above(wid); |
| 457 | + } |
| 458 | + } |
| 472 | 459 | } |
| 473 | | - self.subscribe_target(target_wid); |
| 474 | 460 | } |
| 475 | 461 | |
| 476 | 462 | fn hide(&self, target_wid: u32) { |
| 477 | 463 | if let Some(o) = self.overlays.get(&target_wid) { |
| 478 | | - unsafe { |
| 479 | | - SLSOrderWindow(o.cid, o.wid, 0, 0); |
| 480 | | - } |
| 464 | + debug!("[hide] target={} overlay_wid={}", target_wid, o.wid()); |
| 465 | + o.window.order_out(); |
| 481 | 466 | } |
| 482 | 467 | } |
| 483 | 468 | |
| 484 | 469 | fn unhide(&self, target_wid: u32) { |
| 485 | 470 | if let Some(o) = self.overlays.get(&target_wid) { |
| 486 | | - unsafe { |
| 487 | | - SLSSetWindowLevel(o.cid, o.wid, 0); |
| 488 | | - SLSOrderWindow(o.cid, o.wid, 1, target_wid); |
| 489 | | - } |
| 471 | + debug!("[unhide] target={} overlay_wid={}", target_wid, o.wid()); |
| 472 | + o.window.order_above(target_wid); |
| 490 | 473 | } |
| 491 | 474 | } |
| 492 | 475 | |
@@ -513,38 +496,63 @@ impl BorderMap { |
| 513 | 496 | /// Redraw an existing overlay with a new color (no destroy/recreate). |
| 514 | 497 | fn redraw(&self, target_wid: u32) { |
| 515 | 498 | if let Some(overlay) = self.overlays.get(&target_wid) { |
| 516 | | - unsafe { |
| 517 | | - let mut bounds = CGRect::default(); |
| 518 | | - if SLSGetWindowBounds(overlay.cid, target_wid, &mut bounds) != kCGErrorSuccess { |
| 519 | | - return; |
| 520 | | - } |
| 521 | | - let bw = self.border_width; |
| 522 | | - let ow = bounds.size.width + 2.0 * bw; |
| 523 | | - let oh = bounds.size.height + 2.0 * bw; |
| 524 | | - |
| 525 | | - let ctx = SLWindowContextCreate(overlay.cid, overlay.wid, ptr::null()); |
| 526 | | - if ctx.is_null() { |
| 527 | | - return; |
| 528 | | - } |
| 529 | | - |
| 530 | | - let color = self.color_for(target_wid); |
| 531 | | - draw_border(ctx, ow, oh, bw, self.radius, color); |
| 532 | | - SLSFlushWindowContentRegion(overlay.cid, overlay.wid, ptr::null()); |
| 533 | | - CGContextRelease(ctx); |
| 534 | | - } |
| 499 | + overlay.window.set_color(self.color_for(target_wid)); |
| 535 | 500 | } |
| 536 | 501 | } |
| 537 | 502 | |
| 538 | 503 | /// Detect focused window and update border colors if focus changed. |
| 539 | 504 | fn update_focus(&mut self) { |
| 540 | 505 | let front = get_front_window(self.own_pid); |
| 541 | | - if front == 0 || front == self.focused_wid { |
| 506 | + if front == 0 { |
| 507 | + return; |
| 508 | + } |
| 509 | + if front == self.focused_wid { |
| 510 | + // Same focus as last poll. But a freshly-spawned window may |
| 511 | + // have been focused before its SLS state was complete enough |
| 512 | + // to pass the add_fresh filter — retry on every poll until |
| 513 | + // it sticks. |
| 514 | + if !self.overlays.contains_key(&front) { |
| 515 | + self.add_fresh(front); |
| 516 | + if self.overlays.contains_key(&front) { |
| 517 | + debug!("[focus-retry] front={} now tracked", front); |
| 518 | + self.subscribe_target(front); |
| 519 | + if self.active_only { |
| 520 | + self.unhide(front); |
| 521 | + } |
| 522 | + } |
| 523 | + } |
| 542 | 524 | return; |
| 543 | 525 | } |
| 544 | 526 | |
| 545 | 527 | let old = self.focused_wid; |
| 546 | 528 | self.focused_wid = front; |
| 547 | | - debug!("[focus] {} -> {}", old, front); |
| 529 | + |
| 530 | + // tarmac-style workspace switching can swap focus to a window |
| 531 | + // that wasn't visible (and therefore not discovered) at ers |
| 532 | + // startup. Discover_windows only enumerates on-current-space |
| 533 | + // windows; tarmac stages other workspaces' windows in a hidden |
| 534 | + // state ers never picked up. If focus lands on such a wid, |
| 535 | + // create an overlay for it on demand. |
| 536 | + let new_target = !self.overlays.contains_key(&front); |
| 537 | + debug!( |
| 538 | + "[focus] {} -> {} {}(tracked targets: {:?})", |
| 539 | + old, |
| 540 | + front, |
| 541 | + if new_target { "[NEW] " } else { "" }, |
| 542 | + self.overlays.keys().collect::<Vec<_>>() |
| 543 | + ); |
| 544 | + if new_target { |
| 545 | + self.add_fresh(front); |
| 546 | + self.subscribe_target(front); |
| 547 | + } |
| 548 | + |
| 549 | + // Pull both overlays' positions to the targets' current SLS bounds |
| 550 | + // before un/hiding. AX-driven moves during a stack cycle frequently |
| 551 | + // don't fire SLS WINDOW_MOVE notifications, so a stored overlay |
| 552 | + // can be at stale coordinates. SLSGetWindowBounds (inside |
| 553 | + // sync_overlay) is real-time and doesn't wait for a notification. |
| 554 | + self.sync_overlay(old); |
| 555 | + self.sync_overlay(front); |
| 548 | 556 | |
| 549 | 557 | if self.active_only { |
| 550 | 558 | self.hide(old); |
@@ -580,14 +588,9 @@ impl BorderMap { |
| 580 | 588 | } |
| 581 | 589 | for (&target_wid, o) in &self.overlays { |
| 582 | 590 | if target_wid == self.focused_wid { |
| 583 | | - unsafe { |
| 584 | | - SLSSetWindowLevel(o.cid, o.wid, 0); |
| 585 | | - SLSOrderWindow(o.cid, o.wid, 1, target_wid); |
| 586 | | - } |
| 591 | + o.window.order_above(target_wid); |
| 587 | 592 | } else { |
| 588 | | - unsafe { |
| 589 | | - SLSOrderWindow(o.cid, o.wid, 0, 0); |
| 590 | | - } |
| 593 | + o.window.order_out(); |
| 591 | 594 | } |
| 592 | 595 | } |
| 593 | 596 | } |
@@ -750,10 +753,13 @@ fn print_help() { |
| 750 | 753 | } |
| 751 | 754 | |
| 752 | 755 | fn main() { |
| 756 | + let env_filter = tracing_subscriber::EnvFilter::try_from_default_env() |
| 757 | + .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("ers=info")); |
| 753 | 758 | tracing_subscriber::fmt() |
| 754 | | - .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) |
| 759 | + .with_env_filter(env_filter) |
| 755 | 760 | .with_writer(std::io::stderr) |
| 756 | 761 | .init(); |
| 762 | + debug!("[main] ers starting, pid={}", std::process::id()); |
| 757 | 763 | |
| 758 | 764 | let args: Vec<String> = std::env::args().collect(); |
| 759 | 765 | |
@@ -785,6 +791,13 @@ fn main() { |
| 785 | 791 | |
| 786 | 792 | let active_only = args.iter().any(|s| s == "--active-only"); |
| 787 | 793 | |
| 794 | + // Initialize NSApplication on the main thread before we touch any |
| 795 | + // AppKit APIs. NSWindow operations (used by nswindow_overlay) all |
| 796 | + // require a main-thread context. |
| 797 | + let mtm = nswindow_overlay::init_application(); |
| 798 | + nswindow_overlay::log_screens(mtm); |
| 799 | + register_display_hotplug_callback(); |
| 800 | + |
| 788 | 801 | let cid = unsafe { SLSMainConnectionID() }; |
| 789 | 802 | let own_pid = unsafe { |
| 790 | 803 | let mut pid: i32 = 0; |
@@ -799,7 +812,7 @@ fn main() { |
| 799 | 812 | setup_event_port(cid); |
| 800 | 813 | |
| 801 | 814 | // Discover and create borders |
| 802 | | - let mut borders = BorderMap::new(cid, own_pid, border_width); |
| 815 | + let mut borders = BorderMap::new(cid, own_pid, border_width, mtm); |
| 803 | 816 | borders.radius = radius; |
| 804 | 817 | borders.active_color = active_color; |
| 805 | 818 | borders.inactive_color = inactive_color; |
@@ -863,178 +876,306 @@ fn main() { |
| 863 | 876 | ); |
| 864 | 877 | } |
| 865 | 878 | |
| 866 | | - // Process events on background thread with coalescing |
| 867 | | - let running_bg = Arc::clone(&running); |
| 868 | | - let handle = std::thread::spawn(move || { |
| 869 | | - use std::collections::HashSet; |
| 870 | | - use std::time::{Duration, Instant}; |
| 871 | | - |
| 872 | | - // Persist across batches: windows we know about but haven't bordered yet. |
| 873 | | - // Value is the time the window was first seen — only promote after 100ms |
| 874 | | - // so tarmac has time to position them. |
| 875 | | - let mut pending: HashMap<u32, Instant> = HashMap::new(); |
| 876 | | - |
| 877 | | - while running_bg.load(Ordering::Relaxed) { |
| 878 | | - let first = match rx.recv_timeout(Duration::from_millis(100)) { |
| 879 | | - Ok(e) => e, |
| 880 | | - Err(mpsc::RecvTimeoutError::Timeout) => continue, |
| 881 | | - Err(mpsc::RecvTimeoutError::Disconnected) => break, |
| 882 | | - }; |
| 883 | | - |
| 884 | | - std::thread::sleep(std::time::Duration::from_millis(16)); |
| 885 | | - |
| 886 | | - let mut events = vec![first]; |
| 887 | | - while let Ok(e) = rx.try_recv() { |
| 888 | | - events.push(e); |
| 889 | | - } |
| 879 | + // Process events on the main thread via a CFRunLoopTimer. |
| 880 | + // BorderMap holds Retained<NSWindow> handles, which are |
| 881 | + // !Send/!Sync — AppKit calls must originate from the main thread. |
| 882 | + // Stash state in thread_local for the C callback to access. |
| 883 | + MAIN_STATE.with(|cell| { |
| 884 | + *cell.borrow_mut() = Some(MainState { |
| 885 | + borders, |
| 886 | + rx, |
| 887 | + pending: HashMap::new(), |
| 888 | + batch_events: Vec::new(), |
| 889 | + batch_first_seen: None, |
| 890 | + }); |
| 891 | + }); |
| 890 | 892 | |
| 891 | | - let mut moved: HashSet<u32> = HashSet::new(); |
| 892 | | - let mut resized: HashSet<u32> = HashSet::new(); |
| 893 | | - let mut destroyed: HashSet<u32> = HashSet::new(); |
| 894 | | - let mut needs_resubscribe = false; |
| 895 | | - |
| 896 | | - for event in events { |
| 897 | | - match event { |
| 898 | | - Event::Move(wid) => { |
| 899 | | - if !borders.is_overlay(wid) { |
| 900 | | - moved.insert(wid); |
| 901 | | - } |
| 902 | | - } |
| 903 | | - Event::Resize(wid) => { |
| 904 | | - if !borders.is_overlay(wid) { |
| 905 | | - resized.insert(wid); |
| 906 | | - } |
| 907 | | - } |
| 908 | | - Event::Close(wid) | Event::Destroy(wid) => { |
| 909 | | - if !borders.is_overlay(wid) { |
| 910 | | - destroyed.insert(wid); |
| 911 | | - pending.remove(&wid); |
| 912 | | - } |
| 913 | | - } |
| 914 | | - Event::Create(wid) => { |
| 915 | | - if !borders.is_overlay(wid) { |
| 916 | | - pending.entry(wid).or_insert_with(Instant::now); |
| 917 | | - borders.subscribe_target(wid); |
| 918 | | - } |
| 919 | | - } |
| 920 | | - Event::Hide(wid) => borders.hide(wid), |
| 921 | | - Event::Unhide(wid) => { |
| 922 | | - if !borders.active_only || wid == borders.focused_wid { |
| 923 | | - borders.unhide(wid); |
| 924 | | - } |
| 925 | | - } |
| 926 | | - Event::FrontChange => { |
| 927 | | - needs_resubscribe = true; |
| 928 | | - } |
| 929 | | - Event::SpaceChange => { |
| 930 | | - needs_resubscribe = true; |
| 893 | + unsafe { |
| 894 | + let mut ctx = CFRunLoopTimerContext { |
| 895 | + version: 0, |
| 896 | + info: ptr::null_mut(), |
| 897 | + retain: None, |
| 898 | + release: None, |
| 899 | + copy_description: None, |
| 900 | + }; |
| 901 | + let timer = CFRunLoopTimerCreate( |
| 902 | + ptr::null(), |
| 903 | + CFAbsoluteTimeGetCurrent() + 0.05, |
| 904 | + 0.016, |
| 905 | + 0u64, |
| 906 | + 0i64, |
| 907 | + timer_callback, |
| 908 | + &mut ctx, |
| 909 | + ); |
| 910 | + CFRunLoopAddTimer(CFRunLoopGetMain(), timer, kCFRunLoopDefaultMode); |
| 911 | + } |
| 912 | + |
| 913 | + unsafe { CFRunLoopRun() }; |
| 914 | + |
| 915 | + // Drop everything on the main thread (NSWindow.close in Drop). |
| 916 | + MAIN_STATE.with(|cell| cell.borrow_mut().take()); |
| 917 | + |
| 918 | + SIGNAL_STOP_REQUESTED.store(true, Ordering::Relaxed); |
| 919 | + let _ = signal_watcher.join(); |
| 920 | + drop(running); |
| 921 | +} |
| 922 | + |
| 923 | +struct MainState { |
| 924 | + borders: BorderMap, |
| 925 | + rx: mpsc::Receiver<Event>, |
| 926 | + pending: HashMap<u32, std::time::Instant>, |
| 927 | + batch_events: Vec<Event>, |
| 928 | + batch_first_seen: Option<std::time::Instant>, |
| 929 | +} |
| 930 | + |
| 931 | +thread_local! { |
| 932 | + static MAIN_STATE: std::cell::RefCell<Option<MainState>> = const { std::cell::RefCell::new(None) }; |
| 933 | +} |
| 934 | + |
| 935 | +extern "C" fn timer_callback(_timer: *mut std::ffi::c_void, _info: *mut std::ffi::c_void) { |
| 936 | + use std::time::{Duration, Instant}; |
| 937 | + use std::sync::atomic::AtomicUsize; |
| 938 | + static TICK_COUNT: AtomicUsize = AtomicUsize::new(0); |
| 939 | + let tick = TICK_COUNT.fetch_add(1, Ordering::Relaxed); |
| 940 | + if tick == 0 { |
| 941 | + debug!("[timer] first fire — main-thread event loop is alive"); |
| 942 | + } else if tick % 600 == 0 { |
| 943 | + // every ~10s if interval is 16ms |
| 944 | + debug!("[timer] tick {}", tick); |
| 945 | + } |
| 946 | + MAIN_STATE.with(|cell| { |
| 947 | + let mut state_opt = cell.borrow_mut(); |
| 948 | + let s = match state_opt.as_mut() { |
| 949 | + Some(s) => s, |
| 950 | + None => return, |
| 951 | + }; |
| 952 | + let mut received = 0usize; |
| 953 | + loop { |
| 954 | + match s.rx.try_recv() { |
| 955 | + Ok(e) => { |
| 956 | + if s.batch_events.is_empty() { |
| 957 | + s.batch_first_seen = Some(Instant::now()); |
| 931 | 958 | } |
| 959 | + s.batch_events.push(e); |
| 960 | + received += 1; |
| 932 | 961 | } |
| 962 | + Err(mpsc::TryRecvError::Empty) => break, |
| 963 | + Err(mpsc::TryRecvError::Disconnected) => break, |
| 933 | 964 | } |
| 934 | | - |
| 935 | | - // Destroys |
| 936 | | - for wid in &destroyed { |
| 937 | | - borders.remove(*wid); |
| 965 | + } |
| 966 | + if received > 0 { |
| 967 | + debug!( |
| 968 | + "[timer] received {} new events; batch size now {}", |
| 969 | + received, |
| 970 | + s.batch_events.len() |
| 971 | + ); |
| 972 | + } |
| 973 | + // Process the accumulated batch after a 16ms quiet window |
| 974 | + // (matches the old bg-thread behavior where it slept 16ms after |
| 975 | + // the first event then drained). Events keep arriving, the batch |
| 976 | + // grows; once 16ms passes without new events we flush. |
| 977 | + let should_flush = s.batch_first_seen.is_some_and(|t| { |
| 978 | + t.elapsed() >= Duration::from_millis(16) && received == 0 |
| 979 | + }) || s |
| 980 | + .batch_first_seen |
| 981 | + .is_some_and(|t| t.elapsed() >= Duration::from_millis(120)); |
| 982 | + if should_flush { |
| 983 | + let events = std::mem::take(&mut s.batch_events); |
| 984 | + s.batch_first_seen = None; |
| 985 | + debug!("[timer] processing batch of {}", events.len()); |
| 986 | + process_event_batch(&mut s.borders, &mut s.pending, events); |
| 987 | + } else { |
| 988 | + // Even with no events, poll focus periodically so a missed |
| 989 | + // FrontChange notification doesn't strand the active border. |
| 990 | + // Cheap operation when focus hasn't changed. |
| 991 | + s.borders.update_focus(); |
| 992 | + // Once per second, reconcile tracked overlays against |
| 993 | + // current SLS state. Catches missed Close/Destroy events |
| 994 | + // that would otherwise leave a dead border on screen. |
| 995 | + if tick % 60 == 0 && tick > 0 { |
| 996 | + let removed = s.borders.reconcile_tracked(); |
| 997 | + if removed { |
| 998 | + debug!("[timer] periodic reconcile removed stale overlays"); |
| 999 | + } |
| 1000 | + // Cheap: re-applies just the CAShapeLayer frame/path |
| 1001 | + // for every overlay. Recovers from layer state that |
| 1002 | + // macOS resets during display sleep/wake without |
| 1003 | + // touching the NSWindow frame. |
| 1004 | + s.borders.refresh_all_layers(); |
| 938 | 1005 | } |
| 1006 | + } |
| 1007 | + }); |
| 1008 | +} |
| 939 | 1009 | |
| 940 | | - // Promote pending creates that have waited ≥100ms (tarmac positioning time) |
| 941 | | - let now = Instant::now(); |
| 942 | | - let ready: Vec<u32> = pending |
| 943 | | - .iter() |
| 944 | | - .filter(|(wid, seen_at)| { |
| 945 | | - !destroyed.contains(wid) |
| 946 | | - && now.duration_since(**seen_at) >= Duration::from_millis(100) |
| 947 | | - }) |
| 948 | | - .map(|(wid, _)| *wid) |
| 949 | | - .collect(); |
| 950 | | - // Filter overlapping creates: if two windows overlap, keep smaller one |
| 951 | | - let mut bounds_map: Vec<(u32, CGRect)> = Vec::new(); |
| 952 | | - for &wid in &ready { |
| 953 | | - unsafe { |
| 954 | | - let mut b = CGRect::default(); |
| 955 | | - SLSGetWindowBounds(borders.main_cid, wid, &mut b); |
| 956 | | - bounds_map.push((wid, b)); |
| 1010 | +fn process_event_batch( |
| 1011 | + borders: &mut BorderMap, |
| 1012 | + pending: &mut HashMap<u32, std::time::Instant>, |
| 1013 | + events: Vec<Event>, |
| 1014 | +) { |
| 1015 | + use std::collections::HashSet; |
| 1016 | + use std::time::{Duration, Instant}; |
| 1017 | + |
| 1018 | + let mut moved: HashSet<u32> = HashSet::new(); |
| 1019 | + let mut resized: HashSet<u32> = HashSet::new(); |
| 1020 | + let mut destroyed: HashSet<u32> = HashSet::new(); |
| 1021 | + let mut needs_resubscribe = false; |
| 1022 | + |
| 1023 | + for event in events { |
| 1024 | + match event { |
| 1025 | + Event::Move(wid) => { |
| 1026 | + if !borders.is_overlay(wid) { |
| 1027 | + moved.insert(wid); |
| 957 | 1028 | } |
| 958 | 1029 | } |
| 959 | | - |
| 960 | | - // If two new windows overlap closely, skip the larger one (container) |
| 961 | | - let mut skip: std::collections::HashSet<u32> = HashSet::new(); |
| 962 | | - for i in 0..bounds_map.len() { |
| 963 | | - for j in (i + 1)..bounds_map.len() { |
| 964 | | - let (wid_a, a) = &bounds_map[i]; |
| 965 | | - let (wid_b, b) = &bounds_map[j]; |
| 966 | | - if let Some(preference) = surface_preference(*a, *b) { |
| 967 | | - match preference { |
| 968 | | - SurfacePreference::KeepExisting => { |
| 969 | | - skip.insert(*wid_b); |
| 970 | | - } |
| 971 | | - SurfacePreference::ReplaceExisting => { |
| 972 | | - skip.insert(*wid_a); |
| 973 | | - } |
| 974 | | - } |
| 975 | | - } |
| 1030 | + Event::Resize(wid) => { |
| 1031 | + if !borders.is_overlay(wid) { |
| 1032 | + resized.insert(wid); |
| 976 | 1033 | } |
| 977 | 1034 | } |
| 978 | | - |
| 979 | | - for &wid in &ready { |
| 980 | | - pending.remove(&wid); |
| 981 | | - if !skip.contains(&wid) { |
| 982 | | - borders.add_fresh(wid); |
| 983 | | - if borders.active_only && wid != borders.focused_wid { |
| 984 | | - borders.hide(wid); |
| 985 | | - } |
| 986 | | - needs_resubscribe = true; |
| 1035 | + Event::Close(wid) | Event::Destroy(wid) => { |
| 1036 | + if !borders.is_overlay(wid) { |
| 1037 | + debug!("[event] Close/Destroy target_wid={}", wid); |
| 1038 | + destroyed.insert(wid); |
| 1039 | + pending.remove(&wid); |
| 987 | 1040 | } |
| 988 | 1041 | } |
| 989 | | - |
| 990 | | - // Moves: reposition overlay (no destroy/create) |
| 991 | | - for wid in &moved { |
| 992 | | - if !resized.contains(wid) && !ready.contains(wid) && borders.sync_overlay(*wid) { |
| 993 | | - needs_resubscribe = true; |
| 1042 | + Event::Create(wid) => { |
| 1043 | + if !borders.is_overlay(wid) { |
| 1044 | + pending.entry(wid).or_insert_with(Instant::now); |
| 1045 | + borders.subscribe_target(wid); |
| 994 | 1046 | } |
| 995 | 1047 | } |
| 996 | | - |
| 997 | | - // Resizes: must recreate (can't reshape windows on Tahoe) |
| 998 | | - // Skip windows just created this batch — already at correct size |
| 999 | | - for wid in &resized { |
| 1000 | | - if !ready.contains(wid) |
| 1001 | | - && borders.overlays.contains_key(wid) |
| 1002 | | - && borders.sync_overlay(*wid) |
| 1003 | | - { |
| 1004 | | - needs_resubscribe = true; |
| 1048 | + Event::Hide(wid) => borders.hide(wid), |
| 1049 | + Event::Unhide(wid) => { |
| 1050 | + if !borders.is_overlay(wid) { |
| 1051 | + if !borders.overlays.contains_key(&wid) { |
| 1052 | + borders.add_fresh(wid); |
| 1053 | + borders.subscribe_target(wid); |
| 1054 | + } |
| 1055 | + if !borders.active_only || wid == borders.focused_wid { |
| 1056 | + borders.unhide(wid); |
| 1057 | + } |
| 1005 | 1058 | } |
| 1006 | 1059 | } |
| 1007 | | - |
| 1008 | | - // On space change, discover windows we haven't seen yet |
| 1009 | | - if needs_resubscribe { |
| 1010 | | - borders.discover_untracked(); |
| 1060 | + Event::FrontChange => { |
| 1061 | + needs_resubscribe = true; |
| 1062 | + } |
| 1063 | + Event::SpaceChange => { |
| 1064 | + needs_resubscribe = true; |
| 1011 | 1065 | } |
| 1066 | + } |
| 1067 | + } |
| 1012 | 1068 | |
| 1013 | | - needs_resubscribe |= borders.reconcile_tracked(); |
| 1069 | + for wid in &destroyed { |
| 1070 | + borders.remove(*wid); |
| 1071 | + } |
| 1014 | 1072 | |
| 1015 | | - // Update focus (redraws borders in-place if changed) |
| 1016 | | - borders.update_focus(); |
| 1073 | + let now = Instant::now(); |
| 1074 | + let ready: Vec<u32> = pending |
| 1075 | + .iter() |
| 1076 | + .filter(|(wid, seen_at)| { |
| 1077 | + !destroyed.contains(wid) && now.duration_since(**seen_at) >= Duration::from_millis(100) |
| 1078 | + }) |
| 1079 | + .map(|(wid, _)| *wid) |
| 1080 | + .collect(); |
| 1081 | + |
| 1082 | + let mut bounds_map: Vec<(u32, CGRect)> = Vec::new(); |
| 1083 | + for &wid in &ready { |
| 1084 | + unsafe { |
| 1085 | + let mut b = CGRect::default(); |
| 1086 | + SLSGetWindowBounds(borders.main_cid, wid, &mut b); |
| 1087 | + bounds_map.push((wid, b)); |
| 1088 | + } |
| 1089 | + } |
| 1017 | 1090 | |
| 1018 | | - // Re-subscribe ALL tracked windows (SLSRequestNotificationsForWindows replaces, not appends) |
| 1019 | | - if needs_resubscribe || !destroyed.is_empty() { |
| 1020 | | - borders.subscribe_all(); |
| 1091 | + let mut skip: std::collections::HashSet<u32> = HashSet::new(); |
| 1092 | + for i in 0..bounds_map.len() { |
| 1093 | + for j in (i + 1)..bounds_map.len() { |
| 1094 | + let (wid_a, a) = &bounds_map[i]; |
| 1095 | + let (wid_b, b) = &bounds_map[j]; |
| 1096 | + if let Some(preference) = surface_preference(*a, *b) { |
| 1097 | + match preference { |
| 1098 | + SurfacePreference::KeepExisting => { |
| 1099 | + skip.insert(*wid_b); |
| 1100 | + } |
| 1101 | + SurfacePreference::ReplaceExisting => { |
| 1102 | + skip.insert(*wid_a); |
| 1103 | + } |
| 1104 | + } |
| 1021 | 1105 | } |
| 1106 | + } |
| 1107 | + } |
| 1022 | 1108 | |
| 1023 | | - // After all processing, enforce active-only visibility |
| 1024 | | - borders.enforce_active_only(); |
| 1109 | + for &wid in &ready { |
| 1110 | + pending.remove(&wid); |
| 1111 | + if !skip.contains(&wid) { |
| 1112 | + borders.add_fresh(wid); |
| 1113 | + if borders.active_only && wid != borders.focused_wid { |
| 1114 | + borders.hide(wid); |
| 1115 | + } |
| 1116 | + needs_resubscribe = true; |
| 1025 | 1117 | } |
| 1118 | + } |
| 1026 | 1119 | |
| 1027 | | - // Clean up all overlays before exiting |
| 1028 | | - borders.remove_all(); |
| 1029 | | - }); |
| 1120 | + for wid in &moved { |
| 1121 | + if !resized.contains(wid) && !ready.contains(wid) && borders.sync_overlay(*wid) { |
| 1122 | + needs_resubscribe = true; |
| 1123 | + } |
| 1124 | + } |
| 1030 | 1125 | |
| 1031 | | - unsafe { CFRunLoopRun() }; |
| 1126 | + for wid in &resized { |
| 1127 | + if !ready.contains(wid) |
| 1128 | + && borders.overlays.contains_key(wid) |
| 1129 | + && borders.sync_overlay(*wid) |
| 1130 | + { |
| 1131 | + needs_resubscribe = true; |
| 1132 | + } |
| 1133 | + } |
| 1032 | 1134 | |
| 1033 | | - // SIGINT received — signal background thread to stop and wait |
| 1034 | | - running.store(false, Ordering::Relaxed); |
| 1035 | | - SIGNAL_STOP_REQUESTED.store(true, Ordering::Relaxed); |
| 1036 | | - let _ = signal_watcher.join(); |
| 1037 | | - let _ = handle.join(); |
| 1135 | + if needs_resubscribe { |
| 1136 | + borders.discover_untracked(); |
| 1137 | + } |
| 1138 | + |
| 1139 | + needs_resubscribe |= borders.reconcile_tracked(); |
| 1140 | + |
| 1141 | + borders.update_focus(); |
| 1142 | + |
| 1143 | + if needs_resubscribe || !destroyed.is_empty() { |
| 1144 | + borders.subscribe_all(); |
| 1145 | + } |
| 1146 | + |
| 1147 | + borders.enforce_active_only(); |
| 1148 | +} |
| 1149 | + |
| 1150 | +/// Re-log the screen layout when the display configuration changes |
| 1151 | +/// (monitor plug/unplug, resolution change). The callback also nudges |
| 1152 | +/// every tracked overlay to re-fetch its bounds so any cached cocoa Y |
| 1153 | +/// computed against the old primary height gets refreshed. |
| 1154 | +unsafe extern "C" fn display_reconfig_callback( |
| 1155 | + display_id: u32, |
| 1156 | + flags: u32, |
| 1157 | + _user_info: *mut std::ffi::c_void, |
| 1158 | +) { |
| 1159 | + debug!(display_id, flags, "[hotplug] CGDisplay reconfiguration"); |
| 1160 | + if let Some(mtm) = objc2::MainThreadMarker::new() { |
| 1161 | + nswindow_overlay::log_screens(mtm); |
| 1162 | + } |
| 1163 | + MAIN_STATE.with(|cell| { |
| 1164 | + if let Some(s) = cell.borrow_mut().as_mut() { |
| 1165 | + s.borders.reconcile_all_force(); |
| 1166 | + s.borders.refresh_all_layers(); |
| 1167 | + } |
| 1168 | + }); |
| 1169 | +} |
| 1170 | + |
| 1171 | +fn register_display_hotplug_callback() { |
| 1172 | + unsafe { |
| 1173 | + let rc = CGDisplayRegisterReconfigurationCallback( |
| 1174 | + Some(display_reconfig_callback), |
| 1175 | + std::ptr::null_mut(), |
| 1176 | + ); |
| 1177 | + debug!("[hotplug] register CGDisplayReconfiguration rc={}", rc); |
| 1178 | + } |
| 1038 | 1179 | } |
| 1039 | 1180 | |
| 1040 | 1181 | fn setup_event_port(cid: CGSConnectionID) { |
@@ -1138,107 +1279,6 @@ fn discover_windows(cid: CGSConnectionID, own_pid: i32) -> Vec<u32> { |
| 1138 | 1279 | } |
| 1139 | 1280 | } |
| 1140 | 1281 | |
| 1141 | | -/// Draw a border ring into an existing CGContext, clearing first. |
| 1142 | | -fn draw_border( |
| 1143 | | - ctx: CGContextRef, |
| 1144 | | - width: f64, |
| 1145 | | - height: f64, |
| 1146 | | - border_width: f64, |
| 1147 | | - radius: f64, |
| 1148 | | - color: (f64, f64, f64, f64), |
| 1149 | | -) { |
| 1150 | | - unsafe { |
| 1151 | | - let full = CGRect::new(0.0, 0.0, width, height); |
| 1152 | | - CGContextClearRect(ctx, full); |
| 1153 | | - |
| 1154 | | - let bw = border_width; |
| 1155 | | - let stroke_rect = CGRect::new(bw / 2.0, bw / 2.0, width - bw, height - bw); |
| 1156 | | - let max_r = (stroke_rect.size.width.min(stroke_rect.size.height) / 2.0).max(0.0); |
| 1157 | | - let r = radius.min(max_r); |
| 1158 | | - |
| 1159 | | - CGContextSetRGBStrokeColor(ctx, color.0, color.1, color.2, color.3); |
| 1160 | | - CGContextSetLineWidth(ctx, bw); |
| 1161 | | - let path = CGPathCreateWithRoundedRect(stroke_rect, r, r, ptr::null()); |
| 1162 | | - if !path.is_null() { |
| 1163 | | - CGContextAddPath(ctx, path); |
| 1164 | | - CGContextStrokePath(ctx); |
| 1165 | | - CGPathRelease(path); |
| 1166 | | - } |
| 1167 | | - CGContextFlush(ctx); |
| 1168 | | - } |
| 1169 | | -} |
| 1170 | | - |
| 1171 | | -fn create_overlay( |
| 1172 | | - cid: CGSConnectionID, |
| 1173 | | - target_wid: u32, |
| 1174 | | - border_width: f64, |
| 1175 | | - radius: f64, |
| 1176 | | - color: (f64, f64, f64, f64), |
| 1177 | | -) -> Option<(CGSConnectionID, u32, CGRect, f64)> { |
| 1178 | | - unsafe { |
| 1179 | | - let mut bounds = CGRect::default(); |
| 1180 | | - let rc = SLSGetWindowBounds(cid, target_wid, &mut bounds); |
| 1181 | | - if rc != kCGErrorSuccess { |
| 1182 | | - debug!("[create_overlay] SLSGetWindowBounds failed for wid={target_wid} rc={rc}"); |
| 1183 | | - return None; |
| 1184 | | - } |
| 1185 | | - if !is_trackable_window(bounds, border_width) { |
| 1186 | | - debug!( |
| 1187 | | - "[create_overlay] wid={target_wid} too small: {}x{}", |
| 1188 | | - bounds.size.width, bounds.size.height |
| 1189 | | - ); |
| 1190 | | - return None; |
| 1191 | | - } |
| 1192 | | - |
| 1193 | | - let bw = border_width; |
| 1194 | | - let ow = bounds.size.width + 2.0 * bw; |
| 1195 | | - let oh = bounds.size.height + 2.0 * bw; |
| 1196 | | - let ox = bounds.origin.x - bw; |
| 1197 | | - let oy = bounds.origin.y - bw; |
| 1198 | | - let scale = display_scale_for_bounds(bounds); |
| 1199 | | - |
| 1200 | | - let frame = CGRect::new(0.0, 0.0, ow, oh); |
| 1201 | | - let mut region: CFTypeRef = ptr::null(); |
| 1202 | | - CGSNewRegionWithRect(&frame, &mut region); |
| 1203 | | - if region.is_null() { |
| 1204 | | - debug!("[create_overlay] CGSNewRegionWithRect failed for wid={target_wid}"); |
| 1205 | | - return None; |
| 1206 | | - } |
| 1207 | | - |
| 1208 | | - let mut wid: u32 = 0; |
| 1209 | | - SLSNewWindow(cid, 2, ox as f32, oy as f32, region, &mut wid); |
| 1210 | | - CFRelease(region); |
| 1211 | | - if wid == 0 { |
| 1212 | | - debug!("[create_overlay] SLSNewWindow returned 0 for target={target_wid} cid={cid}"); |
| 1213 | | - return None; |
| 1214 | | - } |
| 1215 | | - |
| 1216 | | - debug!( |
| 1217 | | - "[create_overlay] created overlay wid={wid} for target={target_wid} scale={scale:.2} color=({:.2},{:.2},{:.2},{:.2})", |
| 1218 | | - color.0, color.1, color.2, color.3 |
| 1219 | | - ); |
| 1220 | | - |
| 1221 | | - SLSSetWindowResolution(cid, wid, scale); |
| 1222 | | - SLSSetWindowOpacity(cid, wid, false); |
| 1223 | | - SLSSetWindowLevel(cid, wid, 0); |
| 1224 | | - SLSOrderWindow(cid, wid, 1, target_wid); |
| 1225 | | - |
| 1226 | | - // Draw border (point coordinates) |
| 1227 | | - let ctx = SLWindowContextCreate(cid, wid, ptr::null()); |
| 1228 | | - if ctx.is_null() { |
| 1229 | | - debug!("[create_overlay] SLWindowContextCreate returned null for overlay wid={wid}"); |
| 1230 | | - SLSReleaseWindow(cid, wid); |
| 1231 | | - return None; |
| 1232 | | - } |
| 1233 | | - |
| 1234 | | - draw_border(ctx, ow, oh, bw, radius, color); |
| 1235 | | - SLSFlushWindowContentRegion(cid, wid, ptr::null()); |
| 1236 | | - CGContextRelease(ctx); |
| 1237 | | - |
| 1238 | | - Some((cid, wid, bounds, scale)) |
| 1239 | | - } |
| 1240 | | -} |
| 1241 | | - |
| 1242 | 1282 | fn list_windows() { |
| 1243 | 1283 | let cid = unsafe { SLSMainConnectionID() }; |
| 1244 | 1284 | unsafe { |