vbscript: Handle index read access to array properties.
[wine.git] / dlls / dinput / device.c
blobb85d97d1b95bbdafd67bd210ac03b933a9d30ee2
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 DIOBJECTDATAFORMAT *user_objs = impl->user_format.rgodf;
884 DWORD i, count = impl->device_format.dwNumObjs;
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_objs) return DIERR_NOTFOUND;
892 for (i = 0; i < count; i++)
894 if (!user_objs[i].dwType) continue;
895 if (user_objs[i].dwOfs == filter->dwObj) break;
897 if (i == count) return DIERR_NOTFOUND;
899 filter->dwObj = impl->device_format.rgodf[i].dwOfs;
900 return DI_OK;
903 static HRESULT check_property( struct dinput_device *impl, const GUID *guid, const DIPROPHEADER *header, BOOL set )
905 switch (LOWORD( guid ))
907 case (DWORD_PTR)DIPROP_VIDPID:
908 case (DWORD_PTR)DIPROP_TYPENAME:
909 case (DWORD_PTR)DIPROP_USERNAME:
910 case (DWORD_PTR)DIPROP_KEYNAME:
911 case (DWORD_PTR)DIPROP_LOGICALRANGE:
912 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
913 case (DWORD_PTR)DIPROP_APPDATA:
914 if (impl->dinput->dwVersion < 0x0800) return DIERR_UNSUPPORTED;
915 break;
918 switch (LOWORD( guid ))
920 case (DWORD_PTR)DIPROP_INSTANCENAME:
921 case (DWORD_PTR)DIPROP_KEYNAME:
922 case (DWORD_PTR)DIPROP_PRODUCTNAME:
923 case (DWORD_PTR)DIPROP_TYPENAME:
924 case (DWORD_PTR)DIPROP_USERNAME:
925 if (header->dwSize != sizeof(DIPROPSTRING)) return DIERR_INVALIDPARAM;
926 break;
928 case (DWORD_PTR)DIPROP_AUTOCENTER:
929 case (DWORD_PTR)DIPROP_AXISMODE:
930 case (DWORD_PTR)DIPROP_BUFFERSIZE:
931 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
932 case (DWORD_PTR)DIPROP_DEADZONE:
933 case (DWORD_PTR)DIPROP_FFGAIN:
934 case (DWORD_PTR)DIPROP_FFLOAD:
935 case (DWORD_PTR)DIPROP_GRANULARITY:
936 case (DWORD_PTR)DIPROP_JOYSTICKID:
937 case (DWORD_PTR)DIPROP_SATURATION:
938 case (DWORD_PTR)DIPROP_SCANCODE:
939 case (DWORD_PTR)DIPROP_VIDPID:
940 if (header->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
941 break;
943 case (DWORD_PTR)DIPROP_APPDATA:
944 if (header->dwSize != sizeof(DIPROPPOINTER)) return DIERR_INVALIDPARAM;
945 break;
947 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
948 case (DWORD_PTR)DIPROP_LOGICALRANGE:
949 case (DWORD_PTR)DIPROP_RANGE:
950 if (header->dwSize != sizeof(DIPROPRANGE)) return DIERR_INVALIDPARAM;
951 break;
953 case (DWORD_PTR)DIPROP_GUIDANDPATH:
954 if (header->dwSize != sizeof(DIPROPGUIDANDPATH)) return DIERR_INVALIDPARAM;
955 break;
958 switch (LOWORD( guid ))
960 case (DWORD_PTR)DIPROP_PRODUCTNAME:
961 case (DWORD_PTR)DIPROP_INSTANCENAME:
962 case (DWORD_PTR)DIPROP_VIDPID:
963 case (DWORD_PTR)DIPROP_JOYSTICKID:
964 case (DWORD_PTR)DIPROP_GUIDANDPATH:
965 case (DWORD_PTR)DIPROP_BUFFERSIZE:
966 case (DWORD_PTR)DIPROP_FFGAIN:
967 case (DWORD_PTR)DIPROP_TYPENAME:
968 case (DWORD_PTR)DIPROP_USERNAME:
969 case (DWORD_PTR)DIPROP_AUTOCENTER:
970 case (DWORD_PTR)DIPROP_AXISMODE:
971 case (DWORD_PTR)DIPROP_FFLOAD:
972 if (header->dwHow != DIPH_DEVICE) return DIERR_UNSUPPORTED;
973 if (header->dwObj) return DIERR_INVALIDPARAM;
974 break;
976 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
977 case (DWORD_PTR)DIPROP_LOGICALRANGE:
978 case (DWORD_PTR)DIPROP_RANGE:
979 case (DWORD_PTR)DIPROP_DEADZONE:
980 case (DWORD_PTR)DIPROP_SATURATION:
981 case (DWORD_PTR)DIPROP_GRANULARITY:
982 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
983 if (header->dwHow == DIPH_DEVICE && !set) return DIERR_UNSUPPORTED;
984 break;
986 case (DWORD_PTR)DIPROP_KEYNAME:
987 if (header->dwHow == DIPH_DEVICE) return DIERR_INVALIDPARAM;
988 break;
990 case (DWORD_PTR)DIPROP_SCANCODE:
991 case (DWORD_PTR)DIPROP_APPDATA:
992 if (header->dwHow == DIPH_DEVICE) return DIERR_UNSUPPORTED;
993 break;
996 if (set)
998 switch (LOWORD( guid ))
1000 case (DWORD_PTR)DIPROP_AUTOCENTER:
1001 if (impl->status == STATUS_ACQUIRED && !is_exclusively_acquired( impl )) return DIERR_ACQUIRED;
1002 break;
1003 case (DWORD_PTR)DIPROP_AXISMODE:
1004 case (DWORD_PTR)DIPROP_BUFFERSIZE:
1005 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1006 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1007 if (impl->status == STATUS_ACQUIRED) return DIERR_ACQUIRED;
1008 break;
1009 case (DWORD_PTR)DIPROP_FFLOAD:
1010 case (DWORD_PTR)DIPROP_GRANULARITY:
1011 case (DWORD_PTR)DIPROP_VIDPID:
1012 case (DWORD_PTR)DIPROP_TYPENAME:
1013 case (DWORD_PTR)DIPROP_USERNAME:
1014 case (DWORD_PTR)DIPROP_GUIDANDPATH:
1015 return DIERR_READONLY;
1018 switch (LOWORD( guid ))
1020 case (DWORD_PTR)DIPROP_RANGE:
1022 const DIPROPRANGE *value = (const DIPROPRANGE *)header;
1023 if (value->lMin > value->lMax) return DIERR_INVALIDPARAM;
1024 break;
1026 case (DWORD_PTR)DIPROP_DEADZONE:
1027 case (DWORD_PTR)DIPROP_SATURATION:
1028 case (DWORD_PTR)DIPROP_FFGAIN:
1030 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1031 if (value->dwData > 10000) return DIERR_INVALIDPARAM;
1032 break;
1034 case (DWORD_PTR)DIPROP_AUTOCENTER:
1035 case (DWORD_PTR)DIPROP_AXISMODE:
1036 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1038 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1039 if (value->dwData > 1) return DIERR_INVALIDPARAM;
1040 break;
1042 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1043 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1044 return DIERR_UNSUPPORTED;
1047 else
1049 switch (LOWORD( guid ))
1051 case (DWORD_PTR)DIPROP_RANGE:
1052 case (DWORD_PTR)DIPROP_GRANULARITY:
1053 if (!impl->caps.dwAxes) return DIERR_UNSUPPORTED;
1054 break;
1056 case (DWORD_PTR)DIPROP_KEYNAME:
1057 /* not supported on the mouse */
1058 if (impl->caps.dwAxes && !(impl->caps.dwDevType & DIDEVTYPE_HID)) return DIERR_UNSUPPORTED;
1059 break;
1061 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1062 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1063 case (DWORD_PTR)DIPROP_DEADZONE:
1064 case (DWORD_PTR)DIPROP_SATURATION:
1065 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1066 if (!impl->object_properties) return DIERR_UNSUPPORTED;
1067 break;
1069 case (DWORD_PTR)DIPROP_FFLOAD:
1070 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1071 if (!is_exclusively_acquired( impl )) return DIERR_NOTEXCLUSIVEACQUIRED;
1072 /* fallthrough */
1073 case (DWORD_PTR)DIPROP_PRODUCTNAME:
1074 case (DWORD_PTR)DIPROP_INSTANCENAME:
1075 case (DWORD_PTR)DIPROP_VIDPID:
1076 case (DWORD_PTR)DIPROP_JOYSTICKID:
1077 case (DWORD_PTR)DIPROP_GUIDANDPATH:
1078 if (!impl->vtbl->get_property) return DIERR_UNSUPPORTED;
1079 break;
1083 return DI_OK;
1086 static BOOL CALLBACK find_object( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1088 *(DIDEVICEOBJECTINSTANCEW *)context = *instance;
1089 return DIENUM_STOP;
1092 struct get_object_property_params
1094 IDirectInputDevice8W *iface;
1095 DIPROPHEADER *header;
1096 DWORD property;
1099 static BOOL CALLBACK get_object_property( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1101 static const struct object_properties default_properties =
1103 .range_min = DIPROPRANGE_NOMIN,
1104 .range_max = DIPROPRANGE_NOMAX,
1106 struct get_object_property_params *params = context;
1107 struct dinput_device *impl = impl_from_IDirectInputDevice8W( params->iface );
1108 const struct object_properties *properties = NULL;
1110 if (!impl->object_properties) properties = &default_properties;
1111 else properties = impl->object_properties + instance->dwOfs / sizeof(LONG);
1113 switch (params->property)
1115 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1117 DIPROPRANGE *value = (DIPROPRANGE *)params->header;
1118 value->lMin = properties->physical_min;
1119 value->lMax = properties->physical_max;
1120 return DI_OK;
1122 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1124 DIPROPRANGE *value = (DIPROPRANGE *)params->header;
1125 value->lMin = properties->logical_min;
1126 value->lMax = properties->logical_max;
1127 return DI_OK;
1129 case (DWORD_PTR)DIPROP_RANGE:
1131 DIPROPRANGE *value = (DIPROPRANGE *)params->header;
1132 value->lMin = properties->range_min;
1133 value->lMax = properties->range_max;
1134 return DIENUM_STOP;
1136 case (DWORD_PTR)DIPROP_DEADZONE:
1138 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1139 value->dwData = properties->deadzone;
1140 return DIENUM_STOP;
1142 case (DWORD_PTR)DIPROP_SATURATION:
1144 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1145 value->dwData = properties->saturation;
1146 return DIENUM_STOP;
1148 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1150 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1151 value->dwData = properties->calibration_mode;
1152 return DI_OK;
1154 case (DWORD_PTR)DIPROP_GRANULARITY:
1156 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1157 value->dwData = 1;
1158 return DIENUM_STOP;
1160 case (DWORD_PTR)DIPROP_KEYNAME:
1162 DIPROPSTRING *value = (DIPROPSTRING *)params->header;
1163 lstrcpynW( value->wsz, instance->tszName, ARRAY_SIZE(value->wsz) );
1164 return DIENUM_STOP;
1168 return DIENUM_STOP;
1171 static HRESULT dinput_device_get_property( IDirectInputDevice8W *iface, const GUID *guid, DIPROPHEADER *header )
1173 struct get_object_property_params params = {.iface = iface, .header = header, .property = LOWORD( guid )};
1174 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1175 DWORD object_mask = DIDFT_AXIS | DIDFT_BUTTON | DIDFT_POV;
1176 DIPROPHEADER filter;
1177 HRESULT hr;
1179 filter = *header;
1180 if (FAILED(hr = enum_object_filter_init( impl, &filter ))) return hr;
1181 if (FAILED(hr = check_property( impl, guid, header, FALSE ))) return hr;
1183 switch (LOWORD( guid ))
1185 case (DWORD_PTR)DIPROP_PRODUCTNAME:
1186 case (DWORD_PTR)DIPROP_INSTANCENAME:
1187 case (DWORD_PTR)DIPROP_VIDPID:
1188 case (DWORD_PTR)DIPROP_JOYSTICKID:
1189 case (DWORD_PTR)DIPROP_GUIDANDPATH:
1190 case (DWORD_PTR)DIPROP_FFLOAD:
1191 return impl->vtbl->get_property( iface, LOWORD( guid ), header, NULL );
1193 case (DWORD_PTR)DIPROP_RANGE:
1194 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1195 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1196 case (DWORD_PTR)DIPROP_DEADZONE:
1197 case (DWORD_PTR)DIPROP_SATURATION:
1198 case (DWORD_PTR)DIPROP_GRANULARITY:
1199 case (DWORD_PTR)DIPROP_KEYNAME:
1200 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1201 hr = impl->vtbl->enum_objects( iface, &filter, object_mask, get_object_property, &params );
1202 if (FAILED(hr)) return hr;
1203 if (hr == DIENUM_CONTINUE) return DIERR_NOTFOUND;
1204 return DI_OK;
1206 case (DWORD_PTR)DIPROP_AUTOCENTER:
1208 DIPROPDWORD *value = (DIPROPDWORD *)header;
1209 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1210 value->dwData = impl->autocenter;
1211 return DI_OK;
1213 case (DWORD_PTR)DIPROP_BUFFERSIZE:
1215 DIPROPDWORD *value = (DIPROPDWORD *)header;
1216 value->dwData = impl->buffersize;
1217 return DI_OK;
1219 case (DWORD_PTR)DIPROP_USERNAME:
1221 DIPROPSTRING *value = (DIPROPSTRING *)header;
1222 struct DevicePlayer *device_player;
1223 LIST_FOR_EACH_ENTRY( device_player, &impl->dinput->device_players, struct DevicePlayer, entry )
1225 if (IsEqualGUID( &device_player->instance_guid, &impl->guid ))
1227 if (!*device_player->username) break;
1228 lstrcpynW( value->wsz, device_player->username, ARRAY_SIZE(value->wsz) );
1229 return DI_OK;
1232 return S_FALSE;
1234 case (DWORD_PTR)DIPROP_FFGAIN:
1236 DIPROPDWORD *value = (DIPROPDWORD *)header;
1237 value->dwData = impl->device_gain;
1238 return DI_OK;
1240 case (DWORD_PTR)DIPROP_CALIBRATION:
1241 return DIERR_INVALIDPARAM;
1242 default:
1243 FIXME( "Unknown property %s\n", debugstr_guid( guid ) );
1244 return DIERR_UNSUPPORTED;
1247 return DI_OK;
1250 static HRESULT WINAPI dinput_device_GetProperty( IDirectInputDevice8W *iface, const GUID *guid, DIPROPHEADER *header )
1252 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1253 HRESULT hr;
1255 TRACE( "iface %p, guid %s, header %p\n", iface, debugstr_guid( guid ), header );
1257 if (!header) return DIERR_INVALIDPARAM;
1258 if (header->dwHeaderSize != sizeof(DIPROPHEADER)) return DIERR_INVALIDPARAM;
1259 if (!IS_DIPROP( guid )) return DI_OK;
1261 EnterCriticalSection( &impl->crit );
1262 hr = dinput_device_get_property( iface, guid, header );
1263 LeaveCriticalSection( &impl->crit );
1265 return hr;
1268 struct set_object_property_params
1270 IDirectInputDevice8W *iface;
1271 const DIPROPHEADER *header;
1272 DWORD property;
1275 static BOOL CALLBACK set_object_property( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1277 struct set_object_property_params *params = context;
1278 struct dinput_device *impl = impl_from_IDirectInputDevice8W( params->iface );
1279 struct object_properties *properties = NULL;
1281 if (!impl->object_properties) return DIENUM_STOP;
1282 properties = impl->object_properties + instance->dwOfs / sizeof(LONG);
1284 switch (params->property)
1286 case (DWORD_PTR)DIPROP_RANGE:
1288 const DIPROPRANGE *value = (const DIPROPRANGE *)params->header;
1289 properties->range_min = value->lMin;
1290 properties->range_max = value->lMax;
1291 return DIENUM_CONTINUE;
1293 case (DWORD_PTR)DIPROP_DEADZONE:
1295 const DIPROPDWORD *value = (const DIPROPDWORD *)params->header;
1296 properties->deadzone = value->dwData;
1297 return DIENUM_CONTINUE;
1299 case (DWORD_PTR)DIPROP_SATURATION:
1301 const DIPROPDWORD *value = (const DIPROPDWORD *)params->header;
1302 properties->saturation = value->dwData;
1303 return DIENUM_CONTINUE;
1305 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1307 const DIPROPDWORD *value = (const DIPROPDWORD *)params->header;
1308 properties->calibration_mode = value->dwData;
1309 return DIENUM_CONTINUE;
1313 return DIENUM_STOP;
1316 static BOOL CALLBACK reset_object_value( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1318 struct dinput_device *impl = context;
1319 struct object_properties *properties;
1320 LONG tmp = -1;
1322 if (!impl->object_properties) return DIENUM_STOP;
1323 properties = impl->object_properties + instance->dwOfs / sizeof(LONG);
1325 if (instance->dwType & DIDFT_AXIS)
1327 if (!properties->range_min) tmp = properties->range_max / 2;
1328 else tmp = round( (properties->range_min + properties->range_max) / 2.0 );
1331 *(LONG *)(impl->device_state + instance->dwOfs) = tmp;
1332 return DIENUM_CONTINUE;
1335 static void reset_device_state( IDirectInputDevice8W *iface )
1337 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1338 DIPROPHEADER filter =
1340 .dwHeaderSize = sizeof(DIPROPHEADER),
1341 .dwSize = sizeof(DIPROPHEADER),
1342 .dwHow = DIPH_DEVICE,
1343 .dwObj = 0,
1346 impl->vtbl->enum_objects( iface, &filter, DIDFT_AXIS | DIDFT_POV, reset_object_value, impl );
1349 static HRESULT dinput_device_set_property( IDirectInputDevice8W *iface, const GUID *guid,
1350 const DIPROPHEADER *header )
1352 struct set_object_property_params params = {.iface = iface, .header = header, .property = LOWORD( guid )};
1353 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1354 DWORD object_mask = DIDFT_AXIS | DIDFT_BUTTON | DIDFT_POV;
1355 DIDEVICEOBJECTINSTANCEW instance;
1356 DIPROPHEADER filter;
1357 HRESULT hr;
1359 filter = *header;
1360 if (FAILED(hr = enum_object_filter_init( impl, &filter ))) return hr;
1361 if (FAILED(hr = check_property( impl, guid, header, TRUE ))) return hr;
1363 switch (LOWORD( guid ))
1365 case (DWORD_PTR)DIPROP_RANGE:
1366 case (DWORD_PTR)DIPROP_DEADZONE:
1367 case (DWORD_PTR)DIPROP_SATURATION:
1369 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_AXIS, set_object_property, &params );
1370 if (FAILED(hr)) return hr;
1371 reset_device_state( iface );
1372 return DI_OK;
1374 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1376 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1377 if (value->dwData > DIPROPCALIBRATIONMODE_RAW) return DIERR_INVALIDPARAM;
1378 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_AXIS, set_object_property, &params );
1379 if (FAILED(hr)) return hr;
1380 reset_device_state( iface );
1381 return DI_OK;
1383 case (DWORD_PTR)DIPROP_AUTOCENTER:
1385 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1386 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1388 FIXME( "DIPROP_AUTOCENTER stub!\n" );
1389 impl->autocenter = value->dwData;
1390 return DI_OK;
1392 case (DWORD_PTR)DIPROP_FFGAIN:
1394 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1395 impl->device_gain = value->dwData;
1396 if (!is_exclusively_acquired( impl )) return DI_OK;
1397 if (!impl->vtbl->send_device_gain) return DI_OK;
1398 return impl->vtbl->send_device_gain( iface, impl->device_gain );
1400 case (DWORD_PTR)DIPROP_AXISMODE:
1402 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1404 TRACE( "Axis mode: %s\n", value->dwData == DIPROPAXISMODE_ABS ? "absolute" : "relative" );
1406 impl->user_format.dwFlags &= ~DIDFT_AXIS;
1407 impl->user_format.dwFlags |= value->dwData == DIPROPAXISMODE_ABS ? DIDF_ABSAXIS : DIDF_RELAXIS;
1409 return DI_OK;
1411 case (DWORD_PTR)DIPROP_BUFFERSIZE:
1413 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1415 TRACE( "buffersize %lu\n", value->dwData );
1417 impl->buffersize = value->dwData;
1418 impl->queue_len = min( impl->buffersize, 1024 );
1419 free( impl->data_queue );
1421 impl->data_queue = impl->queue_len ? malloc( impl->queue_len * sizeof(DIDEVICEOBJECTDATA) ) : NULL;
1422 impl->queue_head = impl->queue_tail = impl->overflow = 0;
1423 return DI_OK;
1425 case (DWORD_PTR)DIPROP_APPDATA:
1427 const DIPROPPOINTER *value = (const DIPROPPOINTER *)header;
1428 int user_offset;
1429 hr = impl->vtbl->enum_objects( iface, &filter, object_mask, find_object, &instance );
1430 if (FAILED(hr)) return hr;
1431 if (hr == DIENUM_CONTINUE) return DIERR_OBJECTNOTFOUND;
1432 if ((user_offset = id_to_offset( impl, instance.dwType )) < 0) return DIERR_OBJECTNOTFOUND;
1433 if (!set_app_data( impl, user_offset, value->uData )) return DIERR_OUTOFMEMORY;
1434 return DI_OK;
1436 default:
1437 FIXME( "Unknown property %s\n", debugstr_guid( guid ) );
1438 return DIERR_UNSUPPORTED;
1441 return DI_OK;
1444 static HRESULT WINAPI dinput_device_SetProperty( IDirectInputDevice8W *iface, const GUID *guid,
1445 const DIPROPHEADER *header )
1447 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1448 HRESULT hr;
1450 TRACE( "iface %p, guid %s, header %p\n", iface, debugstr_guid( guid ), header );
1452 if (!header) return DIERR_INVALIDPARAM;
1453 if (header->dwHeaderSize != sizeof(DIPROPHEADER)) return DIERR_INVALIDPARAM;
1454 if (!IS_DIPROP( guid )) return DI_OK;
1456 EnterCriticalSection( &impl->crit );
1457 hr = dinput_device_set_property( iface, guid, header );
1458 LeaveCriticalSection( &impl->crit );
1460 return hr;
1463 static void dinput_device_set_username( struct dinput_device *impl, const DIPROPSTRING *value )
1465 struct DevicePlayer *device_player;
1466 BOOL found = FALSE;
1468 LIST_FOR_EACH_ENTRY( device_player, &impl->dinput->device_players, struct DevicePlayer, entry )
1470 if (IsEqualGUID( &device_player->instance_guid, &impl->guid ))
1472 found = TRUE;
1473 break;
1476 if (!found && (device_player = malloc( sizeof(struct DevicePlayer) )))
1478 list_add_tail( &impl->dinput->device_players, &device_player->entry );
1479 device_player->instance_guid = impl->guid;
1481 if (device_player)
1482 lstrcpynW( device_player->username, value->wsz, ARRAY_SIZE(device_player->username) );
1485 static BOOL CALLBACK get_object_info( const DIDEVICEOBJECTINSTANCEW *instance, void *data )
1487 DIDEVICEOBJECTINSTANCEW *dest = data;
1488 DWORD size = dest->dwSize;
1490 memcpy( dest, instance, size );
1491 dest->dwSize = size;
1493 return DIENUM_STOP;
1496 static HRESULT WINAPI dinput_device_GetObjectInfo( IDirectInputDevice8W *iface,
1497 DIDEVICEOBJECTINSTANCEW *instance, DWORD obj, DWORD how )
1499 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1500 DIPROPHEADER filter =
1502 .dwSize = sizeof(filter),
1503 .dwHeaderSize = sizeof(filter),
1504 .dwHow = how,
1505 .dwObj = obj
1507 HRESULT hr;
1509 TRACE( "iface %p, instance %p, obj %#lx, how %#lx.\n", iface, instance, obj, how );
1511 if (!instance) return E_POINTER;
1512 if (instance->dwSize != sizeof(DIDEVICEOBJECTINSTANCE_DX3W) && instance->dwSize != sizeof(DIDEVICEOBJECTINSTANCEW))
1513 return DIERR_INVALIDPARAM;
1514 if (how == DIPH_DEVICE) return DIERR_INVALIDPARAM;
1515 if (FAILED(hr = enum_object_filter_init( impl, &filter ))) return hr;
1517 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_ALL, get_object_info, instance );
1518 if (FAILED(hr)) return hr;
1519 if (hr == DIENUM_CONTINUE) return DIERR_NOTFOUND;
1520 return DI_OK;
1523 static HRESULT WINAPI dinput_device_GetDeviceState( IDirectInputDevice8W *iface, DWORD size, void *data )
1525 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1526 DIDATAFORMAT *device_format = &impl->device_format, *user_format = &impl->user_format;
1527 DIOBJECTDATAFORMAT *device_obj, *user_obj;
1528 BYTE *user_state = data;
1529 DIPROPHEADER filter =
1531 .dwSize = sizeof(filter),
1532 .dwHeaderSize = sizeof(filter),
1533 .dwHow = DIPH_DEVICE,
1534 .dwObj = 0,
1536 HRESULT hr;
1538 TRACE( "iface %p, size %lu, data %p.\n", iface, size, data );
1540 if (!data) return DIERR_INVALIDPARAM;
1542 IDirectInputDevice2_Poll( iface );
1544 EnterCriticalSection( &impl->crit );
1545 if (impl->status == STATUS_UNPLUGGED)
1546 hr = DIERR_INPUTLOST;
1547 else if (impl->status != STATUS_ACQUIRED)
1548 hr = DIERR_NOTACQUIRED;
1549 else if (!user_format->rgodf)
1550 hr = DIERR_INVALIDPARAM;
1551 else if (size != user_format->dwDataSize)
1552 hr = DIERR_INVALIDPARAM;
1553 else
1555 memset( user_state, 0, size );
1557 user_obj = user_format->rgodf + device_format->dwNumObjs;
1558 device_obj = device_format->rgodf + device_format->dwNumObjs;
1559 while (user_obj-- > user_format->rgodf && device_obj-- > device_format->rgodf)
1561 if (user_obj->dwType & DIDFT_BUTTON)
1562 user_state[user_obj->dwOfs] = impl->device_state[device_obj->dwOfs];
1565 /* reset optional POVs to their default */
1566 user_obj = user_format->rgodf + user_format->dwNumObjs;
1567 while (user_obj-- > user_format->rgodf + device_format->dwNumObjs)
1568 if (user_obj->dwType & DIDFT_POV) *(ULONG *)(user_state + user_obj->dwOfs) = 0xffffffff;
1570 user_obj = user_format->rgodf + device_format->dwNumObjs;
1571 device_obj = device_format->rgodf + device_format->dwNumObjs;
1572 while (user_obj-- > user_format->rgodf && device_obj-- > device_format->rgodf)
1574 if (user_obj->dwType & (DIDFT_POV | DIDFT_AXIS))
1575 *(ULONG *)(user_state + user_obj->dwOfs) = *(ULONG *)(impl->device_state + device_obj->dwOfs);
1576 if (!(user_format->dwFlags & DIDF_ABSAXIS) && (device_obj->dwType & DIDFT_RELAXIS))
1577 *(ULONG *)(impl->device_state + device_obj->dwOfs) = 0;
1580 hr = DI_OK;
1582 LeaveCriticalSection( &impl->crit );
1584 return hr;
1587 static HRESULT WINAPI dinput_device_GetDeviceData( IDirectInputDevice8W *iface, DWORD size, DIDEVICEOBJECTDATA *data,
1588 DWORD *count, DWORD flags )
1590 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
1591 HRESULT ret = DI_OK;
1592 int len;
1594 TRACE( "iface %p, size %lu, data %p, count %p, flags %#lx.\n", iface, size, data, count, flags );
1596 if (This->dinput->dwVersion == 0x0800 || size == sizeof(DIDEVICEOBJECTDATA_DX3))
1598 if (!This->queue_len) return DIERR_NOTBUFFERED;
1599 if (This->status == STATUS_UNPLUGGED) return DIERR_INPUTLOST;
1600 if (This->status != STATUS_ACQUIRED) return DIERR_NOTACQUIRED;
1603 if (!This->queue_len)
1604 return DI_OK;
1605 if (size < sizeof(DIDEVICEOBJECTDATA_DX3)) return DIERR_INVALIDPARAM;
1607 IDirectInputDevice2_Poll(iface);
1608 EnterCriticalSection(&This->crit);
1610 len = This->queue_head - This->queue_tail;
1611 if (len < 0) len += This->queue_len;
1613 if ((*count != INFINITE) && (len > *count)) len = *count;
1615 if (data)
1617 int i;
1618 for (i = 0; i < len; i++)
1620 int n = (This->queue_tail + i) % This->queue_len;
1621 memcpy( (char *)data + size * i, This->data_queue + n, size );
1624 *count = len;
1626 if (This->overflow && This->dinput->dwVersion == 0x0800)
1627 ret = DI_BUFFEROVERFLOW;
1629 if (!(flags & DIGDD_PEEK))
1631 /* Advance reading position */
1632 This->queue_tail = (This->queue_tail + len) % This->queue_len;
1633 This->overflow = FALSE;
1636 LeaveCriticalSection(&This->crit);
1638 TRACE( "Returning %lu events queued\n", *count );
1639 return ret;
1642 static HRESULT WINAPI dinput_device_RunControlPanel( IDirectInputDevice8W *iface, HWND hwnd, DWORD flags )
1644 FIXME( "iface %p, hwnd %p, flags %#lx stub!\n", iface, hwnd, flags );
1645 return DI_OK;
1648 static HRESULT WINAPI dinput_device_Initialize( IDirectInputDevice8W *iface, HINSTANCE instance,
1649 DWORD version, const GUID *guid )
1651 FIXME( "iface %p, instance %p, version %#lx, guid %s stub!\n", iface, instance, version,
1652 debugstr_guid( guid ) );
1653 return DI_OK;
1656 static HRESULT WINAPI dinput_device_CreateEffect( IDirectInputDevice8W *iface, const GUID *guid,
1657 const DIEFFECT *params, IDirectInputEffect **out,
1658 IUnknown *outer )
1660 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1661 DWORD flags;
1662 HRESULT hr;
1664 TRACE( "iface %p, guid %s, params %p, out %p, outer %p\n", iface, debugstr_guid( guid ),
1665 params, out, outer );
1667 if (!out) return E_POINTER;
1668 *out = NULL;
1670 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1671 if (!impl->vtbl->create_effect) return DIERR_UNSUPPORTED;
1672 if (FAILED(hr = impl->vtbl->create_effect( iface, out ))) return hr;
1674 hr = IDirectInputEffect_Initialize( *out, DINPUT_instance, impl->dinput->dwVersion, guid );
1675 if (FAILED(hr)) goto failed;
1676 if (!params) return DI_OK;
1678 flags = params->dwSize == sizeof(DIEFFECT_DX6) ? DIEP_ALLPARAMS : DIEP_ALLPARAMS_DX5;
1679 if (!is_exclusively_acquired( impl )) flags |= DIEP_NODOWNLOAD;
1680 hr = IDirectInputEffect_SetParameters( *out, params, flags );
1681 if (FAILED(hr)) goto failed;
1682 return DI_OK;
1684 failed:
1685 IDirectInputEffect_Release( *out );
1686 *out = NULL;
1687 return hr;
1690 static HRESULT WINAPI dinput_device_EnumEffects( IDirectInputDevice8W *iface, LPDIENUMEFFECTSCALLBACKW callback,
1691 void *context, DWORD type )
1693 DIEFFECTINFOW info = {.dwSize = sizeof(info)};
1694 HRESULT hr;
1696 TRACE( "iface %p, callback %p, context %p, type %#lx.\n", iface, callback, context, type );
1698 if (!callback) return DIERR_INVALIDPARAM;
1700 type = DIEFT_GETTYPE( type );
1702 if (type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1704 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_ConstantForce );
1705 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1706 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1709 if (type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1711 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_RampForce );
1712 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1713 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1716 if (type == DIEFT_ALL || type == DIEFT_PERIODIC)
1718 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Square );
1719 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1720 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1722 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Sine );
1723 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1724 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1726 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Triangle );
1727 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1728 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1730 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_SawtoothUp );
1731 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1732 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1734 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_SawtoothDown );
1735 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1736 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1739 if (type == DIEFT_ALL || type == DIEFT_CONDITION)
1741 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Spring );
1742 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1743 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1745 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Damper );
1746 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1747 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1749 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Inertia );
1750 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1751 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1753 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Friction );
1754 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1755 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1758 return DI_OK;
1761 static HRESULT WINAPI dinput_device_GetEffectInfo( IDirectInputDevice8W *iface, DIEFFECTINFOW *info,
1762 const GUID *guid )
1764 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1766 TRACE( "iface %p, info %p, guid %s.\n", iface, info, debugstr_guid( guid ) );
1768 if (!info) return E_POINTER;
1769 if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
1770 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_DEVICENOTREG;
1771 if (!impl->vtbl->get_effect_info) return DIERR_UNSUPPORTED;
1772 return impl->vtbl->get_effect_info( iface, info, guid );
1775 static HRESULT WINAPI dinput_device_GetForceFeedbackState( IDirectInputDevice8W *iface, DWORD *out )
1777 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1778 HRESULT hr = DI_OK;
1780 TRACE( "iface %p, out %p.\n", iface, out );
1782 if (!out) return E_POINTER;
1783 *out = 0;
1785 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1787 EnterCriticalSection( &impl->crit );
1788 if (!is_exclusively_acquired( impl )) hr = DIERR_NOTEXCLUSIVEACQUIRED;
1789 else *out = impl->force_feedback_state;
1790 LeaveCriticalSection( &impl->crit );
1792 return hr;
1795 static HRESULT WINAPI dinput_device_SendForceFeedbackCommand( IDirectInputDevice8W *iface, DWORD command )
1797 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1798 HRESULT hr;
1800 TRACE( "iface %p, command %#lx.\n", iface, command );
1802 switch (command)
1804 case DISFFC_RESET: break;
1805 case DISFFC_STOPALL: break;
1806 case DISFFC_PAUSE: break;
1807 case DISFFC_CONTINUE: break;
1808 case DISFFC_SETACTUATORSON: break;
1809 case DISFFC_SETACTUATORSOFF: break;
1810 default: return DIERR_INVALIDPARAM;
1813 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1814 if (!impl->vtbl->send_force_feedback_command) return DIERR_UNSUPPORTED;
1816 EnterCriticalSection( &impl->crit );
1817 if (!is_exclusively_acquired( impl )) hr = DIERR_NOTEXCLUSIVEACQUIRED;
1818 else hr = impl->vtbl->send_force_feedback_command( iface, command, FALSE );
1819 LeaveCriticalSection( &impl->crit );
1821 return hr;
1824 static HRESULT WINAPI dinput_device_EnumCreatedEffectObjects( IDirectInputDevice8W *iface,
1825 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK callback,
1826 void *context, DWORD flags )
1828 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1830 TRACE( "iface %p, callback %p, context %p, flags %#lx.\n", iface, callback, context, flags );
1832 if (!callback) return DIERR_INVALIDPARAM;
1833 if (flags) return DIERR_INVALIDPARAM;
1834 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DI_OK;
1835 if (!impl->vtbl->enum_created_effect_objects) return DIERR_UNSUPPORTED;
1837 return impl->vtbl->enum_created_effect_objects( iface, callback, context, flags );
1840 static HRESULT WINAPI dinput_device_Escape( IDirectInputDevice8W *iface, DIEFFESCAPE *escape )
1842 FIXME( "iface %p, escape %p stub!\n", iface, escape );
1843 return DI_OK;
1846 static HRESULT WINAPI dinput_device_Poll( IDirectInputDevice8W *iface )
1848 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1849 HRESULT hr = DI_NOEFFECT;
1851 EnterCriticalSection( &impl->crit );
1852 if (impl->status == STATUS_UNPLUGGED) hr = DIERR_INPUTLOST;
1853 else if (impl->status != STATUS_ACQUIRED) hr = DIERR_NOTACQUIRED;
1854 LeaveCriticalSection( &impl->crit );
1855 if (FAILED(hr)) return hr;
1857 if (impl->vtbl->poll) return impl->vtbl->poll( iface );
1858 return hr;
1861 static HRESULT WINAPI dinput_device_SendDeviceData( IDirectInputDevice8W *iface, DWORD size,
1862 const DIDEVICEOBJECTDATA *data, DWORD *count, DWORD flags )
1864 FIXME( "iface %p, size %lu, data %p, count %p, flags %#lx stub!\n", iface, size, data, count, flags );
1865 return DI_OK;
1868 static HRESULT WINAPI dinput_device_EnumEffectsInFile( IDirectInputDevice8W *iface, const WCHAR *filename,
1869 LPDIENUMEFFECTSINFILECALLBACK callback,
1870 void *context, DWORD flags )
1872 FIXME( "iface %p, filename %s, callback %p, context %p, flags %#lx stub!\n", iface,
1873 debugstr_w(filename), callback, context, flags );
1874 return DI_OK;
1877 static HRESULT WINAPI dinput_device_WriteEffectToFile( IDirectInputDevice8W *iface, const WCHAR *filename,
1878 DWORD count, DIFILEEFFECT *effects, DWORD flags )
1880 FIXME( "iface %p, filename %s, count %lu, effects %p, flags %#lx stub!\n", iface,
1881 debugstr_w(filename), count, effects, flags );
1882 return DI_OK;
1885 static HRESULT WINAPI dinput_device_BuildActionMap( IDirectInputDevice8W *iface, DIACTIONFORMATW *format,
1886 const WCHAR *username, DWORD flags )
1888 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1889 BOOL load_success = FALSE, has_actions = FALSE;
1890 DWORD genre, username_len = MAX_PATH;
1891 WCHAR username_buf[MAX_PATH];
1892 const DIDATAFORMAT *df;
1893 DWORD devMask;
1894 int i;
1896 FIXME( "iface %p, format %p, username %s, flags %#lx stub!\n", iface, format,
1897 debugstr_w(username), flags );
1899 if (!format) return DIERR_INVALIDPARAM;
1901 switch (GET_DIDEVICE_TYPE( impl->instance.dwDevType ))
1903 case DIDEVTYPE_KEYBOARD:
1904 case DI8DEVTYPE_KEYBOARD:
1905 devMask = DIKEYBOARD_MASK;
1906 df = &c_dfDIKeyboard;
1907 break;
1908 case DIDEVTYPE_MOUSE:
1909 case DI8DEVTYPE_MOUSE:
1910 devMask = DIMOUSE_MASK;
1911 df = &c_dfDIMouse2;
1912 break;
1913 default:
1914 devMask = DIGENRE_ANY;
1915 df = &impl->device_format;
1916 break;
1919 /* Unless asked the contrary by these flags, try to load a previous mapping */
1920 if (!(flags & DIDBAM_HWDEFAULTS))
1922 /* Retrieve logged user name if necessary */
1923 if (username == NULL) GetUserNameW( username_buf, &username_len );
1924 else lstrcpynW( username_buf, username, MAX_PATH );
1925 load_success = load_mapping_settings( impl, format, username_buf );
1928 if (load_success) return DI_OK;
1930 for (i = 0; i < format->dwNumActions; i++)
1932 /* Don't touch a user configured action */
1933 if (format->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
1935 genre = format->rgoAction[i].dwSemantic & DIGENRE_ANY;
1936 if (devMask == genre || (devMask == DIGENRE_ANY && genre != DIMOUSE_MASK && genre != DIKEYBOARD_MASK))
1938 DWORD obj_id = semantic_to_obj_id( impl, format->rgoAction[i].dwSemantic );
1939 DWORD type = DIDFT_GETTYPE( obj_id );
1940 DWORD inst = DIDFT_GETINSTANCE( obj_id );
1942 LPDIOBJECTDATAFORMAT odf;
1944 if (type == DIDFT_PSHBUTTON) type = DIDFT_BUTTON;
1945 if (type == DIDFT_RELAXIS) type = DIDFT_AXIS;
1947 /* Make sure the object exists */
1948 odf = dataformat_to_odf_by_type( df, inst, type );
1950 if (odf != NULL)
1952 format->rgoAction[i].dwObjID = obj_id;
1953 format->rgoAction[i].guidInstance = impl->guid;
1954 format->rgoAction[i].dwHow = DIAH_DEFAULT;
1955 has_actions = TRUE;
1958 else if (!(flags & DIDBAM_PRESERVE))
1960 /* We must clear action data belonging to other devices */
1961 memset( &format->rgoAction[i].guidInstance, 0, sizeof(GUID) );
1962 format->rgoAction[i].dwHow = DIAH_UNMAPPED;
1966 if (!has_actions) return DI_NOEFFECT;
1967 if (flags & (DIDBAM_DEFAULT|DIDBAM_PRESERVE|DIDBAM_INITIALIZE|DIDBAM_HWDEFAULTS))
1968 FIXME( "Unimplemented flags %#lx\n", flags );
1969 return DI_OK;
1972 static HRESULT WINAPI dinput_device_SetActionMap( IDirectInputDevice8W *iface, DIACTIONFORMATW *format,
1973 const WCHAR *username, DWORD flags )
1975 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1976 DIDATAFORMAT data_format;
1977 DIOBJECTDATAFORMAT *obj_df = NULL;
1978 DIPROPDWORD dp;
1979 DIPROPRANGE dpr;
1980 DIPROPSTRING dps;
1981 WCHAR username_buf[MAX_PATH];
1982 DWORD username_len = MAX_PATH;
1983 int i, action = 0, num_actions = 0;
1984 unsigned int offset = 0;
1985 const DIDATAFORMAT *df;
1986 ActionMap *action_map;
1988 FIXME( "iface %p, format %p, username %s, flags %#lx stub!\n", iface, format,
1989 debugstr_w(username), flags );
1991 if (!format) return DIERR_INVALIDPARAM;
1993 switch (GET_DIDEVICE_TYPE( impl->instance.dwDevType ))
1995 case DIDEVTYPE_KEYBOARD:
1996 case DI8DEVTYPE_KEYBOARD:
1997 df = &c_dfDIKeyboard;
1998 break;
1999 case DIDEVTYPE_MOUSE:
2000 case DI8DEVTYPE_MOUSE:
2001 df = &c_dfDIMouse2;
2002 break;
2003 default:
2004 df = &impl->device_format;
2005 break;
2008 if (impl->status == STATUS_ACQUIRED) return DIERR_ACQUIRED;
2010 data_format.dwSize = sizeof(data_format);
2011 data_format.dwObjSize = sizeof(DIOBJECTDATAFORMAT);
2012 data_format.dwFlags = DIDF_RELAXIS;
2013 data_format.dwDataSize = format->dwDataSize;
2015 /* Count the actions */
2016 for (i = 0; i < format->dwNumActions; i++)
2017 if (IsEqualGUID( &impl->guid, &format->rgoAction[i].guidInstance ))
2018 num_actions++;
2020 if (num_actions == 0) return DI_NOEFFECT;
2022 /* Construct the dataformat and actionmap */
2023 obj_df = malloc( sizeof(DIOBJECTDATAFORMAT) * num_actions );
2024 data_format.rgodf = (LPDIOBJECTDATAFORMAT)obj_df;
2025 data_format.dwNumObjs = num_actions;
2027 action_map = malloc( sizeof(ActionMap) * num_actions );
2029 for (i = 0; i < format->dwNumActions; i++)
2031 if (IsEqualGUID( &impl->guid, &format->rgoAction[i].guidInstance ))
2033 DWORD inst = DIDFT_GETINSTANCE( format->rgoAction[i].dwObjID );
2034 DWORD type = DIDFT_GETTYPE( format->rgoAction[i].dwObjID );
2035 LPDIOBJECTDATAFORMAT obj;
2037 if (type == DIDFT_PSHBUTTON) type = DIDFT_BUTTON;
2038 if (type == DIDFT_RELAXIS) type = DIDFT_AXIS;
2040 obj = dataformat_to_odf_by_type( df, inst, type );
2042 memcpy( &obj_df[action], obj, df->dwObjSize );
2044 action_map[action].uAppData = format->rgoAction[i].uAppData;
2045 action_map[action].offset = offset;
2046 obj_df[action].dwOfs = offset;
2047 offset += (type & DIDFT_BUTTON) ? 1 : 4;
2049 action++;
2053 IDirectInputDevice8_SetDataFormat( iface, &data_format );
2055 impl->action_map = action_map;
2056 impl->num_actions = num_actions;
2058 free( obj_df );
2060 /* Set the device properties according to the action format */
2061 dpr.diph.dwSize = sizeof(DIPROPRANGE);
2062 dpr.lMin = format->lAxisMin;
2063 dpr.lMax = format->lAxisMax;
2064 dpr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
2065 dpr.diph.dwObj = 0;
2066 dpr.diph.dwHow = DIPH_DEVICE;
2067 IDirectInputDevice8_SetProperty( iface, DIPROP_RANGE, &dpr.diph );
2069 if (format->dwBufferSize > 0)
2071 dp.diph.dwSize = sizeof(DIPROPDWORD);
2072 dp.dwData = format->dwBufferSize;
2073 dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
2074 dp.diph.dwObj = 0;
2075 dp.diph.dwHow = DIPH_DEVICE;
2076 IDirectInputDevice8_SetProperty( iface, DIPROP_BUFFERSIZE, &dp.diph );
2079 /* Retrieve logged user name if necessary */
2080 if (username == NULL) GetUserNameW( username_buf, &username_len );
2081 else lstrcpynW( username_buf, username, MAX_PATH );
2083 dps.diph.dwSize = sizeof(dps);
2084 dps.diph.dwHeaderSize = sizeof(DIPROPHEADER);
2085 dps.diph.dwObj = 0;
2086 dps.diph.dwHow = DIPH_DEVICE;
2087 if (flags & DIDSAM_NOUSER) dps.wsz[0] = '\0';
2088 else lstrcpynW( dps.wsz, username_buf, ARRAY_SIZE(dps.wsz) );
2089 dinput_device_set_username( impl, &dps );
2091 /* Save the settings to disk */
2092 save_mapping_settings( iface, format, username_buf );
2094 return DI_OK;
2097 static HRESULT WINAPI dinput_device_GetImageInfo( IDirectInputDevice8W *iface, DIDEVICEIMAGEINFOHEADERW *header )
2099 FIXME( "iface %p, header %p stub!\n", iface, header );
2100 return DI_OK;
2103 extern const IDirectInputDevice8AVtbl dinput_device_a_vtbl;
2104 static const IDirectInputDevice8WVtbl dinput_device_w_vtbl =
2106 /*** IUnknown methods ***/
2107 dinput_device_QueryInterface,
2108 dinput_device_AddRef,
2109 dinput_device_Release,
2110 /*** IDirectInputDevice methods ***/
2111 dinput_device_GetCapabilities,
2112 dinput_device_EnumObjects,
2113 dinput_device_GetProperty,
2114 dinput_device_SetProperty,
2115 dinput_device_Acquire,
2116 dinput_device_Unacquire,
2117 dinput_device_GetDeviceState,
2118 dinput_device_GetDeviceData,
2119 dinput_device_SetDataFormat,
2120 dinput_device_SetEventNotification,
2121 dinput_device_SetCooperativeLevel,
2122 dinput_device_GetObjectInfo,
2123 dinput_device_GetDeviceInfo,
2124 dinput_device_RunControlPanel,
2125 dinput_device_Initialize,
2126 /*** IDirectInputDevice2 methods ***/
2127 dinput_device_CreateEffect,
2128 dinput_device_EnumEffects,
2129 dinput_device_GetEffectInfo,
2130 dinput_device_GetForceFeedbackState,
2131 dinput_device_SendForceFeedbackCommand,
2132 dinput_device_EnumCreatedEffectObjects,
2133 dinput_device_Escape,
2134 dinput_device_Poll,
2135 dinput_device_SendDeviceData,
2136 /*** IDirectInputDevice7 methods ***/
2137 dinput_device_EnumEffectsInFile,
2138 dinput_device_WriteEffectToFile,
2139 /*** IDirectInputDevice8 methods ***/
2140 dinput_device_BuildActionMap,
2141 dinput_device_SetActionMap,
2142 dinput_device_GetImageInfo,
2145 void dinput_device_init( struct dinput_device *device, const struct dinput_device_vtbl *vtbl,
2146 const GUID *guid, struct dinput *dinput )
2148 device->IDirectInputDevice8A_iface.lpVtbl = &dinput_device_a_vtbl;
2149 device->IDirectInputDevice8W_iface.lpVtbl = &dinput_device_w_vtbl;
2150 device->ref = 1;
2151 device->guid = *guid;
2152 device->instance.dwSize = sizeof(DIDEVICEINSTANCEW);
2153 device->caps.dwSize = sizeof(DIDEVCAPS);
2154 device->caps.dwFlags = DIDC_ATTACHED | DIDC_EMULATED;
2155 device->device_gain = 10000;
2156 device->force_feedback_state = DIGFFS_STOPPED | DIGFFS_EMPTY;
2157 InitializeCriticalSection( &device->crit );
2158 device->dinput = dinput;
2159 IDirectInput_AddRef( &dinput->IDirectInput7A_iface );
2160 device->vtbl = vtbl;
2163 static const GUID *object_instance_guid( const DIDEVICEOBJECTINSTANCEW *instance )
2165 if (IsEqualGUID( &instance->guidType, &GUID_XAxis )) return &GUID_XAxis;
2166 if (IsEqualGUID( &instance->guidType, &GUID_YAxis )) return &GUID_YAxis;
2167 if (IsEqualGUID( &instance->guidType, &GUID_ZAxis )) return &GUID_ZAxis;
2168 if (IsEqualGUID( &instance->guidType, &GUID_RxAxis )) return &GUID_RxAxis;
2169 if (IsEqualGUID( &instance->guidType, &GUID_RyAxis )) return &GUID_RyAxis;
2170 if (IsEqualGUID( &instance->guidType, &GUID_RzAxis )) return &GUID_RzAxis;
2171 if (IsEqualGUID( &instance->guidType, &GUID_Slider )) return &GUID_Slider;
2172 if (IsEqualGUID( &instance->guidType, &GUID_Button )) return &GUID_Button;
2173 if (IsEqualGUID( &instance->guidType, &GUID_Key )) return &GUID_Key;
2174 if (IsEqualGUID( &instance->guidType, &GUID_POV )) return &GUID_POV;
2175 return &GUID_Unknown;
2178 static BOOL CALLBACK enum_objects_init( const DIDEVICEOBJECTINSTANCEW *instance, void *data )
2180 struct dinput_device *impl = impl_from_IDirectInputDevice8W( data );
2181 DIDATAFORMAT *format = &impl->device_format;
2182 DIOBJECTDATAFORMAT *obj_format;
2184 if (!format->rgodf)
2186 format->dwDataSize = max( format->dwDataSize, instance->dwOfs + sizeof(LONG) );
2187 if (instance->dwType & DIDFT_BUTTON) impl->caps.dwButtons++;
2188 if (instance->dwType & DIDFT_AXIS) impl->caps.dwAxes++;
2189 if (instance->dwType & DIDFT_POV) impl->caps.dwPOVs++;
2190 if (instance->dwType & (DIDFT_BUTTON|DIDFT_AXIS|DIDFT_POV))
2192 if (!impl->device_state_report_id)
2193 impl->device_state_report_id = instance->wReportId;
2194 else if (impl->device_state_report_id != instance->wReportId)
2195 FIXME( "multiple device state reports found!\n" );
2198 else
2200 obj_format = format->rgodf + format->dwNumObjs;
2201 obj_format->pguid = object_instance_guid( instance );
2202 obj_format->dwOfs = instance->dwOfs;
2203 obj_format->dwType = instance->dwType;
2204 obj_format->dwFlags = instance->dwFlags;
2207 if (impl->object_properties && (instance->dwType & (DIDFT_AXIS | DIDFT_POV)))
2208 reset_object_value( instance, impl );
2210 format->dwNumObjs++;
2211 return DIENUM_CONTINUE;
2214 HRESULT dinput_device_init_device_format( IDirectInputDevice8W *iface )
2216 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
2217 DIDATAFORMAT *format = &impl->device_format;
2218 ULONG i, size;
2220 IDirectInputDevice8_EnumObjects( iface, enum_objects_init, iface, DIDFT_ALL );
2221 if (format->dwDataSize > DEVICE_STATE_MAX_SIZE)
2223 FIXME( "unable to create device, state is too large\n" );
2224 return DIERR_OUTOFMEMORY;
2227 size = format->dwNumObjs * sizeof(*format->rgodf);
2228 if (!(format->rgodf = calloc( 1, size ))) return DIERR_OUTOFMEMORY;
2230 format->dwSize = sizeof(*format);
2231 format->dwObjSize = sizeof(*format->rgodf);
2232 format->dwFlags = DIDF_ABSAXIS;
2233 format->dwNumObjs = 0;
2234 IDirectInputDevice8_EnumObjects( iface, enum_objects_init, iface, DIDFT_ALL );
2236 if (TRACE_ON( dinput ))
2238 TRACE( "device format %s\n", debugstr_didataformat( format ) );
2239 for (i = 0; i < format->dwNumObjs; ++i) TRACE( " %lu: object %s\n", i, debugstr_diobjectdataformat( format->rgodf + i ) );
2242 return DI_OK;