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 HHOOK callwndproc_hook
;
65 RAWINPUTDEVICE rawinput_devices
[2];
66 struct dinput_device
*devices
[INPUT_THREAD_MAX_DEVICES
];
67 HANDLE events
[INPUT_THREAD_MAX_DEVICES
];
70 static inline struct dinput_device
*impl_from_IDirectInputDevice8W( IDirectInputDevice8W
*iface
)
72 return CONTAINING_RECORD( iface
, struct dinput_device
, IDirectInputDevice8W_iface
);
75 HINSTANCE DINPUT_instance
;
77 static HWND di_em_win
;
78 static HANDLE dinput_thread
;
79 static UINT input_thread_user_count
;
80 static struct input_thread_state
*input_thread_state
;
82 static CRITICAL_SECTION dinput_hook_crit
;
83 static CRITICAL_SECTION_DEBUG dinput_critsect_debug
=
85 0, 0, &dinput_hook_crit
,
86 { &dinput_critsect_debug
.ProcessLocksList
, &dinput_critsect_debug
.ProcessLocksList
},
87 0, 0, { (DWORD_PTR
)(__FILE__
": dinput_hook_crit") }
89 static CRITICAL_SECTION dinput_hook_crit
= { &dinput_critsect_debug
, -1, 0, 0, 0, 0 };
91 static struct list acquired_mouse_list
= LIST_INIT( acquired_mouse_list
);
92 static struct list acquired_rawmouse_list
= LIST_INIT( acquired_rawmouse_list
);
93 static struct list acquired_keyboard_list
= LIST_INIT( acquired_keyboard_list
);
94 static struct list acquired_device_list
= LIST_INIT( acquired_device_list
);
96 void dinput_hooks_acquire_device( IDirectInputDevice8W
*iface
)
98 struct dinput_device
*impl
= impl_from_IDirectInputDevice8W( iface
);
100 EnterCriticalSection( &dinput_hook_crit
);
101 if (IsEqualGUID( &impl
->guid
, &GUID_SysMouse
))
102 list_add_tail( impl
->use_raw_input
? &acquired_rawmouse_list
: &acquired_mouse_list
, &impl
->entry
);
103 else if (IsEqualGUID( &impl
->guid
, &GUID_SysKeyboard
))
104 list_add_tail( &acquired_keyboard_list
, &impl
->entry
);
106 list_add_tail( &acquired_device_list
, &impl
->entry
);
107 LeaveCriticalSection( &dinput_hook_crit
);
109 SendMessageW( di_em_win
, INPUT_THREAD_NOTIFY
, NOTIFY_REFRESH_DEVICES
, 0 );
112 void dinput_hooks_unacquire_device( IDirectInputDevice8W
*iface
)
114 struct dinput_device
*impl
= impl_from_IDirectInputDevice8W( iface
);
116 EnterCriticalSection( &dinput_hook_crit
);
117 list_remove( &impl
->entry
);
118 LeaveCriticalSection( &dinput_hook_crit
);
120 SendMessageW( di_em_win
, INPUT_THREAD_NOTIFY
, NOTIFY_REFRESH_DEVICES
, 0 );
123 static void dinput_device_internal_unacquire( IDirectInputDevice8W
*iface
, DWORD status
)
125 struct dinput_device
*impl
= impl_from_IDirectInputDevice8W( iface
);
127 TRACE( "iface %p.\n", iface
);
129 EnterCriticalSection( &impl
->crit
);
130 if (impl
->status
== STATUS_ACQUIRED
)
132 impl
->vtbl
->unacquire( iface
);
133 impl
->status
= status
;
134 list_remove( &impl
->entry
);
136 LeaveCriticalSection( &impl
->crit
);
139 static LRESULT CALLBACK
input_thread_ll_hook_proc( int code
, WPARAM wparam
, LPARAM lparam
)
141 struct input_thread_state
*state
= input_thread_state
;
144 if (code
!= HC_ACTION
) return CallNextHookEx( 0, code
, wparam
, lparam
);
146 for (i
= state
->events_count
; i
< state
->devices_count
; ++i
)
148 struct dinput_device
*device
= state
->devices
[i
];
149 if (device
->use_raw_input
) continue;
150 if (device
->instance
.dwDevType
& DIDEVTYPE_HID
) continue;
151 switch (GET_DIDEVICE_TYPE( device
->instance
.dwDevType
))
153 case DIDEVTYPE_MOUSE
:
154 case DI8DEVTYPE_MOUSE
:
155 TRACE( "calling dinput_mouse_hook (%p %Ix %Ix)\n", device
, wparam
, lparam
);
156 skip
|= dinput_mouse_hook( &device
->IDirectInputDevice8W_iface
, wparam
, lparam
);
158 case DIDEVTYPE_KEYBOARD
:
159 case DI8DEVTYPE_KEYBOARD
:
160 TRACE( "calling dinput_keyboard_hook (%p %Ix %Ix)\n", device
, wparam
, lparam
);
161 skip
|= dinput_keyboard_hook( &device
->IDirectInputDevice8W_iface
, wparam
, lparam
);
166 return skip
? 1 : CallNextHookEx( 0, code
, wparam
, lparam
);
169 static void dinput_unacquire_window_devices( HWND window
)
171 struct dinput_device
*impl
, *next
;
173 EnterCriticalSection( &dinput_hook_crit
);
175 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_device_list
, struct dinput_device
, entry
)
177 if (window
!= impl
->win
) continue;
178 TRACE( "%p window is not foreground - unacquiring %p\n", impl
->win
, impl
);
179 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
181 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_mouse_list
, struct dinput_device
, entry
)
183 if (window
!= impl
->win
) continue;
184 TRACE( "%p window is not foreground - unacquiring %p\n", impl
->win
, impl
);
185 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
187 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_rawmouse_list
, struct dinput_device
, entry
)
189 if (window
!= impl
->win
) continue;
190 TRACE( "%p window is not foreground - unacquiring %p\n", impl
->win
, impl
);
191 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
193 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_keyboard_list
, struct dinput_device
, entry
)
195 if (window
!= impl
->win
) continue;
196 TRACE( "%p window is not foreground - unacquiring %p\n", impl
->win
, impl
);
197 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
200 LeaveCriticalSection( &dinput_hook_crit
);
203 static void dinput_unacquire_devices(void)
205 struct dinput_device
*impl
, *next
;
207 EnterCriticalSection( &dinput_hook_crit
);
209 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_device_list
, struct dinput_device
, entry
)
210 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
211 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_mouse_list
, struct dinput_device
, entry
)
212 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
213 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_rawmouse_list
, struct dinput_device
, entry
)
214 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
215 LIST_FOR_EACH_ENTRY_SAFE( impl
, next
, &acquired_keyboard_list
, struct dinput_device
, entry
)
216 dinput_device_internal_unacquire( &impl
->IDirectInputDevice8W_iface
, STATUS_UNACQUIRED
);
218 LeaveCriticalSection( &dinput_hook_crit
);
221 static LRESULT CALLBACK
cbt_hook_proc( int code
, WPARAM wparam
, LPARAM lparam
)
223 if (code
== HCBT_ACTIVATE
&& di_em_win
)
225 CBTACTIVATESTRUCT
*data
= (CBTACTIVATESTRUCT
*)lparam
;
226 SendMessageW( di_em_win
, INPUT_THREAD_NOTIFY
, NOTIFY_FOREGROUND_LOST
,
227 (LPARAM
)data
->hWndActive
);
230 return CallNextHookEx( 0, code
, wparam
, lparam
);
233 static void input_thread_update_device_list( struct input_thread_state
*state
)
235 RAWINPUTDEVICE rawinput_mouse
= {.usUsagePage
= HID_USAGE_PAGE_GENERIC
, .usUsage
= HID_USAGE_GENERIC_MOUSE
, .dwFlags
= RIDEV_REMOVE
};
236 UINT count
= 0, keyboard_count
= 0, mouse_count
= 0, foreground_count
= 0;
237 struct dinput_device
*device
;
239 EnterCriticalSection( &dinput_hook_crit
);
240 LIST_FOR_EACH_ENTRY( device
, &acquired_device_list
, struct dinput_device
, entry
)
242 if (device
->dwCoopLevel
& DISCL_FOREGROUND
) foreground_count
++;
243 if (!device
->read_event
|| !device
->vtbl
->read
) continue;
244 state
->events
[count
] = device
->read_event
;
245 dinput_device_internal_addref( (state
->devices
[count
] = device
) );
246 if (++count
>= INPUT_THREAD_MAX_DEVICES
) break;
248 state
->events_count
= count
;
250 LIST_FOR_EACH_ENTRY( device
, &acquired_rawmouse_list
, struct dinput_device
, entry
)
252 if (device
->dwCoopLevel
& DISCL_FOREGROUND
) foreground_count
++;
253 if (device
->dwCoopLevel
& DISCL_BACKGROUND
) rawinput_mouse
.dwFlags
|= RIDEV_INPUTSINK
;
254 if (device
->dwCoopLevel
& DISCL_EXCLUSIVE
) rawinput_mouse
.dwFlags
|= RIDEV_NOLEGACY
| RIDEV_CAPTUREMOUSE
;
255 rawinput_mouse
.dwFlags
&= ~RIDEV_REMOVE
;
256 rawinput_mouse
.hwndTarget
= di_em_win
;
257 if (count
< INPUT_THREAD_MAX_DEVICES
) dinput_device_internal_addref( (state
->devices
[count
++] = device
) );
259 LIST_FOR_EACH_ENTRY( device
, &acquired_mouse_list
, struct dinput_device
, entry
)
261 if (device
->dwCoopLevel
& DISCL_FOREGROUND
) foreground_count
++;
263 if (count
< INPUT_THREAD_MAX_DEVICES
) dinput_device_internal_addref( (state
->devices
[count
++] = device
) );
265 LIST_FOR_EACH_ENTRY( device
, &acquired_keyboard_list
, struct dinput_device
, entry
)
267 if (device
->dwCoopLevel
& DISCL_FOREGROUND
) foreground_count
++;
269 if (count
< INPUT_THREAD_MAX_DEVICES
) dinput_device_internal_addref( (state
->devices
[count
++] = device
) );
271 state
->devices_count
= count
;
272 LeaveCriticalSection( &dinput_hook_crit
);
274 if (foreground_count
&& !state
->callwndproc_hook
)
275 state
->callwndproc_hook
= SetWindowsHookExW( WH_CBT
, cbt_hook_proc
, DINPUT_instance
, 0 );
276 else if (!foreground_count
&& state
->callwndproc_hook
)
278 UnhookWindowsHookEx( state
->callwndproc_hook
);
279 state
->callwndproc_hook
= NULL
;
282 if (keyboard_count
&& !state
->keyboard_ll_hook
)
283 state
->keyboard_ll_hook
= SetWindowsHookExW( WH_KEYBOARD_LL
, input_thread_ll_hook_proc
, DINPUT_instance
, 0 );
284 else if (!keyboard_count
&& state
->keyboard_ll_hook
)
286 UnhookWindowsHookEx( state
->keyboard_ll_hook
);
287 state
->keyboard_ll_hook
= NULL
;
290 if (mouse_count
&& !state
->mouse_ll_hook
)
291 state
->mouse_ll_hook
= SetWindowsHookExW( WH_MOUSE_LL
, input_thread_ll_hook_proc
, DINPUT_instance
, 0 );
292 else if (!mouse_count
&& state
->mouse_ll_hook
)
294 UnhookWindowsHookEx( state
->mouse_ll_hook
);
295 state
->mouse_ll_hook
= NULL
;
298 if (!rawinput_mouse
.hwndTarget
!= !state
->rawinput_devices
[0].hwndTarget
&&
299 !RegisterRawInputDevices( &rawinput_mouse
, 1, sizeof(RAWINPUTDEVICE
) ))
300 WARN( "Failed to (un)register rawinput mouse device.\n" );
302 state
->rawinput_devices
[0] = rawinput_mouse
;
305 static LRESULT WINAPI
di_em_win_wndproc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
307 struct input_thread_state
*state
= input_thread_state
;
308 int rim
= GET_RAWINPUT_CODE_WPARAM( wparam
);
309 UINT i
, size
= sizeof(RAWINPUT
);
312 TRACE( "%p %d %Ix %Ix\n", hwnd
, msg
, wparam
, lparam
);
314 if (msg
== WM_INPUT
&& (rim
== RIM_INPUT
|| rim
== RIM_INPUTSINK
))
316 size
= GetRawInputData( (HRAWINPUT
)lparam
, RID_INPUT
, &ri
, &size
, sizeof(RAWINPUTHEADER
) );
317 if (size
== (UINT
)-1 || size
< sizeof(RAWINPUTHEADER
))
318 WARN( "Unable to read raw input data\n" );
319 else if (ri
.header
.dwType
== RIM_TYPEMOUSE
)
321 for (i
= state
->events_count
; i
< state
->devices_count
; ++i
)
323 struct dinput_device
*device
= state
->devices
[i
];
324 if (!device
->use_raw_input
) continue;
325 if (device
->instance
.dwDevType
& DIDEVTYPE_HID
) continue;
326 switch (GET_DIDEVICE_TYPE( device
->instance
.dwDevType
))
328 case DIDEVTYPE_MOUSE
:
329 case DI8DEVTYPE_MOUSE
:
330 dinput_mouse_rawinput_hook( &device
->IDirectInputDevice8W_iface
, wparam
, lparam
, &ri
);
338 if (msg
== INPUT_THREAD_NOTIFY
)
340 TRACE( "Processing hook change notification wparam %#Ix, lparam %#Ix.\n", wparam
, lparam
);
344 case NOTIFY_THREAD_STOP
:
345 state
->running
= FALSE
;
347 case NOTIFY_REFRESH_DEVICES
:
348 while (state
->devices_count
--) dinput_device_internal_release( state
->devices
[state
->devices_count
] );
349 input_thread_update_device_list( state
);
351 case NOTIFY_FOREGROUND_LOST
:
352 dinput_unacquire_window_devices( (HWND
)lparam
);
359 return DefWindowProcW( hwnd
, msg
, wparam
, lparam
);
362 static void register_di_em_win_class(void)
366 memset(&class, 0, sizeof(class));
367 class.cbSize
= sizeof(class);
368 class.lpfnWndProc
= di_em_win_wndproc
;
369 class.hInstance
= DINPUT_instance
;
370 class.lpszClassName
= L
"DIEmWin";
372 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS
)
373 WARN( "Unable to register message window class\n" );
376 static void unregister_di_em_win_class(void)
378 if (!UnregisterClassW( L
"DIEmWin", NULL
) && GetLastError() != ERROR_CLASS_DOES_NOT_EXIST
)
379 WARN( "Unable to unregister message window class\n" );
382 static DWORD WINAPI
dinput_thread_proc( void *params
)
384 struct input_thread_state state
= {.running
= TRUE
};
385 struct dinput_device
*device
;
386 HANDLE start_event
= params
;
390 di_em_win
= CreateWindowW( L
"DIEmWin", L
"DIEmWin", 0, 0, 0, 0, 0, HWND_MESSAGE
, 0, DINPUT_instance
, NULL
);
391 input_thread_state
= &state
;
392 SetEvent( start_event
);
394 while (state
.running
&& (ret
= MsgWaitForMultipleObjectsEx( state
.events_count
, state
.events
, INFINITE
, QS_ALLINPUT
, 0 )) <= state
.events_count
)
396 if (ret
< state
.events_count
)
398 if ((device
= state
.devices
[ret
]) && FAILED( device
->vtbl
->read( &device
->IDirectInputDevice8W_iface
) ))
400 EnterCriticalSection( &dinput_hook_crit
);
401 dinput_device_internal_unacquire( &device
->IDirectInputDevice8W_iface
, STATUS_UNPLUGGED
);
402 LeaveCriticalSection( &dinput_hook_crit
);
404 state
.events
[ret
] = state
.events
[--state
.events_count
];
405 state
.devices
[ret
] = state
.devices
[state
.events_count
];
406 state
.devices
[state
.events_count
] = state
.devices
[--state
.devices_count
];
407 dinput_device_internal_release( device
);
411 while (PeekMessageW( &msg
, 0, 0, 0, PM_REMOVE
))
413 TranslateMessage(&msg
);
414 DispatchMessageW(&msg
);
420 ERR( "Unexpected termination, ret %#lx\n", ret
);
421 dinput_unacquire_devices();
424 while (state
.devices_count
--) dinput_device_internal_release( state
.devices
[state
.devices_count
] );
425 if (state
.callwndproc_hook
) UnhookWindowsHookEx( state
.callwndproc_hook
);
426 if (state
.keyboard_ll_hook
) UnhookWindowsHookEx( state
.keyboard_ll_hook
);
427 if (state
.mouse_ll_hook
) UnhookWindowsHookEx( state
.mouse_ll_hook
);
428 DestroyWindow( di_em_win
);
433 void input_thread_add_user(void)
435 EnterCriticalSection( &dinput_hook_crit
);
436 if (!input_thread_user_count
++)
440 TRACE( "Starting input thread.\n" );
442 if (!(start_event
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
443 ERR( "Failed to create start event, error %lu\n", GetLastError() );
444 else if (!(dinput_thread
= CreateThread( NULL
, 0, dinput_thread_proc
, start_event
, 0, NULL
)))
445 ERR( "Failed to create internal thread, error %lu\n", GetLastError() );
447 WaitForSingleObject( start_event
, INFINITE
);
449 CloseHandle( start_event
);
451 LeaveCriticalSection( &dinput_hook_crit
);
454 void input_thread_remove_user(void)
456 EnterCriticalSection( &dinput_hook_crit
);
457 if (!--input_thread_user_count
)
459 TRACE( "Stopping input thread.\n" );
461 SendMessageW( di_em_win
, INPUT_THREAD_NOTIFY
, NOTIFY_THREAD_STOP
, 0 );
462 WaitForSingleObject( dinput_thread
, INFINITE
);
463 CloseHandle( dinput_thread
);
465 LeaveCriticalSection( &dinput_hook_crit
);
468 void check_dinput_events(void)
470 /* Windows does not do that, but our current implementation of winex11
471 * requires periodic event polling to forward events to the wineserver.
473 * We have to call this function from multiple places, because:
474 * - some games do not explicitly poll for mouse events
475 * (for example Culpa Innata)
476 * - some games only poll the device, and neither keyboard nor mouse
477 * (for example Civilization: Call to Power 2)
478 * - some games do not explicitly poll for keyboard events
479 * (for example Morrowind in its key binding page)
481 MsgWaitForMultipleObjectsEx(0, NULL
, 0, QS_ALLINPUT
, 0);
484 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, void *reserved
)
486 TRACE( "inst %p, reason %lu, reserved %p.\n", inst
, reason
, reserved
);
490 case DLL_PROCESS_ATTACH
:
491 DisableThreadLibraryCalls(inst
);
492 DINPUT_instance
= inst
;
493 register_di_em_win_class();
495 case DLL_PROCESS_DETACH
:
497 unregister_di_em_win_class();