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 "PlayerSpotify.h"
|
|
|
|
|
|
2011-06-19 14:58:48 +00:00
|
|
|
|
CPlayer* CPlayerSpotify::c_Player = NULL;
|
2011-05-22 14:45:54 +00:00
|
|
|
|
|
2011-05-21 18:17:37 +00:00
|
|
|
|
/*
|
|
|
|
|
** CPlayerSpotify
|
|
|
|
|
**
|
|
|
|
|
** Constructor.
|
|
|
|
|
**
|
|
|
|
|
*/
|
2011-05-22 08:06:43 +00:00
|
|
|
|
CPlayerSpotify::CPlayerSpotify() : CPlayer(),
|
2011-05-21 18:17:37 +00:00
|
|
|
|
m_Window()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** ~CPlayerSpotify
|
|
|
|
|
**
|
|
|
|
|
** Destructor.
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
CPlayerSpotify::~CPlayerSpotify()
|
|
|
|
|
{
|
2011-06-19 14:58:48 +00:00
|
|
|
|
c_Player = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Create
|
|
|
|
|
**
|
|
|
|
|
** Creates a shared class object.
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
CPlayer* CPlayerSpotify::Create()
|
|
|
|
|
{
|
|
|
|
|
if (!c_Player)
|
|
|
|
|
{
|
|
|
|
|
c_Player = new CPlayerSpotify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c_Player;
|
2011-05-21 18:17:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2011-06-17 19:07:06 +00:00
|
|
|
|
** CheckWindow
|
2011-05-21 18:17:37 +00:00
|
|
|
|
**
|
2011-06-17 19:07:06 +00:00
|
|
|
|
** Try to find Spotify periodically.
|
2011-05-21 18:17:37 +00:00
|
|
|
|
**
|
|
|
|
|
*/
|
2011-06-17 19:07:06 +00:00
|
|
|
|
bool CPlayerSpotify::CheckWindow()
|
2011-05-21 18:17:37 +00:00
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
|
static DWORD oldTime = 0;
|
|
|
|
|
DWORD time = GetTickCount();
|
|
|
|
|
|
|
|
|
|
// Try to find Spotify window every 5 seconds
|
|
|
|
|
if (time - oldTime > 5000)
|
2011-05-21 18:17:37 +00:00
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
|
oldTime = time;
|
|
|
|
|
|
|
|
|
|
m_Window = FindWindow(L"SpotifyMainWindow", NULL);
|
|
|
|
|
if (m_Window)
|
|
|
|
|
{
|
|
|
|
|
m_Initialized = true;
|
|
|
|
|
}
|
2011-05-21 18:17:37 +00:00
|
|
|
|
}
|
2011-06-17 19:07:06 +00:00
|
|
|
|
|
|
|
|
|
return m_Initialized;
|
2011-05-21 18:17:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** UpdateData
|
|
|
|
|
**
|
|
|
|
|
** Called during each update of the main measure.
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
void CPlayerSpotify::UpdateData()
|
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
|
if (m_Initialized || CheckWindow())
|
2011-05-21 18:17:37 +00:00
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
|
// Parse title and artist from window title
|
2011-05-21 18:17:37 +00:00
|
|
|
|
WCHAR buffer[256];
|
2011-06-17 19:07:06 +00:00
|
|
|
|
if (GetWindowText(m_Window, buffer, 256) > 10)
|
2011-05-21 18:17:37 +00:00
|
|
|
|
{
|
2011-07-14 10:12:02 +00:00
|
|
|
|
std::wstring title = &buffer[10]; // Skip "Spotify - "
|
2011-05-21 18:17:37 +00:00
|
|
|
|
|
2011-06-17 19:07:06 +00:00
|
|
|
|
std::wstring::size_type pos = title.find(L" <20> ");
|
|
|
|
|
if (pos != std::wstring::npos)
|
2011-05-21 18:17:37 +00:00
|
|
|
|
{
|
2011-07-14 10:12:02 +00:00
|
|
|
|
std::wstring artist(title, 0, pos);
|
|
|
|
|
pos += 3; // Skip " - "
|
|
|
|
|
std::wstring track(title, pos);
|
2011-06-17 19:07:06 +00:00
|
|
|
|
|
2011-07-14 10:12:02 +00:00
|
|
|
|
if (track != m_Title || artist != m_Artist)
|
2011-06-17 19:07:06 +00:00
|
|
|
|
{
|
2011-07-14 10:12:02 +00:00
|
|
|
|
m_State = PLAYER_PLAYING;
|
2011-06-17 19:07:06 +00:00
|
|
|
|
m_Title = track;
|
|
|
|
|
m_Artist = artist;
|
|
|
|
|
++m_TrackCount;
|
2011-06-19 14:58:48 +00:00
|
|
|
|
|
2011-09-08 12:42:03 +00:00
|
|
|
|
if (m_Measures & MEASURE_LYRICS)
|
2011-06-19 14:58:48 +00:00
|
|
|
|
{
|
|
|
|
|
FindLyrics();
|
|
|
|
|
}
|
2011-06-17 19:07:06 +00:00
|
|
|
|
}
|
|
|
|
|
return;
|
2011-05-21 18:17:37 +00:00
|
|
|
|
}
|
2011-06-17 19:07:06 +00:00
|
|
|
|
}
|
|
|
|
|
else if (IsWindow(m_Window))
|
|
|
|
|
{
|
|
|
|
|
m_State = PLAYER_PAUSED;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ClearData();
|
|
|
|
|
m_Initialized = false;
|
2011-05-21 18:17:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2011-06-17 19:07:06 +00:00
|
|
|
|
** Play
|
2011-05-21 18:17:37 +00:00
|
|
|
|
**
|
2011-06-17 19:07:06 +00:00
|
|
|
|
** Handles the Play bang.
|
2011-05-21 18:17:37 +00:00
|
|
|
|
**
|
|
|
|
|
*/
|
2011-06-17 19:07:06 +00:00
|
|
|
|
void CPlayerSpotify::Play()
|
2011-05-21 18:17:37 +00:00
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
|
SendMessage(m_Window, WM_APPCOMMAND, 0, SPOTIFY_PLAYPAUSE);
|
2011-05-21 18:17:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Stop
|
|
|
|
|
**
|
|
|
|
|
** Handles the Stop bang.
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
void CPlayerSpotify::Stop()
|
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
|
SendMessage(m_Window, WM_APPCOMMAND, 0, SPOTIFY_STOP);
|
2011-05-21 18:17:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Next
|
|
|
|
|
**
|
|
|
|
|
** Handles the Next bang.
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
void CPlayerSpotify::Next()
|
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
|
SendMessage(m_Window, WM_APPCOMMAND, 0, SPOTIFY_NEXT);
|
2011-05-21 18:17:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** Previous
|
|
|
|
|
**
|
|
|
|
|
** Handles the Previous bang.
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
void CPlayerSpotify::Previous()
|
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
|
SendMessage(m_Window, WM_APPCOMMAND, 0, SPOTIFY_PREV);
|
2011-05-21 18:17:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** ClosePlayer
|
|
|
|
|
**
|
|
|
|
|
** Handles the ClosePlayer bang.
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
void CPlayerSpotify::ClosePlayer()
|
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
|
// A little harsh...
|
|
|
|
|
DWORD pID;
|
|
|
|
|
GetWindowThreadProcessId(m_Window, &pID);
|
|
|
|
|
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pID);
|
|
|
|
|
if (hProcess)
|
2011-05-21 18:17:37 +00:00
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
|
TerminateProcess(hProcess, 0);
|
|
|
|
|
CloseHandle(hProcess);
|
2011-05-21 18:17:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** OpenPlayer
|
|
|
|
|
**
|
|
|
|
|
** Handles the OpenPlayer bang.
|
|
|
|
|
**
|
|
|
|
|
*/
|
2011-06-17 19:07:06 +00:00
|
|
|
|
void CPlayerSpotify::OpenPlayer(std::wstring& path)
|
2011-05-21 18:17:37 +00:00
|
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
|
if (!m_Initialized)
|
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
|
|
|
|
{
|
|
|
|
|
// Gotta figure out where Winamp is located at
|
|
|
|
|
HKEY hKey;
|
|
|
|
|
RegOpenKeyEx(HKEY_CLASSES_ROOT,
|
|
|
|
|
L"spotify\\DefaultIcon",
|
|
|
|
|
0,
|
|
|
|
|
KEY_QUERY_VALUE,
|
|
|
|
|
&hKey);
|
|
|
|
|
|
|
|
|
|
DWORD size = 512;
|
|
|
|
|
WCHAR* data = new WCHAR[size];
|
|
|
|
|
DWORD type = 0;
|
|
|
|
|
|
|
|
|
|
if (RegQueryValueEx(hKey,
|
|
|
|
|
NULL,
|
|
|
|
|
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.erase(0, 1); // Get rid of the leading quote
|
|
|
|
|
path.resize(path.length() - 3); // And the ",0 at the end
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Already active, restore the window
|
|
|
|
|
ShowWindow(m_Window, SW_SHOWNORMAL);
|
|
|
|
|
BringWindowToTop(m_Window);
|
|
|
|
|
}
|
|
|
|
|
}
|