dinput: Get rid of unused parameter in match_device_object.
[wine.git] / dlls / dinput / device.c
blobe15a22bb413b2ea0b966d713f53e675190243c47
1 /* DirectInput Device
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 /* This file contains all the Device specific functions that can be used as stubs
23 by real device implementations.
25 It also contains all the helper functions.
28 #include <stdarg.h>
29 #include <string.h>
30 #include <math.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "winuser.h"
36 #include "winerror.h"
37 #include "dinput.h"
38 #include "dinputd.h"
39 #include "hidusage.h"
41 #include "initguid.h"
42 #include "device_private.h"
43 #include "dinput_private.h"
45 #include "wine/debug.h"
47 #define WM_WINE_NOTIFY_ACTIVITY WM_USER
49 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
51 /* Windows uses this GUID for guidProduct on non-keyboard/mouse devices.
52 * Data1 contains the device VID (low word) and PID (high word).
53 * Data4 ends with the ASCII bytes "PIDVID".
55 DEFINE_GUID( dinput_pidvid_guid, 0x00000000, 0x0000, 0x0000, 0x00, 0x00, 'P', 'I', 'D', 'V', 'I', 'D' );
57 static inline struct dinput_device *impl_from_IDirectInputDevice8W( IDirectInputDevice8W *iface )
59 return CONTAINING_RECORD( iface, struct dinput_device, IDirectInputDevice8W_iface );
62 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl( struct dinput_device *This )
64 return &This->IDirectInputDevice8A_iface;
66 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl( struct dinput_device *This )
68 return &This->IDirectInputDevice8W_iface;
71 static inline const char *debugstr_didataformat( const DIDATAFORMAT *data )
73 if (!data) return "(null)";
74 return wine_dbg_sprintf( "%p dwSize %lu, dwObjsize %lu, dwFlags %#lx, dwDataSize %lu, dwNumObjs %lu, rgodf %p",
75 data, data->dwSize, data->dwObjSize, data->dwFlags, data->dwDataSize, data->dwNumObjs, data->rgodf );
78 static inline const char *debugstr_diobjectdataformat( const DIOBJECTDATAFORMAT *data )
80 if (!data) return "(null)";
81 return wine_dbg_sprintf( "%p pguid %s, dwOfs %#lx, dwType %#lx, dwFlags %#lx", data,
82 debugstr_guid( data->pguid ), data->dwOfs, data->dwType, data->dwFlags );
85 static inline BOOL is_exclusively_acquired( struct dinput_device *device )
87 return device->status == STATUS_ACQUIRED && (device->dwCoopLevel & DISCL_EXCLUSIVE);
90 /******************************************************************************
91 * Various debugging tools
93 static void _dump_cooperativelevel_DI(DWORD dwFlags) {
94 if (TRACE_ON(dinput)) {
95 unsigned int i;
96 static const struct {
97 DWORD mask;
98 const char *name;
99 } flags[] = {
100 #define FE(x) { x, #x}
101 FE(DISCL_BACKGROUND),
102 FE(DISCL_EXCLUSIVE),
103 FE(DISCL_FOREGROUND),
104 FE(DISCL_NONEXCLUSIVE),
105 FE(DISCL_NOWINKEY)
106 #undef FE
108 TRACE(" cooperative level : ");
109 for (i = 0; i < ARRAY_SIZE(flags); i++)
110 if (flags[i].mask & dwFlags)
111 TRACE("%s ",flags[i].name);
112 TRACE("\n");
116 /******************************************************************************
117 * Get the default and the app-specific config keys.
119 BOOL get_app_key(HKEY *defkey, HKEY *appkey)
121 char buffer[MAX_PATH+16];
122 DWORD len;
124 *appkey = 0;
126 /* @@ Wine registry key: HKCU\Software\Wine\DirectInput */
127 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\DirectInput", defkey))
128 *defkey = 0;
130 len = GetModuleFileNameA(0, buffer, MAX_PATH);
131 if (len && len < MAX_PATH)
133 HKEY tmpkey;
135 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectInput */
136 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
138 char *p, *appname = buffer;
139 if ((p = strrchr(appname, '/'))) appname = p + 1;
140 if ((p = strrchr(appname, '\\'))) appname = p + 1;
141 strcat(appname, "\\DirectInput");
143 if (RegOpenKeyA(tmpkey, appname, appkey)) *appkey = 0;
144 RegCloseKey(tmpkey);
148 return *defkey || *appkey;
151 /******************************************************************************
152 * Get a config key from either the app-specific or the default config
154 DWORD get_config_key( HKEY defkey, HKEY appkey, const WCHAR *name, WCHAR *buffer, DWORD size )
156 if (appkey && !RegQueryValueExW( appkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
158 if (defkey && !RegQueryValueExW( defkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
160 return ERROR_FILE_NOT_FOUND;
163 BOOL device_instance_is_disabled( DIDEVICEINSTANCEW *instance, BOOL *override )
165 static const WCHAR disabled_str[] = {'d', 'i', 's', 'a', 'b', 'l', 'e', 'd', 0};
166 static const WCHAR override_str[] = {'o', 'v', 'e', 'r', 'r', 'i', 'd', 'e', 0};
167 static const WCHAR joystick_key[] = {'J', 'o', 'y', 's', 't', 'i', 'c', 'k', 's', 0};
168 WCHAR buffer[MAX_PATH];
169 HKEY hkey, appkey, temp;
170 BOOL disable = FALSE;
172 get_app_key( &hkey, &appkey );
173 if (override) *override = FALSE;
175 /* Joystick settings are in the 'joysticks' subkey */
176 if (appkey)
178 if (RegOpenKeyW( appkey, joystick_key, &temp )) temp = 0;
179 RegCloseKey( appkey );
180 appkey = temp;
183 if (hkey)
185 if (RegOpenKeyW( hkey, joystick_key, &temp )) temp = 0;
186 RegCloseKey( hkey );
187 hkey = temp;
190 /* Look for the "controllername"="disabled" key */
191 if (!get_config_key( hkey, appkey, instance->tszInstanceName, buffer, sizeof(buffer) ))
193 if (!wcscmp( disabled_str, buffer ))
195 TRACE( "Disabling joystick '%s' based on registry key.\n", debugstr_w(instance->tszInstanceName) );
196 disable = TRUE;
198 else if (override && !wcscmp( override_str, buffer ))
200 TRACE( "Force enabling joystick '%s' based on registry key.\n", debugstr_w(instance->tszInstanceName) );
201 *override = TRUE;
205 if (appkey) RegCloseKey( appkey );
206 if (hkey) RegCloseKey( hkey );
208 return disable;
211 static void dinput_device_release_user_format( struct dinput_device *impl )
213 free( impl->user_format.rgodf );
214 impl->user_format.rgodf = NULL;
217 static inline LPDIOBJECTDATAFORMAT dataformat_to_odf(LPCDIDATAFORMAT df, int idx)
219 if (idx < 0 || idx >= df->dwNumObjs) return NULL;
220 return (LPDIOBJECTDATAFORMAT)((LPBYTE)df->rgodf + idx * df->dwObjSize);
223 /* dataformat_to_odf_by_type
224 * Find the Nth object of the selected type in the DataFormat
226 LPDIOBJECTDATAFORMAT dataformat_to_odf_by_type(LPCDIDATAFORMAT df, int n, DWORD type)
228 int i, nfound = 0;
230 for (i=0; i < df->dwNumObjs; i++)
232 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(df, i);
234 if (odf->dwType & type)
236 if (n == nfound)
237 return odf;
239 nfound++;
243 return NULL;
246 static BOOL match_device_object( const DIDATAFORMAT *device_format, DIDATAFORMAT *user_format,
247 const DIOBJECTDATAFORMAT *match_obj, DWORD version )
249 DWORD i, device_instance, instance = DIDFT_GETINSTANCE( match_obj->dwType );
250 DIOBJECTDATAFORMAT *device_obj, *user_obj;
252 if (version < 0x0700 && instance == 0xff) instance = 0xffff;
254 for (i = 0; i < device_format->dwNumObjs; i++)
256 user_obj = user_format->rgodf + i;
257 device_obj = device_format->rgodf + i;
258 device_instance = DIDFT_GETINSTANCE( device_obj->dwType );
260 if (!(user_obj->dwType & DIDFT_OPTIONAL)) continue; /* already matched */
261 if (match_obj->pguid && device_obj->pguid && !IsEqualGUID( device_obj->pguid, match_obj->pguid )) continue;
262 if (instance != DIDFT_GETINSTANCE( DIDFT_ANYINSTANCE ) && instance != device_instance) continue;
263 if (!(DIDFT_GETTYPE( match_obj->dwType ) & DIDFT_GETTYPE( device_obj->dwType ))) continue;
265 TRACE( "match %s with device %s\n", debugstr_diobjectdataformat( match_obj ),
266 debugstr_diobjectdataformat( device_obj ) );
268 *user_obj = *device_obj;
269 user_obj->dwOfs = match_obj->dwOfs;
270 return TRUE;
273 return FALSE;
276 static HRESULT dinput_device_init_user_format( struct dinput_device *impl, const DIDATAFORMAT *format )
278 DIDATAFORMAT *user_format = &impl->user_format, *device_format = &impl->device_format;
279 DIOBJECTDATAFORMAT *user_obj, *match_obj;
280 DWORD i;
282 *user_format = *device_format;
283 user_format->dwFlags = format->dwFlags;
284 user_format->dwDataSize = format->dwDataSize;
285 user_format->dwNumObjs += format->dwNumObjs;
286 if (!(user_format->rgodf = calloc( user_format->dwNumObjs, sizeof(DIOBJECTDATAFORMAT) ))) return DIERR_OUTOFMEMORY;
288 user_obj = user_format->rgodf + user_format->dwNumObjs;
289 while (user_obj-- > user_format->rgodf) user_obj->dwType |= DIDFT_OPTIONAL;
291 for (i = 0; i < format->dwNumObjs; ++i)
293 match_obj = format->rgodf + i;
295 if (!match_device_object( device_format, user_format, match_obj, impl->dinput->dwVersion ))
297 WARN( "object %s not found\n", debugstr_diobjectdataformat( match_obj ) );
298 if (!(match_obj->dwType & DIDFT_OPTIONAL)) goto failed;
299 user_obj = user_format->rgodf + device_format->dwNumObjs + i;
300 *user_obj = *match_obj;
304 user_obj = user_format->rgodf + user_format->dwNumObjs;
305 while (user_obj-- > user_format->rgodf) user_obj->dwType &= ~DIDFT_OPTIONAL;
307 return DI_OK;
309 failed:
310 free( user_format->rgodf );
311 user_format->rgodf = NULL;
312 return DIERR_INVALIDPARAM;
315 static int id_to_offset( struct dinput_device *impl, int id )
317 DIDATAFORMAT *user_format = &impl->user_format;
318 DIOBJECTDATAFORMAT *user_obj;
320 if (!user_format->rgodf) return -1;
322 user_obj = user_format->rgodf + impl->device_format.dwNumObjs;
323 while (user_obj-- > user_format->rgodf)
325 if (!user_obj->dwType) continue;
326 if ((user_obj->dwType & 0x00ffffff) == (id & 0x00ffffff)) return user_obj->dwOfs;
329 return -1;
332 static DWORD semantic_to_obj_id( struct dinput_device *This, DWORD dwSemantic )
334 DWORD type = (0x0000ff00 & dwSemantic) >> 8;
335 BOOL byofs = (dwSemantic & 0x80000000) != 0;
336 DWORD value = (dwSemantic & 0x000000ff);
337 BOOL found = FALSE;
338 DWORD instance;
339 int i;
341 for (i = 0; i < This->device_format.dwNumObjs && !found; i++)
343 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf( &This->device_format, i );
345 if (byofs && value != odf->dwOfs) continue;
346 if (!byofs && value != DIDFT_GETINSTANCE(odf->dwType)) continue;
347 instance = DIDFT_GETINSTANCE(odf->dwType);
348 found = TRUE;
351 if (!found) return 0;
353 if (type & DIDFT_AXIS) type = DIDFT_RELAXIS;
354 if (type & DIDFT_BUTTON) type = DIDFT_PSHBUTTON;
356 return type | (0x0000ff00 & (instance << 8));
360 * get_mapping_key
361 * Retrieves an open registry key to save the mapping, parametrized for an username,
362 * specific device and specific action mapping guid.
364 static HKEY get_mapping_key(const WCHAR *device, const WCHAR *username, const WCHAR *guid)
366 static const WCHAR *subkey = L"Software\\Wine\\DirectInput\\Mappings\\%s\\%s\\%s";
367 HKEY hkey;
368 WCHAR *keyname;
370 SIZE_T len = wcslen( subkey ) + wcslen( username ) + wcslen( device ) + wcslen( guid ) + 1;
371 keyname = malloc( sizeof(WCHAR) * len );
372 swprintf( keyname, len, subkey, username, device, guid );
374 /* The key used is HKCU\Software\Wine\DirectInput\Mappings\[username]\[device]\[mapping_guid] */
375 if (RegCreateKeyW(HKEY_CURRENT_USER, keyname, &hkey))
376 hkey = 0;
378 free( keyname );
380 return hkey;
383 static HRESULT save_mapping_settings(IDirectInputDevice8W *iface, LPDIACTIONFORMATW lpdiaf, LPCWSTR lpszUsername)
385 WCHAR *guid_str = NULL;
386 DIDEVICEINSTANCEW didev;
387 HKEY hkey;
388 int i;
390 didev.dwSize = sizeof(didev);
391 IDirectInputDevice8_GetDeviceInfo(iface, &didev);
393 if (StringFromCLSID(&lpdiaf->guidActionMap, &guid_str) != S_OK)
394 return DI_SETTINGSNOTSAVED;
396 hkey = get_mapping_key(didev.tszInstanceName, lpszUsername, guid_str);
398 if (!hkey)
400 CoTaskMemFree(guid_str);
401 return DI_SETTINGSNOTSAVED;
404 /* Write each of the actions mapped for this device.
405 Format is "dwSemantic"="dwObjID" and key is of type REG_DWORD
407 for (i = 0; i < lpdiaf->dwNumActions; i++)
409 WCHAR label[9];
411 if (IsEqualGUID(&didev.guidInstance, &lpdiaf->rgoAction[i].guidInstance) &&
412 lpdiaf->rgoAction[i].dwHow != DIAH_UNMAPPED)
414 swprintf( label, 9, L"%x", lpdiaf->rgoAction[i].dwSemantic );
415 RegSetValueExW( hkey, label, 0, REG_DWORD, (const BYTE *)&lpdiaf->rgoAction[i].dwObjID,
416 sizeof(DWORD) );
420 RegCloseKey(hkey);
421 CoTaskMemFree(guid_str);
423 return DI_OK;
426 static BOOL load_mapping_settings( struct dinput_device *This, LPDIACTIONFORMATW lpdiaf, const WCHAR *username )
428 HKEY hkey;
429 WCHAR *guid_str;
430 DIDEVICEINSTANCEW didev;
431 int i, mapped = 0;
433 didev.dwSize = sizeof(didev);
434 IDirectInputDevice8_GetDeviceInfo(&This->IDirectInputDevice8W_iface, &didev);
436 if (StringFromCLSID(&lpdiaf->guidActionMap, &guid_str) != S_OK)
437 return FALSE;
439 hkey = get_mapping_key(didev.tszInstanceName, username, guid_str);
441 if (!hkey)
443 CoTaskMemFree(guid_str);
444 return FALSE;
447 /* Try to read each action in the DIACTIONFORMAT from registry */
448 for (i = 0; i < lpdiaf->dwNumActions; i++)
450 DWORD id, size = sizeof(DWORD);
451 WCHAR label[9];
453 swprintf( label, 9, L"%x", lpdiaf->rgoAction[i].dwSemantic );
455 if (!RegQueryValueExW(hkey, label, 0, NULL, (LPBYTE) &id, &size))
457 lpdiaf->rgoAction[i].dwObjID = id;
458 lpdiaf->rgoAction[i].guidInstance = didev.guidInstance;
459 lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
460 mapped += 1;
464 RegCloseKey(hkey);
465 CoTaskMemFree(guid_str);
467 return mapped > 0;
470 static BOOL set_app_data( struct dinput_device *dev, int offset, UINT_PTR app_data )
472 int num_actions = dev->num_actions;
473 ActionMap *action_map = dev->action_map, *target_map = NULL;
475 if (num_actions == 0)
477 num_actions = 1;
478 action_map = malloc( sizeof(ActionMap) );
479 if (!action_map) return FALSE;
480 target_map = &action_map[0];
482 else
484 int i;
485 for (i = 0; i < num_actions; i++)
487 if (dev->action_map[i].offset != offset) continue;
488 target_map = &dev->action_map[i];
489 break;
492 if (!target_map)
494 num_actions++;
495 action_map = realloc( action_map, sizeof(ActionMap) * num_actions );
496 if (!action_map) return FALSE;
497 target_map = &action_map[num_actions-1];
501 target_map->offset = offset;
502 target_map->uAppData = app_data;
504 dev->action_map = action_map;
505 dev->num_actions = num_actions;
507 return TRUE;
510 /******************************************************************************
511 * queue_event - add new event to the ring queue
514 void queue_event( IDirectInputDevice8W *iface, int inst_id, DWORD data, DWORD time, DWORD seq )
516 static ULONGLONG notify_ms = 0;
517 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
518 int next_pos, ofs = id_to_offset( This, inst_id );
519 ULONGLONG time_ms = GetTickCount64();
521 if (time_ms - notify_ms > 1000)
523 PostMessageW(GetDesktopWindow(), WM_WINE_NOTIFY_ACTIVITY, 0, 0);
524 notify_ms = time_ms;
527 if (!This->queue_len || This->overflow || ofs < 0) return;
529 next_pos = (This->queue_head + 1) % This->queue_len;
530 if (next_pos == This->queue_tail)
532 TRACE(" queue overflowed\n");
533 This->overflow = TRUE;
534 return;
537 TRACE( " queueing %lu at offset %u (queue head %u / size %u)\n", data, ofs, This->queue_head, This->queue_len );
539 This->data_queue[This->queue_head].dwOfs = ofs;
540 This->data_queue[This->queue_head].dwData = data;
541 This->data_queue[This->queue_head].dwTimeStamp = time;
542 This->data_queue[This->queue_head].dwSequence = seq;
543 This->data_queue[This->queue_head].uAppData = -1;
545 /* Set uAppData by means of action mapping */
546 if (This->num_actions > 0)
548 int i;
549 for (i=0; i < This->num_actions; i++)
551 if (This->action_map[i].offset == ofs)
553 TRACE( "Offset %d mapped to uAppData %#Ix\n", ofs, This->action_map[i].uAppData );
554 This->data_queue[This->queue_head].uAppData = This->action_map[i].uAppData;
555 break;
560 This->queue_head = next_pos;
561 /* Send event if asked */
564 /******************************************************************************
565 * Acquire
568 static HRESULT WINAPI dinput_device_Acquire( IDirectInputDevice8W *iface )
570 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
571 HRESULT hr = DI_OK;
573 TRACE( "iface %p.\n", iface );
575 EnterCriticalSection( &impl->crit );
576 if (impl->status == STATUS_ACQUIRED)
577 hr = DI_NOEFFECT;
578 else if (!impl->user_format.rgodf)
579 hr = DIERR_INVALIDPARAM;
580 else if ((impl->dwCoopLevel & DISCL_FOREGROUND) && impl->win != GetForegroundWindow())
581 hr = DIERR_OTHERAPPHASPRIO;
582 else
584 impl->status = STATUS_ACQUIRED;
585 if (FAILED(hr = impl->vtbl->acquire( iface ))) impl->status = STATUS_UNACQUIRED;
587 LeaveCriticalSection( &impl->crit );
588 if (hr != DI_OK) return hr;
590 dinput_hooks_acquire_device( iface );
591 check_dinput_hooks( iface, TRUE );
593 return hr;
596 /******************************************************************************
597 * Unacquire
600 static HRESULT WINAPI dinput_device_Unacquire( IDirectInputDevice8W *iface )
602 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
603 HRESULT hr = DI_OK;
605 TRACE( "iface %p.\n", iface );
607 EnterCriticalSection( &impl->crit );
608 if (impl->status != STATUS_ACQUIRED) hr = DI_NOEFFECT;
609 else hr = impl->vtbl->unacquire( iface );
610 impl->status = STATUS_UNACQUIRED;
611 LeaveCriticalSection( &impl->crit );
612 if (hr != DI_OK) return hr;
614 dinput_hooks_unacquire_device( iface );
615 check_dinput_hooks( iface, FALSE );
617 return hr;
620 /******************************************************************************
621 * IDirectInputDeviceA
624 static HRESULT WINAPI dinput_device_SetDataFormat( IDirectInputDevice8W *iface, const DIDATAFORMAT *format )
626 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
627 HRESULT res = DI_OK;
628 ULONG i;
630 TRACE( "iface %p, format %p.\n", iface, format );
632 if (!format) return E_POINTER;
633 if (TRACE_ON( dinput ))
635 TRACE( "user format %s\n", debugstr_didataformat( format ) );
636 for (i = 0; i < format->dwNumObjs; ++i) TRACE( " %lu: object %s\n", i, debugstr_diobjectdataformat( format->rgodf + i ) );
639 if (format->dwSize != sizeof(DIDATAFORMAT)) return DIERR_INVALIDPARAM;
640 if (format->dwObjSize != sizeof(DIOBJECTDATAFORMAT)) return DIERR_INVALIDPARAM;
641 if (This->status == STATUS_ACQUIRED) return DIERR_ACQUIRED;
643 EnterCriticalSection(&This->crit);
645 free( This->action_map );
646 This->action_map = NULL;
647 This->num_actions = 0;
649 dinput_device_release_user_format( This );
650 res = dinput_device_init_user_format( This, format );
652 LeaveCriticalSection(&This->crit);
653 return res;
656 /******************************************************************************
657 * SetCooperativeLevel
659 * Set cooperative level and the source window for the events.
661 static HRESULT WINAPI dinput_device_SetCooperativeLevel( IDirectInputDevice8W *iface, HWND hwnd, DWORD flags )
663 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
664 HRESULT hr;
666 TRACE( "iface %p, hwnd %p, flags %#lx.\n", iface, hwnd, flags );
668 _dump_cooperativelevel_DI( flags );
670 if ((flags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == 0 ||
671 (flags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE) ||
672 (flags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == 0 ||
673 (flags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == (DISCL_FOREGROUND | DISCL_BACKGROUND))
674 return DIERR_INVALIDPARAM;
676 if (hwnd && GetWindowLongW(hwnd, GWL_STYLE) & WS_CHILD) return E_HANDLE;
678 if (!hwnd && flags == (DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)) hwnd = GetDesktopWindow();
680 if (!IsWindow(hwnd)) return E_HANDLE;
682 /* For security reasons native does not allow exclusive background level
683 for mouse and keyboard only */
684 if (flags & DISCL_EXCLUSIVE && flags & DISCL_BACKGROUND &&
685 (IsEqualGUID( &This->guid, &GUID_SysMouse ) || IsEqualGUID( &This->guid, &GUID_SysKeyboard )))
686 return DIERR_UNSUPPORTED;
688 /* Store the window which asks for the mouse */
689 EnterCriticalSection(&This->crit);
690 if (This->status == STATUS_ACQUIRED) hr = DIERR_ACQUIRED;
691 else
693 This->win = hwnd;
694 This->dwCoopLevel = flags;
695 hr = DI_OK;
697 LeaveCriticalSection(&This->crit);
699 return hr;
702 static HRESULT WINAPI dinput_device_GetDeviceInfo( IDirectInputDevice8W *iface, DIDEVICEINSTANCEW *instance )
704 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
705 DWORD size;
707 TRACE( "iface %p, instance %p.\n", iface, instance );
709 if (!instance) return E_POINTER;
710 if (instance->dwSize != sizeof(DIDEVICEINSTANCE_DX3W) &&
711 instance->dwSize != sizeof(DIDEVICEINSTANCEW))
712 return DIERR_INVALIDPARAM;
714 size = instance->dwSize;
715 memcpy( instance, &impl->instance, size );
716 instance->dwSize = size;
718 return S_OK;
721 /******************************************************************************
722 * SetEventNotification : specifies event to be sent on state change
724 static HRESULT WINAPI dinput_device_SetEventNotification( IDirectInputDevice8W *iface, HANDLE event )
726 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
728 TRACE( "iface %p, event %p.\n", iface, event );
730 EnterCriticalSection(&This->crit);
731 This->hEvent = event;
732 LeaveCriticalSection(&This->crit);
733 return DI_OK;
736 void dinput_device_destroy( IDirectInputDevice8W *iface )
738 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
740 TRACE( "iface %p.\n", iface );
742 free( This->object_properties );
743 free( This->data_queue );
745 /* Free data format */
746 free( This->device_format.rgodf );
747 dinput_device_release_user_format( This );
749 /* Free action mapping */
750 free( This->action_map );
752 IDirectInput_Release(&This->dinput->IDirectInput7A_iface);
753 This->crit.DebugInfo->Spare[0] = 0;
754 DeleteCriticalSection(&This->crit);
756 free( This );
759 static ULONG WINAPI dinput_device_Release( IDirectInputDevice8W *iface )
761 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
762 ULONG ref = InterlockedDecrement( &impl->ref );
764 TRACE( "iface %p, ref %lu.\n", iface, ref );
766 if (!ref)
768 IDirectInputDevice_Unacquire( iface );
769 if (impl->vtbl->release) impl->vtbl->release( iface );
770 else dinput_device_destroy( iface );
773 return ref;
776 static HRESULT WINAPI dinput_device_GetCapabilities( IDirectInputDevice8W *iface, DIDEVCAPS *caps )
778 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
779 DWORD size;
781 TRACE( "iface %p, caps %p.\n", iface, caps );
783 if (!caps) return E_POINTER;
784 if (caps->dwSize != sizeof(DIDEVCAPS_DX3) &&
785 caps->dwSize != sizeof(DIDEVCAPS))
786 return DIERR_INVALIDPARAM;
788 size = caps->dwSize;
789 memcpy( caps, &impl->caps, size );
790 caps->dwSize = size;
792 return DI_OK;
795 static HRESULT WINAPI dinput_device_QueryInterface( IDirectInputDevice8W *iface, const GUID *iid, void **out )
797 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
799 TRACE( "iface %p, iid %s, out %p.\n", iface, debugstr_guid( iid ), out );
801 if (IsEqualGUID( &IID_IDirectInputDeviceA, iid ) ||
802 IsEqualGUID( &IID_IDirectInputDevice2A, iid ) ||
803 IsEqualGUID( &IID_IDirectInputDevice7A, iid ) ||
804 IsEqualGUID( &IID_IDirectInputDevice8A, iid ))
806 IDirectInputDevice2_AddRef(iface);
807 *out = IDirectInputDevice8A_from_impl( This );
808 return DI_OK;
811 if (IsEqualGUID( &IID_IUnknown, iid ) ||
812 IsEqualGUID( &IID_IDirectInputDeviceW, iid ) ||
813 IsEqualGUID( &IID_IDirectInputDevice2W, iid ) ||
814 IsEqualGUID( &IID_IDirectInputDevice7W, iid ) ||
815 IsEqualGUID( &IID_IDirectInputDevice8W, iid ))
817 IDirectInputDevice2_AddRef(iface);
818 *out = IDirectInputDevice8W_from_impl( This );
819 return DI_OK;
822 WARN( "%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid( iid ) );
823 return E_NOINTERFACE;
826 static ULONG WINAPI dinput_device_AddRef( IDirectInputDevice8W *iface )
828 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
829 ULONG ref = InterlockedIncrement( &impl->ref );
830 TRACE( "iface %p, ref %lu.\n", iface, ref );
831 return ref;
834 static HRESULT WINAPI dinput_device_EnumObjects( IDirectInputDevice8W *iface,
835 LPDIENUMDEVICEOBJECTSCALLBACKW callback,
836 void *context, DWORD flags )
838 static const DIPROPHEADER filter =
840 .dwSize = sizeof(filter),
841 .dwHeaderSize = sizeof(filter),
842 .dwHow = DIPH_DEVICE,
844 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
845 HRESULT hr;
847 TRACE( "iface %p, callback %p, context %p, flags %#lx.\n", iface, callback, context, flags );
849 if (!callback) return DIERR_INVALIDPARAM;
850 if (flags & ~(DIDFT_AXIS | DIDFT_POV | DIDFT_BUTTON | DIDFT_NODATA | DIDFT_COLLECTION))
851 return DIERR_INVALIDPARAM;
853 if (flags == DIDFT_ALL || (flags & DIDFT_AXIS))
855 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_AXIS, callback, context );
856 if (FAILED(hr)) return hr;
857 if (hr != DIENUM_CONTINUE) return DI_OK;
859 if (flags == DIDFT_ALL || (flags & DIDFT_POV))
861 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_POV, callback, context );
862 if (FAILED(hr)) return hr;
863 if (hr != DIENUM_CONTINUE) return DI_OK;
865 if (flags == DIDFT_ALL || (flags & DIDFT_BUTTON))
867 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_BUTTON, callback, context );
868 if (FAILED(hr)) return hr;
869 if (hr != DIENUM_CONTINUE) return DI_OK;
871 if (flags == DIDFT_ALL || (flags & (DIDFT_NODATA | DIDFT_COLLECTION)))
873 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_NODATA, callback, context );
874 if (FAILED(hr)) return hr;
875 if (hr != DIENUM_CONTINUE) return DI_OK;
878 return DI_OK;
881 static HRESULT enum_object_filter_init( struct dinput_device *impl, DIPROPHEADER *filter )
883 DIDATAFORMAT *device_format = &impl->device_format, *user_format = &impl->user_format;
884 DIOBJECTDATAFORMAT *device_obj, *user_obj;
886 if (filter->dwHow > DIPH_BYUSAGE) return DIERR_INVALIDPARAM;
887 if (filter->dwHow == DIPH_BYUSAGE && !(impl->instance.dwDevType & DIDEVTYPE_HID)) return DIERR_UNSUPPORTED;
888 if (filter->dwHow != DIPH_BYOFFSET) return DI_OK;
890 if (!user_format->rgodf) return DIERR_NOTFOUND;
892 user_obj = user_format->rgodf + device_format->dwNumObjs;
893 device_obj = device_format->rgodf + device_format->dwNumObjs;
894 while (user_obj-- > user_format->rgodf && device_obj-- > device_format->rgodf)
896 if (!user_obj->dwType) continue;
897 if (user_obj->dwOfs == filter->dwObj) break;
899 if (user_obj < user_format->rgodf) return DIERR_NOTFOUND;
901 filter->dwObj = device_obj->dwOfs;
902 return DI_OK;
905 static HRESULT check_property( struct dinput_device *impl, const GUID *guid, const DIPROPHEADER *header, BOOL set )
907 switch (LOWORD( guid ))
909 case (DWORD_PTR)DIPROP_VIDPID:
910 case (DWORD_PTR)DIPROP_TYPENAME:
911 case (DWORD_PTR)DIPROP_USERNAME:
912 case (DWORD_PTR)DIPROP_KEYNAME:
913 case (DWORD_PTR)DIPROP_LOGICALRANGE:
914 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
915 case (DWORD_PTR)DIPROP_APPDATA:
916 if (impl->dinput->dwVersion < 0x0800) return DIERR_UNSUPPORTED;
917 break;
920 switch (LOWORD( guid ))
922 case (DWORD_PTR)DIPROP_INSTANCENAME:
923 case (DWORD_PTR)DIPROP_KEYNAME:
924 case (DWORD_PTR)DIPROP_PRODUCTNAME:
925 case (DWORD_PTR)DIPROP_TYPENAME:
926 case (DWORD_PTR)DIPROP_USERNAME:
927 if (header->dwSize != sizeof(DIPROPSTRING)) return DIERR_INVALIDPARAM;
928 break;
930 case (DWORD_PTR)DIPROP_AUTOCENTER:
931 case (DWORD_PTR)DIPROP_AXISMODE:
932 case (DWORD_PTR)DIPROP_BUFFERSIZE:
933 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
934 case (DWORD_PTR)DIPROP_DEADZONE:
935 case (DWORD_PTR)DIPROP_FFGAIN:
936 case (DWORD_PTR)DIPROP_FFLOAD:
937 case (DWORD_PTR)DIPROP_GRANULARITY:
938 case (DWORD_PTR)DIPROP_JOYSTICKID:
939 case (DWORD_PTR)DIPROP_SATURATION:
940 case (DWORD_PTR)DIPROP_SCANCODE:
941 case (DWORD_PTR)DIPROP_VIDPID:
942 if (header->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
943 break;
945 case (DWORD_PTR)DIPROP_APPDATA:
946 if (header->dwSize != sizeof(DIPROPPOINTER)) return DIERR_INVALIDPARAM;
947 break;
949 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
950 case (DWORD_PTR)DIPROP_LOGICALRANGE:
951 case (DWORD_PTR)DIPROP_RANGE:
952 if (header->dwSize != sizeof(DIPROPRANGE)) return DIERR_INVALIDPARAM;
953 break;
955 case (DWORD_PTR)DIPROP_GUIDANDPATH:
956 if (header->dwSize != sizeof(DIPROPGUIDANDPATH)) return DIERR_INVALIDPARAM;
957 break;
960 switch (LOWORD( guid ))
962 case (DWORD_PTR)DIPROP_PRODUCTNAME:
963 case (DWORD_PTR)DIPROP_INSTANCENAME:
964 case (DWORD_PTR)DIPROP_VIDPID:
965 case (DWORD_PTR)DIPROP_JOYSTICKID:
966 case (DWORD_PTR)DIPROP_GUIDANDPATH:
967 case (DWORD_PTR)DIPROP_BUFFERSIZE:
968 case (DWORD_PTR)DIPROP_FFGAIN:
969 case (DWORD_PTR)DIPROP_TYPENAME:
970 case (DWORD_PTR)DIPROP_USERNAME:
971 case (DWORD_PTR)DIPROP_AUTOCENTER:
972 case (DWORD_PTR)DIPROP_AXISMODE:
973 case (DWORD_PTR)DIPROP_FFLOAD:
974 if (header->dwHow != DIPH_DEVICE) return DIERR_UNSUPPORTED;
975 if (header->dwObj) return DIERR_INVALIDPARAM;
976 break;
978 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
979 case (DWORD_PTR)DIPROP_LOGICALRANGE:
980 case (DWORD_PTR)DIPROP_RANGE:
981 case (DWORD_PTR)DIPROP_DEADZONE:
982 case (DWORD_PTR)DIPROP_SATURATION:
983 case (DWORD_PTR)DIPROP_GRANULARITY:
984 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
985 if (header->dwHow == DIPH_DEVICE && !set) return DIERR_UNSUPPORTED;
986 break;
988 case (DWORD_PTR)DIPROP_KEYNAME:
989 if (header->dwHow == DIPH_DEVICE) return DIERR_INVALIDPARAM;
990 break;
992 case (DWORD_PTR)DIPROP_SCANCODE:
993 case (DWORD_PTR)DIPROP_APPDATA:
994 if (header->dwHow == DIPH_DEVICE) return DIERR_UNSUPPORTED;
995 break;
998 if (set)
1000 switch (LOWORD( guid ))
1002 case (DWORD_PTR)DIPROP_AUTOCENTER:
1003 if (impl->status == STATUS_ACQUIRED && !is_exclusively_acquired( impl )) return DIERR_ACQUIRED;
1004 break;
1005 case (DWORD_PTR)DIPROP_AXISMODE:
1006 case (DWORD_PTR)DIPROP_BUFFERSIZE:
1007 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1008 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1009 if (impl->status == STATUS_ACQUIRED) return DIERR_ACQUIRED;
1010 break;
1011 case (DWORD_PTR)DIPROP_FFLOAD:
1012 case (DWORD_PTR)DIPROP_GRANULARITY:
1013 case (DWORD_PTR)DIPROP_VIDPID:
1014 case (DWORD_PTR)DIPROP_TYPENAME:
1015 case (DWORD_PTR)DIPROP_USERNAME:
1016 case (DWORD_PTR)DIPROP_GUIDANDPATH:
1017 return DIERR_READONLY;
1020 switch (LOWORD( guid ))
1022 case (DWORD_PTR)DIPROP_RANGE:
1024 const DIPROPRANGE *value = (const DIPROPRANGE *)header;
1025 if (value->lMin > value->lMax) return DIERR_INVALIDPARAM;
1026 break;
1028 case (DWORD_PTR)DIPROP_DEADZONE:
1029 case (DWORD_PTR)DIPROP_SATURATION:
1030 case (DWORD_PTR)DIPROP_FFGAIN:
1032 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1033 if (value->dwData > 10000) return DIERR_INVALIDPARAM;
1034 break;
1036 case (DWORD_PTR)DIPROP_AUTOCENTER:
1037 case (DWORD_PTR)DIPROP_AXISMODE:
1038 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1040 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1041 if (value->dwData > 1) return DIERR_INVALIDPARAM;
1042 break;
1044 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1045 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1046 return DIERR_UNSUPPORTED;
1049 else
1051 switch (LOWORD( guid ))
1053 case (DWORD_PTR)DIPROP_RANGE:
1054 case (DWORD_PTR)DIPROP_GRANULARITY:
1055 if (!impl->caps.dwAxes) return DIERR_UNSUPPORTED;
1056 break;
1058 case (DWORD_PTR)DIPROP_KEYNAME:
1059 /* not supported on the mouse */
1060 if (impl->caps.dwAxes && !(impl->caps.dwDevType & DIDEVTYPE_HID)) return DIERR_UNSUPPORTED;
1061 break;
1063 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1064 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1065 case (DWORD_PTR)DIPROP_DEADZONE:
1066 case (DWORD_PTR)DIPROP_SATURATION:
1067 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1068 if (!impl->object_properties) return DIERR_UNSUPPORTED;
1069 break;
1071 case (DWORD_PTR)DIPROP_FFLOAD:
1072 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1073 if (!is_exclusively_acquired( impl )) return DIERR_NOTEXCLUSIVEACQUIRED;
1074 /* fallthrough */
1075 case (DWORD_PTR)DIPROP_PRODUCTNAME:
1076 case (DWORD_PTR)DIPROP_INSTANCENAME:
1077 case (DWORD_PTR)DIPROP_VIDPID:
1078 case (DWORD_PTR)DIPROP_JOYSTICKID:
1079 case (DWORD_PTR)DIPROP_GUIDANDPATH:
1080 if (!impl->vtbl->get_property) return DIERR_UNSUPPORTED;
1081 break;
1085 return DI_OK;
1088 static BOOL CALLBACK find_object( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1090 *(DIDEVICEOBJECTINSTANCEW *)context = *instance;
1091 return DIENUM_STOP;
1094 struct get_object_property_params
1096 IDirectInputDevice8W *iface;
1097 DIPROPHEADER *header;
1098 DWORD property;
1101 static BOOL CALLBACK get_object_property( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1103 static const struct object_properties default_properties =
1105 .range_min = DIPROPRANGE_NOMIN,
1106 .range_max = DIPROPRANGE_NOMAX,
1108 struct get_object_property_params *params = context;
1109 struct dinput_device *impl = impl_from_IDirectInputDevice8W( params->iface );
1110 const struct object_properties *properties = NULL;
1112 if (!impl->object_properties) properties = &default_properties;
1113 else properties = impl->object_properties + instance->dwOfs / sizeof(LONG);
1115 switch (params->property)
1117 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1119 DIPROPRANGE *value = (DIPROPRANGE *)params->header;
1120 value->lMin = properties->physical_min;
1121 value->lMax = properties->physical_max;
1122 return DI_OK;
1124 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1126 DIPROPRANGE *value = (DIPROPRANGE *)params->header;
1127 value->lMin = properties->logical_min;
1128 value->lMax = properties->logical_max;
1129 return DI_OK;
1131 case (DWORD_PTR)DIPROP_RANGE:
1133 DIPROPRANGE *value = (DIPROPRANGE *)params->header;
1134 value->lMin = properties->range_min;
1135 value->lMax = properties->range_max;
1136 return DIENUM_STOP;
1138 case (DWORD_PTR)DIPROP_DEADZONE:
1140 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1141 value->dwData = properties->deadzone;
1142 return DIENUM_STOP;
1144 case (DWORD_PTR)DIPROP_SATURATION:
1146 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1147 value->dwData = properties->saturation;
1148 return DIENUM_STOP;
1150 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1152 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1153 value->dwData = properties->calibration_mode;
1154 return DI_OK;
1156 case (DWORD_PTR)DIPROP_GRANULARITY:
1158 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1159 value->dwData = 1;
1160 return DIENUM_STOP;
1162 case (DWORD_PTR)DIPROP_KEYNAME:
1164 DIPROPSTRING *value = (DIPROPSTRING *)params->header;
1165 lstrcpynW( value->wsz, instance->tszName, ARRAY_SIZE(value->wsz) );
1166 return DIENUM_STOP;
1170 return DIENUM_STOP;
1173 static HRESULT dinput_device_get_property( IDirectInputDevice8W *iface, const GUID *guid, DIPROPHEADER *header )
1175 struct get_object_property_params params = {.iface = iface, .header = header, .property = LOWORD( guid )};
1176 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1177 DWORD object_mask = DIDFT_AXIS | DIDFT_BUTTON | DIDFT_POV;
1178 DIPROPHEADER filter;
1179 HRESULT hr;
1181 filter = *header;
1182 if (FAILED(hr = enum_object_filter_init( impl, &filter ))) return hr;
1183 if (FAILED(hr = check_property( impl, guid, header, FALSE ))) return hr;
1185 switch (LOWORD( guid ))
1187 case (DWORD_PTR)DIPROP_PRODUCTNAME:
1188 case (DWORD_PTR)DIPROP_INSTANCENAME:
1189 case (DWORD_PTR)DIPROP_VIDPID:
1190 case (DWORD_PTR)DIPROP_JOYSTICKID:
1191 case (DWORD_PTR)DIPROP_GUIDANDPATH:
1192 case (DWORD_PTR)DIPROP_FFLOAD:
1193 return impl->vtbl->get_property( iface, LOWORD( guid ), header, NULL );
1195 case (DWORD_PTR)DIPROP_RANGE:
1196 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1197 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1198 case (DWORD_PTR)DIPROP_DEADZONE:
1199 case (DWORD_PTR)DIPROP_SATURATION:
1200 case (DWORD_PTR)DIPROP_GRANULARITY:
1201 case (DWORD_PTR)DIPROP_KEYNAME:
1202 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1203 hr = impl->vtbl->enum_objects( iface, &filter, object_mask, get_object_property, &params );
1204 if (FAILED(hr)) return hr;
1205 if (hr == DIENUM_CONTINUE) return DIERR_NOTFOUND;
1206 return DI_OK;
1208 case (DWORD_PTR)DIPROP_AUTOCENTER:
1210 DIPROPDWORD *value = (DIPROPDWORD *)header;
1211 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1212 value->dwData = impl->autocenter;
1213 return DI_OK;
1215 case (DWORD_PTR)DIPROP_BUFFERSIZE:
1217 DIPROPDWORD *value = (DIPROPDWORD *)header;
1218 value->dwData = impl->buffersize;
1219 return DI_OK;
1221 case (DWORD_PTR)DIPROP_USERNAME:
1223 DIPROPSTRING *value = (DIPROPSTRING *)header;
1224 struct DevicePlayer *device_player;
1225 LIST_FOR_EACH_ENTRY( device_player, &impl->dinput->device_players, struct DevicePlayer, entry )
1227 if (IsEqualGUID( &device_player->instance_guid, &impl->guid ))
1229 if (!*device_player->username) break;
1230 lstrcpynW( value->wsz, device_player->username, ARRAY_SIZE(value->wsz) );
1231 return DI_OK;
1234 return S_FALSE;
1236 case (DWORD_PTR)DIPROP_FFGAIN:
1238 DIPROPDWORD *value = (DIPROPDWORD *)header;
1239 value->dwData = impl->device_gain;
1240 return DI_OK;
1242 case (DWORD_PTR)DIPROP_CALIBRATION:
1243 return DIERR_INVALIDPARAM;
1244 default:
1245 FIXME( "Unknown property %s\n", debugstr_guid( guid ) );
1246 return DIERR_UNSUPPORTED;
1249 return DI_OK;
1252 static HRESULT WINAPI dinput_device_GetProperty( IDirectInputDevice8W *iface, const GUID *guid, DIPROPHEADER *header )
1254 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1255 HRESULT hr;
1257 TRACE( "iface %p, guid %s, header %p\n", iface, debugstr_guid( guid ), header );
1259 if (!header) return DIERR_INVALIDPARAM;
1260 if (header->dwHeaderSize != sizeof(DIPROPHEADER)) return DIERR_INVALIDPARAM;
1261 if (!IS_DIPROP( guid )) return DI_OK;
1263 EnterCriticalSection( &impl->crit );
1264 hr = dinput_device_get_property( iface, guid, header );
1265 LeaveCriticalSection( &impl->crit );
1267 return hr;
1270 struct set_object_property_params
1272 IDirectInputDevice8W *iface;
1273 const DIPROPHEADER *header;
1274 DWORD property;
1277 static BOOL CALLBACK set_object_property( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1279 struct set_object_property_params *params = context;
1280 struct dinput_device *impl = impl_from_IDirectInputDevice8W( params->iface );
1281 struct object_properties *properties = NULL;
1283 if (!impl->object_properties) return DIENUM_STOP;
1284 properties = impl->object_properties + instance->dwOfs / sizeof(LONG);
1286 switch (params->property)
1288 case (DWORD_PTR)DIPROP_RANGE:
1290 const DIPROPRANGE *value = (const DIPROPRANGE *)params->header;
1291 properties->range_min = value->lMin;
1292 properties->range_max = value->lMax;
1293 return DIENUM_CONTINUE;
1295 case (DWORD_PTR)DIPROP_DEADZONE:
1297 const DIPROPDWORD *value = (const DIPROPDWORD *)params->header;
1298 properties->deadzone = value->dwData;
1299 return DIENUM_CONTINUE;
1301 case (DWORD_PTR)DIPROP_SATURATION:
1303 const DIPROPDWORD *value = (const DIPROPDWORD *)params->header;
1304 properties->saturation = value->dwData;
1305 return DIENUM_CONTINUE;
1307 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1309 const DIPROPDWORD *value = (const DIPROPDWORD *)params->header;
1310 properties->calibration_mode = value->dwData;
1311 return DIENUM_CONTINUE;
1315 return DIENUM_STOP;
1318 static BOOL CALLBACK reset_object_value( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1320 struct dinput_device *impl = context;
1321 struct object_properties *properties;
1322 LONG tmp = -1;
1324 if (!impl->object_properties) return DIENUM_STOP;
1325 properties = impl->object_properties + instance->dwOfs / sizeof(LONG);
1327 if (instance->dwType & DIDFT_AXIS)
1329 if (!properties->range_min) tmp = properties->range_max / 2;
1330 else tmp = round( (properties->range_min + properties->range_max) / 2.0 );
1333 *(LONG *)(impl->device_state + instance->dwOfs) = tmp;
1334 return DIENUM_CONTINUE;
1337 static void reset_device_state( IDirectInputDevice8W *iface )
1339 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1340 DIPROPHEADER filter =
1342 .dwHeaderSize = sizeof(DIPROPHEADER),
1343 .dwSize = sizeof(DIPROPHEADER),
1344 .dwHow = DIPH_DEVICE,
1345 .dwObj = 0,
1348 impl->vtbl->enum_objects( iface, &filter, DIDFT_AXIS | DIDFT_POV, reset_object_value, impl );
1351 static HRESULT WINAPI dinput_device_set_property( IDirectInputDevice8W *iface, const GUID *guid,
1352 const DIPROPHEADER *header )
1354 struct set_object_property_params params = {.iface = iface, .header = header, .property = LOWORD( guid )};
1355 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1356 DWORD object_mask = DIDFT_AXIS | DIDFT_BUTTON | DIDFT_POV;
1357 DIDEVICEOBJECTINSTANCEW instance;
1358 DIPROPHEADER filter;
1359 HRESULT hr;
1361 filter = *header;
1362 if (FAILED(hr = enum_object_filter_init( impl, &filter ))) return hr;
1363 if (FAILED(hr = check_property( impl, guid, header, TRUE ))) return hr;
1365 switch (LOWORD( guid ))
1367 case (DWORD_PTR)DIPROP_RANGE:
1368 case (DWORD_PTR)DIPROP_DEADZONE:
1369 case (DWORD_PTR)DIPROP_SATURATION:
1371 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_AXIS, set_object_property, &params );
1372 if (FAILED(hr)) return hr;
1373 reset_device_state( iface );
1374 return DI_OK;
1376 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1378 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1379 if (value->dwData > DIPROPCALIBRATIONMODE_RAW) return DIERR_INVALIDPARAM;
1380 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_AXIS, set_object_property, &params );
1381 if (FAILED(hr)) return hr;
1382 reset_device_state( iface );
1383 return DI_OK;
1385 case (DWORD_PTR)DIPROP_AUTOCENTER:
1387 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1388 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1390 FIXME( "DIPROP_AUTOCENTER stub!\n" );
1391 impl->autocenter = value->dwData;
1392 return DI_OK;
1394 case (DWORD_PTR)DIPROP_FFGAIN:
1396 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1397 impl->device_gain = value->dwData;
1398 if (!is_exclusively_acquired( impl )) return DI_OK;
1399 if (!impl->vtbl->send_device_gain) return DI_OK;
1400 return impl->vtbl->send_device_gain( iface, impl->device_gain );
1402 case (DWORD_PTR)DIPROP_AXISMODE:
1404 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1406 TRACE( "Axis mode: %s\n", value->dwData == DIPROPAXISMODE_ABS ? "absolute" : "relative" );
1408 impl->user_format.dwFlags &= ~DIDFT_AXIS;
1409 impl->user_format.dwFlags |= value->dwData == DIPROPAXISMODE_ABS ? DIDF_ABSAXIS : DIDF_RELAXIS;
1411 return DI_OK;
1413 case (DWORD_PTR)DIPROP_BUFFERSIZE:
1415 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1417 TRACE( "buffersize %lu\n", value->dwData );
1419 impl->buffersize = value->dwData;
1420 impl->queue_len = min( impl->buffersize, 1024 );
1421 free( impl->data_queue );
1423 impl->data_queue = impl->queue_len ? malloc( impl->queue_len * sizeof(DIDEVICEOBJECTDATA) ) : NULL;
1424 impl->queue_head = impl->queue_tail = impl->overflow = 0;
1425 return DI_OK;
1427 case (DWORD_PTR)DIPROP_APPDATA:
1429 const DIPROPPOINTER *value = (const DIPROPPOINTER *)header;
1430 int user_offset;
1431 hr = impl->vtbl->enum_objects( iface, &filter, object_mask, find_object, &instance );
1432 if (FAILED(hr)) return hr;
1433 if (hr == DIENUM_CONTINUE) return DIERR_OBJECTNOTFOUND;
1434 if ((user_offset = id_to_offset( impl, instance.dwType )) < 0) return DIERR_OBJECTNOTFOUND;
1435 if (!set_app_data( impl, user_offset, value->uData )) return DIERR_OUTOFMEMORY;
1436 return DI_OK;
1438 default:
1439 FIXME( "Unknown property %s\n", debugstr_guid( guid ) );
1440 return DIERR_UNSUPPORTED;
1443 return DI_OK;
1446 static HRESULT WINAPI dinput_device_SetProperty( IDirectInputDevice8W *iface, const GUID *guid,
1447 const DIPROPHEADER *header )
1449 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1450 HRESULT hr;
1452 TRACE( "iface %p, guid %s, header %p\n", iface, debugstr_guid( guid ), header );
1454 if (!header) return DIERR_INVALIDPARAM;
1455 if (header->dwHeaderSize != sizeof(DIPROPHEADER)) return DIERR_INVALIDPARAM;
1456 if (!IS_DIPROP( guid )) return DI_OK;
1458 EnterCriticalSection( &impl->crit );
1459 hr = dinput_device_set_property( iface, guid, header );
1460 LeaveCriticalSection( &impl->crit );
1462 return hr;
1465 static void dinput_device_set_username( struct dinput_device *impl, const DIPROPSTRING *value )
1467 struct DevicePlayer *device_player;
1468 BOOL found = FALSE;
1470 LIST_FOR_EACH_ENTRY( device_player, &impl->dinput->device_players, struct DevicePlayer, entry )
1472 if (IsEqualGUID( &device_player->instance_guid, &impl->guid ))
1474 found = TRUE;
1475 break;
1478 if (!found && (device_player = malloc( sizeof(struct DevicePlayer) )))
1480 list_add_tail( &impl->dinput->device_players, &device_player->entry );
1481 device_player->instance_guid = impl->guid;
1483 if (device_player)
1484 lstrcpynW( device_player->username, value->wsz, ARRAY_SIZE(device_player->username) );
1487 static BOOL CALLBACK get_object_info( const DIDEVICEOBJECTINSTANCEW *instance, void *data )
1489 DIDEVICEOBJECTINSTANCEW *dest = data;
1490 DWORD size = dest->dwSize;
1492 memcpy( dest, instance, size );
1493 dest->dwSize = size;
1495 return DIENUM_STOP;
1498 static HRESULT WINAPI dinput_device_GetObjectInfo( IDirectInputDevice8W *iface,
1499 DIDEVICEOBJECTINSTANCEW *instance, DWORD obj, DWORD how )
1501 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1502 DIPROPHEADER filter =
1504 .dwSize = sizeof(filter),
1505 .dwHeaderSize = sizeof(filter),
1506 .dwHow = how,
1507 .dwObj = obj
1509 HRESULT hr;
1511 TRACE( "iface %p, instance %p, obj %#lx, how %#lx.\n", iface, instance, obj, how );
1513 if (!instance) return E_POINTER;
1514 if (instance->dwSize != sizeof(DIDEVICEOBJECTINSTANCE_DX3W) && instance->dwSize != sizeof(DIDEVICEOBJECTINSTANCEW))
1515 return DIERR_INVALIDPARAM;
1516 if (how == DIPH_DEVICE) return DIERR_INVALIDPARAM;
1517 if (FAILED(hr = enum_object_filter_init( impl, &filter ))) return hr;
1519 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_ALL, get_object_info, instance );
1520 if (FAILED(hr)) return hr;
1521 if (hr == DIENUM_CONTINUE) return DIERR_NOTFOUND;
1522 return DI_OK;
1525 static HRESULT WINAPI dinput_device_GetDeviceState( IDirectInputDevice8W *iface, DWORD size, void *data )
1527 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1528 DIDATAFORMAT *device_format = &impl->device_format, *user_format = &impl->user_format;
1529 DIOBJECTDATAFORMAT *device_obj, *user_obj;
1530 BYTE *user_state = data;
1531 DIPROPHEADER filter =
1533 .dwSize = sizeof(filter),
1534 .dwHeaderSize = sizeof(filter),
1535 .dwHow = DIPH_DEVICE,
1536 .dwObj = 0,
1538 HRESULT hr;
1540 TRACE( "iface %p, size %lu, data %p.\n", iface, size, data );
1542 if (!data) return DIERR_INVALIDPARAM;
1544 IDirectInputDevice2_Poll( iface );
1546 EnterCriticalSection( &impl->crit );
1547 if (impl->status == STATUS_UNPLUGGED)
1548 hr = DIERR_INPUTLOST;
1549 else if (impl->status != STATUS_ACQUIRED)
1550 hr = DIERR_NOTACQUIRED;
1551 else if (!user_format->rgodf)
1552 hr = DIERR_INVALIDPARAM;
1553 else if (size != user_format->dwDataSize)
1554 hr = DIERR_INVALIDPARAM;
1555 else
1557 memset( user_state, 0, size );
1559 user_obj = user_format->rgodf + device_format->dwNumObjs;
1560 device_obj = device_format->rgodf + device_format->dwNumObjs;
1561 while (user_obj-- > user_format->rgodf && device_obj-- > device_format->rgodf)
1563 if (user_obj->dwType & DIDFT_BUTTON)
1564 user_state[user_obj->dwOfs] = impl->device_state[device_obj->dwOfs];
1567 /* reset optional POVs to their default */
1568 user_obj = user_format->rgodf + user_format->dwNumObjs;
1569 while (user_obj-- > user_format->rgodf + device_format->dwNumObjs)
1570 if (user_obj->dwType & DIDFT_POV) *(ULONG *)(user_state + user_obj->dwOfs) = 0xffffffff;
1572 user_obj = user_format->rgodf + device_format->dwNumObjs;
1573 device_obj = device_format->rgodf + device_format->dwNumObjs;
1574 while (user_obj-- > user_format->rgodf && device_obj-- > device_format->rgodf)
1576 if (user_obj->dwType & (DIDFT_POV | DIDFT_AXIS))
1577 *(ULONG *)(user_state + user_obj->dwOfs) = *(ULONG *)(impl->device_state + device_obj->dwOfs);
1578 if (!(user_format->dwFlags & DIDF_ABSAXIS) && (device_obj->dwType & DIDFT_RELAXIS))
1579 *(ULONG *)(impl->device_state + device_obj->dwOfs) = 0;
1582 hr = DI_OK;
1584 LeaveCriticalSection( &impl->crit );
1586 return hr;
1589 static HRESULT WINAPI dinput_device_GetDeviceData( IDirectInputDevice8W *iface, DWORD size, DIDEVICEOBJECTDATA *data,
1590 DWORD *count, DWORD flags )
1592 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
1593 HRESULT ret = DI_OK;
1594 int len;
1596 TRACE( "iface %p, size %lu, data %p, count %p, flags %#lx.\n", iface, size, data, count, flags );
1598 if (This->dinput->dwVersion == 0x0800 || size == sizeof(DIDEVICEOBJECTDATA_DX3))
1600 if (!This->queue_len) return DIERR_NOTBUFFERED;
1601 if (This->status == STATUS_UNPLUGGED) return DIERR_INPUTLOST;
1602 if (This->status != STATUS_ACQUIRED) return DIERR_NOTACQUIRED;
1605 if (!This->queue_len)
1606 return DI_OK;
1607 if (size < sizeof(DIDEVICEOBJECTDATA_DX3)) return DIERR_INVALIDPARAM;
1609 IDirectInputDevice2_Poll(iface);
1610 EnterCriticalSection(&This->crit);
1612 len = This->queue_head - This->queue_tail;
1613 if (len < 0) len += This->queue_len;
1615 if ((*count != INFINITE) && (len > *count)) len = *count;
1617 if (data)
1619 int i;
1620 for (i = 0; i < len; i++)
1622 int n = (This->queue_tail + i) % This->queue_len;
1623 memcpy( (char *)data + size * i, This->data_queue + n, size );
1626 *count = len;
1628 if (This->overflow && This->dinput->dwVersion == 0x0800)
1629 ret = DI_BUFFEROVERFLOW;
1631 if (!(flags & DIGDD_PEEK))
1633 /* Advance reading position */
1634 This->queue_tail = (This->queue_tail + len) % This->queue_len;
1635 This->overflow = FALSE;
1638 LeaveCriticalSection(&This->crit);
1640 TRACE( "Returning %lu events queued\n", *count );
1641 return ret;
1644 static HRESULT WINAPI dinput_device_RunControlPanel( IDirectInputDevice8W *iface, HWND hwnd, DWORD flags )
1646 FIXME( "iface %p, hwnd %p, flags %#lx stub!\n", iface, hwnd, flags );
1647 return DI_OK;
1650 static HRESULT WINAPI dinput_device_Initialize( IDirectInputDevice8W *iface, HINSTANCE instance,
1651 DWORD version, const GUID *guid )
1653 FIXME( "iface %p, instance %p, version %#lx, guid %s stub!\n", iface, instance, version,
1654 debugstr_guid( guid ) );
1655 return DI_OK;
1658 static HRESULT WINAPI dinput_device_CreateEffect( IDirectInputDevice8W *iface, const GUID *guid,
1659 const DIEFFECT *params, IDirectInputEffect **out,
1660 IUnknown *outer )
1662 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1663 DWORD flags;
1664 HRESULT hr;
1666 TRACE( "iface %p, guid %s, params %p, out %p, outer %p\n", iface, debugstr_guid( guid ),
1667 params, out, outer );
1669 if (!out) return E_POINTER;
1670 *out = NULL;
1672 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1673 if (!impl->vtbl->create_effect) return DIERR_UNSUPPORTED;
1674 if (FAILED(hr = impl->vtbl->create_effect( iface, out ))) return hr;
1676 hr = IDirectInputEffect_Initialize( *out, DINPUT_instance, impl->dinput->dwVersion, guid );
1677 if (FAILED(hr)) goto failed;
1678 if (!params) return DI_OK;
1680 flags = params->dwSize == sizeof(DIEFFECT_DX6) ? DIEP_ALLPARAMS : DIEP_ALLPARAMS_DX5;
1681 if (!is_exclusively_acquired( impl )) flags |= DIEP_NODOWNLOAD;
1682 hr = IDirectInputEffect_SetParameters( *out, params, flags );
1683 if (FAILED(hr)) goto failed;
1684 return DI_OK;
1686 failed:
1687 IDirectInputEffect_Release( *out );
1688 *out = NULL;
1689 return hr;
1692 static HRESULT WINAPI dinput_device_EnumEffects( IDirectInputDevice8W *iface, LPDIENUMEFFECTSCALLBACKW callback,
1693 void *context, DWORD type )
1695 DIEFFECTINFOW info = {.dwSize = sizeof(info)};
1696 HRESULT hr;
1698 TRACE( "iface %p, callback %p, context %p, type %#lx.\n", iface, callback, context, type );
1700 if (!callback) return DIERR_INVALIDPARAM;
1702 type = DIEFT_GETTYPE( type );
1704 if (type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1706 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_ConstantForce );
1707 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1708 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1711 if (type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1713 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_RampForce );
1714 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1715 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1718 if (type == DIEFT_ALL || type == DIEFT_PERIODIC)
1720 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Square );
1721 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1722 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1724 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Sine );
1725 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1726 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1728 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Triangle );
1729 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1730 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1732 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_SawtoothUp );
1733 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1734 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1736 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_SawtoothDown );
1737 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1738 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1741 if (type == DIEFT_ALL || type == DIEFT_CONDITION)
1743 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Spring );
1744 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1745 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1747 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Damper );
1748 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1749 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1751 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Inertia );
1752 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1753 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1755 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Friction );
1756 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1757 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1760 return DI_OK;
1763 static HRESULT WINAPI dinput_device_GetEffectInfo( IDirectInputDevice8W *iface, DIEFFECTINFOW *info,
1764 const GUID *guid )
1766 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1768 TRACE( "iface %p, info %p, guid %s.\n", iface, info, debugstr_guid( guid ) );
1770 if (!info) return E_POINTER;
1771 if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
1772 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_DEVICENOTREG;
1773 if (!impl->vtbl->get_effect_info) return DIERR_UNSUPPORTED;
1774 return impl->vtbl->get_effect_info( iface, info, guid );
1777 static HRESULT WINAPI dinput_device_GetForceFeedbackState( IDirectInputDevice8W *iface, DWORD *out )
1779 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1780 HRESULT hr = DI_OK;
1782 TRACE( "iface %p, out %p.\n", iface, out );
1784 if (!out) return E_POINTER;
1785 *out = 0;
1787 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1789 EnterCriticalSection( &impl->crit );
1790 if (!is_exclusively_acquired( impl )) hr = DIERR_NOTEXCLUSIVEACQUIRED;
1791 else *out = impl->force_feedback_state;
1792 LeaveCriticalSection( &impl->crit );
1794 return hr;
1797 static HRESULT WINAPI dinput_device_SendForceFeedbackCommand( IDirectInputDevice8W *iface, DWORD command )
1799 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1800 HRESULT hr;
1802 TRACE( "iface %p, command %#lx.\n", iface, command );
1804 switch (command)
1806 case DISFFC_RESET: break;
1807 case DISFFC_STOPALL: break;
1808 case DISFFC_PAUSE: break;
1809 case DISFFC_CONTINUE: break;
1810 case DISFFC_SETACTUATORSON: break;
1811 case DISFFC_SETACTUATORSOFF: break;
1812 default: return DIERR_INVALIDPARAM;
1815 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1816 if (!impl->vtbl->send_force_feedback_command) return DIERR_UNSUPPORTED;
1818 EnterCriticalSection( &impl->crit );
1819 if (!is_exclusively_acquired( impl )) hr = DIERR_NOTEXCLUSIVEACQUIRED;
1820 else hr = impl->vtbl->send_force_feedback_command( iface, command, FALSE );
1821 LeaveCriticalSection( &impl->crit );
1823 return hr;
1826 static HRESULT WINAPI dinput_device_EnumCreatedEffectObjects( IDirectInputDevice8W *iface,
1827 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK callback,
1828 void *context, DWORD flags )
1830 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1832 TRACE( "iface %p, callback %p, context %p, flags %#lx.\n", iface, callback, context, flags );
1834 if (!callback) return DIERR_INVALIDPARAM;
1835 if (flags) return DIERR_INVALIDPARAM;
1836 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DI_OK;
1837 if (!impl->vtbl->enum_created_effect_objects) return DIERR_UNSUPPORTED;
1839 return impl->vtbl->enum_created_effect_objects( iface, callback, context, flags );
1842 static HRESULT WINAPI dinput_device_Escape( IDirectInputDevice8W *iface, DIEFFESCAPE *escape )
1844 FIXME( "iface %p, escape %p stub!\n", iface, escape );
1845 return DI_OK;
1848 static HRESULT WINAPI dinput_device_Poll( IDirectInputDevice8W *iface )
1850 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1851 HRESULT hr = DI_NOEFFECT;
1853 EnterCriticalSection( &impl->crit );
1854 if (impl->status == STATUS_UNPLUGGED) hr = DIERR_INPUTLOST;
1855 else if (impl->status != STATUS_ACQUIRED) hr = DIERR_NOTACQUIRED;
1856 LeaveCriticalSection( &impl->crit );
1857 if (FAILED(hr)) return hr;
1859 if (impl->vtbl->poll) return impl->vtbl->poll( iface );
1860 return hr;
1863 static HRESULT WINAPI dinput_device_SendDeviceData( IDirectInputDevice8W *iface, DWORD size,
1864 const DIDEVICEOBJECTDATA *data, DWORD *count, DWORD flags )
1866 FIXME( "iface %p, size %lu, data %p, count %p, flags %#lx stub!\n", iface, size, data, count, flags );
1867 return DI_OK;
1870 static HRESULT WINAPI dinput_device_EnumEffectsInFile( IDirectInputDevice8W *iface, const WCHAR *filename,
1871 LPDIENUMEFFECTSINFILECALLBACK callback,
1872 void *context, DWORD flags )
1874 FIXME( "iface %p, filename %s, callback %p, context %p, flags %#lx stub!\n", iface,
1875 debugstr_w(filename), callback, context, flags );
1876 return DI_OK;
1879 static HRESULT WINAPI dinput_device_WriteEffectToFile( IDirectInputDevice8W *iface, const WCHAR *filename,
1880 DWORD count, DIFILEEFFECT *effects, DWORD flags )
1882 FIXME( "iface %p, filename %s, count %lu, effects %p, flags %#lx stub!\n", iface,
1883 debugstr_w(filename), count, effects, flags );
1884 return DI_OK;
1887 static HRESULT WINAPI dinput_device_BuildActionMap( IDirectInputDevice8W *iface, DIACTIONFORMATW *format,
1888 const WCHAR *username, DWORD flags )
1890 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1891 BOOL load_success = FALSE, has_actions = FALSE;
1892 DWORD genre, username_len = MAX_PATH;
1893 WCHAR username_buf[MAX_PATH];
1894 const DIDATAFORMAT *df;
1895 DWORD devMask;
1896 int i;
1898 FIXME( "iface %p, format %p, username %s, flags %#lx stub!\n", iface, format,
1899 debugstr_w(username), flags );
1901 if (!format) return DIERR_INVALIDPARAM;
1903 switch (GET_DIDEVICE_TYPE( impl->instance.dwDevType ))
1905 case DIDEVTYPE_KEYBOARD:
1906 case DI8DEVTYPE_KEYBOARD:
1907 devMask = DIKEYBOARD_MASK;
1908 df = &c_dfDIKeyboard;
1909 break;
1910 case DIDEVTYPE_MOUSE:
1911 case DI8DEVTYPE_MOUSE:
1912 devMask = DIMOUSE_MASK;
1913 df = &c_dfDIMouse2;
1914 break;
1915 default:
1916 devMask = DIGENRE_ANY;
1917 df = &impl->device_format;
1918 break;
1921 /* Unless asked the contrary by these flags, try to load a previous mapping */
1922 if (!(flags & DIDBAM_HWDEFAULTS))
1924 /* Retrieve logged user name if necessary */
1925 if (username == NULL) GetUserNameW( username_buf, &username_len );
1926 else lstrcpynW( username_buf, username, MAX_PATH );
1927 load_success = load_mapping_settings( impl, format, username_buf );
1930 if (load_success) return DI_OK;
1932 for (i = 0; i < format->dwNumActions; i++)
1934 /* Don't touch a user configured action */
1935 if (format->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
1937 genre = format->rgoAction[i].dwSemantic & DIGENRE_ANY;
1938 if (devMask == genre || (devMask == DIGENRE_ANY && genre != DIMOUSE_MASK && genre != DIKEYBOARD_MASK))
1940 DWORD obj_id = semantic_to_obj_id( impl, format->rgoAction[i].dwSemantic );
1941 DWORD type = DIDFT_GETTYPE( obj_id );
1942 DWORD inst = DIDFT_GETINSTANCE( obj_id );
1944 LPDIOBJECTDATAFORMAT odf;
1946 if (type == DIDFT_PSHBUTTON) type = DIDFT_BUTTON;
1947 if (type == DIDFT_RELAXIS) type = DIDFT_AXIS;
1949 /* Make sure the object exists */
1950 odf = dataformat_to_odf_by_type( df, inst, type );
1952 if (odf != NULL)
1954 format->rgoAction[i].dwObjID = obj_id;
1955 format->rgoAction[i].guidInstance = impl->guid;
1956 format->rgoAction[i].dwHow = DIAH_DEFAULT;
1957 has_actions = TRUE;
1960 else if (!(flags & DIDBAM_PRESERVE))
1962 /* We must clear action data belonging to other devices */
1963 memset( &format->rgoAction[i].guidInstance, 0, sizeof(GUID) );
1964 format->rgoAction[i].dwHow = DIAH_UNMAPPED;
1968 if (!has_actions) return DI_NOEFFECT;
1969 if (flags & (DIDBAM_DEFAULT|DIDBAM_PRESERVE|DIDBAM_INITIALIZE|DIDBAM_HWDEFAULTS))
1970 FIXME( "Unimplemented flags %#lx\n", flags );
1971 return DI_OK;
1974 static HRESULT WINAPI dinput_device_SetActionMap( IDirectInputDevice8W *iface, DIACTIONFORMATW *format,
1975 const WCHAR *username, DWORD flags )
1977 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1978 DIDATAFORMAT data_format;
1979 DIOBJECTDATAFORMAT *obj_df = NULL;
1980 DIPROPDWORD dp;
1981 DIPROPRANGE dpr;
1982 DIPROPSTRING dps;
1983 WCHAR username_buf[MAX_PATH];
1984 DWORD username_len = MAX_PATH;
1985 int i, action = 0, num_actions = 0;
1986 unsigned int offset = 0;
1987 const DIDATAFORMAT *df;
1988 ActionMap *action_map;
1990 FIXME( "iface %p, format %p, username %s, flags %#lx stub!\n", iface, format,
1991 debugstr_w(username), flags );
1993 if (!format) return DIERR_INVALIDPARAM;
1995 switch (GET_DIDEVICE_TYPE( impl->instance.dwDevType ))
1997 case DIDEVTYPE_KEYBOARD:
1998 case DI8DEVTYPE_KEYBOARD:
1999 df = &c_dfDIKeyboard;
2000 break;
2001 case DIDEVTYPE_MOUSE:
2002 case DI8DEVTYPE_MOUSE:
2003 df = &c_dfDIMouse2;
2004 break;
2005 default:
2006 df = &impl->device_format;
2007 break;
2010 if (impl->status == STATUS_ACQUIRED) return DIERR_ACQUIRED;
2012 data_format.dwSize = sizeof(data_format);
2013 data_format.dwObjSize = sizeof(DIOBJECTDATAFORMAT);
2014 data_format.dwFlags = DIDF_RELAXIS;
2015 data_format.dwDataSize = format->dwDataSize;
2017 /* Count the actions */
2018 for (i = 0; i < format->dwNumActions; i++)
2019 if (IsEqualGUID( &impl->guid, &format->rgoAction[i].guidInstance ))
2020 num_actions++;
2022 if (num_actions == 0) return DI_NOEFFECT;
2024 /* Construct the dataformat and actionmap */
2025 obj_df = malloc( sizeof(DIOBJECTDATAFORMAT) * num_actions );
2026 data_format.rgodf = (LPDIOBJECTDATAFORMAT)obj_df;
2027 data_format.dwNumObjs = num_actions;
2029 action_map = malloc( sizeof(ActionMap) * num_actions );
2031 for (i = 0; i < format->dwNumActions; i++)
2033 if (IsEqualGUID( &impl->guid, &format->rgoAction[i].guidInstance ))
2035 DWORD inst = DIDFT_GETINSTANCE( format->rgoAction[i].dwObjID );
2036 DWORD type = DIDFT_GETTYPE( format->rgoAction[i].dwObjID );
2037 LPDIOBJECTDATAFORMAT obj;
2039 if (type == DIDFT_PSHBUTTON) type = DIDFT_BUTTON;
2040 if (type == DIDFT_RELAXIS) type = DIDFT_AXIS;
2042 obj = dataformat_to_odf_by_type( df, inst, type );
2044 memcpy( &obj_df[action], obj, df->dwObjSize );
2046 action_map[action].uAppData = format->rgoAction[i].uAppData;
2047 action_map[action].offset = offset;
2048 obj_df[action].dwOfs = offset;
2049 offset += (type & DIDFT_BUTTON) ? 1 : 4;
2051 action++;
2055 IDirectInputDevice8_SetDataFormat( iface, &data_format );
2057 impl->action_map = action_map;
2058 impl->num_actions = num_actions;
2060 free( obj_df );
2062 /* Set the device properties according to the action format */
2063 dpr.diph.dwSize = sizeof(DIPROPRANGE);
2064 dpr.lMin = format->lAxisMin;
2065 dpr.lMax = format->lAxisMax;
2066 dpr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
2067 dpr.diph.dwObj = 0;
2068 dpr.diph.dwHow = DIPH_DEVICE;
2069 IDirectInputDevice8_SetProperty( iface, DIPROP_RANGE, &dpr.diph );
2071 if (format->dwBufferSize > 0)
2073 dp.diph.dwSize = sizeof(DIPROPDWORD);
2074 dp.dwData = format->dwBufferSize;
2075 dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
2076 dp.diph.dwObj = 0;
2077 dp.diph.dwHow = DIPH_DEVICE;
2078 IDirectInputDevice8_SetProperty( iface, DIPROP_BUFFERSIZE, &dp.diph );
2081 /* Retrieve logged user name if necessary */
2082 if (username == NULL) GetUserNameW( username_buf, &username_len );
2083 else lstrcpynW( username_buf, username, MAX_PATH );
2085 dps.diph.dwSize = sizeof(dps);
2086 dps.diph.dwHeaderSize = sizeof(DIPROPHEADER);
2087 dps.diph.dwObj = 0;
2088 dps.diph.dwHow = DIPH_DEVICE;
2089 if (flags & DIDSAM_NOUSER) dps.wsz[0] = '\0';
2090 else lstrcpynW( dps.wsz, username_buf, ARRAY_SIZE(dps.wsz) );
2091 dinput_device_set_username( impl, &dps );
2093 /* Save the settings to disk */
2094 save_mapping_settings( iface, format, username_buf );
2096 return DI_OK;
2099 static HRESULT WINAPI dinput_device_GetImageInfo( IDirectInputDevice8W *iface, DIDEVICEIMAGEINFOHEADERW *header )
2101 FIXME( "iface %p, header %p stub!\n", iface, header );
2102 return DI_OK;
2105 extern const IDirectInputDevice8AVtbl dinput_device_a_vtbl;
2106 static const IDirectInputDevice8WVtbl dinput_device_w_vtbl =
2108 /*** IUnknown methods ***/
2109 dinput_device_QueryInterface,
2110 dinput_device_AddRef,
2111 dinput_device_Release,
2112 /*** IDirectInputDevice methods ***/
2113 dinput_device_GetCapabilities,
2114 dinput_device_EnumObjects,
2115 dinput_device_GetProperty,
2116 dinput_device_SetProperty,
2117 dinput_device_Acquire,
2118 dinput_device_Unacquire,
2119 dinput_device_GetDeviceState,
2120 dinput_device_GetDeviceData,
2121 dinput_device_SetDataFormat,
2122 dinput_device_SetEventNotification,
2123 dinput_device_SetCooperativeLevel,
2124 dinput_device_GetObjectInfo,
2125 dinput_device_GetDeviceInfo,
2126 dinput_device_RunControlPanel,
2127 dinput_device_Initialize,
2128 /*** IDirectInputDevice2 methods ***/
2129 dinput_device_CreateEffect,
2130 dinput_device_EnumEffects,
2131 dinput_device_GetEffectInfo,
2132 dinput_device_GetForceFeedbackState,
2133 dinput_device_SendForceFeedbackCommand,
2134 dinput_device_EnumCreatedEffectObjects,
2135 dinput_device_Escape,
2136 dinput_device_Poll,
2137 dinput_device_SendDeviceData,
2138 /*** IDirectInputDevice7 methods ***/
2139 dinput_device_EnumEffectsInFile,
2140 dinput_device_WriteEffectToFile,
2141 /*** IDirectInputDevice8 methods ***/
2142 dinput_device_BuildActionMap,
2143 dinput_device_SetActionMap,
2144 dinput_device_GetImageInfo,
2147 void dinput_device_init( struct dinput_device *device, const struct dinput_device_vtbl *vtbl,
2148 const GUID *guid, struct dinput *dinput )
2150 device->IDirectInputDevice8A_iface.lpVtbl = &dinput_device_a_vtbl;
2151 device->IDirectInputDevice8W_iface.lpVtbl = &dinput_device_w_vtbl;
2152 device->ref = 1;
2153 device->guid = *guid;
2154 device->instance.dwSize = sizeof(DIDEVICEINSTANCEW);
2155 device->caps.dwSize = sizeof(DIDEVCAPS);
2156 device->caps.dwFlags = DIDC_ATTACHED | DIDC_EMULATED;
2157 device->device_gain = 10000;
2158 device->force_feedback_state = DIGFFS_STOPPED | DIGFFS_EMPTY;
2159 InitializeCriticalSection( &device->crit );
2160 device->dinput = dinput;
2161 IDirectInput_AddRef( &dinput->IDirectInput7A_iface );
2162 device->vtbl = vtbl;
2165 static const GUID *object_instance_guid( const DIDEVICEOBJECTINSTANCEW *instance )
2167 if (IsEqualGUID( &instance->guidType, &GUID_XAxis )) return &GUID_XAxis;
2168 if (IsEqualGUID( &instance->guidType, &GUID_YAxis )) return &GUID_YAxis;
2169 if (IsEqualGUID( &instance->guidType, &GUID_ZAxis )) return &GUID_ZAxis;
2170 if (IsEqualGUID( &instance->guidType, &GUID_RxAxis )) return &GUID_RxAxis;
2171 if (IsEqualGUID( &instance->guidType, &GUID_RyAxis )) return &GUID_RyAxis;
2172 if (IsEqualGUID( &instance->guidType, &GUID_RzAxis )) return &GUID_RzAxis;
2173 if (IsEqualGUID( &instance->guidType, &GUID_Slider )) return &GUID_Slider;
2174 if (IsEqualGUID( &instance->guidType, &GUID_Button )) return &GUID_Button;
2175 if (IsEqualGUID( &instance->guidType, &GUID_Key )) return &GUID_Key;
2176 if (IsEqualGUID( &instance->guidType, &GUID_POV )) return &GUID_POV;
2177 return &GUID_Unknown;
2180 static BOOL CALLBACK enum_objects_init( const DIDEVICEOBJECTINSTANCEW *instance, void *data )
2182 struct dinput_device *impl = impl_from_IDirectInputDevice8W( data );
2183 DIDATAFORMAT *format = &impl->device_format;
2184 DIOBJECTDATAFORMAT *obj_format;
2186 if (!format->rgodf)
2188 format->dwDataSize = max( format->dwDataSize, instance->dwOfs + sizeof(LONG) );
2189 if (instance->dwType & DIDFT_BUTTON) impl->caps.dwButtons++;
2190 if (instance->dwType & DIDFT_AXIS) impl->caps.dwAxes++;
2191 if (instance->dwType & DIDFT_POV) impl->caps.dwPOVs++;
2192 if (instance->dwType & (DIDFT_BUTTON|DIDFT_AXIS|DIDFT_POV))
2194 if (!impl->device_state_report_id)
2195 impl->device_state_report_id = instance->wReportId;
2196 else if (impl->device_state_report_id != instance->wReportId)
2197 FIXME( "multiple device state reports found!\n" );
2200 else
2202 obj_format = format->rgodf + format->dwNumObjs;
2203 obj_format->pguid = object_instance_guid( instance );
2204 obj_format->dwOfs = instance->dwOfs;
2205 obj_format->dwType = instance->dwType;
2206 obj_format->dwFlags = instance->dwFlags;
2209 if (impl->object_properties && (instance->dwType & (DIDFT_AXIS | DIDFT_POV)))
2210 reset_object_value( instance, impl );
2212 format->dwNumObjs++;
2213 return DIENUM_CONTINUE;
2216 HRESULT dinput_device_init_device_format( IDirectInputDevice8W *iface )
2218 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
2219 DIDATAFORMAT *format = &impl->device_format;
2220 ULONG i, size;
2222 IDirectInputDevice8_EnumObjects( iface, enum_objects_init, iface, DIDFT_ALL );
2223 if (format->dwDataSize > DEVICE_STATE_MAX_SIZE)
2225 FIXME( "unable to create device, state is too large\n" );
2226 return DIERR_OUTOFMEMORY;
2229 size = format->dwNumObjs * sizeof(*format->rgodf);
2230 if (!(format->rgodf = calloc( 1, size ))) return DIERR_OUTOFMEMORY;
2232 format->dwSize = sizeof(*format);
2233 format->dwObjSize = sizeof(*format->rgodf);
2234 format->dwFlags = DIDF_ABSAXIS;
2235 format->dwNumObjs = 0;
2236 IDirectInputDevice8_EnumObjects( iface, enum_objects_init, iface, DIDFT_ALL );
2238 if (TRACE_ON( dinput ))
2240 TRACE( "device format %s\n", debugstr_didataformat( format ) );
2241 for (i = 0; i < format->dwNumObjs; ++i) TRACE( " %lu: object %s\n", i, debugstr_diobjectdataformat( format->rgodf + i ) );
2244 return DI_OK;