Media (Windows): detect with external DLL

For backup only
This commit is contained in:
李通洲
2024-06-11 14:44:47 +08:00
parent 9104db0d21
commit ff3ddc774e
4 changed files with 80 additions and 15 deletions
+1 -1
View File
@@ -695,7 +695,7 @@ elseif(WIN32)
src/detection/locale/locale_windows.c
src/detection/localip/localip_windows.c
src/detection/gamepad/gamepad_windows.c
src/detection/media/media_nosupport.c
src/detection/media/media_windows.c
src/detection/memory/memory_windows.c
src/detection/physicalmemory/physicalmemory_linux.c
src/detection/monitor/monitor_windows.c
+4
View File
@@ -20,6 +20,10 @@ const FFMediaResult* ffDetectMedia(void)
if(result.song.length == 0 && result.error.length == 0)
ffStrbufAppendS(&result.error, "No media found");
ffStrbufTrimRightSpace(&result.song);
ffStrbufTrimRightSpace(&result.artist);
ffStrbufTrimRightSpace(&result.album);
ffStrbufTrimRightSpace(&result.player);
}
return &result;
+52
View File
@@ -0,0 +1,52 @@
#include "fastfetch.h"
#include "common/library.h"
#include "util/windows/unicode.h"
#include "media.h"
#define FF_MEDIA_WIN_RESULT_BUFLEN 256
typedef struct FFMediaWinResult
{
wchar_t playerId[FF_MEDIA_WIN_RESULT_BUFLEN];
wchar_t song[FF_MEDIA_WIN_RESULT_BUFLEN];
wchar_t artist[FF_MEDIA_WIN_RESULT_BUFLEN];
wchar_t album[FF_MEDIA_WIN_RESULT_BUFLEN];
char status[FF_MEDIA_WIN_RESULT_BUFLEN];
char error[FF_MEDIA_WIN_RESULT_BUFLEN];
} FFMediaWinResult;
bool ffMediaWinDetectMedia(FFMediaWinResult* result);
static const char* getMedia(FFMediaResult* media)
{
FF_LIBRARY_LOAD(libffdll, NULL, "dlopen fastfetch-dll" FF_LIBRARY_EXTENSION " failed", "fastfetch-dll" FF_LIBRARY_EXTENSION, 0)
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(libffdll, ffMediaWinDetectMedia)
libffdll = NULL; // Don't close libffdll or it may crash
FFMediaWinResult result = {};
if (!ffffMediaWinDetectMedia(&result))
{
ffStrbufAppendS(&media->error, result.error);
ffStrbufAppendS(&media->error, " (DLL error)");
return NULL;
}
ffStrbufSetWS(&media->playerId, result.playerId);
ffStrbufSet(&media->player, &media->playerId);
if (ffStrbufEndsWithIgnCaseS(&media->player, ".exe"))
ffStrbufSubstrBefore(&media->player, media->player.length - 4);
else
ffStrbufSubstrAfterFirstC(&media->player, '!'); // UWP ID
ffStrbufSetWS(&media->song, result.song);
ffStrbufSetWS(&media->artist, result.artist);
ffStrbufSetWS(&media->album, result.album);
ffStrbufSetS(&media->status, result.status);
return NULL;
}
void ffDetectMediaImpl(FFMediaResult* media)
{
const char* error = getMedia(media);
ffStrbufAppendS(&media->error, error);
}
+23 -14
View File
@@ -19,21 +19,30 @@ void ffPrintPlayer(FFPlayerOptions* options)
return;
}
if (media->player.length == 0)
{
ffPrintError(FF_PLAYER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No media player detected");
return;
}
FF_STRBUF_AUTO_DESTROY playerPretty = ffStrbufCreate();
//If we are on a website, prepend the website name
if(
ffStrbufIgnCaseCompS(&media->playerId, "spotify") == 0 ||
ffStrbufIgnCaseCompS(&media->playerId, "vlc") == 0
) {} // do noting, surely not a website, even if the url is set
else if(ffStrbufStartsWithS(&media->url, "https://www."))
ffStrbufAppendS(&playerPretty, media->url.chars + 12);
else if(ffStrbufStartsWithS(&media->url, "http://www."))
ffStrbufAppendS(&playerPretty, media->url.chars + 11);
else if(ffStrbufStartsWithS(&media->url, "https://"))
ffStrbufAppendS(&playerPretty, media->url.chars + 8);
else if(ffStrbufStartsWithS(&media->url, "http://"))
ffStrbufAppendS(&playerPretty, media->url.chars + 7);
if (media->url.length > 0)
{
//If we are on a website, prepend the website name
if(
ffStrbufIgnCaseEqualS(&media->playerId, "spotify") ||
ffStrbufIgnCaseEqualS(&media->playerId, "vlc")
) {} // do noting, surely not a website, even if the url is set
else if(ffStrbufStartsWithS(&media->url, "https://www."))
ffStrbufAppendS(&playerPretty, media->url.chars + 12);
else if(ffStrbufStartsWithS(&media->url, "http://www."))
ffStrbufAppendS(&playerPretty, media->url.chars + 11);
else if(ffStrbufStartsWithS(&media->url, "https://"))
ffStrbufAppendS(&playerPretty, media->url.chars + 8);
else if(ffStrbufStartsWithS(&media->url, "http://"))
ffStrbufAppendS(&playerPretty, media->url.chars + 7);
}
//If we found a website name, make it more pretty
if(playerPretty.length > 0)
@@ -68,7 +77,7 @@ void ffPrintPlayer(FFPlayerOptions* options)
{FF_FORMAT_ARG_TYPE_STRBUF, &playerPretty, "player"},
{FF_FORMAT_ARG_TYPE_STRBUF, &media->player, "name"},
{FF_FORMAT_ARG_TYPE_STRBUF, &media->playerId, "id"},
{FF_FORMAT_ARG_TYPE_STRBUF, &media->url, "url"}
{FF_FORMAT_ARG_TYPE_STRBUF, &media->url, "url"},
}));
}
}