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 "PlayerAIMP.h"
|
2011-05-24 18:20:35 +00:00
|
|
|
#include "AIMP/aimp2_sdk.h"
|
|
|
|
#include "Winamp/wa_ipc.h"
|
2011-05-21 18:17:37 +00:00
|
|
|
|
2011-06-19 14:58:48 +00:00
|
|
|
CPlayer* CPlayerAIMP::c_Player = NULL;
|
2011-05-22 14:45:54 +00:00
|
|
|
|
2011-05-21 18:17:37 +00:00
|
|
|
/*
|
|
|
|
** CPlayerAIMP
|
|
|
|
**
|
|
|
|
** Constructor.
|
|
|
|
**
|
|
|
|
*/
|
2011-05-22 08:06:43 +00:00
|
|
|
CPlayerAIMP::CPlayerAIMP() : CPlayer(),
|
2011-05-21 18:17:37 +00:00
|
|
|
m_Window(),
|
2011-06-19 14:58:48 +00:00
|
|
|
m_WinampWindow(),
|
|
|
|
m_FileMap(),
|
|
|
|
m_FileMapHandle()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** ~CPlayerAIMP
|
|
|
|
**
|
|
|
|
** Destructor.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
CPlayerAIMP::~CPlayerAIMP()
|
|
|
|
{
|
2011-06-19 14:58:48 +00:00
|
|
|
c_Player = NULL;
|
2011-05-21 18:17:37 +00:00
|
|
|
if (m_FileMap) UnmapViewOfFile(m_FileMap);
|
|
|
|
if (m_FileMapHandle) CloseHandle(m_FileMapHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-06-19 14:58:48 +00:00
|
|
|
** Create
|
2011-05-21 18:17:37 +00:00
|
|
|
**
|
2011-06-19 14:58:48 +00:00
|
|
|
** Creates a shared class object.
|
2011-05-21 18:17:37 +00:00
|
|
|
**
|
|
|
|
*/
|
2011-06-19 14:58:48 +00:00
|
|
|
CPlayer* CPlayerAIMP::Create()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-19 14:58:48 +00:00
|
|
|
if (!c_Player)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-19 14:58:48 +00:00
|
|
|
c_Player = new CPlayerAIMP();
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
2011-06-19 14:58:48 +00:00
|
|
|
return c_Player;
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
2011-06-19 14:58:48 +00:00
|
|
|
/*
|
|
|
|
** CheckWindow
|
|
|
|
**
|
|
|
|
** Try to find AIMP periodically.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
bool CPlayerAIMP::CheckWindow()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-19 14:58:48 +00:00
|
|
|
static DWORD oldTime = 0;
|
|
|
|
DWORD time = GetTickCount();
|
|
|
|
|
|
|
|
// Try to find AIMP every 5 seconds
|
|
|
|
if (time - oldTime > 5000)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-19 14:58:48 +00:00
|
|
|
oldTime = time;
|
|
|
|
m_Window = FindWindow(L"AIMP2_RemoteInfo", L"AIMP2_RemoteInfo");
|
2011-05-21 18:17:37 +00:00
|
|
|
|
2011-06-19 14:58:48 +00:00
|
|
|
if (m_Window)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-19 14:58:48 +00:00
|
|
|
m_WinampWindow = FindWindow(L"Winamp v1.x", NULL);
|
2011-05-21 18:17:37 +00:00
|
|
|
|
2011-06-19 14:58:48 +00:00
|
|
|
m_FileMapHandle = OpenFileMapping(FILE_MAP_READ, FALSE, L"AIMP2_RemoteInfo");
|
|
|
|
if (m_FileMapHandle)
|
|
|
|
{
|
|
|
|
m_FileMap = (LPVOID)MapViewOfFile(m_FileMapHandle, FILE_MAP_READ, 0, 0, 2048);
|
|
|
|
if (m_FileMap)
|
|
|
|
{
|
|
|
|
m_Initialized = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LSLog(LOG_ERROR, L"Rainmeter", L"NowPlayingPlugin: Unable to view AIMP file mapping.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LSLog(LOG_ERROR, L"Rainmeter", L"NowPlayingPlugin: AIMP - Unable to access AIMP file mapping.");
|
|
|
|
}
|
|
|
|
}
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
2011-06-19 14:58:48 +00:00
|
|
|
|
|
|
|
return m_Initialized;
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** UpdateData
|
|
|
|
**
|
|
|
|
** Called during each update of the main measure.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CPlayerAIMP::UpdateData()
|
|
|
|
{
|
2011-06-19 14:58:48 +00:00
|
|
|
static long oldFileSize = 0;
|
|
|
|
static UINT oldTitleLen = 0;
|
|
|
|
|
|
|
|
if (!m_Initialized)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-19 14:58:48 +00:00
|
|
|
if (oldTitleLen != 0)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
|
|
|
oldFileSize = 0;
|
|
|
|
oldTitleLen = 0;
|
|
|
|
}
|
2011-06-19 14:58:48 +00:00
|
|
|
|
|
|
|
if (!CheckWindow())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
2011-06-19 14:58:48 +00:00
|
|
|
// If initialized
|
2011-06-17 19:07:06 +00:00
|
|
|
m_State = (PLAYSTATE)SendMessage(m_Window, WM_AIMP_COMMAND, WM_AIMP_STATUS_GET, AIMP_STS_Player);
|
2011-05-21 18:17:37 +00:00
|
|
|
if (m_State == PLAYER_STOPPED)
|
|
|
|
{
|
2011-06-19 14:58:48 +00:00
|
|
|
// Make sure AIMP is still active
|
|
|
|
if (!IsWindow(m_Window))
|
|
|
|
{
|
|
|
|
m_Initialized = false;
|
|
|
|
if (m_FileMap) UnmapViewOfFile(m_FileMap);
|
|
|
|
if (m_FileMapHandle) CloseHandle(m_FileMapHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldTitleLen != 0)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
|
|
|
oldFileSize = 0;
|
|
|
|
oldTitleLen = 0;
|
2011-06-17 19:07:06 +00:00
|
|
|
ClearData();
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
2011-06-19 14:58:48 +00:00
|
|
|
|
|
|
|
// Don't continue if AIMP has quit or is stopped
|
2011-05-21 18:17:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Position = SendMessage(m_Window, WM_AIMP_COMMAND, WM_AIMP_STATUS_GET, AIMP_STS_POS);
|
|
|
|
m_Volume = SendMessage(m_Window, WM_AIMP_COMMAND, WM_AIMP_STATUS_GET, AIMP_STS_VOLUME);
|
|
|
|
|
|
|
|
AIMP2FileInfo* info = (AIMP2FileInfo*)m_FileMap;
|
|
|
|
if (info->cbSizeOf > 0 &&
|
2011-06-19 14:58:48 +00:00
|
|
|
info->nFileSize != oldFileSize || // Avoid reading the same file
|
2011-05-21 18:17:37 +00:00
|
|
|
info->nTitleLen != oldTitleLen)
|
|
|
|
{
|
|
|
|
oldFileSize = info->nFileSize;
|
|
|
|
oldTitleLen = info->nTitleLen;
|
|
|
|
|
|
|
|
// 44 is sizeof(AIMP2FileInfo) / 2 (due to WCHAR being 16-bit).
|
|
|
|
// Written explicitly due to size differences in 32bit/64bit.
|
|
|
|
LPCTSTR stringData = (LPCTSTR)m_FileMap;
|
|
|
|
stringData += 44;
|
|
|
|
|
|
|
|
m_Album.assign(stringData, info->nAlbumLen);
|
|
|
|
|
|
|
|
stringData += info->nAlbumLen;
|
|
|
|
m_Artist.assign(stringData, info->nArtistLen);
|
|
|
|
|
|
|
|
stringData += info->nArtistLen;
|
|
|
|
stringData += info->nDateLen;
|
2011-05-22 08:06:43 +00:00
|
|
|
std::wstring filepath(stringData, info->nFileNameLen);
|
2011-05-22 14:45:54 +00:00
|
|
|
|
|
|
|
stringData += info->nFileNameLen;
|
|
|
|
stringData += info->nGenreLen;
|
|
|
|
m_Title.assign(stringData, info->nTitleLen);
|
|
|
|
|
|
|
|
m_Duration = info->nDuration / 1000;
|
|
|
|
|
|
|
|
// Get rating through the AIMP Winamp API
|
|
|
|
m_Rating = SendMessage(m_WinampWindow, WM_WA_IPC, 0, IPC_GETRATING);
|
|
|
|
|
2011-05-22 08:06:43 +00:00
|
|
|
if (filepath != m_FilePath)
|
|
|
|
{
|
|
|
|
m_FilePath = filepath;
|
2011-06-17 19:07:06 +00:00
|
|
|
++m_TrackCount;
|
2011-05-22 08:06:43 +00:00
|
|
|
|
2011-06-17 19:07:06 +00:00
|
|
|
// Find cover if needed
|
2011-05-22 08:06:43 +00:00
|
|
|
if (m_HasCoverMeasure)
|
|
|
|
{
|
2011-06-19 14:58:48 +00:00
|
|
|
FindCover();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_HasLyricsMeasure)
|
|
|
|
{
|
|
|
|
FindLyrics();
|
2011-05-22 08:06:43 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-09 09:47:55 +00:00
|
|
|
/*
|
|
|
|
** Pause
|
|
|
|
**
|
|
|
|
** Handles the Pause bang.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CPlayerAIMP::Pause()
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
SendMessage(m_Window, WM_AIMP_COMMAND, WM_AIMP_CALLFUNC, AIMP_PAUSE);
|
2011-06-09 09:47:55 +00:00
|
|
|
}
|
|
|
|
|
2011-05-21 18:17:37 +00:00
|
|
|
/*
|
|
|
|
** Play
|
|
|
|
**
|
|
|
|
** Handles the Play bang.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CPlayerAIMP::Play()
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
SendMessage(m_Window, WM_AIMP_COMMAND, WM_AIMP_CALLFUNC, AIMP_PLAY);
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Stop
|
|
|
|
**
|
|
|
|
** Handles the Stop bang.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CPlayerAIMP::Stop()
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
SendMessage(m_Window, WM_AIMP_COMMAND, WM_AIMP_CALLFUNC, AIMP_STOP);
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Next
|
|
|
|
**
|
|
|
|
** Handles the Next bang.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CPlayerAIMP::Next()
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
SendMessage(m_Window, WM_AIMP_COMMAND, WM_AIMP_CALLFUNC, AIMP_NEXT);
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Previous
|
|
|
|
**
|
|
|
|
** Handles the Previous bang.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CPlayerAIMP::Previous()
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
SendMessage(m_Window, WM_AIMP_COMMAND, WM_AIMP_CALLFUNC, AIMP_PREV);
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
2011-06-09 09:47:55 +00:00
|
|
|
/*
|
|
|
|
** SetPosition
|
|
|
|
**
|
|
|
|
** Handles the SetPosition bang.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CPlayerAIMP::SetPosition(int position)
|
|
|
|
{
|
|
|
|
SendMessage(m_Window, WM_AIMP_COMMAND, WM_AIMP_STATUS_SET, MAKELPARAM(position, AIMP_STS_POS));
|
|
|
|
}
|
|
|
|
|
2011-05-21 18:17:37 +00:00
|
|
|
/*
|
|
|
|
** SetRating
|
|
|
|
**
|
|
|
|
** Handles the SetRating bang.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CPlayerAIMP::SetRating(int rating)
|
|
|
|
{
|
|
|
|
// Set rating through the AIMP Winamp API
|
2011-06-17 19:07:06 +00:00
|
|
|
if (m_State != PLAYER_STOPPED)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
|
|
|
if (rating < 0)
|
|
|
|
{
|
|
|
|
rating = 0;
|
|
|
|
}
|
|
|
|
else if (rating > 5)
|
|
|
|
{
|
|
|
|
rating = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
SendMessage(m_WinampWindow, WM_WA_IPC, rating, IPC_SETRATING);
|
|
|
|
m_Rating = rating;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-05-24 18:20:35 +00:00
|
|
|
** SetVolume
|
2011-05-21 18:17:37 +00:00
|
|
|
**
|
|
|
|
** Handles the SetVolume bang.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CPlayerAIMP::SetVolume(int volume)
|
|
|
|
{
|
|
|
|
SendMessage(m_Window, WM_AIMP_COMMAND, WM_AIMP_STATUS_SET, MAKELPARAM(volume, AIMP_STS_VOLUME));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** ClosePlayer
|
|
|
|
**
|
|
|
|
** Handles the ClosePlayer bang.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CPlayerAIMP::ClosePlayer()
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
SendMessage(m_Window, WM_CLOSE, 0, 0);
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** OpenPlayer
|
|
|
|
**
|
|
|
|
** Handles the OpenPlayer bang.
|
|
|
|
**
|
|
|
|
*/
|
2011-06-17 19:07:06 +00:00
|
|
|
void CPlayerAIMP::OpenPlayer(std::wstring& path)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
if (path.empty())
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
|
|
|
// Check for AIMP2 first
|
|
|
|
DWORD size = 512;
|
|
|
|
WCHAR* data = new WCHAR[size];
|
|
|
|
DWORD type = 0;
|
|
|
|
HKEY hKey;
|
|
|
|
|
|
|
|
RegOpenKeyEx(HKEY_LOCAL_MACHINE,
|
|
|
|
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\AIMP2",
|
|
|
|
0,
|
|
|
|
KEY_QUERY_VALUE,
|
|
|
|
&hKey);
|
|
|
|
|
|
|
|
if (RegQueryValueEx(hKey,
|
|
|
|
L"DisplayIcon",
|
|
|
|
NULL,
|
|
|
|
(LPDWORD)&type,
|
|
|
|
(LPBYTE)data,
|
|
|
|
(LPDWORD)&size) == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
if (type == REG_SZ)
|
|
|
|
{
|
|
|
|
ShellExecute(NULL, L"open", data, NULL, NULL, SW_SHOW);
|
2011-06-17 19:07:06 +00:00
|
|
|
path = data;
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Let's try AIMP3
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
RegOpenKeyEx(HKEY_LOCAL_MACHINE,
|
|
|
|
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\AIMP3",
|
|
|
|
0,
|
|
|
|
KEY_QUERY_VALUE,
|
|
|
|
&hKey);
|
|
|
|
|
|
|
|
if (RegQueryValueEx(hKey,
|
|
|
|
L"DisplayIcon",
|
|
|
|
NULL,
|
|
|
|
(LPDWORD)&type,
|
|
|
|
(LPBYTE)data,
|
|
|
|
(LPDWORD)&size) == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
if (type == REG_SZ)
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
path = data;
|
2011-05-21 18:17:37 +00:00
|
|
|
path.resize(path.find_last_of(L'\\') + 1);
|
|
|
|
path += L"AIMP3.exe";
|
|
|
|
ShellExecute(NULL, L"open", path.c_str(), NULL, NULL, SW_SHOW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] data;
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
ShellExecute(NULL, L"open", path.c_str(), NULL, NULL, SW_SHOW);
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
}
|