diff --git a/CMakeLists.txt b/CMakeLists.txt index 83ea5cf24..622521c82 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/detection/media/media.c b/src/detection/media/media.c index c24d8a1c4..585ccc48e 100644 --- a/src/detection/media/media.c +++ b/src/detection/media/media.c @@ -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; diff --git a/src/detection/media/media_windows.c b/src/detection/media/media_windows.c new file mode 100644 index 000000000..3477e058c --- /dev/null +++ b/src/detection/media/media_windows.c @@ -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); +} diff --git a/src/modules/player/player.c b/src/modules/player/player.c index df6c8a534..4e97da8ae 100644 --- a/src/modules/player/player.c +++ b/src/modules/player/player.c @@ -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"}, })); } }