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