From b298df219eb007ea11e6c14a332fd3f4924ff7d2 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 16 Aug 2018 05:26:40 +0200 Subject: Use an `NSApplication` Previously the `setListenInExclusiveMode:` call did appear to work, disabling the normal functioning of the headphone buttons. But my `NSLog`s in `ddhidAppleMikey:press:upOrDown:` didn't get printed, the method having not been called. Since all the other applications I found using DDHidLib were GUI apps using `NSApplication`, I decided to take this approach here. Set up the `NSApplication` in `main.c` and added a new `AppDelegate` class. It seems like the reference to my `HeadphoneKey` instance wasn't getting retained or something. When I moved the: HeadphoneKey *h = [[HeadphoneKey alloc] init]; line into `AppDelegate`'s `applicationDidFinishLaunching:`, nothing changed. However, when I moved all code from `HeadphoneKey` into `AppDelegate`, my `ddhidAppleMikey:press:upOrDown:` method _did_ get called. At that point, it was logical to try adding a `HeadphoneKey` instance variable and `init`ing into that variable. As a result, we now have a working `ddhidAppleMikey:press:upOrDown:` delegate method that correctly logs the headphone key events! Here are the reference applications I looked at using DDHidLib, found using a simple GitHub search: - https://github.com/radiant-player/radiant-player-mac/pull/450/files - https://github.com/7hil/mac_ear_control/blob/8859ed554517ce798b8ae7c2d0e78610b9994300/mac_ear_control/AppDelegate.m - https://github.com/7hil/mac_ear_control/blob/8859ed554517ce798b8ae7c2d0e78610b9994300/mac_ear_control/AppDelegate.m - https://github.com/schrekia/beardedspice-enhanced/blob/a9b78c0dbae6bca2cb0d2ecc362e4b7fe5b02129/BeardedSpiceControllers/BSCService.m - https://github.com/beardedspice/beardedspice/blob/8e1aea5bda68395e6b0d866bca9289a2a5ba40ed/BeardedSpiceControllers/BSCService.m - https://github.com/beardedspice/beardedspice/blob/8e1aea5bda68395e6b0d866bca9289a2a5ba40ed/BeardedSpiceControllers/BSCService.m - https://github.com/BarakaAka1Only/radiant-player-mac-BarakaLyrics/blob/1ce889bbb80f081209aff7936a329a3b426462e9/radiant-player-mac/AppDelegate.m - https://github.com/zsszatmari/MagicKeys/blob/210015d5da876cd7bb1daf73efa3c775c18e7973/MagicKeys-Agent/SPMediaKeyTap.m Thanks immensely this Cocoa With Love article from Matt Gallagher, which was invaluable in showing me how to create a minimal Cocoa application without needing a nib, Info.plist, or even application bundle (instead putting everything in a single executable, making it easier to distribute this as a command-line application with a built-in daemon): https://www.cocoawithlove.com/2010/09/minimalist-cocoa-programming.html --- DomeKey.xcodeproj/project.pbxproj | 6 ++++++ .../xcuserdata/tw.xcuserdatad/xcschemes/xcschememanagement.plist | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'DomeKey.xcodeproj') diff --git a/DomeKey.xcodeproj/project.pbxproj b/DomeKey.xcodeproj/project.pbxproj index 6f61405..ff696e3 100644 --- a/DomeKey.xcodeproj/project.pbxproj +++ b/DomeKey.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ D110C9482122E2D80094F963 /* HeadphoneKey.m in Sources */ = {isa = PBXBuildFile; fileRef = D110C9472122E2D80094F963 /* HeadphoneKey.m */; }; D110C94A2122F1ED0094F963 /* libddhid.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D160C32221197983007D1B50 /* libddhid.a */; }; + D11184622125206E00961687 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = D11184612125206E00961687 /* AppDelegate.m */; }; D160C2A12118EF9D007D1B50 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D160C2A02118EF9D007D1B50 /* main.m */; }; /* End PBXBuildFile section */ @@ -65,6 +66,8 @@ /* Begin PBXFileReference section */ D110C9462122E2D80094F963 /* HeadphoneKey.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HeadphoneKey.h; sourceTree = ""; }; D110C9472122E2D80094F963 /* HeadphoneKey.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HeadphoneKey.m; sourceTree = ""; }; + D11184602125206E00961687 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; + D11184612125206E00961687 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; D160C29D2118EF9D007D1B50 /* DomeKey */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = DomeKey; sourceTree = BUILT_PRODUCTS_DIR; }; D160C2A02118EF9D007D1B50 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; D160C31521197983007D1B50 /* DDHidLib.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = DDHidLib.xcodeproj; path = lib/DDHidLib/DDHidLib.xcodeproj; sourceTree = ""; }; @@ -105,6 +108,8 @@ D110C9462122E2D80094F963 /* HeadphoneKey.h */, D110C9472122E2D80094F963 /* HeadphoneKey.m */, D160C2A02118EF9D007D1B50 /* main.m */, + D11184602125206E00961687 /* AppDelegate.h */, + D11184612125206E00961687 /* AppDelegate.m */, ); path = DomeKey; sourceTree = ""; @@ -225,6 +230,7 @@ files = ( D110C9482122E2D80094F963 /* HeadphoneKey.m in Sources */, D160C2A12118EF9D007D1B50 /* main.m in Sources */, + D11184622125206E00961687 /* AppDelegate.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/DomeKey.xcodeproj/xcuserdata/tw.xcuserdatad/xcschemes/xcschememanagement.plist b/DomeKey.xcodeproj/xcuserdata/tw.xcuserdatad/xcschemes/xcschememanagement.plist index 1112607..424ba24 100644 --- a/DomeKey.xcodeproj/xcuserdata/tw.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/DomeKey.xcodeproj/xcuserdata/tw.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,7 +7,7 @@ DomeKey.xcscheme orderHint - 1 + 0 -- cgit v1.2.3