#include "uipriv_windows.hpp"
static HHOOK filter;
static LRESULT CALLBACK filterProc(int code, WPARAM wParam, LPARAM lParam)
{
MSG *msg = (MSG *) lParam;
if (code < 0)
goto callNext;
if (areaFilter(msg)) goto discard;
goto callNext;
discard:
return 1;
callNext:
return CallNextHookEx(filter, code, wParam, lParam);
}
int registerMessageFilter(void)
{
filter = SetWindowsHookExW(WH_MSGFILTER,
filterProc,
hInstance,
GetCurrentThreadId());
return filter != NULL;
}
void unregisterMessageFilter(void)
{
if (UnhookWindowsHookEx(filter) == 0)
logLastError(L"error unregistering libui message filter");
}
static void processMessage(MSG *msg)
{
HWND correctParent;
if (msg->hwnd != NULL)
correctParent = parentToplevel(msg->hwnd);
else correctParent = GetActiveWindow();
if (correctParent != NULL)
if (IsDialogMessage(correctParent, msg) != 0)
return;
TranslateMessage(msg);
DispatchMessageW(msg);
}
static int waitMessage(MSG *msg)
{
int res;
res = GetMessageW(msg, NULL, 0, 0);
if (res < 0) {
logLastError(L"error calling GetMessage()");
return 0; }
return res != 0; }
void uiMain(void)
{
while (uiMainStep(1))
;
}
void uiMainSteps(void)
{
}
static int peekMessage(MSG *msg)
{
BOOL res;
res = PeekMessageW(msg, NULL, 0, 0, PM_REMOVE);
if (res == 0)
return 2; if (msg->message != WM_QUIT)
return 1; return 0; }
int uiMainStep(int wait)
{
MSG msg;
if (wait) {
if (!waitMessage(&msg))
return 0;
processMessage(&msg);
return 1;
}
switch (peekMessage(&msg)) {
case 0: return 0;
case 1: processMessage(&msg);
}
return 1; }
void uiQuit(void)
{
PostQuitMessage(0);
}
void uiQueueMain(void (*f)(void *data), void *data)
{
if (PostMessageW(utilWindow, msgQueued, (WPARAM) f, (LPARAM) data) == 0)
logLastError(L"error queueing function to run on main thread");
}
void uiTimer(int milliseconds, int (*f)(void *data), void *data)
{
uiprivTimer *timer;
timer = uiprivNew(uiprivTimer);
timer->f = f;
timer->data = data;
if (SetTimer(utilWindow, (UINT_PTR) timer, milliseconds, NULL) == 0)
logLastError(L"error calling SetTimer() in uiTimer()");
}