3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2002 TransGaming Technologies Inc.
6 * Copyright 2007 Vitaliy Margolen
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #define NONAMELESSUNION
42 #include "dinput_private.h"
43 #include "device_private.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(dinput
);
50 #define INPUT_THREAD_MAX_DEVICES 128
52 struct input_thread_state
58 HHOOK keyboard_ll_hook
;
59 HHOOK callwndproc_hook
;
60 RAWINPUTDEVICE rawinput_devices
[2];
61 struct dinput_device
*devices
[INPUT_THREAD_MAX_DEVICES
];
62 HANDLE events
[INPUT_THREAD_MAX_DEVICES
];
65 static inline struct dinput_device
*impl_from_IDirectInputDevice8W( IDirectInputDevice8W
*iface
)
67 return CONTAINING_RECORD( iface
, struct dinput_device
, IDirectInputDevice8W_iface
);
70 HINSTANCE DINPUT_instance
;
72 static HWND di_em_win
;
73 static HANDLE dinput_thread
;
74 static UINT input_thread_user_count
;
75 static struct input_thread_state
*input_thread_state
;
77 static CRITICAL_SECTION dinput_hook_crit
;
78 static CRITICAL_SECTION_DEBUG dinput_critsect_debug
=
80 0, 0, &dinput_hook_crit
,
81 { &dinput_critsect_debug
.ProcessLocksList
, &dinput_critsect_debug
.ProcessLocksList
},
82 0, 0, { (DWORD_PTR
)(__FILE__
": dinput_hook_crit") }
84 static CRITICAL_SECTION dinput_hook_crit
= { &dinput_critsect_debug
, -1, 0, 0, 0, 0 };
86 static struct list acquired_mouse_list
= LIST_INIT( acquired_mouse_list
);
87 static struct list acquired_rawmouse_list
= LIST_INIT( acquired_rawmouse_list
);
88 static struct list acquired_keyboard_list
= LIST_INIT( acquired_keyboard_list
);
89 static struct list acquired_device_list
= LIST_INIT( acquired_device_list
);
91 void dinput_hooks_acquire_device( IDirectInputDevice8W
*iface
)
93 struct dinput_device
*impl
= impl_from_IDirectInputDevice8W( iface
);
95 EnterCriticalSection( &dinput_hook_crit
);
96 if (IsEqualGUID( &impl
->guid
, &GUID_SysMouse
))
97 list_add_tail( impl
->use_raw_input
? &acquired_rawmouse_list
: &acquired_mouse_list
, &impl
->entry
);
98 else if (IsEqualGUID( &impl
->guid
, &GUID_SysKeyboard
))
99 list_add_tail( &acquired_keyboard_list
, &impl
->entry
);
101 list_add_tail( &acquired_device_list
, &impl
->entry
);
102 LeaveCriticalSection( &dinput_hook_crit
);
104 SendMessageW( di_em_win
, WM_USER
+ 0x10, 1, 0 );
107 void dinput_hooks_unacquire_device( IDirectInputDevice8W
*iface
)
109 struct dinput_device
*impl
= impl_from_IDirectInputDevice8W( iface
);
111 EnterCriticalSection( &dinput_hook_crit
);
112 list_remove( &impl
->entry
);
113 LeaveCriticalSection( &dinput_hook_crit
);
115 SendMessageW( di_em_win
, WM_USER
+ 0x10, 1, 0 );
118 static void dinput_device_internal_unacquire( IDirectInputDevice8W
*iface
, DWORD status
)
120 struct dinput_device
*impl
= impl_from_IDirectInputDevice8W( iface
);
122 TRACE( "iface %p.\n", iface
);
124 EnterCriticalSection( &impl
->crit
);
125 if (impl
->status
== STATUS_ACQUIRED
)
127 impl
->vtbl
->unacquire( iface
);
128 impl
->status
= status
;
129 list_remove( &impl
->entry
);
131 LeaveCriticalSection( &impl
->crit
);
134 static LRESULT CALLBACK
input_thread_ll_hook_proc( int code
, WPARAM wparam
, LPARAM lparam
)
136 struct dinput_device
*impl
;
139 if (code
!= HC_ACTION
) return CallNextHookEx( 0, code
, wparam
, lparam
);
141 EnterCriticalSection( &dinput_hook_crit
);
142 LIST_FOR_EACH_ENTRY( impl
, &acquired_mouse_list
, struct dinput_device
, entry
)
144 TRACE( "calling dinput_mouse_hook (%p %Ix %Ix)\n", impl
, wparam
, lparam
);
145 skip
|= dinput_mouse_hook( &impl
->IDirectInputDevice8W_iface
, wparam
, lparam
);
147 LIST_FOR_EACH_ENTRY( impl
, &acquired_keyboard_list
, struct dinput_device
, entry
)
149 if (impl
->use_raw_input
) continue;
150 TRACE( "calling dinput_keyboard_hook (%p %Ix %Ix)\n", impl
, wparam
, lparam
);
151 skip
|= dinput_keyboard_hook( &impl
->IDirectInputDevice8W_iface
, wparam
, lparam
);
153 LeaveCriticalSection( &dinput_hook_crit
);
155 return skip
? 1 : CallNextHookEx( 0, code
, wparam
, lparam
);
158 static void dinput_unacquire_window_devices( HWND window
)
160 struct dinput_device
*impl
, *next
;
162 EnterCriticalSection( &dinput_hook_crit
);
164 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_device_list
, struct dinput_device
, entry
)
166 if (!window
|| window
== impl
->win
)
168 TRACE( "%p window is not foreground - unacquiring %p\n", impl
->win
, impl
);
169 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
172 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_mouse_list
, struct dinput_device
, entry
)
174 if (!window
|| window
== impl
->win
)
176 TRACE( "%p window is not foreground - unacquiring %p\n", impl
->win
, impl
);
177 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
180 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_rawmouse_list
, struct dinput_device
, entry
)
182 if (!window
|| window
== impl
->win
)
184 TRACE( "%p window is not foreground - unacquiring %p\n", impl
->win
, impl
);
185 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
188 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_keyboard_list
, struct dinput_device
, entry
)
190 if (!window
|| window
== impl
->win
)
192 TRACE( "%p window is not foreground - unacquiring %p\n", impl
->win
, impl
);
193 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
197 LeaveCriticalSection( &dinput_hook_crit
);
200 static LRESULT CALLBACK
callwndproc_proc( int code
, WPARAM wparam
, LPARAM lparam
)
202 CWPSTRUCT
*msg
= (CWPSTRUCT
*)lparam
;
204 if (code
!= HC_ACTION
|| (msg
->message
!= WM_KILLFOCUS
&&
205 msg
->message
!= WM_ACTIVATEAPP
&& msg
->message
!= WM_ACTIVATE
))
206 return CallNextHookEx( 0, code
, wparam
, lparam
);
208 if (msg
->hwnd
!= GetForegroundWindow()) dinput_unacquire_window_devices( msg
->hwnd
);
210 return CallNextHookEx( 0, code
, wparam
, lparam
);
213 static void input_thread_update_device_list( struct input_thread_state
*state
)
215 RAWINPUTDEVICE rawinput_mouse
= {.usUsagePage
= HID_USAGE_PAGE_GENERIC
, .usUsage
= HID_USAGE_GENERIC_MOUSE
, .dwFlags
= RIDEV_REMOVE
};
216 UINT count
= 0, keyboard_count
= 0, mouse_count
= 0, foreground_count
= 0;
217 struct dinput_device
*device
;
219 EnterCriticalSection( &dinput_hook_crit
);
220 LIST_FOR_EACH_ENTRY( device
, &acquired_device_list
, struct dinput_device
, entry
)
222 if (device
->dwCoopLevel
& DISCL_FOREGROUND
) foreground_count
++;
223 if (!device
->read_event
|| !device
->vtbl
->read
) continue;
224 state
->events
[count
] = device
->read_event
;
225 dinput_device_internal_addref( (state
->devices
[count
] = device
) );
226 if (++count
>= INPUT_THREAD_MAX_DEVICES
) break;
228 state
->events_count
= count
;
230 LIST_FOR_EACH_ENTRY( device
, &acquired_rawmouse_list
, struct dinput_device
, entry
)
232 if (device
->dwCoopLevel
& DISCL_FOREGROUND
) foreground_count
++;
233 if (device
->dwCoopLevel
& DISCL_BACKGROUND
) rawinput_mouse
.dwFlags
|= RIDEV_INPUTSINK
;
234 if (device
->dwCoopLevel
& DISCL_EXCLUSIVE
) rawinput_mouse
.dwFlags
|= RIDEV_NOLEGACY
| RIDEV_CAPTUREMOUSE
;
235 rawinput_mouse
.dwFlags
&= ~RIDEV_REMOVE
;
236 rawinput_mouse
.hwndTarget
= di_em_win
;
237 dinput_device_internal_addref( (state
->devices
[count
] = device
) );
238 if (++count
>= INPUT_THREAD_MAX_DEVICES
) break;
240 LIST_FOR_EACH_ENTRY( device
, &acquired_mouse_list
, struct dinput_device
, entry
)
242 if (device
->dwCoopLevel
& DISCL_FOREGROUND
) foreground_count
++;
245 LIST_FOR_EACH_ENTRY( device
, &acquired_keyboard_list
, struct dinput_device
, entry
)
247 if (device
->dwCoopLevel
& DISCL_FOREGROUND
) foreground_count
++;
250 state
->devices_count
= count
;
251 LeaveCriticalSection( &dinput_hook_crit
);
253 if (foreground_count
&& !state
->callwndproc_hook
)
254 state
->callwndproc_hook
= SetWindowsHookExW( WH_CALLWNDPROC
, callwndproc_proc
, DINPUT_instance
, GetCurrentThreadId() );
255 else if (!foreground_count
&& state
->callwndproc_hook
)
257 UnhookWindowsHookEx( state
->callwndproc_hook
);
258 state
->callwndproc_hook
= NULL
;
261 if (keyboard_count
&& !state
->keyboard_ll_hook
)
262 state
->keyboard_ll_hook
= SetWindowsHookExW( WH_KEYBOARD_LL
, input_thread_ll_hook_proc
, DINPUT_instance
, 0 );
263 else if (!keyboard_count
&& state
->keyboard_ll_hook
)
265 UnhookWindowsHookEx( state
->keyboard_ll_hook
);
266 state
->keyboard_ll_hook
= NULL
;
269 if (mouse_count
&& !state
->mouse_ll_hook
)
270 state
->mouse_ll_hook
= SetWindowsHookExW( WH_MOUSE_LL
, input_thread_ll_hook_proc
, DINPUT_instance
, 0 );
271 else if (!mouse_count
&& state
->mouse_ll_hook
)
273 UnhookWindowsHookEx( state
->mouse_ll_hook
);
274 state
->mouse_ll_hook
= NULL
;
277 if (!rawinput_mouse
.hwndTarget
!= !state
->rawinput_devices
[0].hwndTarget
&&
278 !RegisterRawInputDevices( &rawinput_mouse
, 1, sizeof(RAWINPUTDEVICE
) ))
279 WARN( "Failed to (un)register rawinput mouse device.\n" );
281 state
->rawinput_devices
[0] = rawinput_mouse
;
284 static LRESULT WINAPI
di_em_win_wndproc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
286 struct input_thread_state
*state
= input_thread_state
;
287 int rim
= GET_RAWINPUT_CODE_WPARAM( wparam
);
288 UINT i
, size
= sizeof(RAWINPUT
);
291 TRACE( "%p %d %Ix %Ix\n", hwnd
, msg
, wparam
, lparam
);
293 if (msg
== WM_INPUT
&& (rim
== RIM_INPUT
|| rim
== RIM_INPUTSINK
))
295 size
= GetRawInputData( (HRAWINPUT
)lparam
, RID_INPUT
, &ri
, &size
, sizeof(RAWINPUTHEADER
) );
296 if (size
== (UINT
)-1 || size
< sizeof(RAWINPUTHEADER
))
297 WARN( "Unable to read raw input data\n" );
298 else if (ri
.header
.dwType
== RIM_TYPEMOUSE
)
300 for (i
= state
->events_count
; i
< state
->devices_count
; ++i
)
302 struct dinput_device
*device
= state
->devices
[i
];
303 if (!device
->use_raw_input
) continue;
304 if (device
->instance
.dwDevType
& DIDEVTYPE_HID
) continue;
305 switch (GET_DIDEVICE_TYPE( device
->instance
.dwDevType
))
307 case DIDEVTYPE_MOUSE
:
308 case DI8DEVTYPE_MOUSE
:
309 dinput_mouse_rawinput_hook( &device
->IDirectInputDevice8W_iface
, wparam
, lparam
, &ri
);
317 if (msg
== WM_USER
+ 0x10)
319 TRACE( "Processing hook change notification wparam %#Ix, lparam %#Ix.\n", wparam
, lparam
);
321 if (!wparam
) state
->running
= FALSE
;
324 while (state
->devices_count
--) dinput_device_internal_release( state
->devices
[state
->devices_count
] );
325 input_thread_update_device_list( state
);
331 return DefWindowProcW( hwnd
, msg
, wparam
, lparam
);
334 static void register_di_em_win_class(void)
338 memset(&class, 0, sizeof(class));
339 class.cbSize
= sizeof(class);
340 class.lpfnWndProc
= di_em_win_wndproc
;
341 class.hInstance
= DINPUT_instance
;
342 class.lpszClassName
= L
"DIEmWin";
344 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS
)
345 WARN( "Unable to register message window class\n" );
348 static void unregister_di_em_win_class(void)
350 if (!UnregisterClassW( L
"DIEmWin", NULL
) && GetLastError() != ERROR_CLASS_DOES_NOT_EXIST
)
351 WARN( "Unable to unregister message window class\n" );
354 static DWORD WINAPI
dinput_thread_proc( void *params
)
356 struct input_thread_state state
= {.running
= TRUE
};
357 struct dinput_device
*device
;
358 HANDLE start_event
= params
;
362 di_em_win
= CreateWindowW( L
"DIEmWin", L
"DIEmWin", 0, 0, 0, 0, 0, HWND_MESSAGE
, 0, DINPUT_instance
, NULL
);
363 input_thread_state
= &state
;
364 SetEvent( start_event
);
366 while (state
.running
&& (ret
= MsgWaitForMultipleObjectsEx( state
.events_count
, state
.events
, INFINITE
, QS_ALLINPUT
, 0 )) <= state
.events_count
)
368 if (ret
< state
.events_count
)
370 if ((device
= state
.devices
[ret
]) && FAILED( device
->vtbl
->read( &device
->IDirectInputDevice8W_iface
) ))
372 EnterCriticalSection( &dinput_hook_crit
);
373 dinput_device_internal_unacquire( &device
->IDirectInputDevice8W_iface
, STATUS_UNPLUGGED
);
374 LeaveCriticalSection( &dinput_hook_crit
);
376 state
.events
[ret
] = state
.events
[--state
.events_count
];
377 state
.devices
[ret
] = state
.devices
[state
.events_count
];
378 state
.devices
[state
.events_count
] = state
.devices
[--state
.devices_count
];
379 dinput_device_internal_release( device
);
383 while (PeekMessageW( &msg
, 0, 0, 0, PM_REMOVE
))
385 TranslateMessage(&msg
);
386 DispatchMessageW(&msg
);
392 ERR( "Unexpected termination, ret %#lx\n", ret
);
393 dinput_unacquire_window_devices( 0 );
396 while (state
.devices_count
--) dinput_device_internal_release( state
.devices
[state
.devices_count
] );
397 if (state
.callwndproc_hook
) UnhookWindowsHookEx( state
.callwndproc_hook
);
398 if (state
.keyboard_ll_hook
) UnhookWindowsHookEx( state
.keyboard_ll_hook
);
399 if (state
.mouse_ll_hook
) UnhookWindowsHookEx( state
.mouse_ll_hook
);
400 DestroyWindow( di_em_win
);
405 void input_thread_add_user(void)
407 EnterCriticalSection( &dinput_hook_crit
);
408 if (!input_thread_user_count
++)
412 TRACE( "Starting input thread.\n" );
414 if (!(start_event
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
415 ERR( "Failed to create start event, error %lu\n", GetLastError() );
416 else if (!(dinput_thread
= CreateThread( NULL
, 0, dinput_thread_proc
, start_event
, 0, NULL
)))
417 ERR( "Failed to create internal thread, error %lu\n", GetLastError() );
419 WaitForSingleObject( start_event
, INFINITE
);
421 CloseHandle( start_event
);
423 LeaveCriticalSection( &dinput_hook_crit
);
426 void input_thread_remove_user(void)
428 EnterCriticalSection( &dinput_hook_crit
);
429 if (!--input_thread_user_count
)
431 TRACE( "Stopping input thread.\n" );
433 SendMessageW( di_em_win
, WM_USER
+ 0x10, 0, 0 );
434 WaitForSingleObject( dinput_thread
, INFINITE
);
435 CloseHandle( dinput_thread
);
437 LeaveCriticalSection( &dinput_hook_crit
);
440 void check_dinput_events(void)
442 /* Windows does not do that, but our current implementation of winex11
443 * requires periodic event polling to forward events to the wineserver.
445 * We have to call this function from multiple places, because:
446 * - some games do not explicitly poll for mouse events
447 * (for example Culpa Innata)
448 * - some games only poll the device, and neither keyboard nor mouse
449 * (for example Civilization: Call to Power 2)
450 * - some games do not explicitly poll for keyboard events
451 * (for example Morrowind in its key binding page)
453 MsgWaitForMultipleObjectsEx(0, NULL
, 0, QS_ALLINPUT
, 0);
456 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, void *reserved
)
458 TRACE( "inst %p, reason %lu, reserved %p.\n", inst
, reason
, reserved
);
462 case DLL_PROCESS_ATTACH
:
463 DisableThreadLibraryCalls(inst
);
464 DINPUT_instance
= inst
;
465 register_di_em_win_class();
467 case DLL_PROCESS_DETACH
:
469 unregister_di_em_win_class();