device.c 59.8 KB
Newer Older
Lionel Ulmer's avatar
Lionel Ulmer committed
1 2 3 4 5
/*		DirectInput Device
 *
 * Copyright 1998 Marcus Meissner
 * Copyright 1998,1999 Lionel Ulmer
 *
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Lionel Ulmer's avatar
Lionel Ulmer committed
20 21 22 23 24 25 26 27 28
 */

/* This file contains all the Device specific functions that can be used as stubs
   by real device implementations.

   It also contains all the helper functions.
*/
#include "config.h"

29
#include <stdarg.h>
30
#include <string.h>
31
#include "wine/debug.h"
32
#include "wine/unicode.h"
33
#include "windef.h"
Lionel Ulmer's avatar
Lionel Ulmer committed
34
#include "winbase.h"
35
#include "winreg.h"
36
#include "winuser.h"
Lionel Ulmer's avatar
Lionel Ulmer committed
37 38 39
#include "winerror.h"
#include "dinput.h"
#include "device_private.h"
40
#include "dinput_private.h"
Lionel Ulmer's avatar
Lionel Ulmer committed
41

42
WINE_DEFAULT_DEBUG_CHANNEL(dinput);
Lionel Ulmer's avatar
Lionel Ulmer committed
43

44 45
static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
{
46
    return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface);
47 48 49
}
static inline IDirectInputDeviceImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
{
50
    return CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface);
51 52
}

53 54
static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(IDirectInputDeviceImpl *This)
{
55
    return &This->IDirectInputDevice8A_iface;
56 57 58
}
static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(IDirectInputDeviceImpl *This)
{
59
    return &This->IDirectInputDevice8W_iface;
60 61
}

Lionel Ulmer's avatar
Lionel Ulmer committed
62 63 64
/******************************************************************************
 *	Various debugging tools
 */
65
static void _dump_cooperativelevel_DI(DWORD dwFlags) {
66
    if (TRACE_ON(dinput)) {
67
	unsigned int   i;
68 69 70 71 72 73 74 75
	static const struct {
	    DWORD       mask;
	    const char  *name;
	} flags[] = {
#define FE(x) { x, #x}
	    FE(DISCL_BACKGROUND),
	    FE(DISCL_EXCLUSIVE),
	    FE(DISCL_FOREGROUND),
76 77
	    FE(DISCL_NONEXCLUSIVE),
	    FE(DISCL_NOWINKEY)
Lionel Ulmer's avatar
Lionel Ulmer committed
78
#undef FE
79
	};
80
	TRACE(" cooperative level : ");
81 82
	for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
	    if (flags[i].mask & dwFlags)
Andrew Riedi's avatar
Andrew Riedi committed
83 84
		TRACE("%s ",flags[i].name);
	TRACE("\n");
85
    }
Lionel Ulmer's avatar
Lionel Ulmer committed
86 87
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
static void _dump_ObjectDataFormat_flags(DWORD dwFlags) {
    unsigned int   i;
    static const struct {
        DWORD       mask;
        const char  *name;
    } flags[] = {
#define FE(x) { x, #x}
        FE(DIDOI_FFACTUATOR),
        FE(DIDOI_FFEFFECTTRIGGER),
        FE(DIDOI_POLLED),
        FE(DIDOI_GUIDISUSAGE)
#undef FE
    };

    if (!dwFlags) return;

    TRACE("Flags:");

    /* First the flags */
    for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++) {
        if (flags[i].mask & dwFlags)
        TRACE(" %s",flags[i].name);
    }

    /* Now specific values */
#define FE(x) case x: TRACE(" "#x); break
    switch (dwFlags & DIDOI_ASPECTMASK) {
        FE(DIDOI_ASPECTACCEL);
        FE(DIDOI_ASPECTFORCE);
        FE(DIDOI_ASPECTPOSITION);
        FE(DIDOI_ASPECTVELOCITY);
    }
#undef FE

}

124
static void _dump_EnumObjects_flags(DWORD dwFlags) {
125
    if (TRACE_ON(dinput)) {
126
	unsigned int   i;
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	DWORD type, instance;
	static const struct {
	    DWORD       mask;
	    const char  *name;
	} flags[] = {
#define FE(x) { x, #x}
	    FE(DIDFT_RELAXIS),
	    FE(DIDFT_ABSAXIS),
	    FE(DIDFT_PSHBUTTON),
	    FE(DIDFT_TGLBUTTON),
	    FE(DIDFT_POV),
	    FE(DIDFT_COLLECTION),
	    FE(DIDFT_NODATA),	    
	    FE(DIDFT_FFACTUATOR),
	    FE(DIDFT_FFEFFECTTRIGGER),
142 143 144 145
	    FE(DIDFT_OUTPUT),
	    FE(DIDFT_VENDORDEFINED),
	    FE(DIDFT_ALIAS),
	    FE(DIDFT_OPTIONAL)
Lionel Ulmer's avatar
Lionel Ulmer committed
146
#undef FE
147 148 149
	};
	type = (dwFlags & 0xFF0000FF);
	instance = ((dwFlags >> 8) & 0xFFFF);
Andrew Riedi's avatar
Andrew Riedi committed
150
	TRACE("Type:");
151
	if (type == DIDFT_ALL) {
Andrew Riedi's avatar
Andrew Riedi committed
152
	    TRACE(" DIDFT_ALL");
153 154 155 156
	} else {
	    for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++) {
		if (flags[i].mask & type) {
		    type &= ~flags[i].mask;
Andrew Riedi's avatar
Andrew Riedi committed
157
		    TRACE(" %s",flags[i].name);
158 159 160
		}
	    }
	    if (type) {
Andrew Riedi's avatar
Andrew Riedi committed
161
                TRACE(" (unhandled: %08x)", type);
162 163
	    }
	}
Andrew Riedi's avatar
Andrew Riedi committed
164
	TRACE(" / Instance: ");
165
	if (instance == ((DIDFT_ANYINSTANCE >> 8) & 0xFFFF)) {
Andrew Riedi's avatar
Andrew Riedi committed
166
	    TRACE("DIDFT_ANYINSTANCE");
167
	} else {
Andrew Riedi's avatar
Andrew Riedi committed
168
            TRACE("%3d", instance);
169 170
	}
    }
Lionel Ulmer's avatar
Lionel Ulmer committed
171 172
}

173
void _dump_DIPROPHEADER(LPCDIPROPHEADER diph) {
174
    if (TRACE_ON(dinput)) {
Andrew Riedi's avatar
Andrew Riedi committed
175 176 177 178 179
        TRACE("  - dwObj = 0x%08x\n", diph->dwObj);
        TRACE("  - dwHow = %s\n",
            ((diph->dwHow == DIPH_DEVICE) ? "DIPH_DEVICE" :
            ((diph->dwHow == DIPH_BYOFFSET) ? "DIPH_BYOFFSET" :
            ((diph->dwHow == DIPH_BYID)) ? "DIPH_BYID" : "unknown")));
180
    }
Lionel Ulmer's avatar
Lionel Ulmer committed
181 182
}

183
void _dump_OBJECTINSTANCEA(const DIDEVICEOBJECTINSTANCEA *ddoi) {
184 185
    TRACE("    - enumerating : %s ('%s') - %2d - 0x%08x - %s - 0x%x\n",
        debugstr_guid(&ddoi->guidType), _dump_dinput_GUID(&ddoi->guidType), ddoi->dwOfs, ddoi->dwType, ddoi->tszName, ddoi->dwFlags);
Lionel Ulmer's avatar
Lionel Ulmer committed
186 187
}

188
void _dump_OBJECTINSTANCEW(const DIDEVICEOBJECTINSTANCEW *ddoi) {
189 190
    TRACE("    - enumerating : %s ('%s'), - %2d - 0x%08x - %s - 0x%x\n",
        debugstr_guid(&ddoi->guidType), _dump_dinput_GUID(&ddoi->guidType), ddoi->dwOfs, ddoi->dwType, debugstr_w(ddoi->tszName), ddoi->dwFlags);
191 192
}

193 194
/* This function is a helper to convert a GUID into any possible DInput GUID out there */
const char *_dump_dinput_GUID(const GUID *guid) {
195
    unsigned int i;
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    static const struct {
	const GUID *guid;
	const char *name;
    } guids[] = {
#define FE(x) { &x, #x}
	FE(GUID_XAxis),
	FE(GUID_YAxis),
	FE(GUID_ZAxis),
	FE(GUID_RxAxis),
	FE(GUID_RyAxis),
	FE(GUID_RzAxis),
	FE(GUID_Slider),
	FE(GUID_Button),
	FE(GUID_Key),
	FE(GUID_POV),
	FE(GUID_Unknown),
	FE(GUID_SysMouse),
	FE(GUID_SysKeyboard),
	FE(GUID_Joystick),
	FE(GUID_ConstantForce),
	FE(GUID_RampForce),
	FE(GUID_Square),
	FE(GUID_Sine),
	FE(GUID_Triangle),
	FE(GUID_SawtoothUp),
	FE(GUID_SawtoothDown),
	FE(GUID_Spring),
	FE(GUID_Damper),
	FE(GUID_Inertia),
	FE(GUID_Friction),
	FE(GUID_CustomForce)
#undef FE
    };
    if (guid == NULL)
	return "null GUID";
    for (i = 0; i < (sizeof(guids) / sizeof(guids[0])); i++) {
	if (IsEqualGUID(guids[i].guid, guid)) {
	    return guids[i].name;
Lionel Ulmer's avatar
Lionel Ulmer committed
234 235
	}
    }
236
    return debugstr_guid(guid);
Lionel Ulmer's avatar
Lionel Ulmer committed
237 238
}

