EPOD Window Bar: a window strip above the macOS Dock
EPOD Window Bar is a macOS agent app. It shows a thin strip above the Dock with one tab per window of the frontmost application. One click raises the window. Build from source with XcodeGen and Xcode. Signing uses Config.xcconfig (ad-hoc by default; a local Local.xcconfig can set a team ID). The app needs Accessibility and Screen Recording permissions, and runs in a reduced mode without Screen Recording. See README.md and docs/ for details.
This commit is contained in:
145
Sources/Private/PrivateAPI.swift
Normal file
145
Sources/Private/PrivateAPI.swift
Normal file
@@ -0,0 +1,145 @@
|
||||
import Foundation
|
||||
import ApplicationServices
|
||||
import CoreGraphics
|
||||
|
||||
// PrivateAPI loads all private symbols at runtime with dlsym. If a symbol is
|
||||
// missing, the loader logs the problem and the feature is disabled. The app
|
||||
// never crashes because of a missing private symbol (spec section 7.5).
|
||||
//
|
||||
// Signature sources:
|
||||
// - _AXUIElementGetWindow: AltTab (PrivateApis.swift), yabai.
|
||||
// - CGSMainConnectionID: common CGS use (yabai, Hammerspoon).
|
||||
// - CGSCopyManagedDisplaySpaces: yabai, AltTab.
|
||||
// - CGSCopySpacesForWindows: AltTab, yabai.
|
||||
// - _AXUIElementCreateWithRemoteToken: AltTab.
|
||||
//
|
||||
// UNCERTAIN: The remote-token layout for _AXUIElementCreateWithRemoteToken is
|
||||
// not documented. The layout below follows AltTab. The constraint and the
|
||||
// cross-Space activation have a fallback if this call returns nil.
|
||||
//
|
||||
// PrivateAPI is nonisolated. The stored function pointers are immutable (let).
|
||||
// The caller runs the AX wrappers on the serial AX queue (spec sections 7.4,
|
||||
// 7.15). The CGS wrappers are also safe off the main thread.
|
||||
final class PrivateAPI {
|
||||
|
||||
static let shared = PrivateAPI()
|
||||
|
||||
// Function pointer typealiases (C calling convention).
|
||||
private typealias GetWindowFn =
|
||||
@convention(c) (AXUIElement, UnsafeMutablePointer<CGWindowID>) -> AXError
|
||||
private typealias MainConnFn =
|
||||
@convention(c) () -> Int32
|
||||
private typealias CopyManagedDisplaySpacesFn =
|
||||
@convention(c) (Int32) -> Unmanaged<CFArray>?
|
||||
private typealias CopySpacesForWindowsFn =
|
||||
@convention(c) (Int32, Int32, CFArray) -> Unmanaged<CFArray>?
|
||||
private typealias CreateWithRemoteTokenFn =
|
||||
@convention(c) (CFData) -> Unmanaged<AXUIElement>?
|
||||
|
||||
private let getWindowFn: GetWindowFn?
|
||||
private let mainConnFn: MainConnFn?
|
||||
private let copyManagedDisplaySpacesFn: CopyManagedDisplaySpacesFn?
|
||||
private let copySpacesForWindowsFn: CopySpacesForWindowsFn?
|
||||
private let createWithRemoteTokenFn: CreateWithRemoteTokenFn?
|
||||
|
||||
// spacesAvailable is true if the Space symbols loaded.
|
||||
let spacesAvailable: Bool
|
||||
|
||||
private init() {
|
||||
// SkyLight holds the CGS symbols on current macOS. The AX symbols live
|
||||
// in ApplicationServices (already loaded). RTLD_DEFAULT finds a symbol
|
||||
// in any loaded image. The loader opens SkyLight as a fallback.
|
||||
let skyLight = dlopen(
|
||||
"/System/Library/PrivateFrameworks/SkyLight.framework/SkyLight",
|
||||
RTLD_NOW)
|
||||
|
||||
func load<T>(_ name: String, _ type: T.Type) -> T? {
|
||||
// PRIVATE API
|
||||
var pointer = dlsym(UnsafeMutableRawPointer(bitPattern: -2), name) // RTLD_DEFAULT
|
||||
if pointer == nil, let skyLight = skyLight {
|
||||
pointer = dlsym(skyLight, name)
|
||||
}
|
||||
guard let pointer = pointer else {
|
||||
Log.privateAPI.error("Private symbol missing: \(name, privacy: .public)")
|
||||
return nil
|
||||
}
|
||||
return unsafeBitCast(pointer, to: T.self)
|
||||
}
|
||||
|
||||
getWindowFn = load("_AXUIElementGetWindow", GetWindowFn.self)
|
||||
mainConnFn = load("CGSMainConnectionID", MainConnFn.self)
|
||||
copyManagedDisplaySpacesFn = load("CGSCopyManagedDisplaySpaces", CopyManagedDisplaySpacesFn.self)
|
||||
copySpacesForWindowsFn = load("CGSCopySpacesForWindows", CopySpacesForWindowsFn.self)
|
||||
createWithRemoteTokenFn = load("_AXUIElementCreateWithRemoteToken", CreateWithRemoteTokenFn.self)
|
||||
|
||||
spacesAvailable = mainConnFn != nil && copySpacesForWindowsFn != nil
|
||||
}
|
||||
|
||||
// windowID returns the CGWindowID of an AX window element.
|
||||
// Fallback if missing: the caller matches by PID, frame, and title.
|
||||
func windowID(for element: AXUIElement) -> CGWindowID? {
|
||||
guard let getWindowFn = getWindowFn else { return nil }
|
||||
var id: CGWindowID = 0
|
||||
let error = getWindowFn(element, &id) // PRIVATE API
|
||||
if error != .success { return nil }
|
||||
return id
|
||||
}
|
||||
|
||||
// mainConnectionID returns the CGS connection ID for this process.
|
||||
func mainConnectionID() -> Int32? {
|
||||
guard let mainConnFn = mainConnFn else { return nil }
|
||||
return mainConnFn() // PRIVATE API
|
||||
}
|
||||
|
||||
// managedDisplaySpaces returns the raw Space list per display. The caller
|
||||
// parses the dictionaries. Fallback if missing: nil (treat all windows as
|
||||
// current Space).
|
||||
func managedDisplaySpaces() -> [[String: Any]]? {
|
||||
guard let copyFn = copyManagedDisplaySpacesFn, let cid = mainConnectionID() else {
|
||||
return nil
|
||||
}
|
||||
guard let array = copyFn(cid)?.takeRetainedValue() as? [[String: Any]] else { // PRIVATE API
|
||||
return nil
|
||||
}
|
||||
return array
|
||||
}
|
||||
|
||||
// spaces returns the Space IDs for the given window IDs. The mask 7 selects
|
||||
// all Space types. Fallback if missing: nil.
|
||||
func spaces(forWindows windowIDs: [CGWindowID]) -> [UInt64]? {
|
||||
guard let copyFn = copySpacesForWindowsFn, let cid = mainConnectionID() else {
|
||||
return nil
|
||||
}
|
||||
let numbers = windowIDs.map { NSNumber(value: $0) } as CFArray
|
||||
let mask: Int32 = 7 // kCGSAllSpacesMask (all Space types)
|
||||
guard let result = copyFn(cid, mask, numbers)?.takeRetainedValue() as? [NSNumber] else { // PRIVATE API
|
||||
return nil
|
||||
}
|
||||
return result.map { $0.uint64Value }
|
||||
}
|
||||
|
||||
// remoteElement returns an AX element for a window on another Space. The
|
||||
// function builds the remote token from the PID and the window ID. The
|
||||
// layout follows AltTab. Fallback if missing or nil: activate the
|
||||
// application only (spec section 7.6, Method B).
|
||||
func remoteElement(pid: pid_t, windowID: CGWindowID) -> AXUIElement? {
|
||||
guard let createFn = createWithRemoteTokenFn else { return nil }
|
||||
// UNCERTAIN LAYOUT (source: AltTab). The token is 20 bytes:
|
||||
// - bytes 0..<4: pid (little endian Int32)
|
||||
// - bytes 4..<8: 0
|
||||
// - bytes 8..<12: magic 'coco' (0x636f636f)
|
||||
// - bytes 12..<20: window ID (UInt64 little endian)
|
||||
var token = Data(count: 20)
|
||||
token.withUnsafeMutableBytes { raw in
|
||||
let base = raw.baseAddress!
|
||||
base.storeBytes(of: Int32(pid).littleEndian, toByteOffset: 0, as: Int32.self)
|
||||
base.storeBytes(of: Int32(0).littleEndian, toByteOffset: 4, as: Int32.self)
|
||||
base.storeBytes(of: UInt32(0x636f636f).littleEndian, toByteOffset: 8, as: UInt32.self)
|
||||
base.storeBytes(of: UInt64(windowID).littleEndian, toByteOffset: 12, as: UInt64.self)
|
||||
}
|
||||
guard let element = createFn(token as CFData)?.takeRetainedValue() else { // PRIVATE API
|
||||
return nil
|
||||
}
|
||||
return element
|
||||
}
|
||||
}
|
||||
68
Sources/Private/SpaceService.swift
Normal file
68
Sources/Private/SpaceService.swift
Normal file
@@ -0,0 +1,68 @@
|
||||
import Foundation
|
||||
import CoreGraphics
|
||||
|
||||
// SpaceService reports the Space (desktop) of each window. The service uses
|
||||
// the private CGS API (PrivateAPI). If the private API is missing, the service
|
||||
// reports no Spaces. The caller then treats all windows as the current Space
|
||||
// (spec section 7.5). The service is main-actor isolated.
|
||||
@MainActor
|
||||
final class SpaceService {
|
||||
|
||||
private let api: PrivateAPI
|
||||
|
||||
init(api: PrivateAPI? = nil) {
|
||||
self.api = api ?? .shared
|
||||
}
|
||||
|
||||
// isAvailable is true if the Space API loaded.
|
||||
var isAvailable: Bool {
|
||||
return api.spacesAvailable
|
||||
}
|
||||
|
||||
// spaces returns a map from window ID to its Space IDs. If the API is
|
||||
// missing, the function returns an empty map.
|
||||
func spaces(for windowIDs: [CGWindowID]) -> [CGWindowID: [UInt64]] {
|
||||
guard isAvailable, windowIDs.isEmpty == false else { return [:] }
|
||||
var result: [CGWindowID: [UInt64]] = [:]
|
||||
// CGSCopySpacesForWindows returns the union of Spaces for the whole
|
||||
// list. The service queries one window at a time to map each window to
|
||||
// its own Space IDs.
|
||||
for id in windowIDs {
|
||||
if let spaces = api.spaces(forWindows: [id]) {
|
||||
result[id] = spaces
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// currentSpaceIDs returns the current Space ID of each display. The service
|
||||
// reads the managed display Space list. If the API is missing, the function
|
||||
// returns an empty array.
|
||||
func currentSpaceIDs() -> [UInt64] {
|
||||
guard let displays = api.managedDisplaySpaces() else { return [] }
|
||||
var ids: [UInt64] = []
|
||||
for display in displays {
|
||||
// Each display dictionary has a "Current Space" dictionary with the
|
||||
// key "ManagedSpaceID" (UInt64). Source: yabai, AltTab.
|
||||
if let current = display["Current Space"] as? [String: Any],
|
||||
let id = current["ManagedSpaceID"] as? UInt64 {
|
||||
ids.append(id)
|
||||
} else if let current = display["Current Space"] as? [String: Any],
|
||||
let number = current["ManagedSpaceID"] as? NSNumber {
|
||||
ids.append(number.uint64Value)
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// isOnCurrentSpace returns true if any Space ID of the window is a current
|
||||
// Space ID. If the service has no data, the function returns true. The app
|
||||
// then treats the window as a window on the current Space.
|
||||
func isOnCurrentSpace(windowSpaceIDs: [UInt64], currentSpaceIDs: [UInt64]) -> Bool {
|
||||
if windowSpaceIDs.isEmpty || currentSpaceIDs.isEmpty { return true }
|
||||
for id in windowSpaceIDs where currentSpaceIDs.contains(id) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user