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
|
2012-01-23 06:36:15 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2011-05-21 18:17:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "PlayerITunes.h"
|
|
|
|
|
2013-05-31 14:28:39 +00:00
|
|
|
Player* PlayerITunes::c_Player = nullptr;
|
2011-07-17 10:36:04 +00:00
|
|
|
extern HINSTANCE g_Instance;
|
2011-05-21 18:17:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Constructor.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
PlayerITunes::CEventHandler::CEventHandler(PlayerITunes* player) :
|
2011-05-31 13:15:53 +00:00
|
|
|
m_Player(player),
|
|
|
|
m_RefCount(),
|
|
|
|
m_ConnectionPoint(),
|
|
|
|
m_ConnectionCookie()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-05-31 13:15:53 +00:00
|
|
|
IConnectionPointContainer* icpc;
|
|
|
|
m_Player->m_iTunes->QueryInterface(IID_IConnectionPointContainer, (void**)&icpc);
|
|
|
|
icpc->FindConnectionPoint(DIID__IiTunesEvents, &m_ConnectionPoint);
|
|
|
|
m_ConnectionPoint->Advise(this, &m_ConnectionCookie);
|
|
|
|
icpc->Release();
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Destructor.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
PlayerITunes::CEventHandler::~CEventHandler()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-05-31 13:15:53 +00:00
|
|
|
if (m_ConnectionPoint)
|
|
|
|
{
|
|
|
|
m_ConnectionPoint->Unadvise(m_ConnectionCookie);
|
|
|
|
m_ConnectionPoint->Release();
|
|
|
|
}
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
HRESULT STDMETHODCALLTYPE PlayerITunes::CEventHandler::QueryInterface(REFIID iid, void** ppvObject)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
|
|
|
if (iid == IID_IUnknown || iid == IID_IUnknown || iid == DIID__IiTunesEvents)
|
|
|
|
{
|
|
|
|
++m_RefCount;
|
|
|
|
*ppvObject = this;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
ULONG STDMETHODCALLTYPE PlayerITunes::CEventHandler::AddRef()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-05-31 13:15:53 +00:00
|
|
|
return ++m_RefCount;
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
ULONG STDMETHODCALLTYPE PlayerITunes::CEventHandler::Release()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-05-31 13:15:53 +00:00
|
|
|
return --m_RefCount;
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
HRESULT STDMETHODCALLTYPE PlayerITunes::CEventHandler::Invoke(DISPID dispidMember, REFIID, LCID, WORD, DISPPARAMS* dispParams, VARIANT*, EXCEPINFO*, UINT*)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
|
|
|
switch (dispidMember)
|
|
|
|
{
|
2011-11-19 11:44:04 +00:00
|
|
|
case ITEventDatabaseChanged:
|
|
|
|
m_Player->OnDatabaseChange();
|
|
|
|
break;
|
|
|
|
|
2011-05-21 18:17:37 +00:00
|
|
|
case ITEventPlayerPlay:
|
2011-05-31 13:15:53 +00:00
|
|
|
m_Player->OnStateChange(true);
|
|
|
|
m_Player->OnTrackChange();
|
|
|
|
break;
|
2011-05-21 18:17:37 +00:00
|
|
|
|
|
|
|
case ITEventPlayerStop:
|
2011-05-31 13:15:53 +00:00
|
|
|
m_Player->OnStateChange(false);
|
2011-05-21 18:17:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ITEventPlayerPlayingTrackChanged:
|
2011-05-31 13:15:53 +00:00
|
|
|
m_Player->OnTrackChange();
|
2011-05-21 18:17:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ITEventSoundVolumeChanged:
|
2011-05-31 13:15:53 +00:00
|
|
|
m_Player->OnVolumeChange(dispParams->rgvarg[0].intVal);
|
2011-05-21 18:17:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ITEventAboutToPromptUserToQuit:
|
2011-07-17 10:36:04 +00:00
|
|
|
PostMessage(m_Player->m_CallbackWindow, WM_USER, ITEventAboutToPromptUserToQuit, 0);
|
2013-05-31 14:28:39 +00:00
|
|
|
SetTimer(m_Player->m_CallbackWindow, TIMER_CHECKACTIVE, 500, nullptr);
|
2011-05-21 18:17:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Constructor.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
PlayerITunes::PlayerITunes() : Player(),
|
2011-07-17 10:36:04 +00:00
|
|
|
m_CallbackWindow(),
|
2011-10-12 13:34:04 +00:00
|
|
|
m_LastCheckTime(0),
|
2011-07-17 10:36:04 +00:00
|
|
|
m_iTunesActive(false),
|
2011-05-21 18:17:37 +00:00
|
|
|
m_iTunes(),
|
2011-05-31 13:15:53 +00:00
|
|
|
m_iTunesEvent()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-07-17 10:36:04 +00:00
|
|
|
// Create windows class
|
|
|
|
WNDCLASS wc = {0};
|
|
|
|
wc.hInstance = g_Instance;
|
|
|
|
wc.lpfnWndProc = WndProc;
|
|
|
|
wc.lpszClassName = L"NowPlayingITunesClass";
|
|
|
|
RegisterClass(&wc);
|
|
|
|
|
|
|
|
// Create callback window
|
|
|
|
m_CallbackWindow = CreateWindow(L"NowPlayingITunesClass",
|
|
|
|
L"CallbackWindow",
|
|
|
|
WS_DISABLED,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
HWND_MESSAGE,
|
2013-05-31 14:28:39 +00:00
|
|
|
nullptr,
|
2011-07-17 10:36:04 +00:00
|
|
|
g_Instance,
|
|
|
|
this);
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Destructor.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
PlayerITunes::~PlayerITunes()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
c_Player = nullptr;
|
2011-07-17 10:36:04 +00:00
|
|
|
|
|
|
|
DestroyWindow(m_CallbackWindow);
|
|
|
|
UnregisterClass(L"NowPlayingITunesClass", g_Instance);
|
|
|
|
|
2011-05-21 18:17:37 +00:00
|
|
|
Uninitialize();
|
|
|
|
}
|
|
|
|
|
2011-06-19 14:58:48 +00:00
|
|
|
/*
|
|
|
|
** Creates a shared class object.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
Player* PlayerITunes::Create()
|
2011-06-19 14:58:48 +00:00
|
|
|
{
|
|
|
|
if (!c_Player)
|
|
|
|
{
|
2013-05-31 14:18:52 +00:00
|
|
|
c_Player = new PlayerITunes();
|
2011-06-19 14:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c_Player;
|
|
|
|
}
|
|
|
|
|
2011-05-21 18:17:37 +00:00
|
|
|
/*
|
|
|
|
** Initialize iTunes COM interface and event handler.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::Initialize()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
HRESULT hr = CoCreateInstance(CLSID_iTunesApp, nullptr, CLSCTX_LOCAL_SERVER, IID_IiTunes, (PVOID*)&m_iTunes);
|
2011-05-21 18:17:37 +00:00
|
|
|
|
|
|
|
if (hr == CO_E_SERVER_EXEC_FAILURE)
|
|
|
|
{
|
|
|
|
// This seems to happen if there is a modal dialog being shown in iTunes
|
|
|
|
// or some other delay has occurred. Retrying should do the trick.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (hr != S_OK)
|
|
|
|
{
|
|
|
|
// Failed to get hold of iTunes instance via COM
|
2013-05-31 14:28:39 +00:00
|
|
|
m_iTunes = nullptr;
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_iTunes)
|
|
|
|
{
|
|
|
|
m_Initialized = true;
|
|
|
|
|
|
|
|
// Set up event handler
|
|
|
|
m_iTunesEvent = new CEventHandler(this);
|
|
|
|
|
|
|
|
// Try getting track info and player state
|
|
|
|
ITPlayerState state;
|
|
|
|
if (SUCCEEDED(m_iTunes->get_PlayerState(&state)))
|
|
|
|
{
|
|
|
|
if (state == ITPlayerStateStopped)
|
|
|
|
{
|
|
|
|
// Determine if paused of stopped
|
2013-10-20 15:55:23 +00:00
|
|
|
long position = 0;
|
2011-05-21 18:17:37 +00:00
|
|
|
m_iTunes->get_PlayerPosition(&position);
|
|
|
|
|
|
|
|
if (position != 0)
|
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
m_State = STATE_PAUSED;
|
2011-07-01 20:34:12 +00:00
|
|
|
OnTrackChange();
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (state == ITPlayerStatePlaying)
|
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
m_State = STATE_PLAYING;
|
2011-05-21 18:17:37 +00:00
|
|
|
OnTrackChange();
|
|
|
|
}
|
|
|
|
}
|
2011-11-19 12:06:51 +00:00
|
|
|
|
|
|
|
long volume;
|
|
|
|
m_iTunes->get_SoundVolume(&volume);
|
|
|
|
m_Volume = (UINT)volume;
|
|
|
|
|
|
|
|
OnDatabaseChange();
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Initialized = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Close iTunes COM interface.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::Uninitialize()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
|
|
|
if (m_Initialized)
|
|
|
|
{
|
|
|
|
m_Initialized = false;
|
2011-07-17 10:36:04 +00:00
|
|
|
ClearData();
|
|
|
|
|
|
|
|
m_iTunes->Release();
|
|
|
|
delete m_iTunesEvent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Window procedure for the callback window.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
LRESULT CALLBACK PlayerITunes::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
2011-07-17 10:36:04 +00:00
|
|
|
{
|
2013-05-31 14:18:52 +00:00
|
|
|
static PlayerITunes* player;
|
2011-07-17 10:36:04 +00:00
|
|
|
|
|
|
|
switch (msg)
|
|
|
|
{
|
|
|
|
case WM_CREATE:
|
2013-05-31 14:18:52 +00:00
|
|
|
// Get pointer to the PlayerITunes class from the CreateWindow call
|
|
|
|
player = (PlayerITunes*)(((CREATESTRUCT*)lParam)->lpCreateParams);
|
2011-07-17 10:36:04 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case WM_USER:
|
|
|
|
if (wParam == ITEventAboutToPromptUserToQuit)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-07-17 10:36:04 +00:00
|
|
|
// Event handler calls this through a PostMessage when iTunes quits
|
|
|
|
player->Uninitialize();
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
2011-07-17 10:36:04 +00:00
|
|
|
return 0;
|
2011-05-21 18:17:37 +00:00
|
|
|
|
2011-07-17 10:36:04 +00:00
|
|
|
case WM_TIMER:
|
|
|
|
if (wParam == TIMER_CHECKACTIVE)
|
|
|
|
{
|
2011-11-05 17:52:53 +00:00
|
|
|
if (!FindWindow(L"iTunesApp", L"iTunes") && !FindWindow(L"iTunes", L"iTunes"))
|
2011-07-17 10:36:04 +00:00
|
|
|
{
|
|
|
|
player->m_iTunesActive = false;
|
|
|
|
KillTimer(hwnd, TIMER_CHECKACTIVE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return DefWindowProc(hwnd, msg, wParam, lParam);
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-17 19:07:06 +00:00
|
|
|
/*
|
|
|
|
** Try to find iTunes periodically.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
bool PlayerITunes::CheckWindow()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-07-17 10:36:04 +00:00
|
|
|
DWORD time = GetTickCount();
|
2011-10-12 13:34:04 +00:00
|
|
|
if (time - m_LastCheckTime > 5000)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-10-12 13:34:04 +00:00
|
|
|
m_LastCheckTime = time;
|
2011-06-17 19:07:06 +00:00
|
|
|
|
2011-11-05 17:52:53 +00:00
|
|
|
if ((FindWindow(L"iTunesApp", L"iTunes") || FindWindow(L"iTunes", L"iTunes")) && !m_iTunesActive)
|
2011-06-17 19:07:06 +00:00
|
|
|
{
|
2011-07-17 10:36:04 +00:00
|
|
|
m_iTunesActive = true;
|
|
|
|
Initialize();
|
2011-06-17 19:07:06 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Called during each update of the main measure.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::UpdateData()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
if ((m_Initialized || CheckWindow()) && m_State != STATE_STOPPED)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2013-10-20 15:55:23 +00:00
|
|
|
long position = 0;
|
2011-05-21 18:17:37 +00:00
|
|
|
m_iTunes->get_PlayerPosition(&position);
|
|
|
|
m_Position = (UINT)position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-19 11:44:04 +00:00
|
|
|
/*
|
|
|
|
** Called by iTunes event handler when the database is changed.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::OnDatabaseChange()
|
2011-11-19 11:44:04 +00:00
|
|
|
{
|
|
|
|
// Check the shuffle state. TODO: Find better way
|
2011-11-19 12:06:51 +00:00
|
|
|
IITPlaylist* playlist;
|
|
|
|
HRESULT hr = m_iTunes->get_CurrentPlaylist(&playlist);
|
|
|
|
if (SUCCEEDED(hr) && playlist)
|
2011-11-19 11:44:04 +00:00
|
|
|
{
|
2011-11-19 12:06:51 +00:00
|
|
|
VARIANT_BOOL shuffle;
|
|
|
|
hr = playlist->get_Shuffle(&shuffle);
|
2011-11-19 11:44:04 +00:00
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
2013-12-23 19:44:42 +00:00
|
|
|
m_Shuffle = shuffle != VARIANT_FALSE;
|
2011-11-19 11:44:04 +00:00
|
|
|
}
|
|
|
|
|
2011-11-19 12:06:51 +00:00
|
|
|
playlist->Release();
|
2011-11-19 11:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-21 18:17:37 +00:00
|
|
|
/*
|
|
|
|
** Called by iTunes event handler on track change.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::OnTrackChange()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
|
|
|
IITTrack* track;
|
|
|
|
HRESULT hr = m_iTunes->get_CurrentTrack(&track);
|
2011-11-06 08:42:56 +00:00
|
|
|
if (SUCCEEDED(hr) && track)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2012-02-22 14:34:57 +00:00
|
|
|
BSTR tmpStr;
|
2011-05-21 18:17:37 +00:00
|
|
|
long tmpVal;
|
|
|
|
|
|
|
|
// Get metadata
|
|
|
|
track->get_Artist(&tmpStr);
|
|
|
|
tmpStr ? (m_Artist = tmpStr) : m_Artist.clear();
|
|
|
|
|
|
|
|
track->get_Name(&tmpStr);
|
|
|
|
tmpStr ? (m_Title = tmpStr) : m_Title.clear();
|
|
|
|
|
|
|
|
track->get_Album(&tmpStr);
|
|
|
|
tmpStr ? (m_Album = tmpStr) : m_Album.clear();
|
|
|
|
|
|
|
|
track->get_Duration(&tmpVal);
|
|
|
|
m_Duration = (UINT)tmpVal;
|
|
|
|
|
2011-06-19 14:58:48 +00:00
|
|
|
// Rating is 0 - 100, divide to 0 - 5
|
2011-05-21 18:17:37 +00:00
|
|
|
track->get_Rating(&tmpVal);
|
|
|
|
tmpVal /= 20L;
|
|
|
|
m_Rating = (UINT)tmpVal;
|
|
|
|
|
2012-03-12 12:01:12 +00:00
|
|
|
track->get_TrackNumber(&tmpVal);
|
|
|
|
m_Number = (UINT)tmpVal;
|
|
|
|
|
|
|
|
track->get_Year(&tmpVal);
|
|
|
|
m_Year = (UINT)tmpVal;
|
|
|
|
|
2011-11-10 12:09:50 +00:00
|
|
|
IITPlaylist* playlist;
|
|
|
|
hr = track->get_Playlist(&playlist);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
ITPlaylistRepeatMode repeat;
|
|
|
|
hr = playlist->get_SongRepeat(&repeat);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
2013-12-23 19:44:42 +00:00
|
|
|
m_Repeat = repeat != ITPlaylistRepeatModeOff;
|
2011-11-10 12:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
playlist->Release();
|
|
|
|
}
|
|
|
|
|
2011-05-22 08:06:43 +00:00
|
|
|
IITFileOrCDTrack* file;
|
|
|
|
hr = track->QueryInterface(&file);
|
|
|
|
if (SUCCEEDED(hr))
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-05-22 08:06:43 +00:00
|
|
|
file->get_Location(&tmpStr);
|
|
|
|
file->Release();
|
|
|
|
if (tmpStr && wcscmp(tmpStr, m_FilePath.c_str()) != 0)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
++m_TrackCount;
|
2011-05-22 08:06:43 +00:00
|
|
|
m_FilePath = tmpStr;
|
2011-05-21 18:17:37 +00:00
|
|
|
|
2011-09-08 12:42:03 +00:00
|
|
|
if (m_Measures & MEASURE_COVER)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-11-05 17:52:53 +00:00
|
|
|
m_CoverPath.clear();
|
|
|
|
|
|
|
|
// Check for embedded art through iTunes interface
|
|
|
|
IITArtworkCollection* artworkCollection;
|
|
|
|
hr = track->get_Artwork(&artworkCollection);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-11-05 17:52:53 +00:00
|
|
|
long count;
|
|
|
|
artworkCollection->get_Count(&count);
|
2011-05-21 18:17:37 +00:00
|
|
|
|
2011-11-05 17:52:53 +00:00
|
|
|
if (count > 0)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-11-05 17:52:53 +00:00
|
|
|
IITArtwork* artwork;
|
|
|
|
hr = artworkCollection->get_Item(1, &artwork);
|
2011-05-22 08:06:43 +00:00
|
|
|
|
2011-11-05 17:52:53 +00:00
|
|
|
if (SUCCEEDED(hr))
|
2011-05-22 08:06:43 +00:00
|
|
|
{
|
2013-05-09 10:23:52 +00:00
|
|
|
_bstr_t coverPath = m_TempCoverPath.c_str();
|
2012-03-02 12:27:37 +00:00
|
|
|
hr = artwork->SaveArtworkToFile(coverPath);
|
2011-06-19 14:58:48 +00:00
|
|
|
if (SUCCEEDED(hr))
|
2011-05-22 08:06:43 +00:00
|
|
|
{
|
2011-11-05 17:52:53 +00:00
|
|
|
m_CoverPath = m_TempCoverPath;
|
2011-05-22 08:06:43 +00:00
|
|
|
}
|
2011-06-19 14:58:48 +00:00
|
|
|
|
2011-11-05 17:52:53 +00:00
|
|
|
artwork->Release();
|
|
|
|
}
|
2011-05-22 11:05:23 +00:00
|
|
|
}
|
2011-11-05 17:52:53 +00:00
|
|
|
|
|
|
|
artworkCollection->Release();
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
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-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
track->Release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-18 16:10:43 +00:00
|
|
|
ClearData(false);
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Called by iTunes event handler on player state change.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::OnStateChange(bool playing)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
|
|
|
if (playing)
|
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
m_State = STATE_PLAYING;
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-20 15:55:23 +00:00
|
|
|
long position = 0;
|
|
|
|
m_iTunes->get_PlayerPosition(&position);
|
|
|
|
|
2011-05-21 18:17:37 +00:00
|
|
|
// Guess if paused or stopped from track time
|
2013-10-20 15:55:23 +00:00
|
|
|
m_State = (position == 0) ? STATE_STOPPED : STATE_PAUSED;
|
|
|
|
if (m_State == STATE_STOPPED)
|
|
|
|
{
|
|
|
|
ClearData(false);
|
|
|
|
}
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Called by iTunes event handler on volume change.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::OnVolumeChange(int volume)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
|
|
|
m_Volume = volume;
|
|
|
|
}
|
|
|
|
|
2011-06-09 09:47:55 +00:00
|
|
|
/*
|
|
|
|
** Handles the Pause bang.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::Pause()
|
2011-06-09 09:47:55 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
m_iTunes->Pause();
|
2011-06-09 09:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Handles the Play bang.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::Play()
|
2011-06-09 09:47:55 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
m_iTunes->Play();
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Handles the Stop bang.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::Stop()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
m_iTunes->Stop();
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Handles the Next bang.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::Next()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
m_iTunes->NextTrack();
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Handles the Previous bang.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::Previous()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
m_iTunes->PreviousTrack();
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
2011-06-09 09:47:55 +00:00
|
|
|
/*
|
|
|
|
** Handles the SetPosition bang.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::SetPosition(int position)
|
2011-06-09 09:47:55 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
m_iTunes->put_PlayerPosition((long)position);
|
2011-06-09 09:47:55 +00:00
|
|
|
}
|
|
|
|
|
2011-05-21 18:17:37 +00:00
|
|
|
/*
|
|
|
|
** Handles the SetRating bang.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::SetRating(int rating)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
IITTrack* track;
|
|
|
|
HRESULT hr = m_iTunes->get_CurrentTrack(&track);
|
2011-11-06 08:42:56 +00:00
|
|
|
if (SUCCEEDED(hr) && track)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
|
|
|
rating *= 20;
|
2011-06-17 19:07:06 +00:00
|
|
|
track->put_Rating((long)rating);
|
|
|
|
track->Release();
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Handles the SetVolume bang.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::SetVolume(int volume)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
m_iTunes->put_SoundVolume((long)volume);
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
2011-09-11 08:22:07 +00:00
|
|
|
/*
|
|
|
|
** Handles the SetShuffle bang.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::SetShuffle(bool state)
|
2011-09-11 08:22:07 +00:00
|
|
|
{
|
|
|
|
IITTrack* track;
|
|
|
|
HRESULT hr = m_iTunes->get_CurrentTrack(&track);
|
2011-11-06 08:42:56 +00:00
|
|
|
if (SUCCEEDED(hr) && track)
|
2011-09-11 08:22:07 +00:00
|
|
|
{
|
|
|
|
IITPlaylist* playlist;
|
|
|
|
hr = track->get_Playlist(&playlist);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
m_Shuffle = state;
|
|
|
|
VARIANT_BOOL shuffle = m_Shuffle ? VARIANT_TRUE : VARIANT_FALSE;
|
|
|
|
playlist->put_Shuffle(shuffle);
|
|
|
|
|
|
|
|
playlist->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
track->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Handles the SetRepeat bang.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::SetRepeat(bool state)
|
2011-09-11 08:22:07 +00:00
|
|
|
{
|
|
|
|
IITTrack* track;
|
|
|
|
HRESULT hr = m_iTunes->get_CurrentTrack(&track);
|
2011-11-06 08:42:56 +00:00
|
|
|
if (SUCCEEDED(hr) && track)
|
2011-09-11 08:22:07 +00:00
|
|
|
{
|
|
|
|
IITPlaylist* playlist;
|
|
|
|
hr = track->get_Playlist(&playlist);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
m_Repeat = state;
|
|
|
|
playlist->put_SongRepeat((ITPlaylistRepeatMode)m_Repeat);
|
|
|
|
|
|
|
|
playlist->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
track->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-21 18:17:37 +00:00
|
|
|
/*
|
|
|
|
** Handles the ClosePlayer bang.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::ClosePlayer()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
m_iTunes->Quit();
|
|
|
|
Uninitialize();
|
2013-05-31 14:28:39 +00:00
|
|
|
SetTimer(m_CallbackWindow, TIMER_CHECKACTIVE, 500, nullptr);
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Handles the OpenPlayer bang.
|
|
|
|
**
|
|
|
|
*/
|
2013-05-31 14:18:52 +00:00
|
|
|
void PlayerITunes::OpenPlayer(std::wstring& path)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2013-05-31 14:28:39 +00:00
|
|
|
ShellExecute(nullptr, L"open", path.empty() ? L"iTunes.exe" : path.c_str(), nullptr, nullptr, SW_SHOW);
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|