239
void _dump_DIDATAFORMAT(const DIDATAFORMAT *df) {
240
    unsigned int i;
Lionel Ulmer's avatar
Lionel Ulmer committed
241

242
    TRACE("Dumping DIDATAFORMAT structure:\n");
243
    TRACE("  - dwSize: %d\n", df->dwSize);
244
    if (df->dwSize != sizeof(DIDATAFORMAT)) {
245
        WARN("Non-standard DIDATAFORMAT structure size %d\n", df->dwSize);
246
    }
247
    TRACE("  - dwObjsize: %d\n", df->dwObjSize);
248
    if (df->dwObjSize != sizeof(DIOBJECTDATAFORMAT)) {
249
        WARN("Non-standard DIOBJECTDATAFORMAT structure size %d\n", df->dwObjSize);
250
    }
251
    TRACE("  - dwFlags: 0x%08x (", df->dwFlags);
252 253 254 255 256 257
    switch (df->dwFlags) {
        case DIDF_ABSAXIS: TRACE("DIDF_ABSAXIS"); break;
	case DIDF_RELAXIS: TRACE("DIDF_RELAXIS"); break;
	default: TRACE("unknown"); break;
    }
    TRACE(")\n");
258 259
    TRACE("  - dwDataSize: %d\n", df->dwDataSize);
    TRACE("  - dwNumObjs: %d\n", df->dwNumObjs);
260 261 262 263
    
    for (i = 0; i < df->dwNumObjs; i++) {
	TRACE("  - Object %d:\n", i);
	TRACE("      * GUID: %s ('%s')\n", debugstr_guid(df->rgodf[i].pguid), _dump_dinput_GUID(df->rgodf[i].pguid));
264 265
        TRACE("      * dwOfs: %d\n", df->rgodf[i].dwOfs);
        TRACE("      * dwType: 0x%08x\n", df->rgodf[i].dwType);
266
	TRACE("        "); _dump_EnumObjects_flags(df->rgodf[i].dwType); TRACE("\n");
267
        TRACE("      * dwFlags: 0x%08x\n", df->rgodf[i].dwFlags);
268
	TRACE("        "); _dump_ObjectDataFormat_flags(df->rgodf[i].dwFlags); TRACE("\n");
269 270
    }
}
271

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
/******************************************************************************
 * Get the default and the app-specific config keys.
 */
BOOL get_app_key(HKEY *defkey, HKEY *appkey)
{
    char buffer[MAX_PATH+16];
    DWORD len;

    *appkey = 0;

    /* @@ Wine registry key: HKCU\Software\Wine\DirectInput */
    if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\DirectInput", defkey))
        *defkey = 0;

    len = GetModuleFileNameA(0, buffer, MAX_PATH);
    if (len && len < MAX_PATH)
    {
        HKEY tmpkey;

        /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectInput */
        if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
        {
            char *p, *appname = buffer;
            if ((p = strrchr(appname, '/'))) appname = p + 1;
            if ((p = strrchr(appname, '\\'))) appname = p + 1;
            strcat(appname, "\\DirectInput");

Vitaliy Margolen's avatar
Vitaliy Margolen committed
299
            if (RegOpenKeyA(tmpkey, appname, appkey)) *appkey = 0;
300 301 302 303 304 305 306
            RegCloseKey(tmpkey);
        }
    }

    return *defkey || *appkey;
}

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/******************************************************************************
 * Get a config key from either the app-specific or the default config
 */
DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
                             char *buffer, DWORD size )
{
    if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size ))
        return 0;

    if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size ))
        return 0;

    return ERROR_FILE_NOT_FOUND;
}

322
/* Conversion between internal data buffer and external data buffer */
323 324
void fill_DataFormat(void *out, DWORD size, const void *in, const DataFormat *df)
{
325
    int i;
326
    const char *in_c = in;
327
    char *out_c = out;
328

329
    memset(out, 0, size);
330 331
    if (df->dt == NULL) {
	/* This means that the app uses Wine's internal data format */
332
        memcpy(out, in, min(size, df->internal_format_size));
333 334 335 336 337 338
    } else {
	for (i = 0; i < df->size; i++) {
	    if (df->dt[i].offset_in >= 0) {
		switch (df->dt[i].size) {
		    case 1:
		        TRACE("Copying (c) to %d from %d (value %d)\n",
339 340
                              df->dt[i].offset_out, df->dt[i].offset_in, *(in_c + df->dt[i].offset_in));
			*(out_c + df->dt[i].offset_out) = *(in_c + df->dt[i].offset_in);
341
			break;
342

343 344
		    case 2:
			TRACE("Copying (s) to %d from %d (value %d)\n",
345 346
			      df->dt[i].offset_out, df->dt[i].offset_in, *((const short *)(in_c + df->dt[i].offset_in)));
			*((short *)(out_c + df->dt[i].offset_out)) = *((const short *)(in_c + df->dt[i].offset_in));
347
			break;
348

349 350
		    case 4:
			TRACE("Copying (i) to %d from %d (value %d)\n",
351 352
                              df->dt[i].offset_out, df->dt[i].offset_in, *((const int *)(in_c + df->dt[i].offset_in)));
                        *((int *)(out_c + df->dt[i].offset_out)) = *((const int *)(in_c + df->dt[i].offset_in));
353
			break;
354

355 356 357 358 359 360 361 362 363
		    default:
			memcpy((out_c + df->dt[i].offset_out), (in_c + df->dt[i].offset_in), df->dt[i].size);
			break;
		}
	    } else {
		switch (df->dt[i].size) {
		    case 1:
		        TRACE("Copying (c) to %d default value %d\n",
			      df->dt[i].offset_out, df->dt[i].value);
364
			*(out_c + df->dt[i].offset_out) = (char) df->dt[i].value;
365 366 367 368 369 370 371 372 373 374 375
			break;
			
		    case 2:
			TRACE("Copying (s) to %d default value %d\n",
			      df->dt[i].offset_out, df->dt[i].value);
			*((short *) (out_c + df->dt[i].offset_out)) = (short) df->dt[i].value;
			break;
			
		    case 4:
			TRACE("Copying (i) to %d default value %d\n",
			      df->dt[i].offset_out, df->dt[i].value);
376
			*((int *) (out_c + df->dt[i].offset_out)) = df->dt[i].value;
377 378 379
			break;
			
		    default:
380
			memset((out_c + df->dt[i].offset_out), 0, df->dt[i].size);
381 382 383 384 385 386
			break;
		}
	    }
	}
    }
}
387

388 389
void release_DataFormat(DataFormat * format)
{
390
    TRACE("Deleting DataFormat: %p\n", format);
391 392

    HeapFree(GetProcessHeap(), 0, format->dt);
393 394 395
    format->dt = NULL;
    HeapFree(GetProcessHeap(), 0, format->offsets);
    format->offsets = NULL;
396 397
    HeapFree(GetProcessHeap(), 0, format->user_df);
    format->user_df = NULL;
398 399
}

400
static inline LPDIOBJECTDATAFORMAT dataformat_to_odf(LPCDIDATAFORMAT df, int idx)
401 402 403 404 405
{
    if (idx < 0 || idx >= df->dwNumObjs) return NULL;
    return (LPDIOBJECTDATAFORMAT)((LPBYTE)df->rgodf + idx * df->dwObjSize);
}

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
/* dataformat_to_odf_by_type
 *  Find the Nth object of the selected type in the DataFormat
 */
LPDIOBJECTDATAFORMAT dataformat_to_odf_by_type(LPCDIDATAFORMAT df, int n, DWORD type)
{
    int i, nfound = 0;

    for (i=0; i < df->dwNumObjs; i++)
    {
        LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(df, i);

        if (odf->dwType & type)
        {
            if (n == nfound)
                return odf;

            nfound++;
        }
    }

    return NULL;
}

429
static HRESULT create_DataFormat(LPCDIDATAFORMAT asked_format, DataFormat *format)
430
{
431
    DataTransform *dt;
432
    unsigned int i, j;
433 434 435
    int same = 1;
    int *done;
    int index = 0;
436
    DWORD next = 0;
437 438

    if (!format->wine_df) return DIERR_INVALIDPARAM;
439
    done = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, asked_format->dwNumObjs * sizeof(int));
440
    dt = HeapAlloc(GetProcessHeap(), 0, asked_format->dwNumObjs * sizeof(DataTransform));
441 442
    if (!dt || !done) goto failed;

443
    if (!(format->offsets = HeapAlloc(GetProcessHeap(), 0, format->wine_df->dwNumObjs * sizeof(int))))
444 445
        goto failed;

446 447 448 449
    if (!(format->user_df = HeapAlloc(GetProcessHeap(), 0, asked_format->dwSize)))
        goto failed;
    memcpy(format->user_df, asked_format, asked_format->dwSize);

450
    TRACE("Creating DataTransform :\n");
451
    
