dinput: Create object_properties for each mouse axis.
[wine.git] / dlls / dinput / device.c
blobd0a5fd0ea1cb1abcc9459f27310b398fa0b1e798
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 /* not supported on the mouse or keyboard */
1067 if (!(impl->caps.dwDevType & DIDEVTYPE_HID)) return DIERR_UNSUPPORTED;
1068 break;
1070 case (DWORD_PTR)DIPROP_FFLOAD:
1071 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1072 if (!is_exclusively_acquired( impl )) return DIERR_NOTEXCLUSIVEACQUIRED;
1073 /* fallthrough */
1074 case (DWORD_PTR)DIPROP_PRODUCTNAME:
1075 case (DWORD_PTR)DIPROP_INSTANCENAME:
1076 case (DWORD_PTR)DIPROP_VIDPID:
1077 case (DWORD_PTR)DIPROP_JOYSTICKID:
1078 case (DWORD_PTR)DIPROP_GUIDANDPATH:
1079 if (!impl->vtbl->get_property) return DIERR_UNSUPPORTED;
1080 break;
1084 return DI_OK;
1087 static BOOL CALLBACK find_object( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1089 *(DIDEVICEOBJECTINSTANCEW *)context = *instance;
1090 return DIENUM_STOP;
1093 struct get_object_property_params
1095 IDirectInputDevice8W *iface;
1096 DIPROPHEADER *header;
1097 DWORD property;
1100 static BOOL CALLBACK get_object_property( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1102 static const struct object_properties default_properties =
1104 .range_min = DIPROPRANGE_NOMIN,
1105 .range_max = DIPROPRANGE_NOMAX,
1107 struct get_object_property_params *params = context;
1108 struct dinput_device *impl = impl_from_IDirectInputDevice8W( params->iface );
1109 const struct object_properties *properties = NULL;
1111 if (!impl->object_properties) properties = &default_properties;
1112 else properties = impl->object_properties + instance->dwOfs / sizeof(LONG);
1114 switch (params->property)
1116 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1118 DIPROPRANGE *value = (DIPROPRANGE *)params->header;
1119 value->lMin = properties->physical_min;
1120 value->lMax = properties->physical_max;
1121 return DI_OK;
1123 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1125 DIPROPRANGE *value = (DIPROPRANGE *)params->header;
1126 value->lMin = properties->logical_min;
1127 value->lMax = properties->logical_max;
1128 return DI_OK;
1130 case (DWORD_PTR)DIPROP_RANGE:
1132 DIPROPRANGE *value = (DIPROPRANGE *)params->header;
1133 value->lMin = properties->range_min;
1134 value->lMax = properties->range_max;
1135 return DIENUM_STOP;
1137 case (DWORD_PTR)DIPROP_DEADZONE:
1139 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1140 value->dwData = properties->deadzone;
1141 return DIENUM_STOP;
1143 case (DWORD_PTR)DIPROP_SATURATION:
1145 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1146 value->dwData = properties->saturation;
1147 return DIENUM_STOP;
1149 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1151 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1152 value->dwData = properties->calibration_mode;
1153 return DI_OK;
1155 case (DWORD_PTR)DIPROP_GRANULARITY:
1157 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1158 value->dwData = 1;
1159 return DIENUM_STOP;
1161 case (DWORD_PTR)DIPROP_KEYNAME:
1163 DIPROPSTRING *value = (DIPROPSTRING *)params->header;
1164 lstrcpynW( value->wsz, instance->tszName, ARRAY_SIZE(value->wsz) );
1165 return DIENUM_STOP;
1169 return DIENUM_STOP;
1172 static HRESULT dinput_device_get_property( IDirectInputDevice8W *iface, const GUID *guid, DIPROPHEADER *header )
1174 struct get_object_property_params params = {.iface = iface, .header = header, .property = LOWORD( guid )};
1175 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1176 DWORD object_mask = DIDFT_AXIS | DIDFT_BUTTON | DIDFT_POV;
1177 DIPROPHEADER filter;
1178 HRESULT hr;
1180 filter = *header;
1181 if (FAILED(hr = enum_object_filter_init( impl, &filter ))) return hr;
1182 if (FAILED(hr = check_property( impl, guid, header, FALSE ))) return hr;
1184 switch (LOWORD( guid ))
1186 case (DWORD_PTR)DIPROP_PRODUCTNAME:
1187 case (DWORD_PTR)DIPROP_INSTANCENAME:
1188 case (DWORD_PTR)DIPROP_VIDPID:
1189 case (DWORD_PTR)DIPROP_JOYSTICKID:
1190 case (DWORD_PTR)DIPROP_GUIDANDPATH:
1191 case (DWORD_PTR)DIPROP_FFLOAD:
1192 return impl->vtbl->get_property( iface, LOWORD( guid ), header, NULL );
1194 case (DWORD_PTR)DIPROP_RANGE:
1195 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1196 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1197 case (DWORD_PTR)DIPROP_DEADZONE:
1198 case (DWORD_PTR)DIPROP_SATURATION:
1199 case (DWORD_PTR)DIPROP_GRANULARITY:
1200 case (DWORD_PTR)DIPROP_KEYNAME:
1201 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1202 hr = impl->vtbl->enum_objects( iface, &filter, object_mask, get_object_property, &params );
1203 if (FAILED(hr)) return hr;
1204 if (hr == DIENUM_CONTINUE) return DIERR_NOTFOUND;
1205 return DI_OK;
1207 case (DWORD_PTR)DIPROP_AUTOCENTER:
1209 DIPROPDWORD *value = (DIPROPDWORD *)header;
1210 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1211 value->dwData = impl->autocenter;
1212 return DI_OK;
1214 case (DWORD_PTR)DIPROP_BUFFERSIZE:
1216 DIPROPDWORD *value = (DIPROPDWORD *)header;
1217 value->dwData = impl->buffersize;
1218 return DI_OK;
1220 case (DWORD_PTR)DIPROP_USERNAME:
1222 DIPROPSTRING *value = (DIPROPSTRING *)header;
1223 struct DevicePlayer *device_player;
1224 LIST_FOR_EACH_ENTRY( device_player, &impl->dinput->device_players, struct DevicePlayer, entry )
1226 if (IsEqualGUID( &device_player->instance_guid, &impl->guid ))
1228 if (!*device_player->username) break;
1229 lstrcpynW( value->wsz, device_player->username, ARRAY_SIZE(value->wsz) );
1230 return DI_OK;
1233 return S_FALSE;
1235 case (DWORD_PTR)DIPROP_FFGAIN:
1237 DIPROPDWORD *value = (DIPROPDWORD *)header;
1238 value->dwData = impl->device_gain;
1239 return DI_OK;
1241 case (DWORD_PTR)DIPROP_CALIBRATION:
1242 return DIERR_INVALIDPARAM;
1243 default:
1244 FIXME( "Unknown property %s\n", debugstr_guid( guid ) );
1245 return DIERR_UNSUPPORTED;
1248 return DI_OK;
1251 static HRESULT WINAPI dinput_device_GetProperty( IDirectInputDevice8W *iface, const GUID *guid, DIPROPHEADER *header )
1253 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1254 HRESULT hr;
1256 TRACE( "iface %p, guid %s, header %p\n", iface, debugstr_guid( guid ), header );
1258 if (!header) return DIERR_INVALIDPARAM;
1259 if (header->dwHeaderSize != sizeof(DIPROPHEADER)) return DIERR_INVALIDPARAM;
1260 if (!IS_DIPROP( guid )) return DI_OK;
1262 EnterCriticalSection( &impl->crit );
1263 hr = dinput_device_get_property( iface, guid, header );
1264 LeaveCriticalSection( &impl->crit );
1266 return hr;
1269 struct set_object_property_params
1271 IDirectInputDevice8W *iface;
1272 const DIPROPHEADER *header;
1273 DWORD property;
1276 static BOOL CALLBACK set_object_property( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1278 struct set_object_property_params *params = context;
1279 struct dinput_device *impl = impl_from_IDirectInputDevice8W( params->iface );
1280 struct object_properties *properties = NULL;
1282 if (!impl->object_properties) return DIENUM_STOP;
1283 properties = impl->object_properties + instance->dwOfs / sizeof(LONG);
1285 switch (params->property)
1287 case (DWORD_PTR)DIPROP_RANGE:
1289 const DIPROPRANGE *value = (const DIPROPRANGE *)params->header;
1290 properties->range_min = value->lMin;
1291 properties->range_max = value->lMax;
1292 return DIENUM_CONTINUE;
1294 case (DWORD_PTR)DIPROP_DEADZONE:
1296 const DIPROPDWORD *value = (const DIPROPDWORD *)params->header;
1297 properties->deadzone = value->dwData;
1298 return DIENUM_CONTINUE;
1300 case (DWORD_PTR)DIPROP_SATURATION:
1302 const DIPROPDWORD *value = (const DIPROPDWORD *)params->header;
1303 properties->saturation = value->dwData;
1304 return DIENUM_CONTINUE;
1306 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1308 const DIPROPDWORD *value = (const DIPROPDWORD *)params->header;
1309 properties->calibration_mode = value->dwData;
1310 return DIENUM_CONTINUE;
1314 return DIENUM_STOP;
1317 static BOOL CALLBACK reset_object_value( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1319 struct dinput_device *impl = context;
1320 struct object_properties *properties;
1321 LONG tmp = -1;
1323 if (!impl->object_properties) return DIENUM_STOP;
1324 properties = impl->object_properties + instance->dwOfs / sizeof(LONG);
1326 if (instance->dwType & DIDFT_AXIS)
1328 if (!properties->range_min) tmp = properties->range_max / 2;
1329 else tmp = round( (properties->range_min + properties->range_max) / 2.0 );
1332 *(LONG *)(impl->device_state + instance->dwOfs) = tmp;
1333 return DIENUM_CONTINUE;
1336 static void reset_device_state( IDirectInputDevice8W *iface )
1338 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1339 DIPROPHEADER filter =
1341 .dwHeaderSize = sizeof(DIPROPHEADER),
1342 .dwSize = sizeof(DIPROPHEADER),
1343 .dwHow = DIPH_DEVICE,
1344 .dwObj = 0,
1347 impl->vtbl->enum_objects( iface, &filter, DIDFT_AXIS | DIDFT_POV, reset_object_value, impl );
1350 static HRESULT dinput_device_set_property( IDirectInputDevice8W *iface, const GUID *guid,
1351 const DIPROPHEADER *header )
1353 struct set_object_property_params params = {.iface = iface, .header = header, .property = LOWORD( guid )};
1354 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1355 DWORD object_mask = DIDFT_AXIS | DIDFT_BUTTON | DIDFT_POV;
1356 DIDEVICEOBJECTINSTANCEW instance;
1357 DIPROPHEADER filter;
1358 HRESULT hr;
1360 filter = *header;
1361 if (FAILED(hr = enum_object_filter_init( impl, &filter ))) return hr;
1362 if (FAILED(hr = check_property( impl, guid, header, TRUE ))) return hr;
1364 switch (LOWORD( guid ))
1366 case (DWORD_PTR)DIPROP_RANGE:
1367 case (DWORD_PTR)DIPROP_DEADZONE:
1368 case (DWORD_PTR)DIPROP_SATURATION:
1370 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_AXIS, set_object_property, &params );
1371 if (FAILED(hr)) return hr;
1372 reset_device_state( iface );
1373 return DI_OK;
1375 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1377 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1378 if (value->dwData > DIPROPCALIBRATIONMODE_RAW) return DIERR_INVALIDPARAM;
1379 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_AXIS, set_object_property, &params );
1380 if (FAILED(hr)) return hr;
1381 reset_device_state( iface );
1382 return DI_OK;
1384 case (DWORD_PTR)DIPROP_AUTOCENTER:
1386 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1387 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1389 FIXME( "DIPROP_AUTOCENTER stub!\n" );
1390 impl->autocenter = value->dwData;
1391 return DI_OK;
1393 case (DWORD_PTR)DIPROP_FFGAIN:
1395 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1396 impl->device_gain = value->dwData;
1397 if (!is_exclusively_acquired( impl )) return DI_OK;
1398 if (!impl->vtbl->send_device_gain) return DI_OK;
1399 return impl->vtbl->send_device_gain( iface, impl->device_gain );
1401 case (DWORD_PTR)DIPROP_AXISMODE:
1403 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1405 TRACE( "Axis mode: %s\n", value->dwData == DIPROPAXISMODE_ABS ? "absolute" : "relative" );
1407 impl->user_format.dwFlags &= ~DIDFT_AXIS;
1408 impl->user_format.dwFlags |= value->dwData == DIPROPAXISMODE_ABS ? DIDF_ABSAXIS : DIDF_RELAXIS;
1410 return DI_OK;
1412 case (DWORD_PTR)DIPROP_BUFFERSIZE:
1414 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1416 TRACE( "buffersize %lu\n", value->dwData );
1418 impl->buffersize = value->dwData;
1419 impl->queue_len = min( impl->buffersize, 1024 );
1420 free( impl->data_queue );
1422 impl->data_queue = impl->queue_len ? malloc( impl->queue_len * sizeof(DIDEVICEOBJECTDATA) ) : NULL;
1423 impl->queue_head = impl->queue_tail = impl->overflow = 0;
1424 return DI_OK;
1426 case (DWORD_PTR)DIPROP_APPDATA:
1428 const DIPROPPOINTER *value = (const DIPROPPOINTER *)header;
1429 int user_offset;
1430 hr = impl->vtbl->enum_objects( iface, &filter, object_mask, find_object, &instance );
1431 if (FAILED(hr)) return hr;
1432 if (hr == DIENUM_CONTINUE) return DIERR_OBJECTNOTFOUND;
1433 if ((user_offset = id_to_offset( impl, instance.dwType )) < 0) return DIERR_OBJECTNOTFOUND;
1434 if (!set_app_data( impl, user_offset, value->uData )) return DIERR_OUTOFMEMORY;
1435 return DI_OK;
1437 default:
1438 FIXME( "Unknown property %s\n", debugstr_guid( guid ) );
1439 return DIERR_UNSUPPORTED;
1442 return DI_OK;
1445 static HRESULT WINAPI dinput_device_SetProperty( IDirectInputDevice8W *iface, const GUID *guid,
1446 const DIPROPHEADER *header )
1448 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1449 HRESULT hr;
1451 TRACE( "iface %p, guid %s, header %p\n", iface, debugstr_guid( guid ), header );
1453 if (!header) return DIERR_INVALIDPARAM;
1454 if (header->dwHeaderSize != sizeof(DIPROPHEADER)) return DIERR_INVALIDPARAM;
1455 if (!IS_DIPROP( guid )) return DI_OK;
1457 EnterCriticalSection( &impl->crit );
1458 hr = dinput_device_set_property( iface, guid, header );
1459 LeaveCriticalSection( &impl->crit );
1461 return hr;
1464 static void dinput_device_set_username( struct dinput_device *impl, const DIPROPSTRING *value )
1466 struct DevicePlayer *device_player;
1467 BOOL found = FALSE;
1469 LIST_FOR_EACH_ENTRY( device_player, &impl->dinput->device_players, struct DevicePlayer, entry )
1471 if (IsEqualGUID( &device_player->instance_guid, &impl->guid ))
1473 found = TRUE;
1474 break;
1477 if (!found && (device_player = malloc( sizeof(struct DevicePlayer) )))
1479 list_add_tail( &impl->dinput->device_players, &device_player->entry );
1480 device_player->instance_guid = impl->guid;
1482 if (device_player)
1483 lstrcpynW( device_player->username, value->wsz, ARRAY_SIZE(device_player->username) );
1486 static BOOL CALLBACK get_object_info( const DIDEVICEOBJECTINSTANCEW *instance, void *data )
1488 DIDEVICEOBJECTINSTANCEW *dest = data;
1489 DWORD size = dest->dwSize;
1491 memcpy( dest, instance, size );
1492 dest->dwSize = size;
1494 return DIENUM_STOP;
1497 static HRESULT WINAPI dinput_device_GetObjectInfo( IDirectInputDevice8W *iface,
1498 DIDEVICEOBJECTINSTANCEW *instance, DWORD obj, DWORD how )
1500 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1501 DIPROPHEADER filter =
1503 .dwSize = sizeof(filter),
1504 .dwHeaderSize = sizeof(filter),
1505 .dwHow = how,
1506 .dwObj = obj
1508 HRESULT hr;
1510 TRACE( "iface %p, instance %p, obj %#lx, how %#lx.\n", iface, instance, obj, how );
1512 if (!instance) return E_POINTER;
1513 if (instance->dwSize != sizeof(DIDEVICEOBJECTINSTANCE_DX3W) && instance->dwSize != sizeof(DIDEVICEOBJECTINSTANCEW))
1514 return DIERR_INVALIDPARAM;
1515 if (how == DIPH_DEVICE) return DIERR_INVALIDPARAM;
1516 if (FAILED(hr = enum_object_filter_init( impl, &filter ))) return hr;
1518 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_ALL, get_object_info, instance );
1519 if (FAILED(hr)) return hr;
1520 if (hr == DIENUM_CONTINUE) return DIERR_NOTFOUND;
1521 return DI_OK;
1524 static HRESULT WINAPI dinput_device_GetDeviceState( IDirectInputDevice8W *iface, DWORD size, void *data )
1526 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1527 DIDATAFORMAT *device_format = &impl->device_format, *user_format = &impl->user_format;
1528 DIOBJECTDATAFORMAT *device_obj, *user_obj;
1529 BYTE *user_state = data;
1530 DIPROPHEADER filter =
1532 .dwSize = sizeof(filter),
1533 .dwHeaderSize = sizeof(filter),
1534 .dwHow = DIPH_DEVICE,
1535 .dwObj = 0,
1537 HRESULT hr;
1539 TRACE( "iface %p, size %lu, data %p.\n", iface, size, data );
1541 if (!data) return DIERR_INVALIDPARAM;
1543 IDirectInputDevice2_Poll( iface );
1545 EnterCriticalSection( &impl->crit );
1546 if (impl->status == STATUS_UNPLUGGED)
1547 hr = DIERR_INPUTLOST;
1548 else if (impl->status != STATUS_ACQUIRED)
1549 hr = DIERR_NOTACQUIRED;
1550 else if (!user_format->rgodf)
1551 hr = DIERR_INVALIDPARAM;
1552 else if (size != user_format->dwDataSize)
1553 hr = DIERR_INVALIDPARAM;
1554 else
1556 memset( user_state, 0, size );
1558 user_obj = user_format->rgodf + device_format->dwNumObjs;
1559 device_obj = device_format->rgodf + device_format->dwNumObjs;
1560 while (user_obj-- > user_format->rgodf && device_obj-- > device_format->rgodf)
1562 if (user_obj->dwType & DIDFT_BUTTON)
1563 user_state[user_obj->dwOfs] = impl->device_state[device_obj->dwOfs];
1566 /* reset optional POVs to their default */
1567 user_obj = user_format->rgodf + user_format->dwNumObjs;
1568 while (user_obj-- > user_format->rgodf + device_format->dwNumObjs)
1569 if (user_obj->dwType & DIDFT_POV) *(ULONG *)(user_state + user_obj->dwOfs) = 0xffffffff;
1571 user_obj = user_format->rgodf + device_format->dwNumObjs;
1572 device_obj = device_format->rgodf + device_format->dwNumObjs;
1573 while (user_obj-- > user_format->rgodf && device_obj-- > device_format->rgodf)
1575 if (user_obj->dwType & (DIDFT_POV | DIDFT_AXIS))
1576 *(ULONG *)(user_state + user_obj->dwOfs) = *(ULONG *)(impl->device_state + device_obj->dwOfs);
1577 if (!(user_format->dwFlags & DIDF_ABSAXIS) && (device_obj->dwType & DIDFT_RELAXIS))
1578 *(ULONG *)(impl->device_state + device_obj->dwOfs) = 0;
1581 hr = DI_OK;
1583 LeaveCriticalSection( &impl->crit );
1585 return hr;
1588 static HRESULT WINAPI dinput_device_GetDeviceData( IDirectInputDevice8W *iface, DWORD size, DIDEVICEOBJECTDATA *data,
1589 DWORD *count, DWORD flags )
1591 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
1592 HRESULT ret = DI_OK;
1593 int len;
1595 TRACE( "iface %p, size %lu, data %p, count %p, flags %#lx.\n", iface, size, data, count, flags );
1597 if (This->dinput->dwVersion == 0x0800 || size == sizeof(DIDEVICEOBJECTDATA_DX3))
1599 if (!This->queue_len) return DIERR_NOTBUFFERED;
1600 if (This->status == STATUS_UNPLUGGED) return DIERR_INPUTLOST;
1601 if (This->status != STATUS_ACQUIRED) return DIERR_NOTACQUIRED;
1604 if (!This->queue_len)
1605 return DI_OK;
1606 if (size < sizeof(DIDEVICEOBJECTDATA_DX3)) return DIERR_INVALIDPARAM;
1608 IDirectInputDevice2_Poll(iface);
1609 EnterCriticalSection(&This->crit);
1611 len = This->queue_head - This->queue_tail;
1612 if (len < 0) len += This->queue_len;
1614 if ((*count != INFINITE) && (len > *count)) len = *count;
1616 if (data)
1618 int i;
1619 for (i = 0; i < len; i++)
1621 int n = (This->queue_tail + i) % This->queue_len;
1622 memcpy( (char *)data + size * i, This->data_queue + n, size );
1625 *count = len;
1627 if (This->overflow && This->dinput->dwVersion == 0x0800)
1628 ret = DI_BUFFEROVERFLOW;
1630 if (!(flags & DIGDD_PEEK))
1632 /* Advance reading position */
1633 This->queue_tail = (This->queue_tail + len) % This->queue_len;
1634 This->overflow = FALSE;
1637 LeaveCriticalSection(&This->crit);
1639 TRACE( "Returning %lu events queued\n", *count );
1640 return ret;
1643 static HRESULT WINAPI dinput_device_RunControlPanel( IDirectInputDevice8W *iface, HWND hwnd, DWORD flags )
1645 FIXME( "iface %p, hwnd %p, flags %#lx stub!\n", iface, hwnd, flags );
1646 return DI_OK;
1649 static HRESULT WINAPI dinput_device_Initialize( IDirectInputDevice8W *iface, HINSTANCE instance,
1650 DWORD version, const GUID *guid )
1652 FIXME( "iface %p, instance %p, version %#lx, guid %s stub!\n", iface, instance, version,
1653 debugstr_guid( guid ) );
1654 return DI_OK;
1657 static HRESULT WINAPI dinput_device_CreateEffect( IDirectInputDevice8W *iface, const GUID *guid,
1658 const DIEFFECT *params, IDirectInputEffect **out,
1659 IUnknown *outer )
1661 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1662 DWORD flags;
1663 HRESULT hr;
1665 TRACE( "iface %p, guid %s, params %p, out %p, outer %p\n", iface, debugstr_guid( guid ),
1666 params, out, outer );
1668 if (!out) return E_POINTER;
1669 *out = NULL;
1671 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1672 if (!impl->vtbl->create_effect) return DIERR_UNSUPPORTED;
1673 if (FAILED(hr = impl->vtbl->create_effect( iface, out ))) return hr;
1675 hr = IDirectInputEffect_Initialize( *out, DINPUT_instance, impl->dinput->dwVersion, guid );
1676 if (FAILED(hr)) goto failed;
1677 if (!params) return DI_OK;
1679 flags = params->dwSize == sizeof(DIEFFECT_DX6) ? DIEP_ALLPARAMS : DIEP_ALLPARAMS_DX5;
1680 if (!is_exclusively_acquired( impl )) flags |= DIEP_NODOWNLOAD;
1681 hr = IDirectInputEffect_SetParameters( *out, params, flags );
1682 if (FAILED(hr)) goto failed;
1683 return DI_OK;
1685 failed:
1686 IDirectInputEffect_Release( *out );
1687 *out = NULL;
1688 return hr;
1691 static HRESULT WINAPI dinput_device_EnumEffects( IDirectInputDevice8W *iface, LPDIENUMEFFECTSCALLBACKW callback,
1692 void *context, DWORD type )
1694 DIEFFECTINFOW info = {.dwSize = sizeof(info)};
1695 HRESULT hr;
1697 TRACE( "iface %p, callback %p, context %p, type %#lx.\n", iface, callback, context, type );
1699 if (!callback) return DIERR_INVALIDPARAM;
1701 type = DIEFT_GETTYPE( type );
1703 if (type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1705 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_ConstantForce );
1706 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1707 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1710 if (type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1712 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_RampForce );
1713 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1714 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1717 if (type == DIEFT_ALL || type == DIEFT_PERIODIC)
1719 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Square );
1720 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1721 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1723 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Sine );
1724 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1725 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1727 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Triangle );
1728 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1729 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1731 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_SawtoothUp );
1732 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1733 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1735 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_SawtoothDown );
1736 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1737 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1740 if (type == DIEFT_ALL || type == DIEFT_CONDITION)
1742 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Spring );
1743 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1744 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1746 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Damper );
1747 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1748 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1750 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Inertia );
1751 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1752 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1754 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Friction );
1755 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1756 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1759 return DI_OK;
1762 static HRESULT WINAPI dinput_device_GetEffectInfo( IDirectInputDevice8W *iface, DIEFFECTINFOW *info,
1763 const GUID *guid )
1765 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1767 TRACE( "iface %p, info %p, guid %s.\n", iface, info, debugstr_guid( guid ) );
1769 if (!info) return E_POINTER;
1770 if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
1771 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_DEVICENOTREG;
1772 if (!impl->vtbl->get_effect_info) return DIERR_UNSUPPORTED;
1773 return impl->vtbl->get_effect_info( iface, info, guid );
1776 static HRESULT WINAPI dinput_device_GetForceFeedbackState( IDirectInputDevice8W *iface, DWORD *out )
1778 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1779 HRESULT hr = DI_OK;
1781 TRACE( "iface %p, out %p.\n", iface, out );
1783 if (!out) return E_POINTER;
1784 *out = 0;
1786 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1788 EnterCriticalSection( &impl->crit );
1789 if (!is_exclusively_acquired( impl )) hr = DIERR_NOTEXCLUSIVEACQUIRED;
1790 else *out = impl->force_feedback_state;
1791 LeaveCriticalSection( &impl->crit );
1793 return hr;
1796 static HRESULT WINAPI dinput_device_SendForceFeedbackCommand( IDirectInputDevice8W *iface, DWORD command )
1798 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1799 HRESULT hr;
1801 TRACE( "iface %p, command %#lx.\n", iface, command );
1803 switch (command)
1805 case DISFFC_RESET: break;
1806 case DISFFC_STOPALL: break;
1807 case DISFFC_PAUSE: break;
1808 case DISFFC_CONTINUE: break;
1809 case DISFFC_SETACTUATORSON: break;
1810 case DISFFC_SETACTUATORSOFF: break;
1811 default: return DIERR_INVALIDPARAM;
1814 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1815 if (!impl->vtbl->send_force_feedback_command) return DIERR_UNSUPPORTED;
1817 EnterCriticalSection( &impl->crit );
1818 if (!is_exclusively_acquired( impl )) hr = DIERR_NOTEXCLUSIVEACQUIRED;
1819 else hr = impl->vtbl->send_force_feedback_command( iface, command, FALSE );
1820 LeaveCriticalSection( &impl->crit );
1822 return hr;
1825 static HRESULT WINAPI dinput_device_EnumCreatedEffectObjects( IDirectInputDevice8W *iface,
1826 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK callback,
1827 void *context, DWORD flags )
1829 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1831 TRACE( "iface %p, callback %p, context %p, flags %#lx.\n", iface, callback, context, flags );
1833 if (!callback) return DIERR_INVALIDPARAM;
1834 if (flags) return DIERR_INVALIDPARAM;
1835 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DI_OK;
1836 if (!impl->vtbl->enum_created_effect_objects) return DIERR_UNSUPPORTED;
1838 return impl->vtbl->enum_created_effect_objects( iface, callback, context, flags );
1841 static HRESULT WINAPI dinput_device_Escape( IDirectInputDevice8W *iface, DIEFFESCAPE *escape )
1843 FIXME( "iface %p, escape %p stub!\n", iface, escape );
1844 return DI_OK;
1847 static HRESULT WINAPI dinput_device_Poll( IDirectInputDevice8W *iface )
1849 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1850 HRESULT hr = DI_NOEFFECT;
1852 EnterCriticalSection( &impl->crit );
1853 if (impl->status == STATUS_UNPLUGGED) hr = DIERR_INPUTLOST;
1854 else if (impl->status != STATUS_ACQUIRED) hr = DIERR_NOTACQUIRED;
1855 LeaveCriticalSection( &impl->crit );
1856 if (FAILED(hr)) return hr;
1858 if (impl->vtbl->poll) return impl->vtbl->poll( iface );
1859 return hr;
1862 static HRESULT WINAPI dinput_device_SendDeviceData( IDirectInputDevice8W *iface, DWORD size,
1863 const DIDEVICEOBJECTDATA *data, DWORD *count, DWORD flags )
1865 FIXME( "iface %p, size %lu, data %p, count %p, flags %#lx stub!\n", iface, size, data, count, flags );
1866 return DI_OK;
1869 static HRESULT WINAPI dinput_device_EnumEffectsInFile( IDirectInputDevice8W *iface, const WCHAR *filename,
1870 LPDIENUMEFFECTSINFILECALLBACK callback,
1871 void *context, DWORD flags )
1873 FIXME( "iface %p, filename %s, callback %p, context %p, flags %#lx stub!\n", iface,
1874 debugstr_w(filename), callback, context, flags );
1875 return DI_OK;
1878 static HRESULT WINAPI dinput_device_WriteEffectToFile( IDirectInputDevice8W *iface, const WCHAR *filename,
1879 DWORD count, DIFILEEFFECT *effects, DWORD flags )
1881 FIXME( "iface %p, filename %s, count %lu, effects %p, flags %#lx stub!\n", iface,
1882 debugstr_w(filename), count, effects, flags );
1883 return DI_OK;
1886 static HRESULT WINAPI dinput_device_BuildActionMap( IDirectInputDevice8W *iface, DIACTIONFORMATW *format,
1887 const WCHAR *username, DWORD flags )
1889 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1890 BOOL load_success = FALSE, has_actions = FALSE;
1891 DWORD genre, username_len = MAX_PATH;
1892 WCHAR username_buf[MAX_PATH];
1893 const DIDATAFORMAT *df;
1894 DWORD devMask;
1895 int i;
1897 FIXME( "iface %p, format %p, username %s, flags %#lx stub!\n", iface, format,
1898 debugstr_w(username), flags );
1900 if (!format) return DIERR_INVALIDPARAM;
1902 switch (GET_DIDEVICE_TYPE( impl->instance.dwDevType ))
1904 case DIDEVTYPE_KEYBOARD:
1905 case DI8DEVTYPE_KEYBOARD:
1906 devMask = DIKEYBOARD_MASK;
1907 df = &c_dfDIKeyboard;
1908 break;
1909 case DIDEVTYPE_MOUSE:
1910 case DI8DEVTYPE_MOUSE:
1911 devMask = DIMOUSE_MASK;
1912 df = &c_dfDIMouse2;
1913 break;
1914 default:
1915 devMask = DIGENRE_ANY;
1916 df = &impl->device_format;
1917 break;
1920 /* Unless asked the contrary by these flags, try to load a previous mapping */
1921 if (!(flags & DIDBAM_HWDEFAULTS))
1923 /* Retrieve logged user name if necessary */
1924 if (username == NULL) GetUserNameW( username_buf, &username_len );
1925 else lstrcpynW( username_buf, username, MAX_PATH );
1926 load_success = load_mapping_settings( impl, format, username_buf );
1929 if (load_success) return DI_OK;
1931 for (i = 0; i < format->dwNumActions; i++)
1933 /* Don't touch a user configured action */
1934 if (format->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
1936 genre = format->rgoAction[i].dwSemantic & DIGENRE_ANY;
1937 if (devMask == genre || (devMask == DIGENRE_ANY && genre != DIMOUSE_MASK && genre != DIKEYBOARD_MASK))
1939 DWORD obj_id = semantic_to_obj_id( impl, format->rgoAction[i].dwSemantic );
1940 DWORD type = DIDFT_GETTYPE( obj_id );
1941 DWORD inst = DIDFT_GETINSTANCE( obj_id );
1943 LPDIOBJECTDATAFORMAT odf;
1945 if (type == DIDFT_PSHBUTTON) type = DIDFT_BUTTON;
1946 if (type == DIDFT_RELAXIS) type = DIDFT_AXIS;
1948 /* Make sure the object exists */
1949 odf = dataformat_to_odf_by_type( df, inst, type );
1951 if (odf != NULL)
1953 format->rgoAction[i].dwObjID = obj_id;
1954 format->rgoAction[i].guidInstance = impl->guid;
1955 format->rgoAction[i].dwHow = DIAH_DEFAULT;
1956 has_actions = TRUE;
1959 else if (!(flags & DIDBAM_PRESERVE))
1961 /* We must clear action data belonging to other devices */
1962 memset( &format->rgoAction[i].guidInstance, 0, sizeof(GUID) );
1963 format->rgoAction[i].dwHow = DIAH_UNMAPPED;
1967 if (!has_actions) return DI_NOEFFECT;
1968 if (flags & (DIDBAM_DEFAULT|DIDBAM_PRESERVE|DIDBAM_INITIALIZE|DIDBAM_HWDEFAULTS))
1969 FIXME( "Unimplemented flags %#lx\n", flags );
1970 return DI_OK;
1973 static HRESULT WINAPI dinput_device_SetActionMap( IDirectInputDevice8W *iface, DIACTIONFORMATW *format,
1974 const WCHAR *username, DWORD flags )
1976 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1977 DIDATAFORMAT data_format;
1978 DIOBJECTDATAFORMAT *obj_df = NULL;
1979 DIPROPDWORD dp;
1980 DIPROPRANGE dpr;
1981 DIPROPSTRING dps;
1982 WCHAR username_buf[MAX_PATH];
1983 DWORD username_len = MAX_PATH;
1984 int i, action = 0, num_actions = 0;
1985 unsigned int offset = 0;
1986 const DIDATAFORMAT *df;
1987 ActionMap *action_map;
1989 FIXME( "iface %p, format %p, username %s, flags %#lx stub!\n", iface, format,
1990 debugstr_w(username), flags );
1992 if (!format) return DIERR_INVALIDPARAM;
1994 switch (GET_DIDEVICE_TYPE( impl->instance.dwDevType ))
1996 case DIDEVTYPE_KEYBOARD:
1997 case DI8DEVTYPE_KEYBOARD:
1998 df = &c_dfDIKeyboard;
1999 break;
2000 case DIDEVTYPE_MOUSE:
2001 case DI8DEVTYPE_MOUSE:
2002 df = &c_dfDIMouse2;
2003 break;
2004 default:
2005 df = &impl->device_format;
2006 break;
2009 if (impl->status == STATUS_ACQUIRED) return DIERR_ACQUIRED;
2011 data_format.dwSize = sizeof(data_format);
2012 data_format.dwObjSize = sizeof(DIOBJECTDATAFORMAT);
2013 data_format.dwFlags = DIDF_RELAXIS;
2014 data_format.dwDataSize = format->dwDataSize;
2016 /* Count the actions */
2017 for (i = 0; i < format->dwNumActions; i++)
2018 if (IsEqualGUID( &impl->guid, &format->rgoAction[i].guidInstance ))
2019 num_actions++;
2021 if (num_actions == 0) return DI_NOEFFECT;
2023 /* Construct the dataformat and actionmap */
2024 obj_df = malloc( sizeof(DIOBJECTDATAFORMAT) * num_actions );
2025 data_format.rgodf = (LPDIOBJECTDATAFORMAT)obj_df;
2026 data_format.dwNumObjs = num_actions;
2028 action_map = malloc( sizeof(ActionMap) * num_actions );
2030 for (i = 0; i < format->dwNumActions; i++)
2032 if (IsEqualGUID( &impl->guid, &format->rgoAction[i].guidInstance ))
2034 DWORD inst = DIDFT_GETINSTANCE( format->rgoAction[i].dwObjID );
2035 DWORD type = DIDFT_GETTYPE( format->rgoAction[i].dwObjID );
2036 LPDIOBJECTDATAFORMAT obj;
2038 if (type == DIDFT_PSHBUTTON) type = DIDFT_BUTTON;
2039 if (type == DIDFT_RELAXIS) type = DIDFT_AXIS;
2041 obj = dataformat_to_odf_by_type( df, inst, type );
2043 memcpy( &obj_df[action], obj, df->dwObjSize );
2045 action_map[action].uAppData = format->rgoAction[i].uAppData;
2046 action_map[action].offset = offset;
2047 obj_df[action].dwOfs = offset;
2048 offset += (type & DIDFT_BUTTON) ? 1 : 4;
2050 action++;
2054 IDirectInputDevice8_SetDataFormat( iface, &data_format );
2056 impl->action_map = action_map;
2057 impl->num_actions = num_actions;
2059 free( obj_df );
2061 /* Set the device properties according to the action format */
2062 dpr.diph.dwSize = sizeof(DIPROPRANGE);
2063 dpr.lMin = format->lAxisMin;
2064 dpr.lMax = format->lAxisMax;
2065 dpr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
2066 dpr.diph.dwObj = 0;
2067 dpr.diph.dwHow = DIPH_DEVICE;
2068 IDirectInputDevice8_SetProperty( iface, DIPROP_RANGE, &dpr.diph );
2070 if (format->dwBufferSize > 0)
2072 dp.diph.dwSize = sizeof(DIPROPDWORD);
2073 dp.dwData = format->dwBufferSize;
2074 dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
2075 dp.diph.dwObj = 0;
2076 dp.diph.dwHow = DIPH_DEVICE;
2077 IDirectInputDevice8_SetProperty( iface, DIPROP_BUFFERSIZE, &dp.diph );
2080 /* Retrieve logged user name if necessary */
2081 if (username == NULL) GetUserNameW( username_buf, &username_len );
2082 else lstrcpynW( username_buf, username, MAX_PATH );
2084 dps.diph.dwSize = sizeof(dps);
2085 dps.diph.dwHeaderSize = sizeof(DIPROPHEADER);
2086 dps.diph.dwObj = 0;
2087 dps.diph.dwHow = DIPH_DEVICE;
2088 if (flags & DIDSAM_NOUSER) dps.wsz[0] = '\0';
2089 else lstrcpynW( dps.wsz, username_buf, ARRAY_SIZE(dps.wsz) );
2090 dinput_device_set_username( impl, &dps );
2092 /* Save the settings to disk */
2093 save_mapping_settings( iface, format, username_buf );
2095 return DI_OK;
2098 static HRESULT WINAPI dinput_device_GetImageInfo( IDirectInputDevice8W *iface, DIDEVICEIMAGEINFOHEADERW *header )
2100 FIXME( "iface %p, header %p stub!\n", iface, header );
2101 return DI_OK;
2104 extern const IDirectInputDevice8AVtbl dinput_device_a_vtbl;
2105 static const IDirectInputDevice8WVtbl dinput_device_w_vtbl =
2107 /*** IUnknown methods ***/
2108 dinput_device_QueryInterface,
2109 dinput_device_AddRef,
2110 dinput_device_Release,
2111 /*** IDirectInputDevice methods ***/
2112 dinput_device_GetCapabilities,
2113 dinput_device_EnumObjects,
2114 dinput_device_GetProperty,
2115 dinput_device_SetProperty,
2116 dinput_device_Acquire,
2117 dinput_device_Unacquire,
2118 dinput_device_GetDeviceState,
2119 dinput_device_GetDeviceData,
2120 dinput_device_SetDataFormat,
2121 dinput_device_SetEventNotification,
2122 dinput_device_SetCooperativeLevel,
2123 dinput_device_GetObjectInfo,
2124 dinput_device_GetDeviceInfo,
2125 dinput_device_RunControlPanel,
2126 dinput_device_Initialize,
2127 /*** IDirectInputDevice2 methods ***/
2128 dinput_device_CreateEffect,
2129 dinput_device_EnumEffects,
2130 dinput_device_GetEffectInfo,
2131 dinput_device_GetForceFeedbackState,
2132 dinput_device_SendForceFeedbackCommand,
2133 dinput_device_EnumCreatedEffectObjects,
2134 dinput_device_Escape,
2135 dinput_device_Poll,
2136 dinput_device_SendDeviceData,
2137 /*** IDirectInputDevice7 methods ***/
2138 dinput_device_EnumEffectsInFile,
2139 dinput_device_WriteEffectToFile,
2140 /*** IDirectInputDevice8 methods ***/
2141 dinput_device_BuildActionMap,
2142 dinput_device_SetActionMap,
2143 dinput_device_GetImageInfo,
2146 void dinput_device_init( struct dinput_device *device, const struct dinput_device_vtbl *vtbl,
2147 const GUID *guid, struct dinput *dinput )
2149 device->IDirectInputDevice8A_iface.lpVtbl = &dinput_device_a_vtbl;
2150 device->IDirectInputDevice8W_iface.lpVtbl = &dinput_device_w_vtbl;
2151 device->ref = 1;
2152 device->guid = *guid;
2153 device->instance.dwSize = sizeof(DIDEVICEINSTANCEW);
2154 device->caps.dwSize = sizeof(DIDEVCAPS);
2155 device->caps.dwFlags = DIDC_ATTACHED | DIDC_EMULATED;
2156 device->device_gain = 10000;
2157 device->force_feedback_state = DIGFFS_STOPPED | DIGFFS_EMPTY;
2158 InitializeCriticalSection( &device->crit );
2159 device->dinput = dinput;
2160 IDirectInput_AddRef( &dinput->IDirectInput7A_iface );
2161 device->vtbl = vtbl;
2164 static const GUID *object_instance_guid( const DIDEVICEOBJECTINSTANCEW *instance )
2166 if (IsEqualGUID( &instance->guidType, &GUID_XAxis )) return &GUID_XAxis;
2167 if (IsEqualGUID( &instance->guidType, &GUID_YAxis )) return &GUID_YAxis;
2168 if (IsEqualGUID( &instance->guidType, &GUID_ZAxis )) return &GUID_ZAxis;
2169 if (IsEqualGUID( &instance->guidType, &GUID_RxAxis )) return &GUID_RxAxis;
2170 if (IsEqualGUID( &instance->guidType, &GUID_RyAxis )) return &GUID_RyAxis;
2171 if (IsEqualGUID( &instance->guidType, &GUID_RzAxis )) return &GUID_RzAxis;
2172 if (IsEqualGUID( &instance->guidType, &GUID_Slider )) return &GUID_Slider;
2173 if (IsEqualGUID( &instance->guidType, &GUID_Button )) return &GUID_Button;
2174 if (IsEqualGUID( &instance->guidType, &GUID_Key )) return &GUID_Key;
2175 if (IsEqualGUID( &instance->guidType, &GUID_POV )) return &GUID_POV;
2176 return &GUID_Unknown;
2179 static BOOL CALLBACK enum_objects_init( const DIDEVICEOBJECTINSTANCEW *instance, void *data )
2181 struct dinput_device *impl = impl_from_IDirectInputDevice8W( data );
2182 DIDATAFORMAT *format = &impl->device_format;
2183 DIOBJECTDATAFORMAT *obj_format;
2185 if (!format->rgodf)
2187 format->dwDataSize = max( format->dwDataSize, instance->dwOfs + sizeof(LONG) );
2188 if (instance->dwType & DIDFT_BUTTON) impl->caps.dwButtons++;
2189 if (instance->dwType & DIDFT_AXIS) impl->caps.dwAxes++;
2190 if (instance->dwType & DIDFT_POV) impl->caps.dwPOVs++;
2191 if (instance->dwType & (DIDFT_BUTTON|DIDFT_AXIS|DIDFT_POV))
2193 if (!impl->device_state_report_id)
2194 impl->device_state_report_id = instance->wReportId;
2195 else if (impl->device_state_report_id != instance->wReportId)
2196 FIXME( "multiple device state reports found!\n" );
2199 else
2201 obj_format = format->rgodf + format->dwNumObjs;
2202 obj_format->pguid = object_instance_guid( instance );
2203 obj_format->dwOfs = instance->dwOfs;
2204 obj_format->dwType = instance->dwType;
2205 obj_format->dwFlags = instance->dwFlags;
2208 if (impl->object_properties && (instance->dwType & (DIDFT_AXIS | DIDFT_POV)))
2209 reset_object_value( instance, impl );
2211 format->dwNumObjs++;
2212 return DIENUM_CONTINUE;
2215 HRESULT dinput_device_init_device_format( IDirectInputDevice8W *iface )
2217 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
2218 DIDATAFORMAT *format = &impl->device_format;
2219 ULONG i, size;
2221 IDirectInputDevice8_EnumObjects( iface, enum_objects_init, iface, DIDFT_ALL );
2222 if (format->dwDataSize > DEVICE_STATE_MAX_SIZE)
2224 FIXME( "unable to create device, state is too large\n" );
2225 return DIERR_OUTOFMEMORY;
2228 size = format->dwNumObjs * sizeof(*format->rgodf);
2229 if (!(format->rgodf = calloc( 1, size ))) return DIERR_OUTOFMEMORY;
2231 format->dwSize = sizeof(*format);
2232 format->dwObjSize = sizeof(*format->rgodf);
2233 format->dwFlags = DIDF_ABSAXIS;
2234 format->dwNumObjs = 0;
2235 IDirectInputDevice8_EnumObjects( iface, enum_objects_init, iface, DIDFT_ALL );
2237 if (TRACE_ON( dinput ))
2239 TRACE( "device format %s\n", debugstr_didataformat( format ) );
2240 for (i = 0; i < format->dwNumObjs; ++i) TRACE( " %lu: object %s\n", i, debugstr_diobjectdataformat( format->rgodf + i ) );
2243 return DI_OK;