Release 6.15.
[wine.git] / dlls / dinput / device.c
blobac60a21326c817816e694fe6672490c184b5c987
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.
27 #include "config.h"
29 #include <stdarg.h>
30 #include <string.h>
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winreg.h"
36 #include "winuser.h"
37 #include "winerror.h"
38 #include "dinput.h"
39 #include "device_private.h"
40 #include "dinput_private.h"
42 #define WM_WINE_NOTIFY_ACTIVITY WM_USER
44 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
46 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
48 return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface);
51 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(IDirectInputDeviceImpl *This)
53 return &This->IDirectInputDevice8A_iface;
55 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(IDirectInputDeviceImpl *This)
57 return &This->IDirectInputDevice8W_iface;
60 /******************************************************************************
61 * Various debugging tools
63 static void _dump_cooperativelevel_DI(DWORD dwFlags) {
64 if (TRACE_ON(dinput)) {
65 unsigned int i;
66 static const struct {
67 DWORD mask;
68 const char *name;
69 } flags[] = {
70 #define FE(x) { x, #x}
71 FE(DISCL_BACKGROUND),
72 FE(DISCL_EXCLUSIVE),
73 FE(DISCL_FOREGROUND),
74 FE(DISCL_NONEXCLUSIVE),
75 FE(DISCL_NOWINKEY)
76 #undef FE
78 TRACE(" cooperative level : ");
79 for (i = 0; i < ARRAY_SIZE(flags); i++)
80 if (flags[i].mask & dwFlags)
81 TRACE("%s ",flags[i].name);
82 TRACE("\n");
86 static void _dump_ObjectDataFormat_flags(DWORD dwFlags) {
87 unsigned int i;
88 static const struct {
89 DWORD mask;
90 const char *name;
91 } flags[] = {
92 #define FE(x) { x, #x}
93 FE(DIDOI_FFACTUATOR),
94 FE(DIDOI_FFEFFECTTRIGGER),
95 FE(DIDOI_POLLED),
96 FE(DIDOI_GUIDISUSAGE)
97 #undef FE
100 if (!dwFlags) return;
102 TRACE("Flags:");
104 /* First the flags */
105 for (i = 0; i < ARRAY_SIZE(flags); i++) {
106 if (flags[i].mask & dwFlags)
107 TRACE(" %s",flags[i].name);
110 /* Now specific values */
111 #define FE(x) case x: TRACE(" "#x); break
112 switch (dwFlags & DIDOI_ASPECTMASK) {
113 FE(DIDOI_ASPECTACCEL);
114 FE(DIDOI_ASPECTFORCE);
115 FE(DIDOI_ASPECTPOSITION);
116 FE(DIDOI_ASPECTVELOCITY);
118 #undef FE
122 static void _dump_EnumObjects_flags(DWORD dwFlags) {
123 if (TRACE_ON(dinput)) {
124 unsigned int i;
125 DWORD type, instance;
126 static const struct {
127 DWORD mask;
128 const char *name;
129 } flags[] = {
130 #define FE(x) { x, #x}
131 FE(DIDFT_RELAXIS),
132 FE(DIDFT_ABSAXIS),
133 FE(DIDFT_PSHBUTTON),
134 FE(DIDFT_TGLBUTTON),
135 FE(DIDFT_POV),
136 FE(DIDFT_COLLECTION),
137 FE(DIDFT_NODATA),
138 FE(DIDFT_FFACTUATOR),
139 FE(DIDFT_FFEFFECTTRIGGER),
140 FE(DIDFT_OUTPUT),
141 FE(DIDFT_VENDORDEFINED),
142 FE(DIDFT_ALIAS),
143 FE(DIDFT_OPTIONAL)
144 #undef FE
146 type = (dwFlags & 0xFF0000FF);
147 instance = ((dwFlags >> 8) & 0xFFFF);
148 TRACE("Type:");
149 if (type == DIDFT_ALL) {
150 TRACE(" DIDFT_ALL");
151 } else {
152 for (i = 0; i < ARRAY_SIZE(flags); i++) {
153 if (flags[i].mask & type) {
154 type &= ~flags[i].mask;
155 TRACE(" %s",flags[i].name);
158 if (type) {
159 TRACE(" (unhandled: %08x)", type);
162 TRACE(" / Instance: ");
163 if (instance == ((DIDFT_ANYINSTANCE >> 8) & 0xFFFF)) {
164 TRACE("DIDFT_ANYINSTANCE");
165 } else {
166 TRACE("%3d", instance);
171 void _dump_DIPROPHEADER(LPCDIPROPHEADER diph) {
172 if (TRACE_ON(dinput)) {
173 TRACE(" - dwObj = 0x%08x\n", diph->dwObj);
174 TRACE(" - dwHow = %s\n",
175 ((diph->dwHow == DIPH_DEVICE) ? "DIPH_DEVICE" :
176 ((diph->dwHow == DIPH_BYOFFSET) ? "DIPH_BYOFFSET" :
177 ((diph->dwHow == DIPH_BYID)) ? "DIPH_BYID" : "unknown")));
181 void _dump_OBJECTINSTANCEA(const DIDEVICEOBJECTINSTANCEA *ddoi) {
182 TRACE(" - enumerating : %s ('%s') - %2d - 0x%08x - %s - 0x%x\n",
183 debugstr_guid(&ddoi->guidType), _dump_dinput_GUID(&ddoi->guidType), ddoi->dwOfs, ddoi->dwType, ddoi->tszName, ddoi->dwFlags);
186 void _dump_OBJECTINSTANCEW(const DIDEVICEOBJECTINSTANCEW *ddoi) {
187 TRACE(" - enumerating : %s ('%s'), - %2d - 0x%08x - %s - 0x%x\n",
188 debugstr_guid(&ddoi->guidType), _dump_dinput_GUID(&ddoi->guidType), ddoi->dwOfs, ddoi->dwType, debugstr_w(ddoi->tszName), ddoi->dwFlags);
191 /* This function is a helper to convert a GUID into any possible DInput GUID out there */
192 const char *_dump_dinput_GUID(const GUID *guid) {
193 unsigned int i;
194 static const struct {
195 const GUID *guid;
196 const char *name;
197 } guids[] = {
198 #define FE(x) { &x, #x}
199 FE(GUID_XAxis),
200 FE(GUID_YAxis),
201 FE(GUID_ZAxis),
202 FE(GUID_RxAxis),
203 FE(GUID_RyAxis),
204 FE(GUID_RzAxis),
205 FE(GUID_Slider),
206 FE(GUID_Button),
207 FE(GUID_Key),
208 FE(GUID_POV),
209 FE(GUID_Unknown),
210 FE(GUID_SysMouse),
211 FE(GUID_SysKeyboard),
212 FE(GUID_Joystick),
213 FE(GUID_ConstantForce),
214 FE(GUID_RampForce),
215 FE(GUID_Square),
216 FE(GUID_Sine),
217 FE(GUID_Triangle),
218 FE(GUID_SawtoothUp),
219 FE(GUID_SawtoothDown),
220 FE(GUID_Spring),
221 FE(GUID_Damper),
222 FE(GUID_Inertia),
223 FE(GUID_Friction),
224 FE(GUID_CustomForce)
225 #undef FE
227 if (guid == NULL)
228 return "null GUID";
229 for (i = 0; i < ARRAY_SIZE(guids); i++) {
230 if (IsEqualGUID(guids[i].guid, guid)) {
231 return guids[i].name;
234 return debugstr_guid(guid);
237 void _dump_DIDATAFORMAT(const DIDATAFORMAT *df) {
238 unsigned int i;
240 TRACE("Dumping DIDATAFORMAT structure:\n");
241 TRACE(" - dwSize: %d\n", df->dwSize);
242 if (df->dwSize != sizeof(DIDATAFORMAT)) {
243 WARN("Non-standard DIDATAFORMAT structure size %d\n", df->dwSize);
245 TRACE(" - dwObjsize: %d\n", df->dwObjSize);
246 if (df->dwObjSize != sizeof(DIOBJECTDATAFORMAT)) {
247 WARN("Non-standard DIOBJECTDATAFORMAT structure size %d\n", df->dwObjSize);
249 TRACE(" - dwFlags: 0x%08x (", df->dwFlags);
250 switch (df->dwFlags) {
251 case DIDF_ABSAXIS: TRACE("DIDF_ABSAXIS"); break;
252 case DIDF_RELAXIS: TRACE("DIDF_RELAXIS"); break;
253 default: TRACE("unknown"); break;
255 TRACE(")\n");
256 TRACE(" - dwDataSize: %d\n", df->dwDataSize);
257 TRACE(" - dwNumObjs: %d\n", df->dwNumObjs);
259 for (i = 0; i < df->dwNumObjs; i++) {
260 TRACE(" - Object %d:\n", i);
261 TRACE(" * GUID: %s ('%s')\n", debugstr_guid(df->rgodf[i].pguid), _dump_dinput_GUID(df->rgodf[i].pguid));
262 TRACE(" * dwOfs: %d\n", df->rgodf[i].dwOfs);
263 TRACE(" * dwType: 0x%08x\n", df->rgodf[i].dwType);
264 TRACE(" "); _dump_EnumObjects_flags(df->rgodf[i].dwType); TRACE("\n");
265 TRACE(" * dwFlags: 0x%08x\n", df->rgodf[i].dwFlags);
266 TRACE(" "); _dump_ObjectDataFormat_flags(df->rgodf[i].dwFlags); TRACE("\n");
270 /******************************************************************************
271 * Get the default and the app-specific config keys.
273 BOOL get_app_key(HKEY *defkey, HKEY *appkey)
275 char buffer[MAX_PATH+16];
276 DWORD len;
278 *appkey = 0;
280 /* @@ Wine registry key: HKCU\Software\Wine\DirectInput */
281 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\DirectInput", defkey))
282 *defkey = 0;
284 len = GetModuleFileNameA(0, buffer, MAX_PATH);
285 if (len && len < MAX_PATH)
287 HKEY tmpkey;
289 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectInput */
290 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
292 char *p, *appname = buffer;
293 if ((p = strrchr(appname, '/'))) appname = p + 1;
294 if ((p = strrchr(appname, '\\'))) appname = p + 1;
295 strcat(appname, "\\DirectInput");
297 if (RegOpenKeyA(tmpkey, appname, appkey)) *appkey = 0;
298 RegCloseKey(tmpkey);
302 return *defkey || *appkey;
305 /******************************************************************************
306 * Get a config key from either the app-specific or the default config
308 DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
309 char *buffer, DWORD size )
311 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size ))
312 return 0;
314 if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size ))
315 return 0;
317 return ERROR_FILE_NOT_FOUND;
320 /* Conversion between internal data buffer and external data buffer */
321 void fill_DataFormat(void *out, DWORD size, const void *in, const DataFormat *df)
323 int i;
324 const char *in_c = in;
325 char *out_c = out;
327 memset(out, 0, size);
328 if (df->dt == NULL) {
329 /* This means that the app uses Wine's internal data format */
330 memcpy(out, in, min(size, df->internal_format_size));
331 } else {
332 for (i = 0; i < df->size; i++) {
333 if (df->dt[i].offset_in >= 0) {
334 switch (df->dt[i].size) {
335 case 1:
336 TRACE("Copying (c) to %d from %d (value %d)\n",
337 df->dt[i].offset_out, df->dt[i].offset_in, *(in_c + df->dt[i].offset_in));
338 *(out_c + df->dt[i].offset_out) = *(in_c + df->dt[i].offset_in);
339 break;
341 case 2:
342 TRACE("Copying (s) to %d from %d (value %d)\n",
343 df->dt[i].offset_out, df->dt[i].offset_in, *((const short *)(in_c + df->dt[i].offset_in)));
344 *((short *)(out_c + df->dt[i].offset_out)) = *((const short *)(in_c + df->dt[i].offset_in));
345 break;
347 case 4:
348 TRACE("Copying (i) to %d from %d (value %d)\n",
349 df->dt[i].offset_out, df->dt[i].offset_in, *((const int *)(in_c + df->dt[i].offset_in)));
350 *((int *)(out_c + df->dt[i].offset_out)) = *((const int *)(in_c + df->dt[i].offset_in));
351 break;
353 default:
354 memcpy((out_c + df->dt[i].offset_out), (in_c + df->dt[i].offset_in), df->dt[i].size);
355 break;
357 } else {
358 switch (df->dt[i].size) {
359 case 1:
360 TRACE("Copying (c) to %d default value %d\n",
361 df->dt[i].offset_out, df->dt[i].value);
362 *(out_c + df->dt[i].offset_out) = (char) df->dt[i].value;
363 break;
365 case 2:
366 TRACE("Copying (s) to %d default value %d\n",
367 df->dt[i].offset_out, df->dt[i].value);
368 *((short *) (out_c + df->dt[i].offset_out)) = (short) df->dt[i].value;
369 break;
371 case 4:
372 TRACE("Copying (i) to %d default value %d\n",
373 df->dt[i].offset_out, df->dt[i].value);
374 *((int *) (out_c + df->dt[i].offset_out)) = df->dt[i].value;
375 break;
377 default:
378 memset((out_c + df->dt[i].offset_out), 0, df->dt[i].size);
379 break;
386 void release_DataFormat(DataFormat * format)
388 TRACE("Deleting DataFormat: %p\n", format);
390 HeapFree(GetProcessHeap(), 0, format->dt);
391 format->dt = NULL;
392 HeapFree(GetProcessHeap(), 0, format->offsets);
393 format->offsets = NULL;
394 HeapFree(GetProcessHeap(), 0, format->user_df);
395 format->user_df = NULL;
398 static inline LPDIOBJECTDATAFORMAT dataformat_to_odf(LPCDIDATAFORMAT df, int idx)
400 if (idx < 0 || idx >= df->dwNumObjs) return NULL;
401 return (LPDIOBJECTDATAFORMAT)((LPBYTE)df->rgodf + idx * df->dwObjSize);
404 /* dataformat_to_odf_by_type
405 * Find the Nth object of the selected type in the DataFormat
407 LPDIOBJECTDATAFORMAT dataformat_to_odf_by_type(LPCDIDATAFORMAT df, int n, DWORD type)
409 int i, nfound = 0;
411 for (i=0; i < df->dwNumObjs; i++)
413 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(df, i);
415 if (odf->dwType & type)
417 if (n == nfound)
418 return odf;
420 nfound++;
424 return NULL;
427 static HRESULT create_DataFormat(LPCDIDATAFORMAT asked_format, DataFormat *format)
429 DataTransform *dt;
430 unsigned int i, j;
431 int same = 1;
432 int *done;
433 int index = 0;
434 DWORD next = 0;
436 if (!format->wine_df) return DIERR_INVALIDPARAM;
437 done = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, asked_format->dwNumObjs * sizeof(int));
438 dt = HeapAlloc(GetProcessHeap(), 0, asked_format->dwNumObjs * sizeof(DataTransform));
439 if (!dt || !done) goto failed;
441 if (!(format->offsets = HeapAlloc(GetProcessHeap(), 0, format->wine_df->dwNumObjs * sizeof(int))))
442 goto failed;
444 if (!(format->user_df = HeapAlloc(GetProcessHeap(), 0, asked_format->dwSize)))
445 goto failed;
446 memcpy(format->user_df, asked_format, asked_format->dwSize);
448 TRACE("Creating DataTransform :\n");
450 for (i = 0; i < format->wine_df->dwNumObjs; i++)
452 format->offsets[i] = -1;
454 for (j = 0; j < asked_format->dwNumObjs; j++) {
455 if (done[j] == 1)
456 continue;
458 if (/* Check if the application either requests any GUID and if not, it if matches
459 * the GUID of the Wine object.
461 ((asked_format->rgodf[j].pguid == NULL) ||
462 (format->wine_df->rgodf[i].pguid == NULL) ||
463 (IsEqualGUID(format->wine_df->rgodf[i].pguid, asked_format->rgodf[j].pguid)))
465 (/* Then check if it accepts any instance id, and if not, if it matches Wine's
466 * instance id.
468 ((asked_format->rgodf[j].dwType & DIDFT_INSTANCEMASK) == DIDFT_ANYINSTANCE) ||
469 (DIDFT_GETINSTANCE(asked_format->rgodf[j].dwType) == 0x00FF) || /* This is mentioned in no DX docs, but it works fine - tested on WinXP */
470 (DIDFT_GETINSTANCE(asked_format->rgodf[j].dwType) == DIDFT_GETINSTANCE(format->wine_df->rgodf[i].dwType)))
472 ( /* Then if the asked type matches the one Wine provides */
473 DIDFT_GETTYPE(asked_format->rgodf[j].dwType) & format->wine_df->rgodf[i].dwType))
475 done[j] = 1;
477 TRACE("Matching :\n");
478 TRACE(" - Asked (%d) :\n", j);
479 TRACE(" * GUID: %s ('%s')\n",
480 debugstr_guid(asked_format->rgodf[j].pguid),
481 _dump_dinput_GUID(asked_format->rgodf[j].pguid));
482 TRACE(" * Offset: %3d\n", asked_format->rgodf[j].dwOfs);
483 TRACE(" * dwType: 0x%08x\n", asked_format->rgodf[j].dwType);
484 TRACE(" "); _dump_EnumObjects_flags(asked_format->rgodf[j].dwType); TRACE("\n");
485 TRACE(" * dwFlags: 0x%08x\n", asked_format->rgodf[j].dwFlags);
486 TRACE(" "); _dump_ObjectDataFormat_flags(asked_format->rgodf[j].dwFlags); TRACE("\n");
488 TRACE(" - Wine (%d) :\n", i);
489 TRACE(" * GUID: %s ('%s')\n",
490 debugstr_guid(format->wine_df->rgodf[i].pguid),
491 _dump_dinput_GUID(format->wine_df->rgodf[i].pguid));
492 TRACE(" * Offset: %3d\n", format->wine_df->rgodf[i].dwOfs);
493 TRACE(" * dwType: 0x%08x\n", format->wine_df->rgodf[i].dwType);
494 TRACE(" "); _dump_EnumObjects_flags(format->wine_df->rgodf[i].dwType); TRACE("\n");
495 TRACE(" * dwFlags: 0x%08x\n", format->wine_df->rgodf[i].dwFlags);
496 TRACE(" "); _dump_ObjectDataFormat_flags(format->wine_df->rgodf[i].dwFlags); TRACE("\n");
498 if (format->wine_df->rgodf[i].dwType & DIDFT_BUTTON)
499 dt[index].size = sizeof(BYTE);
500 else
501 dt[index].size = sizeof(DWORD);
502 dt[index].offset_in = format->wine_df->rgodf[i].dwOfs;
503 dt[index].offset_out = asked_format->rgodf[j].dwOfs;
504 format->offsets[i] = asked_format->rgodf[j].dwOfs;
505 dt[index].value = 0;
506 next = next + dt[index].size;
508 if (format->wine_df->rgodf[i].dwOfs != dt[index].offset_out)
509 same = 0;
511 index++;
512 break;
517 TRACE("Setting to default value :\n");
518 for (j = 0; j < asked_format->dwNumObjs; j++) {
519 if (done[j] == 0) {
520 TRACE(" - Asked (%d) :\n", j);
521 TRACE(" * GUID: %s ('%s')\n",
522 debugstr_guid(asked_format->rgodf[j].pguid),
523 _dump_dinput_GUID(asked_format->rgodf[j].pguid));
524 TRACE(" * Offset: %3d\n", asked_format->rgodf[j].dwOfs);
525 TRACE(" * dwType: 0x%08x\n", asked_format->rgodf[j].dwType);
526 TRACE(" "); _dump_EnumObjects_flags(asked_format->rgodf[j].dwType); TRACE("\n");
527 TRACE(" * dwFlags: 0x%08x\n", asked_format->rgodf[j].dwFlags);
528 TRACE(" "); _dump_ObjectDataFormat_flags(asked_format->rgodf[j].dwFlags); TRACE("\n");
530 same = 0;
532 if (!(asked_format->rgodf[j].dwType & DIDFT_POV))
533 continue; /* fill_DataFormat memsets the buffer to 0 */
535 if (asked_format->rgodf[j].dwType & DIDFT_BUTTON)
536 dt[index].size = sizeof(BYTE);
537 else
538 dt[index].size = sizeof(DWORD);
539 dt[index].offset_in = -1;
540 dt[index].offset_out = asked_format->rgodf[j].dwOfs;
541 dt[index].value = -1;
542 index++;
546 format->internal_format_size = format->wine_df->dwDataSize;
547 format->size = index;
548 if (same) {
549 HeapFree(GetProcessHeap(), 0, dt);
550 dt = NULL;
552 format->dt = dt;
554 HeapFree(GetProcessHeap(), 0, done);
556 return DI_OK;
558 failed:
559 HeapFree(GetProcessHeap(), 0, done);
560 HeapFree(GetProcessHeap(), 0, dt);
561 format->dt = NULL;
562 HeapFree(GetProcessHeap(), 0, format->offsets);
563 format->offsets = NULL;
564 HeapFree(GetProcessHeap(), 0, format->user_df);
565 format->user_df = NULL;
567 return DIERR_OUTOFMEMORY;
570 static int verify_offset(const DataFormat *df, int offset)
572 int i;
574 if (!df->offsets)
575 return -1;
577 for (i = df->wine_df->dwNumObjs - 1; i >= 0; i--)
579 if (df->offsets[i] == offset)
580 return offset;
583 return -1;
586 /* find an object by its offset in a data format */
587 static int offset_to_object(const DataFormat *df, int offset)
589 int i;
591 if (!df->offsets) return -1;
593 for (i = 0; i < df->wine_df->dwNumObjs; i++)
594 if (df->offsets[i] == offset) return i;
596 return -1;
599 int id_to_object(LPCDIDATAFORMAT df, int id)
601 int i;
603 id &= 0x00ffffff;
604 for (i = 0; i < df->dwNumObjs; i++)
605 if ((dataformat_to_odf(df, i)->dwType & 0x00ffffff) == id)
606 return i;
608 return -1;
611 static int id_to_offset(const DataFormat *df, int id)
613 int obj = id_to_object(df->wine_df, id);
615 return obj >= 0 && df->offsets ? df->offsets[obj] : -1;
618 int find_property(const DataFormat *df, LPCDIPROPHEADER ph)
620 switch (ph->dwHow)
622 case DIPH_BYID: return id_to_object(df->wine_df, ph->dwObj);
623 case DIPH_BYOFFSET: return offset_to_object(df, ph->dwObj);
625 FIXME("Unhandled ph->dwHow=='%04X'\n", (unsigned int)ph->dwHow);
627 return -1;
630 static DWORD semantic_to_obj_id(IDirectInputDeviceImpl* This, DWORD dwSemantic)
632 DWORD type = (0x0000ff00 & dwSemantic) >> 8;
633 DWORD offset = 0x000000ff & dwSemantic;
634 DWORD obj_instance = 0;
635 BOOL found = FALSE;
636 int i;
638 for (i = 0; i < This->data_format.wine_df->dwNumObjs; i++)
640 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(This->data_format.wine_df, i);
642 if (odf->dwOfs == offset)
644 obj_instance = DIDFT_GETINSTANCE(odf->dwType);
645 found = TRUE;
646 break;
650 if (!found) return 0;
652 if (type & DIDFT_AXIS) type = DIDFT_RELAXIS;
653 if (type & DIDFT_BUTTON) type = DIDFT_PSHBUTTON;
655 return type | (0x0000ff00 & (obj_instance << 8));
659 * get_mapping_key
660 * Retrieves an open registry key to save the mapping, parametrized for an username,
661 * specific device and specific action mapping guid.
663 static HKEY get_mapping_key(const WCHAR *device, const WCHAR *username, const WCHAR *guid)
665 static const WCHAR subkey[] = {
666 'S','o','f','t','w','a','r','e','\\',
667 'W','i','n','e','\\',
668 'D','i','r','e','c','t','I','n','p','u','t','\\',
669 'M','a','p','p','i','n','g','s','\\','%','s','\\','%','s','\\','%','s','\0'};
670 HKEY hkey;
671 WCHAR *keyname;
673 keyname = HeapAlloc(GetProcessHeap(), 0,
674 sizeof(WCHAR) * (lstrlenW(subkey) + strlenW(username) + strlenW(device) + strlenW(guid)));
675 sprintfW(keyname, subkey, username, device, guid);
677 /* The key used is HKCU\Software\Wine\DirectInput\Mappings\[username]\[device]\[mapping_guid] */
678 if (RegCreateKeyW(HKEY_CURRENT_USER, keyname, &hkey))
679 hkey = 0;
681 HeapFree(GetProcessHeap(), 0, keyname);
683 return hkey;
686 static HRESULT save_mapping_settings(IDirectInputDevice8W *iface, LPDIACTIONFORMATW lpdiaf, LPCWSTR lpszUsername)
688 WCHAR *guid_str = NULL;
689 DIDEVICEINSTANCEW didev;
690 HKEY hkey;
691 int i;
693 didev.dwSize = sizeof(didev);
694 IDirectInputDevice8_GetDeviceInfo(iface, &didev);
696 if (StringFromCLSID(&lpdiaf->guidActionMap, &guid_str) != S_OK)
697 return DI_SETTINGSNOTSAVED;
699 hkey = get_mapping_key(didev.tszInstanceName, lpszUsername, guid_str);
701 if (!hkey)
703 CoTaskMemFree(guid_str);
704 return DI_SETTINGSNOTSAVED;
707 /* Write each of the actions mapped for this device.
708 Format is "dwSemantic"="dwObjID" and key is of type REG_DWORD
710 for (i = 0; i < lpdiaf->dwNumActions; i++)
712 static const WCHAR format[] = {'%','x','\0'};
713 WCHAR label[9];
715 if (IsEqualGUID(&didev.guidInstance, &lpdiaf->rgoAction[i].guidInstance) &&
716 lpdiaf->rgoAction[i].dwHow != DIAH_UNMAPPED)
718 sprintfW(label, format, lpdiaf->rgoAction[i].dwSemantic);
719 RegSetValueExW(hkey, label, 0, REG_DWORD, (const BYTE*) &lpdiaf->rgoAction[i].dwObjID, sizeof(DWORD));
723 RegCloseKey(hkey);
724 CoTaskMemFree(guid_str);
726 return DI_OK;
729 static BOOL load_mapping_settings(IDirectInputDeviceImpl *This, LPDIACTIONFORMATW lpdiaf, const WCHAR *username)
731 HKEY hkey;
732 WCHAR *guid_str;
733 DIDEVICEINSTANCEW didev;
734 int i, mapped = 0;
736 didev.dwSize = sizeof(didev);
737 IDirectInputDevice8_GetDeviceInfo(&This->IDirectInputDevice8W_iface, &didev);
739 if (StringFromCLSID(&lpdiaf->guidActionMap, &guid_str) != S_OK)
740 return FALSE;
742 hkey = get_mapping_key(didev.tszInstanceName, username, guid_str);
744 if (!hkey)
746 CoTaskMemFree(guid_str);
747 return FALSE;
750 /* Try to read each action in the DIACTIONFORMAT from registry */
751 for (i = 0; i < lpdiaf->dwNumActions; i++)
753 static const WCHAR format[] = {'%','x','\0'};
754 DWORD id, size = sizeof(DWORD);
755 WCHAR label[9];
757 sprintfW(label, format, lpdiaf->rgoAction[i].dwSemantic);
759 if (!RegQueryValueExW(hkey, label, 0, NULL, (LPBYTE) &id, &size))
761 lpdiaf->rgoAction[i].dwObjID = id;
762 lpdiaf->rgoAction[i].guidInstance = didev.guidInstance;
763 lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
764 mapped += 1;
768 RegCloseKey(hkey);
769 CoTaskMemFree(guid_str);
771 return mapped > 0;
774 static BOOL set_app_data(IDirectInputDeviceImpl *dev, int offset, UINT_PTR app_data)
776 int num_actions = dev->num_actions;
777 ActionMap *action_map = dev->action_map, *target_map = NULL;
779 if (num_actions == 0)
781 num_actions = 1;
782 action_map = HeapAlloc(GetProcessHeap(), 0, sizeof(ActionMap));
783 if (!action_map) return FALSE;
784 target_map = &action_map[0];
786 else
788 int i;
789 for (i = 0; i < num_actions; i++)
791 if (dev->action_map[i].offset != offset) continue;
792 target_map = &dev->action_map[i];
793 break;
796 if (!target_map)
798 num_actions++;
799 action_map = HeapReAlloc(GetProcessHeap(), 0, action_map, sizeof(ActionMap)*num_actions);
800 if (!action_map) return FALSE;
801 target_map = &action_map[num_actions-1];
805 target_map->offset = offset;
806 target_map->uAppData = app_data;
808 dev->action_map = action_map;
809 dev->num_actions = num_actions;
811 return TRUE;
814 HRESULT _build_action_map(LPDIRECTINPUTDEVICE8W iface, LPDIACTIONFORMATW lpdiaf, LPCWSTR lpszUserName, DWORD dwFlags, DWORD devMask, LPCDIDATAFORMAT df)
816 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
817 WCHAR username[MAX_PATH];
818 DWORD username_size = MAX_PATH;
819 int i;
820 BOOL load_success = FALSE, has_actions = FALSE;
822 /* Unless asked the contrary by these flags, try to load a previous mapping */
823 if (!(dwFlags & DIDBAM_HWDEFAULTS))
825 /* Retrieve logged user name if necessary */
826 if (lpszUserName == NULL)
827 GetUserNameW(username, &username_size);
828 else
829 lstrcpynW(username, lpszUserName, MAX_PATH);
831 load_success = load_mapping_settings(This, lpdiaf, username);
834 if (load_success) return DI_OK;
836 for (i=0; i < lpdiaf->dwNumActions; i++)
838 /* Don't touch a user configured action */
839 if (lpdiaf->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
841 if ((lpdiaf->rgoAction[i].dwSemantic & devMask) == devMask)
843 DWORD obj_id = semantic_to_obj_id(This, lpdiaf->rgoAction[i].dwSemantic);
844 DWORD type = DIDFT_GETTYPE(obj_id);
845 DWORD inst = DIDFT_GETINSTANCE(obj_id);
847 LPDIOBJECTDATAFORMAT odf;
849 if (type == DIDFT_PSHBUTTON) type = DIDFT_BUTTON;
850 if (type == DIDFT_RELAXIS) type = DIDFT_AXIS;
852 /* Make sure the object exists */
853 odf = dataformat_to_odf_by_type(df, inst, type);
855 if (odf != NULL)
857 lpdiaf->rgoAction[i].dwObjID = obj_id;
858 lpdiaf->rgoAction[i].guidInstance = This->guid;
859 lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
860 has_actions = TRUE;
863 else if (!(dwFlags & DIDBAM_PRESERVE))
865 /* We must clear action data belonging to other devices */
866 memset(&lpdiaf->rgoAction[i].guidInstance, 0, sizeof(GUID));
867 lpdiaf->rgoAction[i].dwHow = DIAH_UNMAPPED;
871 if (!has_actions) return DI_NOEFFECT;
873 return IDirectInputDevice8WImpl_BuildActionMap(iface, lpdiaf, lpszUserName, dwFlags);
876 HRESULT _set_action_map(LPDIRECTINPUTDEVICE8W iface, LPDIACTIONFORMATW lpdiaf, LPCWSTR lpszUserName, DWORD dwFlags, LPCDIDATAFORMAT df)
878 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
879 DIDATAFORMAT data_format;
880 DIOBJECTDATAFORMAT *obj_df = NULL;
881 DIPROPDWORD dp;
882 DIPROPRANGE dpr;
883 DIPROPSTRING dps;
884 WCHAR username[MAX_PATH];
885 DWORD username_size = MAX_PATH;
886 int i, action = 0, num_actions = 0;
887 unsigned int offset = 0;
888 ActionMap *action_map;
890 if (This->acquired) return DIERR_ACQUIRED;
892 data_format.dwSize = sizeof(data_format);
893 data_format.dwObjSize = sizeof(DIOBJECTDATAFORMAT);
894 data_format.dwFlags = DIDF_RELAXIS;
895 data_format.dwDataSize = lpdiaf->dwDataSize;
897 /* Count the actions */
898 for (i=0; i < lpdiaf->dwNumActions; i++)
899 if (IsEqualGUID(&This->guid, &lpdiaf->rgoAction[i].guidInstance))
900 num_actions++;
902 if (num_actions == 0) return DI_NOEFFECT;
904 /* Construct the dataformat and actionmap */
905 obj_df = HeapAlloc(GetProcessHeap(), 0, sizeof(DIOBJECTDATAFORMAT)*num_actions);
906 data_format.rgodf = (LPDIOBJECTDATAFORMAT)obj_df;
907 data_format.dwNumObjs = num_actions;
909 action_map = HeapAlloc(GetProcessHeap(), 0, sizeof(ActionMap)*num_actions);
911 for (i = 0; i < lpdiaf->dwNumActions; i++)
913 if (IsEqualGUID(&This->guid, &lpdiaf->rgoAction[i].guidInstance))
915 DWORD inst = DIDFT_GETINSTANCE(lpdiaf->rgoAction[i].dwObjID);
916 DWORD type = DIDFT_GETTYPE(lpdiaf->rgoAction[i].dwObjID);
917 LPDIOBJECTDATAFORMAT obj;
919 if (type == DIDFT_PSHBUTTON) type = DIDFT_BUTTON;
920 if (type == DIDFT_RELAXIS) type = DIDFT_AXIS;
922 obj = dataformat_to_odf_by_type(df, inst, type);
924 memcpy(&obj_df[action], obj, df->dwObjSize);
926 action_map[action].uAppData = lpdiaf->rgoAction[i].uAppData;
927 action_map[action].offset = offset;
928 obj_df[action].dwOfs = offset;
929 offset += (type & DIDFT_BUTTON) ? 1 : 4;
931 action++;
935 IDirectInputDevice8_SetDataFormat(iface, &data_format);
937 This->action_map = action_map;
938 This->num_actions = num_actions;
940 HeapFree(GetProcessHeap(), 0, obj_df);
942 /* Set the device properties according to the action format */
943 dpr.diph.dwSize = sizeof(DIPROPRANGE);
944 dpr.lMin = lpdiaf->lAxisMin;
945 dpr.lMax = lpdiaf->lAxisMax;
946 dpr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
947 dpr.diph.dwHow = DIPH_DEVICE;
948 IDirectInputDevice8_SetProperty(iface, DIPROP_RANGE, &dpr.diph);
950 if (lpdiaf->dwBufferSize > 0)
952 dp.diph.dwSize = sizeof(DIPROPDWORD);
953 dp.dwData = lpdiaf->dwBufferSize;
954 dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
955 dp.diph.dwHow = DIPH_DEVICE;
956 IDirectInputDevice8_SetProperty(iface, DIPROP_BUFFERSIZE, &dp.diph);
959 /* Retrieve logged user name if necessary */
960 if (lpszUserName == NULL)
961 GetUserNameW(username, &username_size);
962 else
963 lstrcpynW(username, lpszUserName, MAX_PATH);
965 dps.diph.dwSize = sizeof(dps);
966 dps.diph.dwHeaderSize = sizeof(DIPROPHEADER);
967 dps.diph.dwObj = 0;
968 dps.diph.dwHow = DIPH_DEVICE;
969 if (dwFlags & DIDSAM_NOUSER)
970 dps.wsz[0] = '\0';
971 else
972 lstrcpynW(dps.wsz, username, ARRAY_SIZE(dps.wsz));
973 IDirectInputDevice8_SetProperty(iface, DIPROP_USERNAME, &dps.diph);
975 /* Save the settings to disk */
976 save_mapping_settings(iface, lpdiaf, username);
978 return DI_OK;
981 /******************************************************************************
982 * queue_event - add new event to the ring queue
985 void queue_event( IDirectInputDevice8W *iface, int inst_id, DWORD data, DWORD time, DWORD seq )
987 static ULONGLONG notify_ms = 0;
988 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W( iface );
989 int next_pos, ofs = id_to_offset(&This->data_format, inst_id);
990 ULONGLONG time_ms = GetTickCount64();
992 /* Event is being set regardless of the queue state */
993 if (This->hEvent) SetEvent(This->hEvent);
995 if (time_ms - notify_ms > 1000)
997 PostMessageW(GetDesktopWindow(), WM_WINE_NOTIFY_ACTIVITY, 0, 0);
998 notify_ms = time_ms;
1001 if (!This->queue_len || This->overflow || ofs < 0) return;
1003 next_pos = (This->queue_head + 1) % This->queue_len;
1004 if (next_pos == This->queue_tail)
1006 TRACE(" queue overflowed\n");
1007 This->overflow = TRUE;
1008 return;
1011 TRACE(" queueing %d at offset %d (queue head %d / size %d)\n",
1012 data, ofs, This->queue_head, This->queue_len);
1014 This->data_queue[This->queue_head].dwOfs = ofs;
1015 This->data_queue[This->queue_head].dwData = data;
1016 This->data_queue[This->queue_head].dwTimeStamp = time;
1017 This->data_queue[This->queue_head].dwSequence = seq;
1018 This->data_queue[This->queue_head].uAppData = -1;
1020 /* Set uAppData by means of action mapping */
1021 if (This->num_actions > 0)
1023 int i;
1024 for (i=0; i < This->num_actions; i++)
1026 if (This->action_map[i].offset == ofs)
1028 TRACE("Offset %d mapped to uAppData %lu\n", ofs, This->action_map[i].uAppData);
1029 This->data_queue[This->queue_head].uAppData = This->action_map[i].uAppData;
1030 break;
1035 This->queue_head = next_pos;
1036 /* Send event if asked */
1039 /******************************************************************************
1040 * Acquire
1043 HRESULT WINAPI IDirectInputDevice2WImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
1045 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1046 HRESULT res;
1048 TRACE("(%p)\n", This);
1050 if (!This->data_format.user_df) return DIERR_INVALIDPARAM;
1051 if (This->dwCoopLevel & DISCL_FOREGROUND && This->win != GetForegroundWindow())
1052 return DIERR_OTHERAPPHASPRIO;
1054 EnterCriticalSection(&This->crit);
1055 res = This->acquired ? S_FALSE : DI_OK;
1056 This->acquired = 1;
1057 LeaveCriticalSection(&This->crit);
1058 if (res != DI_OK) return res;
1060 dinput_hooks_acquire_device(iface);
1061 check_dinput_hooks(iface, TRUE);
1063 return res;
1066 /******************************************************************************
1067 * Unacquire
1070 HRESULT WINAPI IDirectInputDevice2WImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
1072 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1073 HRESULT res;
1075 TRACE("(%p)\n", This);
1077 EnterCriticalSection(&This->crit);
1078 res = !This->acquired ? DI_NOEFFECT : DI_OK;
1079 This->acquired = 0;
1080 LeaveCriticalSection(&This->crit);
1081 if (res != DI_OK) return res;
1083 dinput_hooks_unacquire_device(iface);
1084 check_dinput_hooks(iface, FALSE);
1086 return res;
1089 /******************************************************************************
1090 * IDirectInputDeviceA
1093 HRESULT WINAPI IDirectInputDevice2WImpl_SetDataFormat(LPDIRECTINPUTDEVICE8W iface, LPCDIDATAFORMAT df)
1095 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1096 HRESULT res = DI_OK;
1098 if (!df) return E_POINTER;
1099 TRACE("(%p) %p\n", This, df);
1100 _dump_DIDATAFORMAT(df);
1102 if (df->dwSize != sizeof(DIDATAFORMAT)) return DIERR_INVALIDPARAM;
1103 if (This->acquired) return DIERR_ACQUIRED;
1105 EnterCriticalSection(&This->crit);
1107 HeapFree(GetProcessHeap(), 0, This->action_map);
1108 This->action_map = NULL;
1109 This->num_actions = 0;
1111 release_DataFormat(&This->data_format);
1112 res = create_DataFormat(df, &This->data_format);
1114 LeaveCriticalSection(&This->crit);
1115 return res;
1118 /******************************************************************************
1119 * SetCooperativeLevel
1121 * Set cooperative level and the source window for the events.
1123 HRESULT WINAPI IDirectInputDevice2WImpl_SetCooperativeLevel(LPDIRECTINPUTDEVICE8W iface, HWND hwnd, DWORD dwflags)
1125 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1127 TRACE("(%p) %p,0x%08x\n", This, hwnd, dwflags);
1128 _dump_cooperativelevel_DI(dwflags);
1130 if ((dwflags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == 0 ||
1131 (dwflags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE) ||
1132 (dwflags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == 0 ||
1133 (dwflags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == (DISCL_FOREGROUND | DISCL_BACKGROUND))
1134 return DIERR_INVALIDPARAM;
1136 if (hwnd && GetWindowLongW(hwnd, GWL_STYLE) & WS_CHILD) return E_HANDLE;
1138 if (!hwnd && dwflags == (DISCL_NONEXCLUSIVE | DISCL_BACKGROUND))
1139 hwnd = GetDesktopWindow();
1141 if (!IsWindow(hwnd)) return E_HANDLE;
1143 /* For security reasons native does not allow exclusive background level
1144 for mouse and keyboard only */
1145 if (dwflags & DISCL_EXCLUSIVE && dwflags & DISCL_BACKGROUND &&
1146 (IsEqualGUID(&This->guid, &GUID_SysMouse) ||
1147 IsEqualGUID(&This->guid, &GUID_SysKeyboard)))
1148 return DIERR_UNSUPPORTED;
1150 /* Store the window which asks for the mouse */
1151 EnterCriticalSection(&This->crit);
1152 This->win = hwnd;
1153 This->dwCoopLevel = dwflags;
1154 LeaveCriticalSection(&This->crit);
1156 return DI_OK;
1159 /******************************************************************************
1160 * SetEventNotification : specifies event to be sent on state change
1162 HRESULT WINAPI IDirectInputDevice2WImpl_SetEventNotification(LPDIRECTINPUTDEVICE8W iface, HANDLE event)
1164 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1166 TRACE("(%p) %p\n", This, event);
1168 EnterCriticalSection(&This->crit);
1169 This->hEvent = event;
1170 LeaveCriticalSection(&This->crit);
1171 return DI_OK;
1175 ULONG WINAPI IDirectInputDevice2WImpl_Release(LPDIRECTINPUTDEVICE8W iface)
1177 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1178 ULONG ref = InterlockedDecrement(&(This->ref));
1180 TRACE("(%p) ref %d\n", This, ref);
1182 if (ref) return ref;
1184 IDirectInputDevice_Unacquire(iface);
1185 /* Reset the FF state, free all effects, etc */
1186 IDirectInputDevice8_SendForceFeedbackCommand(iface, DISFFC_RESET);
1188 HeapFree(GetProcessHeap(), 0, This->data_queue);
1190 /* Free data format */
1191 HeapFree(GetProcessHeap(), 0, This->data_format.wine_df->rgodf);
1192 HeapFree(GetProcessHeap(), 0, This->data_format.wine_df);
1193 release_DataFormat(&This->data_format);
1195 /* Free action mapping */
1196 HeapFree(GetProcessHeap(), 0, This->action_map);
1198 IDirectInput_Release(&This->dinput->IDirectInput7A_iface);
1199 This->crit.DebugInfo->Spare[0] = 0;
1200 DeleteCriticalSection(&This->crit);
1202 HeapFree(GetProcessHeap(), 0, This);
1204 return ref;
1207 HRESULT WINAPI IDirectInputDevice2WImpl_QueryInterface(LPDIRECTINPUTDEVICE8W iface, REFIID riid, LPVOID *ppobj)
1209 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1211 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppobj);
1213 if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
1214 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
1215 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
1216 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
1218 IDirectInputDevice2_AddRef(iface);
1219 *ppobj = IDirectInputDevice8A_from_impl(This);
1220 return DI_OK;
1223 if (IsEqualGUID(&IID_IUnknown, riid) ||
1224 IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
1225 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
1226 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
1227 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
1229 IDirectInputDevice2_AddRef(iface);
1230 *ppobj = IDirectInputDevice8W_from_impl(This);
1231 return DI_OK;
1234 WARN("Unsupported interface!\n");
1235 return E_NOINTERFACE;
1238 ULONG WINAPI IDirectInputDevice2WImpl_AddRef(LPDIRECTINPUTDEVICE8W iface)
1240 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1241 ULONG ref = InterlockedIncrement(&This->ref);
1242 TRACE( "(%p) ref %d\n", This, ref );
1243 return ref;
1246 HRESULT WINAPI IDirectInputDevice2WImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface,
1247 LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback, LPVOID lpvRef, DWORD dwFlags)
1249 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1250 DIDEVICEOBJECTINSTANCEW ddoi;
1251 int i;
1253 TRACE("(%p)->(%p,%p flags:%08x)\n", This, lpCallback, lpvRef, dwFlags);
1254 TRACE(" - flags = ");
1255 _dump_EnumObjects_flags(dwFlags);
1256 TRACE("\n");
1258 if (!lpCallback) return DIERR_INVALIDPARAM;
1260 /* Only the fields till dwFFMaxForce are relevant */
1261 memset(&ddoi, 0, sizeof(ddoi));
1262 ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, dwFFMaxForce);
1264 for (i = 0; i < This->data_format.wine_df->dwNumObjs; i++)
1266 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(This->data_format.wine_df, i);
1268 if (dwFlags != DIDFT_ALL && !(dwFlags & DIDFT_GETTYPE(odf->dwType))) continue;
1269 if (IDirectInputDevice_GetObjectInfo(iface, &ddoi, odf->dwType, DIPH_BYID) != DI_OK)
1270 continue;
1272 if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) break;
1275 return DI_OK;
1278 /******************************************************************************
1279 * GetProperty
1282 HRESULT WINAPI IDirectInputDevice2WImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
1284 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1286 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(rguid), pdiph);
1287 _dump_DIPROPHEADER(pdiph);
1289 if (!IS_DIPROP(rguid)) return DI_OK;
1291 switch (LOWORD(rguid))
1293 case (DWORD_PTR) DIPROP_BUFFERSIZE:
1295 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1297 if (pdiph->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
1299 pd->dwData = This->buffersize;
1300 TRACE("buffersize = %d\n", pd->dwData);
1301 break;
1303 case (DWORD_PTR) DIPROP_USERNAME:
1305 LPDIPROPSTRING ps = (LPDIPROPSTRING)pdiph;
1306 struct DevicePlayer *device_player;
1308 if (pdiph->dwSize != sizeof(DIPROPSTRING)) return DIERR_INVALIDPARAM;
1310 LIST_FOR_EACH_ENTRY(device_player, &This->dinput->device_players,
1311 struct DevicePlayer, entry)
1313 if (IsEqualGUID(&device_player->instance_guid, &This->guid))
1315 if (*device_player->username)
1317 lstrcpynW(ps->wsz, device_player->username, ARRAY_SIZE(ps->wsz));
1318 return DI_OK;
1320 else break;
1323 return S_FALSE;
1325 case (DWORD_PTR) DIPROP_VIDPID:
1326 FIXME("DIPROP_VIDPID not implemented\n");
1327 return DIERR_UNSUPPORTED;
1328 default:
1329 FIXME("Unknown property %s\n", debugstr_guid(rguid));
1330 return DIERR_INVALIDPARAM;
1333 return DI_OK;
1336 /******************************************************************************
1337 * SetProperty
1340 HRESULT WINAPI IDirectInputDevice2WImpl_SetProperty(
1341 LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER pdiph)
1343 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1345 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(rguid), pdiph);
1346 _dump_DIPROPHEADER(pdiph);
1348 if (!IS_DIPROP(rguid)) return DI_OK;
1350 switch (LOWORD(rguid))
1352 case (DWORD_PTR) DIPROP_AXISMODE:
1354 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)pdiph;
1356 if (pdiph->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
1357 if (pdiph->dwHow == DIPH_DEVICE && pdiph->dwObj) return DIERR_INVALIDPARAM;
1358 if (This->acquired) return DIERR_ACQUIRED;
1359 if (pdiph->dwHow != DIPH_DEVICE) return DIERR_UNSUPPORTED;
1360 if (!This->data_format.user_df) return DI_OK;
1362 TRACE("Axis mode: %s\n", pd->dwData == DIPROPAXISMODE_ABS ? "absolute" :
1363 "relative");
1365 EnterCriticalSection(&This->crit);
1366 This->data_format.user_df->dwFlags &= ~DIDFT_AXIS;
1367 This->data_format.user_df->dwFlags |= pd->dwData == DIPROPAXISMODE_ABS ?
1368 DIDF_ABSAXIS : DIDF_RELAXIS;
1369 LeaveCriticalSection(&This->crit);
1370 break;
1372 case (DWORD_PTR) DIPROP_BUFFERSIZE:
1374 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)pdiph;
1376 if (pdiph->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
1377 if (This->acquired) return DIERR_ACQUIRED;
1379 TRACE("buffersize = %d\n", pd->dwData);
1381 EnterCriticalSection(&This->crit);
1383 This->buffersize = pd->dwData;
1384 This->queue_len = min(This->buffersize, 1024);
1385 HeapFree(GetProcessHeap(), 0, This->data_queue);
1387 This->data_queue = !This->queue_len ? NULL : HeapAlloc(GetProcessHeap(), 0,
1388 This->queue_len * sizeof(DIDEVICEOBJECTDATA));
1389 This->queue_head = This->queue_tail = This->overflow = 0;
1391 LeaveCriticalSection(&This->crit);
1392 break;
1394 case (DWORD_PTR) DIPROP_USERNAME:
1396 LPCDIPROPSTRING ps = (LPCDIPROPSTRING)pdiph;
1397 struct DevicePlayer *device_player;
1398 BOOL found = FALSE;
1400 if (pdiph->dwSize != sizeof(DIPROPSTRING)) return DIERR_INVALIDPARAM;
1402 LIST_FOR_EACH_ENTRY(device_player, &This->dinput->device_players,
1403 struct DevicePlayer, entry)
1405 if (IsEqualGUID(&device_player->instance_guid, &This->guid))
1407 found = TRUE;
1408 break;
1411 if (!found && (device_player =
1412 HeapAlloc(GetProcessHeap(), 0, sizeof(struct DevicePlayer))))
1414 list_add_tail(&This->dinput->device_players, &device_player->entry);
1415 device_player->instance_guid = This->guid;
1417 if (device_player)
1418 lstrcpynW(device_player->username, ps->wsz, ARRAY_SIZE(device_player->username));
1419 break;
1421 case (DWORD_PTR) DIPROP_APPDATA:
1423 int offset = -1;
1424 LPCDIPROPPOINTER pp = (LPCDIPROPPOINTER)pdiph;
1425 if (pdiph->dwSize != sizeof(DIPROPPOINTER)) return DIERR_INVALIDPARAM;
1427 if (pdiph->dwHow == DIPH_BYID)
1428 offset = id_to_offset(&This->data_format, pdiph->dwObj);
1429 else if (pdiph->dwHow == DIPH_BYOFFSET)
1430 offset = verify_offset(&This->data_format, pdiph->dwObj);
1431 else
1432 return DIERR_UNSUPPORTED;
1434 if (offset == -1) return DIERR_OBJECTNOTFOUND;
1435 if (!set_app_data(This, offset, pp->uData)) return DIERR_OUTOFMEMORY;
1436 break;
1438 default:
1439 WARN("Unknown property %s\n", debugstr_guid(rguid));
1440 return DIERR_UNSUPPORTED;
1443 return DI_OK;
1446 HRESULT WINAPI IDirectInputDevice2WImpl_GetObjectInfo(
1447 LPDIRECTINPUTDEVICE8W iface,
1448 LPDIDEVICEOBJECTINSTANCEW pdidoi,
1449 DWORD dwObj,
1450 DWORD dwHow)
1452 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1453 DWORD dwSize;
1454 LPDIOBJECTDATAFORMAT odf;
1455 int idx = -1;
1457 TRACE("(%p) %d(0x%08x) -> %p\n", This, dwHow, dwObj, pdidoi);
1459 if (!pdidoi) return E_POINTER;
1460 if (pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCEW) &&
1461 pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCE_DX3W))
1462 return DIERR_INVALIDPARAM;
1464 switch (dwHow)
1466 case DIPH_BYOFFSET:
1467 if (!This->data_format.offsets) break;
1468 for (idx = This->data_format.wine_df->dwNumObjs - 1; idx >= 0; idx--)
1469 if (This->data_format.offsets[idx] == dwObj) break;
1470 break;
1471 case DIPH_BYID:
1472 dwObj &= 0x00ffffff;
1473 for (idx = This->data_format.wine_df->dwNumObjs - 1; idx >= 0; idx--)
1474 if ((dataformat_to_odf(This->data_format.wine_df, idx)->dwType & 0x00ffffff) == dwObj)
1475 break;
1476 break;
1478 case DIPH_BYUSAGE:
1479 FIXME("dwHow = DIPH_BYUSAGE not implemented\n");
1480 break;
1481 default:
1482 WARN("invalid parameter: dwHow = %08x\n", dwHow);
1483 return DIERR_INVALIDPARAM;
1485 if (idx < 0) return DIERR_OBJECTNOTFOUND;
1487 odf = dataformat_to_odf(This->data_format.wine_df, idx);
1488 dwSize = pdidoi->dwSize; /* save due to memset below */
1489 memset(pdidoi, 0, pdidoi->dwSize);
1490 pdidoi->dwSize = dwSize;
1491 if (odf->pguid) pdidoi->guidType = *odf->pguid;
1492 pdidoi->dwOfs = This->data_format.offsets ? This->data_format.offsets[idx] : odf->dwOfs;
1493 pdidoi->dwType = odf->dwType;
1494 pdidoi->dwFlags = odf->dwFlags;
1496 return DI_OK;
1499 HRESULT WINAPI IDirectInputDevice2WImpl_GetDeviceData(LPDIRECTINPUTDEVICE8W iface, DWORD dodsize,
1500 LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
1502 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1503 HRESULT ret = DI_OK;
1504 int len;
1506 TRACE("(%p) %p -> %p(%d) x%d, 0x%08x\n",
1507 This, dod, entries, entries ? *entries : 0, dodsize, flags);
1509 if (This->dinput->dwVersion == 0x0800 || dodsize == sizeof(DIDEVICEOBJECTDATA_DX3))
1511 if (!This->queue_len) return DIERR_NOTBUFFERED;
1512 if (!This->acquired) return DIERR_NOTACQUIRED;
1515 if (!This->queue_len)
1516 return DI_OK;
1517 if (dodsize < sizeof(DIDEVICEOBJECTDATA_DX3))
1518 return DIERR_INVALIDPARAM;
1520 IDirectInputDevice2_Poll(iface);
1521 EnterCriticalSection(&This->crit);
1523 len = This->queue_head - This->queue_tail;
1524 if (len < 0) len += This->queue_len;
1526 if ((*entries != INFINITE) && (len > *entries)) len = *entries;
1528 if (dod)
1530 int i;
1531 for (i = 0; i < len; i++)
1533 int n = (This->queue_tail + i) % This->queue_len;
1534 memcpy((char *)dod + dodsize * i, This->data_queue + n, dodsize);
1537 *entries = len;
1539 if (This->overflow && This->dinput->dwVersion == 0x0800)
1540 ret = DI_BUFFEROVERFLOW;
1542 if (!(flags & DIGDD_PEEK))
1544 /* Advance reading position */
1545 This->queue_tail = (This->queue_tail + len) % This->queue_len;
1546 This->overflow = FALSE;
1549 LeaveCriticalSection(&This->crit);
1551 TRACE("Returning %d events queued\n", *entries);
1552 return ret;
1555 HRESULT WINAPI IDirectInputDevice2WImpl_RunControlPanel(LPDIRECTINPUTDEVICE8W iface, HWND hwndOwner, DWORD dwFlags)
1557 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1558 FIXME("%p)->(%p,0x%08x): stub!\n", This, hwndOwner, dwFlags);
1560 return DI_OK;
1563 HRESULT WINAPI IDirectInputDevice2WImpl_Initialize(LPDIRECTINPUTDEVICE8W iface, HINSTANCE hinst, DWORD dwVersion,
1564 REFGUID rguid)
1566 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1567 FIXME("(%p)->(%p,%d,%s): stub!\n", This, hinst, dwVersion, debugstr_guid(rguid));
1568 return DI_OK;
1571 /******************************************************************************
1572 * IDirectInputDevice2A
1575 HRESULT WINAPI IDirectInputDevice2WImpl_CreateEffect(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIEFFECT lpeff,
1576 LPDIRECTINPUTEFFECT *ppdef, LPUNKNOWN pUnkOuter)
1578 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1579 FIXME("(%p)->(%s,%p,%p,%p): stub!\n", This, debugstr_guid(rguid), lpeff, ppdef, pUnkOuter);
1581 FIXME("not available in the generic implementation\n");
1582 *ppdef = NULL;
1583 return DIERR_UNSUPPORTED;
1586 HRESULT WINAPI IDirectInputDevice2WImpl_EnumEffects(
1587 LPDIRECTINPUTDEVICE8W iface,
1588 LPDIENUMEFFECTSCALLBACKW lpCallback,
1589 LPVOID lpvRef,
1590 DWORD dwFlags)
1592 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1593 FIXME("(%p)->(%p,%p,0x%08x): stub!\n", This, lpCallback, lpvRef, dwFlags);
1595 return DI_OK;
1598 HRESULT WINAPI IDirectInputDevice2WImpl_GetEffectInfo(
1599 LPDIRECTINPUTDEVICE8W iface,
1600 LPDIEFFECTINFOW lpdei,
1601 REFGUID rguid)
1603 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1604 FIXME("(%p)->(%p,%s): stub!\n", This, lpdei, debugstr_guid(rguid));
1605 return DI_OK;
1608 HRESULT WINAPI IDirectInputDevice2WImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8W iface, LPDWORD pdwOut)
1610 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1611 FIXME("(%p)->(%p): stub!\n", This, pdwOut);
1612 return DI_OK;
1615 HRESULT WINAPI IDirectInputDevice2WImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8W iface, DWORD dwFlags)
1617 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1618 TRACE("(%p)->(0x%08x)\n", This, dwFlags);
1619 return DI_NOEFFECT;
1622 HRESULT WINAPI IDirectInputDevice2WImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8W iface,
1623 LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback, LPVOID lpvRef, DWORD dwFlags)
1625 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1626 FIXME("(%p)0>(%p,%p,0x%08x): stub!\n", This, lpCallback, lpvRef, dwFlags);
1627 return DI_OK;
1630 HRESULT WINAPI IDirectInputDevice2WImpl_Escape(LPDIRECTINPUTDEVICE8W iface, LPDIEFFESCAPE lpDIEEsc)
1632 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1633 FIXME("(%p)->(%p): stub!\n", This, lpDIEEsc);
1634 return DI_OK;
1637 HRESULT WINAPI IDirectInputDevice2WImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
1639 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1641 if (!This->acquired) return DIERR_NOTACQUIRED;
1643 check_dinput_events();
1644 return DI_OK;
1647 HRESULT WINAPI IDirectInputDevice2WImpl_SendDeviceData(LPDIRECTINPUTDEVICE8W iface, DWORD cbObjectData,
1648 LPCDIDEVICEOBJECTDATA rgdod, LPDWORD pdwInOut,
1649 DWORD dwFlags)
1651 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1652 FIXME("(%p)->(0x%08x,%p,%p,0x%08x): stub!\n", This, cbObjectData, rgdod, pdwInOut, dwFlags);
1654 return DI_OK;
1657 HRESULT WINAPI IDirectInputDevice7WImpl_EnumEffectsInFile(LPDIRECTINPUTDEVICE8W iface,
1658 LPCWSTR lpszFileName,
1659 LPDIENUMEFFECTSINFILECALLBACK pec,
1660 LPVOID pvRef,
1661 DWORD dwFlags)
1663 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1664 FIXME("(%p)->(%s,%p,%p,%08x): stub !\n", This, debugstr_w(lpszFileName), pec, pvRef, dwFlags);
1666 return DI_OK;
1669 HRESULT WINAPI IDirectInputDevice7WImpl_WriteEffectToFile(LPDIRECTINPUTDEVICE8W iface,
1670 LPCWSTR lpszFileName,
1671 DWORD dwEntries,
1672 LPDIFILEEFFECT rgDiFileEft,
1673 DWORD dwFlags)
1675 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1676 FIXME("(%p)->(%s,%08x,%p,%08x): stub !\n", This, debugstr_w(lpszFileName), dwEntries, rgDiFileEft, dwFlags);
1678 return DI_OK;
1681 HRESULT WINAPI IDirectInputDevice8WImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
1682 LPDIACTIONFORMATW lpdiaf,
1683 LPCWSTR lpszUserName,
1684 DWORD dwFlags)
1686 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1687 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", This, lpdiaf, debugstr_w(lpszUserName), dwFlags);
1688 #define X(x) if (dwFlags & x) FIXME("\tdwFlags =|"#x"\n");
1689 X(DIDBAM_DEFAULT)
1690 X(DIDBAM_PRESERVE)
1691 X(DIDBAM_INITIALIZE)
1692 X(DIDBAM_HWDEFAULTS)
1693 #undef X
1695 return DI_OK;
1698 HRESULT WINAPI IDirectInputDevice8WImpl_GetImageInfo(LPDIRECTINPUTDEVICE8W iface,
1699 LPDIDEVICEIMAGEINFOHEADERW lpdiDevImageInfoHeader)
1701 IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1702 FIXME("(%p)->(%p): stub !\n", This, lpdiDevImageInfoHeader);
1704 return DI_OK;
1707 HRESULT direct_input_device_alloc( SIZE_T size, const IDirectInputDevice8WVtbl *vtbl,
1708 const GUID *guid, IDirectInputImpl *dinput, void **out )
1710 IDirectInputDeviceImpl *This;
1711 if (!(This = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) return DIERR_OUTOFMEMORY;
1712 This->IDirectInputDevice8A_iface.lpVtbl = &dinput_device_a_vtbl;
1713 This->IDirectInputDevice8W_iface.lpVtbl = vtbl;
1714 This->ref = 1;
1715 This->guid = *guid;
1716 InitializeCriticalSection( &This->crit );
1717 This->dinput = dinput;
1718 IDirectInput_AddRef( &dinput->IDirectInput7A_iface );
1720 *out = This;
1721 return DI_OK;