452 453
    for (i = 0; i < format->wine_df->dwNumObjs; i++)
    {
454 455
        format->offsets[i] = -1;

456 457 458 459 460 461 462 463
	for (j = 0; j < asked_format->dwNumObjs; j++) {
	    if (done[j] == 1)
		continue;
	    
	    if (/* Check if the application either requests any GUID and if not, it if matches
		 * the GUID of the Wine object.
		 */
		((asked_format->rgodf[j].pguid == NULL) ||
464 465
		 (format->wine_df->rgodf[i].pguid == NULL) ||
		 (IsEqualGUID(format->wine_df->rgodf[i].pguid, asked_format->rgodf[j].pguid)))
466 467 468 469
		&&
		(/* Then check if it accepts any instance id, and if not, if it matches Wine's
		  * instance id.
		  */
470
		 ((asked_format->rgodf[j].dwType & DIDFT_INSTANCEMASK) == DIDFT_ANYINSTANCE) ||
471
		 (DIDFT_GETINSTANCE(asked_format->rgodf[j].dwType) == 0x00FF) || /* This is mentioned in no DX docs, but it works fine - tested on WinXP */
472
		 (DIDFT_GETINSTANCE(asked_format->rgodf[j].dwType) == DIDFT_GETINSTANCE(format->wine_df->rgodf[i].dwType)))
473 474
		&&
		( /* Then if the asked type matches the one Wine provides */
475
                 DIDFT_GETTYPE(asked_format->rgodf[j].dwType) & format->wine_df->rgodf[i].dwType))
476
            {
477 478
		done[j] = 1;
		
479
		TRACE("Matching :\n");
480 481 482 483
		TRACE("   - Asked (%d) :\n", j);
		TRACE("       * GUID: %s ('%s')\n",
		      debugstr_guid(asked_format->rgodf[j].pguid),
		      _dump_dinput_GUID(asked_format->rgodf[j].pguid));
484
                TRACE("       * Offset: %3d\n", asked_format->rgodf[j].dwOfs);
485
                TRACE("       * dwType: 0x%08x\n", asked_format->rgodf[j].dwType);
486
		TRACE("         "); _dump_EnumObjects_flags(asked_format->rgodf[j].dwType); TRACE("\n");
487 488
                TRACE("       * dwFlags: 0x%08x\n", asked_format->rgodf[j].dwFlags);
		TRACE("         "); _dump_ObjectDataFormat_flags(asked_format->rgodf[j].dwFlags); TRACE("\n");
489
		
490
		TRACE("   - Wine  (%d) :\n", i);
491
		TRACE("       * GUID: %s ('%s')\n",
492 493 494
                      debugstr_guid(format->wine_df->rgodf[i].pguid),
                      _dump_dinput_GUID(format->wine_df->rgodf[i].pguid));
                TRACE("       * Offset: %3d\n", format->wine_df->rgodf[i].dwOfs);
495
                TRACE("       * dwType: 0x%08x\n", format->wine_df->rgodf[i].dwType);
496
                TRACE("         "); _dump_EnumObjects_flags(format->wine_df->rgodf[i].dwType); TRACE("\n");
497 498
                TRACE("       * dwFlags: 0x%08x\n", format->wine_df->rgodf[i].dwFlags);
                TRACE("         "); _dump_ObjectDataFormat_flags(format->wine_df->rgodf[i].dwFlags); TRACE("\n");
499
		
500
                if (format->wine_df->rgodf[i].dwType & DIDFT_BUTTON)
501 502 503
		    dt[index].size = sizeof(BYTE);
		else
		    dt[index].size = sizeof(DWORD);
504
                dt[index].offset_in = format->wine_df->rgodf[i].dwOfs;
505
                dt[index].offset_out = asked_format->rgodf[j].dwOfs;
506
                format->offsets[i]   = asked_format->rgodf[j].dwOfs;
507
		dt[index].value = 0;
508
                next = next + dt[index].size;
509
		
510
                if (format->wine_df->rgodf[i].dwOfs != dt[index].offset_out)
511 512
		    same = 0;
		
513
		index++;
514 515 516 517
		break;
	    }
	}
    }
518

519
    TRACE("Setting to default value :\n");
Lionel Ulmer's avatar
Lionel Ulmer committed
520
    for (j = 0; j < asked_format->dwNumObjs; j++) {
521 522 523 524 525
	if (done[j] == 0) {
	    TRACE("   - Asked (%d) :\n", j);
	    TRACE("       * GUID: %s ('%s')\n",
		  debugstr_guid(asked_format->rgodf[j].pguid),
		  _dump_dinput_GUID(asked_format->rgodf[j].pguid));
526
            TRACE("       * Offset: %3d\n", asked_format->rgodf[j].dwOfs);
527
            TRACE("       * dwType: 0x%08x\n", asked_format->rgodf[j].dwType);
528
	    TRACE("         "); _dump_EnumObjects_flags(asked_format->rgodf[j].dwType); TRACE("\n");
529 530
            TRACE("       * dwFlags: 0x%08x\n", asked_format->rgodf[j].dwFlags);
	    TRACE("         "); _dump_ObjectDataFormat_flags(asked_format->rgodf[j].dwFlags); TRACE("\n");
531 532 533 534 535 536 537
	    
	    if (asked_format->rgodf[j].dwType & DIDFT_BUTTON)
		dt[index].size = sizeof(BYTE);
	    else
		dt[index].size = sizeof(DWORD);
	    dt[index].offset_in  = -1;
	    dt[index].offset_out = asked_format->rgodf[j].dwOfs;
538 539 540 541
            if (asked_format->rgodf[j].dwType & DIDFT_POV)
                dt[index].value = -1;
            else
                dt[index].value = 0;
542
	    index++;
543

544 545
	    same = 0;
	}
Lionel Ulmer's avatar
Lionel Ulmer committed
546
    }
547
    
548
    format->internal_format_size = format->wine_df->dwDataSize;
549
    format->size = index;
550 551
    if (same) {
	HeapFree(GetProcessHeap(), 0, dt);
552
        dt = NULL;
Lionel Ulmer's avatar
Lionel Ulmer committed
553
    }
554 555
    format->dt = dt;

556
    HeapFree(GetProcessHeap(), 0, done);
557

558 559 560 561 562 563 564 565
    return DI_OK;

failed:
    HeapFree(GetProcessHeap(), 0, done);
    HeapFree(GetProcessHeap(), 0, dt);
    format->dt = NULL;
    HeapFree(GetProcessHeap(), 0, format->offsets);
    format->offsets = NULL;
566 567
    HeapFree(GetProcessHeap(), 0, format->user_df);
    format->user_df = NULL;
568 569

    return DIERR_OUTOFMEMORY;
Lionel Ulmer's avatar
Lionel Ulmer committed
570 571
}

572
/* find an object by its offset in a data format */
573
static int offset_to_object(const DataFormat *df, int offset)
574 575 576
{
    int i;

577 578 579 580
    if (!df->offsets) return -1;

    for (i = 0; i < df->wine_df->dwNumObjs; i++)
        if (df->offsets[i] == offset) return i;
581 582 583 584

    return -1;
}

585
int id_to_object(LPCDIDATAFORMAT df, int id)
586 587 588
{
    int i;

589
    id &= 0x00ffffff;
590
    for (i = 0; i < df->dwNumObjs; i++)
591
        if ((dataformat_to_odf(df, i)->dwType & 0x00ffffff) == id)
592 593 594 595 596
            return i;

    return -1;
}

597
static int id_to_offset(const DataFormat *df, int id)
598 599 600 601 602 603
{
    int obj = id_to_object(df->wine_df, id);

    return obj >= 0 && df->offsets ? df->offsets[obj] : -1;
}

604
int find_property(const DataFormat *df, LPCDIPROPHEADER ph)
605 606 607
{
    switch (ph->dwHow)
    {
608 609
        case DIPH_BYID:     return id_to_object(df->wine_df, ph->dwObj);
        case DIPH_BYOFFSET: return offset_to_object(df, ph->dwObj);
610 611 612 613 614 615
    }
    FIXME("Unhandled ph->dwHow=='%04X'\n", (unsigned int)ph->dwHow);

    return -1;
}

616
static DWORD semantic_to_obj_id(IDirectInputDeviceImpl* This, DWORD dwSemantic)
617 618 619 620
{
    DWORD type = (0x0000ff00 & dwSemantic) >> 8;
    DWORD offset = 0x000000ff & dwSemantic;
    DWORD obj_instance = 0;
621
    BOOL found = FALSE;
622 623 624 625 626 627 628 629 630
    int i;

    for (i = 0; i < This->data_format.wine_df->dwNumObjs; i++)
    {
        LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(This->data_format.wine_df, i);

        if (odf->dwOfs == offset)
        {
            obj_instance = DIDFT_GETINSTANCE(odf->dwType);
631
            found = TRUE;
632 633 634 635 636 637 638 639 640 641 642 643
            break;
        }
    }

    if (!found) return 0;

    if (type & DIDFT_AXIS)   type = DIDFT_RELAXIS;
    if (type & DIDFT_BUTTON) type = DIDFT_PSHBUTTON;

    return type | (0x0000ff00 & (obj_instance << 8));
}

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
/*
 * get_mapping_key
 * Retrieves an open registry key to save the mapping, parametrized for an username,
 * specific device and specific action mapping guid.
 */
static HKEY get_mapping_key(const WCHAR *device, const WCHAR *username, const WCHAR *guid)
{
    static const WCHAR subkey[] = {
        'S','o','f','t','w','a','r','e','\\',
        'W','i','n','e','\\',
        'D','i','r','e','c','t','I','n','p','u','t','\\',
        'M','a','p','p','i','n','g','s','\\','%','s','\\','%','s','\\','%','s','\0'};
    HKEY hkey;
    WCHAR *keyname;

    keyname = HeapAlloc(GetProcessHeap(), 0,
        sizeof(WCHAR) * (lstrlenW(subkey) + strlenW(username) + strlenW(device) + strlenW(guid)));
    sprintfW(keyname, subkey, username, device, guid);

    /* The key used is HKCU\Software\Wine\DirectInput\Mappings\[username]\[device]\[mapping_guid] */
    if (RegCreateKeyW(HKEY_CURRENT_USER, keyname, &hkey))
        hkey = 0;

    HeapFree(GetProcessHeap(), 0, keyname);

    return hkey;
}

static HRESULT save_mapping_settings(IDirectInputDevice8W *iface, LPDIACTIONFORMATW lpdiaf, LPCWSTR lpszUsername)
{
    WCHAR *guid_str = NULL;
    DIDEVICEINSTANCEW didev;
    HKEY hkey;
    int i;

    didev.dwSize = sizeof(didev);
    IDirectInputDevice8_GetDeviceInfo(iface, &didev);

    if (StringFromCLSID(&lpdiaf->guidActionMap, &guid_str) != S_OK)
        return DI_SETTINGSNOTSAVED;

    hkey = get_mapping_key(didev.tszInstanceName, lpszUsername, guid_str);

    if (!hkey)
    {
        CoTaskMemFree(guid_str);
        return DI_SETTINGSNOTSAVED;
    }

    /* Write each of the actions mapped for this device.
       Format is "dwSemantic"="dwObjID" and key is of type REG_DWORD
    */
    for (i = 0; i < lpdiaf->dwNumActions; i++)
    {
        static const WCHAR format[] = {'%','x','\0'};
        WCHAR label[9];

        if (IsEqualGUID(&didev.guidInstance, &lpdiaf->rgoAction[i].guidInstance) &&
            lpdiaf->rgoAction[i].dwHow != DIAH_UNMAPPED)
        {
             sprintfW(label, format, lpdiaf->rgoAction[i].dwSemantic);
             RegSetValueExW(hkey, label, 0, REG_DWORD, (const BYTE*) &lpdiaf->rgoAction[i].dwObjID, sizeof(DWORD));
        }
    }

    RegCloseKey(hkey);
    CoTaskMemFree(guid_str);

    return DI_OK;
}

