From fcf11610ea23ecb5a4b86e67c7c838457e2d8dcf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Bernon?= <rbernon@codeweavers.com>
Date: Wed, 24 Jun 2020 12:13:02 +0200
Subject: [PATCH] dinput8: Stop using LL hooks for keyboard devices.

---
 dlls/dinput/dinput_main.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/dlls/dinput/dinput_main.c b/dlls/dinput/dinput_main.c
index 0e45d821897..9b6c4d4c434 100644
--- a/dlls/dinput/dinput_main.c
+++ b/dlls/dinput/dinput_main.c
@@ -107,6 +107,7 @@ static struct list direct_input_list = LIST_INIT( direct_input_list );
 static struct list acquired_mouse_list = LIST_INIT( acquired_mouse_list );
 static struct list acquired_rawmouse_list = LIST_INIT( acquired_rawmouse_list );
 static struct list acquired_keyboard_list = LIST_INIT( acquired_keyboard_list );
+static struct list acquired_rawkeyboard_list = LIST_INIT( acquired_rawkeyboard_list );
 static struct list acquired_device_list = LIST_INIT( acquired_device_list );
 
 static HRESULT initialize_directinput_instance(IDirectInputImpl *This, DWORD dwVersion);
@@ -120,7 +121,7 @@ void dinput_hooks_acquire_device(LPDIRECTINPUTDEVICE8W iface)
     if (IsEqualGUID( &dev->guid, &GUID_SysMouse ))
         list_add_tail( dev->use_raw_input ? &acquired_rawmouse_list : &acquired_mouse_list, &dev->entry );
     else if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ))
-        list_add_tail( &acquired_keyboard_list, &dev->entry );
+        list_add_tail( dev->use_raw_input ? &acquired_rawkeyboard_list : &acquired_keyboard_list, &dev->entry );
     else
         list_add_tail( &acquired_device_list, &dev->entry );
     LeaveCriticalSection( &dinput_hook_crit );
@@ -665,11 +666,8 @@ static LRESULT WINAPI di_em_win_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPAR
         else if (ri.header.dwType == RIM_TYPEKEYBOARD)
         {
             EnterCriticalSection( &dinput_hook_crit );
-            LIST_FOR_EACH_ENTRY( dev, &acquired_keyboard_list, IDirectInputDeviceImpl, entry )
-            {
-                if (!dev->use_raw_input) continue;
+            LIST_FOR_EACH_ENTRY( dev, &acquired_rawkeyboard_list, IDirectInputDeviceImpl, entry )
                 dinput_keyboard_rawinput_hook( &dev->IDirectInputDevice8A_iface, wparam, lparam, &ri );
-            }
             LeaveCriticalSection( &dinput_hook_crit );
         }
     }
@@ -1736,7 +1734,6 @@ static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
     }
     LIST_FOR_EACH_ENTRY( dev, &acquired_keyboard_list, IDirectInputDeviceImpl, entry )
     {
-        if (dev->use_raw_input) continue;
         TRACE("calling dinput_keyboard_hook (%p %lx %lx)\n", dev, wparam, lparam);
         skip |= dinput_keyboard_hook( &dev->IDirectInputDevice8A_iface, wparam, lparam );
     }
@@ -1790,6 +1787,14 @@ static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam
             IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
         }
     }
+    LIST_FOR_EACH_ENTRY_SAFE( dev, next, &acquired_rawkeyboard_list, IDirectInputDeviceImpl, entry )
+    {
+        if (msg->hwnd == dev->win && msg->hwnd != foreground)
+        {
+            TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
+            IDirectInputDevice_Unacquire( &dev->IDirectInputDevice8A_iface );
+        }
+    }
     LeaveCriticalSection( &dinput_hook_crit );
 
     return CallNextHookEx( 0, code, wparam, lparam );
-- 
2.27.0

