[win32k]
[reactos.git] / reactos / subsystems / win32 / win32k / ntuser / msgqueue.c
index e596958..77d1ef4 100644 (file)
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id$
- *
+/*
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
  * PURPOSE:          Message queues
- * FILE:             subsys/win32k/ntuser/msgqueue.c
+ * FILE:             subsystems/win32/win32k/ntuser/msgqueue.c
  * PROGRAMER:        Casper S. Hornstrup (chorns@users.sourceforge.net)
  * REVISION HISTORY:
  *       06-06-2001  CSH  Created
@@ -64,10 +63,36 @@ static PAGED_LOOKASIDE_LIST TimerLookasideList;
 
 /* FUNCTIONS *****************************************************************/
 
+//
+// Wakeup any thread/process waiting on idle input.
+//
+static VOID FASTCALL
+IdlePing(VOID)
+{
+  HWND hWnd;
+  PWINDOW_OBJECT Window;
+  PPROCESSINFO W32d = PsGetCurrentProcessWin32Process();
+
+  hWnd = UserGetForegroundWindow();
+
+  Window = UserGetWindowObject(hWnd);
+
+  if (Window && Window->ti)
+  {
+     if (Window->ti->fsHooks & HOOKID_TO_FLAG(WH_FOREGROUNDIDLE))
+     {
+        co_HOOK_CallHooks(WH_FOREGROUNDIDLE,HC_ACTION,0,0);
+     }
+  }
+
+  if (W32d && W32d->InputIdleEvent)
+     KePulseEvent( W32d->InputIdleEvent, EVENT_INCREMENT, TRUE);
+}
+
 HANDLE FASTCALL
 IntMsqSetWakeMask(DWORD WakeMask)
 {
-   PW32THREAD Win32Thread;
+   PTHREADINFO Win32Thread;
    PUSER_MESSAGE_QUEUE MessageQueue;
    HANDLE MessageEventHandle;
 
@@ -85,7 +110,7 @@ IntMsqSetWakeMask(DWORD WakeMask)
 BOOL FASTCALL
 IntMsqClearWakeMask(VOID)
 {
-   PW32THREAD Win32Thread;
+   PTHREADINFO Win32Thread;
    PUSER_MESSAGE_QUEUE MessageQueue;
 
    Win32Thread = PsGetCurrentThreadWin32Thread();
@@ -93,6 +118,7 @@ IntMsqClearWakeMask(VOID)
       return FALSE;
 
    MessageQueue = Win32Thread->MessageQueue;
+// HACK!!!!!!! Newbies that wrote this should hold your head down in shame! (jt)
    MessageQueue->WakeMask = ~0;
 
    return TRUE;
@@ -129,14 +155,14 @@ MsqInitializeImpl(VOID)
                                   NULL,
                                   0,
                                   sizeof(USER_MESSAGE),
-                                  0,
+                                  TAG_USRMSG,
                                   256);
    ExInitializePagedLookasideList(&TimerLookasideList,
                                   NULL,
                                   NULL,
                                   0,
                                   sizeof(TIMER_ENTRY),
-                                  0,
+                                  TAG_TIMER,
                                   64);
 
    return(STATUS_SUCCESS);
@@ -148,6 +174,7 @@ MsqInsertSystemMessage(MSG* Msg)
    LARGE_INTEGER LargeTickCount;
    KIRQL OldIrql;
    ULONG Prev;
+   EVENTMSG Event;
 
    IntLockSystemMessageQueue(OldIrql);
 
@@ -164,7 +191,14 @@ MsqInsertSystemMessage(MSG* Msg)
 
    KeQueryTickCount(&LargeTickCount);
    Msg->time = MsqCalculateMessageTime(&LargeTickCount);
-   
+
+   Event.message = Msg->message;
+   Event.time    = Msg->time;
+   Event.hwnd    = Msg->hwnd;
+   Event.paramL  = Msg->pt.x;
+   Event.paramH  = Msg->pt.y;
+   co_HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&Event);
+
    /*
     * If we got WM_MOUSEMOVE and there are already messages in the
     * system message queue, check if the last message is mouse move
@@ -201,17 +235,19 @@ MsqInsertSystemMessage(MSG* Msg)
 BOOL FASTCALL
 MsqIsDblClk(LPMSG Msg, BOOL Remove)
 {
+   PTHREADINFO pti;
    PWINSTATION_OBJECT WinStaObject;
    PSYSTEM_CURSORINFO CurInfo;
    LONG dX, dY;
    BOOL Res;
 
-   if (PsGetCurrentThreadWin32Thread()->Desktop == NULL)
+   pti = PsGetCurrentThreadWin32Thread();
+   if (pti->Desktop == NULL)
    {
       return FALSE;
    }
 
-   WinStaObject = PsGetCurrentThreadWin32Thread()->Desktop->WindowStation;
+   WinStaObject = pti->Desktop->WindowStation;
 
    CurInfo = IntGetSysCursorInfo(WinStaObject);
    Res = (Msg->hwnd == (HWND)CurInfo->LastClkWnd) &&
@@ -259,15 +295,15 @@ MsqIsDblClk(LPMSG Msg, BOOL Remove)
    return Res;
 }
 
-BOOL static STDCALL
-co_MsqTranslateMouseMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd, UINT FilterLow, UINT FilterHigh,
+static BOOL APIENTRY
+co_MsqTranslateMouseMessage(PUSER_MESSAGE_QUEUE MessageQueue, PWINDOW_OBJECT Window, UINT FilterLow, UINT FilterHigh,
                             PUSER_MESSAGE Message, BOOL Remove, PBOOL Freed,
-                            PWINDOW_OBJECT ScopeWin, PPOINT ScreenPoint, BOOL FromGlobalQueue)
+                            PWINDOW_OBJECT ScopeWin, PPOINT ScreenPoint, BOOL FromGlobalQueue, PLIST_ENTRY *Next)
 {
    USHORT Msg = Message->Msg.message;
-   PWINDOW_OBJECT Window = NULL;
+   PWINDOW_OBJECT CaptureWindow = NULL;
    HWND hCaptureWin;
-   
+
    ASSERT_REFS_CO(ScopeWin);
 
    /*
@@ -275,22 +311,22 @@ co_MsqTranslateMouseMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd, UINT Fi
    that window has a ref that we need to deref. Thats why we add "dummy"
    refs in all other cases.
    */
