mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Share more code between media detection on different platforms
This commit is contained in:
+4
-3
@@ -199,6 +199,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/detection/memory/memory.c
|
||||
src/detection/displayserver/displayserver.c
|
||||
src/detection/terminalfont/terminalfont.c
|
||||
src/detection/media/media.c
|
||||
src/modules/break.c
|
||||
src/modules/custom.c
|
||||
src/modules/title.c
|
||||
@@ -242,6 +243,8 @@ set(LIBFASTFETCH_SRC
|
||||
|
||||
if(APPLE)
|
||||
list(APPEND LIBFASTFETCH_SRC
|
||||
src/util/apple/cfdict_helpers.c
|
||||
src/util/apple/osascript.m
|
||||
src/detection/host/host_apple.c
|
||||
src/detection/os/os_apple.c
|
||||
src/detection/cpu/cpu_apple.c
|
||||
@@ -250,8 +253,6 @@ if(APPLE)
|
||||
src/detection/memory/memory_apple.c
|
||||
src/detection/displayserver/displayserver_apple.c
|
||||
src/detection/terminalfont/terminalfont_apple.c
|
||||
src/util/apple/cfdict_helpers.c
|
||||
src/util/apple/osascript.m
|
||||
src/detection/media/media_apple.m
|
||||
)
|
||||
elseif(ANDROID)
|
||||
@@ -264,7 +265,7 @@ elseif(ANDROID)
|
||||
src/detection/memory/memory_linux.c
|
||||
src/detection/displayserver/displayserver_android.c
|
||||
src/detection/terminalfont/terminalfont_android.c
|
||||
src/detection/media/media_linux.c
|
||||
src/detection/media/media_android.c
|
||||
)
|
||||
else()
|
||||
list(APPEND LIBFASTFETCH_SRC
|
||||
|
||||
@@ -32,7 +32,7 @@ static void* libraryLoad(const char* path, int maxVersion)
|
||||
|
||||
void* ffLibraryLoad(const FFstrbuf* userProvidedName, ...)
|
||||
{
|
||||
if(userProvidedName->length > 0)
|
||||
if(userProvidedName != NULL && userProvidedName->length > 0)
|
||||
return dlopen(userProvidedName->chars, RTLD_LAZY);
|
||||
|
||||
va_list defaultNames;
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#include "media.h"
|
||||
#include "detection/internal.h"
|
||||
|
||||
void ffDetectMediaImpl(const FFinstance* instance, FFMediaResult* media);
|
||||
|
||||
const FFMediaResult* ffDetectMedia(const FFinstance* instance)
|
||||
{
|
||||
FF_DETECTION_INTERNAL_GUARD(FFMediaResult,
|
||||
ffStrbufInitA(&result.error, 0);
|
||||
ffStrbufInit(&result.busNameShort);
|
||||
ffStrbufInit(&result.player);
|
||||
ffStrbufInit(&result.song);
|
||||
ffStrbufInit(&result.artist);
|
||||
ffStrbufInit(&result.album);
|
||||
ffStrbufInit(&result.url);
|
||||
|
||||
ffDetectMediaImpl(instance, &result);
|
||||
|
||||
if(result.song.length == 0 && result.error.length == 0)
|
||||
ffStrbufAppendS(&result.error, "No media found");
|
||||
);
|
||||
}
|
||||
@@ -7,15 +7,15 @@
|
||||
|
||||
typedef struct FFMediaResult
|
||||
{
|
||||
FFstrbuf error;
|
||||
FFstrbuf busNameShort; //e.g. plasma-browser-integration
|
||||
FFstrbuf player; // e.g. Google Chrome
|
||||
FFstrbuf song;
|
||||
FFstrbuf artist;
|
||||
FFstrbuf album;
|
||||
FFstrbuf url;
|
||||
const char* error;
|
||||
} FFMediaResult;
|
||||
|
||||
const FFMediaResult* ffDetectMedia(FFinstance* instance);
|
||||
const FFMediaResult* ffDetectMedia(const FFinstance* instance);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "media.h"
|
||||
|
||||
void ffDetectMediaImpl(const FFinstance* instance, FFMediaResult* media)
|
||||
{
|
||||
FF_UNUSED(instance);
|
||||
ffStrbufAppendS(&media->error, "Media not supported on Android");
|
||||
}
|
||||
@@ -3,31 +3,16 @@
|
||||
#include "common/library.h"
|
||||
#include "util/apple/cfdict_helpers.h"
|
||||
|
||||
#import <pthread.h>
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <CoreFoundation/CoreFoundation.h>
|
||||
|
||||
void MRMediaRemoteGetNowPlayingInfo(dispatch_queue_t dispatcher, void(^callback)(_Nullable CFDictionaryRef info));
|
||||
|
||||
const char* getMedia(FFMediaResult* result)
|
||||
static const char* getMedia(FFMediaResult* result)
|
||||
{
|
||||
ffStrbufInit(&result->busNameShort);
|
||||
ffStrbufInit(&result->player);
|
||||
ffStrbufInit(&result->song);
|
||||
ffStrbufInit(&result->artist);
|
||||
ffStrbufInit(&result->album);
|
||||
ffStrbufInit(&result->url);
|
||||
|
||||
FFstrbuf fake;
|
||||
ffStrbufInitA(&fake, 0);//MediaRemote is a macOS builtin framework thus its path should not change
|
||||
|
||||
FF_LIBRARY_LOAD(
|
||||
MediaRemote,
|
||||
fake,
|
||||
"dlopen MediaRemote failed",
|
||||
"/System/Library/PrivateFrameworks/MediaRemote.framework/MediaRemote",
|
||||
1);
|
||||
FF_LIBRARY_LOAD(MediaRemote, NULL, "dlopen MediaRemote failed", "/System/Library/PrivateFrameworks/MediaRemote.framework/MediaRemote", -1);
|
||||
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(MediaRemote, MRMediaRemoteGetNowPlayingInfo);
|
||||
|
||||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
||||
ffMRMediaRemoteGetNowPlayingInfo(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(_Nullable CFDictionaryRef info) {
|
||||
if(info != nil) {
|
||||
@@ -38,26 +23,21 @@ const char* getMedia(FFMediaResult* result)
|
||||
dispatch_semaphore_signal(semaphore);
|
||||
});
|
||||
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
|
||||
return NULL;
|
||||
|
||||
if(result->song.length > 0)
|
||||
return NULL;
|
||||
|
||||
return "MediaRemote failed";
|
||||
}
|
||||
|
||||
const FFMediaResult* ffDetectMedia(FFinstance* instance)
|
||||
void ffDetectMediaImpl(const FFinstance* instance, FFMediaResult* media)
|
||||
{
|
||||
FF_UNUSED(instance)
|
||||
static FFMediaResult result;
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static bool init = false;
|
||||
const char* error = getMedia(media);
|
||||
ffStrbufAppendS(&media->error, error);
|
||||
|
||||
if(!init)
|
||||
{
|
||||
pthread_mutex_lock(&mutex);
|
||||
if(!init)
|
||||
{
|
||||
result.error = getMedia(&result);
|
||||
init = true;
|
||||
}
|
||||
pthread_mutex_unlock(&mutex);
|
||||
}
|
||||
|
||||
return &result;
|
||||
//TODO: proper detection
|
||||
//I already set it here, because the player module expects it to be set if the error is not set
|
||||
if(error == NULL)
|
||||
ffStrbufAppendS(&media->player, "Media Player");
|
||||
}
|
||||
|
||||
@@ -232,7 +232,7 @@ static bool getBusProperties(const char* busName, FFMediaResult* result, DBusDat
|
||||
return true;
|
||||
}
|
||||
|
||||
static void getCustomBus(FFinstance* instance, FFMediaResult* result, DBusData* data)
|
||||
static void getCustomBus(const FFinstance* instance, FFMediaResult* result, DBusData* data)
|
||||
{
|
||||
if(ffStrbufStartsWithS(&instance->config.playerName, FF_DBUS_MPRIS_PREFIX))
|
||||
{
|
||||
@@ -291,33 +291,33 @@ static void getBestBus(FFMediaResult* result, DBusData* data)
|
||||
data->ffdbus_message_unref(reply);
|
||||
}
|
||||
|
||||
static void getMedia(FFinstance* instance, FFMediaResult* result)
|
||||
static const char* getMedia(const FFinstance* instance, FFMediaResult* result)
|
||||
{
|
||||
DBusData data;
|
||||
|
||||
FF_LIBRARY_LOAD(dbus, instance->config.libDBus, , "libdbus-1.so", 4);
|
||||
FF_LIBRARY_LOAD_SYMBOL(dbus, dbus_bus_get,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_message_new_method_call,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_message_iter_init,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_message_iter_init_append,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_message_iter_append_basic,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_message_iter_get_arg_type,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_message_iter_get_basic,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_message_iter_recurse,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_message_iter_has_next,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_message_iter_next,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_message_unref,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_connection_send_with_reply,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_connection_flush,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_pending_call_block,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_pending_call_steal_reply,)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR(dbus, data, dbus_pending_call_unref,)
|
||||
FF_LIBRARY_LOAD(dbus, instance->config.libDBus, "dlopen dbus failed", "libdbus-1.so", 4);
|
||||
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(dbus, dbus_bus_get)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_message_new_method_call)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_message_iter_init)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_message_iter_init_append)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_message_iter_append_basic)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_message_iter_get_arg_type)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_message_iter_get_basic)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_message_iter_recurse)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_message_iter_has_next)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_message_iter_next)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_message_unref)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_connection_send_with_reply)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_connection_flush)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_pending_call_block)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_pending_call_steal_reply)
|
||||
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(dbus, data, dbus_pending_call_unref)
|
||||
|
||||
data.connection = ffdbus_bus_get(DBUS_BUS_SESSION, NULL);
|
||||
if(data.connection == NULL)
|
||||
{
|
||||
dlclose(dbus);
|
||||
return;
|
||||
return "Failed to connect to session dbus";
|
||||
}
|
||||
|
||||
if(instance->config.playerName.length > 0)
|
||||
@@ -326,44 +326,18 @@ static void getMedia(FFinstance* instance, FFMediaResult* result)
|
||||
getBestBus(result, &data);
|
||||
|
||||
dlclose(dbus);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
const FFMediaResult* ffDetectMedia(FFinstance* instance)
|
||||
void ffDetectMediaImpl(const FFinstance* instance, FFMediaResult* media)
|
||||
{
|
||||
static FFMediaResult result;
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static bool init = false;
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
if(init)
|
||||
{
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return &result;
|
||||
}
|
||||
init = true;
|
||||
|
||||
ffStrbufInit(&result.busNameShort);
|
||||
ffStrbufInit(&result.player);
|
||||
ffStrbufInit(&result.song);
|
||||
ffStrbufInit(&result.artist);
|
||||
ffStrbufInit(&result.album);
|
||||
ffStrbufInit(&result.url);
|
||||
|
||||
#ifdef FF_HAVE_DBUS
|
||||
getMedia(instance, &result);
|
||||
const char* error = getMedia(instance, media);
|
||||
ffStrbufAppendS(&media->error, error);
|
||||
#else
|
||||
FF_UNUSED(instance);
|
||||
ffStrbufAppendS(&media->error, "Fastfetch was compiled without DBus support");
|
||||
#endif
|
||||
|
||||
//Set player if a custom player was given, but loading with dbus failed
|
||||
if(instance->config.playerName.length > 0 && result.song.length == 0)
|
||||
{
|
||||
if(ffStrbufStartsWithS(&instance->config.playerName, FF_DBUS_MPRIS_PREFIX))
|
||||
ffStrbufAppendS(&result.player, instance->config.playerName.chars + sizeof(FF_DBUS_MPRIS_PREFIX) - 1);
|
||||
else
|
||||
ffStrbufAppend(&result.player, &instance->config.playerName);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return &result;
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@ void ffPrintPlayer(FFinstance* instance)
|
||||
{
|
||||
const FFMediaResult* media = ffDetectMedia(instance);
|
||||
|
||||
if(media->player.length == 0)
|
||||
if(media->error.length > 0)
|
||||
{
|
||||
ffPrintError(instance, FF_PLAYER_MODULE_NAME, 0, &instance->config.player, "No media player found");
|
||||
ffPrintError(instance, FF_PLAYER_MODULE_NAME, 0, &instance->config.player, "%s", media->error.chars);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -46,9 +46,9 @@ void ffPrintSong(FFinstance* instance)
|
||||
{
|
||||
const FFMediaResult* media = ffDetectMedia(instance);
|
||||
|
||||
if(media->song.length == 0)
|
||||
if(media->error.length > 0)
|
||||
{
|
||||
ffPrintError(instance, FF_SONG_MODULE_NAME, 0, &instance->config.song, "No song detected");
|
||||
ffPrintError(instance, FF_SONG_MODULE_NAME, 0, &instance->config.song, "%s", media->error.chars);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user