static BOOL load_mapping_settings(IDirectInputDeviceImpl *This, LPDIACTIONFORMATW lpdiaf, const WCHAR *username)
{
    HKEY hkey;
    WCHAR *guid_str;
    DIDEVICEINSTANCEW didev;
720
    int i, mapped = 0;
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748

    didev.dwSize = sizeof(didev);
    IDirectInputDevice8_GetDeviceInfo(&This->IDirectInputDevice8W_iface, &didev);

    if (StringFromCLSID(&lpdiaf->guidActionMap, &guid_str) != S_OK)
        return FALSE;

    hkey = get_mapping_key(didev.tszInstanceName, username, guid_str);

    if (!hkey)
    {
        CoTaskMemFree(guid_str);
        return FALSE;
    }

    /* Try to read each action in the DIACTIONFORMAT from registry */
    for (i = 0; i < lpdiaf->dwNumActions; i++)
    {
        static const WCHAR format[] = {'%','x','\0'};
        DWORD id, size = sizeof(DWORD);
        WCHAR label[9];

        sprintfW(label, format, lpdiaf->rgoAction[i].dwSemantic);

        if (!RegQueryValueExW(hkey, label, 0, NULL, (LPBYTE) &id, &size))
        {
            lpdiaf->rgoAction[i].dwObjID = id;
            lpdiaf->rgoAction[i].guidInstance = didev.guidInstance;
749 750
            lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
            mapped += 1;
751 752 753 754 755 756
        }
    }

    RegCloseKey(hkey);
    CoTaskMemFree(guid_str);

757
    return mapped > 0;
758 759
}

760 761 762
HRESULT _build_action_map(LPDIRECTINPUTDEVICE8W iface, LPDIACTIONFORMATW lpdiaf, LPCWSTR lpszUserName, DWORD dwFlags, DWORD devMask, LPCDIDATAFORMAT df)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
763 764
    WCHAR username[MAX_PATH];
    DWORD username_size = MAX_PATH;
765 766
    int i;
    BOOL load_success = FALSE, has_actions = FALSE;
767 768 769 770 771 772 773 774 775 776 777 778 779 780

    /* Unless asked the contrary by these flags, try to load a previous mapping */
    if (!(dwFlags & DIDBAM_HWDEFAULTS))
    {
        /* Retrieve logged user name if necessary */
        if (lpszUserName == NULL)
            GetUserNameW(username, &username_size);
        else
            lstrcpynW(username, lpszUserName, MAX_PATH);

        load_success = load_mapping_settings(This, lpdiaf, username);
    }

    if (load_success) return DI_OK;
781 782 783

    for (i=0; i < lpdiaf->dwNumActions; i++)
    {
784
        /* Don't touch a user configured action */
785 786
        if (lpdiaf->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;

787 788 789 790 791 792 793 794 795 796 797
        if ((lpdiaf->rgoAction[i].dwSemantic & devMask) == devMask)
        {
            DWORD obj_id = semantic_to_obj_id(This, lpdiaf->rgoAction[i].dwSemantic);
            DWORD type = DIDFT_GETTYPE(obj_id);
            DWORD inst = DIDFT_GETINSTANCE(obj_id);

            LPDIOBJECTDATAFORMAT odf;

            if (type == DIDFT_PSHBUTTON) type = DIDFT_BUTTON;
            if (type == DIDFT_RELAXIS) type = DIDFT_AXIS;

798
            /* Make sure the object exists */
799 800 801 802 803 804 805
            odf = dataformat_to_odf_by_type(df, inst, type);

            if (odf != NULL)
            {
                lpdiaf->rgoAction[i].dwObjID = obj_id;
                lpdiaf->rgoAction[i].guidInstance = This->guid;
                lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
806
                has_actions = TRUE;
807 808 809 810
            }
        }
        else if (!(dwFlags & DIDBAM_PRESERVE))
        {
811
            /* We must clear action data belonging to other devices */
812 813 814 815 816 817 818 819 820 821
            memset(&lpdiaf->rgoAction[i].guidInstance, 0, sizeof(GUID));
            lpdiaf->rgoAction[i].dwHow = DIAH_UNMAPPED;
        }
    }

    if (!has_actions) return DI_NOEFFECT;

    return  IDirectInputDevice8WImpl_BuildActionMap(iface, lpdiaf, lpszUserName, dwFlags);
}

822 823 824 825 826 827 828
HRESULT _set_action_map(LPDIRECTINPUTDEVICE8W iface, LPDIACTIONFORMATW lpdiaf, LPCWSTR lpszUserName, DWORD dwFlags, LPCDIDATAFORMAT df)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
    DIDATAFORMAT data_format;
    DIOBJECTDATAFORMAT *obj_df = NULL;
    DIPROPDWORD dp;
    DIPROPRANGE dpr;
829
    DIPROPSTRING dps;
830 831
    WCHAR username[MAX_PATH];
    DWORD username_size = MAX_PATH;
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
    int i, action = 0, num_actions = 0;
    unsigned int offset = 0;

    if (This->acquired) return DIERR_ACQUIRED;

    data_format.dwSize = sizeof(data_format);
    data_format.dwObjSize = sizeof(DIOBJECTDATAFORMAT);
    data_format.dwFlags = DIDF_RELAXIS;
    data_format.dwDataSize = lpdiaf->dwDataSize;

    /* Count the actions */
    for (i=0; i < lpdiaf->dwNumActions; i++)
        if (IsEqualGUID(&This->guid, &lpdiaf->rgoAction[i].guidInstance))
            num_actions++;

    if (num_actions == 0) return DI_NOEFFECT;

    This->num_actions = num_actions;

    /* Construct the dataformat and actionmap */
    obj_df = HeapAlloc(GetProcessHeap(), 0, sizeof(DIOBJECTDATAFORMAT)*num_actions);
    data_format.rgodf = (LPDIOBJECTDATAFORMAT)obj_df;
    data_format.dwNumObjs = num_actions;

856
    HeapFree(GetProcessHeap(), 0, This->action_map);
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
    This->action_map = HeapAlloc(GetProcessHeap(), 0, sizeof(ActionMap)*num_actions);

    for (i = 0; i < lpdiaf->dwNumActions; i++)
    {
        if (IsEqualGUID(&This->guid, &lpdiaf->rgoAction[i].guidInstance))
        {
            DWORD inst = DIDFT_GETINSTANCE(lpdiaf->rgoAction[i].dwObjID);
            DWORD type = DIDFT_GETTYPE(lpdiaf->rgoAction[i].dwObjID);
            LPDIOBJECTDATAFORMAT obj;

            if (type == DIDFT_PSHBUTTON) type = DIDFT_BUTTON;
            if (type == DIDFT_RELAXIS) type = DIDFT_AXIS;

            obj = dataformat_to_odf_by_type(df, inst, type);

            memcpy(&obj_df[action], obj, df->dwObjSize);

            This->action_map[action].uAppData = lpdiaf->rgoAction[i].uAppData;
            This->action_map[action].offset = offset;
            obj_df[action].dwOfs = offset;
            offset += (type & DIDFT_BUTTON) ? 1 : 4;

            action++;
        }
    }

    IDirectInputDevice8_SetDataFormat(iface, &data_format);

    HeapFree(GetProcessHeap(), 0, obj_df);

    /* Set the device properties according to the action format */
    dpr.diph.dwSize = sizeof(DIPROPRANGE);
    dpr.lMin = lpdiaf->lAxisMin;
    dpr.lMax = lpdiaf->lAxisMax;
    dpr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
    dpr.diph.dwHow = DIPH_DEVICE;
    IDirectInputDevice8_SetProperty(iface, DIPROP_RANGE, &dpr.diph);

    if (lpdiaf->dwBufferSize > 0)
    {
        dp.diph.dwSize = sizeof(DIPROPDWORD);
        dp.dwData = lpdiaf->dwBufferSize;
        dp.diph.dwHeaderSize = sizeof(DIPROPHEADER);
        dp.diph.dwHow = DIPH_DEVICE;
        IDirectInputDevice8_SetProperty(iface, DIPROP_BUFFERSIZE, &dp.diph);
    }

904 905 906 907 908 909
    /* Retrieve logged user name if necessary */
    if (lpszUserName == NULL)
        GetUserNameW(username, &username_size);
    else
        lstrcpynW(username, lpszUserName, MAX_PATH);

910 911 912 913 914 915 916 917 918 919
    dps.diph.dwSize = sizeof(dps);
    dps.diph.dwHeaderSize = sizeof(DIPROPHEADER);
    dps.diph.dwObj = 0;
    dps.diph.dwHow = DIPH_DEVICE;
    if (dwFlags & DIDSAM_NOUSER)
        dps.wsz[0] = '\0';
    else
        lstrcpynW(dps.wsz, username, sizeof(dps.wsz)/sizeof(WCHAR));
    IDirectInputDevice8_SetProperty(iface, DIPROP_USERNAME, &dps.diph);

920 921 922
    /* Save the settings to disk */
    save_mapping_settings(iface, lpdiaf, username);

923
    return DI_OK;
924 925
}

926 927 928 929
/******************************************************************************
 *	queue_event - add new event to the ring queue
 */

