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"
2011-06-19 14:58:48 +00:00
# include "Internet.h"
2011-05-21 18:17:37 +00:00
# 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-06-17 19:07:06 +00:00
static std : : map < UINT , ChildMeasure * > g_Measures ;
2011-05-21 18:17:37 +00:00
std : : wstring g_CachePath ;
2011-06-12 10:05:37 +00:00
std : : wstring g_SettingsFile ;
2011-05-21 18:17:37 +00:00
/*
* * Initialize
* *
* * Called when the measure is initialized .
* *
*/
UINT Initialize ( HMODULE instance , LPCTSTR iniFile , LPCTSTR section , UINT id )
{
2011-06-17 19:07:06 +00:00
if ( g_Measures . empty ( ) )
2011-05-21 18:17:37 +00:00
{
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-06-17 19:07:06 +00:00
else
{
LSLog ( LOG_ERROR , L " Rainmeter " , L " NowPlayingPlugin: Unable to get path to Plugins.ini. " ) ;
}
2011-06-19 14:58:48 +00:00
CInternet : : Initialize ( ) ;
2011-05-21 18:17:37 +00:00
}
2011-06-17 19:07:06 +00:00
// Data is stored in two structs: ChildMeasure and ParentMeasure. ParentMeasure is created for measures
// with PlayerName=someplayer. ChildMeasure is created for all measures and points to ParentMeasure as
// referenced in PlayerName=[section].
ChildMeasure * child = new ChildMeasure ;
2011-05-21 18:17:37 +00:00
UINT maxValue = 0 ;
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 ' [ ' )
{
2011-06-17 19:07:06 +00:00
// PlayerName starts with [ so use referenced section
2011-05-21 18:17:37 +00:00
int len = wcslen ( str ) - 2 ;
if ( len > 0 )
{
2011-06-17 19:07:06 +00:00
std : : map < UINT , ChildMeasure * > : : iterator it = g_Measures . begin ( ) ;
for ( ; it ! = g_Measures . end ( ) ; + + it )
2011-05-21 18:17:37 +00:00
{
2011-06-17 19:07:06 +00:00
if ( wcsncmp ( & str [ 1 ] , it - > second - > parent - > name . c_str ( ) , len ) = = 0 & &
wcscmp ( iniFile , it - > second - > parent - > iniFile . c_str ( ) ) = = 0 )
2011-05-21 18:17:37 +00:00
{
2011-06-17 19:07:06 +00:00
// Use same ParentMeasure as referenced section
child - > parent = it - > second - > parent ;
+ + child - > parent - > childCount ;
break ;
2011-05-21 18:17:37 +00:00
}
}
2011-06-17 19:07:06 +00:00
if ( ! child - > parent )
2011-05-21 18:17:37 +00:00
{
2011-06-17 19:07:06 +00:00
// The referenced section doesn't exist
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-06-17 19:07:06 +00:00
error + = L " in [ " ;
2011-05-22 14:45:54 +00:00
error + = section ;
error + = L " ] does not exist. " ;
2011-05-21 18:17:37 +00:00
LSLog ( LOG_WARNING , L " Rainmeter " , error . c_str ( ) ) ;
2011-06-17 19:07:06 +00:00
delete child ;
2011-05-21 18:17:37 +00:00
return maxValue ;
}
}
}
else
{
2011-06-17 19:07:06 +00:00
// ParentMeasure is created when PlayerName is an actual player (and not a reference)
ParentMeasure * parent = new ParentMeasure ;
parent - > name = section ;
parent - > iniFile = iniFile ;
2011-05-21 18:17:37 +00:00
if ( _wcsicmp ( L " AIMP " , str ) = = 0 )
{
2011-06-19 14:58:48 +00:00
parent - > player = CPlayerAIMP : : Create ( ) ;
2011-05-21 18:17:37 +00:00
}
2011-06-12 10:05:37 +00:00
else if ( _wcsicmp ( L " CAD " , str ) = = 0 )
{
2011-06-19 14:58:48 +00:00
parent - > player = CPlayerCAD : : Create ( ) ;
2011-06-12 10:05:37 +00:00
}
2011-05-25 14:04:52 +00:00
else if ( _wcsicmp ( L " foobar2000 " , str ) = = 0 )
2011-05-24 18:20:35 +00:00
{
2011-06-19 14:58:48 +00:00
parent - > player = CPlayerFoobar : : Create ( ) ;
2011-05-24 18:20:35 +00:00
}
2011-05-21 18:17:37 +00:00
else if ( _wcsicmp ( L " iTunes " , str ) = = 0 )
{
2011-06-19 14:58:48 +00:00
parent - > player = CPlayerITunes : : Create ( ) ;
2011-05-21 18:17:37 +00:00
}
2011-05-28 07:50:33 +00:00
else if ( _wcsicmp ( L " MediaMonkey " , str ) = = 0 )
{
2011-06-19 14:58:48 +00:00
parent - > player = CPlayerWinamp : : Create ( WA_MEDIAMONKEY ) ;
2011-05-28 07:50:33 +00:00
}
2011-05-21 18:17:37 +00:00
else if ( _wcsicmp ( L " Spotify " , str ) = = 0 )
{
2011-06-19 14:58:48 +00:00
parent - > player = CPlayerSpotify : : Create ( ) ;
2011-05-21 18:17:37 +00:00
}
2011-05-28 07:50:33 +00:00
else if ( _wcsicmp ( L " WinAmp " , str ) = = 0 )
2011-05-21 18:17:37 +00:00
{
2011-06-19 14:58:48 +00:00
parent - > player = CPlayerWinamp : : Create ( WA_WINAMP ) ;
2011-05-21 18:17:37 +00:00
}
2011-06-12 10:05:37 +00:00
else if ( _wcsicmp ( L " WLM " , str ) = = 0 )
{
2011-06-19 14:58:48 +00:00
parent - > player = CPlayerWLM : : Create ( ) ;
2011-06-12 10:05:37 +00:00
}
2011-05-21 18:17:37 +00:00
else if ( _wcsicmp ( L " WMP " , str ) = = 0 )
{
2011-06-19 14:58:48 +00:00
parent - > player = CPlayerWMP : : Create ( ) ;
2011-05-21 18:17:37 +00:00
}
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-06-17 19:07:06 +00:00
delete parent ;
delete child ;
2011-05-21 18:17:37 +00:00
return maxValue ;
}
2011-06-17 19:07:06 +00:00
parent - > id = id ;
parent - > childCount = 1 ;
parent - > player - > AddInstance ( ) ;
parent - > playerPath = ReadConfigString ( section , L " PlayerPath " , L " " ) ;
parent - > trackChangeAction = ReadConfigString ( section , L " TrackChangeAction " , L " " ) ;
2011-05-21 18:17:37 +00:00
2011-06-17 19:07:06 +00:00
if ( ! parent - > trackChangeAction . empty ( ) )
2011-05-21 18:17:37 +00:00
{
2011-06-17 19:07:06 +00:00
// Get window handle to send the bang later on
parent - > window = FindMeterWindow ( parent - > iniFile ) ;
parent - > trackCount = 1 ;
2011-05-21 18:17:37 +00:00
}
2011-06-17 19:07:06 +00:00
str = ReadConfigString ( section , L " DisableLeadingZero " , L " 0 " ) ;
if ( str )
2011-05-21 18:17:37 +00:00
{
2011-06-17 19:07:06 +00:00
parent - > disableLeadingZero = ( 1 = = _wtoi ( str ) ) ;
2011-05-21 18:17:37 +00:00
}
2011-06-17 19:07:06 +00:00
child - > parent = parent ;
2011-05-21 18:17:37 +00:00
}
}
str = ReadConfigString ( section , L " PlayerType " , NULL ) ;
if ( str )
{
if ( _wcsicmp ( L " ARTIST " , str ) = = 0 )
{
2011-06-17 19:07:06 +00:00
child - > type = MEASURE_ARTIST ;
2011-05-21 18:17:37 +00:00
}
else if ( _wcsicmp ( L " TITLE " , str ) = = 0 )
{
2011-06-17 19:07:06 +00:00
child - > type = MEASURE_TITLE ;
2011-05-21 18:17:37 +00:00
}
else if ( _wcsicmp ( L " ALBUM " , str ) = = 0 )
{
2011-06-17 19:07:06 +00:00
child - > type = MEASURE_ALBUM ;
}
else if ( _wcsicmp ( L " LYRICS " , str ) = = 0 )
{
child - > type = MEASURE_LYRICS ;
2011-05-21 18:17:37 +00:00
}
else if ( _wcsicmp ( L " COVER " , str ) = = 0 )
{
2011-06-17 19:07:06 +00:00
child - > type = MEASURE_COVER ;
2011-05-21 18:17:37 +00:00
}
else if ( _wcsicmp ( L " DURATION " , str ) = = 0 )
{
2011-06-17 19:07:06 +00:00
child - > type = MEASURE_DURATION ;
2011-05-21 18:17:37 +00:00
}
else if ( _wcsicmp ( L " POSITION " , str ) = = 0 )
{
2011-06-17 19:07:06 +00:00
child - > type = MEASURE_POSITION ;
2011-05-21 18:17:37 +00:00
}
else if ( _wcsicmp ( L " PROGRESS " , str ) = = 0 )
{
2011-06-17 19:07:06 +00:00
child - > type = MEASURE_PROGRESS ;
2011-05-21 18:17:37 +00:00
maxValue = 100 ;
}
else if ( _wcsicmp ( L " RATING " , str ) = = 0 )
{
2011-06-17 19:07:06 +00:00
child - > type = MEASURE_RATING ;
2011-05-21 18:17:37 +00:00
maxValue = 5 ;
}
else if ( _wcsicmp ( L " STATE " , str ) = = 0 )
{
2011-06-17 19:07:06 +00:00
child - > type = MEASURE_STATE ;
2011-05-21 18:17:37 +00:00
}
else if ( _wcsicmp ( L " VOLUME " , str ) = = 0 )
{
2011-06-17 19:07:06 +00:00
child - > type = MEASURE_VOLUME ;
2011-05-21 18:17:37 +00:00
maxValue = 100 ;
}
2011-05-22 08:06:43 +00:00
else if ( _wcsicmp ( L " FILE " , str ) = = 0 )
{
2011-06-17 19:07:06 +00:00
child - > type = MEASURE_FILE ;
}
else
{
std : : wstring error = L " NowPlayingPlugin: PlayerType= " ;
error + = str ;
error + = L " in section [ " ;
error + = section ;
error + = L " ] is not valid. " ;
LSLog ( LOG_WARNING , L " Rainmeter " , error . c_str ( ) ) ;
2011-05-22 08:06:43 +00:00
}
2011-06-17 19:07:06 +00:00
child - > parent - > player - > AddMeasure ( child - > type ) ;
2011-05-21 18:17:37 +00:00
}
2011-06-17 19:07:06 +00:00
g_Measures [ id ] = child ;
2011-05-21 18:17:37 +00:00
return maxValue ;
}
/*
* * Finalize
* *
* * Called when the measure is destroyed ( during refresh / quit ) .
* *
*/
void Finalize ( HMODULE instance , UINT id )
{
2011-06-17 19:07:06 +00:00
std : : map < UINT , ChildMeasure * > : : iterator i = g_Measures . find ( id ) ;
if ( i ! = g_Measures . end ( ) )
2011-05-21 18:17:37 +00:00
{
2011-06-17 19:07:06 +00:00
ChildMeasure * child = ( * i ) . second ;
ParentMeasure * parent = child - > parent ;
CPlayer * player = parent - > player ;
if ( - - parent - > childCount = = 0 )
{
player - > RemoveInstance ( ) ;
delete parent ;
}
delete child ;
g_Measures . erase ( i ) ;
2011-06-19 14:58:48 +00:00
if ( g_Measures . empty ( ) )
{
CInternet : : Finalize ( ) ;
}
2011-05-21 18:17:37 +00:00
}
}
/*
2011-06-17 19:07:06 +00:00
2011-05-21 18:17:37 +00:00
* * Update
* *
* * Called on each update .
* *
*/
UINT Update ( UINT id )
{
2011-06-17 19:07:06 +00:00
std : : map < UINT , ChildMeasure * > : : iterator i = g_Measures . find ( id ) ;
if ( i ! = g_Measures . end ( ) )
2011-05-21 18:17:37 +00:00
{
2011-06-17 19:07:06 +00:00
ChildMeasure * child = ( * i ) . second ;
ParentMeasure * parent = child - > parent ;
CPlayer * player = parent - > player ;
if ( parent - > id = = id )
2011-05-21 18:17:37 +00:00
{
2011-06-17 19:07:06 +00:00
player - > UpdateMeasure ( ) ;
2011-05-22 08:06:43 +00:00
2011-06-17 19:07:06 +00:00
// Execute TrackChangeAction= if necessary
if ( ! parent - > trackChangeAction . empty ( ) & &
parent - > trackCount ! = player - > GetTrackCount ( ) )
{
ExecuteCommand ( parent - > trackChangeAction , parent - > window ) ;
parent - > trackCount = player - > GetTrackCount ( ) ;
}
}
2011-05-21 18:17:37 +00:00
2011-06-17 19:07:06 +00:00
switch ( child - > type )
2011-05-21 18:17:37 +00:00
{
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 :
2011-06-17 19:07:06 +00:00
return ( UINT ) player - > GetState ( ) ;
2011-05-21 18:17:37 +00:00
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 )
{
2011-06-17 19:07:06 +00:00
std : : map < UINT , ChildMeasure * > : : iterator i = g_Measures . find ( id ) ;
if ( i ! = g_Measures . end ( ) )
2011-05-21 18:17:37 +00:00
{
2011-06-17 19:07:06 +00:00
ChildMeasure * child = ( * i ) . second ;
ParentMeasure * parent = child - > parent ;
CPlayer * player = parent - > player ;
2011-05-21 18:17:37 +00:00
static WCHAR buffer [ 32 ] ;
2011-06-17 19:07:06 +00:00
switch ( child - > type )
2011-05-21 18:17:37 +00:00
{
case MEASURE_ARTIST :
return player - > GetArtist ( ) ;
case MEASURE_TITLE :
return player - > GetTitle ( ) ;
case MEASURE_ALBUM :
return player - > GetAlbum ( ) ;
2011-06-19 14:58:48 +00:00
case MEASURE_LYRICS :
return player - > GetLyrics ( ) ;
2011-05-21 18:17:37 +00:00
case MEASURE_COVER :
return player - > GetCoverPath ( ) ;
case MEASURE_DURATION :
2011-06-17 19:07:06 +00:00
SecondsToTime ( player - > GetDuration ( ) , parent - > disableLeadingZero , buffer ) ;
2011-05-21 18:17:37 +00:00
return buffer ;
case MEASURE_POSITION :
2011-06-17 19:07:06 +00:00
SecondsToTime ( player - > GetPosition ( ) , parent - > disableLeadingZero , buffer ) ;
2011-05-21 18:17:37 +00:00
return buffer ;
case MEASURE_PROGRESS :
2011-06-17 19:07:06 +00:00
_itow ( player - > GetDuration ( ) ? ( ( player - > GetPosition ( ) * 100 ) / player - > GetDuration ( ) ) : 0 , buffer , 10 ) ;
return buffer ;
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
{
2011-06-17 19:07:06 +00:00
return L " Error: Invalid player. " ;
2011-05-21 18:17:37 +00:00
}
return L " " ;
}
/*
* * ExecuteBang
* *
* * Called when a ! RainmeterPluginBang is executed .
* *
*/
void ExecuteBang ( LPCTSTR bang , UINT id )
{
2011-06-17 19:07:06 +00:00
std : : map < UINT , ChildMeasure * > : : iterator i = g_Measures . find ( id ) ;
if ( i ! = g_Measures . end ( ) )
2011-05-21 18:17:37 +00:00
{
2011-06-17 19:07:06 +00:00
ChildMeasure * child = ( * i ) . second ;
ParentMeasure * parent = child - > parent ;
CPlayer * player = parent - > player ;
2011-05-21 18:17:37 +00:00
2011-06-17 19:07:06 +00:00
if ( ! player - > IsInitialized ( ) )
{
if ( _wcsicmp ( bang , L " OpenPlayer " ) = = 0 | | _wcsicmp ( bang , L " TogglePlayer " ) = = 0 )
{
player - > OpenPlayer ( parent - > playerPath ) ;
}
}
else if ( _wcsicmp ( bang , L " Pause " ) = = 0 )
2011-06-09 09:47:55 +00:00
{
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 )
{
2011-06-17 19:07:06 +00:00
( player - > GetState ( ) ! = PLAYER_PLAYING ) ? player - > Play ( ) : player - > Pause ( ) ;
2011-05-21 18:17:37 +00:00
}
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 ( ) ;
}
2011-06-17 19:07:06 +00:00
else if ( _wcsicmp ( bang , L " ClosePlayer " ) = = 0 | | _wcsicmp ( bang , L " TogglePlayer " ) = = 0 )
2011-05-21 18:17:37 +00:00
{
player - > ClosePlayer ( ) ;
}
else
{
LPCTSTR arg = wcschr ( bang , L ' ' ) ;
2011-06-17 19:07:06 +00:00
if ( arg )
2011-05-21 18:17:37 +00:00
{
2011-06-17 19:07:06 +00:00
+ + 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) " ;
}
2011-06-17 19:07:06 +00:00
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 ( 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 ) ;
}
}
void ExecuteCommand ( std : : wstring & command , HWND wnd )
{
COPYDATASTRUCT cds ;
cds . dwData = 1 ;
cds . cbData = ( DWORD ) ( command . size ( ) + 1 ) * sizeof ( WCHAR ) ;
cds . lpData = ( void * ) command . c_str ( ) ;
// Send bang to the Rainmeter window
SendMessage ( wnd , WM_COPYDATA , ( WPARAM ) NULL , ( LPARAM ) & cds ) ;
}
bool BelongToSameProcess ( HWND wnd )
{
DWORD procId = 0 ;
GetWindowThreadProcessId ( wnd , & procId ) ;
return ( procId = = GetCurrentProcessId ( ) ) ;
}
HWND FindMeterWindow ( HWND parent )
{
HWND wnd = NULL ;
while ( wnd = FindWindowEx ( parent , wnd , L " RainmeterMeterWindow " , NULL ) )
{
if ( BelongToSameProcess ( wnd ) )
{
return wnd ;
}
}
return NULL ;
}
HWND FindMeterWindow ( const std : : wstring & iniFile )
{
std : : wstring str = PluginBridge ( L " getconfig " , iniFile . c_str ( ) ) ;
if ( ! str . empty ( ) )
{
str = PluginBridge ( L " getwindow " , str . c_str ( ) ) ;
if ( str ! = L " error " )
{
return ( HWND ) UlongToPtr ( wcstoul ( str . c_str ( ) , NULL , 10 ) ) ;
}
}
return FindMeterWindow ( NULL ) ; // Use old way to find
}