import CoreGraphics import Foundation /// One on-screen window as the capture resolver sees it. Kept as plain data so the /// choice logic is testable without a window server. public struct WindowCandidate: Sendable, Equatable { public let windowID: UInt32 public let ownerBundleID: String? public let ownerPID: Int32 public let frame: CGRect public let layer: Int public init( windowID: UInt32, ownerBundleID: String?, ownerPID: Int32, frame: CGRect, layer: Int ) { self.windowID = windowID self.ownerBundleID = ownerBundleID self.ownerPID = ownerPID self.frame = frame self.layer = layer } } /// What a capture will take: one region of a display, one whole display, or one window. public enum CaptureTarget: Sendable, Equatable { case region(CaptureRegion) case display(displayID: UInt32) case window(windowID: UInt32, frame: CGRect, displayID: UInt32) } /// Chooses the capture target for a scope from the windows that are on screen. /// /// Candidates arrive front-to-back (the order the window server reports), which is what /// makes "the front window" well defined. Redline's own windows are never candidates, /// so choosing Window never captures the app's own panel. public enum CaptureTargetResolver { /// Bundles whose front window counts as a browser page. public static let browserBundleIDs: Set = [ "com.apple.Safari", "com.google.Chrome", "com.google.Chrome.canary", "com.microsoft.edgemac", "com.brave.Browser", "company.thebrowser.Browser", "org.mozilla.firefox", "com.vivaldi.Vivaldi", "com.operasoftware.Opera", ] /// Redline's own bundle identifier — never a capture target. public static let ownBundleID = "ai.flowmaster.shotdeck" public static func resolve( scope: CaptureScope, savedArea: CaptureRegion?, frontmostPID: Int32, candidates: [WindowCandidate] ) throws -> CaptureTarget { let usable = candidates.filter { candidate in candidate.layer == 0 && candidate.ownerBundleID != ownBundleID && candidate.frame.width >= 40 && candidate.frame.height >= 40 } switch scope { case .area: guard let savedArea else { throw ShotdeckError.noRegionRemembered } return .region(savedArea) case .screen: if let savedArea { return .display(displayID: savedArea.displayID) } guard let window = usable.first else { throw ShotdeckError.noFrontWindow } return .display(displayID: displayID(containing: window.frame)) case .window: let ownAppFirst = usable.first { $0.ownerPID == frontmostPID } guard let window = ownAppFirst ?? usable.first else { throw ShotdeckError.noFrontWindow } return .window( windowID: window.windowID, frame: window.frame, displayID: displayID(containing: window.frame) ) case .browser: guard let window = usable.first(where: { guard let bundle = $0.ownerBundleID else { return false } return browserBundleIDs.contains(bundle) }) else { throw ShotdeckError.noBrowserWindow } return .window( windowID: window.windowID, frame: window.frame, displayID: displayID(containing: window.frame) ) } } /// The display whose bounds contain the window's centre; the main display when a /// window straddles two screens and belongs to neither cleanly. public static func displayID(containing frame: CGRect) -> UInt32 { let centre = CGPoint(x: frame.midX, y: frame.midY) var count: UInt32 = 0 guard CGGetActiveDisplayList(0, nil, &count) == .success, count > 0 else { return CGMainDisplayID() } var displays = [CGDirectDisplayID](repeating: 0, count: Int(count)) guard CGGetActiveDisplayList(count, &displays, &count) == .success else { return CGMainDisplayID() } for display in displays.prefix(Int(count)) where CGDisplayBounds(display).contains(centre) { return display } return CGMainDisplayID() } }