930
void queue_event(LPDIRECTINPUTDEVICE8A iface, int inst_id, DWORD data, DWORD time, DWORD seq)
931
{
932
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
933
    int next_pos, ofs = id_to_offset(&This->data_format, inst_id);
934

935 936 937
    /* Event is being set regardless of the queue state */
    if (This->hEvent) SetEvent(This->hEvent);

938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
    if (!This->queue_len || This->overflow || ofs < 0) return;

    next_pos = (This->queue_head + 1) % This->queue_len;
    if (next_pos == This->queue_tail)
    {
        TRACE(" queue overflowed\n");
        This->overflow = TRUE;
        return;
    }

    TRACE(" queueing %d at offset %d (queue head %d / size %d)\n",
          data, ofs, This->queue_head, This->queue_len);

    This->data_queue[This->queue_head].dwOfs       = ofs;
    This->data_queue[This->queue_head].dwData      = data;
    This->data_queue[This->queue_head].dwTimeStamp = time;
    This->data_queue[This->queue_head].dwSequence  = seq;
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970

    /* Set uAppData by means of action mapping */
    if (This->num_actions > 0)
    {
        int i;
        for (i=0; i < This->num_actions; i++)
        {
            if (This->action_map[i].offset == ofs)
            {
                TRACE("Offset %d mapped to uAppData %lu\n", ofs, This->action_map[i].uAppData);
                This->data_queue[This->queue_head].uAppData = This->action_map[i].uAppData;
                break;
            }
        }
    }

971
    This->queue_head = next_pos;
972
    /* Send event if asked */
973 974
}

975 976 977 978
/******************************************************************************
 *	Acquire
 */

979
HRESULT WINAPI IDirectInputDevice2WImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
980
{
981
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
982
    HRESULT res;
983

984 985
    TRACE("(%p)\n", This);

986
    if (!This->data_format.user_df) return DIERR_INVALIDPARAM;
987 988
    if (This->dwCoopLevel & DISCL_FOREGROUND && This->win != GetForegroundWindow())
        return DIERR_OTHERAPPHASPRIO;
989

990 991
    EnterCriticalSection(&This->crit);
    res = This->acquired ? S_FALSE : DI_OK;
992
    This->acquired = 1;
993
    if (res == DI_OK)
994
        check_dinput_hooks(iface);
995
    LeaveCriticalSection(&This->crit);
996

997
    return res;
998 999
}

1000 1001 1002 1003 1004 1005 1006
HRESULT WINAPI IDirectInputDevice2AImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_Acquire(IDirectInputDevice8W_from_impl(This));
}


1007 1008 1009 1010
/******************************************************************************
 *	Unacquire
 */

1011
HRESULT WINAPI IDirectInputDevice2WImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
1012
{
1013
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1014
    HRESULT res;
1015

1016 1017
    TRACE("(%p)\n", This);

1018 1019
    EnterCriticalSection(&This->crit);
    res = !This->acquired ? DI_NOEFFECT : DI_OK;
1020
    This->acquired = 0;
1021
    if (res == DI_OK)
1022
        check_dinput_hooks(iface);
1023
    LeaveCriticalSection(&This->crit);
1024

1025
    return res;
1026 1027
}

1028 1029 1030 1031 1032 1033
HRESULT WINAPI IDirectInputDevice2AImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
}

Lionel Ulmer's avatar
Lionel Ulmer committed
1034 1035 1036 1037
/******************************************************************************
 *	IDirectInputDeviceA
 */

1038
HRESULT WINAPI IDirectInputDevice2WImpl_SetDataFormat(LPDIRECTINPUTDEVICE8W iface, LPCDIDATAFORMAT df)
1039
{
1040
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1041 1042 1043 1044
    HRESULT res = DI_OK;

    if (!df) return E_POINTER;
    TRACE("(%p) %p\n", This, df);
1045
    _dump_DIDATAFORMAT(df);
1046 1047 1048 1049 1050 1051 1052

    if (df->dwSize != sizeof(DIDATAFORMAT)) return DIERR_INVALIDPARAM;
    if (This->acquired) return DIERR_ACQUIRED;

    EnterCriticalSection(&This->crit);

    release_DataFormat(&This->data_format);
1053
    res = create_DataFormat(df, &This->data_format);
1054 1055 1056

    LeaveCriticalSection(&This->crit);
    return res;
Lionel Ulmer's avatar
Lionel Ulmer committed
1057 1058
}

1059 1060 1061 1062 1063 1064
HRESULT WINAPI IDirectInputDevice2AImpl_SetDataFormat(LPDIRECTINPUTDEVICE8A iface, LPCDIDATAFORMAT df)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_SetDataFormat(IDirectInputDevice8W_from_impl(This), df);
}

1065 1066 1067 1068 1069
/******************************************************************************
  *     SetCooperativeLevel
  *
  *  Set cooperative level and the source window for the events.
  */
1070
HRESULT WINAPI IDirectInputDevice2WImpl_SetCooperativeLevel(LPDIRECTINPUTDEVICE8W iface, HWND hwnd, DWORD dwflags)
1071
{
1072
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

    TRACE("(%p) %p,0x%08x\n", This, hwnd, dwflags);
    _dump_cooperativelevel_DI(dwflags);

    if ((dwflags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == 0 ||
        (dwflags & (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE)) == (DISCL_EXCLUSIVE | DISCL_NONEXCLUSIVE) ||
        (dwflags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == 0 ||
        (dwflags & (DISCL_FOREGROUND | DISCL_BACKGROUND)) == (DISCL_FOREGROUND | DISCL_BACKGROUND))
        return DIERR_INVALIDPARAM;

1083 1084
    if (hwnd && GetWindowLongW(hwnd, GWL_STYLE) & WS_CHILD) return E_HANDLE;

1085
    if (!hwnd && dwflags == (DISCL_NONEXCLUSIVE | DISCL_BACKGROUND))
1086 1087
        hwnd = GetDesktopWindow();

1088
    if (!IsWindow(hwnd)) return E_HANDLE;
1089 1090 1091 1092 1093 1094 1095 1096 1097

    /* For security reasons native does not allow exclusive background level
       for mouse and keyboard only */
    if (dwflags & DISCL_EXCLUSIVE && dwflags & DISCL_BACKGROUND &&
        (IsEqualGUID(&This->guid, &GUID_SysMouse) ||
         IsEqualGUID(&This->guid, &GUID_SysKeyboard)))
        return DIERR_UNSUPPORTED;

    /* Store the window which asks for the mouse */
1098
    EnterCriticalSection(&This->crit);
1099 1100
    This->win = hwnd;
    This->dwCoopLevel = dwflags;
1101
    LeaveCriticalSection(&This->crit);
1102

1103
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1104 1105
}

1106 1107 1108 1109 1110 1111
HRESULT WINAPI IDirectInputDevice2AImpl_SetCooperativeLevel(LPDIRECTINPUTDEVICE8A iface, HWND hwnd, DWORD dwflags)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_SetCooperativeLevel(IDirectInputDevice8W_from_impl(This), hwnd, dwflags);
}

1112 1113 1114
/******************************************************************************
  *     SetEventNotification : specifies event to be sent on state change
  */
1115
HRESULT WINAPI IDirectInputDevice2WImpl_SetEventNotification(LPDIRECTINPUTDEVICE8W iface, HANDLE event)
1116
{
1117
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1118 1119 1120

    TRACE("(%p) %p\n", This, event);

1121
    EnterCriticalSection(&This->crit);
1122
    This->hEvent = event;
1123
    LeaveCriticalSection(&This->crit);
1124
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1125 1126
}

1127
HRESULT WINAPI IDirectInputDevice2AImpl_SetEventNotification(LPDIRECTINPUTDEVICE8A iface, HANDLE event)
Lionel Ulmer's avatar
Lionel Ulmer committed
1128
{
1129
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1130 1131 1132 1133 1134 1135 1136
    return IDirectInputDevice2WImpl_SetEventNotification(IDirectInputDevice8W_from_impl(This), event);
}


ULONG WINAPI IDirectInputDevice2WImpl_Release(LPDIRECTINPUTDEVICE8W iface)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1137 1138 1139
    ULONG ref = InterlockedDecrement(&(This->ref));

    TRACE("(%p) releasing from %d\n", This, ref + 1);
1140 1141 1142

    if (ref) return ref;

1143 1144 1145 1146
    IDirectInputDevice_Unacquire(iface);
    /* Reset the FF state, free all effects, etc */
    IDirectInputDevice8_SendForceFeedbackCommand(iface, DISFFC_RESET);

1147
    HeapFree(GetProcessHeap(), 0, This->data_queue);
1148 1149 1150 1151 1152 1153

    /* Free data format */
    HeapFree(GetProcessHeap(), 0, This->data_format.wine_df->rgodf);
    HeapFree(GetProcessHeap(), 0, This->data_format.wine_df);
    release_DataFormat(&This->data_format);

1154 1155 1156
    /* Free action mapping */
    HeapFree(GetProcessHeap(), 0, This->action_map);

1157 1158 1159 1160
    EnterCriticalSection( &This->dinput->crit );
    list_remove( &This->entry );
    LeaveCriticalSection( &This->dinput->crit );

1161
    IDirectInput_Release(&This->dinput->IDirectInput7A_iface);
1162 1163 1164
    This->crit.DebugInfo->Spare[0] = 0;
    DeleteCriticalSection(&This->crit);

1165 1166 1167
    HeapFree(GetProcessHeap(), 0, This);

    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1168 1169
}

1170 1171 1172 1173 1174 1175
ULONG WINAPI IDirectInputDevice2AImpl_Release(LPDIRECTINPUTDEVICE8A iface)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_Release(IDirectInputDevice8W_from_impl(This));
}

1176
HRESULT WINAPI IDirectInputDevice2WImpl_QueryInterface(LPDIRECTINPUTDEVICE8W iface, REFIID riid, LPVOID *ppobj)
Lionel Ulmer's avatar
Lionel Ulmer committed
1177
{
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);

    TRACE("(%p this=%p,%s,%p)\n", iface, This, debugstr_guid(riid), ppobj);
    if (IsEqualGUID(&IID_IUnknown, riid) ||
        IsEqualGUID(&IID_IDirectInputDeviceA,  riid) ||
        IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
        IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
        IsEqualGUID(&IID_IDirectInputDevice8A, riid))
    {
        IDirectInputDevice2_AddRef(iface);
        *ppobj = IDirectInputDevice8A_from_impl(This);
        return DI_OK;
1190
    }
