11#include " fastfetch.h"
22#include " detection/media/media.h"
33#include " common/library.h"
4- #include " util/apple/cfdict_helpers.h"
4+ #include " util/apple/cf_helpers.h"
5+ #include " util/apple/osascript.h"
56
67#import < Foundation/Foundation.h>
78#import < CoreFoundation/CoreFoundation.h>
89
9- void MRMediaRemoteGetNowPlayingInfo (dispatch_queue_t dispatcher, void (^callback)(_Nullable CFDictionaryRef info));
10+ extern void MRMediaRemoteGetNowPlayingInfo (dispatch_queue_t dispatcher, void (^callback)(_Nullable CFDictionaryRef info)) __attribute__((weak_import));
11+ extern void MRMediaRemoteGetNowPlayingClient (dispatch_queue_t dispatcher, void (^callback)(_Nullable id clientObj)) __attribute__((weak_import));
12+ extern CFStringRef MRNowPlayingClientGetBundleIdentifier (id clientObj) __attribute__((weak_import));
13+ extern CFStringRef MRNowPlayingClientGetParentAppBundleIdentifier (id clientObj) __attribute__((weak_import));
14+ void MRMediaRemoteGetNowPlayingApplicationIsPlaying (dispatch_queue_t queue, void (^callback)(BOOL playing));
1015
1116static const char * getMedia (FFMediaResult* result)
1217{
13- FF_LIBRARY_LOAD (MediaRemote, NULL , " dlopen MediaRemote failed" , " /System/Library/PrivateFrameworks/MediaRemote.framework/MediaRemote" , -1 );
14- FF_LIBRARY_LOAD_SYMBOL_MESSAGE (MediaRemote, MRMediaRemoteGetNowPlayingInfo);
18+ #define FF_TEST_FN_EXISTANCE (fn ) if (!fn) return " MediaRemote function " #fn " is not available"
19+ FF_TEST_FN_EXISTANCE (MRMediaRemoteGetNowPlayingInfo);
20+ FF_TEST_FN_EXISTANCE (MRMediaRemoteGetNowPlayingClient);
21+ FF_TEST_FN_EXISTANCE (MRNowPlayingClientGetBundleIdentifier);
22+ FF_TEST_FN_EXISTANCE (MRNowPlayingClientGetParentAppBundleIdentifier);
23+ #undef FF_TEST_FN_EXISTANCE
1524
16- dispatch_semaphore_t semaphore = dispatch_semaphore_create (0 );
17- ffMRMediaRemoteGetNowPlayingInfo (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_HIGH, 0 ), ^(_Nullable CFDictionaryRef info) {
25+ dispatch_group_t group = dispatch_group_create ();
26+ dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_HIGH, 0 );
27+
28+ dispatch_group_enter (group);
29+ MRMediaRemoteGetNowPlayingApplicationIsPlaying (queue, ^(BOOL playing) {
30+ ffStrbufAppendS (&result->status , playing ? " Playing" : " Paused" );
31+ dispatch_group_leave (group);
32+ });
33+
34+ dispatch_group_enter (group);
35+ MRMediaRemoteGetNowPlayingInfo (queue, ^(_Nullable CFDictionaryRef info) {
1836 if (info != nil ) {
1937 ffCfDictGetString (info, CFSTR (" kMRMediaRemoteNowPlayingInfoTitle" ), &result->song );
2038 ffCfDictGetString (info, CFSTR (" kMRMediaRemoteNowPlayingInfoArtist" ), &result->artist );
2139 ffCfDictGetString (info, CFSTR (" kMRMediaRemoteNowPlayingInfoAlbum" ), &result->album );
2240 }
23- dispatch_semaphore_signal (semaphore );
41+ dispatch_group_leave (group );
2442 });
25- dispatch_semaphore_wait (semaphore, DISPATCH_TIME_FOREVER);
43+
44+ dispatch_group_enter (group);
45+ MRMediaRemoteGetNowPlayingClient (queue, ^(_Nullable id clientObj) {
46+ if (clientObj != nil ) {
47+ CFStringRef identifier = MRNowPlayingClientGetBundleIdentifier (clientObj);
48+ if (identifier == nil )
49+ identifier = MRNowPlayingClientGetParentAppBundleIdentifier (clientObj);
50+ if (identifier != nil )
51+ ffCfStrGetString (identifier, &result->busNameShort );
52+ }
53+ dispatch_group_leave (group);
54+ });
55+
56+ dispatch_group_wait (group, DISPATCH_TIME_FOREVER);
57+
58+ if (result->busNameShort .length > 0 )
59+ {
60+ char buf[128 ];
61+ snprintf (buf, sizeof (buf), " name of app id \" %s \" " , result->busNameShort .chars );
62+ ffOsascript (buf, &result->player );
63+ }
2664
2765 if (result->song .length > 0 )
2866 return NULL ;
@@ -35,9 +73,4 @@ void ffDetectMediaImpl(const FFinstance* instance, FFMediaResult* media)
3573 FF_UNUSED (instance)
3674 const char * error = getMedia (media);
3775 ffStrbufAppendS (&media->error , error);
38-
39- // TODO: proper detection
40- // I already set it here, because the player module expects it to be set if the error is not set
41- if (error == NULL )
42- ffStrbufAppendS (&media->player , " Media Player" );
4376}
0 commit comments