-   
+
    hCaptureWin = IntGetCaptureWindow();
    if (hCaptureWin == NULL)
    {
-      if(Msg == WM_MOUSEWHEEL)
+      if (Msg == WM_MOUSEWHEEL)
       {
-         Window = UserGetWindowObject(IntGetFocusWindow());
-         if (Window) UserRefObject(Window);
+         CaptureWindow = UserGetWindowObject(IntGetFocusWindow());
+         if (CaptureWindow) UserReferenceObject(CaptureWindow);
       }
       else
       {
-         co_WinPosWindowFromPoint(ScopeWin, NULL, &Message->Msg.pt, &Window);
-         if(Window == NULL)
+         co_WinPosWindowFromPoint(ScopeWin, NULL, &Message->Msg.pt, &CaptureWindow);
+         if(CaptureWindow == NULL)
          {
-            Window = ScopeWin;
-            if (Window) UserRefObject(Window);
+            CaptureWindow = ScopeWin;
+            if (CaptureWindow) UserReferenceObject(CaptureWindow);
          }
          else
          {
@@ -303,13 +339,13 @@ co_MsqTranslateMouseMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd, UINT Fi
    {
       /* FIXME - window messages should go to the right window if no buttons are
                  pressed */
-      Window = UserGetWindowObject(hCaptureWin);
-      if (Window) UserRefObject(Window);
+      CaptureWindow = UserGetWindowObject(hCaptureWin);
+      if (CaptureWindow) UserReferenceObject(CaptureWindow);
    }
 
 
 
-   if (Window == NULL)
+   if (CaptureWindow == NULL)
    {
       if(!FromGlobalQueue)
       {
@@ -319,12 +355,13 @@ co_MsqTranslateMouseMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd, UINT Fi
             MessageQueue->MouseMoveMsg = NULL;
          }
       }
+      // when FromGlobalQueue is true, the caller has already removed the Message
       ExFreePool(Message);
       *Freed = TRUE;
       return(FALSE);
    }
 
-   if (Window->MessageQueue != MessageQueue)
+   if (CaptureWindow->MessageQueue != MessageQueue)
    {
       if (! FromGlobalQueue)
       {
@@ -347,37 +384,37 @@ co_MsqTranslateMouseMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd, UINT Fi
 
       /* lock the destination message queue, so we don't get in trouble with other
          threads, messing with it at the same time */
-      IntLockHardwareMessageQueue(Window->MessageQueue);
-      InsertTailList(&Window->MessageQueue->HardwareMessagesListHead,
+      IntLockHardwareMessageQueue(CaptureWindow->MessageQueue);
+      InsertTailList(&CaptureWindow->MessageQueue->HardwareMessagesListHead,
                      &Message->ListEntry);
       if(Message->Msg.message == WM_MOUSEMOVE)
       {
-         if(Window->MessageQueue->MouseMoveMsg)
+         if(CaptureWindow->MessageQueue->MouseMoveMsg)
          {
             /* remove the old WM_MOUSEMOVE message, we're processing a more recent
                one */
-            RemoveEntryList(&Window->MessageQueue->MouseMoveMsg->ListEntry);
-            ExFreePool(Window->MessageQueue->MouseMoveMsg);
+            RemoveEntryList(&CaptureWindow->MessageQueue->MouseMoveMsg->ListEntry);
+            ExFreePool(CaptureWindow->MessageQueue->MouseMoveMsg);
          }
          /* save the pointer to the WM_MOUSEMOVE message in the new queue */
-         Window->MessageQueue->MouseMoveMsg = Message;
+         CaptureWindow->MessageQueue->MouseMoveMsg = Message;
 
-         Window->MessageQueue->QueueBits |= QS_MOUSEMOVE;
-         Window->MessageQueue->ChangedBits |= QS_MOUSEMOVE;
-         if (Window->MessageQueue->WakeMask & QS_MOUSEMOVE)
-            KeSetEvent(Window->MessageQueue->NewMessages, IO_NO_INCREMENT, FALSE);
+         CaptureWindow->MessageQueue->QueueBits |= QS_MOUSEMOVE;
+         CaptureWindow->MessageQueue->ChangedBits |= QS_MOUSEMOVE;
+         if (CaptureWindow->MessageQueue->WakeMask & QS_MOUSEMOVE)
+            KeSetEvent(CaptureWindow->MessageQueue->NewMessages, IO_NO_INCREMENT, FALSE);
       }
       else
       {
-         Window->MessageQueue->QueueBits |= QS_MOUSEBUTTON;
-         Window->MessageQueue->ChangedBits |= QS_MOUSEBUTTON;
-         if (Window->MessageQueue->WakeMask & QS_MOUSEBUTTON)
-            KeSetEvent(Window->MessageQueue->NewMessages, IO_NO_INCREMENT, FALSE);
+         CaptureWindow->MessageQueue->QueueBits |= QS_MOUSEBUTTON;
+         CaptureWindow->MessageQueue->ChangedBits |= QS_MOUSEBUTTON;
+         if (CaptureWindow->MessageQueue->WakeMask & QS_MOUSEBUTTON)
+            KeSetEvent(CaptureWindow->MessageQueue->NewMessages, IO_NO_INCREMENT, FALSE);
       }
-      IntUnLockHardwareMessageQueue(Window->MessageQueue);
+      IntUnLockHardwareMessageQueue(CaptureWindow->MessageQueue);
 
       *Freed = FALSE;
-      UserDerefObject(Window);
+      UserDereferenceObject(CaptureWindow);
       return(FALSE);
    }
 
@@ -385,8 +422,8 @@ co_MsqTranslateMouseMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd, UINT Fi
 
    *ScreenPoint = Message->Msg.pt;
 
-   if((hWnd != NULL && Window->hSelf != hWnd) ||
-         ((FilterLow != 0 || FilterLow != 0) && (Msg < FilterLow || Msg > FilterHigh)))
+   if((Window != NULL && (int)Window != 1 && CaptureWindow->hSelf != Window->hSelf) ||
+         ((FilterLow != 0 || FilterHigh != 0) && (Msg < FilterLow || Msg > FilterHigh)))
    {
       /* Reject the message because it doesn't match the filter */
 
@@ -395,38 +432,43 @@ co_MsqTranslateMouseMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd, UINT Fi
          /* Lock the message queue so no other thread can mess with it.
             Our own message queue is not locked while fetching from the global
             queue, so we have to make sure nothing interferes! */
-         IntLockHardwareMessageQueue(Window->MessageQueue);
+         IntLockHardwareMessageQueue(CaptureWindow->MessageQueue);
          /* if we're from the global queue, we need to add our message to our
             private queue so we don't loose it! */
-         InsertTailList(&Window->MessageQueue->HardwareMessagesListHead,
+         InsertTailList(&CaptureWindow->MessageQueue->HardwareMessagesListHead,
                         &Message->ListEntry);
       }
 
       if (Message->Msg.message == WM_MOUSEMOVE)
       {
-         if(Window->MessageQueue->MouseMoveMsg &&
-               (Window->MessageQueue->MouseMoveMsg != Message))
+         if(CaptureWindow->MessageQueue->MouseMoveMsg &&
+               (CaptureWindow->MessageQueue->MouseMoveMsg != Message))
          {
             /* delete the old message */
-            RemoveEntryList(&Window->MessageQueue->MouseMoveMsg->ListEntry);
-            ExFreePool(Window->MessageQueue->MouseMoveMsg);
+            RemoveEntryList(&CaptureWindow->MessageQueue->MouseMoveMsg->ListEntry);
+            ExFreePool(CaptureWindow->MessageQueue->MouseMoveMsg);
+            if (!FromGlobalQueue)
+            {
+               // We might have deleted the next one in our queue, so fix next
+               *Next = Message->ListEntry.Flink;
+            }
          }
          /* always save a pointer to this WM_MOUSEMOVE message here because we're
             sure that the message is in the private queue */
-         Window->MessageQueue->MouseMoveMsg = Message;
+         CaptureWindow->MessageQueue->MouseMoveMsg = Message;
       }
       if(FromGlobalQueue)
       {
-         IntUnLockHardwareMessageQueue(Window->MessageQueue);
+         IntUnLockHardwareMessageQueue(CaptureWindow->MessageQueue);
       }
 
-      UserDerefObject(Window);
+      UserDereferenceObject(CaptureWindow);
       *Freed = FALSE;
       return(FALSE);
    }
 
    /* FIXME - only assign if removing? */
-   Message->Msg.hwnd = Window->hSelf;
+   Message->Msg.hwnd = CaptureWindow->hSelf;
    Message->Msg.message = Msg;
    Message->Msg.lParam = MAKELONG(Message->Msg.pt.x, Message->Msg.pt.y);
 
@@ -440,32 +482,32 @@ co_MsqTranslateMouseMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd, UINT Fi
          /* Lock the message queue so no other thread can mess with it.
             Our own message queue is not locked while fetching from the global
             queue, so we have to make sure nothing interferes! */
-         IntLockHardwareMessageQueue(Window->MessageQueue);
-         if(Window->MessageQueue->MouseMoveMsg)
+         IntLockHardwareMessageQueue(CaptureWindow->MessageQueue);
+         if(CaptureWindow->MessageQueue->MouseMoveMsg)
          {
             /* delete the WM_(NC)MOUSEMOVE message in the private queue, we're dealing
                with one that's been sent later */
-            RemoveEntryList(&Window->MessageQueue->MouseMoveMsg->ListEntry);
-            ExFreePool(Window->MessageQueue->MouseMoveMsg);
+            RemoveEntryList(&CaptureWindow->MessageQueue->MouseMoveMsg->ListEntry);
+            ExFreePool(CaptureWindow->MessageQueue->MouseMoveMsg);
             /* our message is not in the private queue so we can remove the pointer
                instead of setting it to the current message we're processing */
-            Window->MessageQueue->MouseMoveMsg = NULL;
+            CaptureWindow->MessageQueue->MouseMoveMsg = NULL;
          }
-         IntUnLockHardwareMessageQueue(Window->MessageQueue);
+         IntUnLockHardwareMessageQueue(CaptureWindow->MessageQueue);
       }
-      else if(Window->MessageQueue->MouseMoveMsg == Message)
+      else if (CaptureWindow->MessageQueue->MouseMoveMsg == Message)
       {
-         Window->MessageQueue->MouseMoveMsg = NULL;
+         CaptureWindow->MessageQueue->MouseMoveMsg = NULL;
       }
    }
 
-   UserDerefObject(Window);
+   UserDereferenceObject(CaptureWindow);
    *Freed = FALSE;
    return(TRUE);
 }
 
-BOOL STDCALL
-co_MsqPeekHardwareMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd,
+BOOL APIENTRY
+co_MsqPeekHardwareMessage(PUSER_MESSAGE_QUEUE MessageQueue, PWINDOW_OBJECT Window,
                           UINT FilterLow, UINT FilterHigh, BOOL Remove,
                           PUSER_MESSAGE* Message)
 {
@@ -478,29 +520,31 @@ co_MsqPeekHardwareMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd,
    NTSTATUS WaitStatus;
    DECLARE_RETURN(BOOL);
    USER_REFERENCE_ENTRY Ref;
-   
+   PDESKTOPINFO Desk = NULL;
+
    WaitObjects[1] = MessageQueue->NewMessages;
    WaitObjects[0] = &HardwareMessageQueueLock;
    do
    {
+      IdlePing();
+
       UserLeaveCo();
 
       WaitStatus = KeWaitForMultipleObjects(2, WaitObjects, WaitAny, UserRequest,
                                             UserMode, FALSE, NULL, NULL);
 
       UserEnterCo();
-
-      while (co_MsqDispatchOneSentMessage(MessageQueue))
-      {
-         ;
-      }
    }
    while (NT_SUCCESS(WaitStatus) && STATUS_WAIT_0 != WaitStatus);
 
    DesktopWindow = UserGetWindowObject(IntGetDesktopWindow());
-   
-   if (DesktopWindow) UserRefObjectCo(DesktopWindow, &Ref);//can DesktopWindow be NULL?
-   
+
+   if (DesktopWindow)
+   {
+       UserRefObjectCo(DesktopWindow, &Ref);//can DesktopWindow be NULL?
+       Desk = DesktopWindow->ti->pDeskInfo;
+   }
+
    /* Process messages in the message queue itself. */
    IntLockHardwareMessageQueue(MessageQueue);
    CurrentEntry = MessageQueue->HardwareMessagesListHead.Flink;
@@ -512,12 +556,11 @@ co_MsqPeekHardwareMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd,
       if (Current->Msg.message >= WM_MOUSEFIRST &&
             Current->Msg.message <= WM_MOUSELAST)
       {
-         
-         
-         
-         Accept = co_MsqTranslateMouseMessage(MessageQueue, hWnd, FilterLow, FilterHigh,
+
+
+         Accept = co_MsqTranslateMouseMessage(MessageQueue, Window, FilterLow, FilterHigh,
                                               Current, Remove, &Freed,
-                                              DesktopWindow, &ScreenPoint, FALSE);
+                                              DesktopWindow, &ScreenPoint, FALSE, &CurrentEntry);
          if (Accept)
          {
             if (Remove)
@@ -527,7 +570,10 @@ co_MsqPeekHardwareMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd,
             IntUnLockHardwareMessageQueue(MessageQueue);
             IntUnLockSystemHardwareMessageQueueLock(FALSE);
             *Message = Current;
-            
+
+            if (Desk)
+                Desk->LastInputWasKbd = FALSE;
+
             RETURN(TRUE);
          }
 
@@ -612,9 +658,9 @@ co_MsqPeekHardwareMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd,
       {
          const ULONG ActiveStamp = HardwareMessageQueueStamp;
          /* Translate the message. */
-         Accept = co_MsqTranslateMouseMessage(MessageQueue, hWnd, FilterLow, FilterHigh,
+         Accept = co_MsqTranslateMouseMessage(MessageQueue, Window, FilterLow, FilterHigh,
                                               Current, Remove, &Freed,
-                                              DesktopWindow, &ScreenPoint, TRUE);
+                                              DesktopWindow, &ScreenPoint, TRUE, NULL);
          if (Accept)
          {
             /* Check for no more messages in the system queue. */
@@ -671,12 +717,15 @@ co_MsqPeekHardwareMessage(PUSER_MESSAGE_QUEUE MessageQueue, HWND hWnd,
 
    RETURN(FALSE);
 
-CLEANUP:   
+CLEANUP:
    if (DesktopWindow) UserDerefObjectCo(DesktopWindow);
-   
+
    END_CLEANUP;
 }
 
+//
+// Note: Only called from input.c.
+//
 VOID FASTCALL
 co_MsqPostKeyboardMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
 {
@@ -684,17 +733,42 @@ co_MsqPostKeyboardMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
    MSG Msg;
    LARGE_INTEGER LargeTickCount;
    KBDLLHOOKSTRUCT KbdHookData;
+   EVENTMSG Event;
+   BOOLEAN Entered = FALSE;
 
    DPRINT("MsqPostKeyboardMessage(uMsg 0x%x, wParam 0x%x, lParam 0x%x)\n",
           uMsg, wParam, lParam);
 
+   // Condition may arise when calling MsqPostMessage and waiting for an event.
+   if (!UserIsEntered())
+   {
+         // Fixme: Not sure ATM if this thread is locked.
+         UserEnterExclusive();
+         Entered = TRUE;
+   }
+
+   FocusMessageQueue = IntGetFocusMessageQueue();
+
    Msg.hwnd = 0;
+
+   if (FocusMessageQueue && (FocusMessageQueue->FocusWindow != (HWND)0))
+       Msg.hwnd = FocusMessageQueue->FocusWindow;
+
    Msg.message = uMsg;
    Msg.wParam = wParam;
    Msg.lParam = lParam;
 
    KeQueryTickCount(&LargeTickCount);
    Msg.time = MsqCalculateMessageTime(&LargeTickCount);
+
+   Event.message = Msg.message;
+   Event.hwnd    = Msg.hwnd;
+   Event.time    = Msg.time;
+   Event.paramL  = (Msg.wParam & 0xFF) | (HIWORD(Msg.lParam) << 8);
+   Event.paramH  = Msg.lParam & 0x7FFF;
+   if (HIWORD(Msg.lParam) & 0x0100) Event.paramH |= 0x8000;
+   co_HOOK_CallHooks( WH_JOURNALRECORD, HC_ACTION, 0, (LPARAM)&Event);
+
    /* We can't get the Msg.pt point here since we don't know thread
       (and thus the window station) the message will end up in yet. */
 
@@ -709,35 +783,42 @@ co_MsqPostKeyboardMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
       DPRINT("Kbd msg %d wParam %d lParam 0x%08x dropped by WH_KEYBOARD_LL hook\n",
              Msg.message, Msg.wParam, Msg.lParam);
+      if (Entered) UserLeave();
       return;
    }
 
-   FocusMessageQueue = IntGetFocusMessageQueue();
-      if (FocusMessageQueue == NULL)
-      {
+   if (FocusMessageQueue == NULL)
+   {
          DPRINT("No focus message queue\n");
+         if (Entered) UserLeave();
          return;
-      }
+   }
 
-      if (FocusMessageQueue->FocusWindow != (HWND)0)
-      {
+   if (FocusMessageQueue->FocusWindow != (HWND)0)
+   {
          Msg.hwnd = FocusMessageQueue->FocusWindow;
          DPRINT("Msg.hwnd = %x\n", Msg.hwnd);
+
+         FocusMessageQueue->Desktop->DesktopInfo->LastInputWasKbd = TRUE;
+
          IntGetCursorLocation(FocusMessageQueue->Desktop->WindowStation,
                               &Msg.pt);
          MsqPostMessage(FocusMessageQueue, &Msg, FALSE, QS_KEY);
-      }
-      else
-      {
+   }
+   else
+   {
          DPRINT("Invalid focus window handle\n");
-      }
    }
 
+   if (Entered) UserLeave();
+   return;
+}
+
 VOID FASTCALL
 MsqPostHotKeyMessage(PVOID Thread, HWND hWnd, WPARAM wParam, LPARAM lParam)
 {
    PWINDOW_OBJECT Window;
-   PW32THREAD Win32Thread;
+   PTHREADINFO Win32Thread;
    PWINSTATION_OBJECT WinSta;
    MSG Mesg;
    LARGE_INTEGER LargeTickCount;
@@ -773,7 +854,7 @@ MsqPostHotKeyMessage(PVOID Thread, HWND hWnd, WPARAM wParam, LPARAM lParam)
    Mesg.time = MsqCalculateMessageTime(&LargeTickCount);
    IntGetCursorLocation(WinSta, &Mesg.pt);
    MsqPostMessage(Window->MessageQueue, &Mesg, FALSE, QS_HOTKEY);
-   ObmDereferenceObject(Window);
+   UserDereferenceObject(Window);
    ObDereferenceObject (Thread);
 
    //  InsertHeadList(&pThread->MessageQueue->PostedMessagesListHead,
@@ -855,13 +936,20 @@ co_MsqDispatchOneSentMessage(PUSER_MESSAGE_QUEUE MessageQueue)
    InsertTailList(&MessageQueue->LocalDispatchingMessagesHead,
                   &Message->ListEntry);
 
-   if (Message->HookMessage)
+   if (Message->HookMessage == MSQ_ISHOOK)
    {
       Result = co_HOOK_CallHooks(Message->Msg.message,
-                                 (INT) Message->Msg.hwnd,
+                                 (INT)(INT_PTR)Message->Msg.hwnd,
                                  Message->Msg.wParam,
                                  Message->Msg.lParam);
    }
+   else if (Message->HookMessage == MSQ_ISEVENT)
+   {
+      Result = co_EVENT_CallEvents( Message->Msg.message,
+                                    Message->Msg.hwnd,
+                                    Message->Msg.wParam,
+                                    Message->Msg.lParam);                                  
+   }
    else
    {
       /* Call the window procedure. */
@@ -927,7 +1015,7 @@ Notified:
    return(TRUE);
 }
 
-VOID STDCALL
+VOID APIENTRY
 MsqRemoveWindowMessagesFromQueue(PVOID pWindow)
 {
    PUSER_SENT_MESSAGE SentMessage;
@@ -1016,9 +1104,10 @@ MsqSendNotifyMessage(PUSER_MESSAGE_QUEUE MessageQueue,
 NTSTATUS FASTCALL
 co_MsqSendMessage(PUSER_MESSAGE_QUEUE MessageQueue,
                   HWND Wnd, UINT Msg, WPARAM wParam, LPARAM lParam,
-                  UINT uTimeout, BOOL Block, BOOL HookMessage,
+                  UINT uTimeout, BOOL Block, INT HookMessage,
                   ULONG_PTR *uResult)
 {
+   PTHREADINFO pti;
    PUSER_SENT_MESSAGE Message;
    KEVENT CompletionEvent;
    NTSTATUS WaitStatus;
@@ -1035,7 +1124,8 @@ co_MsqSendMessage(PUSER_MESSAGE_QUEUE MessageQueue,
 
    KeInitializeEvent(&CompletionEvent, NotificationEvent, FALSE);
 
-   ThreadQueue = PsGetCurrentThreadWin32Thread()->MessageQueue;
+   pti = PsGetCurrentThreadWin32Thread();
+   ThreadQueue = pti->MessageQueue;
    ASSERT(ThreadQueue != MessageQueue);
 
    Timeout.QuadPart = (LONGLONG) uTimeout * (LONGLONG) -10000;
@@ -1071,6 +1161,7 @@ co_MsqSendMessage(PUSER_MESSAGE_QUEUE MessageQueue,
 
    if(Block)
    {
+      IdlePing();
 
       UserLeaveCo();
 
@@ -1133,6 +1224,7 @@ co_MsqSendMessage(PUSER_MESSAGE_QUEUE MessageQueue,
       WaitObjects[1] = ThreadQueue->NewMessages;
       do
       {
+         IdlePing();
 
          UserLeaveCo();
 
@@ -1225,11 +1317,11 @@ MsqPostQuitMessage(PUSER_MESSAGE_QUEUE MessageQueue, ULONG ExitCode)
       KeSetEvent(MessageQueue->NewMessages, IO_NO_INCREMENT, FALSE);
 }
 
-BOOLEAN STDCALL
+BOOLEAN APIENTRY
 co_MsqFindMessage(IN PUSER_MESSAGE_QUEUE MessageQueue,
                   IN BOOLEAN Hardware,
                   IN BOOLEAN Remove,
-                  IN HWND Wnd,
+                  IN PWINDOW_OBJECT Window,
                   IN UINT MsgFilterLow,
                   IN UINT MsgFilterHigh,
                   OUT PUSER_MESSAGE* Message)
@@ -1240,7 +1332,7 @@ co_MsqFindMessage(IN PUSER_MESSAGE_QUEUE MessageQueue,
 
    if (Hardware)
    {
-      return(co_MsqPeekHardwareMessage(MessageQueue, Wnd,
+      return(co_MsqPeekHardwareMessage(MessageQueue, Window,
                                        MsgFilterLow, MsgFilterHigh,
                                        Remove, Message));
    }
@@ -1251,7 +1343,7 @@ co_MsqFindMessage(IN PUSER_MESSAGE_QUEUE MessageQueue,
    {
       CurrentMessage = CONTAINING_RECORD(CurrentEntry, USER_MESSAGE,
                                          ListEntry);
-      if ((Wnd == 0 || Wnd == CurrentMessage->Msg.hwnd) &&
+      if ((!Window || (int)Window == 1 || Window->hSelf == CurrentMessage->Msg.hwnd) &&
             ((MsgFilterLow == 0 && MsgFilterHigh == 0) ||
              (MsgFilterLow <= CurrentMessage->Msg.message &&
               MsgFilterHigh >= CurrentMessage->Msg.message)))
@@ -1271,7 +1363,7 @@ co_MsqFindMessage(IN PUSER_MESSAGE_QUEUE MessageQueue,
 }
 
 NTSTATUS FASTCALL
-co_MsqWaitForNewMessages(PUSER_MESSAGE_QUEUE MessageQueue, HWND WndFilter,
+co_MsqWaitForNewMessages(PUSER_MESSAGE_QUEUE MessageQueue, PWINDOW_OBJECT WndFilter,
                          UINT MsgFilterMin, UINT MsgFilterMax)
 {
    PVOID WaitObjects[2] = {MessageQueue->NewMessages, &HardwareMessageEvent};
@@ -1288,6 +1380,8 @@ co_MsqWaitForNewMessages(PUSER_MESSAGE_QUEUE MessageQueue, HWND WndFilter,
       Timeout = NULL;
    }
 
+   IdlePing(); // Going to wait so send Idle ping.
+
    UserLeaveCo();
 
    ret = KeWaitForMultipleObjects(2,
@@ -1334,6 +1428,7 @@ MsqInitializeMessageQueue(struct _ETHREAD *Thread, PUSER_MESSAGE_QUEUE MessageQu
    MessageQueue->LastMsgRead = LargeTickCount.u.LowPart;
    MessageQueue->FocusWindow = NULL;
    MessageQueue->PaintCount = 0;
+// HACK!!!!!!! Newbies that wrote this should hold your head down in shame! (jt)
    MessageQueue->WakeMask = ~0;
    MessageQueue->NewMessagesHandle = NULL;
 
@@ -1461,7 +1556,7 @@ MsqCreateMessageQueue(struct _ETHREAD *Thread)
 {
    PUSER_MESSAGE_QUEUE MessageQueue;
 
-   MessageQueue = (PUSER_MESSAGE_QUEUE)ExAllocatePoolWithTag(PagedPool,
+   MessageQueue = (PUSER_MESSAGE_QUEUE)ExAllocatePoolWithTag(NonPagedPool,
                   sizeof(USER_MESSAGE_QUEUE) + sizeof(THRDCARETINFO),
                   TAG_MSGQ);
 
@@ -1486,12 +1581,12 @@ MsqCreateMessageQueue(struct _ETHREAD *Thread)
 VOID FASTCALL
 MsqDestroyMessageQueue(PUSER_MESSAGE_QUEUE MessageQueue)
 {
-   PDESKTOP_OBJECT desk;
+   PDESKTOP desk;
 
    /* remove the message queue from any desktops */
-   if ((desk = (PDESKTOP_OBJECT)InterlockedExchange((LONG*)&MessageQueue->Desktop, 0)))
+   if ((desk = InterlockedExchangePointer((PVOID*)&MessageQueue->Desktop, 0)))
    {
-      InterlockedExchange((LONG*)&desk->ActiveMessageQueue, 0);
+      (void)InterlockedExchangePointer((PVOID*)&desk->ActiveMessageQueue, 0);
       IntDereferenceMessageQueue(MessageQueue);
    }
 
@@ -1518,9 +1613,11 @@ LPARAM FASTCALL
 MsqSetMessageExtraInfo(LPARAM lParam)
 {
    LPARAM Ret;
+   PTHREADINFO pti;
    PUSER_MESSAGE_QUEUE MessageQueue;
 
-   MessageQueue = PsGetCurrentThreadWin32Thread()->MessageQueue;
+   pti = PsGetCurrentThreadWin32Thread();
+   MessageQueue = pti->MessageQueue;
    if(!MessageQueue)
    {
       return 0;
@@ -1535,9 +1632,11 @@ MsqSetMessageExtraInfo(LPARAM lParam)
 LPARAM FASTCALL
 MsqGetMessageExtraInfo(VOID)
 {
+   PTHREADINFO pti;
    PUSER_MESSAGE_QUEUE MessageQueue;
 
-   MessageQueue = PsGetCurrentThreadWin32Thread()->MessageQueue;
+   pti = PsGetCurrentThreadWin32Thread();
+   MessageQueue = pti->MessageQueue;
    if(!MessageQueue)
    {
       return 0;
@@ -1729,7 +1828,7 @@ MsqKillTimer(PUSER_MESSAGE_QUEUE MessageQueue, HWND Wnd,
 
 BOOLEAN FASTCALL
 MsqGetTimerMessage(PUSER_MESSAGE_QUEUE MessageQueue,
-                   HWND WndFilter, UINT MsgFilterMin, UINT MsgFilterMax,
+                   PWINDOW_OBJECT WindowFilter, UINT MsgFilterMin, UINT MsgFilterMax,
                    MSG *Msg, BOOLEAN Restart)
 {
    PTIMER_ENTRY Timer;
@@ -1737,6 +1836,7 @@ MsqGetTimerMessage(PUSER_MESSAGE_QUEUE MessageQueue,
    LARGE_INTEGER LargeTickCount;
    PLIST_ENTRY EnumEntry;
    BOOLEAN GotMessage;
+   PTHREADINFO pti;
 
    DPRINT("MsqGetTimerMessage queue %p msg %p restart %s\n",
           MessageQueue, Msg, Restart ? "TRUE" : "FALSE");
@@ -1752,7 +1852,7 @@ MsqGetTimerMessage(PUSER_MESSAGE_QUEUE MessageQueue,
       DPRINT("Checking timer %p wnd %x expiry %I64d\n", Timer, Timer->Wnd,
              Timer->ExpiryTime.QuadPart);
       EnumEntry = EnumEntry->Flink;
-      if ((NULL == WndFilter || Timer->Wnd == WndFilter) &&
+      if ((NULL == WindowFilter || Timer->Wnd == WindowFilter->hSelf) &&
             ((MsgFilterMin == 0 && MsgFilterMax == 0) ||
              (MsgFilterMin <= Timer->Msg &&
               Timer->Msg <= MsgFilterMax)))
@@ -1772,7 +1872,7 @@ MsqGetTimerMessage(PUSER_MESSAGE_QUEUE MessageQueue,
       else
       {
          DPRINT("timer %p (wnd %x msg %d) failed filter wnd %x msgmin %d msgmax %d\n",
-                Timer, Timer->Wnd, Timer->Msg, WndFilter, MsgFilterMin, MsgFilterMax);
+                Timer, Timer->Wnd, Timer->Msg, WindowFilter->hSelf, MsgFilterMin, MsgFilterMax);
       }
    }
 
@@ -1788,7 +1888,8 @@ MsqGetTimerMessage(PUSER_MESSAGE_QUEUE MessageQueue,
    Msg->lParam = (LPARAM) Timer->TimerFunc;
    KeQueryTickCount(&LargeTickCount);
    Msg->time = MsqCalculateMessageTime(&LargeTickCount);
-   IntGetCursorLocation(PsGetCurrentThreadWin32Thread()->Desktop->WindowStation,
+   pti = PsGetCurrentThreadWin32Thread();
+   IntGetCursorLocation(pti->Desktop->WindowStation,
                         &Msg->pt);
 
    if (Restart)
@@ -1841,7 +1942,7 @@ MsqRemoveTimersWindow(PUSER_MESSAGE_QUEUE MessageQueue, HWND Wnd)
 
 BOOLEAN FASTCALL
 MsqGetFirstTimerExpiry(PUSER_MESSAGE_QUEUE MessageQueue,
-                       HWND WndFilter, UINT MsgFilterMin, UINT MsgFilterMax,
+                       PWINDOW_OBJECT WndFilter, UINT MsgFilterMin, UINT MsgFilterMax,
                        PLARGE_INTEGER FirstTimerExpiry)
 {
    PTIMER_ENTRY Timer;
@@ -1856,7 +1957,7 @@ MsqGetFirstTimerExpiry(PUSER_MESSAGE_QUEUE MessageQueue,
       Timer = CONTAINING_RECORD(MessageQueue->TimerListHead.Flink,
                                 TIMER_ENTRY, ListEntry);
       EnumEntry = EnumEntry->Flink;
-      if ((NULL == WndFilter || Timer->Wnd == WndFilter) &&
+      if ((NULL == WndFilter || (int)WndFilter == 1 || Timer->Wnd == WndFilter->hSelf) &&
             ((MsgFilterMin == 0 && MsgFilterMax == 0) ||
              (MsgFilterMin <= Timer->Msg &&
               Timer->Msg <= MsgFilterMax)))