1191 1192 1193 1194 1195 1196 1197 1198
    if (IsEqualGUID(&IID_IDirectInputDeviceW,  riid) ||
        IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
        IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
        IsEqualGUID(&IID_IDirectInputDevice8W, riid))
    {
        IDirectInputDevice2_AddRef(iface);
        *ppobj = IDirectInputDevice8W_from_impl(This);
        return DI_OK;
1199
    }
1200 1201

    WARN("Unsupported interface!\n");
1202
    return E_FAIL;
Lionel Ulmer's avatar
Lionel Ulmer committed
1203 1204
}

1205
HRESULT WINAPI IDirectInputDevice2AImpl_QueryInterface(LPDIRECTINPUTDEVICE8A iface, REFIID riid, LPVOID *ppobj)
1206
{
1207 1208
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_QueryInterface(IDirectInputDevice8W_from_impl(This), riid, ppobj);
1209 1210
}

1211 1212 1213 1214 1215 1216 1217
ULONG WINAPI IDirectInputDevice2WImpl_AddRef(LPDIRECTINPUTDEVICE8W iface)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
    return InterlockedIncrement(&This->ref);
}

ULONG WINAPI IDirectInputDevice2AImpl_AddRef(LPDIRECTINPUTDEVICE8A iface)
Lionel Ulmer's avatar
Lionel Ulmer committed
1218
{
1219
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1220
    return IDirectInputDevice2WImpl_AddRef(IDirectInputDevice8W_from_impl(This));
Lionel Ulmer's avatar
Lionel Ulmer committed
1221 1222
}

1223 1224
HRESULT WINAPI IDirectInputDevice2AImpl_EnumObjects(LPDIRECTINPUTDEVICE8A iface,
        LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback, LPVOID lpvRef, DWORD dwFlags)
Lionel Ulmer's avatar
Lionel Ulmer committed
1225
{
1226
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1227
    DIDEVICEOBJECTINSTANCEA ddoi;
1228
    int i;
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240

    TRACE("(%p) %p,%p flags:%08x)\n", iface, lpCallback, lpvRef, dwFlags);
    TRACE("  - flags = ");
    _dump_EnumObjects_flags(dwFlags);
    TRACE("\n");

    /* Only the fields till dwFFMaxForce are relevant */
    memset(&ddoi, 0, sizeof(ddoi));
    ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);

    for (i = 0; i < This->data_format.wine_df->dwNumObjs; i++)
    {
1241
        LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(This->data_format.wine_df, i);
1242

1243
        if (dwFlags != DIDFT_ALL && !(dwFlags & DIDFT_GETTYPE(odf->dwType))) continue;
1244 1245
        if (IDirectInputDevice_GetObjectInfo(iface, &ddoi, odf->dwType, DIPH_BYID) != DI_OK)
            continue;
1246 1247

	if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) break;
1248
    }
1249

1250
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1251
}
1252

1253 1254
HRESULT WINAPI IDirectInputDevice2WImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface,
        LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback, LPVOID lpvRef, DWORD dwFlags)
1255
{
1256
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1257 1258
    DIDEVICEOBJECTINSTANCEW ddoi;
    int i;
1259

1260 1261 1262 1263
    TRACE("(%p) %p,%p flags:%08x)\n", iface, lpCallback, lpvRef, dwFlags);
    TRACE("  - flags = ");
    _dump_EnumObjects_flags(dwFlags);
    TRACE("\n");
1264

1265 1266 1267 1268 1269 1270 1271 1272
    /* Only the fields till dwFFMaxForce are relevant */
    memset(&ddoi, 0, sizeof(ddoi));
    ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, dwFFMaxForce);

    for (i = 0; i < This->data_format.wine_df->dwNumObjs; i++)
    {
        LPDIOBJECTDATAFORMAT odf = dataformat_to_odf(This->data_format.wine_df, i);

1273
        if (dwFlags != DIDFT_ALL && !(dwFlags & DIDFT_GETTYPE(odf->dwType))) continue;
1274 1275 1276 1277 1278 1279 1280
        if (IDirectInputDevice_GetObjectInfo(iface, &ddoi, odf->dwType, DIPH_BYID) != DI_OK)
            continue;

	if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) break;
    }

    return DI_OK;
1281 1282
}

1283 1284 1285 1286
/******************************************************************************
 *	GetProperty
 */

1287
HRESULT WINAPI IDirectInputDevice2WImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
Lionel Ulmer's avatar
Lionel Ulmer committed
1288
{
1289
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1290 1291 1292 1293

    TRACE("(%p) %s,%p\n", iface, debugstr_guid(rguid), pdiph);
    _dump_DIPROPHEADER(pdiph);

1294
    if (!IS_DIPROP(rguid)) return DI_OK;
1295 1296 1297

    switch (LOWORD(rguid))
    {
1298
        case (DWORD_PTR) DIPROP_BUFFERSIZE:
1299 1300 1301 1302 1303 1304 1305 1306 1307
        {
            LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;

            if (pdiph->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;

            pd->dwData = This->queue_len;
            TRACE("buffersize = %d\n", pd->dwData);
            break;
        }
1308 1309 1310
        case (DWORD_PTR) DIPROP_USERNAME:
        {
            LPDIPROPSTRING ps = (LPDIPROPSTRING)pdiph;
1311
            struct DevicePlayer *device_player;
1312 1313 1314

            if (pdiph->dwSize != sizeof(DIPROPSTRING)) return DIERR_INVALIDPARAM;

1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
            LIST_FOR_EACH_ENTRY(device_player, &This->dinput->device_players,
                struct DevicePlayer, entry)
            {
                if (IsEqualGUID(&device_player->instance_guid, &This->guid))
                {
                    if (*device_player->username)
                    {
                        lstrcpynW(ps->wsz, device_player->username, sizeof(ps->wsz)/sizeof(WCHAR));
                        return DI_OK;
                    }
                    else break;
                }
            }
            return S_FALSE;
1329
        }
1330 1331 1332
        case (DWORD_PTR) DIPROP_VIDPID:
            FIXME("DIPROP_VIDPID not implemented\n");
            return DIERR_UNSUPPORTED;
1333
        default:
1334 1335
            FIXME("Unknown property %s\n", debugstr_guid(rguid));
            return DIERR_INVALIDPARAM;
1336 1337 1338 1339 1340
    }

    return DI_OK;
}

1341 1342 1343 1344 1345 1346
HRESULT WINAPI IDirectInputDevice2AImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
}

1347 1348 1349 1350
/******************************************************************************
 *	SetProperty
 */

1351 1352
HRESULT WINAPI IDirectInputDevice2WImpl_SetProperty(
        LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER pdiph)
1353
{
1354
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1355 1356 1357 1358

    TRACE("(%p) %s,%p\n", iface, debugstr_guid(rguid), pdiph);
    _dump_DIPROPHEADER(pdiph);

1359
    if (!IS_DIPROP(rguid)) return DI_OK;
1360 1361 1362

    switch (LOWORD(rguid))
    {
1363
        case (DWORD_PTR) DIPROP_AXISMODE:
1364 1365 1366 1367 1368 1369 1370
        {
            LPCDIPROPDWORD pd = (LPCDIPROPDWORD)pdiph;

            if (pdiph->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
            if (pdiph->dwHow == DIPH_DEVICE && pdiph->dwObj) return DIERR_INVALIDPARAM;
            if (This->acquired) return DIERR_ACQUIRED;
            if (pdiph->dwHow != DIPH_DEVICE) return DIERR_UNSUPPORTED;
1371
            if (!This->data_format.user_df) return DI_OK;
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382

            TRACE("Axis mode: %s\n", pd->dwData == DIPROPAXISMODE_ABS ? "absolute" :
                                                                        "relative");

            EnterCriticalSection(&This->crit);
            This->data_format.user_df->dwFlags &= ~DIDFT_AXIS;
            This->data_format.user_df->dwFlags |= pd->dwData == DIPROPAXISMODE_ABS ?
                                                  DIDF_ABSAXIS : DIDF_RELAXIS;
            LeaveCriticalSection(&This->crit);
            break;
        }
1383
        case (DWORD_PTR) DIPROP_BUFFERSIZE:
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
        {
            LPCDIPROPDWORD pd = (LPCDIPROPDWORD)pdiph;

            if (pdiph->dwSize != sizeof(DIPROPDWORD)) return DIERR_INVALIDPARAM;
            if (This->acquired) return DIERR_ACQUIRED;

            TRACE("buffersize = %d\n", pd->dwData);

            EnterCriticalSection(&This->crit);
            HeapFree(GetProcessHeap(), 0, This->data_queue);

            This->data_queue = !pd->dwData ? NULL : HeapAlloc(GetProcessHeap(), 0,
                                pd->dwData * sizeof(DIDEVICEOBJECTDATA));
            This->queue_head = This->queue_tail = This->overflow = 0;
            This->queue_len  = pd->dwData;

            LeaveCriticalSection(&This->crit);
            break;
        }
1403 1404 1405
        case (DWORD_PTR) DIPROP_USERNAME:
        {
            LPCDIPROPSTRING ps = (LPCDIPROPSTRING)pdiph;
1406 1407
            struct DevicePlayer *device_player;
            BOOL found = FALSE;
1408 1409 1410

            if (pdiph->dwSize != sizeof(DIPROPSTRING)) return DIERR_INVALIDPARAM;

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
            LIST_FOR_EACH_ENTRY(device_player, &This->dinput->device_players,
                struct DevicePlayer, entry)
            {
                if (IsEqualGUID(&device_player->instance_guid, &This->guid))
                {
                    found = TRUE;
                    break;
                }
            }
            if (!found && (device_player =
                    HeapAlloc(GetProcessHeap(), 0, sizeof(struct DevicePlayer))))
            {
                list_add_tail(&This->dinput->device_players, &device_player->entry);
                device_player->instance_guid = This->guid;
            }
            if (device_player)
                lstrcpynW(device_player->username, ps->wsz,
                    sizeof(device_player->username)/sizeof(WCHAR));
1429 1430
            break;
        }
1431 1432 1433 1434 1435
        default:
            WARN("Unknown property %s\n", debugstr_guid(rguid));
            return DIERR_UNSUPPORTED;
    }

1436
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1437 1438
}

1439 1440 1441 1442 1443 1444 1445
HRESULT WINAPI IDirectInputDevice2AImpl_SetProperty(
        LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER pdiph)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
}

