dinput: BuildActionMap and SetActionMap stubs for generic joystick.
[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 static HRESULT create_DataFormat(LPCDIDATAFORMAT asked_format, DataFormat *format)
370 {
371     DataTransform *dt;
372     unsigned int i, j;
373     int same = 1;
374     int *done;
375     int index = 0;
376     DWORD next = 0;
377
378     if (!format->wine_df) return DIERR_INVALIDPARAM;
379     done = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, asked_format->dwNumObjs * sizeof(int));
380     dt = HeapAlloc(GetProcessHeap(), 0, asked_format->dwNumObjs * sizeof(DataTransform));
381     if (!dt || !done) goto failed;
382
383     if (!(format->offsets = HeapAlloc(GetProcessHeap(), 0, format->wine_df->dwNumObjs * sizeof(int))))
384         goto failed;
385
386     if (!(format->user_df = HeapAlloc(GetProcessHeap(), 0, asked_format->dwSize)))
387         goto failed;
388     memcpy(format->user_df, asked_format, asked_format->dwSize);
389
390     TRACE("Creating DataTransform :\n");
391     
392     for (i = 0; i < format->wine_df->dwNumObjs; i++)
393     {
394         format->offsets[i] = -1;
395
396         for (j = 0; j < asked_format->dwNumObjs; j++) {
397             if (done[j] == 1)
398                 continue;
399             
400             if (/* Check if the application either requests any GUID and if not, it if matches
401                  * the GUID of the Wine object.
402                  */
403                 ((asked_format->rgodf[j].pguid == NULL) ||
404                  (format->wine_df->rgodf[i].pguid == NULL) ||
405                  (IsEqualGUID(format->wine_df->rgodf[i].pguid, asked_format->rgodf[j].pguid)))
406                 &&
407                 (/* Then check if it accepts any instance id, and if not, if it matches Wine's
408                   * instance id.
409                   */
410                  ((asked_format->rgodf[j].dwType & DIDFT_INSTANCEMASK) == DIDFT_ANYINSTANCE) ||
411                  (DIDFT_GETINSTANCE(asked_format->rgodf[j].dwType) == 0x00FF) || /* This is mentionned in no DX docs, but it works fine - tested on WinXP */
412                  (DIDFT_GETINSTANCE(asked_format->rgodf[j].dwType) == DIDFT_GETINSTANCE(format->wine_df->rgodf[i].dwType)))
413                 &&
414                 ( /* Then if the asked type matches the one Wine provides */
415                  DIDFT_GETTYPE(asked_format->rgodf[j].dwType) & format->wine_df->rgodf[i].dwType))
416             {
417                 done[j] = 1;
418                 
419                 TRACE("Matching :\n");
420                 TRACE("   - Asked (%d) :\n", j);
421                 TRACE("       * GUID: %s ('%s')\n",
422                       debugstr_guid(asked_format->rgodf[j].pguid),
423                       _dump_dinput_GUID(asked_format->rgodf[j].pguid));
424                 TRACE("       * Offset: %3d\n", asked_format->rgodf[j].dwOfs);
425                 TRACE("       * dwType: %08x\n", asked_format->rgodf[j].dwType);
426                 TRACE("         "); _dump_EnumObjects_flags(asked_format->rgodf[j].dwType); TRACE("\n");
427                 
428                 TRACE("   - Wine  (%d) :\n", i);
429                 TRACE("       * GUID: %s ('%s')\n",
430                       debugstr_guid(format->wine_df->rgodf[i].pguid),
431                       _dump_dinput_GUID(format->wine_df->rgodf[i].pguid));
432                 TRACE("       * Offset: %3d\n", format->wine_df->rgodf[i].dwOfs);
433                 TRACE("       * dwType: %08x\n", format->wine_df->rgodf[i].dwType);
434                 TRACE("         "); _dump_EnumObjects_flags(format->wine_df->rgodf[i].dwType); TRACE("\n");
435                 
436                 if (format->wine_df->rgodf[i].dwType & DIDFT_BUTTON)
437                     dt[index].size = sizeof(BYTE);
438                 else
439                     dt[index].size = sizeof(DWORD);
440                 dt[index].offset_in = format->wine_df->rgodf[i].dwOfs;
441                 dt[index].offset_out = asked_format->rgodf[j].dwOfs;
442                 format->offsets[i]   = asked_format->rgodf[j].dwOfs;
443                 dt[index].value = 0;
444                 next = next + dt[index].size;
445                 
446                 if (format->wine_df->rgodf[i].dwOfs != dt[index].offset_out)
447                     same = 0;
448                 
449                 index++;
450                 break;
451             }
452         }
453     }
454
455     TRACE("Setting to default value :\n");
456     for (j = 0; j < asked_format->dwNumObjs; j++) {
457         if (done[j] == 0) {
458             TRACE("   - Asked (%d) :\n", j);
459             TRACE("       * GUID: %s ('%s')\n",
460                   debugstr_guid(asked_format->rgodf[j].pguid),
461                   _dump_dinput_GUID(asked_format->rgodf[j].pguid));
462             TRACE("       * Offset: %3d\n", asked_format->rgodf[j].dwOfs);
463             TRACE("       * dwType: %08x\n", asked_format->rgodf[j].dwType);
464             TRACE("         "); _dump_EnumObjects_flags(asked_format->rgodf[j].dwType); TRACE("\n");
465             
466             if (asked_format->rgodf[j].dwType & DIDFT_BUTTON)
467                 dt[index].size = sizeof(BYTE);
468             else
469                 dt[index].size = sizeof(DWORD);
470             dt[index].offset_in  = -1;
471             dt[index].offset_out = asked_format->rgodf[j].dwOfs;
472             if (asked_format->rgodf[j].dwType & DIDFT_POV)
473                 dt[index].value = -1;
474             else
475                 dt[index].value = 0;
476             index++;
477
478             same = 0;
479         }
480     }
481     
482     format->internal_format_size = format->wine_df->dwDataSize;
483     format->size = index;
484     if (same) {
485         HeapFree(GetProcessHeap(), 0, dt);
486         dt = NULL;
487     }
488     format->dt = dt;
489
490     HeapFree(GetProcessHeap(), 0, done);
491
492     return DI_OK;
493
494 failed:
495     HeapFree(GetProcessHeap(), 0, done);
496     HeapFree(GetProcessHeap(), 0, dt);
497     format->dt = NULL;
498     HeapFree(GetProcessHeap(), 0, format->offsets);
499     format->offsets = NULL;
500     HeapFree(GetProcessHeap(), 0, format->user_df);
501     format->user_df = NULL;
502
503     return DIERR_OUTOFMEMORY;
504 }
505
506 /* find an object by it's offset in a data format */
507 static int offset_to_object(const DataFormat *df, int offset)
508 {
509     int i;
510
511     if (!df->offsets) return -1;
512
513     for (i = 0; i < df->wine_df->dwNumObjs; i++)
514         if (df->offsets[i] == offset) return i;
515
516     return -1;
517 }
518
519 int id_to_object(LPCDIDATAFORMAT df, int id)
520 {
521     int i;
522
523     id &= 0x00ffffff;
524     for (i = 0; i < df->dwNumObjs; i++)
525         if ((dataformat_to_odf(df, i)->dwType & 0x00ffffff) == id)
526             return i;
527
528     return -1;
529 }
530
531 static int id_to_offset(const DataFormat *df, int id)
532 {
533     int obj = id_to_object(df->wine_df, id);
534
535     return obj >= 0 && df->offsets ? df->offsets[obj] : -1;
536 }
537
538 int find_property(const DataFormat *df, LPCDIPROPHEADER ph)
539 {
540     switch (ph->dwHow)
541     {
542         case DIPH_BYID:     return id_to_object(df->wine_df, ph->dwObj);
543         case DIPH_BYOFFSET: return offset_to_object(df, ph->dwObj);
544     }
545     FIXME("Unhandled ph->dwHow=='%04X'\n", (unsigned int)ph->dwHow);
546
547     return -1;
548 }
549
550 DWORD semantic_to_obj_id(IDirectInputDeviceImpl* This, DWORD dwSemantic)
551 {
552     DWORD type = (0x0000ff00 & dwSemantic) >> 8;
553     DWORD offset = 0x000000ff & dwSemantic;
554     DWORD obj_instance = 0;
555     DWORD found = 0;
556     int i;
557
558     for (i = 0; i < This->data_format.wine_df->dwNumObjs; i++)
559     {
560         LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(This->data_format.wine_df, i);
561
562         if (odf->dwOfs == offset)
563         {
564             obj_instance = DIDFT_GETINSTANCE(odf->dwType);
565             found = 1;
566             break;
567         }
568     }
569
570     if (!found) return 0;
571
572     if (type & DIDFT_AXIS)   type = DIDFT_RELAXIS;
573     if (type & DIDFT_BUTTON) type = DIDFT_PSHBUTTON;
574
575     return type | (0x0000ff00 & (obj_instance << 8));
576 }
577
578 /******************************************************************************
579  *      queue_event - add new event to the ring queue
580  */
581
582 void queue_event(LPDIRECTINPUTDEVICE8A iface, int inst_id, DWORD data, DWORD time, DWORD seq)
583 {
584     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
585     int next_pos, ofs = id_to_offset(&This->data_format, inst_id);
586
587     /* Event is being set regardless of the queue state */
588     if (This->hEvent) SetEvent(This->hEvent);
589
590     if (!This->queue_len || This->overflow || ofs < 0) return;
591
592     next_pos = (This->queue_head + 1) % This->queue_len;
593     if (next_pos == This->queue_tail)
594     {
595         TRACE(" queue overflowed\n");
596         This->overflow = TRUE;
597         return;
598     }
599
600     TRACE(" queueing %d at offset %d (queue head %d / size %d)\n",
601           data, ofs, This->queue_head, This->queue_len);
602
603     This->data_queue[This->queue_head].dwOfs       = ofs;
604     This->data_queue[This->queue_head].dwData      = data;
605     This->data_queue[This->queue_head].dwTimeStamp = time;
606     This->data_queue[This->queue_head].dwSequence  = seq;
607
608     /* Set uAppData by means of action mapping */
609     if (This->num_actions > 0)
610     {
611         int i;
612         for (i=0; i < This->num_actions; i++)
613         {
614             if (This->action_map[i].offset == ofs)
615             {
616                 TRACE("Offset %d mapped to uAppData %lu\n", ofs, This->action_map[i].uAppData);
617                 This->data_queue[This->queue_head].uAppData = This->action_map[i].uAppData;
618                 break;
619             }
620         }
621     }
622
623     This->queue_head = next_pos;
624     /* Send event if asked */
625 }
626
627 /******************************************************************************
628  *      Acquire
629  */
630
631 HRESULT WINAPI IDirectInputDevice2WImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
632 {
633     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
634     HRESULT res;
635
636     if (!This->data_format.user_df) return DIERR_INVALIDPARAM;
637     if (This->dwCoopLevel & DISCL_FOREGROUND && This->win != GetForegroundWindow())
638         return DIERR_OTHERAPPHASPRIO;
639
640     EnterCriticalSection(&This->crit);
641     res = This->acquired ? S_FALSE : DI_OK;
642     This->acquired = 1;
643     if (res == DI_OK)
644     {
645         This->queue_head = This->queue_tail = This->overflow = 0;
646         check_dinput_hooks(iface);
647     }
648     LeaveCriticalSection(&This->crit);
649
650     return res;
651 }
652
653 HRESULT WINAPI IDirectInputDevice2AImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
654 {
655     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
656     return IDirectInputDevice2WImpl_Acquire(IDirectInputDevice8W_from_impl(This));
657 }
658
659
660 /******************************************************************************
661  *      Unacquire
662  */
663
664 HRESULT WINAPI IDirectInputDevice2WImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
665 {
666     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
667     HRESULT res;
668
669     EnterCriticalSection(&This->crit);
670     res = !This->acquired ? DI_NOEFFECT : DI_OK;
671     This->acquired = 0;
672     if (res == DI_OK)
673         check_dinput_hooks(iface);
674     LeaveCriticalSection(&This->crit);
675
676     return res;
677 }
678
679 HRESULT WINAPI IDirectInputDevice2AImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
680 {
681     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
682     return IDirectInputDevice2WImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
683 }
684
685 /******************************************************************************
686  *      IDirectInputDeviceA
687  */
688
689 HRESULT WINAPI IDirectInputDevice2WImpl_SetDataFormat(LPDIRECTINPUTDEVICE8W iface, LPCDIDATAFORMAT df)
690 {
691     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
692     HRESULT res = DI_OK;
693
694     if (!df) return E_POINTER;
695     TRACE("(%p) %p\n", This, df);
696     _dump_DIDATAFORMAT(df);
697
698     if (df->dwSize != sizeof(DIDATAFORMAT)) return DIERR_INVALIDPARAM;
699     if (This->acquired) return DIERR_ACQUIRED;
700
701     EnterCriticalSection(&This->crit);
702
703     release_DataFormat(&This->data_format);
704     res = create_DataFormat(df, &This->data_format);
705
706     LeaveCriticalSection(&This->crit);
707     return res;
708 }
709
710 HRESULT WINAPI IDirectInputDevice2AImpl_SetDataFormat(LPDIRECTINPUTDEVICE8A iface, LPCDIDATAFORMAT df)
711 {
712     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
713     return IDirectInputDevice2WImpl_SetDataFormat(IDirectInputDevice8W_from_impl(This), df);
714 }
715
716 /******************************************************************************
717   *     SetCooperativeLevel
718   *
719   *  Set cooperative level and the source window for the events.
720   */
721 HRESULT WINAPI IDirectInputDevice2WImpl_SetCooperativeLevel(LPDIRECTINPUTDEVICE8W iface, HWND hwnd, DWORD dwflags)
722 {
723     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
724
725     TRACE("(%p) %p,0x%08x\n", This, hwnd, dwflags);
726     _dump_cooperativelevel_DI(dwflags);
727
728     if ((dwflags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == 0 ||
729         (dwflags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE) ||
730         (dwflags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == 0 ||
731         (dwflags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == (DISCL_FOREGROUND | DISCL_BACKGROUND))
732         return DIERR_INVALIDPARAM;
733
734     if (dwflags == (DISCL_NONEXCLUSIVE | DISCL_BACKGROUND))
735         hwnd = GetDesktopWindow();
736
737     if (!hwnd) return E_HANDLE;
738
739     /* For security reasons native does not allow exclusive background level
740        for mouse and keyboard only */
741     if (dwflags & DISCL_EXCLUSIVE && dwflags & DISCL_BACKGROUND &&
742         (IsEqualGUID(&This->guid, &GUID_SysMouse) ||
743          IsEqualGUID(&This->guid, &GUID_SysKeyboard)))
744         return DIERR_UNSUPPORTED;
745
746     /* Store the window which asks for the mouse */
747     EnterCriticalSection(&This->crit);
748     This->win = hwnd;
749     This->dwCoopLevel = dwflags;
750     LeaveCriticalSection(&This->crit);
751
752     return DI_OK;
753 }
754
755 HRESULT WINAPI IDirectInputDevice2AImpl_SetCooperativeLevel(LPDIRECTINPUTDEVICE8A iface, HWND hwnd, DWORD dwflags)
756 {
757     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
758     return IDirectInputDevice2WImpl_SetCooperativeLevel(IDirectInputDevice8W_from_impl(This), hwnd, dwflags);
759 }
760
761 /******************************************************************************
762   *     SetEventNotification : specifies event to be sent on state change
763   */
764 HRESULT WINAPI IDirectInputDevice2WImpl_SetEventNotification(LPDIRECTINPUTDEVICE8W iface, HANDLE event)
765 {
766     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
767
768     TRACE("(%p) %p\n", This, event);
769
770     EnterCriticalSection(&This->crit);
771     This->hEvent = event;
772     LeaveCriticalSection(&This->crit);
773     return DI_OK;
774 }
775
776 HRESULT WINAPI IDirectInputDevice2AImpl_SetEventNotification(LPDIRECTINPUTDEVICE8A iface, HANDLE event)
777 {
778     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
779     return IDirectInputDevice2WImpl_SetEventNotification(IDirectInputDevice8W_from_impl(This), event);
780 }
781
782
783 ULONG WINAPI IDirectInputDevice2WImpl_Release(LPDIRECTINPUTDEVICE8W iface)
784 {
785     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
786     ULONG ref;
787
788     ref = InterlockedDecrement(&(This->ref));
789     if (ref) return ref;
790
791     IDirectInputDevice_Unacquire(iface);
792     /* Reset the FF state, free all effects, etc */
793     IDirectInputDevice8_SendForceFeedbackCommand(iface, DISFFC_RESET);
794
795     HeapFree(GetProcessHeap(), 0, This->data_queue);
796
797     /* Free data format */
798     HeapFree(GetProcessHeap(), 0, This->data_format.wine_df->rgodf);
799     HeapFree(GetProcessHeap(), 0, This->data_format.wine_df);
800     release_DataFormat(&This->data_format);
801
802     /* Free action mapping */
803     HeapFree(GetProcessHeap(), 0, This->action_map);
804
805     EnterCriticalSection( &This->dinput->crit );
806     list_remove( &This->entry );
807     LeaveCriticalSection( &This->dinput->crit );
808
809     IDirectInput_Release(&This->dinput->IDirectInput7A_iface);
810     This->crit.DebugInfo->Spare[0] = 0;
811     DeleteCriticalSection(&This->crit);
812
813     HeapFree(GetProcessHeap(), 0, This);
814
815     return DI_OK;
816 }
817
818 ULONG WINAPI IDirectInputDevice2AImpl_Release(LPDIRECTINPUTDEVICE8A iface)
819 {
820     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
821     return IDirectInputDevice2WImpl_Release(IDirectInputDevice8W_from_impl(This));
822 }
823
824 HRESULT WINAPI IDirectInputDevice2WImpl_QueryInterface(LPDIRECTINPUTDEVICE8W iface, REFIID riid, LPVOID *ppobj)
825 {
826     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
827
828     TRACE("(%p this=%p,%s,%p)\n", iface, This, debugstr_guid(riid), ppobj);
829     if (IsEqualGUID(&IID_IUnknown, riid) ||
830         IsEqualGUID(&IID_IDirectInputDeviceA,  riid) ||
831         IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
832         IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
833         IsEqualGUID(&IID_IDirectInputDevice8A, riid))
834     {
835         IDirectInputDevice2_AddRef(iface);
836         *ppobj = IDirectInputDevice8A_from_impl(This);
837         return DI_OK;
838     }
839     if (IsEqualGUID(&IID_IDirectInputDeviceW,  riid) ||
840         IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
841         IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
842         IsEqualGUID(&IID_IDirectInputDevice8W, riid))
843     {
844         IDirectInputDevice2_AddRef(iface);
845         *ppobj = IDirectInputDevice8W_from_impl(This);
846         return DI_OK;
847     }
848
849     WARN("Unsupported interface!\n");
850     return E_FAIL;
851 }
852
853 HRESULT WINAPI IDirectInputDevice2AImpl_QueryInterface(LPDIRECTINPUTDEVICE8A iface, REFIID riid, LPVOID *ppobj)
854 {
855     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
856     return IDirectInputDevice2WImpl_QueryInterface(IDirectInputDevice8W_from_impl(This), riid, ppobj);
857 }
858
859 ULONG WINAPI IDirectInputDevice2WImpl_AddRef(LPDIRECTINPUTDEVICE8W iface)
860 {
861     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
862     return InterlockedIncrement(&This->ref);
863 }
864
865 ULONG WINAPI IDirectInputDevice2AImpl_AddRef(LPDIRECTINPUTDEVICE8A iface)
866 {
867     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
868     return IDirectInputDevice2WImpl_AddRef(IDirectInputDevice8W_from_impl(This));
869 }
870
871 HRESULT WINAPI IDirectInputDevice2AImpl_EnumObjects(LPDIRECTINPUTDEVICE8A iface,
872         LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback, LPVOID lpvRef, DWORD dwFlags)
873 {
874     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
875     DIDEVICEOBJECTINSTANCEA ddoi;
876     int i;
877
878     TRACE("(%p) %p,%p flags:%08x)\n", iface, lpCallback, lpvRef, dwFlags);
879     TRACE("  - flags = ");
880     _dump_EnumObjects_flags(dwFlags);
881     TRACE("\n");
882
883     /* Only the fields till dwFFMaxForce are relevant */
884     memset(&ddoi, 0, sizeof(ddoi));
885     ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
886
887     for (i = 0; i < This->data_format.wine_df->dwNumObjs; i++)
888     {
889         LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(This->data_format.wine_df, i);
890
891         if (dwFlags != DIDFT_ALL && !(dwFlags & DIEFT_GETTYPE(odf->dwType))) continue;
892         if (IDirectInputDevice_GetObjectInfo(iface, &ddoi, odf->dwType, DIPH_BYID) != DI_OK)
893             continue;
894
895         if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) break;
896     }
897
898     return DI_OK;
899 }
900
901 HRESULT WINAPI IDirectInputDevice2WImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface,
902         LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback, LPVOID lpvRef, DWORD dwFlags)
903 {
904     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
905     DIDEVICEOBJECTINSTANCEW ddoi;
906     int i;
907
908     TRACE("(%p) %p,%p flags:%08x)\n", iface, lpCallback, lpvRef, dwFlags);
909     TRACE("  - flags = ");
910     _dump_EnumObjects_flags(dwFlags);
911     TRACE("\n");
912
913     /* Only the fields till dwFFMaxForce are relevant */
914     memset(&ddoi, 0, sizeof(ddoi));
915     ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, dwFFMaxForce);
916
917     for (i = 0; i < This->data_format.wine_df->dwNumObjs; i++)
918     {
919         LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(This->data_format.wine_df, i);
920
921         if (dwFlags != DIDFT_ALL && !(dwFlags & DIEFT_GETTYPE(odf->dwType))) continue;
922         if (IDirectInputDevice_GetObjectInfo(iface, &ddoi, odf->dwType, DIPH_BYID) != DI_OK)
923             continue;
924
925         if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) break;
926     }
927
928     return DI_OK;
929 }
930
931 /******************************************************************************
932  *      GetProperty
933  */
934
935 HRESULT WINAPI IDirectInputDevice2WImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
936 {
937     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
938
939     TRACE("(%p) %s,%p\n", iface, debugstr_guid(rguid), pdiph);
940     _dump_DIPROPHEADER(pdiph);
941
942     if (!IS_DIPROP(rguid)) return DI_OK;
943
944     switch (LOWORD(rguid))
945     {
946         case (DWORD_PTR) DIPROP_BUFFERSIZE:
947         {
948             LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
949
950             if (pdiph->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
951
952             pd->dwData = This->queue_len;
953             TRACE("buffersize = %d\n", pd->dwData);
954             break;
955         }
956         case (DWORD_PTR) DIPROP_VIDPID:
957             FIXME("DIPROP_VIDPID not implemented\n");
958             return DIERR_UNSUPPORTED;
959         default:
960             FIXME("Unknown property %s\n", debugstr_guid(rguid));
961             return DIERR_INVALIDPARAM;
962     }
963
964     return DI_OK;
965 }
966
967 HRESULT WINAPI IDirectInputDevice2AImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
968 {
969     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
970     return IDirectInputDevice2WImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
971 }
972
973 /******************************************************************************
974  *      SetProperty
975  */
976
977 HRESULT WINAPI IDirectInputDevice2WImpl_SetProperty(
978         LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER pdiph)
979 {
980     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
981
982     TRACE("(%p) %s,%p\n", iface, debugstr_guid(rguid), pdiph);
983     _dump_DIPROPHEADER(pdiph);
984
985     if (!IS_DIPROP(rguid)) return DI_OK;
986
987     switch (LOWORD(rguid))
988     {
989         case (DWORD_PTR) DIPROP_AXISMODE:
990         {
991             LPCDIPROPDWORD pd = (LPCDIPROPDWORD)pdiph;
992
993             if (pdiph->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
994             if (pdiph->dwHow == DIPH_DEVICE && pdiph->dwObj) return DIERR_INVALIDPARAM;
995             if (This->acquired) return DIERR_ACQUIRED;
996             if (pdiph->dwHow != DIPH_DEVICE) return DIERR_UNSUPPORTED;
997             if (!This->data_format.user_df) return DI_OK;
998
999             TRACE("Axis mode: %s\n", pd->dwData == DIPROPAXISMODE_ABS ? "absolute" :
1000                                                                         "relative");
1001
1002             EnterCriticalSection(&This->crit);
1003             This->data_format.user_df->dwFlags &= ~DIDFT_AXIS;
1004             This->data_format.user_df->dwFlags |= pd->dwData == DIPROPAXISMODE_ABS ?
1005                                                   DIDF_ABSAXIS : DIDF_RELAXIS;
1006             LeaveCriticalSection(&This->crit);
1007             break;
1008         }
1009         case (DWORD_PTR) DIPROP_BUFFERSIZE:
1010         {
1011             LPCDIPROPDWORD pd = (LPCDIPROPDWORD)pdiph;
1012
1013             if (pdiph->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
1014             if (This->acquired) return DIERR_ACQUIRED;
1015
1016             TRACE("buffersize = %d\n", pd->dwData);
1017
1018             EnterCriticalSection(&This->crit);
1019             HeapFree(GetProcessHeap(), 0, This->data_queue);
1020
1021             This->data_queue = !pd->dwData ? NULL : HeapAlloc(GetProcessHeap(), 0,
1022                                 pd->dwData * sizeof(DIDEVICEOBJECTDATA));
1023             This->queue_head = This->queue_tail = This->overflow = 0;
1024             This->queue_len  = pd->dwData;
1025
1026             LeaveCriticalSection(&This->crit);
1027             break;
1028         }
1029         default:
1030             WARN("Unknown property %s\n", debugstr_guid(rguid));
1031             return DIERR_UNSUPPORTED;
1032     }
1033
1034     return DI_OK;
1035 }
1036
1037 HRESULT WINAPI IDirectInputDevice2AImpl_SetProperty(
1038         LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER pdiph)
1039 {
1040     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1041     return IDirectInputDevice2WImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
1042 }
1043
1044 HRESULT WINAPI IDirectInputDevice2AImpl_GetObjectInfo(
1045         LPDIRECTINPUTDEVICE8A iface,
1046         LPDIDEVICEOBJECTINSTANCEA pdidoi,
1047         DWORD dwObj,
1048         DWORD dwHow)
1049 {
1050     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1051     DIDEVICEOBJECTINSTANCEW didoiW;
1052     HRESULT res;
1053
1054     if (!pdidoi ||
1055         (pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCEA) &&
1056          pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCE_DX3A)))
1057         return DIERR_INVALIDPARAM;
1058
1059     didoiW.dwSize = sizeof(didoiW);
1060     res = IDirectInputDevice2WImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
1061     if (res == DI_OK)
1062     {
1063         DWORD dwSize = pdidoi->dwSize;
1064
1065         memset(pdidoi, 0, pdidoi->dwSize);
1066         pdidoi->dwSize   = dwSize;
1067         pdidoi->guidType = didoiW.guidType;
1068         pdidoi->dwOfs    = didoiW.dwOfs;
1069         pdidoi->dwType   = didoiW.dwType;
1070         pdidoi->dwFlags  = didoiW.dwFlags;
1071     }
1072
1073     return res;
1074 }
1075
1076 HRESULT WINAPI IDirectInputDevice2WImpl_GetObjectInfo(
1077         LPDIRECTINPUTDEVICE8W iface,
1078         LPDIDEVICEOBJECTINSTANCEW pdidoi,
1079         DWORD dwObj,
1080         DWORD dwHow)
1081 {
1082     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1083     DWORD dwSize;
1084     LPDIOBJECTDATAFORMAT odf;
1085     int idx = -1;
1086
1087     TRACE("(%p) %d(0x%08x) -> %p\n", This, dwHow, dwObj, pdidoi);
1088
1089     if (!pdidoi ||
1090         (pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCEW) &&
1091          pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCE_DX3W)))
1092         return DIERR_INVALIDPARAM;
1093
1094     switch (dwHow)
1095     {
1096     case DIPH_BYOFFSET:
1097         if (!This->data_format.offsets) break;
1098         for (idx = This->data_format.wine_df->dwNumObjs - 1; idx >= 0; idx--)
1099             if (This->data_format.offsets[idx] == dwObj) break;
1100         break;
1101     case DIPH_BYID:
1102         dwObj &= 0x00ffffff;
1103         for (idx = This->data_format.wine_df->dwNumObjs - 1; idx >= 0; idx--)
1104             if ((dataformat_to_odf(This->data_format.wine_df, idx)->dwType & 0x00ffffff) == dwObj)
1105                 break;
1106         break;
1107
1108     case DIPH_BYUSAGE:
1109         FIXME("dwHow = DIPH_BYUSAGE not implemented\n");
1110         break;
1111     default:
1112         WARN("invalid parameter: dwHow = %08x\n", dwHow);
1113         return DIERR_INVALIDPARAM;
1114     }
1115     if (idx < 0) return DIERR_OBJECTNOTFOUND;
1116
1117     odf = dataformat_to_odf(This->data_format.wine_df, idx);
1118     dwSize = pdidoi->dwSize; /* save due to memset below */
1119     memset(pdidoi, 0, pdidoi->dwSize);
1120     pdidoi->dwSize   = dwSize;
1121     if (odf->pguid) pdidoi->guidType = *odf->pguid;
1122     pdidoi->dwOfs    = This->data_format.offsets ? This->data_format.offsets[idx] : odf->dwOfs;
1123     pdidoi->dwType   = odf->dwType;
1124     pdidoi->dwFlags  = odf->dwFlags;
1125
1126     return DI_OK;
1127 }
1128
1129 HRESULT WINAPI IDirectInputDevice2WImpl_GetDeviceData(LPDIRECTINPUTDEVICE8W iface, DWORD dodsize,
1130                                                       LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
1131 {
1132     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1133     HRESULT ret = DI_OK;
1134     int len;
1135
1136     TRACE("(%p) %p -> %p(%d) x%d, 0x%08x\n",
1137           This, dod, entries, entries ? *entries : 0, dodsize, flags);
1138
1139     if (!This->acquired)
1140         return DIERR_NOTACQUIRED;
1141     if (!This->queue_len)
1142         return DIERR_NOTBUFFERED;
1143     if (dodsize < sizeof(DIDEVICEOBJECTDATA_DX3))
1144         return DIERR_INVALIDPARAM;
1145
1146     IDirectInputDevice2_Poll(iface);
1147     EnterCriticalSection(&This->crit);
1148
1149     len = This->queue_head - This->queue_tail;
1150     if (len < 0) len += This->queue_len;
1151
1152     if ((*entries != INFINITE) && (len > *entries)) len = *entries;
1153
1154     if (dod)
1155     {
1156         int i;
1157         for (i = 0; i < len; i++)
1158         {
1159             int n = (This->queue_tail + i) % This->queue_len;
1160             memcpy((char *)dod + dodsize * i, This->data_queue + n, dodsize);
1161         }
1162     }
1163     *entries = len;
1164
1165     if (This->overflow)
1166         ret = DI_BUFFEROVERFLOW;
1167
1168     if (!(flags & DIGDD_PEEK))
1169     {
1170         /* Advance reading position */
1171         This->queue_tail = (This->queue_tail + len) % This->queue_len;
1172         This->overflow = FALSE;
1173     }
1174
1175     LeaveCriticalSection(&This->crit);
1176
1177     TRACE("Returning %d events queued\n", *entries);
1178     return ret;
1179 }
1180
1181 HRESULT WINAPI IDirectInputDevice2AImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface, DWORD dodsize,
1182                                                       LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
1183 {
1184     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1185     return IDirectInputDevice2WImpl_GetDeviceData(IDirectInputDevice8W_from_impl(This), dodsize, dod, entries, flags);
1186 }
1187
1188 HRESULT WINAPI IDirectInputDevice2WImpl_RunControlPanel(LPDIRECTINPUTDEVICE8W iface, HWND hwndOwner, DWORD dwFlags)
1189 {
1190     FIXME("(this=%p,%p,0x%08x): stub!\n", iface, hwndOwner, dwFlags);
1191
1192     return DI_OK;
1193 }
1194
1195 HRESULT WINAPI IDirectInputDevice2AImpl_RunControlPanel(LPDIRECTINPUTDEVICE8A iface, HWND hwndOwner, DWORD dwFlags)
1196 {
1197     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1198     return IDirectInputDevice2WImpl_RunControlPanel(IDirectInputDevice8W_from_impl(This), hwndOwner, dwFlags);
1199 }
1200
1201 HRESULT WINAPI IDirectInputDevice2WImpl_Initialize(LPDIRECTINPUTDEVICE8W iface, HINSTANCE hinst, DWORD dwVersion,
1202                                                    REFGUID rguid)
1203 {
1204     FIXME("(this=%p,%p,%d,%s): stub!\n", iface, hinst, dwVersion, debugstr_guid(rguid));
1205     return DI_OK;
1206 }
1207
1208 HRESULT WINAPI IDirectInputDevice2AImpl_Initialize(LPDIRECTINPUTDEVICE8A iface, HINSTANCE hinst, DWORD dwVersion,
1209                                                    REFGUID rguid)
1210 {
1211     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1212     return IDirectInputDevice2WImpl_Initialize(IDirectInputDevice8W_from_impl(This), hinst, dwVersion, rguid);
1213 }
1214
1215 /******************************************************************************
1216  *      IDirectInputDevice2A
1217  */
1218
1219 HRESULT WINAPI IDirectInputDevice2WImpl_CreateEffect(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIEFFECT lpeff,
1220                                                      LPDIRECTINPUTEFFECT *ppdef, LPUNKNOWN pUnkOuter)
1221 {
1222     FIXME("(this=%p,%s,%p,%p,%p): stub!\n", iface, debugstr_guid(rguid), lpeff, ppdef, pUnkOuter);
1223     return DI_OK;
1224 }
1225
1226 HRESULT WINAPI IDirectInputDevice2AImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIEFFECT lpeff,
1227                                                      LPDIRECTINPUTEFFECT *ppdef, LPUNKNOWN pUnkOuter)
1228 {
1229     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1230     return IDirectInputDevice2WImpl_CreateEffect(IDirectInputDevice8W_from_impl(This), rguid, lpeff, ppdef, pUnkOuter);
1231 }
1232
1233 HRESULT WINAPI IDirectInputDevice2AImpl_EnumEffects(
1234         LPDIRECTINPUTDEVICE8A iface,
1235         LPDIENUMEFFECTSCALLBACKA lpCallback,
1236         LPVOID lpvRef,
1237         DWORD dwFlags)
1238 {
1239     FIXME("(this=%p,%p,%p,0x%08x): stub!\n",
1240           iface, lpCallback, lpvRef, dwFlags);
1241     
1242     return DI_OK;
1243 }
1244
1245 HRESULT WINAPI IDirectInputDevice2WImpl_EnumEffects(
1246         LPDIRECTINPUTDEVICE8W iface,
1247         LPDIENUMEFFECTSCALLBACKW lpCallback,
1248         LPVOID lpvRef,
1249         DWORD dwFlags)
1250 {
1251     FIXME("(this=%p,%p,%p,0x%08x): stub!\n",
1252           iface, lpCallback, lpvRef, dwFlags);
1253     
1254     return DI_OK;
1255 }
1256
1257 HRESULT WINAPI IDirectInputDevice2AImpl_GetEffectInfo(
1258         LPDIRECTINPUTDEVICE8A iface,
1259         LPDIEFFECTINFOA lpdei,
1260         REFGUID rguid)
1261 {
1262     FIXME("(this=%p,%p,%s): stub!\n",
1263           iface, lpdei, debugstr_guid(rguid));
1264     return DI_OK;
1265 }
1266
1267 HRESULT WINAPI IDirectInputDevice2WImpl_GetEffectInfo(
1268         LPDIRECTINPUTDEVICE8W iface,
1269         LPDIEFFECTINFOW lpdei,
1270         REFGUID rguid)
1271 {
1272     FIXME("(this=%p,%p,%s): stub!\n",
1273           iface, lpdei, debugstr_guid(rguid));
1274     return DI_OK;
1275 }
1276
1277 HRESULT WINAPI IDirectInputDevice2WImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8W iface, LPDWORD pdwOut)
1278 {
1279     FIXME("(this=%p,%p): stub!\n", iface, pdwOut);
1280     return DI_OK;
1281 }
1282
1283 HRESULT WINAPI IDirectInputDevice2AImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8A iface, LPDWORD pdwOut)
1284 {
1285     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1286     return IDirectInputDevice2WImpl_GetForceFeedbackState(IDirectInputDevice8W_from_impl(This), pdwOut);
1287 }
1288
1289 HRESULT WINAPI IDirectInputDevice2WImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8W iface, DWORD dwFlags)
1290 {
1291     TRACE("(%p) 0x%08x:\n", iface, dwFlags);
1292     return DI_NOEFFECT;
1293 }
1294
1295 HRESULT WINAPI IDirectInputDevice2AImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8A iface, DWORD dwFlags)
1296 {
1297     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1298     return IDirectInputDevice2WImpl_SendForceFeedbackCommand(IDirectInputDevice8W_from_impl(This), dwFlags);
1299 }
1300
1301 HRESULT WINAPI IDirectInputDevice2WImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8W iface,
1302         LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback, LPVOID lpvRef, DWORD dwFlags)
1303 {
1304     FIXME("(this=%p,%p,%p,0x%08x): stub!\n", iface, lpCallback, lpvRef, dwFlags);
1305     return DI_OK;
1306 }
1307
1308 HRESULT WINAPI IDirectInputDevice2AImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8A iface,
1309         LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback, LPVOID lpvRef, DWORD dwFlags)
1310 {
1311     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1312     return IDirectInputDevice2WImpl_EnumCreatedEffectObjects(IDirectInputDevice8W_from_impl(This), lpCallback, lpvRef, dwFlags);
1313 }
1314
1315 HRESULT WINAPI IDirectInputDevice2WImpl_Escape(LPDIRECTINPUTDEVICE8W iface, LPDIEFFESCAPE lpDIEEsc)
1316 {
1317     FIXME("(this=%p,%p): stub!\n", iface, lpDIEEsc);
1318     return DI_OK;
1319 }
1320
1321 HRESULT WINAPI IDirectInputDevice2AImpl_Escape(LPDIRECTINPUTDEVICE8A iface, LPDIEFFESCAPE lpDIEEsc)
1322 {
1323     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1324     return IDirectInputDevice2WImpl_Escape(IDirectInputDevice8W_from_impl(This), lpDIEEsc);
1325 }
1326
1327 HRESULT WINAPI IDirectInputDevice2WImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
1328 {
1329     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1330
1331     if (!This->acquired) return DIERR_NOTACQUIRED;
1332     /* Because wine devices do not need to be polled, just return DI_NOEFFECT */
1333     return DI_NOEFFECT;
1334 }
1335
1336 HRESULT WINAPI IDirectInputDevice2AImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
1337 {
1338     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1339     return IDirectInputDevice2WImpl_Poll(IDirectInputDevice8W_from_impl(This));
1340 }
1341
1342 HRESULT WINAPI IDirectInputDevice2WImpl_SendDeviceData(LPDIRECTINPUTDEVICE8W iface, DWORD cbObjectData,
1343                                                        LPCDIDEVICEOBJECTDATA rgdod, LPDWORD pdwInOut,
1344                                                        DWORD dwFlags)
1345 {
1346     FIXME("(this=%p,0x%08x,%p,%p,0x%08x): stub!\n", iface, cbObjectData, rgdod, pdwInOut, dwFlags);
1347
1348     return DI_OK;
1349 }
1350
1351 HRESULT WINAPI IDirectInputDevice2AImpl_SendDeviceData(LPDIRECTINPUTDEVICE8A iface, DWORD cbObjectData,
1352                                                        LPCDIDEVICEOBJECTDATA rgdod, LPDWORD pdwInOut,
1353                                                        DWORD dwFlags)
1354 {
1355     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1356     return IDirectInputDevice2WImpl_SendDeviceData(IDirectInputDevice8W_from_impl(This), cbObjectData, rgdod,
1357                                                    pdwInOut, dwFlags);
1358 }
1359
1360 HRESULT WINAPI IDirectInputDevice7AImpl_EnumEffectsInFile(LPDIRECTINPUTDEVICE8A iface,
1361                                                           LPCSTR lpszFileName,
1362                                                           LPDIENUMEFFECTSINFILECALLBACK pec,
1363                                                           LPVOID pvRef,
1364                                                           DWORD dwFlags)
1365 {
1366     FIXME("(%p)->(%s,%p,%p,%08x): stub !\n", iface, lpszFileName, pec, pvRef, dwFlags);
1367     
1368     return DI_OK;
1369 }
1370
1371 HRESULT WINAPI IDirectInputDevice7WImpl_EnumEffectsInFile(LPDIRECTINPUTDEVICE8W iface,
1372                                                           LPCWSTR lpszFileName,
1373                                                           LPDIENUMEFFECTSINFILECALLBACK pec,
1374                                                           LPVOID pvRef,
1375                                                           DWORD dwFlags)
1376 {
1377     FIXME("(%p)->(%s,%p,%p,%08x): stub !\n", iface, debugstr_w(lpszFileName), pec, pvRef, dwFlags);
1378     
1379     return DI_OK;
1380 }
1381
1382 HRESULT WINAPI IDirectInputDevice7AImpl_WriteEffectToFile(LPDIRECTINPUTDEVICE8A iface,
1383                                                           LPCSTR lpszFileName,
1384                                                           DWORD dwEntries,
1385                                                           LPDIFILEEFFECT rgDiFileEft,
1386                                                           DWORD dwFlags)
1387 {
1388     FIXME("(%p)->(%s,%08x,%p,%08x): stub !\n", iface, lpszFileName, dwEntries, rgDiFileEft, dwFlags);
1389     
1390     return DI_OK;
1391 }
1392
1393 HRESULT WINAPI IDirectInputDevice7WImpl_WriteEffectToFile(LPDIRECTINPUTDEVICE8W iface,
1394                                                           LPCWSTR lpszFileName,
1395                                                           DWORD dwEntries,
1396                                                           LPDIFILEEFFECT rgDiFileEft,
1397                                                           DWORD dwFlags)
1398 {
1399     FIXME("(%p)->(%s,%08x,%p,%08x): stub !\n", iface, debugstr_w(lpszFileName), dwEntries, rgDiFileEft, dwFlags);
1400     
1401     return DI_OK;
1402 }
1403
1404 HRESULT WINAPI IDirectInputDevice8AImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
1405                                                        LPDIACTIONFORMATA lpdiaf,
1406                                                        LPCSTR lpszUserName,
1407                                                        DWORD dwFlags)
1408 {
1409     FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, lpszUserName, dwFlags);
1410 #define X(x) if (dwFlags & x) FIXME("\tdwFlags =|"#x"\n");
1411         X(DIDBAM_DEFAULT)
1412         X(DIDBAM_PRESERVE)
1413         X(DIDBAM_INITIALIZE)
1414         X(DIDBAM_HWDEFAULTS)
1415 #undef X
1416     _dump_diactionformatA(lpdiaf);
1417     return DI_OK;
1418 }
1419
1420 HRESULT WINAPI IDirectInputDevice8WImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
1421                                                        LPDIACTIONFORMATW lpdiaf,
1422                                                        LPCWSTR lpszUserName,
1423                                                        DWORD dwFlags)
1424 {
1425     FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
1426 #define X(x) if (dwFlags & x) FIXME("\tdwFlags =|"#x"\n");
1427         X(DIDBAM_DEFAULT)
1428         X(DIDBAM_PRESERVE)
1429         X(DIDBAM_INITIALIZE)
1430         X(DIDBAM_HWDEFAULTS)
1431 #undef X
1432   
1433     return DI_OK;
1434 }
1435
1436 HRESULT WINAPI IDirectInputDevice8AImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
1437                                                      LPDIACTIONFORMATA lpdiaf,
1438                                                      LPCSTR lpszUserName,
1439                                                      DWORD dwFlags)
1440 {
1441     IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1442     DIACTIONFORMATW diafW;
1443     HRESULT hr;
1444
1445     FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, lpszUserName, dwFlags);
1446
1447     diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
1448     _copy_diactionformatAtoW(&diafW, lpdiaf);
1449
1450     hr = IDirectInputDevice8WImpl_SetActionMap(&This->IDirectInputDevice8W_iface, &diafW, NULL, dwFlags);
1451
1452     HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
1453
1454     return hr;
1455 }
1456
1457 HRESULT WINAPI IDirectInputDevice8WImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
1458                                                      LPDIACTIONFORMATW lpdiaf,
1459                                                      LPCWSTR lpszUserName,
1460                                                      DWORD dwFlags)
1461 {
1462     DIPROPDWORD dp;
1463
1464     FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
1465
1466     if (lpdiaf->dwBufferSize > 0)
1467     {
1468         dp.diph.dwSize = sizeof(DIPROPDWORD);
1469         dp.dwData = lpdiaf->dwBufferSize;
1470         dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
1471         dp.diph.dwHow = DIPH_DEVICE;
1472         IDirectInputDevice8_SetProperty(iface, DIPROP_BUFFERSIZE, &dp.diph);
1473     }
1474
1475     return DI_OK;
1476 }
1477
1478 HRESULT WINAPI IDirectInputDevice8AImpl_GetImageInfo(LPDIRECTINPUTDEVICE8A iface,
1479                                                      LPDIDEVICEIMAGEINFOHEADERA lpdiDevImageInfoHeader)
1480 {
1481     FIXME("(%p)->(%p): stub !\n", iface, lpdiDevImageInfoHeader);
1482     
1483     return DI_OK;
1484 }
1485
1486 HRESULT WINAPI IDirectInputDevice8WImpl_GetImageInfo(LPDIRECTINPUTDEVICE8W iface,
1487                                                      LPDIDEVICEIMAGEINFOHEADERW lpdiDevImageInfoHeader)
1488 {
1489     FIXME("(%p)->(%p): stub !\n", iface, lpdiDevImageInfoHeader);
1490     
1491     return DI_OK;
1492 }