From 797d7b206d68e351bceaef6e88c9d1816db959ef Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Fri, 22 Feb 2019 08:35:52 -0500 Subject: [PATCH] Add MediaKeyTap to capture media keys This is resulting in an alarming warning appearing in the console: ``` pid(87021)/euid(501) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!! ``` I've opened an issue on the library GitHub page for now. --- Brewfile | 1 + Cartfile | 1 + Cartfile.resolved | 1 + Persephone.xcodeproj/project.pbxproj | 28 ++++++++++++++++++++++++++++ Persephone/AppDelegate.swift | 18 +++++++++++++++++- bin/setup | 2 ++ 6 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 Cartfile create mode 100644 Cartfile.resolved diff --git a/Brewfile b/Brewfile index 0721bb6..9a01409 100644 --- a/Brewfile +++ b/Brewfile @@ -1,2 +1,3 @@ +brew "carthage" brew "meson" brew "ninja" diff --git a/Cartfile b/Cartfile new file mode 100644 index 0000000..8a652e9 --- /dev/null +++ b/Cartfile @@ -0,0 +1 @@ +github "nhurden/MediaKeyTap" diff --git a/Cartfile.resolved b/Cartfile.resolved new file mode 100644 index 0000000..e689609 --- /dev/null +++ b/Cartfile.resolved @@ -0,0 +1 @@ +github "nhurden/MediaKeyTap" "2.2.0" diff --git a/Persephone.xcodeproj/project.pbxproj b/Persephone.xcodeproj/project.pbxproj index b05f28a..c8caed2 100644 --- a/Persephone.xcodeproj/project.pbxproj +++ b/Persephone.xcodeproj/project.pbxproj @@ -24,6 +24,9 @@ E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41B22C521FB932700D544F6 /* MPDClient.swift */; }; E41EA46C221636AF0068EF46 /* PreferencesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41EA46B221636AF0068EF46 /* PreferencesViewController.swift */; }; E41EA46F221715910068EF46 /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41EA46E221715910068EF46 /* Preferences.swift */; }; + E421AC9D221F7333008B2449 /* MediaKeyTap.framework.dSYM in CopyFiles */ = {isa = PBXBuildFile; fileRef = E421AC9C221F7333008B2449 /* MediaKeyTap.framework.dSYM */; }; + E421ACA3221F73C4008B2449 /* MediaKeyTap.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E421ACA1221F73B8008B2449 /* MediaKeyTap.framework */; }; + E421ACA4221F73C4008B2449 /* MediaKeyTap.framework in Embed Libraries */ = {isa = PBXBuildFile; fileRef = E421ACA1221F73B8008B2449 /* MediaKeyTap.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; E42A8F3B22176D6400A13ED9 /* LICENSE.md in Resources */ = {isa = PBXBuildFile; fileRef = E42A8F3922176D6400A13ED9 /* LICENSE.md */; }; E42A8F3C22176D6400A13ED9 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = E42A8F3A22176D6400A13ED9 /* README.md */; }; E465049A21E94DF500A70F4C /* WindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E465049921E94DF500A70F4C /* WindowController.swift */; }; @@ -62,10 +65,21 @@ dstSubfolderSpec = 10; files = ( E41B22C121FB6C3300D544F6 /* libmpdclient.2.dylib in Embed Libraries */, + E421ACA4221F73C4008B2449 /* MediaKeyTap.framework in Embed Libraries */, ); name = "Embed Libraries"; runOnlyForDeploymentPostprocessing = 0; }; + E421AC9B221F7319008B2449 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 16; + files = ( + E421AC9D221F7333008B2449 /* MediaKeyTap.framework.dSYM in CopyFiles */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ @@ -128,6 +142,8 @@ E41B22EB21FB966C00D544F6 /* playlist.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = playlist.h; sourceTree = ""; }; E41EA46B221636AF0068EF46 /* PreferencesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesViewController.swift; sourceTree = ""; }; E41EA46E221715910068EF46 /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = ""; }; + E421AC9C221F7333008B2449 /* MediaKeyTap.framework.dSYM */ = {isa = PBXFileReference; lastKnownFileType = wrapper.dsym; name = MediaKeyTap.framework.dSYM; path = Carthage/Build/Mac/MediaKeyTap.framework.dSYM; sourceTree = ""; }; + E421ACA1221F73B8008B2449 /* MediaKeyTap.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MediaKeyTap.framework; path = Carthage/Build/Mac/MediaKeyTap.framework; sourceTree = ""; }; E42A8F3922176D6400A13ED9 /* LICENSE.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = LICENSE.md; sourceTree = ""; }; E42A8F3A22176D6400A13ED9 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; E465049921E94DF500A70F4C /* WindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowController.swift; sourceTree = ""; }; @@ -147,6 +163,7 @@ buildActionMask = 2147483647; files = ( E41B22C021FB6BBA00D544F6 /* libmpdclient.2.dylib in Frameworks */, + E421ACA3221F73C4008B2449 /* MediaKeyTap.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -170,6 +187,7 @@ E407860F2110CE6E006887B1 = { isa = PBXGroup; children = ( + E421AC9C221F7333008B2449 /* MediaKeyTap.framework.dSYM */, E407861A2110CE6E006887B1 /* Persephone */, E407862D2110CE70006887B1 /* PersephoneTests */, E40786382110CE70006887B1 /* PersephoneUITests */, @@ -264,6 +282,7 @@ E41B22BE21FB6B3300D544F6 /* Frameworks */ = { isa = PBXGroup; children = ( + E421ACA1221F73B8008B2449 /* MediaKeyTap.framework */, E41B22BF21FB6BBA00D544F6 /* libmpdclient.2.dylib */, ); name = Frameworks; @@ -390,6 +409,7 @@ E40786152110CE6E006887B1 /* Frameworks */, E40786162110CE6E006887B1 /* Resources */, E41B22C221FB6C3300D544F6 /* Embed Libraries */, + E421AC9B221F7319008B2449 /* CopyFiles */, ); buildRules = ( ); @@ -708,6 +728,10 @@ CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; DEVELOPMENT_TEAM = BDEE7ZBFZ3; + FRAMEWORK_SEARCH_PATHS = ( + "$(inherited)", + "$(PROJECT_DIR)/Carthage/Build/Mac", + ); INFOPLIST_FILE = Persephone/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", @@ -735,6 +759,10 @@ CODE_SIGN_STYLE = Manual; COMBINE_HIDPI_IMAGES = YES; DEVELOPMENT_TEAM = BDEE7ZBFZ3; + FRAMEWORK_SEARCH_PATHS = ( + "$(inherited)", + "$(PROJECT_DIR)/Carthage/Build/Mac", + ); INFOPLIST_FILE = Persephone/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", diff --git a/Persephone/AppDelegate.swift b/Persephone/AppDelegate.swift index 6a52a54..f8ab378 100644 --- a/Persephone/AppDelegate.swift +++ b/Persephone/AppDelegate.swift @@ -7,10 +7,12 @@ // import Cocoa +import MediaKeyTap @NSApplicationMain -class AppDelegate: NSObject, NSApplicationDelegate { +class AppDelegate: NSObject, NSApplicationDelegate, MediaKeyTapDelegate { var preferences = Preferences() + var mediaKeyTap: MediaKeyTap? static let mpdClient = MPDClient( withDelegate: NotificationsController() @@ -21,6 +23,9 @@ class AppDelegate: NSObject, NSApplicationDelegate { preferences.addObserver(self, forKeyPath: "mpdHost") preferences.addObserver(self, forKeyPath: "mpdPort") + + mediaKeyTap = MediaKeyTap(delegate: self) + mediaKeyTap?.start() } func applicationWillTerminate(_ aNotification: Notification) { @@ -42,6 +47,17 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } + func handle(mediaKey: MediaKey, event: KeyEvent) { + switch mediaKey { + case .playPause: + AppDelegate.mpdClient.playPause() + case .next, .fastForward: + AppDelegate.mpdClient.nextTrack() + case .previous, .rewind: + AppDelegate.mpdClient.prevTrack() + } + } + func connect() { AppDelegate.mpdClient.connect( host: preferences.mpdHostOrDefault, diff --git a/bin/setup b/bin/setup index 3883297..a1126df 100755 --- a/bin/setup +++ b/bin/setup @@ -2,6 +2,8 @@ git submodule update --init +carthage update + cd libmpdclient && \ meson . output && \ ninja -C output