mshtml: Implement MediaQueryList's addListener method.
[wine.git] / dlls / dinput / dinput_main.c
blob0d502821966fa792ed05585fe0868b62e79985ad
1 /* DirectInput
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
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <string.h>
28 #define COBJMACROS
29 #define NONAMELESSUNION
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "winerror.h"
35 #include "objbase.h"
36 #include "rpcproxy.h"
37 #include "devguid.h"
38 #include "hidusage.h"
39 #include "initguid.h"
40 #include "dinputd.h"
42 #include "dinput_private.h"
43 #include "device_private.h"
45 #include "wine/asm.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
59 BOOL running;
60 UINT events_count;
61 UINT devices_count;
62 HHOOK mouse_ll_hook;
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 void dinput_hooks_acquire_device( IDirectInputDevice8W *iface )
94 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
96 EnterCriticalSection( &dinput_hook_crit );
97 list_add_tail( &acquired_device_list, &impl->entry );
98 LeaveCriticalSection( &dinput_hook_crit );
100 SendMessageW( di_em_win, INPUT_THREAD_NOTIFY, NOTIFY_REFRESH_DEVICES, 0 );
103 void dinput_hooks_unacquire_device( IDirectInputDevice8W *iface )
105 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
107 EnterCriticalSection( &dinput_hook_crit );
108 list_remove( &impl->entry );
109 LeaveCriticalSection( &dinput_hook_crit );
111 SendMessageW( di_em_win, INPUT_THREAD_NOTIFY, NOTIFY_REFRESH_DEVICES, 0 );
114 static void unhook_device_window_foreground_changes( struct dinput_device *device )
116 if (!device->cbt_hook) return;
117 UnhookWindowsHookEx( device->cbt_hook );
118 device->cbt_hook = NULL;
121 static void dinput_device_internal_unacquire( IDirectInputDevice8W *iface, DWORD status )
123 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
125 TRACE( "iface %p.\n", iface );
127 unhook_device_window_foreground_changes( impl );
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;
142 int i, skip = 0;
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 );
157 break;
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 );
162 break;
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 );
182 LeaveCriticalSection( &dinput_hook_crit );
185 static void dinput_unacquire_devices(void)
187 struct dinput_device *impl, *next;
189 EnterCriticalSection( &dinput_hook_crit );
191 LIST_FOR_EACH_ENTRY_SAFE( impl, next, &acquired_device_list, struct dinput_device, entry )
192 dinput_device_internal_unacquire( &impl->IDirectInputDevice8W_iface, STATUS_UNACQUIRED );
194 LeaveCriticalSection( &dinput_hook_crit );
197 static LRESULT CALLBACK cbt_hook_proc( int code, WPARAM wparam, LPARAM lparam )
199 if (code == HCBT_ACTIVATE && di_em_win)
201 CBTACTIVATESTRUCT *data = (CBTACTIVATESTRUCT *)lparam;
202 SendMessageW( di_em_win, INPUT_THREAD_NOTIFY, NOTIFY_FOREGROUND_LOST,
203 (LPARAM)data->hWndActive );
206 return CallNextHookEx( 0, code, wparam, lparam );
209 static void hook_device_window_foreground_changes( struct dinput_device *device )
211 DWORD tid, pid;
212 if (!(tid = GetWindowThreadProcessId( device->win, &pid ))) return;
213 device->cbt_hook = SetWindowsHookExW( WH_CBT, cbt_hook_proc, DINPUT_instance, tid );
216 static void input_thread_update_device_list( struct input_thread_state *state )
218 RAWINPUTDEVICE rawinput_keyboard = {.usUsagePage = HID_USAGE_PAGE_GENERIC, .usUsage = HID_USAGE_GENERIC_KEYBOARD, .dwFlags = RIDEV_REMOVE};
219 RAWINPUTDEVICE rawinput_mouse = {.usUsagePage = HID_USAGE_PAGE_GENERIC, .usUsage = HID_USAGE_GENERIC_MOUSE, .dwFlags = RIDEV_REMOVE};
220 UINT count = 0, keyboard_ll_count = 0, mouse_ll_count = 0;
221 struct dinput_device *device;
223 EnterCriticalSection( &dinput_hook_crit );
224 LIST_FOR_EACH_ENTRY( device, &acquired_device_list, struct dinput_device, entry )
226 unhook_device_window_foreground_changes( device );
227 if (device->dwCoopLevel & DISCL_FOREGROUND) hook_device_window_foreground_changes( device );
229 if (!device->read_event || !device->vtbl->read) continue;
230 state->events[count] = device->read_event;
231 dinput_device_internal_addref( (state->devices[count] = device) );
232 if (++count >= INPUT_THREAD_MAX_DEVICES) break;
234 state->events_count = count;
236 LIST_FOR_EACH_ENTRY( device, &acquired_device_list, struct dinput_device, entry )
238 RAWINPUTDEVICE *rawinput_device = NULL;
240 if (device->read_event && device->vtbl->read) continue;
241 switch (GET_DIDEVICE_TYPE( device->instance.dwDevType ))
243 case DIDEVTYPE_MOUSE:
244 case DI8DEVTYPE_MOUSE:
245 if (device->dwCoopLevel & DISCL_EXCLUSIVE) rawinput_mouse.dwFlags |= RIDEV_CAPTUREMOUSE;
246 if (!device->use_raw_input) mouse_ll_count++;
247 else rawinput_device = &rawinput_mouse;
248 break;
249 case DIDEVTYPE_KEYBOARD:
250 case DI8DEVTYPE_KEYBOARD:
251 if (device->dwCoopLevel & DISCL_EXCLUSIVE) rawinput_keyboard.dwFlags |= RIDEV_NOHOTKEYS;
252 if (!device->use_raw_input) keyboard_ll_count++;
253 else rawinput_device = &rawinput_keyboard;
254 break;
257 if (rawinput_device)
259 if (device->dwCoopLevel & DISCL_BACKGROUND) rawinput_device->dwFlags |= RIDEV_INPUTSINK;
260 if (device->dwCoopLevel & DISCL_EXCLUSIVE) rawinput_device->dwFlags |= RIDEV_NOLEGACY;
261 rawinput_device->dwFlags &= ~RIDEV_REMOVE;
262 rawinput_device->hwndTarget = di_em_win;
265 if (count < INPUT_THREAD_MAX_DEVICES) dinput_device_internal_addref( (state->devices[count++] = device) );
267 state->devices_count = count;
268 LeaveCriticalSection( &dinput_hook_crit );
270 if (keyboard_ll_count && !state->keyboard_ll_hook)
271 state->keyboard_ll_hook = SetWindowsHookExW( WH_KEYBOARD_LL, input_thread_ll_hook_proc, DINPUT_instance, 0 );
272 else if (!keyboard_ll_count && state->keyboard_ll_hook)
274 UnhookWindowsHookEx( state->keyboard_ll_hook );
275 state->keyboard_ll_hook = NULL;
278 if (mouse_ll_count && !state->mouse_ll_hook)
279 state->mouse_ll_hook = SetWindowsHookExW( WH_MOUSE_LL, input_thread_ll_hook_proc, DINPUT_instance, 0 );
280 else if (!mouse_ll_count && state->mouse_ll_hook)
282 UnhookWindowsHookEx( state->mouse_ll_hook );
283 state->mouse_ll_hook = NULL;
286 if (!rawinput_mouse.hwndTarget != !state->rawinput_devices[0].hwndTarget &&
287 !RegisterRawInputDevices( &rawinput_mouse, 1, sizeof(RAWINPUTDEVICE) ))
288 WARN( "Failed to (un)register rawinput mouse device.\n" );
289 if (!rawinput_keyboard.hwndTarget != !state->rawinput_devices[1].hwndTarget &&
290 !RegisterRawInputDevices( &rawinput_keyboard, 1, sizeof(RAWINPUTDEVICE) ))
291 WARN( "Failed to (un)register rawinput mouse device.\n" );
293 state->rawinput_devices[0] = rawinput_mouse;
294 state->rawinput_devices[1] = rawinput_keyboard;
297 static LRESULT WINAPI di_em_win_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
299 struct input_thread_state *state = input_thread_state;
300 int rim = GET_RAWINPUT_CODE_WPARAM( wparam );
301 UINT i, size = sizeof(RAWINPUT);
302 RAWINPUT ri;
304 TRACE( "%p %d %Ix %Ix\n", hwnd, msg, wparam, lparam );
306 if (msg == WM_INPUT && (rim == RIM_INPUT || rim == RIM_INPUTSINK))
308 size = GetRawInputData( (HRAWINPUT)lparam, RID_INPUT, &ri, &size, sizeof(RAWINPUTHEADER) );
309 if (size == (UINT)-1 || size < sizeof(RAWINPUTHEADER))
310 WARN( "Unable to read raw input data\n" );
311 else if (ri.header.dwType == RIM_TYPEHID)
312 WARN( "Unexpected HID rawinput message\n" );
313 else
315 for (i = state->events_count; i < state->devices_count; ++i)
317 struct dinput_device *device = state->devices[i];
318 if (!device->use_raw_input) continue;
319 if (device->instance.dwDevType & DIDEVTYPE_HID) continue;
320 switch (GET_DIDEVICE_TYPE( device->instance.dwDevType ))
322 case DIDEVTYPE_MOUSE:
323 case DI8DEVTYPE_MOUSE:
324 if (ri.header.dwType != RIM_TYPEMOUSE) break;
325 dinput_mouse_rawinput_hook( &device->IDirectInputDevice8W_iface, wparam, lparam, &ri );
326 break;
327 case DIDEVTYPE_KEYBOARD:
328 case DI8DEVTYPE_KEYBOARD:
329 if (ri.header.dwType != RIM_TYPEKEYBOARD) break;
330 dinput_keyboard_rawinput_hook( &device->IDirectInputDevice8W_iface, wparam, lparam, &ri );
331 break;
332 default: break;
338 if (msg == INPUT_THREAD_NOTIFY)
340 TRACE( "Processing hook change notification wparam %#Ix, lparam %#Ix.\n", wparam, lparam );
342 switch (wparam)
344 case NOTIFY_THREAD_STOP:
345 state->running = FALSE;
346 break;
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 );
350 break;
351 case NOTIFY_FOREGROUND_LOST:
352 dinput_unacquire_window_devices( (HWND)lparam );
353 break;
356 return 0;
359 return DefWindowProcW( hwnd, msg, wparam, lparam );
362 static void register_di_em_win_class(void)
364 WNDCLASSEXW class;
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;
387 DWORD ret;
388 MSG msg;
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);
418 if (state.running)
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.keyboard_ll_hook) UnhookWindowsHookEx( state.keyboard_ll_hook );
426 if (state.mouse_ll_hook) UnhookWindowsHookEx( state.mouse_ll_hook );
427 DestroyWindow( di_em_win );
428 di_em_win = NULL;
429 return 0;
432 void input_thread_add_user(void)
434 EnterCriticalSection( &dinput_hook_crit );
435 if (!input_thread_user_count++)
437 HANDLE start_event;
439 TRACE( "Starting input thread.\n" );
441 if (!(start_event = CreateEventW( NULL, FALSE, FALSE, NULL )))
442 ERR( "Failed to create start event, error %lu\n", GetLastError() );
443 else if (!(dinput_thread = CreateThread( NULL, 0, dinput_thread_proc, start_event, 0, NULL )))
444 ERR( "Failed to create internal thread, error %lu\n", GetLastError() );
445 else
446 WaitForSingleObject( start_event, INFINITE );
448 CloseHandle( start_event );
450 LeaveCriticalSection( &dinput_hook_crit );
453 void input_thread_remove_user(void)
455 EnterCriticalSection( &dinput_hook_crit );
456 if (!--input_thread_user_count)
458 TRACE( "Stopping input thread.\n" );
460 SendMessageW( di_em_win, INPUT_THREAD_NOTIFY, NOTIFY_THREAD_STOP, 0 );
461 WaitForSingleObject( dinput_thread, INFINITE );
462 CloseHandle( dinput_thread );
464 LeaveCriticalSection( &dinput_hook_crit );
467 void check_dinput_events(void)
469 /* Windows does not do that, but our current implementation of winex11
470 * requires periodic event polling to forward events to the wineserver.
472 * We have to call this function from multiple places, because:
473 * - some games do not explicitly poll for mouse events
474 * (for example Culpa Innata)
475 * - some games only poll the device, and neither keyboard nor mouse
476 * (for example Civilization: Call to Power 2)
477 * - some games do not explicitly poll for keyboard events
478 * (for example Morrowind in its key binding page)
480 MsgWaitForMultipleObjectsEx(0, NULL, 0, QS_ALLINPUT, 0);
483 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, void *reserved )
485 TRACE( "inst %p, reason %lu, reserved %p.\n", inst, reason, reserved );
487 switch(reason)
489 case DLL_PROCESS_ATTACH:
490 DisableThreadLibraryCalls(inst);
491 DINPUT_instance = inst;
492 register_di_em_win_class();
493 break;
494 case DLL_PROCESS_DETACH:
495 if (reserved) break;
496 unregister_di_em_win_class();
497 break;
499 return TRUE;