Lionel Ulmer's avatar
Lionel Ulmer committed
1446
HRESULT WINAPI IDirectInputDevice2AImpl_GetObjectInfo(
1447
	LPDIRECTINPUTDEVICE8A iface,
Lionel Ulmer's avatar
Lionel Ulmer committed
1448 1449 1450 1451
	LPDIDEVICEOBJECTINSTANCEA pdidoi,
	DWORD dwObj,
	DWORD dwHow)
{
1452
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1453 1454 1455 1456 1457 1458 1459 1460 1461
    DIDEVICEOBJECTINSTANCEW didoiW;
    HRESULT res;

    if (!pdidoi ||
        (pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCEA) &&
         pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCE_DX3A)))
        return DIERR_INVALIDPARAM;

    didoiW.dwSize = sizeof(didoiW);
1462
    res = IDirectInputDevice2WImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
    if (res == DI_OK)
    {
        DWORD dwSize = pdidoi->dwSize;

        memset(pdidoi, 0, pdidoi->dwSize);
        pdidoi->dwSize   = dwSize;
        pdidoi->guidType = didoiW.guidType;
        pdidoi->dwOfs    = didoiW.dwOfs;
        pdidoi->dwType   = didoiW.dwType;
        pdidoi->dwFlags  = didoiW.dwFlags;
    }

    return res;
Lionel Ulmer's avatar
Lionel Ulmer committed
1476
}
1477

1478 1479 1480 1481 1482 1483
HRESULT WINAPI IDirectInputDevice2WImpl_GetObjectInfo(
	LPDIRECTINPUTDEVICE8W iface,
	LPDIDEVICEOBJECTINSTANCEW pdidoi,
	DWORD dwObj,
	DWORD dwHow)
{
1484
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1485
    DWORD dwSize;
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
    LPDIOBJECTDATAFORMAT odf;
    int idx = -1;

    TRACE("(%p) %d(0x%08x) -> %p\n", This, dwHow, dwObj, pdidoi);

    if (!pdidoi ||
        (pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCEW) &&
         pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCE_DX3W)))
        return DIERR_INVALIDPARAM;

    switch (dwHow)
    {
    case DIPH_BYOFFSET:
        if (!This->data_format.offsets) break;
        for (idx = This->data_format.wine_df->dwNumObjs - 1; idx >= 0; idx--)
            if (This->data_format.offsets[idx] == dwObj) break;
        break;
    case DIPH_BYID:
        dwObj &= 0x00ffffff;
        for (idx = This->data_format.wine_df->dwNumObjs - 1; idx >= 0; idx--)
            if ((dataformat_to_odf(This->data_format.wine_df, idx)->dwType & 0x00ffffff) == dwObj)
                break;
        break;

    case DIPH_BYUSAGE:
        FIXME("dwHow = DIPH_BYUSAGE not implemented\n");
        break;
    default:
        WARN("invalid parameter: dwHow = %08x\n", dwHow);
        return DIERR_INVALIDPARAM;
    }
    if (idx < 0) return DIERR_OBJECTNOTFOUND;

    odf = dataformat_to_odf(This->data_format.wine_df, idx);
1520
    dwSize = pdidoi->dwSize; /* save due to memset below */
1521 1522
    memset(pdidoi, 0, pdidoi->dwSize);
    pdidoi->dwSize   = dwSize;
1523
    if (odf->pguid) pdidoi->guidType = *odf->pguid;
1524 1525 1526 1527
    pdidoi->dwOfs    = This->data_format.offsets ? This->data_format.offsets[idx] : odf->dwOfs;
    pdidoi->dwType   = odf->dwType;
    pdidoi->dwFlags  = odf->dwFlags;

1528
    return DI_OK;
1529 1530
}

1531 1532
HRESULT WINAPI IDirectInputDevice2WImpl_GetDeviceData(LPDIRECTINPUTDEVICE8W iface, DWORD dodsize,
                                                      LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
1533
{
1534
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1535 1536 1537 1538 1539 1540
    HRESULT ret = DI_OK;
    int len;

    TRACE("(%p) %p -> %p(%d) x%d, 0x%08x\n",
          This, dod, entries, entries ? *entries : 0, dodsize, flags);

1541
    if (This->dinput->dwVersion == 0x0800 || dodsize == sizeof(DIDEVICEOBJECTDATA_DX3))
1542 1543
    {
        if (!This->queue_len) return DIERR_NOTBUFFERED;
1544
        if (!This->acquired) return DIERR_NOTACQUIRED;
1545 1546
    }

1547
    if (!This->queue_len)
1548
        return DI_OK;
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
    if (dodsize < sizeof(DIDEVICEOBJECTDATA_DX3))
        return DIERR_INVALIDPARAM;

    IDirectInputDevice2_Poll(iface);
    EnterCriticalSection(&This->crit);

    len = This->queue_head - This->queue_tail;
    if (len < 0) len += This->queue_len;

    if ((*entries != INFINITE) && (len > *entries)) len = *entries;

    if (dod)
    {
        int i;
        for (i = 0; i < len; i++)
        {
            int n = (This->queue_tail + i) % This->queue_len;
            memcpy((char *)dod + dodsize * i, This->data_queue + n, dodsize);
        }
    }
    *entries = len;

1571
    if (This->overflow && This->dinput->dwVersion == 0x0800)
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
        ret = DI_BUFFEROVERFLOW;

    if (!(flags & DIGDD_PEEK))
    {
        /* Advance reading position */
        This->queue_tail = (This->queue_tail + len) % This->queue_len;
        This->overflow = FALSE;
    }

    LeaveCriticalSection(&This->crit);

    TRACE("Returning %d events queued\n", *entries);
    return ret;
}

1587 1588
HRESULT WINAPI IDirectInputDevice2AImpl_GetDeviceData(LPDIRECTINPUTDEVICE8A iface, DWORD dodsize,
                                                      LPDIDEVICEOBJECTDATA dod, LPDWORD entries, DWORD flags)
Lionel Ulmer's avatar
Lionel Ulmer committed
1589
{
1590 1591 1592 1593 1594 1595 1596
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_GetDeviceData(IDirectInputDevice8W_from_impl(This), dodsize, dod, entries, flags);
}

HRESULT WINAPI IDirectInputDevice2WImpl_RunControlPanel(LPDIRECTINPUTDEVICE8W iface, HWND hwndOwner, DWORD dwFlags)
{
    FIXME("(this=%p,%p,0x%08x): stub!\n", iface, hwndOwner, dwFlags);
1597

1598
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1599
}
1600

1601 1602 1603 1604 1605 1606 1607 1608
HRESULT WINAPI IDirectInputDevice2AImpl_RunControlPanel(LPDIRECTINPUTDEVICE8A iface, HWND hwndOwner, DWORD dwFlags)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_RunControlPanel(IDirectInputDevice8W_from_impl(This), hwndOwner, dwFlags);
}

HRESULT WINAPI IDirectInputDevice2WImpl_Initialize(LPDIRECTINPUTDEVICE8W iface, HINSTANCE hinst, DWORD dwVersion,
                                                   REFGUID rguid)
Lionel Ulmer's avatar
Lionel Ulmer committed
1609
{
1610
    FIXME("(this=%p,%p,%d,%s): stub!\n", iface, hinst, dwVersion, debugstr_guid(rguid));
1611
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1612
}
1613

1614 1615 1616 1617 1618 1619 1620
HRESULT WINAPI IDirectInputDevice2AImpl_Initialize(LPDIRECTINPUTDEVICE8A iface, HINSTANCE hinst, DWORD dwVersion,
                                                   REFGUID rguid)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_Initialize(IDirectInputDevice8W_from_impl(This), hinst, dwVersion, rguid);
}

Lionel Ulmer's avatar
Lionel Ulmer committed
1621 1622 1623 1624
/******************************************************************************
 *	IDirectInputDevice2A
 */

1625 1626
HRESULT WINAPI IDirectInputDevice2WImpl_CreateEffect(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIEFFECT lpeff,
                                                     LPDIRECTINPUTEFFECT *ppdef, LPUNKNOWN pUnkOuter)
Lionel Ulmer's avatar
Lionel Ulmer committed
1627
{
1628
    FIXME("(this=%p,%s,%p,%p,%p): stub!\n", iface, debugstr_guid(rguid), lpeff, ppdef, pUnkOuter);
1629 1630 1631 1632

    FIXME("not available in the generic implementation\n");
    *ppdef = NULL;
    return DIERR_UNSUPPORTED;
Lionel Ulmer's avatar
Lionel Ulmer committed
1633 1634
}

1635 1636 1637 1638 1639 1640 1641
HRESULT WINAPI IDirectInputDevice2AImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIEFFECT lpeff,
                                                     LPDIRECTINPUTEFFECT *ppdef, LPUNKNOWN pUnkOuter)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_CreateEffect(IDirectInputDevice8W_from_impl(This), rguid, lpeff, ppdef, pUnkOuter);
}

Lionel Ulmer's avatar
Lionel Ulmer committed
1642
HRESULT WINAPI IDirectInputDevice2AImpl_EnumEffects(
1643
	LPDIRECTINPUTDEVICE8A iface,
Lionel Ulmer's avatar
Lionel Ulmer committed
1644 1645 1646 1647
	LPDIENUMEFFECTSCALLBACKA lpCallback,
	LPVOID lpvRef,
	DWORD dwFlags)
{
1648
    FIXME("(this=%p,%p,%p,0x%08x): stub!\n",
1649 1650 1651
	  iface, lpCallback, lpvRef, dwFlags);
    
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1652 1653
}

