forked from nhurden/MediaKeyTap
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMediaApplicationWatcher.swift
More file actions
215 lines (180 loc) · 7.59 KB
/
MediaApplicationWatcher.swift
File metadata and controls
215 lines (180 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
// MediaApplicationWatcher.swift
// Castle
//
// Maintains a list of active media applications.
//
// Created by Nicholas Hurden on 18/02/2016.
// Copyright © 2016 Nicholas Hurden. All rights reserved.
//
import Cocoa
protocol MediaApplicationWatcherDelegate: class {
func updateIsActiveMediaApp(_ active: Bool)
func whitelistedAppStarted()
}
class MediaApplicationWatcher {
var mediaApps: [NSRunningApplication]
weak var delegate: MediaApplicationWatcherDelegate?
// A set of bundle identifiers that notifications have been received from
var dynamicWhitelist: Set<String>
let mediaKeyTapDidStartNotification = "MediaKeyTapDidStart" // Sent on start()
let mediaKeyTapReplyNotification = "MediaKeyTapReply" // Sent on receipt of a mediaKeyTapDidStartNotification
init() {
mediaApps = []
dynamicWhitelist = []
}
/// Activate the currently running application (without an NSNotification)
func activate() {
handleApplicationActivation(application: NSRunningApplication.current)
}
func start() {
let notificationCenter = NSWorkspace.shared.notificationCenter
notificationCenter.addObserver(
self,
selector: #selector(applicationLaunched),
name: NSWorkspace.didLaunchApplicationNotification,
object: nil
)
notificationCenter.addObserver(
self,
selector: #selector(applicationActivated),
name: NSWorkspace.didActivateApplicationNotification,
object: nil
)
notificationCenter.addObserver(
self,
selector: #selector(applicationTerminated),
name: NSWorkspace.didTerminateApplicationNotification,
object: nil
)
setupDistributedNotifications()
}
func stop() {
NSWorkspace.shared.notificationCenter.removeObserver(self)
}
func setupDistributedNotifications() {
let distributedNotificationCenter = DistributedNotificationCenter.default()
// Notify any other apps using this library using a distributed notification
// deliverImmediately is needed to ensure that backgrounded apps can resign the
// media tap immediately when new media apps are launched
let ownBundleIdentifier = Bundle.main.bundleIdentifier
distributedNotificationCenter.postNotificationName(
NSNotification.Name(rawValue: mediaKeyTapDidStartNotification),
object: ownBundleIdentifier,
userInfo: nil,
deliverImmediately: true
)
distributedNotificationCenter.addObserver(
forName: NSNotification.Name(rawValue: mediaKeyTapDidStartNotification),
object: nil,
queue: nil
) { notification in
if let otherBundleIdentifier = notification.object as? String {
guard otherBundleIdentifier != ownBundleIdentifier else { return }
self.dynamicWhitelist.insert(otherBundleIdentifier)
// Send a reply so that the sender knows that this app exists
distributedNotificationCenter.postNotificationName(
NSNotification.Name(rawValue: self.mediaKeyTapReplyNotification),
object: ownBundleIdentifier,
userInfo: nil,
deliverImmediately: true
)
}
}
distributedNotificationCenter.addObserver(
forName: NSNotification.Name(rawValue: mediaKeyTapReplyNotification),
object: nil,
queue: nil
) { notification in
if let otherBundleIdentifier = notification.object as? String {
guard otherBundleIdentifier != ownBundleIdentifier else { return }
self.dynamicWhitelist.insert(otherBundleIdentifier)
}
}
}
// MARK: - Notifications
@objc private func applicationLaunched(_ notification: Notification) {
if let application = (notification as NSNotification).userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication {
if inStaticWhitelist(application), application != NSRunningApplication.current {
delegate?.whitelistedAppStarted()
}
}
}
@objc private func applicationActivated(_ notification: Notification) {
if let application = (notification as NSNotification).userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication {
guard whitelisted(application) else { return }
handleApplicationActivation(application: application)
}
}
@objc private func applicationTerminated(_ notification: Notification) {
if let application = (notification as NSNotification).userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication {
mediaApps = mediaApps.filter { $0 != application }
updateKeyInterceptStatus()
}
}
// When activated, move `application` to the front of `mediaApps` and toggle the tap as necessary
private func handleApplicationActivation(application: NSRunningApplication) {
mediaApps = mediaApps.filter { $0 != application }
mediaApps.insert(application, at: 0)
updateKeyInterceptStatus()
}
private func updateKeyInterceptStatus() {
guard mediaApps.count > 0 else { return }
let activeApp = mediaApps.first!
let ownApp = NSRunningApplication.current
delegate?.updateIsActiveMediaApp(activeApp == ownApp)
}
// MARK: - Identifier Whitelist
// The static SPMediaKeyTap whitelist
func whitelistedApplicationIdentifiers() -> Set<String> {
var whitelist: Set<String> = [
"at.justp.Theremin",
"co.rackit.mate",
"com.Timenut.SongKey",
"com.apple.Aperture",
"com.apple.QuickTimePlayerX",
"com.apple.iPhoto",
"com.apple.iTunes",
"com.apple.iWork.Keynote",
"com.apple.quicktimeplayer",
"com.beardedspice.BeardedSpice",
"com.beatport.BeatportPro",
"com.bitcartel.pandorajam",
"com.ilabs.PandorasHelper",
"com.jriver.MediaCenter18",
"com.jriver.MediaCenter19",
"com.jriver.MediaCenter20",
"com.macromedia.fireworks", // the tap messes up their mouse input
"com.mahasoftware.pandabar",
"com.netease.163music",
"com.plexsquared.Plex",
"com.plug.Plug",
"com.plug.Plug2",
"com.soundcloud.desktop",
"com.spotify.client",
"com.ttitt.b-music",
"fm.last.Last.fm",
"fm.last.Scrobbler",
"org.clementine-player.clementine",
"org.niltsh.MPlayerX",
"org.quodlibet.quodlibet",
"org.videolan.vlc",
"ru.ya.themblsha.YandexMusic",
]
if let ownIdentifier = Bundle.main.bundleIdentifier {
whitelist.insert(ownIdentifier)
}
return whitelist
}
private func inStaticWhitelist(_ application: NSRunningApplication) -> Bool {
guard let id = application.bundleIdentifier else { return false }
return whitelistedApplicationIdentifiers().contains(id)
}
private func inDynamicWhitelist(_ application: NSRunningApplication) -> Bool {
guard let id = application.bundleIdentifier else { return false }
return dynamicWhitelist.contains(id)
}
private func whitelisted(_ application: NSRunningApplication) -> Bool {
inStaticWhitelist(application) || inDynamicWhitelist(application)
}
}