Skip to content

Commit

Permalink
refactor: remove debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Mar 10, 2020
1 parent 52626aa commit 946d812
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 37 deletions.
21 changes: 2 additions & 19 deletions src/api-wrappers/AXUIElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,16 @@ extension AXUIElement {
}

func subscribeWithRetry(_ axObserver: AXObserver, _ notification: String, _ pointer: UnsafeMutableRawPointer?, _ callback: (() -> Void)? = nil, _ runningApplication: NSRunningApplication? = nil, _ wid: CGWindowID? = nil, _ attemptsCount: Int = 0) {
if attemptsCount == 0 || attemptsCount % 1000 == 0 {
if let runningApplication = runningApplication {
// debugPrint("attempt pid", attemptsCount, pid, notification, Applications.appsInSubscriptionRetryLoop.filter { $0.starts(with: String(pid)) })
}
if let wid = wid {
// debugPrint("attempt wid", attemptsCount, wid, notification, Windows.windowsInSubscriptionRetryLoop.filter { $0.starts(with: String(wid)) })
}
}
if let runningApplication = runningApplication, Applications.appsInSubscriptionRetryLoop.first(where: { $0 == String(runningApplication.processIdentifier) + String(notification) }) == nil {
// debugPrint("early quit pid", attemptsCount, pid, notification)
return
}
if let wid = wid, Windows.windowsInSubscriptionRetryLoop.first(where: { $0 == String(wid) + String(notification) }) == nil {
// debugPrint("early quit wid", attemptsCount, wid, notification)
return
}
if let runningApplication = runningApplication, Applications.appsInSubscriptionRetryLoop.first(where: { $0 == String(runningApplication.processIdentifier) + String(notification) }) == nil { return }
if let wid = wid, Windows.windowsInSubscriptionRetryLoop.first(where: { $0 == String(wid) + String(notification) }) == nil { return }
let result = AXObserverAddNotification(axObserver, self, notification as CFString, pointer)
if result == .success || result == .notificationUnsupported || result == .notificationAlreadyRegistered {
debugPrint("subbed", attemptsCount, runningApplication, wid, Applications.list.first(where: { $0.runningApplication.processIdentifier == runningApplication?.processIdentifier }))
callback?()
if let runningApplication = runningApplication {
Application.stopSubscriptionRetries(notification, runningApplication)
debugPrint("app sub list", Applications.appsInSubscriptionRetryLoop)
}
if let wid = wid {
Window.stopSubscriptionRetries(notification, wid)
debugPrint("win sub list", Windows.windowsInSubscriptionRetryLoop)
}
return
}
Expand Down
8 changes: 2 additions & 6 deletions src/logic/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Application: NSObject {

// some apps never finish their subscription retry loop; they should be stopped to avoid infinite loop
static func stopSubscriptionRetries(_ notification: String, _ runningApplication: NSRunningApplication) {
debugPrint("removeObservers", runningApplication.processIdentifier, runningApplication.bundleIdentifier)
Applications.appsInSubscriptionRetryLoop.removeAll { $0 == String(runningApplication.processIdentifier) + String(notification) }
}

Expand All @@ -31,7 +30,6 @@ class Application: NSObject {
}

deinit {
debugPrint("deinit", runningApplication.processIdentifier, runningApplication.bundleIdentifier)
// some apps never finish launching; subscription retries should be stopped to avoid infinite loops
Application.notifications.forEach { Application.stopSubscriptionRetries($0, runningApplication) }
// some apps never finish launching; observer should be removed to avoid leak
Expand All @@ -45,7 +43,7 @@ class Application: NSObject {
private func addAndObserveWindows() {
axUiElement = AXUIElementCreateApplication(runningApplication.processIdentifier)
AXObserverCreate(runningApplication.processIdentifier, axObserverCallback, &axObserver)
debugPrint("Adding app: " + (runningApplication.bundleIdentifier ?? "nil"))
debugPrint("Adding app", runningApplication.processIdentifier, runningApplication.bundleIdentifier ?? "nil")
observeEvents()
}

Expand Down Expand Up @@ -76,7 +74,6 @@ class Application: NSObject {
guard let axObserver = axObserver else { return }
let selfPointer = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
for notification in Application.notifications {
debugPrint("subscribeWithRetry app", runningApplication.processIdentifier, notification, runningApplication.bundleIdentifier)
Applications.appsInSubscriptionRetryLoop.append(String(runningApplication.processIdentifier) + String(notification))
axUiElement!.subscribeWithRetry(axObserver, notification, selfPointer, { [weak self] in
// some apps have `isFinishedLaunching == true` but are actually not finished, and will return .cannotComplete
Expand All @@ -88,7 +85,6 @@ class Application: NSObject {
}
}, runningApplication)
}
debugPrint("app sub list", Applications.appsInSubscriptionRetryLoop)
CFRunLoopAddSource(CFRunLoopGetCurrent(), AXObserverGetRunLoopSource(axObserver), .defaultMode)
}
}
Expand All @@ -97,7 +93,7 @@ private func axObserverCallback(observer: AXObserver, element: AXUIElement, noti
let application = Unmanaged<Application>.fromOpaque(applicationPointer!).takeUnretainedValue()
let app = App.shared as! App
let type = notificationName as String
debugPrint("OS event: " + type, element.title() ?? "nil")
debugPrint("OS event", type, element.title() ?? "nil")
switch type {
case kAXApplicationActivatedNotification: eventApplicationActivated(app, element)
case kAXApplicationHiddenNotification, kAXApplicationShownNotification: eventApplicationHiddenOrShown(app, element, type)
Expand Down
6 changes: 2 additions & 4 deletions src/logic/Applications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ class Applications {
Applications.list.removeAll(where: { $0.runningApplication.isEqual(runningApp) })
Windows.list.removeAll(where: { $0.application.runningApplication.isEqual(runningApp) })
}
debugPrint("app sub list", Applications.appsInSubscriptionRetryLoop)
debugPrint("win sub list", Windows.windowsInSubscriptionRetryLoop)
guard Windows.list.count > 0 else { (App.shared as! App).hideUi(); return }
// TODO: implement of more sophisticated way to decide which thumbnail gets focused on app quit
Windows.focusedWindowIndex = 1
Expand All @@ -78,11 +76,11 @@ class RunningApplicationsObserver: NSObject {
switch type {
case .insertion:
let apps = change![.newKey] as! [NSRunningApplication]
debugPrint("OS event: apps launched", apps.map { ($0.processIdentifier, $0.bundleIdentifier) })
debugPrint("OS event", "apps launched", apps.map { ($0.processIdentifier, $0.bundleIdentifier) })
Applications.addRunningApplications(apps)
case .removal:
let apps = change![.oldKey] as! [NSRunningApplication]
debugPrint("OS event: apps quit", apps.map { ($0.processIdentifier, $0.bundleIdentifier) })
debugPrint("OS event", "apps quit", apps.map { ($0.processIdentifier, $0.bundleIdentifier) })
Applications.removeRunningApplications(apps)
default: return
}
Expand Down
10 changes: 2 additions & 8 deletions src/logic/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Window {
]

static func stopSubscriptionRetries(_ notification: String, _ cgWindowId: CGWindowID) {
debugPrint("removeObservers", cgWindowId)
Windows.windowsInSubscriptionRetryLoop.removeAll { $0 == (String(cgWindowId) + String(notification)) }
}

Expand All @@ -38,12 +37,11 @@ class Window {
self.spaceIndex = Spaces.currentSpaceIndex
self.isOnAllSpaces = false
self.title = Window.bestEffortTitle(axUiElement, cgWindowId, application)
debugPrint("Adding window: " + title, application.runningApplication.bundleIdentifier ?? "nil", Spaces.currentSpaceId, Spaces.currentSpaceIndex)
debugPrint("Adding window", cgWindowId, title, application.runningApplication.bundleIdentifier ?? "nil", Spaces.currentSpaceId, Spaces.currentSpaceIndex)
observeEvents()
}

deinit {
debugPrint("deinit", cgWindowId, title)
// some windows never finish launching; subscription retries should be stopped to avoid infinite loops
Window.notifications.forEach { Window.stopSubscriptionRetries($0, cgWindowId) }
}
Expand All @@ -52,12 +50,9 @@ class Window {
AXObserverCreate(application.runningApplication.processIdentifier, axObserverCallback, &axObserver)
guard let axObserver = axObserver else { return }
for notification in Window.notifications {
debugPrint("subscribeWithRetry win", cgWindowId, notification, title)
Windows.windowsInSubscriptionRetryLoop.append(String(cgWindowId) + String(notification))
axUiElement.subscribeWithRetry(axObserver, notification, nil, nil, nil, cgWindowId)
}
debugPrint("app sub list", Applications.appsInSubscriptionRetryLoop)
debugPrint("win sub list", Windows.windowsInSubscriptionRetryLoop)
CFRunLoopAddSource(CFRunLoopGetCurrent(), AXObserverGetRunLoopSource(axObserver), .defaultMode)
}

Expand Down Expand Up @@ -121,7 +116,7 @@ class Window {
private func axObserverCallback(observer: AXObserver, element: AXUIElement, notificationName: CFString, _: UnsafeMutableRawPointer?) -> Void {
let type = notificationName as String
let app = App.shared as! App
debugPrint("OS event: " + type, element.title() ?? "nil")
debugPrint("OS event", type, element.title() ?? "nil")
switch type {
case kAXUIElementDestroyedNotification: eventWindowDestroyed(app, element)
case kAXWindowMiniaturizedNotification, kAXWindowDeminiaturizedNotification: eventWindowMiniaturizedOrDeminiaturized(app, element, type)
Expand All @@ -133,7 +128,6 @@ private func axObserverCallback(observer: AXObserver, element: AXUIElement, noti
private func eventWindowDestroyed(_ app: App, _ element: AXUIElement) {
guard let existingIndex = Windows.list.firstIndexThatMatches(element) else { return }
Windows.list.remove(at: existingIndex)
debugPrint("win sub list", Windows.windowsInSubscriptionRetryLoop)
guard Windows.list.count > 0 else { app.hideUi(); return }
Windows.moveFocusedWindowIndexAfterWindowDestroyedInBackground(existingIndex)
app.refreshOpenUi()
Expand Down

0 comments on commit 946d812

Please sign in to comment.