From b266e9ca53f2d7bcf460af35917665ad5c9711b3 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Tue, 9 Aug 2022 14:35:44 -0500 Subject: [PATCH] Fix type-based window-targeting --- src/MediaPlayer.cpp | 29 +++++++++++++++++++++-------- src/MediaPlayer.h | 1 + src/Song.cpp | 9 +++++++++ src/Song.h | 3 +++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/MediaPlayer.cpp b/src/MediaPlayer.cpp index 47983b2..36b8305 100644 --- a/src/MediaPlayer.cpp +++ b/src/MediaPlayer.cpp @@ -152,19 +152,24 @@ MediaPlayer::Window() fWindowTargetHops = kTargetHops; + Song current; + CurrentItem(¤t, false); + for (int32 i = CountWindows() - 1; i >= 0; i--) { MediaPlayer mp(i, MP_BY_INDEX); if (mp.IsValid()) { if (fWindowTarget == MP_BY_LATEST) - return i; + return fWindowIndex = i; - Song current; - BString type; - if (CurrentItem(¤t, false) - && BNode(current.Path().Path()).ReadAttrString("BEOS:TYPE", &type) == B_OK) - if ((type.StartsWith("audio") && fWindowTarget == MP_BY_TYPE_AUDIO) - || (type.StartsWith("video") && fWindowTarget == MP_BY_TYPE_VIDEO)) - return i; + Song mpcurrent; + mp.CurrentItem(&mpcurrent, false); + + // If the current item's type is correct, we'll keep it + if (!current.InitCheck() || !_MatchesTypeTarget(current.Type(), fWindowTarget)) + if (mpcurrent.InitCheck() && _MatchesTypeTarget(mpcurrent.Type(), fWindowTarget)) + return fWindowIndex = i; + else + fWindowIndex = -1; } } return fWindowIndex; @@ -259,6 +264,14 @@ MediaPlayer::_ScriptingCall(const char* attribute, BMessage* send, BMessage* rep } +bool +MediaPlayer::_MatchesTypeTarget(BString mimetype, target typeTarget) +{ + return (mimetype.StartsWith("video") && typeTarget == MP_BY_TYPE_VIDEO) + || (mimetype.StartsWith("audio") && typeTarget == MP_BY_TYPE_AUDIO); +} + + const char* MediaPlayer::_UriToPath(BString URI) { diff --git a/src/MediaPlayer.h b/src/MediaPlayer.h index e7a55d2..7c5255c 100644 --- a/src/MediaPlayer.h +++ b/src/MediaPlayer.h @@ -71,6 +71,7 @@ private: void _ScriptingCall(const char* attribute, BMessage* send, BMessage* reply, int32 trackIndex = MP_NO_TRACK, bool window = true); + bool _MatchesTypeTarget(BString mimetype, target typeTarget); const char* _UriToPath(BString URI); int32 fWindowIndex; diff --git a/src/Song.cpp b/src/Song.cpp index e300ade..3865323 100644 --- a/src/Song.cpp +++ b/src/Song.cpp @@ -75,6 +75,15 @@ Song::Path() } +BString +Song::Type() +{ + if (fType.IsEmpty()) + BNode(fPath.Path()).ReadAttrString("BEOS:TYPE", &fType); + return fType; +} + + int64 Song::Duration() { diff --git a/src/Song.h b/src/Song.h index 6fecebd..d36f21e 100644 --- a/src/Song.h +++ b/src/Song.h @@ -18,6 +18,7 @@ #define SONG_H #include +#include class BBitmap; @@ -32,6 +33,7 @@ public: BBitmap* Cover(); BPath Path(); + BString Type(); int64 Duration(); int operator ==(Song a) @@ -60,6 +62,7 @@ private: int64 fDuration; BPath fPath; + BString fType; }; #endif // SONG_H