Skip to content

Commit

Permalink
Fixed hangs AIMP at shutdown / restart
Browse files Browse the repository at this point in the history
  • Loading branch information
Exle committed Apr 13, 2018
1 parent fa94201 commit 1f38cca
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aimp_DiscordPresence_ReadMe.txt
89 changes: 36 additions & 53 deletions AIMPRemote.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <cmath>
#include "AIMPRemote.h"
#include <process.h>

AIMPRemote *AIMPRemote::PAIMPRemote;

Expand All @@ -22,10 +21,10 @@ AIMPRemote::~AIMPRemote()
if (MyWnd != NULL)
{
DestroyWindow(MyWnd);
MyWnd = NULL;
}

delete PAIMPRemote;
delete FAIMPRemoteHandle;
delete MyWnd;
}

VOID AIMPRemote::AIMPExecuteCommand(INT ACommand)
Expand Down Expand Up @@ -63,65 +62,49 @@ VOID AIMPRemote::AIMPSetEvents(AIMPEvents *Events)

BOOL AIMPRemote::AIMPSetRemoteHandle(const HWND Value)
{
if (PAIMPRemote->FAIMPRemoteHandle != Value)
if (PAIMPRemote->FAIMPRemoteHandle == Value)
{
if (PAIMPRemote->FAIMPRemoteHandle != NULL)
{
SendMessage(PAIMPRemote->FAIMPRemoteHandle, WM_AIMP_COMMAND, AIMP_RA_CMD_UNREGISTER_NOTIFY, (LPARAM)PAIMPRemote->MyWnd);
PAIMPRemote->FAIMPRemoteHandle = NULL;
}
return true;
}

if (Value != NULL)
{
if (PAIMPRemote->MyWnd == NULL)
{
WNDCLASSEX wx = {};
wx.cbSize = sizeof(WNDCLASSEX);
wx.lpfnWndProc = WMAIMPNotify;
wx.hInstance = GetModuleHandle(NULL);
wx.lpszClassName = AIMPRemoteClassName;
if (RegisterClassEx(&wx))
{
PAIMPRemote->MyWnd = CreateWindowEx(WS_EX_CLIENTEDGE, AIMPRemoteClassName, AIMPRemoteClassName, NULL, NULL, NULL, NULL, NULL, HWND_MESSAGE, NULL, NULL, NULL);
}
}

if (PAIMPRemote->MyWnd == NULL)
{
return false;
}

PAIMPRemote->FAIMPRemoteHandle = Value;

PAIMPRemote->InfoUpdateVersionInfo();
PAIMPRemote->InfoUpdatePlayerState();
PAIMPRemote->InfoUpdateTrackInfo();
PAIMPRemote->InfoUpdateTrackPositionInfo();
if (PAIMPRemote->FAIMPRemoteHandle)
{
SendMessage(PAIMPRemote->FAIMPRemoteHandle, WM_AIMP_COMMAND, AIMP_RA_CMD_UNREGISTER_NOTIFY, (LPARAM)PAIMPRemote->MyWnd);
}

SendMessage(PAIMPRemote->FAIMPRemoteHandle, WM_AIMP_COMMAND, AIMP_RA_CMD_REGISTER_NOTIFY, (LPARAM)PAIMPRemote->MyWnd);
if (!Value)
{
return true;
}

_beginthread(ThreadStart, 0, NULL);
if (!PAIMPRemote->MyWnd)
{
WNDCLASSEX wx = {};
wx.cbSize = sizeof(WNDCLASSEX);
wx.lpfnWndProc = WMAIMPNotify;
wx.hInstance = GetModuleHandle(NULL);
wx.lpszClassName = AIMPRemoteClassName;
if (RegisterClassEx(&wx))
{
PAIMPRemote->MyWnd = CreateWindowExW(WS_EX_CLIENTEDGE, AIMPRemoteClassName, AIMPRemoteClassName, NULL, NULL, NULL, NULL, NULL, HWND_MESSAGE, NULL, NULL, NULL);
}
}

return true;
}
PAIMPRemote->FAIMPRemoteHandle = Value;

VOID AIMPRemote::ThreadStart(PVOID)
{
MSG msg;
do
PAIMPRemote->InfoUpdateVersionInfo();
PAIMPRemote->InfoUpdatePlayerState();
PAIMPRemote->InfoUpdateTrackInfo();
PAIMPRemote->InfoUpdateTrackPositionInfo();

if (!PAIMPRemote->MyWnd)
{
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
Sleep(1000L);
}
return false;
}
while (WaitForSingleObject(NULL, 75L) == WAIT_TIMEOUT);

_endthread();
SendMessage(PAIMPRemote->FAIMPRemoteHandle, WM_AIMP_COMMAND, AIMP_RA_CMD_REGISTER_NOTIFY, (LPARAM)PAIMPRemote->MyWnd);

return true;
}

BOOL AIMPRemote::InfoUpdateTrackInfo()
Expand All @@ -132,13 +115,13 @@ BOOL AIMPRemote::InfoUpdateTrackInfo()
WCHAR buffer[256];

hFile = OpenFileMappingA(FILE_MAP_READ, true, AIMPRemoteAccessClass);
if (hFile == NULL)
if (!hFile)
{
return false;
}

AIMPRemote_TrackInfo = (PAIMPRemoteFileInfo)MapViewOfFile(hFile, FILE_MAP_READ, NULL, NULL, AIMPRemoteAccessMapFileSize);
if (AIMPRemote_TrackInfo == NULL)
if (!AIMPRemote_TrackInfo)
{
CloseHandle(hFile);
return false;
Expand Down
2 changes: 0 additions & 2 deletions AIMPRemote.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ class AIMPRemote
BOOL InfoUpdateVersionInfo();
BOOL InfoUpdateTrackInfo();

static VOID ThreadStart(PVOID);

HWND FAIMPRemoteHandle;
HWND MyWnd;

Expand Down
1 change: 1 addition & 0 deletions DiscordRPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DiscordRPC::DiscordRPC()

DiscordRPC::~DiscordRPC()
{
ClearPresence();
Shutdown();
}

Expand Down
15 changes: 10 additions & 5 deletions IUnknownInterfaceImpl.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* https://github.com/AdrianEddy/AIMPYouTube/blob/master/IUnknownInterfaceImpl.h */

template <typename T>
class IUnknownInterfaceImpl : public T
{
Expand All @@ -8,12 +10,15 @@ class IUnknownInterfaceImpl : public T

virtual ~IUnknownInterfaceImpl() {}

virtual HRESULT WINAPI QueryInterface(REFIID riid, LPVOID* ppvObj) {
if (!ppvObj) {
virtual HRESULT WINAPI QueryInterface(REFIID riid, LPVOID* ppvObj)
{
if (!ppvObj)
{
return E_POINTER;
}

if (IID_IUnknown == riid) {
if (IID_IUnknown == riid)
{
*ppvObj = this;
AddRef();
return S_OK;
Expand All @@ -30,14 +35,14 @@ class IUnknownInterfaceImpl : public T
virtual ULONG WINAPI Release(void) {
ULONG reference_count = --reference_count_;

if (reference_count == 0) {
if (reference_count == 0)
{
delete this;
}

return reference_count;
}

private:

ULONG reference_count_;
};

0 comments on commit 1f38cca

Please sign in to comment.