Skip to content

Commit

Permalink
fix: ignore more non-user-facing apps (xpc processes)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Oct 6, 2020
1 parent cd0d53d commit 8417564
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
7 changes: 7 additions & 0 deletions src/api-wrappers/HelperExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,10 @@ extension pid_t {
return kinfo.kp_proc.p_stat == SZOMB
}
}

extension String {
// convert a FourCharCode into a String
init(_ fourCharCode: FourCharCode) { // or `OSType`, or `UInt32`
self = NSFileTypeForHFSTypeCode(fourCharCode).trimmingCharacters(in: CharacterSet(charactersIn: "'"))
}
}
20 changes: 10 additions & 10 deletions src/api-wrappers/PrivateApis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ func setNativeCommandTabEnabled(_ isEnabled: Bool) {
CGSSetSymbolicHotKeyEnabled(2, isEnabled) // command+shift+tab
}

// returns info about a given psn
// * macOS 10.9-10.15 (officially removed in 10.9, but available as a private API still)
@_silgen_name("GetProcessInformation") @discardableResult
func GetProcessInformation(_ psn: inout ProcessSerialNumber, _ info: inout ProcessInfoRec) -> OSErr
//
// returns the psn for a given pid
// * macOS 10.9-10.15 (officially removed in 10.9, but available as a private API still)
@_silgen_name("GetProcessForPID") @discardableResult
func GetProcessForPID(_ pid: pid_t, _ psn: inout ProcessSerialNumber) -> OSStatus


// ------------------------------------------------------------
// below are some notes on some private APIs I experimented with
Expand Down Expand Up @@ -190,16 +200,6 @@ func setNativeCommandTabEnabled(_ isEnabled: Bool) {
//@_silgen_name("GetProcessPID")
//func GetProcessPID(_ psn: inout ProcessSerialNumber, _ pid: inout pid_t) -> Void
//
//// returns info about a given psn
//// * macOS 10.9-
//@_silgen_name("GetProcessInformation") @discardableResult
//func GetProcessInformation(_ psn: inout ProcessSerialNumber, _ info: inout ProcessInfoRec) -> OSErr
//
//// returns the psn for a given pid
//// * macOS 10.9-
//@_silgen_name("GetProcessForPID") @discardableResult
//func GetProcessForPID(_ pid: pid_t, _ psn: inout ProcessSerialNumber) -> OSStatus
//
//// crashed the app with SIGSEGV
//// * macOS 10.10+
//@_silgen_name("CGSGetWindowType") @discardableResult
Expand Down
14 changes: 9 additions & 5 deletions src/logic/Applications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,18 @@ class Applications {
}

private static func isActualApplication(_ app: NSRunningApplication) -> Bool {
return (app.activationPolicy != .prohibited || isNotXpc(app)) && !app.processIdentifier.isZombie()
// an app can be both activationPolicy == .accessory and XPC (e.g. com.apple.dock.etci)
return app.activationPolicy != .prohibited && isNotXpc(app) && !app.processIdentifier.isZombie()
}

private static func isNotXpc(_ app: NSRunningApplication) -> Bool {
return app.bundleURL
.flatMap { Bundle(url: $0) }
.flatMap { $0.infoDictionary }
.flatMap { $0["CFBundlePackageType"] as? String } != "XPC!"
// these private APIs are more reliable than Bundle.init? as it can return nil (e.g. for com.apple.dock.etci)
var psn = ProcessSerialNumber()
GetProcessForPID(app.processIdentifier, &psn)
var info = ProcessInfoRec()
GetProcessInformation(&psn, &info)
debugPrint(app.bundleIdentifier, String(info.processType))
return String(info.processType) != "XPC!"
}

// managing AltTab windows within AltTab create all sorts of side effects
Expand Down

0 comments on commit 8417564

Please sign in to comment.