Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import Darwinpublic import Foundationimport ParcelCore
public struct RuleSpaceMoveRecord: Codable, Hashable, Sendable { public let window: UInt32 public let pid: Int32 public let bundleID: String public let display: String public let desktop: Int
public init(window: UInt32, pid: Int32, bundleID: String, display: String, desktop: Int) { self.window = window self.pid = pid self.bundleID = bundleID self.display = display self.desktop = desktop }}
public enum RuleSpaceMoveStoreError: Error, Equatable, Sendable { case unsupportedVersion(Int) case invalidRecord case capacityExceeded case bootSessionUnavailable(Int32)}
public struct RuleSpaceMoveStore: Sendable { public let fileURL: URL
public init( fileURL: URL = URL.applicationSupportDirectory .appendingPathComponent("dev.vinnymac.parcel/rule-space-moves.json") ) { self.fileURL = fileURL }
public func load() throws -> [RuleSpaceMoveRecord] { guard FileManager.default.fileExists(atPath: fileURL.path) else { return [] } let data = try Data(contentsOf: fileURL) guard data.count <= 4_194_304 else { throw RuleSpaceMoveStoreError.capacityExceeded } let document = try JSONDecoder().decode(Document.self, from: data) guard document.version == 1 else { throw RuleSpaceMoveStoreError.unsupportedVersion(document.version) } try validate(document.records) guard let session = document.bootSessionID, UUID(uuidString: session) != nil else { throw RuleSpaceMoveStoreError.invalidRecord } // Window and process identifiers can be reused after a reboot. return session == (try bootSessionID()) ? document.records : [] }
public func save(_ records: [RuleSpaceMoveRecord]) throws { try validate(records) let encoder = JSONEncoder() encoder.outputFormatting = [.prettyPrinted, .sortedKeys] let data = try encoder.encode(Document(version: 1, bootSessionID: bootSessionID(), records: records)) guard data.count <= 4_194_304 else { throw RuleSpaceMoveStoreError.capacityExceeded } try FileManager.default.createDirectory( at: fileURL.deletingLastPathComponent(), withIntermediateDirectories: true) try data.write(to: fileURL, options: .atomic) }
public func reset() throws { if FileManager.default.fileExists(atPath: fileURL.path) { try FileManager.default.removeItem(at: fileURL) } }
private func validate(_ records: [RuleSpaceMoveRecord]) throws { guard records.count <= 16_384 else { throw RuleSpaceMoveStoreError.capacityExceeded } guard Set(records).count == records.count, records.allSatisfy({ $0.window > 0 && $0.pid > 0 && !$0.display.isEmpty && (1...16).contains($0.desktop) }) else { throw RuleSpaceMoveStoreError.invalidRecord } }
private func bootSessionID() throws -> String { var size = 0 guard sysctlbyname("kern.bootsessionuuid", nil, &size, nil, 0) == 0 else { throw RuleSpaceMoveStoreError.bootSessionUnavailable(errno) } guard (2...128).contains(size) else { throw RuleSpaceMoveStoreError.invalidRecord } var bytes = [CChar](repeating: 0, count: size) let result = bytes.withUnsafeMutableBytes { buffer in sysctlbyname("kern.bootsessionuuid", buffer.baseAddress, &size, nil, 0) } guard result == 0 else { throw RuleSpaceMoveStoreError.bootSessionUnavailable(errno) } guard (2...bytes.count).contains(size), bytes[size - 1] == 0 else { throw RuleSpaceMoveStoreError.invalidRecord } let text = bytes.withUnsafeBufferPointer { buffer -> String? in guard let base = buffer.baseAddress else { return nil } return String(validatingCString: base) } guard let text, let uuid = UUID(uuidString: text) else { throw RuleSpaceMoveStoreError.invalidRecord } return uuid.uuidString }
private struct Document: Codable { let version: Int let bootSessionID: String? let records: [RuleSpaceMoveRecord] }}
extension ParcelEngine { public var ruleSpaceMoveHistoryFileURL: URL? { ruleSpaceMoveStore?.fileURL }}