1654 1655 1656 1657 1658 1659
HRESULT WINAPI IDirectInputDevice2WImpl_EnumEffects(
	LPDIRECTINPUTDEVICE8W iface,
	LPDIENUMEFFECTSCALLBACKW lpCallback,
	LPVOID lpvRef,
	DWORD dwFlags)
{
1660
    FIXME("(this=%p,%p,%p,0x%08x): stub!\n",
1661 1662 1663
	  iface, lpCallback, lpvRef, dwFlags);
    
    return DI_OK;
1664 1665
}

Lionel Ulmer's avatar
Lionel Ulmer committed
1666
HRESULT WINAPI IDirectInputDevice2AImpl_GetEffectInfo(
1667
	LPDIRECTINPUTDEVICE8A iface,
Lionel Ulmer's avatar
Lionel Ulmer committed
1668 1669 1670
	LPDIEFFECTINFOA lpdei,
	REFGUID rguid)
{
1671 1672 1673
    FIXME("(this=%p,%p,%s): stub!\n",
	  iface, lpdei, debugstr_guid(rguid));
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1674 1675
}

1676 1677 1678 1679 1680
HRESULT WINAPI IDirectInputDevice2WImpl_GetEffectInfo(
	LPDIRECTINPUTDEVICE8W iface,
	LPDIEFFECTINFOW lpdei,
	REFGUID rguid)
{
1681 1682 1683
    FIXME("(this=%p,%p,%s): stub!\n",
	  iface, lpdei, debugstr_guid(rguid));
    return DI_OK;
1684 1685
}

1686
HRESULT WINAPI IDirectInputDevice2WImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8W iface, LPDWORD pdwOut)
Lionel Ulmer's avatar
Lionel Ulmer committed
1687
{
1688
    FIXME("(this=%p,%p): stub!\n", iface, pdwOut);
1689
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1690 1691
}

1692 1693 1694 1695 1696 1697 1698
HRESULT WINAPI IDirectInputDevice2AImpl_GetForceFeedbackState(LPDIRECTINPUTDEVICE8A iface, LPDWORD pdwOut)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_GetForceFeedbackState(IDirectInputDevice8W_from_impl(This), pdwOut);
}

HRESULT WINAPI IDirectInputDevice2WImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8W iface, DWORD dwFlags)
Lionel Ulmer's avatar
Lionel Ulmer committed
1699
{
1700 1701
    TRACE("(%p) 0x%08x:\n", iface, dwFlags);
    return DI_NOEFFECT;
Lionel Ulmer's avatar
Lionel Ulmer committed
1702 1703
}

1704
HRESULT WINAPI IDirectInputDevice2AImpl_SendForceFeedbackCommand(LPDIRECTINPUTDEVICE8A iface, DWORD dwFlags)
Lionel Ulmer's avatar
Lionel Ulmer committed
1705
{
1706 1707 1708 1709 1710 1711 1712 1713
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_SendForceFeedbackCommand(IDirectInputDevice8W_from_impl(This), dwFlags);
}

HRESULT WINAPI IDirectInputDevice2WImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8W iface,
        LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback, LPVOID lpvRef, DWORD dwFlags)
{
    FIXME("(this=%p,%p,%p,0x%08x): stub!\n", iface, lpCallback, lpvRef, dwFlags);
1714
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1715 1716
}

1717 1718 1719 1720 1721 1722 1723 1724
HRESULT WINAPI IDirectInputDevice2AImpl_EnumCreatedEffectObjects(LPDIRECTINPUTDEVICE8A iface,
        LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback, LPVOID lpvRef, DWORD dwFlags)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_EnumCreatedEffectObjects(IDirectInputDevice8W_from_impl(This), lpCallback, lpvRef, dwFlags);
}

HRESULT WINAPI IDirectInputDevice2WImpl_Escape(LPDIRECTINPUTDEVICE8W iface, LPDIEFFESCAPE lpDIEEsc)
Lionel Ulmer's avatar
Lionel Ulmer committed
1725
{
1726
    FIXME("(this=%p,%p): stub!\n", iface, lpDIEEsc);
1727
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1728 1729
}

1730
HRESULT WINAPI IDirectInputDevice2AImpl_Escape(LPDIRECTINPUTDEVICE8A iface, LPDIEFFESCAPE lpDIEEsc)
Lionel Ulmer's avatar
Lionel Ulmer committed
1731
{
1732
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
1733 1734 1735 1736 1737 1738
    return IDirectInputDevice2WImpl_Escape(IDirectInputDevice8W_from_impl(This), lpDIEEsc);
}

HRESULT WINAPI IDirectInputDevice2WImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8W(iface);
1739 1740

    if (!This->acquired) return DIERR_NOTACQUIRED;
1741 1742 1743

    check_dinput_events();
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1744 1745
}

1746
HRESULT WINAPI IDirectInputDevice2AImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
Lionel Ulmer's avatar
Lionel Ulmer committed
1747
{
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_Poll(IDirectInputDevice8W_from_impl(This));
}

HRESULT WINAPI IDirectInputDevice2WImpl_SendDeviceData(LPDIRECTINPUTDEVICE8W iface, DWORD cbObjectData,
                                                       LPCDIDEVICEOBJECTDATA rgdod, LPDWORD pdwInOut,
                                                       DWORD dwFlags)
{
    FIXME("(this=%p,0x%08x,%p,%p,0x%08x): stub!\n", iface, cbObjectData, rgdod, pdwInOut, dwFlags);

1758
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1759 1760
}

1761 1762 1763 1764 1765 1766 1767 1768 1769
HRESULT WINAPI IDirectInputDevice2AImpl_SendDeviceData(LPDIRECTINPUTDEVICE8A iface, DWORD cbObjectData,
                                                       LPCDIDEVICEOBJECTDATA rgdod, LPDWORD pdwInOut,
                                                       DWORD dwFlags)
{
    IDirectInputDeviceImpl *This = impl_from_IDirectInputDevice8A(iface);
    return IDirectInputDevice2WImpl_SendDeviceData(IDirectInputDevice8W_from_impl(This), cbObjectData, rgdod,
                                                   pdwInOut, dwFlags);
}

1770
HRESULT WINAPI IDirectInputDevice7AImpl_EnumEffectsInFile(LPDIRECTINPUTDEVICE8A iface,
Lionel Ulmer's avatar
Lionel Ulmer committed
1771 1772 1773 1774 1775
							  LPCSTR lpszFileName,
							  LPDIENUMEFFECTSINFILECALLBACK pec,
							  LPVOID pvRef,
							  DWORD dwFlags)
{
1776
    FIXME("(%p)->(%s,%p,%p,%08x): stub !\n", iface, lpszFileName, pec, pvRef, dwFlags);
1777 1778
    
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1779 1780
}

1781 1782 1783 1784 1785 1786
HRESULT WINAPI IDirectInputDevice7WImpl_EnumEffectsInFile(LPDIRECTINPUTDEVICE8W iface,
							  LPCWSTR lpszFileName,
							  LPDIENUMEFFECTSINFILECALLBACK pec,
							  LPVOID pvRef,
							  DWORD dwFlags)
{
1787
    FIXME("(%p)->(%s,%p,%p,%08x): stub !\n", iface, debugstr_w(lpszFileName), pec, pvRef, dwFlags);
1788 1789
    
    return DI_OK;
1790 1791
}

1792
HRESULT WINAPI IDirectInputDevice7AImpl_WriteEffectToFile(LPDIRECTINPUTDEVICE8A iface,
Lionel Ulmer's avatar
Lionel Ulmer committed
1793 1794 1795 1796 1797
							  LPCSTR lpszFileName,
							  DWORD dwEntries,
							  LPDIFILEEFFECT rgDiFileEft,
							  DWORD dwFlags)
{
1798
    FIXME("(%p)->(%s,%08x,%p,%08x): stub !\n", iface, lpszFileName, dwEntries, rgDiFileEft, dwFlags);
1799 1800
    
    return DI_OK;
Lionel Ulmer's avatar
Lionel Ulmer committed
1801
}
1802

1803 1804 1805 1806 1807 1808
HRESULT WINAPI IDirectInputDevice7WImpl_WriteEffectToFile(LPDIRECTINPUTDEVICE8W iface,
							  LPCWSTR lpszFileName,
							  DWORD dwEntries,
							  LPDIFILEEFFECT rgDiFileEft,
							  DWORD dwFlags)
{
1809
    FIXME("(%p)->(%s,%08x,%p,%08x): stub !\n", iface, debugstr_w(lpszFileName), dwEntries, rgDiFileEft, dwFlags);
1810 1811
    
    return DI_OK;
1812 1813 1814 1815 1816 1817 1818
}

HRESULT WINAPI IDirectInputDevice8WImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
						       LPDIACTIONFORMATW lpdiaf,
						       LPCWSTR lpszUserName,
						       DWORD dwFlags)
{
1819
    FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
1820 1821 1822 1823 1824 1825
#define X(x) if (dwFlags & x) FIXME("\tdwFlags =|"#x"\n");
	X(DIDBAM_DEFAULT)
	X(DIDBAM_PRESERVE)
	X(DIDBAM_INITIALIZE)
	X(DIDBAM_HWDEFAULTS)
#undef X
1826 1827
  
    return DI_OK;
1828 1829
}

1830 1831 1832
HRESULT WINAPI IDirectInputDevice8AImpl_GetImageInfo(LPDIRECTINPUTDEVICE8A iface,
						     LPDIDEVICEIMAGEINFOHEADERA lpdiDevImageInfoHeader)
{
1833 1834 1835
    FIXME("(%p)->(%p): stub !\n", iface, lpdiDevImageInfoHeader);
    
    return DI_OK;
1836
}
1837 1838 1839 1840

HRESULT WINAPI IDirectInputDevice8WImpl_GetImageInfo(LPDIRECTINPUTDEVICE8W iface,
						     LPDIDEVICEIMAGEINFOHEADERW lpdiDevImageInfoHeader)
{
1841 1842 1843
    FIXME("(%p)->(%p): stub !\n", iface, lpdiDevImageInfoHeader);
    
    return DI_OK;
1844
}