dinput: Remove IDirectInputDevice8AImpl_{Build,Set}ActionMap() as it is unused.
[wine] / dlls / dinput / device.c
1 /*              DirectInput Device
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998,1999 Lionel Ulmer
5  *
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /* This file contains all the Device specific functions that can be used as stubs
23    by real device implementations.
24
25    It also contains all the helper functions.
26 */
27 #include "config.h"
28
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"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
43
44 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
45 {
46     return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface);
47 }
48 static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
49 {
50     return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface);
51 }
52
53 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(IDirectInputDeviceImpl *This)
54 {
55     return &This->IDirectInputDevice8A_iface;
56 }
57 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(IDirectInputDeviceImpl *This)
58 {
59     return &This->IDirectInputDevice8W_iface;
60 }
61
62 /******************************************************************************
63  *      Various debugging tools
64  */
65 static void _dump_cooperativelevel_DI(DWORD dwFlags) {
66     if (TRACE_ON(dinput)) {
67         unsigned int   i;
68         static const struct {
69             DWORD       mask;
70             const char  *name;
71         } flags[] = {
72 #define FE(x) { x, #x}
73             FE(DISCL_BACKGROUND),
74             FE(DISCL_EXCLUSIVE),
75             FE(DISCL_FOREGROUND),
76             FE(DISCL_NONEXCLUSIVE),
77             FE(DISCL_NOWINKEY)
78 #undef FE
79         };
80         TRACE(" cooperative level : ");
81         for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
82             if (flags[i].mask & dwFlags)
83                 TRACE("%s ",flags[i].name);
84         TRACE("\n");
85     }
86 }
87
88 static void _dump_EnumObjects_flags(DWORD dwFlags) {
89     if (TRACE_ON(dinput)) {
90         unsigned int   i;
91         DWORD type, instance;
92         static const struct {
93             DWORD       mask;
94             const char  *name;
95         } flags[] = {
96 #define FE(x) { x, #x}
97             FE(DIDFT_RELAXIS),
98             FE(DIDFT_ABSAXIS),
99             FE(DIDFT_PSHBUTTON),
100             FE(DIDFT_TGLBUTTON),
101             FE(DIDFT_POV),
102             FE(DIDFT_COLLECTION),
103             FE(DIDFT_NODATA),       
104             FE(DIDFT_FFACTUATOR),
105             FE(DIDFT_FFEFFECTTRIGGER),
106             FE(DIDFT_OUTPUT),
107             FE(DIDFT_VENDORDEFINED),
108             FE(DIDFT_ALIAS),
109             FE(DIDFT_OPTIONAL)
110 #undef FE
111         };
112         type = (dwFlags & 0xFF0000FF);
113         instance = ((dwFlags >> 8) & 0xFFFF);
114         TRACE("Type:");
115         if (type == DIDFT_ALL) {
116             TRACE(" DIDFT_ALL");
117         } else {
118             for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++) {
119                 if (flags[i].mask & type) {
120                     type &= ~flags[i].mask;
121                     TRACE(" %s",flags[i].name);
122                 }
123             }
124             if (type) {
125                 TRACE(" (unhandled: %08x)", type);
126             }
127         }
128         TRACE(" / Instance: ");
129         if (instance == ((DIDFT_ANYINSTANCE >> 8) & 0xFFFF)) {
130             TRACE("DIDFT_ANYINSTANCE");
131         } else {
132             TRACE("%3d", instance);
133         }
134     }
135 }
136
137 void _dump_DIPROPHEADER(LPCDIPROPHEADER diph) {
138     if (TRACE_ON(dinput)) {
139         TRACE("  - dwObj = 0x%08x\n", diph->dwObj);
140         TRACE("  - dwHow = %s\n",
141             ((diph->dwHow == DIPH_DEVICE) ? "DIPH_DEVICE" :
142             ((diph->dwHow == DIPH_BYOFFSET) ? "DIPH_BYOFFSET" :
143             ((diph->dwHow == DIPH_BYID)) ? "DIPH_BYID" : "unknown")));
144     }
145 }
146
147 void _dump_OBJECTINSTANCEA(const DIDEVICEOBJECTINSTANCEA *ddoi) {
148     TRACE("    - enumerating : %s ('%s') - %2d - 0x%08x - %s\n",
149         debugstr_guid(&ddoi->guidType), _dump_dinput_GUID(&ddoi->guidType), ddoi->dwOfs, ddoi->dwType, ddoi->tszName);
150 }
151
152 void _dump_OBJECTINSTANCEW(const DIDEVICEOBJECTINSTANCEW *ddoi) {
153     TRACE("    - enumerating : %s ('%s'), - %2d - 0x%08x - %s\n",
154         debugstr_guid(&ddoi->guidType), _dump_dinput_GUID(&ddoi->guidType), ddoi->dwOfs, ddoi->dwType, debugstr_w(ddoi->tszName));
155 }
156
157 /* This function is a helper to convert a GUID into any possible DInput GUID out there */
158 const char *_dump_dinput_GUID(const GUID *guid) {
159     unsigned int i;
160     static const struct {
161         const GUID *guid;
162         const char *name;
163     } guids[] = {
164 #define FE(x) { &x, #x}
165         FE(GUID_XAxis),
166         FE(GUID_YAxis),
167         FE(GUID_ZAxis),
168         FE(GUID_RxAxis),
169         FE(GUID_RyAxis),
170         FE(GUID_RzAxis),
171         FE(GUID_Slider),
172         FE(GUID_Button),
173         FE(GUID_Key),
174         FE(GUID_POV),
175         FE(GUID_Unknown),
176         FE(GUID_SysMouse),
177         FE(GUID_SysKeyboard),
178         FE(GUID_Joystick),
179         FE(GUID_ConstantForce),
180         FE(GUID_RampForce),
181         FE(GUID_Square),
182         FE(GUID_Sine),
183         FE(GUID_Triangle),
184         FE(GUID_SawtoothUp),
185         FE(GUID_SawtoothDown),
186         FE(GUID_Spring),
187         FE(GUID_Damper),
188         FE(GUID_Inertia),
189         FE(GUID_Friction),
190         FE(GUID_CustomForce)
191 #undef FE
192     };
193     if (guid == NULL)
194         return "null GUID";
195     for (i = 0; i < (sizeof(guids) / sizeof(guids[0])); i++) {
196         if (IsEqualGUID(guids[i].guid, guid)) {
197             return guids[i].name;
198         }
199     }
200     return debugstr_guid(guid);
201 }
202
203 void _dump_DIDATAFORMAT(const DIDATAFORMAT *df) {
204     unsigned int i;
205
206     TRACE("Dumping DIDATAFORMAT structure:\n");
207     TRACE("  - dwSize: %d\n", df->dwSize);
208     if (df->dwSize != sizeof(DIDATAFORMAT)) {
209         WARN("Non-standard DIDATAFORMAT structure size %d\n", df->dwSize);
210     }
211     TRACE("  - dwObjsize: %d\n", df->dwObjSize);
212     if (df->dwObjSize != sizeof(DIOBJECTDATAFORMAT)) {
213         WARN("Non-standard DIOBJECTDATAFORMAT structure size %d\n", df->dwObjSize);
214     }
215     TRACE("  - dwFlags: 0x%08x (", df->dwFlags);
216     switch (df->dwFlags) {
217         case DIDF_ABSAXIS: TRACE("DIDF_ABSAXIS"); break;
218         case DIDF_RELAXIS: TRACE("DIDF_RELAXIS"); break;
219         default: TRACE("unknown"); break;
220     }
221     TRACE(")\n");
222     TRACE("  - dwDataSize: %d\n", df->dwDataSize);
223     TRACE("  - dwNumObjs: %d\n", df->dwNumObjs);
224     
225     for (i = 0; i < df->dwNumObjs; i++) {
226         TRACE("  - Object %d:\n", i);
227         TRACE("      * GUID: %s ('%s')\n", debugstr_guid(df->rgodf[i].pguid), _dump_dinput_GUID(df->rgodf[i].pguid));
228         TRACE("      * dwOfs: %d\n", df->rgodf[i].dwOfs);
229         TRACE("      * dwType: 0x%08x\n", df->rgodf[i].dwType);
230         TRACE("        "); _dump_EnumObjects_flags(df->rgodf[i].dwType); TRACE("\n");
231         TRACE("      * dwFlags: 0x%08x\n", df->rgodf[i].dwFlags);
232     }
233 }
234
235 /******************************************************************************
236  * Get the default and the app-specific config keys.
237  */
238 BOOL get_app_key(HKEY *defkey, HKEY *appkey)
239 {
240     char buffer[MAX_PATH+16];
241     DWORD len;
242
243     *appkey = 0;
244
245     /* @@ Wine registry key: HKCU\Software\Wine\DirectInput */
246     if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\DirectInput", defkey))
247         *defkey = 0;
248
249     len = GetModuleFileNameA(0, buffer, MAX_PATH);
250     if (len && len < MAX_PATH)
251     {
252         HKEY tmpkey;
253
254         /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectInput */
255         if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
256         {
257             char *p, *appname = buffer;
258             if ((p = strrchr(appname, '/'))) appname = p + 1;
259             if ((p = strrchr(appname, '\\'))) appname = p + 1;
260             strcat(appname, "\\DirectInput");
261
262             if (RegOpenKeyA(tmpkey, appname, appkey)) *appkey = 0;
263             RegCloseKey(tmpkey);
264         }
265     }
266
267     return *defkey || *appkey;
268 }
269
270 /******************************************************************************
271  * Get a config key from either the app-specific or the default config
272  */
273 DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
274                              char *buffer, DWORD size )
275 {
276     if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size ))
277         return 0;
278
279     if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size ))
280         return 0;
281
282     return ERROR_FILE_NOT_FOUND;
283 }
284
285 /* Conversion between internal data buffer and external data buffer */
286 void fill_DataFormat(void *out, DWORD size, const void *in, const DataFormat *df)
287 {
288     int i;
289     const char *in_c = in;
290     char *out_c = out;
291
292     memset(out, 0, size);
293     if (df->dt == NULL) {
294         /* This means that the app uses Wine's internal data format */
295         memcpy(out, in, min(size, df->internal_format_size));
296     } else {
297         for (i = 0; i < df->size; i++) {
298             if (df->dt[i].offset_in >= 0) {
299                 switch (df->dt[i].size) {
300                     case 1:
301                         TRACE("Copying (c) to %d from %d (value %d)\n",
302                               df->dt[i].offset_out, df->dt[i].offset_in, *(in_c + df->dt[i].offset_in));
303                         *(out_c + df->dt[i].offset_out) = *(in_c + df->dt[i].offset_in);
304                         break;
305
306                     case 2:
307                         TRACE("Copying (s) to %d from %d (value %d)\n",
308                               df->dt[i].offset_out, df->dt[i].offset_in, *((const short *)(in_c + df->dt[i].offset_in)));
309                         *((short *)(out_c + df->dt[i].offset_out)) = *((const short *)(in_c + df->dt[i].offset_in));
310                         break;
311
312                     case 4:
313                         TRACE("Copying (i) to %d from %d (value %d)\n",
314                               df->dt[i].offset_out, df->dt[i].offset_in, *((const int *)(in_c + df->dt[i].offset_in)));
315                         *((int *)(out_c + df->dt[i].offset_out)) = *((const int *)(in_c + df->dt[i].offset_in));
316                         break;
317
318                     default:
319                         memcpy((out_c + df->dt[i].offset_out), (in_c + df->dt[i].offset_in), df->dt[i].size);
320                         break;
321                 }
322             } else {
323                 switch (df->dt[i].size) {
324                     case 1:
325                         TRACE("Copying (c) to %d default value %d\n",
326                               df->dt[i].offset_out, df->dt[i].value);
327                         *(out_c + df->dt[i].offset_out) = (char) df->dt[i].value;
328                         break;
329                         
330                     case 2:
331                         TRACE("Copying (s) to %d default value %d\n",
332                               df->dt[i].offset_out, df->dt[i].value);
333                         *((short *) (out_c + df->dt[i].offset_out)) = (short) df->dt[i].value;
334                         break;
335                         
336                     case 4:
337                         TRACE("Copying (i) to %d default value %d\n",
338                               df->dt[i].offset_out, df->dt[i].value);
339                         *((int *) (out_c + df->dt[i].offset_out)) = df->dt[i].value;
340                         break;
341                         
342                     default:
343                         memset((out_c + df->dt[i].offset_out), 0, df->dt[i].size);
344                         break;
345                 }
346             }
347         }
348     }
349 }
350
351 void release_DataFormat(DataFormat * format)
352 {
353     TRACE("Deleting DataFormat: %p\n", format);
354
355     HeapFree(GetProcessHeap(), 0, format->dt);
356     format->dt = NULL;
357     HeapFree(GetProcessHeap(), 0, format->offsets);
358     format->offsets = NULL;
359     HeapFree(GetProcessHeap(), 0, format->user_df);
360     format->user_df = NULL;
361 }
362
363 static inline LPDIOBJECTDATAFORMAT dataformat_to_odf(LPCDIDATAFORMAT df, int idx)
364 {
365     if (idx < 0 || idx >= df->dwNumObjs) return NULL;
366     return (LPDIOBJECTDATAFORMAT)((LPBYTE)df->rgodf + idx * df->dwObjSize);
367 }
368
369 /* dataformat_to_odf_by_type
370  *  Find the Nth object of the selected type in the DataFormat
371  */
372 LPDIOBJECTDATAFORMAT dataformat_to_odf_by_type(LPCDIDATAFORMAT df, int n, DWORD type)
373 {
374     int i, nfound = 0;
375
376     for (i=0; i < df->dwNumObjs; i++)
377     {
378         LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(df, i);
379
380         if (odf->dwType & type)
381         {
382             if (n == nfound)
383                 return odf;
384
385             nfound++;
386         }
387     }
388
389     return NULL;
390 }
391
392 static HRESULT create_DataFormat(LPCDIDATAFORMAT asked_format, DataFormat *format)
393 {
394     DataTransform *dt;
395     unsigned int i, j;
396     int same = 1;
397     int *done;
398     int index = 0;
399     DWORD next = 0;
400
401     if (!format->wine_df) return DIERR_INVALIDPARAM;
402     done = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, asked_format->dwNumObjs * sizeof(int));
403     dt = HeapAlloc(GetProcessHeap(), 0, asked_format->dwNumObjs * sizeof(DataTransform));
404     if (!dt || !done) goto failed;
405
406     if (!(format->offsets = HeapAlloc(GetProcessHeap(), 0, format->wine_df->dwNumObjs * sizeof(int))))
407         goto failed;
408
409     if (!(format->user_df = HeapAlloc(GetProcessHeap(), 0, asked_format->dwSize)))
410         goto failed;
411     memcpy(format->user_df, asked_format, asked_format->dwSize);
412
413     TRACE("Creating DataTransform :\n");
414     
415     for (i = 0; i < format->wine_df->dwNumObjs; i++)
416     {
417         format->offsets[i] = -1;
418
419         for (j = 0; j < asked_format->dwNumObjs; j++) {
420             if (done[j] == 1)
421                 continue;
422             
423             if (/* Check if the application either requests any GUID and if not, it if matches
424                  * the GUID of the Wine object.
425                  */
426                 ((asked_format->rgodf[j].pguid == NULL) ||
427                  (format->wine_df->rgodf[i].pguid == NULL) ||
428                  (IsEqualGUID(format->wine_df->rgodf[i].pguid, asked_format->rgodf[j].pguid)))
429                 &&
430                 (/* Then check if it accepts any instance id, and if not, if it matches Wine's
431                   * instance id.
432                   */
433                  ((asked_format->rgodf[j].dwType & DIDFT_INSTANCEMASK) == DIDFT_ANYINSTANCE) ||
434                  (DIDFT_GETINSTANCE(asked_format->rgodf[j].dwType) == 0x00FF) || /* This is mentionned in no DX docs, but it works fine - tested on WinXP */
435                  (DIDFT_GETINSTANCE(asked_format->rgodf[j].dwType) == DIDFT_GETINSTANCE(format->wine_df->rgodf[i].dwType)))
436                 &&
437                 ( /* Then if the asked type matches the one Wine provides */
438                  DIDFT_GETTYPE(asked_format->rgodf[j].dwType) & format->wine_df->rgodf[i].dwType))
439             {
440                 done[j] = 1;
441                 
442                 TRACE("Matching :\n");
443                 TRACE("   - Asked (%d) :\n", j);
444                 TRACE("       * GUID: %s ('%s')\n",
445                       debugstr_guid(asked_format->rgodf[j].pguid),
446                       _dump_dinput_GUID(asked_format->rgodf[j].pguid));
447                 TRACE("       * Offset: %3d\n", asked_format->rgodf[j].dwOfs);
448                 TRACE("       * dwType: %08x\n", asked_format->rgodf[j].dwType);
449                 TRACE("         "); _dump_EnumObjects_flags(asked_format->rgodf[j].dwType); TRACE("\n");
450                 
451                 TRACE("   - Wine  (%d) :\n", i);
452                 TRACE("       * GUID: %s ('%s')\n",
453                       debugstr_guid(format->wine_df->rgodf[i].pguid),
454                       _dump_dinput_GUID(format->wine_df->rgodf[i].pguid));
455                 TRACE("       * Offset: %3d\n", format->wine_df->rgodf[i].dwOfs);
456                 TRACE("       * dwType: %08x\n", format->wine_df->rgodf[i].dwType);
457                 TRACE("         "); _dump_EnumObjects_flags(format->wine_df->rgodf[i].dwType); TRACE("\n");
458                 
459                 if (format->wine_df->rgodf[i].dwType & DIDFT_BUTTON)
460                     dt[index].size = sizeof(BYTE);
461                 else
462                     dt[index].size = sizeof(DWORD);
463                 dt[index].offset_in = format->wine_df->rgodf[i].dwOfs;
464                 dt[index].offset_out = asked_format->rgodf[j].dwOfs;
465                 format->offsets[i]   = asked_format->rgodf[j].dwOfs;
466                 dt[index].value = 0;
467                 next = next + dt[index].size;
468                 
469                 if (format->wine_df->rgodf[i].dwOfs != dt[index].offset_out)
470                     same = 0;
471                 
472                 index++;
473                 break;
474             }
475         }
476     }
477
478     TRACE("Setting to default value :\n");
479     for (j = 0; j < asked_format->dwNumObjs; j++) {
480         if (done[j] == 0) {
481             TRACE("   - Asked (%d) :\n", j);
482             TRACE("       * GUID: %s ('%s')\n",
483                   debugstr_guid(asked_format->rgodf[j].pguid),
484                   _dump_dinput_GUID(asked_format->rgodf[j].pguid));
485             TRACE("       * Offset: %3d\n", asked_format->rgodf[j].dwOfs);
486             TRACE("       * dwType: %08x\n", asked_format->rgodf[j].dwType);
487             TRACE("         "); _dump_EnumObjects_flags(asked_format->rgodf[j].dwType); TRACE("\n");
488             
489             if (asked_format->rgodf[j].dwType & DIDFT_BUTTON)
490                 dt[index].size = sizeof(BYTE);
491             else
492                 dt[index].size = sizeof(DWORD);
493             dt[index].offset_in  = -1;
494             dt[index].offset_out = asked_format->rgodf[j].dwOfs;
495             if (asked_format->rgodf[j].dwType & DIDFT_POV)
496                 dt[index].value = -1;
497             else
498                 dt[index].value = 0;
499             index++;
500
501             same = 0;
502         }
503     }
504     
505     format->internal_format_size = format->wine_df->dwDataSize;
506     format->size = index;
507     if (same) {
508         HeapFree(GetProcessHeap(), 0, dt);
509         dt = NULL;
510     }
511     format->dt = dt;
512
513     HeapFree(GetProcessHeap(), 0, done);
514
515     return DI_OK;
516
517 failed:
518     HeapFree(GetProcessHeap(), 0, done);
519     HeapFree(GetProcessHeap(), 0, dt);
520     format->dt = NULL;
521     HeapFree(GetProcessHeap(), 0, format->offsets);
522     format->offsets = NULL;
523     HeapFree(GetProcessHeap(), 0, format->user_df);
524     format->user_df = NULL;
525
526     return DIERR_OUTOFMEMORY;
527 }
528
529 /* find an object by it's offset in a data format */
530 static int offset_to_object(const DataFormat *df, int offset)
531 {
532     int i;
533
534     if (!df->offsets) return -1;
535
536     for (i = 0; i < df->wine_df->dwNumObjs; i++)
537         if (df->offsets[i] == offset) return i;
538
539     return -1;
540 }
541
542 int id_to_object(LPCDIDATAFORMAT df, int id)
543 {
544     int i;
545
546     id &= 0x00ffffff;
547     for (i = 0; i < df->dwNumObjs; i++)
548         if ((dataformat_to_odf(df, i)->dwType & 0x00ffffff) == id)
549             return i;
550
551     return -1;
552 }
553
554 static int id_to_offset(const DataFormat *df, int id)
555 {
556     int obj = id_to_object(df->wine_df, id);
557
558     return obj >= 0 && df->offsets ? df->offsets[obj] : -1;
559 }
560
561 int find_property(const DataFormat *df, LPCDIPROPHEADER ph)
562 {
563     switch (ph->dwHow)
564     {
565         case DIPH_BYID:     return id_to_object(df->wine_df, ph->dwObj);
566         case DIPH_BYOFFSET: return offset_to_object(df, ph->dwObj);
567     }
568     FIXME("Unhandled ph->dwHow=='%04X'\n", (unsigned int)ph->dwHow);
569
570     return -1;
571 }
572
573 DWORD semantic_to_obj_id(IDirectInputDeviceImpl* This, DWORD dwSemantic)
574 {
575     DWORD type = (0x0000ff00 & dwSemantic) >> 8;
576     DWORD offset = 0x000000ff & dwSemantic;
577     DWORD obj_instance = 0;
578     DWORD found = 0;
579     int i;
580
581     for (i = 0; i < This->data_format.wine_df->dwNumObjs; i++)
582     {
583         LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(This->data_format.wine_df, i);
584
585         if (odf->dwOfs == offset)
586         {
587             obj_instance = DIDFT_GETINSTANCE(odf->dwType);
588             found = 1;
589             break;
590         }
591     }
592
593     if (!found) return 0;
594
595     if (type & DIDFT_AXIS)   type = DIDFT_RELAXIS;
596     if (type & DIDFT_BUTTON) type = DIDFT_PSHBUTTON;
597
598     return type | (0x0000ff00 & (obj_instance << 8));
599 }
600
601 /******************************************************************************
602  *      queue_event - add new event to the ring queue
603  */
604
605 void queue_event(LPDIRECTINPUTDEVICE8A iface, int inst_id, DWORD data, DWORD time, DWORD seq)
606 {
607     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
608     int next_pos, ofs = id_to_offset(&This->data_format, inst_id);
609
610     /* Event is being set regardless of the queue state */
611     if (This->hEvent) SetEvent(This->hEvent);
612
613     if (!This->queue_len || This->overflow || ofs < 0) return;
614
615     next_pos = (This->queue_head + 1) % This->queue_len;
616     if (next_pos == This->queue_tail)
617     {
618         TRACE(" queue overflowed\n");
619         This->overflow = TRUE;
620         return;
621     }
622
623     TRACE(" queueing %d at offset %d (queue head %d / size %d)\n",
624           data, ofs, This->queue_head, This->queue_len);
625
626     This->data_queue[This->queue_head].dwOfs       = ofs;
627     This->data_queue[This->queue_head].dwData      = data;
628     This->data_queue[This->queue_head].dwTimeStamp = time;
629     This->data_queue[This->queue_head].dwSequence  = seq;
630
631     /* Set uAppData by means of action mapping */
632     if (This->num_actions > 0)
633     {
634         int i;
635         for (i=0; i < This->num_actions; i++)
636         {
637             if (This->action_map[i].offset == ofs)
638             {
639                 TRACE("Offset %d mapped to uAppData %lu\n", ofs, This->action_map[i].uAppData);
640                 This->data_queue[This->queue_head].uAppData = This->action_map[i].uAppData;
641                 break;
642             }
643         }
644     }
645
646     This->queue_head = next_pos;
647     /* Send event if asked */
648 }
649
650 /******************************************************************************
651  *      Acquire
652  */
653
654 HRESULT WINAPI IDirectInputDevice2WImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
655 {
656     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
657     HRESULT res;
658
659     if (!This->data_format.user_df) return DIERR_INVALIDPARAM;
660     if (This->dwCoopLevel & DISCL_FOREGROUND && This->win != GetForegroundWindow())
661         return DIERR_OTHERAPPHASPRIO;
662
663     EnterCriticalSection(&This->crit);
664     res = This->acquired ? S_FALSE : DI_OK;
665     This->acquired = 1;
666     if (res == DI_OK)
667     {
668         This->queue_head = This->queue_tail = This->overflow = 0;
669         check_dinput_hooks(iface);
670     }
671     LeaveCriticalSection(&This->crit);
672
673     return res;
674 }
675
676 HRESULT WINAPI IDirectInputDevice2AImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
677 {
678     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
679     return IDirectInputDevice2WImpl_Acquire(IDirectInputDevice8W_from_impl(This));
680 }
681
682
683 /******************************************************************************
684  *      Unacquire
685  */
686
687 HRESULT WINAPI IDirectInputDevice2WImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
688 {
689     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
690     HRESULT res;
691
692     EnterCriticalSection(&This->crit);
693     res = !This->acquired ? DI_NOEFFECT : DI_OK;
694     This->acquired = 0;
695     if (res == DI_OK)
696         check_dinput_hooks(iface);
697     LeaveCriticalSection(&This->crit);
698
699     return res;
700 }
701
702 HRESULT WINAPI IDirectInputDevice2AImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
703 {
704     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
705     return IDirectInputDevice2WImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
706 }
707
708 /******************************************************************************
709  *      IDirectInputDeviceA
710  */
711
712 HRESULT WINAPI IDirectInputDevice2WImpl_SetDataFormat(LPDIRECTINPUTDEVICE8W iface, LPCDIDATAFORMAT df)
713 {
714     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
715     HRESULT res = DI_OK;
716
717     if (!df) return E_POINTER;
718     TRACE("(%p) %p\n", This, df);
719     _dump_DIDATAFORMAT(df);
720
721     if (df->dwSize != sizeof(DIDATAFORMAT)) return DIERR_INVALIDPARAM;
722     if (This->acquired) return DIERR_ACQUIRED;
723
724     EnterCriticalSection(&This->crit);
725
726     release_DataFormat(&This->data_format);
727     res = create_DataFormat(df, &This->data_format);
728
729     LeaveCriticalSection(&This->crit);
730     return res;
731 }
732
733 HRESULT WINAPI IDirectInputDevice2AImpl_SetDataFormat(LPDIRECTINPUTDEVICE8A iface, LPCDIDATAFORMAT df)
734 {
735     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
736     return IDirectInputDevice2WImpl_SetDataFormat(IDirectInputDevice8W_from_impl(This), df);
737 }
738
739 /******************************************************************************
740   *     SetCooperativeLevel
741   *
742   *  Set cooperative level and the source window for the events.
743   */
744 HRESULT WINAPI IDirectInputDevice2WImpl_SetCooperativeLevel(LPDIRECTINPUTDEVICE8W iface, HWND hwnd, DWORD dwflags)
745 {
746     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
747
748     TRACE("(%p) %p,0x%08x\n", This, hwnd, dwflags);
749     _dump_cooperativelevel_DI(dwflags);
750
751     if ((dwflags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == 0 ||
752         (dwflags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE) ||
753         (dwflags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == 0 ||
754         (dwflags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == (DISCL_FOREGROUND | DISCL_BACKGROUND))
755         return DIERR_INVALIDPARAM;
756
757     if (dwflags == (DISCL_NONEXCLUSIVE | DISCL_BACKGROUND))
758         hwnd = GetDesktopWindow();
759
760     if (!hwnd) return E_HANDLE;
761
762     /* For security reasons native does not allow exclusive background level
763        for mouse and keyboard only */
764     if (dwflags & DISCL_EXCLUSIVE && dwflags & DISCL_BACKGROUND &&
765         (IsEqualGUID(&This->guid, &GUID_SysMouse) ||
766          IsEqualGUID(&This->guid, &GUID_SysKeyboard)))
767         return DIERR_UNSUPPORTED;
768
769     /* Store the window which asks for the mouse */
770     EnterCriticalSection(&This->crit);
771     This->win = hwnd;
772     This->dwCoopLevel = dwflags;
773     LeaveCriticalSection(&This->crit);
774
775     return DI_OK;
776 }
777
778 HRESULT WINAPI IDirectInputDevice2AImpl_SetCooperativeLevel(LPDIRECTINPUTDEVICE8A iface, HWND hwnd, DWORD dwflags)
779 {
780     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
781     return IDirectInputDevice2WImpl_SetCooperativeLevel(IDirectInputDevice8W_from_impl(This), hwnd, dwflags);
782 }
783
784 /******************************************************************************
785   *     SetEventNotification : specifies event to be sent on state change
786   */
787 HRESULT WINAPI IDirectInputDevice2WImpl_SetEventNotification(LPDIRECTINPUTDEVICE8W iface, HANDLE event)
788 {
789     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
790
791     TRACE("(%p) %p\n", This, event);
792
793     EnterCriticalSection(&This->crit);
794     This->hEvent = event;
795     LeaveCriticalSection(&This->crit);
796     return DI_OK;
797 }
798
799 HRESULT WINAPI IDirectInputDevice2AImpl_SetEventNotification(LPDIRECTINPUTDEVICE8A iface, HANDLE event)
800 {
801     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
802     return IDirectInputDevice2WImpl_SetEventNotification(IDirectInputDevice8W_from_impl(This), event);
803 }
804
805
806 ULONG WINAPI IDirectInputDevice2WImpl_Release(LPDIRECTINPUTDEVICE8W iface)
807 {
808     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
809     ULONG ref;
810
811     ref = InterlockedDecrement(&(This->ref));
812     if (ref) return ref;
813
814     IDirectInputDevice_Unacquire(iface);
815     /* Reset the FF state, free all effects, etc */
816     IDirectInputDevice8_SendForceFeedbackCommand(iface, DISFFC_RESET);
817
818     HeapFree(GetProcessHeap(), 0, This->data_queue);
819
820     /* Free data format */
821     HeapFree(GetProcessHeap(), 0, This->data_format.wine_df->rgodf);
822     HeapFree(GetProcessHeap(), 0, This->data_format.wine_df);
823     release_DataFormat(&This->data_format);
824
825     /* Free action mapping */
826     HeapFree(GetProcessHeap(), 0, This->action_map);
827
828     EnterCriticalSection( &This->dinput->crit );
829     list_remove( &This->entry );
830     LeaveCriticalSection( &This->dinput->crit );
831
832     IDirectInput_Release(&This->dinput->IDirectInput7A_iface);
833     This->crit.DebugInfo->Spare[0] = 0;
834     DeleteCriticalSection(&This->crit);
835
836     HeapFree(GetProcessHeap(), 0, This);
837
838     return DI_OK;
839 }
840
841 ULONG WINAPI IDirectInputDevice2AImpl_Release(LPDIRECTINPUTDEVICE8A iface)
842 {
843     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
844     return IDirectInputDevice2WImpl_Release(IDirectInputDevice8W_from_impl(This));
845 }
846
847 HRESULT WINAPI IDirectInputDevice2WImpl_QueryInterface(LPDIRECTINPUTDEVICE8W iface, REFIID riid, LPVOID *ppobj)
848 {
849     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
850
851     TRACE("(%p this=%p,%s,%p)\n", iface, This, debugstr_guid(riid), ppobj);
852     if (IsEqualGUID(&IID_IUnknown, riid) ||
853         IsEqualGUID(&IID_IDirectInputDeviceA,  riid) ||
854         IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
855         IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
856         IsEqualGUID(&IID_IDirectInputDevice8A, riid))
857     {
858         IDirectInputDevice2_AddRef(iface);
859         *ppobj = IDirectInputDevice8A_from_impl(This);
860         return DI_OK;
861     }
862     if (IsEqualGUID(&IID_IDirectInputDeviceW,  riid) ||
863         IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
864         IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
865         IsEqualGUID(&IID_IDirectInputDevice8W, riid))
866     {
867         IDirectInputDevice2_AddRef(iface);
868         *ppobj = IDirectInputDevice8W_from_impl(This);
869         return DI_OK;
870     }
871
872     WARN("Unsupported interface!\n");
873     return E_FAIL;
874 }
875
876 HRESULT WINAPI IDirectInputDevice2AImpl_QueryInterface(LPDIRECTINPUTDEVICE8A iface, REFIID riid, LPVOID *ppobj)
877 {
878     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
879     return IDirectInputDevice2WImpl_QueryInterface(IDirectInputDevice8W_from_impl(This), riid, ppobj);
880 }
881
882 ULONG WINAPI IDirectInputDevice2WImpl_AddRef(LPDIRECTINPUTDEVICE8W iface)
883 {
884     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
885     return InterlockedIncrement(&This->ref);
886 }
887
888 ULONG WINAPI IDirectInputDevice2AImpl_AddRef(LPDIRECTINPUTDEVICE8A iface)
889 {
890     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
891     return IDirectInputDevice2WImpl_AddRef(IDirectInputDevice8W_from_impl(This));
892 }
893
894 HRESULT WINAPI IDirectInputDevice2AImpl_EnumObjects(LPDIRECTINPUTDEVICE8A iface,
895         LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback, LPVOID lpvRef, DWORD dwFlags)
896 {
897     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
898     DIDEVICEOBJECTINSTANCEA ddoi;
899     int i;
900
901     TRACE("(%p) %p,%p flags:%08x)\n", iface, lpCallback, lpvRef, dwFlags);
902     TRACE("  - flags = ");
903     _dump_EnumObjects_flags(dwFlags);
904     TRACE("\n");
905
906     /* Only the fields till dwFFMaxForce are relevant */
907     memset(&ddoi, 0, sizeof(ddoi));
908     ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
909
910     for (i = 0; i < This->data_format.wine_df->dwNumObjs; i++)
911     {
912         LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(This->data_format.wine_df, i);
913
914         if (dwFlags != DIDFT_ALL && !(dwFlags & DIDFT_GETTYPE(odf->dwType))) continue;
915         if (IDirectInputDevice_GetObjectInfo(iface, &ddoi, odf->dwType, DIPH_BYID) != DI_OK)
916             continue;
917
918         if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) break;
919     }
920
921     return DI_OK;
922 }
923
924 HRESULT WINAPI IDirectInputDevice2WImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface,
925         LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback, LPVOID lpvRef, DWORD dwFlags)
926 {
927     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
928     DIDEVICEOBJECTINSTANCEW ddoi;
929     int i;
930
931     TRACE("(%p) %p,%p flags:%08x)\n", iface, lpCallback, lpvRef, dwFlags);
932     TRACE("  - flags = ");
933     _dump_EnumObjects_flags(dwFlags);
934     TRACE("\n");
935
936     /* Only the fields till dwFFMaxForce are relevant */
937     memset(&ddoi, 0, sizeof(ddoi));
938     ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, dwFFMaxForce);
939
940     for (i = 0; i < This->data_format.wine_df->dwNumObjs; i++)
941     {
942         LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(This->data_format.wine_df, i);
943
944         if (dwFlags != DIDFT_ALL && !(dwFlags & DIDFT_GETTYPE(odf->dwType))) continue;
945         if (IDirectInputDevice_GetObjectInfo(iface, &ddoi, odf->dwType, DIPH_BYID) != DI_OK)
946             continue;
947
948         if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) break;
949     }
950
951     return DI_OK;
952 }
953
954 /******************************************************************************
955  *      GetProperty
956  */
957
958 HRESULT WINAPI IDirectInputDevice2WImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
959 {
960     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
961
962     TRACE("(%p) %s,%p\n", iface, debugstr_guid(rguid), pdiph);
963     _dump_DIPROPHEADER(pdiph);
964
965     if (!IS_DIPROP(rguid)) return DI_OK;
966
967     switch (LOWORD(rguid))
968     {
969         case (DWORD_PTR) DIPROP_BUFFERSIZE:
970         {
971             LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
972
973             if (pdiph->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
974
975             pd->dwData = This->queue_len;
976             TRACE("buffersize = %d\n", pd->dwData);
977             break;
978         }
979         case (DWORD_PTR) DIPROP_VIDPID:
980             FIXME("DIPROP_VIDPID not implemented\n");
981             return DIERR_UNSUPPORTED;
982         default:
983             FIXME("Unknown property %s\n", debugstr_guid(rguid));
984             return DIERR_INVALIDPARAM;
985     }
986
987     return DI_OK;
988 }
989
990 HRESULT WINAPI IDirectInputDevice2AImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
991 {
992     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
993     return IDirectInputDevice2WImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
994 }
995
996 /******************************************************************************
997  *      SetProperty
998  */
999
1000 HRESULT WINAPI IDirectInputDevice2WImpl_SetProperty(
1001         LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER pdiph)
1002 {
1003     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1004
1005     TRACE("(%p) %s,%p\n", iface, debugstr_guid(rguid), pdiph);
1006     _dump_DIPROPHEADER(pdiph);
1007
1008     if (!IS_DIPROP(rguid)) return DI_OK;
1009
1010     switch (LOWORD(rguid))
1011     {
1012         case (DWORD_PTR) DIPROP_AXISMODE:
1013         {
1014             LPCDIPROPDWORD pd = (LPCDIPROPDWORD)pdiph;
1015
1016             if (pdiph->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
1017             if (pdiph->dwHow == DIPH_DEVICE && pdiph->dwObj) return DIERR_INVALIDPARAM;
1018             if (This->acquired) return DIERR_ACQUIRED;
1019             if (pdiph->dwHow != DIPH_DEVICE) return DIERR_UNSUPPORTED;
1020             if (!This->data_format.user_df) return DI_OK;
1021
1022             TRACE("Axis mode: %s\n", pd->dwData == DIPROPAXISMODE_ABS ? "absolute" :
1023                                                                         "relative");
1024
1025             EnterCriticalSection(&This->crit);
1026             This->data_format.user_df->dwFlags &= ~DIDFT_AXIS;
1027             This->data_format.user_df->dwFlags |= pd->dwData == DIPROPAXISMODE_ABS ?
1028                                                   DIDF_ABSAXIS : DIDF_RELAXIS;
1029             LeaveCriticalSection(&This->crit);
1030             break;
1031         }
1032         case (DWORD_PTR) DIPROP_BUFFERSIZE:
1033         {
1034             LPCDIPROPDWORD pd = (LPCDIPROPDWORD)pdiph;
1035
1036             if (pdiph->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
1037             if (This->acquired) return DIERR_ACQUIRED;
1038
1039             TRACE("buffersize = %d\n", pd->dwData);
1040
1041             EnterCriticalSection(&This->crit);
1042             HeapFree(GetProcessHeap(), 0, This->data_queue);
1043
1044             This->data_queue = !pd->dwData ? NULL : HeapAlloc(GetProcessHeap(), 0,
1045                                 pd->dwData * sizeof(DIDEVICEOBJECTDATA));
1046             This->queue_head = This->queue_tail = This->overflow = 0;
1047             This->queue_len  = pd->dwData;
1048
1049             LeaveCriticalSection(&This->crit);
1050             break;
1051         }
1052         default:
1053             WARN("Unknown property %s\n", debugstr_guid(rguid));
1054             return DIERR_UNSUPPORTED;
1055     }
1056
1057     return DI_OK;
1058 }
1059
1060 HRESULT WINAPI IDirectInputDevice2AImpl_SetProperty(
1061         LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER pdiph)
1062 {
1063     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1064     return IDirectInputDevice2WImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
1065 }
1066
1067 HRESULT WINAPI IDirectInputDevice2AImpl_GetObjectInfo(
1068         LPDIRECTINPUTDEVICE8A iface,
1069         LPDIDEVICEOBJECTINSTANCEA pdidoi,
1070         DWORD dwObj,
1071         DWORD dwHow)
1072 {
1073     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1074     DIDEVICEOBJECTINSTANCEW didoiW;
1075     HRESULT res;
1076
1077     if (!pdidoi ||
1078         (pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCEA) &&
1079          pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCE_DX3A)))
1080         return DIERR_INVALIDPARAM;
1081
1082     didoiW.dwSize = sizeof(didoiW);
1083     res = IDirectInputDevice2WImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
1084     if (res == DI_OK)
1085     {
1086         DWORD dwSize = pdidoi->dwSize;
1087
1088         memset(pdidoi, 0, pdidoi->dwSize);
1089         pdidoi->dwSize   = dwSize;
1090         pdidoi->guidType = didoiW.guidType;
1091         pdidoi->dwOfs    = didoiW.dwOfs;
1092         pdidoi->dwType   = didoiW.dwType;
1093         pdidoi->dwFlags  = didoiW.dwFlags;
1094     }
1095
1096     return res;
1097 }
1098
1099 HRESULT WINAPI IDirectInputDevice2WImpl_GetObjectInfo(
1100         LPDIRECTINPUTDEVICE8W iface,
1101         LPDIDEVICEOBJECTINSTANCEW pdidoi,
1102         DWORD dwObj,
1103         DWORD dwHow)
1104 {
1105     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1106     DWORD dwSize;
1107     LPDIOBJECTDATAFORMAT odf;
1108     int idx = -1;
1109
1110     TRACE("(%p) %d(0x%08x) -> %p\n", This, dwHow, dwObj, pdidoi);
1111
1112     if (!pdidoi ||
1113         (pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCEW) &&
1114          pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCE_DX3W)))
1115         return DIERR_INVALIDPARAM;
1116
1117     switch (dwHow)
1118     {
1119     case DIPH_BYOFFSET:
1120         if (!This->data_format.offsets) break;
1121         for (idx = This->data_format.wine_df->dwNumObjs - 1; idx >= 0; idx--)
1122             if (This->data_format.offsets[idx] == dwObj) break;
1123         break;
1124     case DIPH_BYID:
1125         dwObj &= 0x00ffffff;
1126         for (idx = This->data_format.wine_df->dwNumObjs - 1; idx >= 0; idx--)
1127             if ((dataformat_to_odf(This->data_format.wine_df, idx)->dwType & 0x00ffffff) == dwObj)
1128                 break;
1129         break;
1130
1131     case DIPH_BYUSAGE:
1132         FIXME("dwHow = DIPH_BYUSAGE not implemented\n");
1133         break;
1134     default:
1135         WARN("invalid parameter: dwHow = %08x\n", dwHow);
1136         return DIERR_INVALIDPARAM;
1137     }
1138     if (idx < 0) return DIERR_OBJECTNOTFOUND;
1139
1140     odf = dataformat_to_odf(This->data_format.wine_df, idx);
1141     dwSize = pdidoi->dwSize; /* save due to memset below */
1142     memset(pdidoi, 0, pdidoi->dwSize);
1143     pdidoi->dwSize   = dwSize;
1144     if (odf->pguid) pdidoi->guidType = *odf->pguid;
1145     pdidoi->dwOfs    = This->data_format.offsets ? This->data_format.offsets[idx] : odf->dwOfs;
1146     pdidoi->dwType   = odf->dwType;
1147     pdidoi->dwFlags  = odf->dwFlags;
1148
1149     return DI_OK;
1150 }
1151
1152 HRESULT WINAPI IDirectInputDevice2WImpl_GetDeviceData(LPDIRECTINPUTDEVICE8W iface, DWORD dodsize,
1153                                                       LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
1154 {
1155     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1156     HRESULT ret = DI_OK;
1157     int len;
1158
1159     TRACE("(%p) %p -> %p(%d) x%d, 0x%08x\n",
1160           This, dod, entries, entries ? *entries : 0, dodsize, flags);
1161
1162     if (!This->acquired)
1163         return DIERR_NOTACQUIRED;
1164     if (!This->queue_len)
1165         return DIERR_NOTBUFFERED;
1166     if (dodsize < sizeof(DIDEVICEOBJECTDATA_DX3))
1167         return DIERR_INVALIDPARAM;
1168
1169     IDirectInputDevice2_Poll(iface);
1170     EnterCriticalSection(&This->crit);
1171
1172     len = This->queue_head - This->queue_tail;
1173     if (len < 0) len += This->queue_len;
1174
1175     if ((*entries != INFINITE) && (len > *entries)) len = *entries;
1176
1177     if (dod)
1178     {
1179         int i;
1180         for (i = 0; i < len; i++)
1181         {
1182             int n = (This->queue_tail + i) % This->queue_len;
1183             memcpy((char *)dod + dodsize * i, This->data_queue + n, dodsize);
1184         }
1185     }
1186     *entries = len;
1187
1188     if (This->overflow)
1189         ret = DI_BUFFEROVERFLOW;
1190
1191     if (!(flags & DIGDD_PEEK))
1192     {
1193         /* Advance reading position */
1194         This->queue_tail = (This->queue_tail + len) % This->queue_len;
1195         This->overflow = FALSE;
1196     }
1197
1198     LeaveCriticalSection(&This->crit);
1199
1200     TRACE("Returning %d events queued\n", *entries);
1201     return ret;
1202 }
1203
1204 HRESULT WINAPI IDirectInputDevice2AImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface, DWORD dodsize,
1205                                                       LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
1206 {
1207     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1208     return IDirectInputDevice2WImpl_GetDeviceData(IDirectInputDevice8W_from_impl(This), dodsize, dod, entries, flags);
1209 }
1210
1211 HRESULT WINAPI IDirectInputDevice2WImpl_RunControlPanel(LPDIRECTINPUTDEVICE8W iface, HWND hwndOwner, DWORD dwFlags)
1212 {
1213     FIXME("(this=%p,%p,0x%08x): stub!\n", iface, hwndOwner, dwFlags);
1214
1215     return DI_OK;
1216 }
1217
1218 HRESULT WINAPI IDirectInputDevice2AImpl_RunControlPanel(LPDIRECTINPUTDEVICE8A iface, HWND hwndOwner, DWORD dwFlags)
1219 {
1220     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1221     return IDirectInputDevice2WImpl_RunControlPanel(IDirectInputDevice8W_from_impl(This), hwndOwner, dwFlags);
1222 }
1223
1224 HRESULT WINAPI IDirectInputDevice2WImpl_Initialize(LPDIRECTINPUTDEVICE8W iface, HINSTANCE hinst, DWORD dwVersion,
1225                                                    REFGUID rguid)
1226 {
1227     FIXME("(this=%p,%p,%d,%s): stub!\n", iface, hinst, dwVersion, debugstr_guid(rguid));
1228     return DI_OK;
1229 }
1230
1231 HRESULT WINAPI IDirectInputDevice2AImpl_Initialize(LPDIRECTINPUTDEVICE8A iface, HINSTANCE hinst, DWORD dwVersion,
1232                                                    REFGUID rguid)
1233 {
1234     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1235     return IDirectInputDevice2WImpl_Initialize(IDirectInputDevice8W_from_impl(This), hinst, dwVersion, rguid);
1236 }
1237
1238 /******************************************************************************
1239  *      IDirectInputDevice2A
1240  */
1241
1242 HRESULT WINAPI IDirectInputDevice2WImpl_CreateEffect(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIEFFECT lpeff,
1243                                                      LPDIRECTINPUTEFFECT *ppdef, LPUNKNOWN pUnkOuter)
1244 {
1245     FIXME("(this=%p,%s,%p,%p,%p): stub!\n", iface, debugstr_guid(rguid), lpeff, ppdef, pUnkOuter);
1246     return DI_OK;
1247 }
1248
1249 HRESULT WINAPI IDirectInputDevice2AImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIEFFECT lpeff,
1250                                                      LPDIRECTINPUTEFFECT *ppdef, LPUNKNOWN pUnkOuter)
1251 {
1252     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1253     return IDirectInputDevice2WImpl_CreateEffect(IDirectInputDevice8W_from_impl(This), rguid, lpeff, ppdef, pUnkOuter);
1254 }
1255
1256 HRESULT WINAPI IDirectInputDevice2AImpl_EnumEffects(
1257         LPDIRECTINPUTDEVICE8A iface,
1258         LPDIENUMEFFECTSCALLBACKA lpCallback,
1259         LPVOID lpvRef,
1260         DWORD dwFlags)
1261 {
1262     FIXME("(this=%p,%p,%p,0x%08x): stub!\n",
1263           iface, lpCallback, lpvRef, dwFlags);
1264     
1265     return DI_OK;
1266 }
1267
1268 HRESULT WINAPI IDirectInputDevice2WImpl_EnumEffects(
1269         LPDIRECTINPUTDEVICE8W iface,
1270         LPDIENUMEFFECTSCALLBACKW lpCallback,
1271         LPVOID lpvRef,
1272         DWORD dwFlags)
1273 {
1274     FIXME("(this=%p,%p,%p,0x%08x): stub!\n",
1275           iface, lpCallback, lpvRef, dwFlags);
1276     
1277     return DI_OK;
1278 }
1279
1280 HRESULT WINAPI IDirectInputDevice2AImpl_GetEffectInfo(
1281         LPDIRECTINPUTDEVICE8A iface,
1282         LPDIEFFECTINFOA lpdei,
1283         REFGUID rguid)
1284 {
1285     FIXME("(this=%p,%p,%s): stub!\n",
1286           iface, lpdei, debugstr_guid(rguid));
1287     return DI_OK;
1288 }
1289
1290 HRESULT WINAPI IDirectInputDevice2WImpl_GetEffectInfo(
1291         LPDIRECTINPUTDEVICE8W iface,
1292         LPDIEFFECTINFOW lpdei,
1293         REFGUID rguid)
1294 {
1295     FIXME("(this=%p,%p,%s): stub!\n",
1296           iface, lpdei, debugstr_guid(rguid));
1297     return DI_OK;
1298 }
1299
1300 HRESULT WINAPI IDirectInputDevice2WImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8W iface, LPDWORD pdwOut)
1301 {
1302     FIXME("(this=%p,%p): stub!\n", iface, pdwOut);
1303     return DI_OK;
1304 }
1305
1306 HRESULT WINAPI IDirectInputDevice2AImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8A iface, LPDWORD pdwOut)
1307 {
1308     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1309     return IDirectInputDevice2WImpl_GetForceFeedbackState(IDirectInputDevice8W_from_impl(This), pdwOut);
1310 }
1311
1312 HRESULT WINAPI IDirectInputDevice2WImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8W iface, DWORD dwFlags)
1313 {
1314     TRACE("(%p) 0x%08x:\n", iface, dwFlags);
1315     return DI_NOEFFECT;
1316 }
1317
1318 HRESULT WINAPI IDirectInputDevice2AImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8A iface, DWORD dwFlags)
1319 {
1320     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1321     return IDirectInputDevice2WImpl_SendForceFeedbackCommand(IDirectInputDevice8W_from_impl(This), dwFlags);
1322 }
1323
1324 HRESULT WINAPI IDirectInputDevice2WImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8W iface,
1325         LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback, LPVOID lpvRef, DWORD dwFlags)
1326 {
1327     FIXME("(this=%p,%p,%p,0x%08x): stub!\n", iface, lpCallback, lpvRef, dwFlags);
1328     return DI_OK;
1329 }
1330
1331 HRESULT WINAPI IDirectInputDevice2AImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8A iface,
1332         LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback, LPVOID lpvRef, DWORD dwFlags)
1333 {
1334     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1335     return IDirectInputDevice2WImpl_EnumCreatedEffectObjects(IDirectInputDevice8W_from_impl(This), lpCallback, lpvRef, dwFlags);
1336 }
1337
1338 HRESULT WINAPI IDirectInputDevice2WImpl_Escape(LPDIRECTINPUTDEVICE8W iface, LPDIEFFESCAPE lpDIEEsc)
1339 {
1340     FIXME("(this=%p,%p): stub!\n", iface, lpDIEEsc);
1341     return DI_OK;
1342 }
1343
1344 HRESULT WINAPI IDirectInputDevice2AImpl_Escape(LPDIRECTINPUTDEVICE8A iface, LPDIEFFESCAPE lpDIEEsc)
1345 {
1346     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1347     return IDirectInputDevice2WImpl_Escape(IDirectInputDevice8W_from_impl(This), lpDIEEsc);
1348 }
1349
1350 HRESULT WINAPI IDirectInputDevice2WImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
1351 {
1352     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1353
1354     if (!This->acquired) return DIERR_NOTACQUIRED;
1355     /* Because wine devices do not need to be polled, just return DI_NOEFFECT */
1356     return DI_NOEFFECT;
1357 }
1358
1359 HRESULT WINAPI IDirectInputDevice2AImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
1360 {
1361     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1362     return IDirectInputDevice2WImpl_Poll(IDirectInputDevice8W_from_impl(This));
1363 }
1364
1365 HRESULT WINAPI IDirectInputDevice2WImpl_SendDeviceData(LPDIRECTINPUTDEVICE8W iface, DWORD cbObjectData,
1366                                                        LPCDIDEVICEOBJECTDATA rgdod, LPDWORD pdwInOut,
1367                                                        DWORD dwFlags)
1368 {
1369     FIXME("(this=%p,0x%08x,%p,%p,0x%08x): stub!\n", iface, cbObjectData, rgdod, pdwInOut, dwFlags);
1370
1371     return DI_OK;
1372 }
1373
1374 HRESULT WINAPI IDirectInputDevice2AImpl_SendDeviceData(LPDIRECTINPUTDEVICE8A iface, DWORD cbObjectData,
1375                                                        LPCDIDEVICEOBJECTDATA rgdod, LPDWORD pdwInOut,
1376                                                        DWORD dwFlags)
1377 {
1378     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1379     return IDirectInputDevice2WImpl_SendDeviceData(IDirectInputDevice8W_from_impl(This), cbObjectData, rgdod,
1380                                                    pdwInOut, dwFlags);
1381 }
1382
1383 HRESULT WINAPI IDirectInputDevice7AImpl_EnumEffectsInFile(LPDIRECTINPUTDEVICE8A iface,
1384                                                           LPCSTR lpszFileName,
1385                                                           LPDIENUMEFFECTSINFILECALLBACK pec,
1386                                                           LPVOID pvRef,
1387                                                           DWORD dwFlags)
1388 {
1389     FIXME("(%p)->(%s,%p,%p,%08x): stub !\n", iface, lpszFileName, pec, pvRef, dwFlags);
1390     
1391     return DI_OK;
1392 }
1393
1394 HRESULT WINAPI IDirectInputDevice7WImpl_EnumEffectsInFile(LPDIRECTINPUTDEVICE8W iface,
1395                                                           LPCWSTR lpszFileName,
1396                                                           LPDIENUMEFFECTSINFILECALLBACK pec,
1397                                                           LPVOID pvRef,
1398                                                           DWORD dwFlags)
1399 {
1400     FIXME("(%p)->(%s,%p,%p,%08x): stub !\n", iface, debugstr_w(lpszFileName), pec, pvRef, dwFlags);
1401     
1402     return DI_OK;
1403 }
1404
1405 HRESULT WINAPI IDirectInputDevice7AImpl_WriteEffectToFile(LPDIRECTINPUTDEVICE8A iface,
1406                                                           LPCSTR lpszFileName,
1407                                                           DWORD dwEntries,
1408                                                           LPDIFILEEFFECT rgDiFileEft,
1409                                                           DWORD dwFlags)
1410 {
1411     FIXME("(%p)->(%s,%08x,%p,%08x): stub !\n", iface, lpszFileName, dwEntries, rgDiFileEft, dwFlags);
1412     
1413     return DI_OK;
1414 }
1415
1416 HRESULT WINAPI IDirectInputDevice7WImpl_WriteEffectToFile(LPDIRECTINPUTDEVICE8W iface,
1417                                                           LPCWSTR lpszFileName,
1418                                                           DWORD dwEntries,
1419                                                           LPDIFILEEFFECT rgDiFileEft,
1420                                                           DWORD dwFlags)
1421 {
1422     FIXME("(%p)->(%s,%08x,%p,%08x): stub !\n", iface, debugstr_w(lpszFileName), dwEntries, rgDiFileEft, dwFlags);
1423     
1424     return DI_OK;
1425 }
1426
1427 HRESULT WINAPI IDirectInputDevice8WImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
1428                                                        LPDIACTIONFORMATW lpdiaf,
1429                                                        LPCWSTR lpszUserName,
1430                                                        DWORD dwFlags)
1431 {
1432     FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
1433 #define X(x) if (dwFlags & x) FIXME("\tdwFlags =|"#x"\n");
1434         X(DIDBAM_DEFAULT)
1435         X(DIDBAM_PRESERVE)
1436         X(DIDBAM_INITIALIZE)
1437         X(DIDBAM_HWDEFAULTS)
1438 #undef X
1439   
1440     return DI_OK;
1441 }
1442
1443 HRESULT WINAPI IDirectInputDevice8WImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
1444                                                      LPDIACTIONFORMATW lpdiaf,
1445                                                      LPCWSTR lpszUserName,
1446                                                      DWORD dwFlags)
1447 {
1448     DIPROPDWORD dp;
1449     DIPROPRANGE dpr;
1450
1451     FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
1452
1453     dpr.diph.dwSize = sizeof(DIPROPRANGE);
1454     dpr.lMin = lpdiaf->lAxisMin;
1455     dpr.lMax = lpdiaf->lAxisMax;
1456     dpr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
1457     dpr.diph.dwHow = DIPH_DEVICE;
1458     IDirectInputDevice8_SetProperty(iface, DIPROP_RANGE, &dpr.diph);
1459
1460     if (lpdiaf->dwBufferSize > 0)
1461     {
1462         dp.diph.dwSize = sizeof(DIPROPDWORD);
1463         dp.dwData = lpdiaf->dwBufferSize;
1464         dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
1465         dp.diph.dwHow = DIPH_DEVICE;
1466         IDirectInputDevice8_SetProperty(iface, DIPROP_BUFFERSIZE, &dp.diph);
1467     }
1468
1469     return DI_OK;
1470 }
1471
1472 HRESULT WINAPI IDirectInputDevice8AImpl_GetImageInfo(LPDIRECTINPUTDEVICE8A iface,
1473                                                      LPDIDEVICEIMAGEINFOHEADERA lpdiDevImageInfoHeader)
1474 {
1475     FIXME("(%p)->(%p): stub !\n", iface, lpdiDevImageInfoHeader);
1476     
1477     return DI_OK;
1478 }
1479
1480 HRESULT WINAPI IDirectInputDevice8WImpl_GetImageInfo(LPDIRECTINPUTDEVICE8W iface,
1481                                                      LPDIDEVICEIMAGEINFOHEADERW lpdiDevImageInfoHeader)
1482 {
1483     FIXME("(%p)->(%p): stub !\n", iface, lpdiDevImageInfoHeader);
1484     
1485     return DI_OK;
1486 }