2009-09-18 06:58:48 +00:00
|
|
|
#include "Global.h"
|
2009-02-11 09:24:56 +00:00
|
|
|
#include "WndProcEater.h"
|
|
|
|
|
2009-11-30 20:52:09 +00:00
|
|
|
WndProcEater::WndProcEater()
|
|
|
|
{
|
|
|
|
hWndEaten = 0;
|
|
|
|
eatenWndProc = 0;
|
2009-04-19 23:04:21 +00:00
|
|
|
|
2009-11-30 20:52:09 +00:00
|
|
|
extraProcs = 0;
|
|
|
|
numExtraProcs = 0;
|
2009-04-19 23:04:21 +00:00
|
|
|
|
2009-11-30 20:52:09 +00:00
|
|
|
hMutex = CreateMutex(0, 0, L"LilyPad");
|
|
|
|
}
|
|
|
|
|
|
|
|
WndProcEater::~WndProcEater() throw()
|
|
|
|
{
|
|
|
|
if (hMutex) {
|
|
|
|
ReleaseMutex(hMutex);
|
|
|
|
CloseHandle(hMutex);
|
|
|
|
}
|
|
|
|
}
|
2009-02-11 09:24:56 +00:00
|
|
|
|
2009-11-30 20:52:09 +00:00
|
|
|
void WndProcEater::ReleaseExtraProc(ExtraWndProc proc) {
|
2009-03-23 23:29:27 +00:00
|
|
|
// Probably isn't needed, but just in case...
|
|
|
|
if (hMutex) WaitForSingleObject(hMutex, 100);
|
|
|
|
|
2009-11-30 20:52:09 +00:00
|
|
|
//printf( "(Lilypad) Regurgitating! -> 0x%x\n", proc );
|
|
|
|
|
2009-02-11 09:24:56 +00:00
|
|
|
for (int i=0; i<numExtraProcs; i++) {
|
2009-04-19 23:04:21 +00:00
|
|
|
if (extraProcs[i].proc == proc) {
|
2009-02-11 09:24:56 +00:00
|
|
|
extraProcs[i] = extraProcs[--numExtraProcs];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!numExtraProcs && eatenWndProc) {
|
|
|
|
free(extraProcs);
|
|
|
|
extraProcs = 0;
|
2010-02-12 23:28:42 +00:00
|
|
|
// As numExtraProcs is 0, won't cause recursion if called from Release().
|
|
|
|
Release();
|
2009-02-11 09:24:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-30 20:52:09 +00:00
|
|
|
void WndProcEater::Release() {
|
2009-04-19 23:04:21 +00:00
|
|
|
while (numExtraProcs) ReleaseExtraProc(extraProcs[0].proc);
|
2010-02-12 23:07:07 +00:00
|
|
|
if (hWndEaten && IsWindow(hWndEaten)) {
|
2010-02-12 23:28:42 +00:00
|
|
|
RemoveProp(hWndEaten, L"LilyHaxxor");
|
2010-02-12 23:07:07 +00:00
|
|
|
SetWindowLongPtr(hWndEaten, GWLP_WNDPROC, (LONG_PTR)eatenWndProc);
|
2010-02-12 23:09:27 +00:00
|
|
|
hWndEaten = 0;
|
|
|
|
eatenWndProc = 0;
|
2010-02-12 23:07:07 +00:00
|
|
|
}
|
2009-02-11 09:24:56 +00:00
|
|
|
}
|
|
|
|
|
2009-11-30 20:52:09 +00:00
|
|
|
LRESULT WndProcEater::_OverrideWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
if( hWnd != hWndEaten )
|
|
|
|
fprintf( stderr, "Totally mismatched window handles on OverrideWndProc!\n" );
|
|
|
|
|
2009-02-11 09:24:56 +00:00
|
|
|
ExtraWndProcResult res = CONTINUE_BLISSFULLY;
|
|
|
|
LRESULT out = 0;
|
|
|
|
// Here because want it for binding, even when no keyboard mode is selected.
|
|
|
|
if (uMsg == WM_GETDLGCODE) {
|
|
|
|
return DLGC_WANTALLKEYS | CallWindowProc(eatenWndProc, hWnd, uMsg, wParam, lParam);
|
|
|
|
}
|
2009-04-19 23:04:21 +00:00
|
|
|
|
2009-02-11 09:24:56 +00:00
|
|
|
for (int i=0; i<numExtraProcs; i++) {
|
2009-04-19 23:04:21 +00:00
|
|
|
// Note: Second bit of deviceUpdateQueued is only set when I receive a device change
|
|
|
|
// notification, which is handled in the GS thread in one of the extraProcs, so this
|
|
|
|
// is all I need to prevent bad things from happening while updating devices. No mutex needed.
|
2009-09-21 00:24:04 +00:00
|
|
|
// if ((deviceUpdateQueued&2) && (extraProcs[i].flags & EATPROC_NO_UPDATE_WHILE_UPDATING_DEVICES)) continue;
|
2009-04-19 23:04:21 +00:00
|
|
|
|
|
|
|
ExtraWndProcResult res2 = extraProcs[i].proc(hWnd, uMsg, wParam, lParam, &out);
|
2009-02-11 09:24:56 +00:00
|
|
|
if (res2 != res) {
|
|
|
|
if (res2 == CONTINUE_BLISSFULLY_AND_RELEASE_PROC) {
|
2009-04-19 23:04:21 +00:00
|
|
|
ReleaseExtraProc(extraProcs[i].proc);
|
2009-02-11 09:24:56 +00:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
else if (res2 > res) res = res2;
|
|
|
|
}
|
|
|
|
}
|
2009-04-19 23:04:21 +00:00
|
|
|
|
2009-02-11 09:24:56 +00:00
|
|
|
if (res != NO_WND_PROC) {
|
|
|
|
if (out == WM_DESTROY) {
|
2009-11-30 20:52:09 +00:00
|
|
|
Release();
|
2009-02-11 09:24:56 +00:00
|
|
|
}
|
|
|
|
if (res == CONTINUE_BLISSFULLY)
|
|
|
|
out = CallWindowProc(eatenWndProc, hWnd, uMsg, wParam, lParam);
|
|
|
|
else if (res == USE_DEFAULT_WND_PROC)
|
|
|
|
out = DefWindowProc(hWnd, uMsg, wParam, lParam);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2009-11-30 20:52:09 +00:00
|
|
|
static LRESULT CALLBACK OverrideWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
WndProcEater* obj = (WndProcEater*)GetProp(hWnd, L"LilyHaxxor");
|
|
|
|
return (obj == NULL) ?
|
|
|
|
DefWindowProc(hWnd, uMsg, wParam, lParam) :
|
|
|
|
obj->_OverrideWndProc( hWnd, uMsg, wParam, lParam );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WndProcEater::SetWndHandle(HWND hWnd)
|
|
|
|
{
|
|
|
|
if(hWnd == hWndEaten) return true;
|
|
|
|
|
|
|
|
//printf( "(Lilypad) (Re)-Setting window handle! -> this=0x%08x, hWnd=0x%08x\n", this, hWnd );
|
|
|
|
|
|
|
|
Release();
|
|
|
|
SetProp(hWnd, L"LilyHaxxor", (HANDLE)this);
|
|
|
|
|
|
|
|
eatenWndProc = (WNDPROC) SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)OverrideWndProc);
|
|
|
|
hWndEaten = (eatenWndProc) ? hWnd : 0;
|
|
|
|
|
|
|
|
return !!hWndEaten;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WndProcEater::Eat(ExtraWndProc proc, DWORD flags) {
|
|
|
|
|
|
|
|
// check if Subclassing failed to init during SetWndHandle
|
|
|
|
if (!hWndEaten) return;
|
2009-02-11 09:24:56 +00:00
|
|
|
|
2009-03-23 23:29:27 +00:00
|
|
|
// Probably isn't needed, but just in case...
|
|
|
|
if (hMutex) WaitForSingleObject(hMutex, 100);
|
|
|
|
|
2009-11-30 20:52:09 +00:00
|
|
|
//printf( "(Lilypad) EatingWndProc! -> 0x%x\n", proc );
|
2009-03-23 23:29:27 +00:00
|
|
|
|
2009-11-30 20:52:09 +00:00
|
|
|
extraProcs = (ExtraWndProcInfo*) realloc(extraProcs, sizeof(ExtraWndProcInfo)*(numExtraProcs+1));
|
|
|
|
extraProcs[numExtraProcs].proc = proc;
|
|
|
|
extraProcs[numExtraProcs].flags = flags;
|
|
|
|
numExtraProcs++;
|
2009-02-11 09:24:56 +00:00
|
|
|
}
|