rainmeter-studio/Plugins/PluginNowPlaying/NowPlaying.cpp

601 lines
14 KiB
C++
Raw Normal View History

/*
Copyright (C) 2011 Birunthan Mohanathas (www.poiru.net)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "StdAfx.h"
#include "NowPlaying.h"
#include "Internet.h"
#include "PlayerAIMP.h"
#include "PlayerCAD.h"
#include "PlayerFoobar.h"
#include "PlayerITunes.h"
#include "PlayerSpotify.h"
#include "PlayerWinamp.h"
#include "PlayerWLM.h"
#include "PlayerWMP.h"
2012-01-08 17:35:29 +00:00
static std::vector<ParentMeasure*> g_ParentMeasures;
std::wstring g_SettingsFile;
2011-07-15 15:18:02 +00:00
HINSTANCE g_Instance = NULL;
2012-01-08 17:35:29 +00:00
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
2012-01-08 17:35:29 +00:00
switch (fdwReason)
{
2012-01-08 17:35:29 +00:00
case DLL_PROCESS_ATTACH:
g_Instance = hinstDLL;
2012-01-08 17:35:29 +00:00
// Disable DLL_THREAD_ATTACH and DLL_THREAD_DETACH notification calls
DisableThreadLibraryCalls(hinstDLL);
break;
}
return TRUE;
}
PLUGIN_EXPORT void Initialize(void** data, void* rm)
2012-01-08 17:35:29 +00:00
{
Measure* measure = new Measure;
*data = measure;
if (g_SettingsFile.empty())
{
g_SettingsFile = RmGetSettingsFile();
CInternet::Initialize();
}
2012-01-08 17:35:29 +00:00
}
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
{
Measure* measure = (Measure*)data;
2012-01-08 17:35:29 +00:00
// Data is stored in two structs: Measure and ParentMeasure. ParentMeasure is created for measures
// with PlayerName=someplayer. Measure is created for all measures and points to ParentMeasure as
// referenced in PlayerName=[section].
2011-05-31 13:15:53 +00:00
// Read settings from the ini-file
2012-01-08 17:35:29 +00:00
void* skin = RmGetSkin(rm);
LPCWSTR str = RmReadString(rm, L"PlayerName", L"", FALSE);
if (str[0] == L'[')
{
2012-01-08 17:35:29 +00:00
if (measure->parent)
{
// Don't let a measure measure change its parent
}
else
{
// PlayerName starts with [ so use referenced section
2012-01-08 17:35:29 +00:00
++str;
int len = wcslen(str);
if (len > 0 && str[len - 1] == L']')
{
2012-01-08 17:35:29 +00:00
--len;
std::vector<ParentMeasure*>::iterator iter = g_ParentMeasures.begin();
for ( ; iter != g_ParentMeasures.end(); ++iter)
{
2012-01-08 17:35:29 +00:00
if (skin == (*iter)->skin &&
wcsncmp(str, (*iter)->ownerName, len) == 0)
{
// Use same ParentMeasure as referenced section
2012-01-08 17:35:29 +00:00
measure->parent = (*iter);
++measure->parent->measureCount;
break;
}
}
2012-01-08 17:35:29 +00:00
if (!measure->parent)
{
// The referenced section doesn't exist
std::wstring error = L"NowPlaying.dll: Invalid PlayerName=";
2012-01-08 17:35:29 +00:00
error.append(str - 1, len + 2);
error += L" in [";
2012-01-08 17:35:29 +00:00
error += RmGetMeasureName(rm);
error += L"]";
2012-01-08 17:35:29 +00:00
RmLog(LOG_WARNING, error.c_str());
return;
}
}
}
2012-01-08 17:35:29 +00:00
}
else
{
// ParentMeasure is created when PlayerName is an actual player (and not a reference)
ParentMeasure* parent = measure->parent;
CPlayer* oldPlayer = NULL;
if (parent)
{
2012-01-08 17:35:29 +00:00
if (parent->data != data)
{
2012-01-08 17:35:29 +00:00
// Don't let a measure-only measure become a parent measure
return;
}
2012-01-08 17:35:29 +00:00
oldPlayer = parent->player;
}
2012-01-08 17:35:29 +00:00
else
{
2012-01-08 17:35:29 +00:00
parent = new ParentMeasure;
g_ParentMeasures.push_back(parent);
parent->data = data;
parent->skin = skin;
parent->ownerName = RmGetMeasureName(rm);
measure->parent = parent;
}
2012-01-08 17:35:29 +00:00
if (_wcsicmp(L"AIMP", str) == 0)
{
2012-01-08 17:35:29 +00:00
parent->player = CPlayerAIMP::Create();
}
2012-01-08 17:35:29 +00:00
else if (_wcsicmp(L"CAD", str) == 0)
{
2012-01-08 17:35:29 +00:00
parent->player = CPlayerCAD::Create();
}
2012-01-08 17:35:29 +00:00
else if (_wcsicmp(L"foobar2000", str) == 0)
{
2012-01-08 17:35:29 +00:00
parent->player = CPlayerFoobar::Create();
}
2012-01-08 17:35:29 +00:00
else if (_wcsicmp(L"iTunes", str) == 0)
{
2012-01-08 17:35:29 +00:00
parent->player = CPlayerITunes::Create();
}
2012-01-08 17:35:29 +00:00
else if (_wcsicmp(L"MediaMonkey", str) == 0)
{
2012-01-08 17:35:29 +00:00
parent->player = CPlayerWinamp::Create(WA_MEDIAMONKEY);
}
2012-01-08 17:35:29 +00:00
else if (_wcsicmp(L"Spotify", str) == 0)
{
2012-01-08 17:35:29 +00:00
parent->player = CPlayerSpotify::Create();
}
2012-01-08 17:35:29 +00:00
else if (_wcsicmp(L"WinAmp", str) == 0)
{
2012-01-08 17:35:29 +00:00
parent->player = CPlayerWinamp::Create(WA_WINAMP);
}
2012-01-08 17:35:29 +00:00
else if (_wcsicmp(L"WMP", str) == 0)
{
2012-01-08 17:35:29 +00:00
parent->player = CPlayerWMP::Create();
}
else
{
// Default to WLM
parent->player = CPlayerWLM::Create();
if (_wcsicmp(L"WLM", str) != 0)
{
std::wstring error = L"NowPlaying.dll: Invalid PlayerName=";
error += str;
error += L" in [";
error += parent->ownerName;
error += L"]";
RmLog(LOG_ERROR, error.c_str());
}
}
2012-01-08 17:35:29 +00:00
parent->player->AddInstance();
parent->playerPath = RmReadString(rm, L"PlayerPath", L"", TRUE);
parent->trackChangeAction = RmReadString(rm, L"TrackChangeAction", L"", TRUE);
parent->disableLeadingZero = RmReadInt(rm, L"DisableLeadingZero", 0);
if (oldPlayer)
{
parent->player->SetMeasures(oldPlayer->GetMeasures());
2012-01-08 17:35:29 +00:00
// Remove instance here so that player doesn't have to reinitialize if PlayerName was
// not changed.
oldPlayer->RemoveInstance();
}
}
str = RmReadString(rm, L"PlayerType", L"");
if (_wcsicmp(L"ARTIST", str) == 0)
{
measure->type = MEASURE_ARTIST;
}
else if (_wcsicmp(L"TITLE", str) == 0)
{
measure->type = MEASURE_TITLE;
}
else if (_wcsicmp(L"ALBUM", str) == 0)
{
measure->type = MEASURE_ALBUM;
}
else if (_wcsicmp(L"COVER", str) == 0)
{
measure->type = MEASURE_COVER;
}
else if (_wcsicmp(L"DURATION", str) == 0)
{
measure->type = MEASURE_DURATION;
}
else if (_wcsicmp(L"POSITION", str) == 0)
{
measure->type = MEASURE_POSITION;
}
else if (_wcsicmp(L"PROGRESS", str) == 0)
{
measure->type = MEASURE_PROGRESS;
*maxValue = 100.0;
}
else if (_wcsicmp(L"RATING", str) == 0)
{
measure->type = MEASURE_RATING;
*maxValue = 5.0;
}
else if (_wcsicmp(L"STATE", str) == 0)
{
measure->type = MEASURE_STATE;
}
else if (_wcsicmp(L"STATUS", str) == 0)
{
measure->type = MEASURE_STATUS;
}
else if (_wcsicmp(L"VOLUME", str) == 0)
{
measure->type = MEASURE_VOLUME;
*maxValue = 100.0;
}
else if (_wcsicmp(L"SHUFFLE", str) == 0)
{
measure->type = MEASURE_SHUFFLE;
}
else if (_wcsicmp(L"REPEAT", str) == 0)
{
measure->type = MEASURE_REPEAT;
}
else if (_wcsicmp(L"LYRICS", str) == 0)
{
RmLog(LOG_WARNING, L"NowPlaying.dll: Using undocumented PlayerType=LYRICS!");
measure->type = MEASURE_LYRICS;
}
else if (_wcsicmp(L"FILE", str) == 0)
{
measure->type = MEASURE_FILE;
}
else if (_wcsicmp(L"NUMBER", str) == 0)
{
measure->type = MEASURE_NUMBER;
}
else if (_wcsicmp(L"YEAR", str) == 0)
{
measure->type = MEASURE_YEAR;
}
2012-01-08 17:35:29 +00:00
else
{
std::wstring error = L"NowPlaying.dll: Invalid PlayerType=";
error += str;
error += L" in [";
error += RmGetMeasureName(rm);
error += L"]";
RmLog(LOG_WARNING, error.c_str());
}
2012-01-08 17:35:29 +00:00
measure->parent->player->AddMeasure(measure->type);
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT double Update(void* data)
{
2012-01-08 17:35:29 +00:00
Measure* measure = (Measure*)data;
ParentMeasure* parent = measure->parent;
if (!parent) return 0.0;
2012-01-08 17:35:29 +00:00
CPlayer* player = parent->player;
2012-01-08 17:35:29 +00:00
// Only allow parent measure to update
if (parent->data == data)
{
player->UpdateMeasure();
2012-01-08 17:35:29 +00:00
// Execute TrackChangeAction= if necessary
if (!parent->trackChangeAction.empty() &&
parent->trackCount != player->GetTrackCount())
{
2012-01-08 17:35:29 +00:00
RmExecute(parent->skin, parent->trackChangeAction.c_str());
parent->trackCount = player->GetTrackCount();
}
}
2012-01-08 17:35:29 +00:00
switch (measure->type)
{
2012-01-08 17:35:29 +00:00
case MEASURE_DURATION:
return player->GetDuration();
2012-01-08 17:35:29 +00:00
case MEASURE_POSITION:
return player->GetPosition();
2012-01-08 17:35:29 +00:00
case MEASURE_PROGRESS:
if (player->GetDuration())
{
2012-01-08 17:35:29 +00:00
return (player->GetPosition() * 100) / player->GetDuration();
}
return 0.0;
2012-01-08 17:35:29 +00:00
case MEASURE_RATING:
return player->GetRating();
2012-01-08 17:35:29 +00:00
case MEASURE_VOLUME:
return player->GetVolume();
2012-01-08 17:35:29 +00:00
case MEASURE_STATE:
return player->GetState();
2012-01-08 17:35:29 +00:00
case MEASURE_STATUS:
return player->IsInitialized();
2012-01-08 17:35:29 +00:00
case MEASURE_SHUFFLE:
return player->GetShuffle();
2012-01-08 17:35:29 +00:00
case MEASURE_REPEAT:
return player->GetRepeat();
case MEASURE_NUMBER:
return player->GetNumber();
case MEASURE_YEAR:
return player->GetYear();
}
2012-01-08 17:35:29 +00:00
return 0.0;
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT LPCWSTR GetString(void* data)
{
2012-01-08 17:35:29 +00:00
Measure* measure = (Measure*)data;
ParentMeasure* parent = measure->parent;
if (!parent) return NULL;
2012-01-08 17:35:29 +00:00
const CPlayer* player = parent->player;
static WCHAR buffer[32];
2012-01-08 17:35:29 +00:00
switch (measure->type)
{
case MEASURE_ARTIST:
return player->GetArtist();
2012-01-08 17:35:29 +00:00
case MEASURE_TITLE:
return player->GetTitle();
2012-01-08 17:35:29 +00:00
case MEASURE_ALBUM:
return player->GetAlbum();
2012-01-08 17:35:29 +00:00
case MEASURE_LYRICS:
return player->GetLyrics();
2012-01-08 17:35:29 +00:00
case MEASURE_COVER:
return player->GetCoverPath();
2012-01-08 17:35:29 +00:00
case MEASURE_FILE:
return player->GetFilePath();
2012-01-08 17:35:29 +00:00
case MEASURE_DURATION:
SecondsToTime(player->GetDuration(), parent->disableLeadingZero, buffer);
return buffer;
2012-01-08 17:35:29 +00:00
case MEASURE_POSITION:
SecondsToTime(player->GetPosition(), parent->disableLeadingZero, buffer);
return buffer;
2012-01-08 17:35:29 +00:00
case MEASURE_PROGRESS:
_itow_s(player->GetDuration() ? ((player->GetPosition() * 100) / player->GetDuration()) : 0, buffer, 10);
return buffer;
2012-01-08 17:35:29 +00:00
case MEASURE_RATING:
_itow_s(player->GetRating(), buffer, 10);
return buffer;
2012-01-08 17:35:29 +00:00
case MEASURE_VOLUME:
_itow_s(player->GetVolume(), buffer, 10);
return buffer;
2012-01-08 17:35:29 +00:00
case MEASURE_STATE:
_itow_s(player->GetState(), buffer, 10);
return buffer;
2012-01-08 17:35:29 +00:00
case MEASURE_STATUS:
_itow_s((int)player->IsInitialized(), buffer, 10);
return buffer;
2012-01-08 17:35:29 +00:00
case MEASURE_SHUFFLE:
_itow_s((int)player->GetShuffle(), buffer, 10);
return buffer;
case MEASURE_REPEAT:
_itow_s((int)player->GetRepeat(), buffer, 10);
return buffer;
}
return NULL;
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Finalize(void* data)
{
2012-01-08 17:35:29 +00:00
Measure* measure = (Measure*)data;
ParentMeasure* parent = measure->parent;
if (parent)
{
CPlayer* player = parent->player;
2012-01-08 17:35:29 +00:00
if (--parent->measureCount == 0)
{
2012-01-08 17:35:29 +00:00
player->RemoveInstance();
delete parent;
std::vector<ParentMeasure*>::iterator iter = std::find(g_ParentMeasures.begin(), g_ParentMeasures.end(), parent);
g_ParentMeasures.erase(iter);
if (g_ParentMeasures.empty())
{
2012-01-08 17:35:29 +00:00
CInternet::Finalize();
}
}
2012-01-08 17:35:29 +00:00
}
delete measure;
}
PLUGIN_EXPORT void ExecuteBang(void* data, LPCWSTR args)
{
Measure* measure = (Measure*)data;
ParentMeasure* parent = measure->parent;
if (!parent) return;
CPlayer* player = parent->player;
if (!player->IsInitialized())
{
if (_wcsicmp(args, L"OpenPlayer") == 0 || _wcsicmp(args, L"TogglePlayer") == 0)
{
player->OpenPlayer(parent->playerPath);
}
2012-01-08 17:35:29 +00:00
}
else if (_wcsicmp(args, L"Pause") == 0)
{
player->Pause();
}
else if (_wcsicmp(args, L"Play") == 0)
{
player->Play();
}
else if (_wcsicmp(args, L"PlayPause") == 0)
{
(player->GetState() != STATE_PLAYING) ? player->Play() : player->Pause();
}
else if (_wcsicmp(args, L"Next") == 0)
{
player->Next();
}
else if (_wcsicmp(args, L"Previous") == 0)
{
player->Previous();
}
else if (_wcsicmp(args, L"Stop") == 0)
{
player->Stop();
}
else if (_wcsicmp(args, L"OpenPlayer") == 0)
{
player->OpenPlayer(parent->playerPath);
}
else if (_wcsicmp(args, L"ClosePlayer") == 0 || _wcsicmp(args, L"TogglePlayer") == 0)
{
player->ClosePlayer();
}
else
{
LPCWSTR arg = wcschr(args, L' ');
if (arg)
{
2012-01-08 17:35:29 +00:00
++arg; // Skip the space
2012-01-08 17:35:29 +00:00
if (wcsnicmp(args, L"SetPosition", 11) == 0)
{
2012-01-08 17:35:29 +00:00
int position = (_wtoi(arg) * (int)player->GetDuration()) / 100;
if (arg[0] == L'+' || arg[0] == L'-')
{
2012-01-08 17:35:29 +00:00
position += player->GetPosition();
}
2012-01-08 17:35:29 +00:00
player->SetPosition(position);
}
else if (wcsnicmp(args, L"SetRating", 9) == 0)
{
int rating = _wtoi(arg);
if (rating >= 0 && rating <= 5)
{
2012-01-08 17:35:29 +00:00
player->SetRating(rating);
}
2012-01-08 17:35:29 +00:00
}
else if (wcsnicmp(args, L"SetVolume", 9) == 0)
{
int volume = _wtoi(arg);
if (arg[0] == L'+' || arg[0] == L'-')
{
2012-01-08 17:35:29 +00:00
// Relative to current volume
volume += player->GetVolume();
}
2012-01-08 17:35:29 +00:00
if (volume < 0)
{
volume = 0;
}
2012-01-08 17:35:29 +00:00
else if (volume > 100)
{
2012-01-08 17:35:29 +00:00
volume = 100;
}
2012-01-08 17:35:29 +00:00
player->SetVolume(volume);;
}
else if (wcsnicmp(args, L"SetShuffle", 9) == 0)
{
int state = _wtoi(arg);
if (state == -1)
{
2012-01-08 17:35:29 +00:00
player->SetShuffle(!player->GetShuffle());
}
else if (state == 0 || state == 1)
{
player->SetShuffle((bool)state);
}
}
else if (wcsnicmp(args, L"SetRepeat", 9) == 0)
{
int state = _wtoi(arg);
if (state == -1)
{
player->SetRepeat(!player->GetRepeat());
}
2012-01-08 17:35:29 +00:00
else if (state == 0 || state == 1)
{
2012-01-08 17:35:29 +00:00
player->SetRepeat((bool)state);
}
}
else
{
2012-01-08 17:35:29 +00:00
RmLog(LOG_WARNING, L"NowPlaying.dll: Unknown args");
}
}
2012-01-08 17:35:29 +00:00
else
{
RmLog(LOG_WARNING, L"NowPlaying.dll: Unknown args");
}
}
}
void SecondsToTime(UINT seconds, bool leadingZero, WCHAR* buffer)
{
int hours = seconds;
int mins = seconds;
hours /= 3600;
mins %= 3600;
int secs = mins;
mins /= 60;
secs %= 60;
if (seconds < 0)
{
hours = mins = secs = 0;
}
if (hours)
{
_snwprintf_s(buffer, 32, _TRUNCATE, leadingZero ? L"%i:%02i:%02i" : L"%02i:%02i:%02i", hours, mins, secs);
}
else
{
_snwprintf_s(buffer, 32, _TRUNCATE, leadingZero ? L"%i:%02i" : L"%02i:%02i", mins, secs);
}
}