static KEVENT HardwareMessageEvent;
static PAGED_LOOKASIDE_LIST MessageLookasideList;
-static PAGED_LOOKASIDE_LIST TimerLookasideList;
#define IntLockSystemMessageQueue(OldIrql) \
KeAcquireSpinLock(&SystemMessageQueueLock, &OldIrql)
sizeof(USER_MESSAGE),
TAG_USRMSG,
256);
- ExInitializePagedLookasideList(&TimerLookasideList,
- NULL,
- NULL,
- 0,
- sizeof(TIMER_ENTRY),
- TAG_TIMER,
- 64);
return(STATUS_SUCCESS);
}
InitializeListHead(&MessageQueue->PostedMessagesListHead);
InitializeListHead(&MessageQueue->SentMessagesListHead);
InitializeListHead(&MessageQueue->HardwareMessagesListHead);
- InitializeListHead(&MessageQueue->TimerListHead);
InitializeListHead(&MessageQueue->DispatchingMessagesHead);
InitializeListHead(&MessageQueue->LocalDispatchingMessagesHead);
KeInitializeMutex(&MessageQueue->HardwareLock, 0);
{
PLIST_ENTRY CurrentEntry;
PUSER_MESSAGE CurrentMessage;
- PTIMER_ENTRY CurrentTimer;
PUSER_SENT_MESSAGE CurrentSentMessage;
/* cleanup posted messages */
ExFreePool(CurrentSentMessage);
}
- /* cleanup timers */
- while (! IsListEmpty(&MessageQueue->TimerListHead))
- {
- CurrentEntry = RemoveHeadList(&MessageQueue->TimerListHead);
- CurrentTimer = CONTAINING_RECORD(CurrentEntry, TIMER_ENTRY, ListEntry);
- ExFreeToPagedLookasideList(&TimerLookasideList, CurrentTimer);
- }
-
/* notify senders of dispatching messages. This needs to be cleaned up if e.g.
ExitThread() was called in a SendMessage() umode callback */
while (!IsListEmpty(&MessageQueue->LocalDispatchingMessagesHead))
return NULL;
}
-#ifndef NDEBUG
-static VOID FASTCALL
-DumpTimerList(PUSER_MESSAGE_QUEUE MessageQueue)
-{
- PLIST_ENTRY Current;
- PTIMER_ENTRY Timer;
-
- Current = MessageQueue->TimerListHead.Flink;
- if (Current == &MessageQueue->TimerListHead)
- {
- DPRINT("timer list is empty for queue %p\n", MessageQueue);
- }
- while (Current != &MessageQueue->TimerListHead)
- {
- Timer = CONTAINING_RECORD(Current, TIMER_ENTRY, ListEntry);
- DPRINT("queue %p timer %p expiry %I64d wnd %x id %p period %u timerproc %p msg %u\n",
- MessageQueue, Timer, Timer->ExpiryTime.QuadPart, Timer->Wnd, Timer->IDEvent,
- Timer->Period, Timer->TimerFunc, Timer->Msg);
- Current = Current->Flink;
- }
-}
-#endif /* ! defined(NDEBUG) */
-
-/* Must have the message queue locked while calling this */
-static VOID FASTCALL
-InsertTimer(PUSER_MESSAGE_QUEUE MessageQueue, PTIMER_ENTRY NewTimer)
-{
- PLIST_ENTRY Current;
-
- Current = MessageQueue->TimerListHead.Flink;
- while (Current != &MessageQueue->TimerListHead)
- {
- if (NewTimer->ExpiryTime.QuadPart <
- CONTAINING_RECORD(Current, TIMER_ENTRY, ListEntry)->ExpiryTime.QuadPart)
- {
- break;
- }
- Current = Current->Flink;
- }
-
- InsertTailList(Current, &NewTimer->ListEntry);
-}
-
-/* Must have the message queue locked while calling this */
-static PTIMER_ENTRY FASTCALL
-RemoveTimer(PUSER_MESSAGE_QUEUE MessageQueue, HWND Wnd, UINT_PTR IDEvent, UINT Msg)
-{
- PTIMER_ENTRY Timer;
- PLIST_ENTRY EnumEntry;
-
- /* Remove timer if already in the queue */
- EnumEntry = MessageQueue->TimerListHead.Flink;
- while (EnumEntry != &MessageQueue->TimerListHead)
- {
- Timer = CONTAINING_RECORD(EnumEntry, TIMER_ENTRY, ListEntry);
- EnumEntry = EnumEntry->Flink;
-
- if (Timer->Wnd == Wnd &&
- Timer->IDEvent == IDEvent &&
- Timer->Msg == Msg)
- {
- RemoveEntryList(&Timer->ListEntry);
- return Timer;
- }
- }
-
- return NULL;
-}
-
-BOOLEAN FASTCALL
-MsqSetTimer(PUSER_MESSAGE_QUEUE MessageQueue, HWND Wnd,
- UINT_PTR IDEvent, UINT Period, TIMERPROC TimerFunc,
- UINT Msg)
-{
- PTIMER_ENTRY Timer;
- LARGE_INTEGER CurrentTime;
-
- DPRINT("MsqSetTimer queue %p wnd %x id %p period %u timerproc %p msg %d\n",
- MessageQueue, Wnd, IDEvent, Period, TimerFunc, Msg);
-
- Timer = RemoveTimer(MessageQueue, Wnd, IDEvent, Msg);
- if (NULL == Timer)
- {
- Timer = ExAllocateFromPagedLookasideList(&TimerLookasideList);
- if (NULL == Timer)
- {
- DPRINT1("Failed to allocate timer entry\n");
- return FALSE;
- }
- DPRINT("Allocated new timer entry %p\n", Timer);
- Timer->Wnd = Wnd;
- Timer->IDEvent = IDEvent;
- Timer->Msg = Msg;
- }
- else
- {
- DPRINT("Updating existing timer entry %p\n", Timer);
- }
-
- KeQuerySystemTime(&CurrentTime);
- Timer->ExpiryTime.QuadPart = CurrentTime.QuadPart +
- (ULONGLONG) Period * (ULONGLONG) 10000;
- Timer->Period = Period;
- Timer->TimerFunc = TimerFunc;
- DPRINT("Insert timer now %I64d expiry %I64d\n", CurrentTime.QuadPart,
- Timer->ExpiryTime.QuadPart);
-
- InsertTimer(MessageQueue, Timer);
-
-#ifndef NDEBUG
-
- DumpTimerList(MessageQueue);
-#endif /* ! defined(NDEBUG) */
-
- return TRUE;
-}
-
-BOOLEAN FASTCALL
-MsqKillTimer(PUSER_MESSAGE_QUEUE MessageQueue, HWND Wnd,
- UINT_PTR IDEvent, UINT Msg)
-{
- PTIMER_ENTRY Timer;
-
- DPRINT("MsqKillTimer queue %p wnd %x id %p msg %d\n",
- MessageQueue, Wnd, IDEvent, Msg);
-
- Timer = RemoveTimer(MessageQueue, Wnd, IDEvent, Msg);
-
- if (NULL == Timer)
- {
- DPRINT("Failed to remove timer from list, not found\n");
- }
- else
- {
- ExFreeToPagedLookasideList(&TimerLookasideList, Timer);
- }
-
-#ifndef NDEBUG
- DumpTimerList(MessageQueue);
-#endif /* ! defined(NDEBUG) */
-
- return NULL != Timer;
-}
-
-BOOLEAN FASTCALL
-MsqGetTimerMessage(PUSER_MESSAGE_QUEUE MessageQueue,
- PWINDOW_OBJECT WindowFilter, UINT MsgFilterMin, UINT MsgFilterMax,
- MSG *Msg, BOOLEAN Restart)
-{
- PTIMER_ENTRY Timer;
- LARGE_INTEGER CurrentTime;
- LARGE_INTEGER LargeTickCount;
- PLIST_ENTRY EnumEntry;
- BOOLEAN GotMessage;
-
- DPRINT("MsqGetTimerMessage queue %p msg %p restart %s\n",
- MessageQueue, Msg, Restart ? "TRUE" : "FALSE");
-
- KeQuerySystemTime(&CurrentTime);
- DPRINT("Current time %I64d\n", CurrentTime.QuadPart);
- EnumEntry = MessageQueue->TimerListHead.Flink;
- GotMessage = FALSE;
- while (EnumEntry != &MessageQueue->TimerListHead)
- {
- Timer = CONTAINING_RECORD(MessageQueue->TimerListHead.Flink,
- TIMER_ENTRY, ListEntry);
- DPRINT("Checking timer %p wnd %x expiry %I64d\n", Timer, Timer->Wnd,
- Timer->ExpiryTime.QuadPart);
- EnumEntry = EnumEntry->Flink;
- if ((NULL == WindowFilter || Timer->Wnd == WindowFilter->hSelf) &&
- ((MsgFilterMin == 0 && MsgFilterMax == 0) ||
- (MsgFilterMin <= Timer->Msg &&
- Timer->Msg <= MsgFilterMax)))
- {
- if (Timer->ExpiryTime.QuadPart <= CurrentTime.QuadPart)
- {
- DPRINT("Timer is expired\n");
- GotMessage = TRUE;
- break;
- }
- else
- {
- DPRINT("No need to check later timers\n");
- break;
- }
- }
- else
- {
- DPRINT("timer %p (wnd %x msg %d) failed filter wnd %x msgmin %d msgmax %d\n",
- Timer, Timer->Wnd, Timer->Msg, WindowFilter->hSelf, MsgFilterMin, MsgFilterMax);
- }
- }
-
- if (! GotMessage)
- {
- DPRINT("No timer pending\n");
- return FALSE;
- }
-
- Msg->hwnd = Timer->Wnd;
- Msg->message = Timer->Msg;
- Msg->wParam = (WPARAM) Timer->IDEvent;
- Msg->lParam = (LPARAM) Timer->TimerFunc;
- KeQueryTickCount(&LargeTickCount);
- Msg->time = MsqCalculateMessageTime(&LargeTickCount);
- Msg->pt = gpsi->ptCursor;
-
- if (Restart)
- {
- RemoveEntryList(&Timer->ListEntry);
- Timer->ExpiryTime.QuadPart = CurrentTime.QuadPart +
- (ULONGLONG) Timer->Period * (ULONGLONG) 10000;
- DPRINT("Restarting timer %p expires %I64d\n", Timer, Timer->ExpiryTime.QuadPart);
- InsertTimer(MessageQueue, Timer);
-
-#ifndef NDEBUG
-
- DumpTimerList(MessageQueue);
-#endif /* ! defined(NDEBUG) */
-
- }
-
- DPRINT("Created message wnd %x msg %d wParam %u lParam %u\n", Msg->hwnd, Msg->message,
- Msg->wParam, Msg->lParam);
-
- return TRUE;
-}
-
-VOID FASTCALL
-MsqRemoveTimersWindow(PUSER_MESSAGE_QUEUE MessageQueue, HWND Wnd)
-{
- PTIMER_ENTRY Timer;
- PLIST_ENTRY EnumEntry;
-
- DPRINT("MsqRemoveTimersWindow queue %p wnd %x\n", MessageQueue, Wnd);
-
- EnumEntry = MessageQueue->TimerListHead.Flink;
- while (EnumEntry != &MessageQueue->TimerListHead)
- {
- Timer = CONTAINING_RECORD(EnumEntry, TIMER_ENTRY, ListEntry);
- EnumEntry = EnumEntry->Flink;
- if (Timer->Wnd == Wnd)
- {
- DPRINT("Removing timer %p because its window is going away\n", Timer);
- RemoveEntryList(&Timer->ListEntry);
- ExFreeToPagedLookasideList(&TimerLookasideList, Timer);
- }
- }
-
-#ifndef NDEBUG
- DumpTimerList(MessageQueue);
-#endif /* ! defined(NDEBUG) */
-
-}
-
-BOOLEAN FASTCALL
-MsqGetFirstTimerExpiry(PUSER_MESSAGE_QUEUE MessageQueue,
- PWINDOW_OBJECT WndFilter, UINT MsgFilterMin, UINT MsgFilterMax,
- PLARGE_INTEGER FirstTimerExpiry)
-{
- PTIMER_ENTRY Timer;
- PLIST_ENTRY EnumEntry;
-
- DPRINT("MsqGetFirstTimerExpiry queue %p wndfilter %x msgfiltermin %d msgfiltermax %d expiry %p\n",
- MessageQueue, WndFilter, MsgFilterMin, MsgFilterMax, FirstTimerExpiry);
-
- EnumEntry = MessageQueue->TimerListHead.Flink;
- while (EnumEntry != &MessageQueue->TimerListHead)
- {
- Timer = CONTAINING_RECORD(MessageQueue->TimerListHead.Flink,
- TIMER_ENTRY, ListEntry);
- EnumEntry = EnumEntry->Flink;
- if ((NULL == WndFilter || PtrToInt(WndFilter) == 1 || Timer->Wnd == WndFilter->hSelf) &&
- ((MsgFilterMin == 0 && MsgFilterMax == 0) ||
- (MsgFilterMin <= Timer->Msg &&
- Timer->Msg <= MsgFilterMax)))
- {
- *FirstTimerExpiry = Timer->ExpiryTime;
- DPRINT("First timer expires %I64d\n", Timer->ExpiryTime);
- return TRUE;
- }
- }
-
- return FALSE;
-}
-
/* EOF */