2011-05-21 18:17:37 +00:00
/*
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 . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*/
# include "StdAfx.h"
# include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
# include "NowPlaying.h"
# include "PlayerAIMP.h"
2011-05-24 18:20:35 +00:00
# include "PlayerCAD.h"
2011-05-21 18:17:37 +00:00
# include "PlayerFoobar.h"
# include "PlayerITunes.h"
# include "PlayerSpotify.h"
# include "PlayerWinamp.h"
2011-06-12 10:05:37 +00:00
# include "PlayerWLM.h"
2011-05-21 18:17:37 +00:00
# include "PlayerWMP.h"
2011-05-22 14:45:54 +00:00
CPlayer * g_AIMP = NULL ;
2011-05-24 18:20:35 +00:00
CPlayer * g_CAD = NULL ;
2011-05-22 14:45:54 +00:00
CPlayer * g_Foobar = NULL ;
CPlayer * g_iTunes = NULL ;
CPlayer * g_Spotify = NULL ;
CPlayer * g_Winamp = NULL ;
2011-06-12 10:05:37 +00:00
CPlayer * g_WLM = NULL ;
2011-05-22 14:45:54 +00:00
CPlayer * g_WMP = NULL ;
2011-05-21 18:17:37 +00:00
static MeasureMap g_Values ;
static bool g_DisableLeazingZero = false ;
std : : wstring g_CachePath ;
2011-06-12 10:05:37 +00:00
std : : wstring g_SettingsFile ;
2011-05-21 18:17:37 +00:00
void SecondsToTime ( UINT seconds , WCHAR * buffer )
{
int hours = seconds ;
int mins = seconds ;
hours / = 3600 ;
mins % = 3600 ;
int secs = mins ;
mins / = 60 ;
secs % = 60 ;
if ( hours )
{
_snwprintf_s ( buffer , 32 , _TRUNCATE , g_DisableLeazingZero ? L " %i:%02i:%02i " : L " %02i:%02i:%02i " , hours , mins , secs ) ;
}
else
{
_snwprintf_s ( buffer , 32 , _TRUNCATE , g_DisableLeazingZero ? L " %i:%02i " : L " %02i:%02i " , mins , secs ) ;
}
}
/*
* * Initialize
* *
* * Called when the measure is initialized .
* *
*/
UINT Initialize ( HMODULE instance , LPCTSTR iniFile , LPCTSTR section , UINT id )
{
if ( g_Values . empty ( ) )
{
2011-06-12 10:05:37 +00:00
// Get path to temporary folder (for cover art cache)
2011-05-21 18:17:37 +00:00
WCHAR buffer [ MAX_PATH ] ;
GetTempPath ( MAX_PATH , buffer ) ;
wcscat ( buffer , L " Rainmeter-Cache \\ " ) ;
CreateDirectory ( buffer , NULL ) ;
g_CachePath = buffer ;
2011-06-12 10:05:37 +00:00
// Get path to Plugins.ini (usually %APPDATA%\Rainmeter\Plugins.ini)
std : : wstring str = PluginBridge ( L " getconfig " , iniFile ) ;
if ( ! str . empty ( ) )
{
str + = L " \" SETTINGSPATH \" " ;
g_SettingsFile = PluginBridge ( L " getvariable " , str . c_str ( ) ) ;
g_SettingsFile + = L " Plugins.ini " ;
}
2011-05-21 18:17:37 +00:00
}
UINT maxValue = 0 ;
MeasureData * data = new MeasureData ;
2011-05-31 13:15:53 +00:00
2011-05-21 18:17:37 +00:00
// Read settings from the ini-file
LPCTSTR str = ReadConfigString ( section , L " PlayerName " , NULL ) ;
if ( str )
{
if ( str [ 0 ] = = L ' [ ' )
{
int len = wcslen ( str ) - 2 ;
if ( len > 0 )
{
2011-05-31 13:15:53 +00:00
MeasureMap : : iterator it = g_Values . begin ( ) ;
for ( ; it ! = g_Values . end ( ) ; + + it )
2011-05-21 18:17:37 +00:00
{
if ( wcsncmp ( & str [ 1 ] , it - > second - > section . c_str ( ) , len ) = = 0 & &
wcscmp ( iniFile , it - > second - > iniFile . c_str ( ) ) = = 0 )
{
// Use same player instance as pointed section
data - > player = it - > second - > player ;
}
}
if ( ! data - > player )
{
2011-05-22 14:45:54 +00:00
std : : wstring error = L " NowPlayingPlugin: PlayerName= " ;
2011-05-21 18:17:37 +00:00
error + = str ;
2011-05-22 14:45:54 +00:00
error + = L " in section [ " ;
error + = section ;
error + = L " ] does not exist. " ;
2011-05-21 18:17:37 +00:00
LSLog ( LOG_WARNING , L " Rainmeter " , error . c_str ( ) ) ;
return maxValue ;
}
}
}
else
{
data - > section = section ;
data - > iniFile = iniFile ;
if ( _wcsicmp ( L " AIMP " , str ) = = 0 )
{
if ( ! g_AIMP )
{
2011-05-25 14:04:52 +00:00
g_AIMP = new CPlayerAIMP ( ) ;
2011-05-21 18:17:37 +00:00
}
data - > player = g_AIMP ;
}
2011-06-12 10:05:37 +00:00
else if ( _wcsicmp ( L " CAD " , str ) = = 0 )
{
if ( ! g_CAD )
{
g_CAD = new CPlayerCAD ( ) ;
}
data - > player = g_CAD ;
}
2011-05-25 14:04:52 +00:00
else if ( _wcsicmp ( L " foobar2000 " , str ) = = 0 )
2011-05-24 18:20:35 +00:00
{
2011-05-25 14:04:52 +00:00
if ( ! g_Foobar )
2011-05-24 18:20:35 +00:00
{
2011-05-25 14:04:52 +00:00
g_Foobar = new CPlayerFoobar ( ) ;
2011-05-24 18:20:35 +00:00
}
2011-05-25 14:04:52 +00:00
data - > player = g_Foobar ;
2011-05-24 18:20:35 +00:00
}
2011-05-21 18:17:37 +00:00
else if ( _wcsicmp ( L " iTunes " , str ) = = 0 )
{
if ( ! g_iTunes )
{
2011-05-25 14:04:52 +00:00
g_iTunes = new CPlayerITunes ( ) ;
2011-05-21 18:17:37 +00:00
}
data - > player = g_iTunes ;
}
2011-05-28 07:50:33 +00:00
else if ( _wcsicmp ( L " MediaMonkey " , str ) = = 0 )
{
if ( ! g_Winamp )
{
g_Winamp = new CPlayerWinamp ( WA_MEDIAMONKEY ) ;
}
data - > player = g_Winamp ;
}
2011-05-21 18:17:37 +00:00
else if ( _wcsicmp ( L " Spotify " , str ) = = 0 )
{
if ( ! g_Spotify )
{
2011-05-25 14:04:52 +00:00
g_Spotify = new CPlayerSpotify ( ) ;
2011-05-21 18:17:37 +00:00
}
data - > player = g_Spotify ;
}
2011-05-28 07:50:33 +00:00
else if ( _wcsicmp ( L " WinAmp " , str ) = = 0 )
2011-05-21 18:17:37 +00:00
{
if ( ! g_Winamp )
{
2011-05-28 07:50:33 +00:00
g_Winamp = new CPlayerWinamp ( WA_WINAMP ) ;
2011-05-21 18:17:37 +00:00
}
data - > player = g_Winamp ;
}
2011-06-12 10:05:37 +00:00
else if ( _wcsicmp ( L " WLM " , str ) = = 0 )
{
if ( ! g_WLM )
{
g_WLM = new CPlayerWLM ( ) ;
}
data - > player = g_WLM ;
}
2011-05-21 18:17:37 +00:00
else if ( _wcsicmp ( L " WMP " , str ) = = 0 )
{
if ( ! g_WMP )
{
2011-05-25 14:04:52 +00:00
g_WMP = new CPlayerWMP ( ) ;
2011-05-21 18:17:37 +00:00
}
data - > player = g_WMP ;
}
else
{
2011-06-12 10:05:37 +00:00
if ( _wcsicmp ( L " MusicBee " , str ) = = 0 )
{
// TODO: Remove this in a few weeks (left here for MusicBee backwards compatibility)
MessageBox ( NULL , L " Due to some internal changes in the NowPlaying plugin, PlayerName=MusicBee is not valid any longer. \n \n Please edit the skin and change to PlayerName=CAD to continue use with MusicBee. " , L " NowPlaying " , MB_OK | MB_ICONINFORMATION | MB_TOPMOST ) ;
}
2011-05-22 11:05:23 +00:00
std : : wstring error = L " NowPlayingPlugin: PlayerName= " ;
2011-05-21 18:17:37 +00:00
error + = str ;
2011-05-22 14:45:54 +00:00
error + = L " in section [ " ;
2011-05-21 18:17:37 +00:00
error + = section ;
2011-05-22 14:45:54 +00:00
error + = L " ] is not valid. " ;
2011-05-22 11:05:23 +00:00
LSLog ( LOG_ERROR , L " Rainmeter " , error . c_str ( ) ) ;
2011-05-21 18:17:37 +00:00
delete data ;
return maxValue ;
}
str = ReadConfigString ( section , L " PlayerPath " , NULL ) ;
if ( str & & * str )
{
data - > player - > SetPlayerPath ( str ) ;
}
str = ReadConfigString ( section , L " DisableLeadingZero " , NULL ) ;
if ( str & & * str )
{
g_DisableLeazingZero = ( 1 = = _wtoi ( str ) ) ;
}
str = ReadConfigString ( section , L " TrackChangeAction " , NULL ) ;
if ( str & & * str )
{
data - > player - > SetTrackChangeAction ( str ) ;
}
}
}
str = ReadConfigString ( section , L " PlayerType " , NULL ) ;
if ( str )
{
if ( _wcsicmp ( L " ARTIST " , str ) = = 0 )
{
data - > measure = MEASURE_ARTIST ;
}
else if ( _wcsicmp ( L " TITLE " , str ) = = 0 )
{
data - > measure = MEASURE_TITLE ;
}
else if ( _wcsicmp ( L " ALBUM " , str ) = = 0 )
{
data - > measure = MEASURE_ALBUM ;
}
else if ( _wcsicmp ( L " COVER " , str ) = = 0 )
{
data - > measure = MEASURE_COVER ;
}
else if ( _wcsicmp ( L " DURATION " , str ) = = 0 )
{
data - > measure = MEASURE_DURATION ;
}
else if ( _wcsicmp ( L " POSITION " , str ) = = 0 )
{
data - > measure = MEASURE_POSITION ;
}
else if ( _wcsicmp ( L " PROGRESS " , str ) = = 0 )
{
data - > measure = MEASURE_PROGRESS ;
maxValue = 100 ;
}
else if ( _wcsicmp ( L " RATING " , str ) = = 0 )
{
data - > measure = MEASURE_RATING ;
maxValue = 5 ;
}
else if ( _wcsicmp ( L " STATE " , str ) = = 0 )
{
data - > measure = MEASURE_STATE ;
}
else if ( _wcsicmp ( L " VOLUME " , str ) = = 0 )
{
data - > measure = MEASURE_VOLUME ;
maxValue = 100 ;
}
2011-05-22 08:06:43 +00:00
else if ( _wcsicmp ( L " FILE " , str ) = = 0 )
{
data - > measure = MEASURE_FILE ;
}
2011-05-21 18:17:37 +00:00
}
data - > player - > AddInstance ( data - > measure ) ;
g_Values [ id ] = data ;
return maxValue ;
}
/*
* * Finalize
* *
* * Called when the measure is destroyed ( during refresh / quit ) .
* *
*/
void Finalize ( HMODULE instance , UINT id )
{
MeasureMap : : iterator i = g_Values . find ( id ) ;
if ( i ! = g_Values . end ( ) )
{
( * i ) . second - > player - > RemoveInstance ( ) ;
delete ( * i ) . second ;
g_Values . erase ( i ) ;
}
}
/*
* * Update
* *
* * Called on each update .
* *
*/
UINT Update ( UINT id )
{
MeasureMap : : iterator i = g_Values . find ( id ) ;
if ( i ! = g_Values . end ( ) )
{
if ( ! ( * i ) . second - > section . empty ( ) )
{
// Only allow main measure to update
( * i ) . second - > player - > UpdateData ( ) ;
}
2011-05-22 08:06:43 +00:00
2011-05-21 18:17:37 +00:00
CPlayer * player = ( * i ) . second - > player ;
switch ( ( * i ) . second - > measure )
{
case MEASURE_DURATION :
return player - > GetDuration ( ) ;
case MEASURE_POSITION :
return player - > GetPosition ( ) ;
case MEASURE_PROGRESS :
if ( player - > GetDuration ( ) )
{
return ( player - > GetPosition ( ) * 100 ) / player - > GetDuration ( ) ;
}
return 0 ;
2011-05-22 08:06:43 +00:00
case MEASURE_RATING :
return player - > GetRating ( ) ;
2011-05-21 18:17:37 +00:00
case MEASURE_STATE :
return ( int ) player - > GetState ( ) ;
case MEASURE_VOLUME :
return player - > GetVolume ( ) ;
}
2011-05-22 11:05:23 +00:00
return 0 ;
2011-05-21 18:17:37 +00:00
}
2011-05-22 11:05:23 +00:00
return 1 ;
2011-05-21 18:17:37 +00:00
}
/*
* * GetString
* *
* * Called when a string value is needed .
* *
*/
LPCTSTR GetString ( UINT id , UINT flags )
{
MeasureMap : : iterator i = g_Values . find ( id ) ;
if ( i ! = g_Values . end ( ) )
{
CPlayer * player = ( * i ) . second - > player ;
static WCHAR buffer [ 32 ] ;
switch ( ( * i ) . second - > measure )
{
case MEASURE_ARTIST :
return player - > GetArtist ( ) ;
case MEASURE_TITLE :
return player - > GetTitle ( ) ;
case MEASURE_ALBUM :
return player - > GetAlbum ( ) ;
case MEASURE_COVER :
return player - > GetCoverPath ( ) ;
case MEASURE_DURATION :
SecondsToTime ( player - > GetDuration ( ) , buffer ) ;
return buffer ;
case MEASURE_POSITION :
SecondsToTime ( player - > GetPosition ( ) , buffer ) ;
return buffer ;
case MEASURE_PROGRESS :
if ( player - > GetDuration ( ) )
{
2011-06-09 09:47:55 +00:00
int res = ( player - > GetPosition ( ) * 100 ) / player - > GetDuration ( ) ;
2011-05-21 18:17:37 +00:00
_itow ( res , buffer , 10 ) ;
return buffer ;
}
return L " 0 " ;
2011-05-22 08:06:43 +00:00
case MEASURE_RATING :
_itow ( player - > GetRating ( ) , buffer , 10 ) ;
return buffer ;
case MEASURE_STATE :
_itow ( player - > GetState ( ) , buffer , 10 ) ;
return buffer ;
case MEASURE_VOLUME :
_itow ( player - > GetVolume ( ) , buffer , 10 ) ;
return buffer ;
case MEASURE_FILE :
return player - > GetFilePath ( ) ;
2011-05-21 18:17:37 +00:00
}
}
else
{
// For invalid PlayerName=
return L " 0 " ;
}
return L " " ;
}
/*
* * ExecuteBang
* *
* * Called when a ! RainmeterPluginBang is executed .
* *
*/
void ExecuteBang ( LPCTSTR bang , UINT id )
{
MeasureMap : : iterator i = g_Values . find ( id ) ;
if ( i ! = g_Values . end ( ) )
{
CPlayer * player = ( * i ) . second - > player ;
2011-06-09 09:47:55 +00:00
if ( _wcsicmp ( bang , L " Pause " ) = = 0 )
{
player - > Pause ( ) ;
}
else if ( _wcsicmp ( bang , L " Play " ) = = 0 )
2011-05-21 18:17:37 +00:00
{
player - > Play ( ) ;
}
else if ( _wcsicmp ( bang , L " PlayPause " ) = = 0 )
{
player - > PlayPause ( ) ;
}
else if ( _wcsicmp ( bang , L " Stop " ) = = 0 )
{
player - > Stop ( ) ;
}
else if ( _wcsicmp ( bang , L " Next " ) = = 0 )
{
player - > Next ( ) ;
}
else if ( _wcsicmp ( bang , L " Previous " ) = = 0 )
{
player - > Previous ( ) ;
}
else if ( _wcsicmp ( bang , L " ClosePlayer " ) = = 0 )
{
player - > ClosePlayer ( ) ;
}
else if ( _wcsicmp ( bang , L " OpenPlayer " ) = = 0 )
{
player - > OpenPlayer ( ) ;
}
else if ( _wcsicmp ( bang , L " TogglePlayer " ) = = 0 )
{
player - > TogglePlayer ( ) ;
}
else
{
LPCTSTR arg = wcschr ( bang , L ' ' ) ;
if ( + + arg ) // Skip the space
{
2011-06-09 09:47:55 +00:00
if ( wcsnicmp ( bang , L " SetPosition " , 11 ) = = 0 )
{
int position = ( _wtoi ( arg ) * player - > GetDuration ( ) ) / 100 ;
if ( arg [ 0 ] = = L ' + ' | | arg [ 0 ] = = L ' - ' )
{
position + = player - > GetPosition ( ) ;
}
player - > SetPosition ( position ) ;
}
else if ( wcsnicmp ( bang , L " SetRating " , 9 ) = = 0 )
2011-05-21 18:17:37 +00:00
{
player - > SetRating ( _wtoi ( arg ) ) ;
}
else if ( wcsnicmp ( bang , L " SetVolume " , 9 ) = = 0 )
{
2011-06-09 09:47:55 +00:00
int volume = _wtoi ( arg ) ;
2011-05-21 18:17:37 +00:00
if ( arg [ 0 ] = = L ' + ' | | arg [ 0 ] = = L ' - ' )
{
2011-06-09 09:47:55 +00:00
// Relative to current volume
volume + = player - > GetVolume ( ) ;
2011-05-21 18:17:37 +00:00
}
2011-06-09 09:47:55 +00:00
player - > SetVolume ( volume ) ;
2011-05-21 18:17:37 +00:00
}
else
{
LSLog ( LOG_WARNING , L " Rainmeter " , L " NowPlayingPlugin: Unknown bang! " ) ;
}
}
}
}
}
/*
* * GetPluginVersion
* *
* * Returns the version number of the plugin .
* *
*/
UINT GetPluginVersion ( )
{
// Major * 1000 + Minor
2011-06-12 10:05:37 +00:00
return 1001 ;
2011-05-21 18:17:37 +00:00
}
/*
* * GetPluginAuthor
* *
* * Returns the author of the plugin for the About dialog .
* *
*/
LPCTSTR GetPluginAuthor ( )
{
return L " Birunthan Mohanathas (www.poiru.net) " ;
}