dinput: Use an enumeration instead of BOOL for acquired status.
[wine.git] / dlls / dinput / device.c
blob47cf4d72dc32451f60acee7116017dc4284a0c64
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 %u, dwObjsize %u, dwFlags %#x, dwDataSize %u, dwNumObjs %u, 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 %#x, dwType %#x, dwFlags %#x", 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 if (impl->user_format) free( impl->user_format->rgodf );
214 free( impl->user_format );
215 impl->user_format = NULL;
218 static inline LPDIOBJECTDATAFORMAT dataformat_to_odf(LPCDIDATAFORMAT df, int idx)
220 if (idx < 0 || idx >= df->dwNumObjs) return NULL;
221 return (LPDIOBJECTDATAFORMAT)((LPBYTE)df->rgodf + idx * df->dwObjSize);
224 /* dataformat_to_odf_by_type
225 * Find the Nth object of the selected type in the DataFormat
227 LPDIOBJECTDATAFORMAT dataformat_to_odf_by_type(LPCDIDATAFORMAT df, int n, DWORD type)
229 int i, nfound = 0;
231 for (i=0; i < df->dwNumObjs; i++)
233 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(df, i);
235 if (odf->dwType & type)
237 if (n == nfound)
238 return odf;
240 nfound++;
244 return NULL;
247 static BOOL match_device_object( DIDATAFORMAT *device_format, DIDATAFORMAT *user_format,
248 const DIDATAFORMAT *format, const DIOBJECTDATAFORMAT *match_obj, DWORD version )
250 DWORD i, device_instance, instance = DIDFT_GETINSTANCE( match_obj->dwType );
251 DIOBJECTDATAFORMAT *device_obj, *user_obj;
253 if (version < 0x0700 && instance == 0xff) instance = 0xffff;
255 for (i = 0; i < device_format->dwNumObjs; i++)
257 user_obj = user_format->rgodf + i;
258 device_obj = device_format->rgodf + i;
259 device_instance = DIDFT_GETINSTANCE( device_obj->dwType );
261 if (!(user_obj->dwType & DIDFT_OPTIONAL)) continue; /* already matched */
262 if (match_obj->pguid && device_obj->pguid && !IsEqualGUID( device_obj->pguid, match_obj->pguid )) continue;
263 if (instance != DIDFT_GETINSTANCE( DIDFT_ANYINSTANCE ) && instance != device_instance) continue;
264 if (!(DIDFT_GETTYPE( match_obj->dwType ) & DIDFT_GETTYPE( device_obj->dwType ))) continue;
266 TRACE( "match %s with device %s\n", debugstr_diobjectdataformat( match_obj ),
267 debugstr_diobjectdataformat( device_obj ) );
269 *user_obj = *device_obj;
270 user_obj->dwOfs = match_obj->dwOfs;
271 return TRUE;
274 return FALSE;
277 static HRESULT dinput_device_init_user_format( struct dinput_device *impl, const DIDATAFORMAT *format )
279 DIDATAFORMAT *user_format, *device_format = impl->device_format;
280 DIOBJECTDATAFORMAT *user_obj, *match_obj;
281 DWORD i;
283 if (!device_format) return DIERR_INVALIDPARAM;
284 if (!(user_format = malloc( sizeof(DIDATAFORMAT) ))) return DIERR_OUTOFMEMORY;
285 *user_format = *device_format;
286 user_format->dwFlags = format->dwFlags;
287 user_format->dwDataSize = format->dwDataSize;
288 user_format->dwNumObjs += format->dwNumObjs;
289 if (!(user_format->rgodf = calloc( user_format->dwNumObjs, sizeof(DIOBJECTDATAFORMAT) )))
291 free( user_format );
292 return DIERR_OUTOFMEMORY;
295 user_obj = user_format->rgodf + user_format->dwNumObjs;
296 while (user_obj-- > user_format->rgodf) user_obj->dwType |= DIDFT_OPTIONAL;
298 for (i = 0; i < format->dwNumObjs; ++i)
300 match_obj = format->rgodf + i;
302 if (!match_device_object( device_format, user_format, format, match_obj, impl->dinput->dwVersion ))
304 WARN( "object %s not found\n", debugstr_diobjectdataformat( match_obj ) );
305 if (!(match_obj->dwType & DIDFT_OPTIONAL)) goto failed;
306 user_obj = user_format->rgodf + device_format->dwNumObjs + i;
307 *user_obj = *match_obj;
311 user_obj = user_format->rgodf + user_format->dwNumObjs;
312 while (user_obj-- > user_format->rgodf) user_obj->dwType &= ~DIDFT_OPTIONAL;
314 impl->user_format = user_format;
315 return DI_OK;
317 failed:
318 free( user_format->rgodf );
319 free( user_format );
320 return DIERR_INVALIDPARAM;
323 static int id_to_offset( struct dinput_device *impl, int id )
325 DIDATAFORMAT *device_format = impl->device_format, *user_format = impl->user_format;
326 DIOBJECTDATAFORMAT *user_obj;
328 if (!user_format) return -1;
330 user_obj = user_format->rgodf + device_format->dwNumObjs;
331 while (user_obj-- > user_format->rgodf)
333 if (!user_obj->dwType) continue;
334 if ((user_obj->dwType & 0x00ffffff) == (id & 0x00ffffff)) return user_obj->dwOfs;
337 return -1;
340 static DWORD semantic_to_obj_id( struct dinput_device *This, DWORD dwSemantic )
342 DWORD type = (0x0000ff00 & dwSemantic) >> 8;
343 BOOL byofs = (dwSemantic & 0x80000000) != 0;
344 DWORD value = (dwSemantic & 0x000000ff);
345 BOOL found = FALSE;
346 DWORD instance;
347 int i;
349 for (i = 0; i < This->device_format->dwNumObjs && !found; i++)
351 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf( This->device_format, i );
353 if (byofs && value != odf->dwOfs) continue;
354 if (!byofs && value != DIDFT_GETINSTANCE(odf->dwType)) continue;
355 instance = DIDFT_GETINSTANCE(odf->dwType);
356 found = TRUE;
359 if (!found) return 0;
361 if (type & DIDFT_AXIS) type = DIDFT_RELAXIS;
362 if (type & DIDFT_BUTTON) type = DIDFT_PSHBUTTON;
364 return type | (0x0000ff00 & (instance << 8));
368 * get_mapping_key
369 * Retrieves an open registry key to save the mapping, parametrized for an username,
370 * specific device and specific action mapping guid.
372 static HKEY get_mapping_key(const WCHAR *device, const WCHAR *username, const WCHAR *guid)
374 static const WCHAR *subkey = L"Software\\Wine\\DirectInput\\Mappings\\%s\\%s\\%s";
375 HKEY hkey;
376 WCHAR *keyname;
378 SIZE_T len = wcslen( subkey ) + wcslen( username ) + wcslen( device ) + wcslen( guid ) + 1;
379 keyname = malloc( sizeof(WCHAR) * len );
380 swprintf( keyname, len, subkey, username, device, guid );
382 /* The key used is HKCU\Software\Wine\DirectInput\Mappings\[username]\[device]\[mapping_guid] */
383 if (RegCreateKeyW(HKEY_CURRENT_USER, keyname, &hkey))
384 hkey = 0;
386 free( keyname );
388 return hkey;
391 static HRESULT save_mapping_settings(IDirectInputDevice8W *iface, LPDIACTIONFORMATW lpdiaf, LPCWSTR lpszUsername)
393 WCHAR *guid_str = NULL;
394 DIDEVICEINSTANCEW didev;
395 HKEY hkey;
396 int i;
398 didev.dwSize = sizeof(didev);
399 IDirectInputDevice8_GetDeviceInfo(iface, &didev);
401 if (StringFromCLSID(&lpdiaf->guidActionMap, &guid_str) != S_OK)
402 return DI_SETTINGSNOTSAVED;
404 hkey = get_mapping_key(didev.tszInstanceName, lpszUsername, guid_str);
406 if (!hkey)
408 CoTaskMemFree(guid_str);
409 return DI_SETTINGSNOTSAVED;
412 /* Write each of the actions mapped for this device.
413 Format is "dwSemantic"="dwObjID" and key is of type REG_DWORD
415 for (i = 0; i < lpdiaf->dwNumActions; i++)
417 WCHAR label[9];
419 if (IsEqualGUID(&didev.guidInstance, &lpdiaf->rgoAction[i].guidInstance) &&
420 lpdiaf->rgoAction[i].dwHow != DIAH_UNMAPPED)
422 swprintf( label, 9, L"%x", lpdiaf->rgoAction[i].dwSemantic );
423 RegSetValueExW( hkey, label, 0, REG_DWORD, (const BYTE *)&lpdiaf->rgoAction[i].dwObjID,
424 sizeof(DWORD) );
428 RegCloseKey(hkey);
429 CoTaskMemFree(guid_str);
431 return DI_OK;
434 static BOOL load_mapping_settings( struct dinput_device *This, LPDIACTIONFORMATW lpdiaf, const WCHAR *username )
436 HKEY hkey;
437 WCHAR *guid_str;
438 DIDEVICEINSTANCEW didev;
439 int i, mapped = 0;
441 didev.dwSize = sizeof(didev);
442 IDirectInputDevice8_GetDeviceInfo(&This->IDirectInputDevice8W_iface, &didev);
444 if (StringFromCLSID(&lpdiaf->guidActionMap, &guid_str) != S_OK)
445 return FALSE;
447 hkey = get_mapping_key(didev.tszInstanceName, username, guid_str);
449 if (!hkey)
451 CoTaskMemFree(guid_str);
452 return FALSE;
455 /* Try to read each action in the DIACTIONFORMAT from registry */
456 for (i = 0; i < lpdiaf->dwNumActions; i++)
458 DWORD id, size = sizeof(DWORD);
459 WCHAR label[9];
461 swprintf( label, 9, L"%x", lpdiaf->rgoAction[i].dwSemantic );
463 if (!RegQueryValueExW(hkey, label, 0, NULL, (LPBYTE) &id, &size))
465 lpdiaf->rgoAction[i].dwObjID = id;
466 lpdiaf->rgoAction[i].guidInstance = didev.guidInstance;
467 lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
468 mapped += 1;
472 RegCloseKey(hkey);
473 CoTaskMemFree(guid_str);
475 return mapped > 0;
478 static BOOL set_app_data( struct dinput_device *dev, int offset, UINT_PTR app_data )
480 int num_actions = dev->num_actions;
481 ActionMap *action_map = dev->action_map, *target_map = NULL;
483 if (num_actions == 0)
485 num_actions = 1;
486 action_map = malloc( sizeof(ActionMap) );
487 if (!action_map) return FALSE;
488 target_map = &action_map[0];
490 else
492 int i;
493 for (i = 0; i < num_actions; i++)
495 if (dev->action_map[i].offset != offset) continue;
496 target_map = &dev->action_map[i];
497 break;
500 if (!target_map)
502 num_actions++;
503 action_map = realloc( action_map, sizeof(ActionMap) * num_actions );
504 if (!action_map) return FALSE;
505 target_map = &action_map[num_actions-1];
509 target_map->offset = offset;
510 target_map->uAppData = app_data;
512 dev->action_map = action_map;
513 dev->num_actions = num_actions;
515 return TRUE;
518 /******************************************************************************
519 * queue_event - add new event to the ring queue
522 void queue_event( IDirectInputDevice8W *iface, int inst_id, DWORD data, DWORD time, DWORD seq )
524 static ULONGLONG notify_ms = 0;
525 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
526 int next_pos, ofs = id_to_offset( This, inst_id );
527 ULONGLONG time_ms = GetTickCount64();
529 if (time_ms - notify_ms > 1000)
531 PostMessageW(GetDesktopWindow(), WM_WINE_NOTIFY_ACTIVITY, 0, 0);
532 notify_ms = time_ms;
535 if (!This->queue_len || This->overflow || ofs < 0) return;
537 next_pos = (This->queue_head + 1) % This->queue_len;
538 if (next_pos == This->queue_tail)
540 TRACE(" queue overflowed\n");
541 This->overflow = TRUE;
542 return;
545 TRACE(" queueing %d at offset %d (queue head %d / size %d)\n",
546 data, ofs, This->queue_head, This->queue_len);
548 This->data_queue[This->queue_head].dwOfs = ofs;
549 This->data_queue[This->queue_head].dwData = data;
550 This->data_queue[This->queue_head].dwTimeStamp = time;
551 This->data_queue[This->queue_head].dwSequence = seq;
552 This->data_queue[This->queue_head].uAppData = -1;
554 /* Set uAppData by means of action mapping */
555 if (This->num_actions > 0)
557 int i;
558 for (i=0; i < This->num_actions; i++)
560 if (This->action_map[i].offset == ofs)
562 TRACE("Offset %d mapped to uAppData %lu\n", ofs, This->action_map[i].uAppData);
563 This->data_queue[This->queue_head].uAppData = This->action_map[i].uAppData;
564 break;
569 This->queue_head = next_pos;
570 /* Send event if asked */
573 /******************************************************************************
574 * Acquire
577 static HRESULT WINAPI dinput_device_Acquire( IDirectInputDevice8W *iface )
579 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
580 HRESULT hr = DI_OK;
582 TRACE( "iface %p.\n", iface );
584 EnterCriticalSection( &impl->crit );
585 if (impl->status == STATUS_ACQUIRED)
586 hr = DI_NOEFFECT;
587 else if (!impl->user_format)
588 hr = DIERR_INVALIDPARAM;
589 else if ((impl->dwCoopLevel & DISCL_FOREGROUND) && impl->win != GetForegroundWindow())
590 hr = DIERR_OTHERAPPHASPRIO;
591 else
593 impl->status = STATUS_ACQUIRED;
594 if (FAILED(hr = impl->vtbl->acquire( iface ))) impl->status = STATUS_UNACQUIRED;
596 LeaveCriticalSection( &impl->crit );
597 if (hr != DI_OK) return hr;
599 dinput_hooks_acquire_device( iface );
600 check_dinput_hooks( iface, TRUE );
602 return hr;
605 /******************************************************************************
606 * Unacquire
609 static HRESULT WINAPI dinput_device_Unacquire( IDirectInputDevice8W *iface )
611 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
612 HRESULT hr = DI_OK;
614 TRACE( "iface %p.\n", iface );
616 EnterCriticalSection( &impl->crit );
617 if (impl->status != STATUS_ACQUIRED) hr = DI_NOEFFECT;
618 else hr = impl->vtbl->unacquire( iface );
619 impl->status = STATUS_UNACQUIRED;
620 LeaveCriticalSection( &impl->crit );
621 if (hr != DI_OK) return hr;
623 dinput_hooks_unacquire_device( iface );
624 check_dinput_hooks( iface, FALSE );
626 return hr;
629 /******************************************************************************
630 * IDirectInputDeviceA
633 static HRESULT WINAPI dinput_device_SetDataFormat( IDirectInputDevice8W *iface, const DIDATAFORMAT *format )
635 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
636 HRESULT res = DI_OK;
637 ULONG i;
639 TRACE( "iface %p, format %p.\n", iface, format );
641 if (!format) return E_POINTER;
642 if (TRACE_ON( dinput ))
644 TRACE( "user format %s\n", debugstr_didataformat( format ) );
645 for (i = 0; i < format->dwNumObjs; ++i) TRACE( " %u: object %s\n", i, debugstr_diobjectdataformat( format->rgodf + i ) );
648 if (format->dwSize != sizeof(DIDATAFORMAT)) return DIERR_INVALIDPARAM;
649 if (format->dwObjSize != sizeof(DIOBJECTDATAFORMAT)) return DIERR_INVALIDPARAM;
650 if (This->status == STATUS_ACQUIRED) return DIERR_ACQUIRED;
652 EnterCriticalSection(&This->crit);
654 free( This->action_map );
655 This->action_map = NULL;
656 This->num_actions = 0;
658 dinput_device_release_user_format( This );
659 res = dinput_device_init_user_format( This, format );
661 LeaveCriticalSection(&This->crit);
662 return res;
665 /******************************************************************************
666 * SetCooperativeLevel
668 * Set cooperative level and the source window for the events.
670 static HRESULT WINAPI dinput_device_SetCooperativeLevel( IDirectInputDevice8W *iface, HWND hwnd, DWORD flags )
672 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
673 HRESULT hr;
675 TRACE( "iface %p, hwnd %p, flags %#x.\n", iface, hwnd, flags );
677 _dump_cooperativelevel_DI( flags );
679 if ((flags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == 0 ||
680 (flags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE) ||
681 (flags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == 0 ||
682 (flags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == (DISCL_FOREGROUND | DISCL_BACKGROUND))
683 return DIERR_INVALIDPARAM;
685 if (hwnd && GetWindowLongW(hwnd, GWL_STYLE) & WS_CHILD) return E_HANDLE;
687 if (!hwnd && flags == (DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)) hwnd = GetDesktopWindow();
689 if (!IsWindow(hwnd)) return E_HANDLE;
691 /* For security reasons native does not allow exclusive background level
692 for mouse and keyboard only */
693 if (flags & DISCL_EXCLUSIVE && flags & DISCL_BACKGROUND &&
694 (IsEqualGUID( &This->guid, &GUID_SysMouse ) || IsEqualGUID( &This->guid, &GUID_SysKeyboard )))
695 return DIERR_UNSUPPORTED;
697 /* Store the window which asks for the mouse */
698 EnterCriticalSection(&This->crit);
699 if (This->status == STATUS_ACQUIRED) hr = DIERR_ACQUIRED;
700 else
702 This->win = hwnd;
703 This->dwCoopLevel = flags;
704 hr = DI_OK;
706 LeaveCriticalSection(&This->crit);
708 return hr;
711 static HRESULT WINAPI dinput_device_GetDeviceInfo( IDirectInputDevice8W *iface, DIDEVICEINSTANCEW *instance )
713 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
714 DWORD size;
716 TRACE( "iface %p, instance %p.\n", iface, instance );
718 if (!instance) return E_POINTER;
719 if (instance->dwSize != sizeof(DIDEVICEINSTANCE_DX3W) &&
720 instance->dwSize != sizeof(DIDEVICEINSTANCEW))
721 return DIERR_INVALIDPARAM;
723 size = instance->dwSize;
724 memcpy( instance, &impl->instance, size );
725 instance->dwSize = size;
727 return S_OK;
730 /******************************************************************************
731 * SetEventNotification : specifies event to be sent on state change
733 static HRESULT WINAPI dinput_device_SetEventNotification( IDirectInputDevice8W *iface, HANDLE event )
735 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
737 TRACE( "iface %p, event %p.\n", iface, event );
739 EnterCriticalSection(&This->crit);
740 This->hEvent = event;
741 LeaveCriticalSection(&This->crit);
742 return DI_OK;
745 void dinput_device_destroy( IDirectInputDevice8W *iface )
747 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
749 TRACE( "iface %p.\n", iface );
751 free( This->object_properties );
752 free( This->data_queue );
754 /* Free data format */
755 free( This->device_format->rgodf );
756 free( This->device_format );
757 dinput_device_release_user_format( This );
759 /* Free action mapping */
760 free( This->action_map );
762 IDirectInput_Release(&This->dinput->IDirectInput7A_iface);
763 This->crit.DebugInfo->Spare[0] = 0;
764 DeleteCriticalSection(&This->crit);
766 free( This );
769 static ULONG WINAPI dinput_device_Release( IDirectInputDevice8W *iface )
771 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
772 ULONG ref = InterlockedDecrement( &impl->ref );
774 TRACE( "iface %p, ref %u.\n", iface, ref );
776 if (!ref)
778 IDirectInputDevice_Unacquire( iface );
779 if (impl->vtbl->release) impl->vtbl->release( iface );
780 else dinput_device_destroy( iface );
783 return ref;
786 static HRESULT WINAPI dinput_device_GetCapabilities( IDirectInputDevice8W *iface, DIDEVCAPS *caps )
788 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
789 DWORD size;
791 TRACE( "iface %p, caps %p.\n", iface, caps );
793 if (!caps) return E_POINTER;
794 if (caps->dwSize != sizeof(DIDEVCAPS_DX3) &&
795 caps->dwSize != sizeof(DIDEVCAPS))
796 return DIERR_INVALIDPARAM;
798 size = caps->dwSize;
799 memcpy( caps, &impl->caps, size );
800 caps->dwSize = size;
802 return DI_OK;
805 static HRESULT WINAPI dinput_device_QueryInterface( IDirectInputDevice8W *iface, const GUID *iid, void **out )
807 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
809 TRACE( "iface %p, iid %s, out %p.\n", iface, debugstr_guid( iid ), out );
811 if (IsEqualGUID( &IID_IDirectInputDeviceA, iid ) ||
812 IsEqualGUID( &IID_IDirectInputDevice2A, iid ) ||
813 IsEqualGUID( &IID_IDirectInputDevice7A, iid ) ||
814 IsEqualGUID( &IID_IDirectInputDevice8A, iid ))
816 IDirectInputDevice2_AddRef(iface);
817 *out = IDirectInputDevice8A_from_impl( This );
818 return DI_OK;
821 if (IsEqualGUID( &IID_IUnknown, iid ) ||
822 IsEqualGUID( &IID_IDirectInputDeviceW, iid ) ||
823 IsEqualGUID( &IID_IDirectInputDevice2W, iid ) ||
824 IsEqualGUID( &IID_IDirectInputDevice7W, iid ) ||
825 IsEqualGUID( &IID_IDirectInputDevice8W, iid ))
827 IDirectInputDevice2_AddRef(iface);
828 *out = IDirectInputDevice8W_from_impl( This );
829 return DI_OK;
832 WARN( "%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid( iid ) );
833 return E_NOINTERFACE;
836 static ULONG WINAPI dinput_device_AddRef( IDirectInputDevice8W *iface )
838 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
839 ULONG ref = InterlockedIncrement( &impl->ref );
840 TRACE( "iface %p, ref %u.\n", iface, ref );
841 return ref;
844 static HRESULT WINAPI dinput_device_EnumObjects( IDirectInputDevice8W *iface,
845 LPDIENUMDEVICEOBJECTSCALLBACKW callback,
846 void *context, DWORD flags )
848 static const DIPROPHEADER filter =
850 .dwSize = sizeof(filter),
851 .dwHeaderSize = sizeof(filter),
852 .dwHow = DIPH_DEVICE,
854 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
855 HRESULT hr;
857 TRACE( "iface %p, callback %p, context %p, flags %#x.\n", iface, callback, context, flags );
859 if (!callback) return DIERR_INVALIDPARAM;
860 if (flags & ~(DIDFT_AXIS | DIDFT_POV | DIDFT_BUTTON | DIDFT_NODATA | DIDFT_COLLECTION))
861 return DIERR_INVALIDPARAM;
863 if (flags == DIDFT_ALL || (flags & DIDFT_AXIS))
865 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_AXIS, callback, context );
866 if (FAILED(hr)) return hr;
867 if (hr != DIENUM_CONTINUE) return DI_OK;
869 if (flags == DIDFT_ALL || (flags & DIDFT_POV))
871 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_POV, callback, context );
872 if (FAILED(hr)) return hr;
873 if (hr != DIENUM_CONTINUE) return DI_OK;
875 if (flags == DIDFT_ALL || (flags & DIDFT_BUTTON))
877 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_BUTTON, callback, context );
878 if (FAILED(hr)) return hr;
879 if (hr != DIENUM_CONTINUE) return DI_OK;
881 if (flags == DIDFT_ALL || (flags & (DIDFT_NODATA | DIDFT_COLLECTION)))
883 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_NODATA, callback, context );
884 if (FAILED(hr)) return hr;
885 if (hr != DIENUM_CONTINUE) return DI_OK;
888 return DI_OK;
891 static HRESULT enum_object_filter_init( struct dinput_device *impl, DIPROPHEADER *filter )
893 DIDATAFORMAT *device_format = impl->device_format, *user_format = impl->user_format;
894 DIOBJECTDATAFORMAT *device_obj, *user_obj;
896 if (filter->dwHow > DIPH_BYUSAGE) return DIERR_INVALIDPARAM;
897 if (filter->dwHow == DIPH_BYUSAGE && !(impl->instance.dwDevType & DIDEVTYPE_HID)) return DIERR_UNSUPPORTED;
898 if (filter->dwHow != DIPH_BYOFFSET) return DI_OK;
900 if (!impl->user_format) return DIERR_NOTFOUND;
902 user_obj = user_format->rgodf + device_format->dwNumObjs;
903 device_obj = device_format->rgodf + device_format->dwNumObjs;
904 while (user_obj-- > user_format->rgodf && device_obj-- > device_format->rgodf)
906 if (!user_obj->dwType) continue;
907 if (user_obj->dwOfs == filter->dwObj) break;
909 if (user_obj < user_format->rgodf) return DIERR_NOTFOUND;
911 filter->dwObj = device_obj->dwOfs;
912 return DI_OK;
915 static HRESULT check_property( struct dinput_device *impl, const GUID *guid, const DIPROPHEADER *header, BOOL set )
917 switch (LOWORD( guid ))
919 case (DWORD_PTR)DIPROP_VIDPID:
920 case (DWORD_PTR)DIPROP_TYPENAME:
921 case (DWORD_PTR)DIPROP_USERNAME:
922 case (DWORD_PTR)DIPROP_KEYNAME:
923 case (DWORD_PTR)DIPROP_LOGICALRANGE:
924 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
925 case (DWORD_PTR)DIPROP_APPDATA:
926 if (impl->dinput->dwVersion < 0x0800) return DIERR_UNSUPPORTED;
927 break;
930 switch (LOWORD( guid ))
932 case (DWORD_PTR)DIPROP_INSTANCENAME:
933 case (DWORD_PTR)DIPROP_KEYNAME:
934 case (DWORD_PTR)DIPROP_PRODUCTNAME:
935 case (DWORD_PTR)DIPROP_TYPENAME:
936 case (DWORD_PTR)DIPROP_USERNAME:
937 if (header->dwSize != sizeof(DIPROPSTRING)) return DIERR_INVALIDPARAM;
938 break;
940 case (DWORD_PTR)DIPROP_AUTOCENTER:
941 case (DWORD_PTR)DIPROP_AXISMODE:
942 case (DWORD_PTR)DIPROP_BUFFERSIZE:
943 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
944 case (DWORD_PTR)DIPROP_DEADZONE:
945 case (DWORD_PTR)DIPROP_FFGAIN:
946 case (DWORD_PTR)DIPROP_FFLOAD:
947 case (DWORD_PTR)DIPROP_GRANULARITY:
948 case (DWORD_PTR)DIPROP_JOYSTICKID:
949 case (DWORD_PTR)DIPROP_SATURATION:
950 case (DWORD_PTR)DIPROP_SCANCODE:
951 case (DWORD_PTR)DIPROP_VIDPID:
952 if (header->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
953 break;
955 case (DWORD_PTR)DIPROP_APPDATA:
956 if (header->dwSize != sizeof(DIPROPPOINTER)) return DIERR_INVALIDPARAM;
957 break;
959 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
960 case (DWORD_PTR)DIPROP_LOGICALRANGE:
961 case (DWORD_PTR)DIPROP_RANGE:
962 if (header->dwSize != sizeof(DIPROPRANGE)) return DIERR_INVALIDPARAM;
963 break;
965 case (DWORD_PTR)DIPROP_GUIDANDPATH:
966 if (header->dwSize != sizeof(DIPROPGUIDANDPATH)) return DIERR_INVALIDPARAM;
967 break;
970 switch (LOWORD( guid ))
972 case (DWORD_PTR)DIPROP_PRODUCTNAME:
973 case (DWORD_PTR)DIPROP_INSTANCENAME:
974 case (DWORD_PTR)DIPROP_VIDPID:
975 case (DWORD_PTR)DIPROP_JOYSTICKID:
976 case (DWORD_PTR)DIPROP_GUIDANDPATH:
977 case (DWORD_PTR)DIPROP_BUFFERSIZE:
978 case (DWORD_PTR)DIPROP_FFGAIN:
979 case (DWORD_PTR)DIPROP_TYPENAME:
980 case (DWORD_PTR)DIPROP_USERNAME:
981 case (DWORD_PTR)DIPROP_AUTOCENTER:
982 case (DWORD_PTR)DIPROP_AXISMODE:
983 case (DWORD_PTR)DIPROP_FFLOAD:
984 if (header->dwHow != DIPH_DEVICE) return DIERR_UNSUPPORTED;
985 if (header->dwObj) return DIERR_INVALIDPARAM;
986 break;
988 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
989 case (DWORD_PTR)DIPROP_LOGICALRANGE:
990 case (DWORD_PTR)DIPROP_RANGE:
991 case (DWORD_PTR)DIPROP_DEADZONE:
992 case (DWORD_PTR)DIPROP_SATURATION:
993 case (DWORD_PTR)DIPROP_GRANULARITY:
994 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
995 if (header->dwHow == DIPH_DEVICE && !set) return DIERR_UNSUPPORTED;
996 break;
998 case (DWORD_PTR)DIPROP_KEYNAME:
999 if (header->dwHow == DIPH_DEVICE) return DIERR_INVALIDPARAM;
1000 break;
1002 case (DWORD_PTR)DIPROP_SCANCODE:
1003 case (DWORD_PTR)DIPROP_APPDATA:
1004 if (header->dwHow == DIPH_DEVICE) return DIERR_UNSUPPORTED;
1005 break;
1008 if (set)
1010 switch (LOWORD( guid ))
1012 case (DWORD_PTR)DIPROP_AUTOCENTER:
1013 case (DWORD_PTR)DIPROP_AXISMODE:
1014 case (DWORD_PTR)DIPROP_BUFFERSIZE:
1015 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1016 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1017 if (impl->status == STATUS_ACQUIRED) return DIERR_ACQUIRED;
1018 break;
1019 case (DWORD_PTR)DIPROP_FFLOAD:
1020 case (DWORD_PTR)DIPROP_GRANULARITY:
1021 case (DWORD_PTR)DIPROP_VIDPID:
1022 case (DWORD_PTR)DIPROP_TYPENAME:
1023 case (DWORD_PTR)DIPROP_USERNAME:
1024 case (DWORD_PTR)DIPROP_GUIDANDPATH:
1025 return DIERR_READONLY;
1028 switch (LOWORD( guid ))
1030 case (DWORD_PTR)DIPROP_RANGE:
1032 const DIPROPRANGE *value = (const DIPROPRANGE *)header;
1033 if (value->lMin > value->lMax) return DIERR_INVALIDPARAM;
1034 break;
1036 case (DWORD_PTR)DIPROP_DEADZONE:
1037 case (DWORD_PTR)DIPROP_SATURATION:
1038 case (DWORD_PTR)DIPROP_FFGAIN:
1040 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1041 if (value->dwData > 10000) return DIERR_INVALIDPARAM;
1042 break;
1044 case (DWORD_PTR)DIPROP_AUTOCENTER:
1045 case (DWORD_PTR)DIPROP_AXISMODE:
1046 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1048 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1049 if (value->dwData > 1) return DIERR_INVALIDPARAM;
1050 break;
1052 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1053 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1054 return DIERR_UNSUPPORTED;
1057 else
1059 switch (LOWORD( guid ))
1061 case (DWORD_PTR)DIPROP_RANGE:
1062 case (DWORD_PTR)DIPROP_GRANULARITY:
1063 if (!impl->caps.dwAxes) return DIERR_UNSUPPORTED;
1064 break;
1066 case (DWORD_PTR)DIPROP_KEYNAME:
1067 /* not supported on the mouse */
1068 if (impl->caps.dwAxes && !(impl->caps.dwDevType & DIDEVTYPE_HID)) return DIERR_UNSUPPORTED;
1069 break;
1071 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1072 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1073 case (DWORD_PTR)DIPROP_DEADZONE:
1074 case (DWORD_PTR)DIPROP_SATURATION:
1075 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1076 if (!impl->object_properties) return DIERR_UNSUPPORTED;
1077 break;
1079 case (DWORD_PTR)DIPROP_FFLOAD:
1080 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1081 if (!is_exclusively_acquired( impl )) return DIERR_NOTEXCLUSIVEACQUIRED;
1082 /* fallthrough */
1083 case (DWORD_PTR)DIPROP_PRODUCTNAME:
1084 case (DWORD_PTR)DIPROP_INSTANCENAME:
1085 case (DWORD_PTR)DIPROP_VIDPID:
1086 case (DWORD_PTR)DIPROP_JOYSTICKID:
1087 case (DWORD_PTR)DIPROP_GUIDANDPATH:
1088 if (!impl->vtbl->get_property) return DIERR_UNSUPPORTED;
1089 break;
1093 return DI_OK;
1096 static BOOL CALLBACK find_object( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1098 *(DIDEVICEOBJECTINSTANCEW *)context = *instance;
1099 return DIENUM_STOP;
1102 struct get_object_property_params
1104 IDirectInputDevice8W *iface;
1105 DIPROPHEADER *header;
1106 DWORD property;
1109 static BOOL CALLBACK get_object_property( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1111 static const struct object_properties default_properties =
1113 .range_min = DIPROPRANGE_NOMIN,
1114 .range_max = DIPROPRANGE_NOMAX,
1116 struct get_object_property_params *params = context;
1117 struct dinput_device *impl = impl_from_IDirectInputDevice8W( params->iface );
1118 const struct object_properties *properties = NULL;
1120 if (!impl->object_properties) properties = &default_properties;
1121 else properties = impl->object_properties + instance->dwOfs / sizeof(LONG);
1123 switch (params->property)
1125 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1127 DIPROPRANGE *value = (DIPROPRANGE *)params->header;
1128 value->lMin = properties->physical_min;
1129 value->lMax = properties->physical_max;
1130 return DI_OK;
1132 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1134 DIPROPRANGE *value = (DIPROPRANGE *)params->header;
1135 value->lMin = properties->logical_min;
1136 value->lMax = properties->logical_max;
1137 return DI_OK;
1139 case (DWORD_PTR)DIPROP_RANGE:
1141 DIPROPRANGE *value = (DIPROPRANGE *)params->header;
1142 value->lMin = properties->range_min;
1143 value->lMax = properties->range_max;
1144 return DIENUM_STOP;
1146 case (DWORD_PTR)DIPROP_DEADZONE:
1148 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1149 value->dwData = properties->deadzone;
1150 return DIENUM_STOP;
1152 case (DWORD_PTR)DIPROP_SATURATION:
1154 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1155 value->dwData = properties->saturation;
1156 return DIENUM_STOP;
1158 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1160 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1161 value->dwData = properties->calibration_mode;
1162 return DI_OK;
1164 case (DWORD_PTR)DIPROP_GRANULARITY:
1166 DIPROPDWORD *value = (DIPROPDWORD *)params->header;
1167 value->dwData = 1;
1168 return DIENUM_STOP;
1170 case (DWORD_PTR)DIPROP_KEYNAME:
1172 DIPROPSTRING *value = (DIPROPSTRING *)params->header;
1173 lstrcpynW( value->wsz, instance->tszName, ARRAY_SIZE(value->wsz) );
1174 return DIENUM_STOP;
1178 return DIENUM_STOP;
1181 static HRESULT dinput_device_get_property( IDirectInputDevice8W *iface, const GUID *guid, DIPROPHEADER *header )
1183 struct get_object_property_params params = {.iface = iface, .header = header, .property = LOWORD( guid )};
1184 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1185 DWORD object_mask = DIDFT_AXIS | DIDFT_BUTTON | DIDFT_POV;
1186 DIPROPHEADER filter;
1187 HRESULT hr;
1189 filter = *header;
1190 if (FAILED(hr = enum_object_filter_init( impl, &filter ))) return hr;
1191 if (FAILED(hr = check_property( impl, guid, header, FALSE ))) return hr;
1193 switch (LOWORD( guid ))
1195 case (DWORD_PTR)DIPROP_PRODUCTNAME:
1196 case (DWORD_PTR)DIPROP_INSTANCENAME:
1197 case (DWORD_PTR)DIPROP_VIDPID:
1198 case (DWORD_PTR)DIPROP_JOYSTICKID:
1199 case (DWORD_PTR)DIPROP_GUIDANDPATH:
1200 case (DWORD_PTR)DIPROP_FFLOAD:
1201 return impl->vtbl->get_property( iface, LOWORD( guid ), header, NULL );
1203 case (DWORD_PTR)DIPROP_RANGE:
1204 case (DWORD_PTR)DIPROP_PHYSICALRANGE:
1205 case (DWORD_PTR)DIPROP_LOGICALRANGE:
1206 case (DWORD_PTR)DIPROP_DEADZONE:
1207 case (DWORD_PTR)DIPROP_SATURATION:
1208 case (DWORD_PTR)DIPROP_GRANULARITY:
1209 case (DWORD_PTR)DIPROP_KEYNAME:
1210 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1211 hr = impl->vtbl->enum_objects( iface, &filter, object_mask, get_object_property, &params );
1212 if (FAILED(hr)) return hr;
1213 if (hr == DIENUM_CONTINUE) return DIERR_NOTFOUND;
1214 return DI_OK;
1216 case (DWORD_PTR)DIPROP_AUTOCENTER:
1218 DIPROPDWORD *value = (DIPROPDWORD *)header;
1219 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1220 value->dwData = impl->autocenter;
1221 return DI_OK;
1223 case (DWORD_PTR)DIPROP_BUFFERSIZE:
1225 DIPROPDWORD *value = (DIPROPDWORD *)header;
1226 value->dwData = impl->buffersize;
1227 return DI_OK;
1229 case (DWORD_PTR)DIPROP_USERNAME:
1231 DIPROPSTRING *value = (DIPROPSTRING *)header;
1232 struct DevicePlayer *device_player;
1233 LIST_FOR_EACH_ENTRY( device_player, &impl->dinput->device_players, struct DevicePlayer, entry )
1235 if (IsEqualGUID( &device_player->instance_guid, &impl->guid ))
1237 if (!*device_player->username) break;
1238 lstrcpynW( value->wsz, device_player->username, ARRAY_SIZE(value->wsz) );
1239 return DI_OK;
1242 return S_FALSE;
1244 case (DWORD_PTR)DIPROP_FFGAIN:
1246 DIPROPDWORD *value = (DIPROPDWORD *)header;
1247 value->dwData = impl->device_gain;
1248 return DI_OK;
1250 case (DWORD_PTR)DIPROP_CALIBRATION:
1251 return DIERR_INVALIDPARAM;
1252 default:
1253 FIXME( "Unknown property %s\n", debugstr_guid( guid ) );
1254 return DIERR_UNSUPPORTED;
1257 return DI_OK;
1260 static HRESULT WINAPI dinput_device_GetProperty( IDirectInputDevice8W *iface, const GUID *guid, DIPROPHEADER *header )
1262 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1263 HRESULT hr;
1265 TRACE( "iface %p, guid %s, header %p\n", iface, debugstr_guid( guid ), header );
1267 if (!header) return DIERR_INVALIDPARAM;
1268 if (header->dwHeaderSize != sizeof(DIPROPHEADER)) return DIERR_INVALIDPARAM;
1269 if (!IS_DIPROP( guid )) return DI_OK;
1271 EnterCriticalSection( &impl->crit );
1272 hr = dinput_device_get_property( iface, guid, header );
1273 LeaveCriticalSection( &impl->crit );
1275 return hr;
1278 struct set_object_property_params
1280 IDirectInputDevice8W *iface;
1281 const DIPROPHEADER *header;
1282 DWORD property;
1285 static BOOL CALLBACK set_object_property( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1287 struct set_object_property_params *params = context;
1288 struct dinput_device *impl = impl_from_IDirectInputDevice8W( params->iface );
1289 struct object_properties *properties = NULL;
1291 if (!impl->object_properties) return DIENUM_STOP;
1292 properties = impl->object_properties + instance->dwOfs / sizeof(LONG);
1294 switch (params->property)
1296 case (DWORD_PTR)DIPROP_RANGE:
1298 const DIPROPRANGE *value = (const DIPROPRANGE *)params->header;
1299 properties->range_min = value->lMin;
1300 properties->range_max = value->lMax;
1301 return DIENUM_CONTINUE;
1303 case (DWORD_PTR)DIPROP_DEADZONE:
1305 const DIPROPDWORD *value = (const DIPROPDWORD *)params->header;
1306 properties->deadzone = value->dwData;
1307 return DIENUM_CONTINUE;
1309 case (DWORD_PTR)DIPROP_SATURATION:
1311 const DIPROPDWORD *value = (const DIPROPDWORD *)params->header;
1312 properties->saturation = value->dwData;
1313 return DIENUM_CONTINUE;
1315 case (DWORD_PTR)DIPROP_CALIBRATIONMODE:
1317 const DIPROPDWORD *value = (const DIPROPDWORD *)params->header;
1318 properties->calibration_mode = value->dwData;
1319 return DIENUM_CONTINUE;
1323 return DIENUM_STOP;
1326 static BOOL CALLBACK reset_object_value( const DIDEVICEOBJECTINSTANCEW *instance, void *context )
1328 struct dinput_device *impl = context;
1329 struct object_properties *properties;
1330 LONG tmp = -1;
1332 if (!impl->object_properties) return DIENUM_STOP;
1333 properties = impl->object_properties + instance->dwOfs / sizeof(LONG);
1335 if (instance->dwType & DIDFT_AXIS)
1337 if (!properties->range_min) tmp = properties->range_max / 2;
1338 else tmp = round( (properties->range_min + properties->range_max) / 2.0 );
1341 *(LONG *)(impl->device_state + instance->dwOfs) = tmp;
1342 return DIENUM_CONTINUE;
1345 static void reset_device_state( IDirectInputDevice8W *iface )
1347 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1348 DIPROPHEADER filter =
1350 .dwHeaderSize = sizeof(DIPROPHEADER),
1351 .dwSize = sizeof(DIPROPHEADER),
1352 .dwHow = DIPH_DEVICE,
1353 .dwObj = 0,
1356 impl->vtbl->enum_objects( iface, &filter, DIDFT_AXIS | DIDFT_POV, reset_object_value, impl );
1359 static HRESULT WINAPI dinput_device_set_property( IDirectInputDevice8W *iface, const GUID *guid,
1360 const DIPROPHEADER *header )
1362 struct set_object_property_params params = {.iface = iface, .header = header, .property = LOWORD( guid )};
1363 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1364 DWORD object_mask = DIDFT_AXIS | DIDFT_BUTTON | DIDFT_POV;
1365 DIDEVICEOBJECTINSTANCEW instance;
1366 DIPROPHEADER filter;
1367 HRESULT hr;
1369 filter = *header;
1370 if (FAILED(hr = enum_object_filter_init( impl, &filter ))) return hr;
1371 if (FAILED(hr = check_property( impl, guid, header, TRUE ))) return hr;
1373 switch (LOWORD( guid ))
1375 case (DWORD_PTR)DIPROP_RANGE:
1376 case (DWORD_PTR)DIPROP_DEADZONE:
1377 case (DWORD_PTR)DIPROP_SATURATION:
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_CALIBRATIONMODE:
1386 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1387 if (value->dwData > DIPROPCALIBRATIONMODE_RAW) return DIERR_INVALIDPARAM;
1388 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_AXIS, set_object_property, &params );
1389 if (FAILED(hr)) return hr;
1390 reset_device_state( iface );
1391 return DI_OK;
1393 case (DWORD_PTR)DIPROP_AUTOCENTER:
1395 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1396 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1398 FIXME( "DIPROP_AUTOCENTER stub!\n" );
1399 impl->autocenter = value->dwData;
1400 return DI_OK;
1402 case (DWORD_PTR)DIPROP_FFGAIN:
1404 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1405 if (!impl->vtbl->send_device_gain) return DIERR_UNSUPPORTED;
1406 impl->device_gain = value->dwData;
1407 if (!is_exclusively_acquired( impl )) return DI_OK;
1408 return impl->vtbl->send_device_gain( iface, impl->device_gain );
1410 case (DWORD_PTR)DIPROP_AXISMODE:
1412 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1414 TRACE( "Axis mode: %s\n", value->dwData == DIPROPAXISMODE_ABS ? "absolute" : "relative" );
1415 if (impl->user_format)
1417 impl->user_format->dwFlags &= ~DIDFT_AXIS;
1418 impl->user_format->dwFlags |= value->dwData == DIPROPAXISMODE_ABS ? DIDF_ABSAXIS : DIDF_RELAXIS;
1420 return DI_OK;
1422 case (DWORD_PTR)DIPROP_BUFFERSIZE:
1424 const DIPROPDWORD *value = (const DIPROPDWORD *)header;
1426 TRACE( "buffersize = %d\n", value->dwData );
1428 impl->buffersize = value->dwData;
1429 impl->queue_len = min( impl->buffersize, 1024 );
1430 free( impl->data_queue );
1432 impl->data_queue = impl->queue_len ? malloc( impl->queue_len * sizeof(DIDEVICEOBJECTDATA) ) : NULL;
1433 impl->queue_head = impl->queue_tail = impl->overflow = 0;
1434 return DI_OK;
1436 case (DWORD_PTR)DIPROP_APPDATA:
1438 const DIPROPPOINTER *value = (const DIPROPPOINTER *)header;
1439 int user_offset;
1440 hr = impl->vtbl->enum_objects( iface, &filter, object_mask, find_object, &instance );
1441 if (FAILED(hr)) return hr;
1442 if (hr == DIENUM_CONTINUE) return DIERR_OBJECTNOTFOUND;
1443 if ((user_offset = id_to_offset( impl, instance.dwType )) < 0) return DIERR_OBJECTNOTFOUND;
1444 if (!set_app_data( impl, user_offset, value->uData )) return DIERR_OUTOFMEMORY;
1445 return DI_OK;
1447 default:
1448 FIXME( "Unknown property %s\n", debugstr_guid( guid ) );
1449 return DIERR_UNSUPPORTED;
1452 return DI_OK;
1455 static HRESULT WINAPI dinput_device_SetProperty( IDirectInputDevice8W *iface, const GUID *guid,
1456 const DIPROPHEADER *header )
1458 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1459 HRESULT hr;
1461 TRACE( "iface %p, guid %s, header %p\n", iface, debugstr_guid( guid ), header );
1463 if (!header) return DIERR_INVALIDPARAM;
1464 if (header->dwHeaderSize != sizeof(DIPROPHEADER)) return DIERR_INVALIDPARAM;
1465 if (!IS_DIPROP( guid )) return DI_OK;
1467 EnterCriticalSection( &impl->crit );
1468 hr = dinput_device_set_property( iface, guid, header );
1469 LeaveCriticalSection( &impl->crit );
1471 return hr;
1474 static void dinput_device_set_username( struct dinput_device *impl, const DIPROPSTRING *value )
1476 struct DevicePlayer *device_player;
1477 BOOL found = FALSE;
1479 LIST_FOR_EACH_ENTRY( device_player, &impl->dinput->device_players, struct DevicePlayer, entry )
1481 if (IsEqualGUID( &device_player->instance_guid, &impl->guid ))
1483 found = TRUE;
1484 break;
1487 if (!found && (device_player = malloc( sizeof(struct DevicePlayer) )))
1489 list_add_tail( &impl->dinput->device_players, &device_player->entry );
1490 device_player->instance_guid = impl->guid;
1492 if (device_player)
1493 lstrcpynW( device_player->username, value->wsz, ARRAY_SIZE(device_player->username) );
1496 static BOOL CALLBACK get_object_info( const DIDEVICEOBJECTINSTANCEW *instance, void *data )
1498 DIDEVICEOBJECTINSTANCEW *dest = data;
1499 DWORD size = dest->dwSize;
1501 memcpy( dest, instance, size );
1502 dest->dwSize = size;
1504 return DIENUM_STOP;
1507 static HRESULT WINAPI dinput_device_GetObjectInfo( IDirectInputDevice8W *iface,
1508 DIDEVICEOBJECTINSTANCEW *instance, DWORD obj, DWORD how )
1510 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1511 DIPROPHEADER filter =
1513 .dwSize = sizeof(filter),
1514 .dwHeaderSize = sizeof(filter),
1515 .dwHow = how,
1516 .dwObj = obj
1518 HRESULT hr;
1520 TRACE( "iface %p, instance %p, obj %#x, how %#x.\n", iface, instance, obj, how );
1522 if (!instance) return E_POINTER;
1523 if (instance->dwSize != sizeof(DIDEVICEOBJECTINSTANCE_DX3W) && instance->dwSize != sizeof(DIDEVICEOBJECTINSTANCEW))
1524 return DIERR_INVALIDPARAM;
1525 if (how == DIPH_DEVICE) return DIERR_INVALIDPARAM;
1526 if (FAILED(hr = enum_object_filter_init( impl, &filter ))) return hr;
1528 hr = impl->vtbl->enum_objects( iface, &filter, DIDFT_ALL, get_object_info, instance );
1529 if (FAILED(hr)) return hr;
1530 if (hr == DIENUM_CONTINUE) return DIERR_NOTFOUND;
1531 return DI_OK;
1534 static HRESULT WINAPI dinput_device_GetDeviceState( IDirectInputDevice8W *iface, DWORD size, void *data )
1536 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1537 DIDATAFORMAT *device_format = impl->device_format, *user_format;
1538 DIOBJECTDATAFORMAT *device_obj, *user_obj;
1539 BYTE *user_state = data;
1540 DIPROPHEADER filter =
1542 .dwSize = sizeof(filter),
1543 .dwHeaderSize = sizeof(filter),
1544 .dwHow = DIPH_DEVICE,
1545 .dwObj = 0,
1547 HRESULT hr;
1549 TRACE( "iface %p, size %u, data %p.\n", iface, size, data );
1551 if (!data) return DIERR_INVALIDPARAM;
1553 IDirectInputDevice2_Poll( iface );
1555 EnterCriticalSection( &impl->crit );
1556 if (impl->status != STATUS_ACQUIRED)
1557 hr = DIERR_NOTACQUIRED;
1558 else if (!(user_format = impl->user_format))
1559 hr = DIERR_INVALIDPARAM;
1560 else if (size != user_format->dwDataSize)
1561 hr = DIERR_INVALIDPARAM;
1562 else
1564 memset( user_state, 0, size );
1566 user_obj = user_format->rgodf + device_format->dwNumObjs;
1567 device_obj = device_format->rgodf + device_format->dwNumObjs;
1568 while (user_obj-- > user_format->rgodf && device_obj-- > device_format->rgodf)
1570 if (user_obj->dwType & DIDFT_BUTTON)
1571 user_state[user_obj->dwOfs] = impl->device_state[device_obj->dwOfs];
1574 /* reset optional POVs to their default */
1575 user_obj = user_format->rgodf + user_format->dwNumObjs;
1576 while (user_obj-- > user_format->rgodf + device_format->dwNumObjs)
1577 if (user_obj->dwType & DIDFT_POV) *(ULONG *)(user_state + user_obj->dwOfs) = 0xffffffff;
1579 user_obj = user_format->rgodf + device_format->dwNumObjs;
1580 device_obj = device_format->rgodf + device_format->dwNumObjs;
1581 while (user_obj-- > user_format->rgodf && device_obj-- > device_format->rgodf)
1583 if (user_obj->dwType & (DIDFT_POV | DIDFT_AXIS))
1584 *(ULONG *)(user_state + user_obj->dwOfs) = *(ULONG *)(impl->device_state + device_obj->dwOfs);
1585 if (!(user_format->dwFlags & DIDF_ABSAXIS) && (device_obj->dwType & DIDFT_RELAXIS))
1586 *(ULONG *)(impl->device_state + device_obj->dwOfs) = 0;
1589 hr = DI_OK;
1591 LeaveCriticalSection( &impl->crit );
1593 return hr;
1596 static HRESULT WINAPI dinput_device_GetDeviceData( IDirectInputDevice8W *iface, DWORD size, DIDEVICEOBJECTDATA *data,
1597 DWORD *count, DWORD flags )
1599 struct dinput_device *This = impl_from_IDirectInputDevice8W( iface );
1600 HRESULT ret = DI_OK;
1601 int len;
1603 TRACE( "iface %p, size %u, data %p, count %p, flags %#x.\n", iface, size, data, count, flags );
1605 if (This->dinput->dwVersion == 0x0800 || size == sizeof(DIDEVICEOBJECTDATA_DX3))
1607 if (!This->queue_len) return DIERR_NOTBUFFERED;
1608 if (This->status != STATUS_ACQUIRED) return DIERR_NOTACQUIRED;
1611 if (!This->queue_len)
1612 return DI_OK;
1613 if (size < sizeof(DIDEVICEOBJECTDATA_DX3)) return DIERR_INVALIDPARAM;
1615 IDirectInputDevice2_Poll(iface);
1616 EnterCriticalSection(&This->crit);
1618 len = This->queue_head - This->queue_tail;
1619 if (len < 0) len += This->queue_len;
1621 if ((*count != INFINITE) && (len > *count)) len = *count;
1623 if (data)
1625 int i;
1626 for (i = 0; i < len; i++)
1628 int n = (This->queue_tail + i) % This->queue_len;
1629 memcpy( (char *)data + size * i, This->data_queue + n, size );
1632 *count = len;
1634 if (This->overflow && This->dinput->dwVersion == 0x0800)
1635 ret = DI_BUFFEROVERFLOW;
1637 if (!(flags & DIGDD_PEEK))
1639 /* Advance reading position */
1640 This->queue_tail = (This->queue_tail + len) % This->queue_len;
1641 This->overflow = FALSE;
1644 LeaveCriticalSection(&This->crit);
1646 TRACE( "Returning %d events queued\n", *count );
1647 return ret;
1650 static HRESULT WINAPI dinput_device_RunControlPanel( IDirectInputDevice8W *iface, HWND hwnd, DWORD flags )
1652 FIXME( "iface %p, hwnd %p, flags %#x stub!\n", iface, hwnd, flags );
1653 return DI_OK;
1656 static HRESULT WINAPI dinput_device_Initialize( IDirectInputDevice8W *iface, HINSTANCE instance,
1657 DWORD version, const GUID *guid )
1659 FIXME( "iface %p, instance %p, version %#x, guid %s stub!\n", iface, instance, version,
1660 debugstr_guid( guid ) );
1661 return DI_OK;
1664 static HRESULT WINAPI dinput_device_CreateEffect( IDirectInputDevice8W *iface, const GUID *guid,
1665 const DIEFFECT *params, IDirectInputEffect **out,
1666 IUnknown *outer )
1668 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1669 DWORD flags;
1670 HRESULT hr;
1672 TRACE( "iface %p, guid %s, params %p, out %p, outer %p\n", iface, debugstr_guid( guid ),
1673 params, out, outer );
1675 if (!out) return E_POINTER;
1676 *out = NULL;
1678 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1679 if (!impl->vtbl->create_effect) return DIERR_UNSUPPORTED;
1680 if (FAILED(hr = impl->vtbl->create_effect( iface, out ))) return hr;
1682 hr = IDirectInputEffect_Initialize( *out, DINPUT_instance, impl->dinput->dwVersion, guid );
1683 if (FAILED(hr)) goto failed;
1684 if (!params) return DI_OK;
1686 flags = params->dwSize == sizeof(DIEFFECT_DX6) ? DIEP_ALLPARAMS : DIEP_ALLPARAMS_DX5;
1687 if (!is_exclusively_acquired( impl )) flags |= DIEP_NODOWNLOAD;
1688 hr = IDirectInputEffect_SetParameters( *out, params, flags );
1689 if (FAILED(hr)) goto failed;
1690 return DI_OK;
1692 failed:
1693 IDirectInputEffect_Release( *out );
1694 *out = NULL;
1695 return hr;
1698 static HRESULT WINAPI dinput_device_EnumEffects( IDirectInputDevice8W *iface, LPDIENUMEFFECTSCALLBACKW callback,
1699 void *context, DWORD type )
1701 DIEFFECTINFOW info = {.dwSize = sizeof(info)};
1702 HRESULT hr;
1704 TRACE( "iface %p, callback %p, context %p, type %#x.\n", iface, callback, context, type );
1706 if (!callback) return DIERR_INVALIDPARAM;
1708 type = DIEFT_GETTYPE( type );
1710 if (type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1712 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_ConstantForce );
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_RAMPFORCE)
1719 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_RampForce );
1720 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1721 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1724 if (type == DIEFT_ALL || type == DIEFT_PERIODIC)
1726 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Square );
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_Sine );
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_Triangle );
1735 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1736 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1738 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_SawtoothUp );
1739 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1740 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1742 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_SawtoothDown );
1743 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1744 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1747 if (type == DIEFT_ALL || type == DIEFT_CONDITION)
1749 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Spring );
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_Damper );
1754 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1755 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1757 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Inertia );
1758 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1759 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1761 hr = IDirectInputDevice8_GetEffectInfo( iface, &info, &GUID_Friction );
1762 if (FAILED(hr) && hr != DIERR_DEVICENOTREG) return hr;
1763 if (hr == DI_OK && callback( &info, context ) == DIENUM_STOP) return DI_OK;
1766 return DI_OK;
1769 static HRESULT WINAPI dinput_device_GetEffectInfo( IDirectInputDevice8W *iface, DIEFFECTINFOW *info,
1770 const GUID *guid )
1772 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1774 TRACE( "iface %p, info %p, guid %s.\n", iface, info, debugstr_guid( guid ) );
1776 if (!info) return E_POINTER;
1777 if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
1778 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_DEVICENOTREG;
1779 if (!impl->vtbl->get_effect_info) return DIERR_UNSUPPORTED;
1780 return impl->vtbl->get_effect_info( iface, info, guid );
1783 static HRESULT WINAPI dinput_device_GetForceFeedbackState( IDirectInputDevice8W *iface, DWORD *out )
1785 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1786 HRESULT hr = DI_OK;
1788 TRACE( "iface %p, out %p.\n", iface, out );
1790 if (!out) return E_POINTER;
1791 *out = 0;
1793 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1795 EnterCriticalSection( &impl->crit );
1796 if (!is_exclusively_acquired( impl )) hr = DIERR_NOTEXCLUSIVEACQUIRED;
1797 else *out = impl->force_feedback_state;
1798 LeaveCriticalSection( &impl->crit );
1800 return hr;
1803 static HRESULT WINAPI dinput_device_SendForceFeedbackCommand( IDirectInputDevice8W *iface, DWORD command )
1805 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1806 HRESULT hr;
1808 TRACE( "iface %p, command %#x.\n", iface, command );
1810 switch (command)
1812 case DISFFC_RESET: break;
1813 case DISFFC_STOPALL: break;
1814 case DISFFC_PAUSE: break;
1815 case DISFFC_CONTINUE: break;
1816 case DISFFC_SETACTUATORSON: break;
1817 case DISFFC_SETACTUATORSOFF: break;
1818 default: return DIERR_INVALIDPARAM;
1821 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DIERR_UNSUPPORTED;
1822 if (!impl->vtbl->send_force_feedback_command) return DIERR_UNSUPPORTED;
1824 EnterCriticalSection( &impl->crit );
1825 if (!is_exclusively_acquired( impl )) hr = DIERR_NOTEXCLUSIVEACQUIRED;
1826 else hr = impl->vtbl->send_force_feedback_command( iface, command, FALSE );
1827 LeaveCriticalSection( &impl->crit );
1829 return hr;
1832 static HRESULT WINAPI dinput_device_EnumCreatedEffectObjects( IDirectInputDevice8W *iface,
1833 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK callback,
1834 void *context, DWORD flags )
1836 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1838 TRACE( "iface %p, callback %p, context %p, flags %#x.\n", iface, callback, context, flags );
1840 if (!callback) return DIERR_INVALIDPARAM;
1841 if (flags) return DIERR_INVALIDPARAM;
1842 if (!(impl->caps.dwFlags & DIDC_FORCEFEEDBACK)) return DI_OK;
1843 if (!impl->vtbl->enum_created_effect_objects) return DIERR_UNSUPPORTED;
1845 return impl->vtbl->enum_created_effect_objects( iface, callback, context, flags );
1848 static HRESULT WINAPI dinput_device_Escape( IDirectInputDevice8W *iface, DIEFFESCAPE *escape )
1850 FIXME( "iface %p, escape %p stub!\n", iface, escape );
1851 return DI_OK;
1854 static HRESULT WINAPI dinput_device_Poll( IDirectInputDevice8W *iface )
1856 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1857 HRESULT hr = DI_NOEFFECT;
1859 EnterCriticalSection( &impl->crit );
1860 if (impl->status != STATUS_ACQUIRED) hr = DIERR_NOTACQUIRED;
1861 LeaveCriticalSection( &impl->crit );
1862 if (FAILED(hr)) return hr;
1864 if (impl->vtbl->poll) return impl->vtbl->poll( iface );
1865 return hr;
1868 static HRESULT WINAPI dinput_device_SendDeviceData( IDirectInputDevice8W *iface, DWORD size,
1869 const DIDEVICEOBJECTDATA *data, DWORD *count, DWORD flags )
1871 FIXME( "iface %p, size %u, data %p, count %p, flags %#x stub!\n", iface, size, data, count, flags );
1872 return DI_OK;
1875 static HRESULT WINAPI dinput_device_EnumEffectsInFile( IDirectInputDevice8W *iface, const WCHAR *filename,
1876 LPDIENUMEFFECTSINFILECALLBACK callback,
1877 void *context, DWORD flags )
1879 FIXME( "iface %p, filename %s, callback %p, context %p, flags %#x stub!\n", iface,
1880 debugstr_w(filename), callback, context, flags );
1881 return DI_OK;
1884 static HRESULT WINAPI dinput_device_WriteEffectToFile( IDirectInputDevice8W *iface, const WCHAR *filename,
1885 DWORD count, DIFILEEFFECT *effects, DWORD flags )
1887 FIXME( "iface %p, filename %s, count %u, effects %p, flags %#x stub!\n", iface,
1888 debugstr_w(filename), count, effects, flags );
1889 return DI_OK;
1892 static HRESULT WINAPI dinput_device_BuildActionMap( IDirectInputDevice8W *iface, DIACTIONFORMATW *format,
1893 const WCHAR *username, DWORD flags )
1895 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1896 BOOL load_success = FALSE, has_actions = FALSE;
1897 DWORD genre, username_len = MAX_PATH;
1898 WCHAR username_buf[MAX_PATH];
1899 const DIDATAFORMAT *df;
1900 DWORD devMask;
1901 int i;
1903 FIXME( "iface %p, format %p, username %s, flags %#x semi-stub!\n", iface, format,
1904 debugstr_w(username), flags );
1906 if (!format) return DIERR_INVALIDPARAM;
1908 switch (GET_DIDEVICE_TYPE( impl->instance.dwDevType ))
1910 case DIDEVTYPE_KEYBOARD:
1911 case DI8DEVTYPE_KEYBOARD:
1912 devMask = DIKEYBOARD_MASK;
1913 df = &c_dfDIKeyboard;
1914 break;
1915 case DIDEVTYPE_MOUSE:
1916 case DI8DEVTYPE_MOUSE:
1917 devMask = DIMOUSE_MASK;
1918 df = &c_dfDIMouse2;
1919 break;
1920 default:
1921 devMask = DIGENRE_ANY;
1922 df = impl->device_format;
1923 break;
1926 /* Unless asked the contrary by these flags, try to load a previous mapping */
1927 if (!(flags & DIDBAM_HWDEFAULTS))
1929 /* Retrieve logged user name if necessary */
1930 if (username == NULL) GetUserNameW( username_buf, &username_len );
1931 else lstrcpynW( username_buf, username, MAX_PATH );
1932 load_success = load_mapping_settings( impl, format, username_buf );
1935 if (load_success) return DI_OK;
1937 for (i = 0; i < format->dwNumActions; i++)
1939 /* Don't touch a user configured action */
1940 if (format->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
1942 genre = format->rgoAction[i].dwSemantic & DIGENRE_ANY;
1943 if (devMask == genre || (devMask == DIGENRE_ANY && genre != DIMOUSE_MASK && genre != DIKEYBOARD_MASK))
1945 DWORD obj_id = semantic_to_obj_id( impl, format->rgoAction[i].dwSemantic );
1946 DWORD type = DIDFT_GETTYPE( obj_id );
1947 DWORD inst = DIDFT_GETINSTANCE( obj_id );
1949 LPDIOBJECTDATAFORMAT odf;
1951 if (type == DIDFT_PSHBUTTON) type = DIDFT_BUTTON;
1952 if (type == DIDFT_RELAXIS) type = DIDFT_AXIS;
1954 /* Make sure the object exists */
1955 odf = dataformat_to_odf_by_type( df, inst, type );
1957 if (odf != NULL)
1959 format->rgoAction[i].dwObjID = obj_id;
1960 format->rgoAction[i].guidInstance = impl->guid;
1961 format->rgoAction[i].dwHow = DIAH_DEFAULT;
1962 has_actions = TRUE;
1965 else if (!(flags & DIDBAM_PRESERVE))
1967 /* We must clear action data belonging to other devices */
1968 memset( &format->rgoAction[i].guidInstance, 0, sizeof(GUID) );
1969 format->rgoAction[i].dwHow = DIAH_UNMAPPED;
1973 if (!has_actions) return DI_NOEFFECT;
1974 if (flags & (DIDBAM_DEFAULT|DIDBAM_PRESERVE|DIDBAM_INITIALIZE|DIDBAM_HWDEFAULTS))
1975 FIXME("Unimplemented flags %#x\n", flags);
1976 return DI_OK;
1979 static HRESULT WINAPI dinput_device_SetActionMap( IDirectInputDevice8W *iface, DIACTIONFORMATW *format,
1980 const WCHAR *username, DWORD flags )
1982 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
1983 DIDATAFORMAT data_format;
1984 DIOBJECTDATAFORMAT *obj_df = NULL;
1985 DIPROPDWORD dp;
1986 DIPROPRANGE dpr;
1987 DIPROPSTRING dps;
1988 WCHAR username_buf[MAX_PATH];
1989 DWORD username_len = MAX_PATH;
1990 int i, action = 0, num_actions = 0;
1991 unsigned int offset = 0;
1992 const DIDATAFORMAT *df;
1993 ActionMap *action_map;
1995 FIXME( "iface %p, format %p, username %s, flags %#x semi-stub!\n", iface, format,
1996 debugstr_w(username), flags );
1998 if (!format) return DIERR_INVALIDPARAM;
2000 switch (GET_DIDEVICE_TYPE( impl->instance.dwDevType ))
2002 case DIDEVTYPE_KEYBOARD:
2003 case DI8DEVTYPE_KEYBOARD:
2004 df = &c_dfDIKeyboard;
2005 break;
2006 case DIDEVTYPE_MOUSE:
2007 case DI8DEVTYPE_MOUSE:
2008 df = &c_dfDIMouse2;
2009 break;
2010 default:
2011 df = impl->device_format;
2012 break;
2015 if (impl->status == STATUS_ACQUIRED) return DIERR_ACQUIRED;
2017 data_format.dwSize = sizeof(data_format);
2018 data_format.dwObjSize = sizeof(DIOBJECTDATAFORMAT);
2019 data_format.dwFlags = DIDF_RELAXIS;
2020 data_format.dwDataSize = format->dwDataSize;
2022 /* Count the actions */
2023 for (i = 0; i < format->dwNumActions; i++)
2024 if (IsEqualGUID( &impl->guid, &format->rgoAction[i].guidInstance ))
2025 num_actions++;
2027 if (num_actions == 0) return DI_NOEFFECT;
2029 /* Construct the dataformat and actionmap */
2030 obj_df = malloc( sizeof(DIOBJECTDATAFORMAT) * num_actions );
2031 data_format.rgodf = (LPDIOBJECTDATAFORMAT)obj_df;
2032 data_format.dwNumObjs = num_actions;
2034 action_map = malloc( sizeof(ActionMap) * num_actions );
2036 for (i = 0; i < format->dwNumActions; i++)
2038 if (IsEqualGUID( &impl->guid, &format->rgoAction[i].guidInstance ))
2040 DWORD inst = DIDFT_GETINSTANCE( format->rgoAction[i].dwObjID );
2041 DWORD type = DIDFT_GETTYPE( format->rgoAction[i].dwObjID );
2042 LPDIOBJECTDATAFORMAT obj;
2044 if (type == DIDFT_PSHBUTTON) type = DIDFT_BUTTON;
2045 if (type == DIDFT_RELAXIS) type = DIDFT_AXIS;
2047 obj = dataformat_to_odf_by_type( df, inst, type );
2049 memcpy( &obj_df[action], obj, df->dwObjSize );
2051 action_map[action].uAppData = format->rgoAction[i].uAppData;
2052 action_map[action].offset = offset;
2053 obj_df[action].dwOfs = offset;
2054 offset += (type & DIDFT_BUTTON) ? 1 : 4;
2056 action++;
2060 IDirectInputDevice8_SetDataFormat( iface, &data_format );
2062 impl->action_map = action_map;
2063 impl->num_actions = num_actions;
2065 free( obj_df );
2067 /* Set the device properties according to the action format */
2068 dpr.diph.dwSize = sizeof(DIPROPRANGE);
2069 dpr.lMin = format->lAxisMin;
2070 dpr.lMax = format->lAxisMax;
2071 dpr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
2072 dpr.diph.dwObj = 0;
2073 dpr.diph.dwHow = DIPH_DEVICE;
2074 IDirectInputDevice8_SetProperty( iface, DIPROP_RANGE, &dpr.diph );
2076 if (format->dwBufferSize > 0)
2078 dp.diph.dwSize = sizeof(DIPROPDWORD);
2079 dp.dwData = format->dwBufferSize;
2080 dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
2081 dp.diph.dwObj = 0;
2082 dp.diph.dwHow = DIPH_DEVICE;
2083 IDirectInputDevice8_SetProperty( iface, DIPROP_BUFFERSIZE, &dp.diph );
2086 /* Retrieve logged user name if necessary */
2087 if (username == NULL) GetUserNameW( username_buf, &username_len );
2088 else lstrcpynW( username_buf, username, MAX_PATH );
2090 dps.diph.dwSize = sizeof(dps);
2091 dps.diph.dwHeaderSize = sizeof(DIPROPHEADER);
2092 dps.diph.dwObj = 0;
2093 dps.diph.dwHow = DIPH_DEVICE;
2094 if (flags & DIDSAM_NOUSER) dps.wsz[0] = '\0';
2095 else lstrcpynW( dps.wsz, username_buf, ARRAY_SIZE(dps.wsz) );
2096 dinput_device_set_username( impl, &dps );
2098 /* Save the settings to disk */
2099 save_mapping_settings( iface, format, username_buf );
2101 return DI_OK;
2104 static HRESULT WINAPI dinput_device_GetImageInfo( IDirectInputDevice8W *iface, DIDEVICEIMAGEINFOHEADERW *header )
2106 FIXME( "iface %p, header %p stub!\n", iface, header );
2107 return DI_OK;
2110 extern const IDirectInputDevice8AVtbl dinput_device_a_vtbl;
2111 static const IDirectInputDevice8WVtbl dinput_device_w_vtbl =
2113 /*** IUnknown methods ***/
2114 dinput_device_QueryInterface,
2115 dinput_device_AddRef,
2116 dinput_device_Release,
2117 /*** IDirectInputDevice methods ***/
2118 dinput_device_GetCapabilities,
2119 dinput_device_EnumObjects,
2120 dinput_device_GetProperty,
2121 dinput_device_SetProperty,
2122 dinput_device_Acquire,
2123 dinput_device_Unacquire,
2124 dinput_device_GetDeviceState,
2125 dinput_device_GetDeviceData,
2126 dinput_device_SetDataFormat,
2127 dinput_device_SetEventNotification,
2128 dinput_device_SetCooperativeLevel,
2129 dinput_device_GetObjectInfo,
2130 dinput_device_GetDeviceInfo,
2131 dinput_device_RunControlPanel,
2132 dinput_device_Initialize,
2133 /*** IDirectInputDevice2 methods ***/
2134 dinput_device_CreateEffect,
2135 dinput_device_EnumEffects,
2136 dinput_device_GetEffectInfo,
2137 dinput_device_GetForceFeedbackState,
2138 dinput_device_SendForceFeedbackCommand,
2139 dinput_device_EnumCreatedEffectObjects,
2140 dinput_device_Escape,
2141 dinput_device_Poll,
2142 dinput_device_SendDeviceData,
2143 /*** IDirectInputDevice7 methods ***/
2144 dinput_device_EnumEffectsInFile,
2145 dinput_device_WriteEffectToFile,
2146 /*** IDirectInputDevice8 methods ***/
2147 dinput_device_BuildActionMap,
2148 dinput_device_SetActionMap,
2149 dinput_device_GetImageInfo,
2152 HRESULT dinput_device_alloc( SIZE_T size, const struct dinput_device_vtbl *vtbl,
2153 const GUID *guid, IDirectInputImpl *dinput, void **out )
2155 struct dinput_device *This;
2156 DIDATAFORMAT *format;
2158 if (!(This = calloc( 1, size ))) return DIERR_OUTOFMEMORY;
2159 if (!(format = calloc( 1, sizeof(*format) )))
2161 free( This );
2162 return DIERR_OUTOFMEMORY;
2165 This->IDirectInputDevice8A_iface.lpVtbl = &dinput_device_a_vtbl;
2166 This->IDirectInputDevice8W_iface.lpVtbl = &dinput_device_w_vtbl;
2167 This->ref = 1;
2168 This->guid = *guid;
2169 This->instance.dwSize = sizeof(DIDEVICEINSTANCEW);
2170 This->caps.dwSize = sizeof(DIDEVCAPS);
2171 This->caps.dwFlags = DIDC_ATTACHED | DIDC_EMULATED;
2172 This->device_format = format;
2173 This->device_gain = 10000;
2174 This->force_feedback_state = DIGFFS_STOPPED | DIGFFS_EMPTY;
2175 InitializeCriticalSection( &This->crit );
2176 This->dinput = dinput;
2177 IDirectInput_AddRef( &dinput->IDirectInput7A_iface );
2178 This->vtbl = vtbl;
2180 *out = This;
2181 return DI_OK;
2184 static const GUID *object_instance_guid( const DIDEVICEOBJECTINSTANCEW *instance )
2186 if (IsEqualGUID( &instance->guidType, &GUID_XAxis )) return &GUID_XAxis;
2187 if (IsEqualGUID( &instance->guidType, &GUID_YAxis )) return &GUID_YAxis;
2188 if (IsEqualGUID( &instance->guidType, &GUID_ZAxis )) return &GUID_ZAxis;
2189 if (IsEqualGUID( &instance->guidType, &GUID_RxAxis )) return &GUID_RxAxis;
2190 if (IsEqualGUID( &instance->guidType, &GUID_RyAxis )) return &GUID_RyAxis;
2191 if (IsEqualGUID( &instance->guidType, &GUID_RzAxis )) return &GUID_RzAxis;
2192 if (IsEqualGUID( &instance->guidType, &GUID_Slider )) return &GUID_Slider;
2193 if (IsEqualGUID( &instance->guidType, &GUID_Button )) return &GUID_Button;
2194 if (IsEqualGUID( &instance->guidType, &GUID_Key )) return &GUID_Key;
2195 if (IsEqualGUID( &instance->guidType, &GUID_POV )) return &GUID_POV;
2196 return &GUID_Unknown;
2199 static BOOL CALLBACK enum_objects_init( const DIDEVICEOBJECTINSTANCEW *instance, void *data )
2201 struct dinput_device *impl = impl_from_IDirectInputDevice8W( data );
2202 DIDATAFORMAT *format = impl->device_format;
2203 DIOBJECTDATAFORMAT *obj_format;
2205 if (!format->rgodf)
2207 format->dwDataSize = max( format->dwDataSize, instance->dwOfs + sizeof(LONG) );
2208 if (instance->dwType & DIDFT_BUTTON) impl->caps.dwButtons++;
2209 if (instance->dwType & DIDFT_AXIS) impl->caps.dwAxes++;
2210 if (instance->dwType & DIDFT_POV) impl->caps.dwPOVs++;
2211 if (instance->dwType & (DIDFT_BUTTON|DIDFT_AXIS|DIDFT_POV))
2213 if (!impl->device_state_report_id)
2214 impl->device_state_report_id = instance->wReportId;
2215 else if (impl->device_state_report_id != instance->wReportId)
2216 FIXME( "multiple device state reports found!\n" );
2219 else
2221 obj_format = format->rgodf + format->dwNumObjs;
2222 obj_format->pguid = object_instance_guid( instance );
2223 obj_format->dwOfs = instance->dwOfs;
2224 obj_format->dwType = instance->dwType;
2225 obj_format->dwFlags = instance->dwFlags;
2228 if (impl->object_properties && (instance->dwType & (DIDFT_AXIS | DIDFT_POV)))
2229 reset_object_value( instance, impl );
2231 format->dwNumObjs++;
2232 return DIENUM_CONTINUE;
2235 HRESULT dinput_device_init( IDirectInputDevice8W *iface )
2237 struct dinput_device *impl = impl_from_IDirectInputDevice8W( iface );
2238 DIDATAFORMAT *format = impl->device_format;
2239 ULONG i, size;
2241 IDirectInputDevice8_EnumObjects( iface, enum_objects_init, iface, DIDFT_ALL );
2242 if (format->dwDataSize > DEVICE_STATE_MAX_SIZE)
2244 FIXME( "unable to create device, state is too large\n" );
2245 return DIERR_OUTOFMEMORY;
2248 size = format->dwNumObjs * sizeof(*format->rgodf);
2249 if (!(format->rgodf = calloc( 1, size ))) return DIERR_OUTOFMEMORY;
2251 format->dwSize = sizeof(*format);
2252 format->dwObjSize = sizeof(*format->rgodf);
2253 format->dwFlags = DIDF_ABSAXIS;
2254 format->dwNumObjs = 0;
2255 IDirectInputDevice8_EnumObjects( iface, enum_objects_init, iface, DIDFT_ALL );
2257 if (TRACE_ON( dinput ))
2259 TRACE( "device format %s\n", debugstr_didataformat( format ) );
2260 for (i = 0; i < format->dwNumObjs; ++i) TRACE( " %u: object %s\n", i, debugstr_diobjectdataformat( format->rgodf + i ) );
2263 return DI_OK;