+
+/******************************************************************************
+ * DInput hook thread
+ */
+
+static LRESULT CALLBACK LL_hook_proc( int code, WPARAM wparam, LPARAM lparam )
+{
+ IDirectInputImpl *dinput;
+
+ if (code != HC_ACTION) return CallNextHookEx( 0, code, wparam, lparam );
+
+ EnterCriticalSection( &dinput_hook_crit );
+ LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
+ {
+ IDirectInputDevice2AImpl *dev;
+
+ EnterCriticalSection( &dinput->crit );
+ LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
+ if (dev->acquired && dev->event_proc)
+ {
+ TRACE("calling %p->%p (%lx %lx)\n", dev, dev->event_proc, wparam, lparam);
+ dev->event_proc( (LPDIRECTINPUTDEVICE8A)dev, wparam, lparam );
+ }
+ LeaveCriticalSection( &dinput->crit );
+ }
+ LeaveCriticalSection( &dinput_hook_crit );
+
+ return CallNextHookEx( 0, code, wparam, lparam );
+}
+
+static LRESULT CALLBACK callwndproc_proc( int code, WPARAM wparam, LPARAM lparam )
+{
+ CWPSTRUCT *msg = (CWPSTRUCT *)lparam;
+ IDirectInputImpl *dinput;
+ HWND foreground;
+
+ if (code != HC_ACTION || (msg->message != WM_KILLFOCUS &&
+ msg->message != WM_ACTIVATEAPP && msg->message != WM_ACTIVATE))
+ return CallNextHookEx( 0, code, wparam, lparam );
+
+ foreground = GetForegroundWindow();
+
+ EnterCriticalSection( &dinput_hook_crit );
+
+ LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
+ {
+ IDirectInputDevice2AImpl *dev;
+
+ EnterCriticalSection( &dinput->crit );
+ LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
+ {
+ if (!dev->acquired) continue;
+
+ if (msg->hwnd == dev->win && msg->hwnd != foreground)
+ {
+ TRACE( "%p window is not foreground - unacquiring %p\n", dev->win, dev );
+ IDirectInputDevice_Unacquire( (LPDIRECTINPUTDEVICE8A)dev );
+ }
+ }
+ LeaveCriticalSection( &dinput->crit );
+ }
+ LeaveCriticalSection( &dinput_hook_crit );
+
+ return CallNextHookEx( 0, code, wparam, lparam );
+}
+
+static DWORD WINAPI hook_thread_proc(void *param)
+{
+ static HHOOK kbd_hook, mouse_hook;
+ MSG msg;
+
+ /* Force creation of the message queue */
+ PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE );
+ SetEvent(*(LPHANDLE)param);
+
+ while (GetMessageW( &msg, 0, 0, 0 ))
+ {
+ UINT kbd_cnt = 0, mice_cnt = 0;
+
+ if (msg.message == WM_USER+0x10)
+ {
+ IDirectInputImpl *dinput;
+
+ TRACE( "Processing hook change notification lp:%ld\n", msg.lParam );
+
+ if (!msg.wParam && !msg.lParam)
+ {
+ if (kbd_hook) UnhookWindowsHookEx( kbd_hook );
+ if (mouse_hook) UnhookWindowsHookEx( mouse_hook );
+ kbd_hook = mouse_hook = NULL;
+ break;
+ }
+
+ EnterCriticalSection( &dinput_hook_crit );
+
+ /* Count acquired keyboards and mice*/
+ LIST_FOR_EACH_ENTRY( dinput, &direct_input_list, IDirectInputImpl, entry )
+ {
+ IDirectInputDevice2AImpl *dev;
+
+ EnterCriticalSection( &dinput->crit );
+ LIST_FOR_EACH_ENTRY( dev, &dinput->devices_list, IDirectInputDevice2AImpl, entry )
+ {
+ if (!dev->acquired || !dev->event_proc) continue;
+
+ if (IsEqualGUID( &dev->guid, &GUID_SysKeyboard ) ||
+ IsEqualGUID( &dev->guid, &DInput_Wine_Keyboard_GUID ))
+ kbd_cnt++;
+ else
+ if (IsEqualGUID( &dev->guid, &GUID_SysMouse ) ||
+ IsEqualGUID( &dev->guid, &DInput_Wine_Mouse_GUID ))
+ mice_cnt++;
+ }
+ LeaveCriticalSection( &dinput->crit );
+ }
+ LeaveCriticalSection( &dinput_hook_crit );
+
+ if (kbd_cnt && !kbd_hook)
+ kbd_hook = SetWindowsHookExW( WH_KEYBOARD_LL, LL_hook_proc, DINPUT_instance, 0 );
+ else if (!kbd_cnt && kbd_hook)
+ {
+ UnhookWindowsHookEx( kbd_hook );
+ kbd_hook = NULL;
+ }
+
+ if (mice_cnt && !mouse_hook)
+ mouse_hook = SetWindowsHookExW( WH_MOUSE_LL, LL_hook_proc, DINPUT_instance, 0 );
+ else if (!mice_cnt && mouse_hook)
+ {
+ UnhookWindowsHookEx( mouse_hook );
+ mouse_hook = NULL;
+ }
+ }
+ TranslateMessage(&msg);
+ DispatchMessageW(&msg);
+ }
+
+ return 0;
+}
+
+static DWORD hook_thread_id;
+
+static CRITICAL_SECTION_DEBUG dinput_critsect_debug =
+{
+ 0, 0, &dinput_hook_crit,
+ { &dinput_critsect_debug.ProcessLocksList, &dinput_critsect_debug.ProcessLocksList },
+ 0, 0, { (DWORD_PTR)(__FILE__ ": dinput_hook_crit") }
+};
+static CRITICAL_SECTION dinput_hook_crit = { &dinput_critsect_debug, -1, 0, 0, 0, 0 };
+
+static BOOL check_hook_thread(void)
+{
+ static HANDLE hook_thread;
+
+ EnterCriticalSection(&dinput_hook_crit);
+
+ TRACE("IDirectInputs left: %d\n", list_count(&direct_input_list));
+ if (!list_empty(&direct_input_list) && !hook_thread)
+ {
+ HANDLE event;
+
+ event = CreateEventW(NULL, FALSE, FALSE, NULL);
+ hook_thread = CreateThread(NULL, 0, hook_thread_proc, &event, 0, &hook_thread_id);
+ if (event && hook_thread)
+ {
+ HANDLE handles[2];
+ handles[0] = event;
+ handles[1] = hook_thread;
+ WaitForMultipleObjects(2, handles, FALSE, INFINITE);
+ }
+ LeaveCriticalSection(&dinput_hook_crit);
+ CloseHandle(event);
+ }
+ else if (list_empty(&direct_input_list) && hook_thread)
+ {
+ DWORD tid = hook_thread_id;
+
+ hook_thread_id = 0;
+ PostThreadMessageW(tid, WM_USER+0x10, 0, 0);
+ LeaveCriticalSection(&dinput_hook_crit);
+
+ /* wait for hook thread to exit */
+ WaitForSingleObject(hook_thread, INFINITE);
+ CloseHandle(hook_thread);
+ hook_thread = NULL;
+ }
+ else
+ LeaveCriticalSection(&dinput_hook_crit);
+
+ return hook_thread_id != 0;
+}
+
+void check_dinput_hooks(LPDIRECTINPUTDEVICE8A iface)
+{
+ static HHOOK callwndproc_hook;
+ static ULONG foreground_cnt;
+ IDirectInputDevice2AImpl *dev = (IDirectInputDevice2AImpl *)iface;
+
+ EnterCriticalSection(&dinput_hook_crit);
+
+ if (dev->dwCoopLevel & DISCL_FOREGROUND)
+ {
+ if (dev->acquired)
+ foreground_cnt++;
+ else
+ foreground_cnt--;
+ }
+
+ if (foreground_cnt && !callwndproc_hook)
+ callwndproc_hook = SetWindowsHookExW( WH_CALLWNDPROC, callwndproc_proc,
+ DINPUT_instance, GetCurrentThreadId() );
+ else if (!foreground_cnt && callwndproc_hook)
+ {
+ UnhookWindowsHookEx( callwndproc_hook );
+ callwndproc_hook = NULL;
+ }
+
+ PostThreadMessageW( hook_thread_id, WM_USER+0x10, 1, 0 );
+
+ LeaveCriticalSection(&dinput_hook_crit);
+}