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 #define INPUT_THREAD_NOTIFY (WM_USER + 0x10)
53 #define NOTIFY_THREAD_STOP 0
54 #define NOTIFY_REFRESH_DEVICES 1
55 #define NOTIFY_FOREGROUND_LOST 2
57 struct input_thread_state
63 HHOOK keyboard_ll_hook
;
64 RAWINPUTDEVICE rawinput_devices
[2];
65 struct dinput_device
*devices
[INPUT_THREAD_MAX_DEVICES
];
66 HANDLE events
[INPUT_THREAD_MAX_DEVICES
];
69 static inline struct dinput_device
*impl_from_IDirectInputDevice8W( IDirectInputDevice8W
*iface
)
71 return CONTAINING_RECORD( iface
, struct dinput_device
, IDirectInputDevice8W_iface
);
74 HINSTANCE DINPUT_instance
;
76 static HWND di_em_win
;
77 static HANDLE dinput_thread
;
78 static UINT input_thread_user_count
;
79 static struct input_thread_state
*input_thread_state
;
81 static CRITICAL_SECTION dinput_hook_crit
;
82 static CRITICAL_SECTION_DEBUG dinput_critsect_debug
=
84 0, 0, &dinput_hook_crit
,
85 { &dinput_critsect_debug
.ProcessLocksList
, &dinput_critsect_debug
.ProcessLocksList
},
86 0, 0, { (DWORD_PTR
)(__FILE__
": dinput_hook_crit") }
88 static CRITICAL_SECTION dinput_hook_crit
= { &dinput_critsect_debug
, -1, 0, 0, 0, 0 };
90 static struct list acquired_device_list
= LIST_INIT( acquired_device_list
);
92 static void unhook_device_window_foreground_changes( struct dinput_device
*device
)
94 if (!device
->cbt_hook
) return;
95 UnhookWindowsHookEx( device
->cbt_hook
);
96 device
->cbt_hook
= NULL
;
99 static void dinput_device_internal_unacquire( IDirectInputDevice8W
*iface
, DWORD status
)
101 struct dinput_device
*impl
= impl_from_IDirectInputDevice8W( iface
);
103 TRACE( "iface %p.\n", iface
);
105 unhook_device_window_foreground_changes( impl
);
107 EnterCriticalSection( &impl
->crit
);
108 if (impl
->status
== STATUS_ACQUIRED
)
110 impl
->vtbl
->unacquire( iface
);
111 impl
->status
= status
;
112 list_remove( &impl
->entry
);
114 LeaveCriticalSection( &impl
->crit
);
117 static LRESULT CALLBACK
input_thread_ll_hook_proc( int code
, WPARAM wparam
, LPARAM lparam
)
119 struct input_thread_state
*state
= input_thread_state
;
122 if (code
!= HC_ACTION
) return CallNextHookEx( 0, code
, wparam
, lparam
);
124 for (i
= state
->events_count
; i
< state
->devices_count
; ++i
)
126 struct dinput_device
*device
= state
->devices
[i
];
127 if (device
->use_raw_input
) continue;
128 if (device
->instance
.dwDevType
& DIDEVTYPE_HID
) continue;
129 switch (GET_DIDEVICE_TYPE( device
->instance
.dwDevType
))
131 case DIDEVTYPE_MOUSE
:
132 case DI8DEVTYPE_MOUSE
:
133 TRACE( "calling dinput_mouse_hook (%p %Ix %Ix)\n", device
, wparam
, lparam
);
134 skip
|= dinput_mouse_hook( &device
->IDirectInputDevice8W_iface
, wparam
, lparam
);
136 case DIDEVTYPE_KEYBOARD
:
137 case DI8DEVTYPE_KEYBOARD
:
138 TRACE( "calling dinput_keyboard_hook (%p %Ix %Ix)\n", device
, wparam
, lparam
);
139 skip
|= dinput_keyboard_hook( &device
->IDirectInputDevice8W_iface
, wparam
, lparam
);
144 return skip
? 1 : CallNextHookEx( 0, code
, wparam
, lparam
);
147 static void dinput_unacquire_window_devices( HWND window
)
149 struct dinput_device
*impl
, *next
;
151 EnterCriticalSection( &dinput_hook_crit
);
153 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_device_list
, struct dinput_device
, entry
)
155 if (window
!= impl
->win
) continue;
156 TRACE( "%p window is not foreground - unacquiring %p\n", impl
->win
, impl
);
157 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
160 LeaveCriticalSection( &dinput_hook_crit
);
163 static void dinput_unacquire_devices(void)
165 struct dinput_device
*impl
, *next
;
167 EnterCriticalSection( &dinput_hook_crit
);
169 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_device_list
, struct dinput_device
, entry
)
170 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
172 LeaveCriticalSection( &dinput_hook_crit
);
175 static LRESULT CALLBACK
cbt_hook_proc( int code
, WPARAM wparam
, LPARAM lparam
)
177 if (code
== HCBT_ACTIVATE
&& di_em_win
)
179 CBTACTIVATESTRUCT
*data
= (CBTACTIVATESTRUCT
*)lparam
;
180 SendMessageW( di_em_win
, INPUT_THREAD_NOTIFY
, NOTIFY_FOREGROUND_LOST
,
181 (LPARAM
)data
->hWndActive
);
184 return CallNextHookEx( 0, code
, wparam
, lparam
);
187 static void hook_device_window_foreground_changes( struct dinput_device
*device
)
190 if (!(tid
= GetWindowThreadProcessId( device
->win
, &pid
))) return;
191 device
->cbt_hook
= SetWindowsHookExW( WH_CBT
, cbt_hook_proc
, DINPUT_instance
, tid
);
194 static void input_thread_update_device_list( struct input_thread_state
*state
)
196 RAWINPUTDEVICE rawinput_keyboard
= {.usUsagePage
= HID_USAGE_PAGE_GENERIC
, .usUsage
= HID_USAGE_GENERIC_KEYBOARD
, .dwFlags
= RIDEV_REMOVE
};
197 RAWINPUTDEVICE rawinput_mouse
= {.usUsagePage
= HID_USAGE_PAGE_GENERIC
, .usUsage
= HID_USAGE_GENERIC_MOUSE
, .dwFlags
= RIDEV_REMOVE
};
198 UINT count
= 0, keyboard_ll_count
= 0, mouse_ll_count
= 0;
199 struct dinput_device
*device
;
201 EnterCriticalSection( &dinput_hook_crit
);
202 LIST_FOR_EACH_ENTRY( device
, &acquired_device_list
, struct dinput_device
, entry
)
204 unhook_device_window_foreground_changes( device
);
205 if (device
->dwCoopLevel
& DISCL_FOREGROUND
) hook_device_window_foreground_changes( device
);
207 if (!device
->read_event
|| !device
->vtbl
->read
) continue;
208 state
->events
[count
] = device
->read_event
;
209 dinput_device_internal_addref( (state
->devices
[count
] = device
) );
210 if (++count
>= INPUT_THREAD_MAX_DEVICES
) break;
212 state
->events_count
= count
;
214 LIST_FOR_EACH_ENTRY( device
, &acquired_device_list
, struct dinput_device
, entry
)
216 RAWINPUTDEVICE
*rawinput_device
= NULL
;
218 if (device
->read_event
&& device
->vtbl
->read
) continue;
219 switch (GET_DIDEVICE_TYPE( device
->instance
.dwDevType
))
221 case DIDEVTYPE_MOUSE
:
222 case DI8DEVTYPE_MOUSE
:
223 if (device
->dwCoopLevel
& DISCL_EXCLUSIVE
) rawinput_mouse
.dwFlags
|= RIDEV_CAPTUREMOUSE
;
224 if (!device
->use_raw_input
) mouse_ll_count
++;
225 else rawinput_device
= &rawinput_mouse
;
227 case DIDEVTYPE_KEYBOARD
:
228 case DI8DEVTYPE_KEYBOARD
:
229 if (device
->dwCoopLevel
& DISCL_EXCLUSIVE
) rawinput_keyboard
.dwFlags
|= RIDEV_NOHOTKEYS
;
230 if (!device
->use_raw_input
) keyboard_ll_count
++;
231 else rawinput_device
= &rawinput_keyboard
;
237 if (device
->dwCoopLevel
& DISCL_BACKGROUND
) rawinput_device
->dwFlags
|= RIDEV_INPUTSINK
;
238 if (device
->dwCoopLevel
& DISCL_EXCLUSIVE
) rawinput_device
->dwFlags
|= RIDEV_NOLEGACY
;
239 rawinput_device
->dwFlags
&= ~RIDEV_REMOVE
;
240 rawinput_device
->hwndTarget
= di_em_win
;
243 if (count
< INPUT_THREAD_MAX_DEVICES
) dinput_device_internal_addref( (state
->devices
[count
++] = device
) );
245 state
->devices_count
= count
;
246 LeaveCriticalSection( &dinput_hook_crit
);
248 if (keyboard_ll_count
&& !state
->keyboard_ll_hook
)
249 state
->keyboard_ll_hook
= SetWindowsHookExW( WH_KEYBOARD_LL
, input_thread_ll_hook_proc
, DINPUT_instance
, 0 );
250 else if (!keyboard_ll_count
&& state
->keyboard_ll_hook
)
252 UnhookWindowsHookEx( state
->keyboard_ll_hook
);
253 state
->keyboard_ll_hook
= NULL
;
256 if (mouse_ll_count
&& !state
->mouse_ll_hook
)
257 state
->mouse_ll_hook
= SetWindowsHookExW( WH_MOUSE_LL
, input_thread_ll_hook_proc
, DINPUT_instance
, 0 );
258 else if (!mouse_ll_count
&& state
->mouse_ll_hook
)
260 UnhookWindowsHookEx( state
->mouse_ll_hook
);
261 state
->mouse_ll_hook
= NULL
;
264 if (!rawinput_mouse
.hwndTarget
!= !state
->rawinput_devices
[0].hwndTarget
&&
265 !RegisterRawInputDevices( &rawinput_mouse
, 1, sizeof(RAWINPUTDEVICE
) ))
266 WARN( "Failed to (un)register rawinput mouse device.\n" );
267 if (!rawinput_keyboard
.hwndTarget
!= !state
->rawinput_devices
[1].hwndTarget
&&
268 !RegisterRawInputDevices( &rawinput_keyboard
, 1, sizeof(RAWINPUTDEVICE
) ))
269 WARN( "Failed to (un)register rawinput mouse device.\n" );
271 state
->rawinput_devices
[0] = rawinput_mouse
;
272 state
->rawinput_devices
[1] = rawinput_keyboard
;
275 static LRESULT WINAPI
di_em_win_wndproc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
277 struct input_thread_state
*state
= input_thread_state
;
278 int rim
= GET_RAWINPUT_CODE_WPARAM( wparam
);
279 UINT i
, size
= sizeof(RAWINPUT
);
282 TRACE( "%p %d %Ix %Ix\n", hwnd
, msg
, wparam
, lparam
);
284 if (msg
== WM_INPUT
&& (rim
== RIM_INPUT
|| rim
== RIM_INPUTSINK
))
286 size
= GetRawInputData( (HRAWINPUT
)lparam
, RID_INPUT
, &ri
, &size
, sizeof(RAWINPUTHEADER
) );
287 if (size
== (UINT
)-1 || size
< sizeof(RAWINPUTHEADER
))
288 WARN( "Unable to read raw input data\n" );
289 else if (ri
.header
.dwType
== RIM_TYPEHID
)
290 WARN( "Unexpected HID rawinput message\n" );
293 for (i
= state
->events_count
; i
< state
->devices_count
; ++i
)
295 struct dinput_device
*device
= state
->devices
[i
];
296 if (!device
->use_raw_input
) continue;
297 if (device
->instance
.dwDevType
& DIDEVTYPE_HID
) continue;
298 switch (GET_DIDEVICE_TYPE( device
->instance
.dwDevType
))
300 case DIDEVTYPE_MOUSE
:
301 case DI8DEVTYPE_MOUSE
:
302 if (ri
.header
.dwType
!= RIM_TYPEMOUSE
) break;
303 dinput_mouse_rawinput_hook( &device
->IDirectInputDevice8W_iface
, wparam
, lparam
, &ri
);
305 case DIDEVTYPE_KEYBOARD
:
306 case DI8DEVTYPE_KEYBOARD
:
307 if (ri
.header
.dwType
!= RIM_TYPEKEYBOARD
) break;
308 dinput_keyboard_rawinput_hook( &device
->IDirectInputDevice8W_iface
, wparam
, lparam
, &ri
);
316 if (msg
== INPUT_THREAD_NOTIFY
)
318 TRACE( "Processing hook change notification wparam %#Ix, lparam %#Ix.\n", wparam
, lparam
);
322 case NOTIFY_THREAD_STOP
:
323 state
->running
= FALSE
;
325 case NOTIFY_REFRESH_DEVICES
:
326 while (state
->devices_count
--) dinput_device_internal_release( state
->devices
[state
->devices_count
] );
327 input_thread_update_device_list( state
);
329 case NOTIFY_FOREGROUND_LOST
:
330 dinput_unacquire_window_devices( (HWND
)lparam
);
337 return DefWindowProcW( hwnd
, msg
, wparam
, lparam
);
340 static void register_di_em_win_class(void)
344 memset(&class, 0, sizeof(class));
345 class.cbSize
= sizeof(class);
346 class.lpfnWndProc
= di_em_win_wndproc
;
347 class.hInstance
= DINPUT_instance
;
348 class.lpszClassName
= L
"DIEmWin";
350 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS
)
351 WARN( "Unable to register message window class\n" );
354 static void unregister_di_em_win_class(void)
356 if (!UnregisterClassW( L
"DIEmWin", NULL
) && GetLastError() != ERROR_CLASS_DOES_NOT_EXIST
)
357 WARN( "Unable to unregister message window class\n" );
360 static DWORD WINAPI
dinput_thread_proc( void *params
)
362 struct input_thread_state state
= {.running
= TRUE
};
363 struct dinput_device
*device
;
364 HANDLE start_event
= params
;
368 di_em_win
= CreateWindowW( L
"DIEmWin", L
"DIEmWin", 0, 0, 0, 0, 0, HWND_MESSAGE
, 0, DINPUT_instance
, NULL
);
369 input_thread_state
= &state
;
370 SetEvent( start_event
);
372 while (state
.running
&& (ret
= MsgWaitForMultipleObjectsEx( state
.events_count
, state
.events
, INFINITE
, QS_ALLINPUT
, 0 )) <= state
.events_count
)
374 if (ret
< state
.events_count
)
376 if ((device
= state
.devices
[ret
]) && FAILED( device
->vtbl
->read( &device
->IDirectInputDevice8W_iface
) ))
378 EnterCriticalSection( &dinput_hook_crit
);
379 dinput_device_internal_unacquire( &device
->IDirectInputDevice8W_iface
, STATUS_UNPLUGGED
);
380 LeaveCriticalSection( &dinput_hook_crit
);
382 state
.events
[ret
] = state
.events
[--state
.events_count
];
383 state
.devices
[ret
] = state
.devices
[state
.events_count
];
384 state
.devices
[state
.events_count
] = state
.devices
[--state
.devices_count
];
385 dinput_device_internal_release( device
);
389 while (PeekMessageW( &msg
, 0, 0, 0, PM_REMOVE
))
391 TranslateMessage(&msg
);
392 DispatchMessageW(&msg
);
398 ERR( "Unexpected termination, ret %#lx\n", ret
);
399 dinput_unacquire_devices();
402 while (state
.devices_count
--) dinput_device_internal_release( state
.devices
[state
.devices_count
] );
403 if (state
.keyboard_ll_hook
) UnhookWindowsHookEx( state
.keyboard_ll_hook
);
404 if (state
.mouse_ll_hook
) UnhookWindowsHookEx( state
.mouse_ll_hook
);
405 DestroyWindow( di_em_win
);
410 void input_thread_start(void)
414 TRACE( "Starting input thread.\n" );
416 if (!(start_event
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
417 ERR( "Failed to create start event, error %lu\n", GetLastError() );
418 else if (!(dinput_thread
= CreateThread( NULL
, 0, dinput_thread_proc
, start_event
, 0, NULL
)))
419 ERR( "Failed to create internal thread, error %lu\n", GetLastError() );
421 WaitForSingleObject( start_event
, INFINITE
);
423 CloseHandle( start_event
);
426 void dinput_hooks_acquire_device( IDirectInputDevice8W
*iface
)
428 struct dinput_device
*impl
= impl_from_IDirectInputDevice8W( iface
);
430 EnterCriticalSection( &dinput_hook_crit
);
431 /* start the input thread now if it wasn't started already */
432 if (!dinput_thread
) input_thread_start();
433 list_add_tail( &acquired_device_list
, &impl
->entry
);
434 LeaveCriticalSection( &dinput_hook_crit
);
436 SendMessageW( di_em_win
, INPUT_THREAD_NOTIFY
, NOTIFY_REFRESH_DEVICES
, 0 );
439 void dinput_hooks_unacquire_device( IDirectInputDevice8W
*iface
)
441 struct dinput_device
*impl
= impl_from_IDirectInputDevice8W( iface
);
443 EnterCriticalSection( &dinput_hook_crit
);
444 list_remove( &impl
->entry
);
445 LeaveCriticalSection( &dinput_hook_crit
);
447 SendMessageW( di_em_win
, INPUT_THREAD_NOTIFY
, NOTIFY_REFRESH_DEVICES
, 0 );
450 void input_thread_add_user(void)
452 /* we cannot start the input thread here because some games create dinput objects from their DllMain, and
453 * starting the thread will wait for it to initialize, which requires the loader lock to be released.
455 EnterCriticalSection( &dinput_hook_crit
);
456 input_thread_user_count
++;
457 LeaveCriticalSection( &dinput_hook_crit
);
460 void input_thread_remove_user(void)
462 EnterCriticalSection( &dinput_hook_crit
);
463 if (!--input_thread_user_count
&& dinput_thread
)
465 TRACE( "Stopping input thread.\n" );
467 SendMessageW( di_em_win
, INPUT_THREAD_NOTIFY
, NOTIFY_THREAD_STOP
, 0 );
468 WaitForSingleObject( dinput_thread
, INFINITE
);
469 CloseHandle( dinput_thread
);
470 dinput_thread
= NULL
;
472 LeaveCriticalSection( &dinput_hook_crit
);
475 void check_dinput_events(void)
477 /* Windows does not do that, but our current implementation of winex11
478 * requires periodic event polling to forward events to the wineserver.
480 * We have to call this function from multiple places, because:
481 * - some games do not explicitly poll for mouse events
482 * (for example Culpa Innata)
483 * - some games only poll the device, and neither keyboard nor mouse
484 * (for example Civilization: Call to Power 2)
485 * - some games do not explicitly poll for keyboard events
486 * (for example Morrowind in its key binding page)
488 MsgWaitForMultipleObjectsEx(0, NULL
, 0, QS_ALLINPUT
, 0);
491 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, void *reserved
)
493 TRACE( "inst %p, reason %lu, reserved %p.\n", inst
, reason
, reserved
);
497 case DLL_PROCESS_ATTACH
:
498 DisableThreadLibraryCalls(inst
);
499 DINPUT_instance
= inst
;
500 register_di_em_win_class();
502 case DLL_PROCESS_DETACH
:
504 unregister_di_em_win_class();