Fixed skipped events from keyboard:
[reactos.git] / reactos / subsys / win32k / ntuser / input.c
index 57a8f77..f9e77ff 100644 (file)
@@ -35,7 +35,7 @@
 #define NDEBUG
 #include <debug.h>
 
-extern BYTE QueueKeyStateTable[];
+extern BYTE gQueueKeyStateTable[];
 
 /* GLOBALS *******************************************************************/
 
@@ -151,7 +151,7 @@ ProcessMouseInputData(PMOUSE_INPUT_DATA Data, ULONG InputCount)
 VOID STDCALL
 MouseThreadMain(PVOID StartContext)
 {
-   UNICODE_STRING MouseDeviceName = RTL_CONSTANT_STRING(L"\\??\\Mouse");
+   UNICODE_STRING MouseDeviceName = RTL_CONSTANT_STRING(L"\\Device\\PointerClass0");
    OBJECT_ATTRIBUTES MouseObjectAttributes;
    IO_STATUS_BLOCK Iosb;
    NTSTATUS Status;
@@ -161,17 +161,20 @@ MouseThreadMain(PVOID StartContext)
                               0,
                               NULL,
                               NULL);
-   Status = NtOpenFile(&MouseDeviceHandle,
+   do
+   {
+      LARGE_INTEGER DueTime;
+      KEVENT Event;
+      DueTime.QuadPart = (LONGLONG)(-10000000);
+      KeInitializeEvent(&Event, NotificationEvent, FALSE);
+      Status = KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, &DueTime); 
+      Status = NtOpenFile(&MouseDeviceHandle,
                        FILE_ALL_ACCESS,
                        &MouseObjectAttributes,
                        &Iosb,
                        0,
                        FILE_SYNCHRONOUS_IO_ALERT);
-   if(!NT_SUCCESS(Status))
-   {
-      DPRINT1("Win32K: Failed to open mouse.\n");
-      return; //(Status);
-   }
+   } while (!NT_SUCCESS(Status));
 
    for(;;)
    {
@@ -377,7 +380,7 @@ IntKeyboardSendWinKeyMsg()
    PWINDOW_OBJECT Window;
    MSG Mesg;
 
-   if (!(Window = IntGetWindowObject(InputWindowStation->ShellWindow)))
+   if (!(Window = UserGetWindowObject(InputWindowStation->ShellWindow)))
    {
       DPRINT1("Couldn't find window to send Windows key message!\n");
       return;
@@ -390,8 +393,6 @@ IntKeyboardSendWinKeyMsg()
 
    /* The QS_HOTKEY is just a guess */
    MsqPostMessage(Window->MessageQueue, &Mesg, FALSE, QS_HOTKEY);
-
-   ObmDereferenceObject(Window);
 }
 
 STATIC VOID STDCALL
@@ -403,7 +404,7 @@ co_IntKeyboardSendAltKeyMsg()
 STATIC VOID STDCALL
 KeyboardThreadMain(PVOID StartContext)
 {
-   UNICODE_STRING KeyboardDeviceName = RTL_CONSTANT_STRING(L"\\??\\Keyboard");
+   UNICODE_STRING KeyboardDeviceName = RTL_CONSTANT_STRING(L"\\Device\\KeyboardClass0");
    OBJECT_ATTRIBUTES KeyboardObjectAttributes;
    IO_STATUS_BLOCK Iosb;
    NTSTATUS Status;
@@ -412,7 +413,6 @@ KeyboardThreadMain(PVOID StartContext)
    struct _ETHREAD *FocusThread;
    extern NTSTATUS Win32kInitWin32Thread(PETHREAD Thread);
 
-
    PKEYBOARD_INDICATOR_TRANSLATION IndicatorTrans = NULL;
    UINT ModifierState = 0;
    USHORT LastMakeCode = 0;
@@ -424,17 +424,20 @@ KeyboardThreadMain(PVOID StartContext)
                               0,
                               NULL,
                               NULL);
-   Status = NtOpenFile(&KeyboardDeviceHandle,
+   do
+   {
+      LARGE_INTEGER DueTime;
+      KEVENT Event;
+      DueTime.QuadPart = (LONGLONG)(-10000000);
+      KeInitializeEvent(&Event, NotificationEvent, FALSE);
+      Status = KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, &DueTime); 
+      Status = NtOpenFile(&KeyboardDeviceHandle,
                        FILE_ALL_ACCESS,
                        &KeyboardObjectAttributes,
                        &Iosb,
                        0,
                        FILE_SYNCHRONOUS_IO_ALERT);
-   if (!NT_SUCCESS(Status))
-   {
-      DPRINT1("Win32K: Failed to open keyboard.\n");
-      return; //(Status);
-   }
+   } while (!NT_SUCCESS(Status));
 
    /* Not sure if converting this thread to a win32 thread is such
       a great idea. Since we're posting keyboard messages to the focus
@@ -482,8 +485,10 @@ KeyboardThreadMain(PVOID StartContext)
          HWND hWnd;
          int id;
 
+        DPRINT("KeyInput @ %08x\n", &KeyInput);
+
          Status = NtReadFile (KeyboardDeviceHandle,
-                              NULL,
+                             NULL,
                               NULL,
                               NULL,
                               &Iosb,
@@ -491,6 +496,22 @@ KeyboardThreadMain(PVOID StartContext)
                               sizeof(KEYBOARD_INPUT_DATA),
                               NULL,
                               NULL);
+
+         if(Status == STATUS_ALERTED && !InputThreadsRunning)
+         {
+            break;
+         }
+         if(Status == STATUS_PENDING)
+         {
+            NtWaitForSingleObject(KeyboardDeviceHandle, FALSE, NULL);
+            Status = Iosb.Status;
+         }
+         if(!NT_SUCCESS(Status))
+         {
+            DPRINT1("Win32K: Failed to read from mouse.\n");
+            return; //(Status);
+         }
+
          DPRINT("KeyRaw: %s %04x\n",
                 (KeyInput.Flags & KEY_BREAK) ? "up" : "down",
                 KeyInput.MakeCode );
@@ -666,19 +687,18 @@ KeyboardThreadMain(PVOID StartContext)
             FocusThread = FocusQueue->Thread;
 
             if (!(FocusThread && FocusThread->Tcb.Win32Thread &&
-                  FocusThread->Tcb.Win32Thread->KeyboardLayout))
+                  ((PW32THREAD)FocusThread->Tcb.Win32Thread)->KeyboardLayout))
                continue;
 
             /* This function uses lParam to fill wParam according to the
              * keyboard layout in use.
              */
             W32kKeyProcessMessage(&msg,
-                                  FocusThread->Tcb.Win32Thread->KeyboardLayout,
+                                  ((PW32THREAD)FocusThread->Tcb.Win32Thread)->KeyboardLayout,
                                   KeyInput.Flags & KEY_E0 ? 0xE0 :
                                   (KeyInput.Flags & KEY_E1 ? 0xE1 : 0));
 
-            if (GetHotKey(InputWindowStation,
-                          ModifierState,
+            if (GetHotKey(ModifierState,
                           msg.wParam,
                           &Thread,
                           &hWnd,
@@ -719,6 +739,7 @@ UserAcquireOrReleaseInputOwnership(BOOLEAN Release)
       InputThreadsRunning = FALSE;
 
       NtAlertThread(KeyboardThreadHandle);
+      NtAlertThread(MouseThreadHandle);
    }
    else if (!Release && !InputThreadsRunning)
    {
@@ -1026,42 +1047,42 @@ IntMouseInput(MOUSEINPUT *mi)
    Msg.message = 0;
    if(mi->dwFlags & MOUSEEVENTF_LEFTDOWN)
    {
-      QueueKeyStateTable[VK_LBUTTON] |= 0xc0;
+      gQueueKeyStateTable[VK_LBUTTON] |= 0xc0;
       Msg.message = SwapBtnMsg[0][SwapButtons];
       CurInfo->ButtonsDown |= SwapBtn[SwapButtons];
       MsqInsertSystemMessage(&Msg);
    }
    else if(mi->dwFlags & MOUSEEVENTF_LEFTUP)
    {
-      QueueKeyStateTable[VK_LBUTTON] &= ~0x80;
+      gQueueKeyStateTable[VK_LBUTTON] &= ~0x80;
       Msg.message = SwapBtnMsg[1][SwapButtons];
       CurInfo->ButtonsDown &= ~SwapBtn[SwapButtons];
       MsqInsertSystemMessage(&Msg);
    }
    if(mi->dwFlags & MOUSEEVENTF_MIDDLEDOWN)
    {
-      QueueKeyStateTable[VK_MBUTTON] |= 0xc0;
+      gQueueKeyStateTable[VK_MBUTTON] |= 0xc0;
       Msg.message = WM_MBUTTONDOWN;
       CurInfo->ButtonsDown |= MK_MBUTTON;
       MsqInsertSystemMessage(&Msg);
    }
    else if(mi->dwFlags & MOUSEEVENTF_MIDDLEUP)
    {
-      QueueKeyStateTable[VK_MBUTTON] &= ~0x80;
+      gQueueKeyStateTable[VK_MBUTTON] &= ~0x80;
       Msg.message = WM_MBUTTONUP;
       CurInfo->ButtonsDown &= ~MK_MBUTTON;
       MsqInsertSystemMessage(&Msg);
    }
    if(mi->dwFlags & MOUSEEVENTF_RIGHTDOWN)
    {
-      QueueKeyStateTable[VK_RBUTTON] |= 0xc0;
+      gQueueKeyStateTable[VK_RBUTTON] |= 0xc0;
       Msg.message = SwapBtnMsg[0][!SwapButtons];
       CurInfo->ButtonsDown |= SwapBtn[!SwapButtons];
       MsqInsertSystemMessage(&Msg);
    }
    else if(mi->dwFlags & MOUSEEVENTF_RIGHTUP)
    {
-      QueueKeyStateTable[VK_RBUTTON] &= ~0x80;
+      gQueueKeyStateTable[VK_RBUTTON] &= ~0x80;
       Msg.message = SwapBtnMsg[1][!SwapButtons];
       CurInfo->ButtonsDown &= ~SwapBtn[!SwapButtons];
       MsqInsertSystemMessage(&Msg);
@@ -1079,14 +1100,14 @@ IntMouseInput(MOUSEINPUT *mi)
       Msg.message = WM_XBUTTONDOWN;
       if(mi->mouseData & XBUTTON1)
       {
-         QueueKeyStateTable[VK_XBUTTON1] |= 0xc0;
+         gQueueKeyStateTable[VK_XBUTTON1] |= 0xc0;
          Msg.wParam = MAKEWPARAM(CurInfo->ButtonsDown, XBUTTON1);
          CurInfo->ButtonsDown |= XBUTTON1;
          MsqInsertSystemMessage(&Msg);
       }
       if(mi->mouseData & XBUTTON2)
       {
-         QueueKeyStateTable[VK_XBUTTON2] |= 0xc0;
+         gQueueKeyStateTable[VK_XBUTTON2] |= 0xc0;
          Msg.wParam = MAKEWPARAM(CurInfo->ButtonsDown, XBUTTON2);
          CurInfo->ButtonsDown |= XBUTTON2;
          MsqInsertSystemMessage(&Msg);
@@ -1097,14 +1118,14 @@ IntMouseInput(MOUSEINPUT *mi)
       Msg.message = WM_XBUTTONUP;
       if(mi->mouseData & XBUTTON1)
       {
-         QueueKeyStateTable[VK_XBUTTON1] &= ~0x80;
+         gQueueKeyStateTable[VK_XBUTTON1] &= ~0x80;
          Msg.wParam = MAKEWPARAM(CurInfo->ButtonsDown, XBUTTON1);
          CurInfo->ButtonsDown &= ~XBUTTON1;
          MsqInsertSystemMessage(&Msg);
       }
       if(mi->mouseData & XBUTTON2)
       {
-         QueueKeyStateTable[VK_XBUTTON2] &= ~0x80;
+         gQueueKeyStateTable[VK_XBUTTON2] &= ~0x80;
          Msg.wParam = MAKEWPARAM(CurInfo->ButtonsDown, XBUTTON2);
          CurInfo->ButtonsDown &= ~XBUTTON2;
          MsqInsertSystemMessage(&Msg);