ddraw.c 47.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright 2008 Stefan Dösinger for CodeWeavers
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#define COBJMACROS
20
#define NONAMELESSUNION
21

22 23
#include <stdarg.h>
#include "windef.h"
24 25 26 27 28 29 30
#include "winbase.h"
#include "wingdi.h"

#include "ddraw.h"
#include "d3d.h"

#include "ddrawex_private.h"
31
#include "wine/debug.h"
32 33 34

WINE_DEFAULT_DEBUG_CHANNEL(ddrawex);

35
static struct ddrawex *impl_from_IDirectDraw(IDirectDraw *iface)
36
{
37
    return CONTAINING_RECORD(iface, struct ddrawex, IDirectDraw_iface);
38 39
}

40
static struct ddrawex *impl_from_IDirectDraw2(IDirectDraw2 *iface)
41
{
42
    return CONTAINING_RECORD(iface, struct ddrawex, IDirectDraw2_iface);
43 44
}

45
static struct ddrawex *impl_from_IDirectDraw3(IDirectDraw3 *iface)
46
{
47
    return CONTAINING_RECORD(iface, struct ddrawex, IDirectDraw3_iface);
48 49
}

50
static struct ddrawex *impl_from_IDirectDraw4(IDirectDraw4 *iface)
51
{
52
    return CONTAINING_RECORD(iface, struct ddrawex, IDirectDraw4_iface);
53 54
}

55
static HRESULT WINAPI ddrawex4_QueryInterface(IDirectDraw4 *iface, REFIID riid, void **out)
56
{
57
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);
58

59
    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
60

61
    if (!riid)
62
    {
63
        *out = NULL;
64 65 66
        return DDERR_INVALIDPARAMS;
    }

67 68
    if (IsEqualGUID(&IID_IDirectDraw4, riid)
            || IsEqualGUID(&IID_IUnknown, riid))
69
    {
70
        *out = &ddrawex->IDirectDraw4_iface;
71
    }
72
    else if (IsEqualGUID(&IID_IDirectDraw3, riid))
73
    {
74
        *out = &ddrawex->IDirectDraw3_iface;
75
    }
76
    else if (IsEqualGUID(&IID_IDirectDraw2, riid))
77
    {
78
        *out = &ddrawex->IDirectDraw2_iface;
79
    }
80
    else if (IsEqualGUID(&IID_IDirectDraw, riid))
81
    {
82
        *out = &ddrawex->IDirectDraw_iface;
83 84 85
    }
    else
    {
86 87
        *out = NULL;
        WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
88 89
        return E_NOINTERFACE;
    }
90 91

    IUnknown_AddRef((IUnknown *)*out);
92 93 94
    return S_OK;
}

95
static HRESULT WINAPI ddrawex3_QueryInterface(IDirectDraw3 *iface, REFIID riid, void **out)
96
{
97 98 99 100 101
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);

    return ddrawex4_QueryInterface(&ddrawex->IDirectDraw4_iface, riid, out);
102 103
}

104
static HRESULT WINAPI ddrawex2_QueryInterface(IDirectDraw2 *iface, REFIID riid, void **out)
105
{
106 107 108 109 110
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);

    return ddrawex4_QueryInterface(&ddrawex->IDirectDraw4_iface, riid, out);
111 112
}

113
static HRESULT WINAPI ddrawex1_QueryInterface(IDirectDraw *iface, REFIID riid, void **out)
114
{
115 116 117 118 119
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);

    return ddrawex4_QueryInterface(&ddrawex->IDirectDraw4_iface, riid, out);
120 121
}

122
static ULONG WINAPI ddrawex4_AddRef(IDirectDraw4 *iface)
123
{
124 125
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);
    ULONG refcount = InterlockedIncrement(&ddrawex->ref);
126

127
    TRACE("%p increasing refcount to %u.\n", iface, refcount);
128

129
    return refcount;
130 131
}

132
static ULONG WINAPI ddrawex3_AddRef(IDirectDraw3 *iface)
133
{
134 135 136 137 138
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_AddRef(&ddrawex->IDirectDraw4_iface);
139 140
}

141
static ULONG WINAPI ddrawex2_AddRef(IDirectDraw2 *iface)
142
{
143 144 145 146 147
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_AddRef(&ddrawex->IDirectDraw4_iface);
148 149
}

150
static ULONG WINAPI ddrawex1_AddRef(IDirectDraw *iface)
151
{
152 153 154 155 156
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_AddRef(&ddrawex->IDirectDraw4_iface);
157 158
}

159
static ULONG WINAPI ddrawex4_Release(IDirectDraw4 *iface)
160
{
161 162
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);
    ULONG refcount = InterlockedDecrement(&ddrawex->ref);
163

164
    TRACE("%p decreasing refcount to %u.\n", iface, refcount);
165

166
    if (!refcount)
167
    {
168
        IDirectDraw4_Release(ddrawex->parent);
169
        heap_free(ddrawex);
170
    }
171 172

    return refcount;
173 174
}

175
static ULONG WINAPI ddrawex3_Release(IDirectDraw3 *iface)
176
{
177 178 179 180 181
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_Release(&ddrawex->IDirectDraw4_iface);
182 183
}

184
static ULONG WINAPI ddrawex2_Release(IDirectDraw2 *iface)
185
{
186 187 188 189 190
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_Release(&ddrawex->IDirectDraw4_iface);
191 192
}

193
static ULONG WINAPI ddrawex1_Release(IDirectDraw *iface)
194
{
195 196 197 198 199
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_Release(&ddrawex->IDirectDraw4_iface);
200 201
}

202
static HRESULT WINAPI ddrawex4_Compact(IDirectDraw4 *iface)
203
{
204 205 206
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p.\n", iface);
207

208
    return IDirectDraw4_Compact(ddrawex->parent);
209 210
}

211
static HRESULT WINAPI ddrawex3_Compact(IDirectDraw3 *iface)
212
{
213 214 215 216 217
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_Compact(&ddrawex->IDirectDraw4_iface);
218 219
}

220
static HRESULT WINAPI ddrawex2_Compact(IDirectDraw2 *iface)
221
{
222 223 224 225 226
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_Compact(&ddrawex->IDirectDraw4_iface);
227 228
}

229
static HRESULT WINAPI ddrawex1_Compact(IDirectDraw *iface)
230
{
231 232 233 234 235
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_Compact(&ddrawex->IDirectDraw4_iface);
236 237
}

238 239
static HRESULT WINAPI ddrawex4_CreateClipper(IDirectDraw4 *iface, DWORD flags,
        IDirectDrawClipper **clipper, IUnknown *outer_unknown)
240
{
241
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);
242

243 244
    TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
            iface, flags, clipper, outer_unknown);
245

246 247 248 249 250
    /* This may require a wrapper interface for clippers too which handles this. */
    if (outer_unknown)
        FIXME("Test and implement aggregation for ddrawex clippers.\n");

    return IDirectDraw4_CreateClipper(ddrawex->parent, flags, clipper, outer_unknown);
251 252
}

253 254
static HRESULT WINAPI ddrawex3_CreateClipper(IDirectDraw3 *iface, DWORD flags,
        IDirectDrawClipper **clipper, IUnknown *outer_unknown)
255
{
256 257 258 259 260 261
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
            iface, flags, clipper, outer_unknown);

    return ddrawex4_CreateClipper(&ddrawex->IDirectDraw4_iface, flags, clipper, outer_unknown);
262 263
}

264 265
static HRESULT WINAPI ddrawex2_CreateClipper(IDirectDraw2 *iface, DWORD flags,
        IDirectDrawClipper **clipper, IUnknown *outer_unknown)
266
{
267 268 269 270 271 272
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
            iface, flags, clipper, outer_unknown);

    return ddrawex4_CreateClipper(&ddrawex->IDirectDraw4_iface, flags, clipper, outer_unknown);
273 274
}

275 276
static HRESULT WINAPI ddrawex1_CreateClipper(IDirectDraw *iface, DWORD flags,
        IDirectDrawClipper **clipper, IUnknown *outer_unknown)
277
{
278 279 280 281 282 283
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, flags %#x, clipper %p, outer_unknown %p.\n",
            iface, flags, clipper, outer_unknown);

    return ddrawex4_CreateClipper(&ddrawex->IDirectDraw4_iface, flags, clipper, outer_unknown);
284 285
}

286 287
static HRESULT WINAPI ddrawex4_CreatePalette(IDirectDraw4 *iface, DWORD flags,
        PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
288
{
289
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);
290

291 292
    TRACE("iface %p, flags %#x. entries %p, palette %p, outer_unknown %p.\n",
            iface, flags, entries, palette, outer_unknown);
293

294 295 296 297 298
    /* This may require a wrapper interface for palettes too which handles this. */
    if (outer_unknown)
        FIXME("Test and implement aggregation for ddrawex palettes.\n");

    return IDirectDraw4_CreatePalette(ddrawex->parent, flags, entries, palette, outer_unknown);
299 300
}

301 302
static HRESULT WINAPI ddrawex3_CreatePalette(IDirectDraw3 *iface, DWORD flags,
        PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
303
{
304 305 306 307 308 309
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p, flags %#x. entries %p, palette %p, outer_unknown %p.\n",
            iface, flags, entries, palette, outer_unknown);

    return ddrawex4_CreatePalette(&ddrawex->IDirectDraw4_iface, flags, entries, palette, outer_unknown);
310 311
}

312 313
static HRESULT WINAPI ddrawex2_CreatePalette(IDirectDraw2 *iface, DWORD flags,
        PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
314
{
315 316 317 318 319 320
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, flags %#x. entries %p, palette %p, outer_unknown %p.\n",
            iface, flags, entries, palette, outer_unknown);

    return ddrawex4_CreatePalette(&ddrawex->IDirectDraw4_iface, flags, entries, palette, outer_unknown);
321 322
}

323 324
static HRESULT WINAPI ddrawex1_CreatePalette(IDirectDraw *iface, DWORD flags,
        PALETTEENTRY *entries, IDirectDrawPalette **palette, IUnknown *outer_unknown)
325
{
326 327 328 329 330 331
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, flags %#x. entries %p, palette %p, outer_unknown %p.\n",
            iface, flags, entries, palette, outer_unknown);

    return ddrawex4_CreatePalette(&ddrawex->IDirectDraw4_iface, flags, entries, palette, outer_unknown);
332 333
}

334 335
static HRESULT WINAPI ddrawex4_CreateSurface(IDirectDraw4 *iface, DDSURFACEDESC2 *desc,
        IDirectDrawSurface4 **surface, IUnknown *outer_unknown)
336
{
337
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);
338
    HRESULT hr;
339 340
    const DWORD perm_dc_flags = DDSCAPS_VIDEOMEMORY | DDSCAPS_SYSTEMMEMORY;
    BOOL permanent_dc;
341
    IDirectDrawSurface4 *inner_surface;
342

343 344
    TRACE("iface %p, desc %p, surface %p, outer_unknown %p.\n",
            iface, desc, surface, outer_unknown);
345

346 347 348 349 350 351 352 353 354 355
    /* Handle this in this dll. Don't forward the outer_unknown to ddraw.dll. */
    if (outer_unknown)
        FIXME("Implement aggregation for ddrawex surfaces.\n");

    /* Plain ddraw.dll refuses to create a surface that has both VIDMEM and
     * SYSMEM flags set. In ddrawex this succeeds, and the GetDC() call
     * changes the behavior. The DC is permanently valid, and the surface can
     * be locked between GetDC() and ReleaseDC() calls. GetDC() can be called
     * more than once too. */
    if ((desc->ddsCaps.dwCaps & perm_dc_flags) == perm_dc_flags)
356 357
    {
        permanent_dc = TRUE;
358 359
        desc->ddsCaps.dwCaps &= ~DDSCAPS_VIDEOMEMORY;
        desc->ddsCaps.dwCaps |= DDSCAPS_OWNDC;
360 361 362 363 364 365
    }
    else
    {
        permanent_dc = FALSE;
    }

366 367 368 369 370 371 372 373 374 375 376
    hr = IDirectDraw4_CreateSurface(ddrawex->parent, desc, &inner_surface, outer_unknown);
    if (FAILED(hr))
    {
        *surface = NULL;
        return hr;
    }

    *surface = dds_get_outer(inner_surface);
    /* The wrapper created by dds_get_outer holds a reference to its inner surface. */
    IDirectDrawSurface4_Release(inner_surface);

377 378 379
    if (permanent_dc)
        prepare_permanent_dc(*surface);

380
    return hr;
381 382
}

383
void DDSD_to_DDSD2(const DDSURFACEDESC *in, DDSURFACEDESC2 *out)
384 385 386 387 388 389
{
    memset(out, 0, sizeof(*out));
    out->dwSize = sizeof(*out);
    out->dwFlags = in->dwFlags;
    if(in->dwFlags & DDSD_WIDTH) out->dwWidth = in->dwWidth;
    if(in->dwFlags & DDSD_HEIGHT) out->dwHeight = in->dwHeight;
390
    if(in->dwFlags & DDSD_PIXELFORMAT) out->u4.ddpfPixelFormat = in->ddpfPixelFormat;
391
    if(in->dwFlags & DDSD_CAPS) out->ddsCaps.dwCaps = in->ddsCaps.dwCaps;
392
    if(in->dwFlags & DDSD_PITCH) out->u1.lPitch = in->u1.lPitch;
393
    if(in->dwFlags & DDSD_BACKBUFFERCOUNT) out->u5.dwBackBufferCount = in->dwBackBufferCount;
394
    if(in->dwFlags & DDSD_ZBUFFERBITDEPTH) out->u2.dwMipMapCount = in->u2.dwZBufferBitDepth; /* same union */
395
    if(in->dwFlags & DDSD_ALPHABITDEPTH) out->dwAlphaBitDepth = in->dwAlphaBitDepth;
396 397
    /* DDraw(native, and wine) does not set the DDSD_LPSURFACE, so always copy */
    out->lpSurface = in->lpSurface;
398
    if(in->dwFlags & DDSD_CKDESTOVERLAY) out->u3.ddckCKDestOverlay = in->ddckCKDestOverlay;
399 400 401
    if(in->dwFlags & DDSD_CKDESTBLT) out->ddckCKDestBlt = in->ddckCKDestBlt;
    if(in->dwFlags & DDSD_CKSRCOVERLAY) out->ddckCKSrcOverlay = in->ddckCKSrcOverlay;
    if(in->dwFlags & DDSD_CKSRCBLT) out->ddckCKSrcBlt = in->ddckCKSrcBlt;
402 403 404
    if(in->dwFlags & DDSD_MIPMAPCOUNT) out->u2.dwMipMapCount = in->u2.dwMipMapCount;
    if(in->dwFlags & DDSD_REFRESHRATE) out->u2.dwRefreshRate = in->u2.dwRefreshRate;
    if(in->dwFlags & DDSD_LINEARSIZE) out->u1.dwLinearSize = in->u1.dwLinearSize;
405 406 407 408 409
    /* Does not exist in DDSURFACEDESC:
     * DDSD_TEXTURESTAGE, DDSD_FVF, DDSD_SRCVBHANDLE,
     */
}

410
void DDSD2_to_DDSD(const DDSURFACEDESC2 *in, DDSURFACEDESC *out)
411 412 413 414 415 416
{
    memset(out, 0, sizeof(*out));
    out->dwSize = sizeof(*out);
    out->dwFlags = in->dwFlags;
    if(in->dwFlags & DDSD_WIDTH) out->dwWidth = in->dwWidth;
    if(in->dwFlags & DDSD_HEIGHT) out->dwHeight = in->dwHeight;
417
    if(in->dwFlags & DDSD_PIXELFORMAT) out->ddpfPixelFormat = in->u4.ddpfPixelFormat;
418
    if(in->dwFlags & DDSD_CAPS) out->ddsCaps.dwCaps = in->ddsCaps.dwCaps;
419
    if(in->dwFlags & DDSD_PITCH) out->u1.lPitch = in->u1.lPitch;
420
    if(in->dwFlags & DDSD_BACKBUFFERCOUNT) out->dwBackBufferCount = in->u5.dwBackBufferCount;
421
    if(in->dwFlags & DDSD_ZBUFFERBITDEPTH) out->u2.dwZBufferBitDepth = in->u2.dwMipMapCount; /* same union */
422
    if(in->dwFlags & DDSD_ALPHABITDEPTH) out->dwAlphaBitDepth = in->dwAlphaBitDepth;
423 424
    /* DDraw(native, and wine) does not set the DDSD_LPSURFACE, so always copy */
    out->lpSurface = in->lpSurface;
425
    if(in->dwFlags & DDSD_CKDESTOVERLAY) out->ddckCKDestOverlay = in->u3.ddckCKDestOverlay;
426 427 428
    if(in->dwFlags & DDSD_CKDESTBLT) out->ddckCKDestBlt = in->ddckCKDestBlt;
    if(in->dwFlags & DDSD_CKSRCOVERLAY) out->ddckCKSrcOverlay = in->ddckCKSrcOverlay;
    if(in->dwFlags & DDSD_CKSRCBLT) out->ddckCKSrcBlt = in->ddckCKSrcBlt;
429 430 431
    if(in->dwFlags & DDSD_MIPMAPCOUNT) out->u2.dwMipMapCount = in->u2.dwMipMapCount;
    if(in->dwFlags & DDSD_REFRESHRATE) out->u2.dwRefreshRate = in->u2.dwRefreshRate;
    if(in->dwFlags & DDSD_LINEARSIZE) out->u1.dwLinearSize = in->u1.dwLinearSize;
432 433 434 435 436 437 438 439 440
    /* Does not exist in DDSURFACEDESC:
     * DDSD_TEXTURESTAGE, DDSD_FVF, DDSD_SRCVBHANDLE,
     */
    if(in->dwFlags & DDSD_TEXTURESTAGE) WARN("Does not exist in DDSURFACEDESC: DDSD_TEXTURESTAGE\n");
    if(in->dwFlags & DDSD_FVF) WARN("Does not exist in DDSURFACEDESC: DDSD_FVF\n");
    if(in->dwFlags & DDSD_SRCVBHANDLE) WARN("Does not exist in DDSURFACEDESC: DDSD_SRCVBHANDLE\n");
    out->dwFlags &= ~(DDSD_TEXTURESTAGE | DDSD_FVF | DDSD_SRCVBHANDLE);
}

441 442
static HRESULT WINAPI ddrawex3_CreateSurface(IDirectDraw3 *iface, DDSURFACEDESC *desc,
        IDirectDrawSurface **surface, IUnknown *outer_unknown)
443
{
444
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);
445 446 447 448
    DDSURFACEDESC2 ddsd2;
    IDirectDrawSurface4 *surf4 = NULL;
    HRESULT hr;

449 450
    TRACE("iface %p, desc %p, surface %p, outer_unknown %p.\n",
            iface, desc, surface, outer_unknown);
451

452 453
    DDSD_to_DDSD2(desc, &ddsd2);
    if (FAILED(hr = ddrawex4_CreateSurface(&ddrawex->IDirectDraw4_iface, &ddsd2, &surf4, outer_unknown)))
454
    {
455
        *surface = NULL;
456 457 458
        return hr;
    }

459
    TRACE("Got surface %p\n", surf4);
460
    IDirectDrawSurface4_QueryInterface(surf4, &IID_IDirectDrawSurface, (void **)surface);
461 462 463 464
    IDirectDrawSurface4_Release(surf4);
    return hr;
}

465 466
static HRESULT WINAPI ddrawex2_CreateSurface(IDirectDraw2 *iface, DDSURFACEDESC *desc,
        IDirectDrawSurface **surface, IUnknown *outer_unknown)
467
{
468 469 470 471 472 473
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, desc %p, surface %p, outer_unknown %p.\n",
            iface, desc, surface, outer_unknown);

    return ddrawex3_CreateSurface(&ddrawex->IDirectDraw3_iface, desc, surface, outer_unknown);
474 475
}

476 477
static HRESULT WINAPI ddrawex1_CreateSurface(IDirectDraw *iface, DDSURFACEDESC *desc,
        IDirectDrawSurface **surface, IUnknown *outer_unknown)
478
{
479 480 481 482 483 484
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, desc %p, surface %p, outer_unknown %p.\n",
            iface, desc, surface, outer_unknown);

    return ddrawex3_CreateSurface(&ddrawex->IDirectDraw3_iface, desc, surface, outer_unknown);
485 486
}

487
static HRESULT WINAPI ddrawex4_DuplicateSurface(IDirectDraw4 *iface,
488
        IDirectDrawSurface4 *src, IDirectDrawSurface4 **dst)
489
{
490
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);
491
    struct ddrawex_surface *src_impl = unsafe_impl_from_IDirectDrawSurface4(src);
492

493 494
    TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
    FIXME("Create a wrapper surface.\n");
495

496
    return IDirectDraw4_DuplicateSurface(ddrawex->parent, src_impl ? src_impl->parent : NULL, dst);
497 498
}

499
static HRESULT WINAPI ddrawex3_DuplicateSurface(IDirectDraw3 *iface,
500
        IDirectDrawSurface *src, IDirectDrawSurface **dst)
501
{
502
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);
503 504 505 506
    IDirectDrawSurface4 *src_4;
    IDirectDrawSurface4 *dst_4;
    HRESULT hr;

507
    TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);
508

509 510 511 512
    IDirectDrawSurface_QueryInterface(src, &IID_IDirectDrawSurface4, (void **)&src_4);
    hr = ddrawex4_DuplicateSurface(&ddrawex->IDirectDraw4_iface, src_4, &dst_4);
    IDirectDrawSurface4_Release(src_4);
    if (FAILED(hr))
513 514 515 516
    {
        *dst = NULL;
        return hr;
    }
517 518

    IDirectDrawSurface4_QueryInterface(dst_4, &IID_IDirectDrawSurface, (void **)dst);
519
    IDirectDrawSurface4_Release(dst_4);
520

521 522 523
    return hr;
}

524
static HRESULT WINAPI ddrawex2_DuplicateSurface(IDirectDraw2 *iface,
525
        IDirectDrawSurface *src, IDirectDrawSurface **dst)
526
{
527 528 529 530 531
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);

    return ddrawex3_DuplicateSurface(&ddrawex->IDirectDraw3_iface, src, dst);
532 533
}

534 535
static HRESULT WINAPI ddrawex1_DuplicateSurface(IDirectDraw *iface,
        IDirectDrawSurface *src, IDirectDrawSurface **dst)
536
{
537 538 539 540 541
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, src %p, dst %p.\n", iface, src, dst);

    return ddrawex3_DuplicateSurface(&ddrawex->IDirectDraw3_iface, src, dst);
542 543
}

544 545
static HRESULT WINAPI ddrawex4_EnumDisplayModes(IDirectDraw4 *iface, DWORD flags,
        DDSURFACEDESC2 *desc, void *ctx, LPDDENUMMODESCALLBACK2 cb)
546
{
547 548 549
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p, flags %#x, desc %p, ctx %p, cb %p.\n", iface, flags, desc, ctx, cb);
550

551
    return IDirectDraw4_EnumDisplayModes(ddrawex->parent, flags, desc, ctx, cb);
552 553 554 555 556 557 558 559 560 561 562
}

struct enummodes_ctx
{
    LPDDENUMMODESCALLBACK orig_cb;
    void *orig_ctx;
};

static HRESULT WINAPI
enum_modes_cb2(DDSURFACEDESC2 *ddsd2, void *vctx)
{
563
    struct enummodes_ctx *ctx = vctx;
564 565 566 567 568 569
    DDSURFACEDESC ddsd;

    DDSD2_to_DDSD(ddsd2, &ddsd);
    return ctx->orig_cb(&ddsd, ctx->orig_ctx);
}

570 571
static HRESULT WINAPI ddrawex3_EnumDisplayModes(IDirectDraw3 *iface, DWORD flags,
        DDSURFACEDESC *desc, void *ctx, LPDDENUMMODESCALLBACK cb)
572
{
573 574
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);
    struct enummodes_ctx cb_ctx;
575 576
    DDSURFACEDESC2 ddsd2;

577 578 579 580 581 582
    TRACE("iface %p, flags %#x, desc %p, ctx %p, cb %p.\n", iface, flags, desc, ctx, cb);

    DDSD_to_DDSD2(desc, &ddsd2);
    cb_ctx.orig_cb = cb;
    cb_ctx.orig_ctx = ctx;
    return ddrawex4_EnumDisplayModes(&ddrawex->IDirectDraw4_iface, flags, &ddsd2, &cb_ctx, enum_modes_cb2);
583 584
}

585 586
static HRESULT WINAPI ddrawex2_EnumDisplayModes(IDirectDraw2 *iface, DWORD flags,
        DDSURFACEDESC *desc, void *ctx, LPDDENUMMODESCALLBACK cb)
587
{
588 589 590 591 592
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, flags %#x, desc %p, ctx %p, cb %p.\n", iface, flags, desc, ctx, cb);

    return ddrawex3_EnumDisplayModes(&ddrawex->IDirectDraw3_iface, flags, desc, ctx, cb);
593 594
}

595 596
static HRESULT WINAPI ddrawex1_EnumDisplayModes(IDirectDraw *iface, DWORD flags,
        DDSURFACEDESC *desc, void *ctx, LPDDENUMMODESCALLBACK cb)
597
{
598 599 600 601 602
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, flags %#x, desc %p, ctx %p, cb %p.\n", iface, flags, desc, ctx, cb);

    return ddrawex3_EnumDisplayModes(&ddrawex->IDirectDraw3_iface, flags, desc, ctx, cb);
603 604
}

605 606 607 608 609 610 611 612 613
struct enumsurfaces4_ctx
{
    LPDDENUMSURFACESCALLBACK2 orig_cb;
    void *orig_ctx;
};

static HRESULT WINAPI
enum_surfaces_wrapper(IDirectDrawSurface4 *surf4, DDSURFACEDESC2 *ddsd2, void *vctx)
{
614
    struct enumsurfaces4_ctx *ctx = vctx;
615 616 617 618 619 620 621
    IDirectDrawSurface4 *outer = dds_get_outer(surf4);
    IDirectDrawSurface4_AddRef(outer);
    IDirectDrawSurface4_Release(surf4);
    TRACE("Returning wrapper surface %p for enumerated inner surface %p\n", outer, surf4);
    return ctx->orig_cb(outer, ddsd2, ctx->orig_ctx);
}

622 623
static HRESULT WINAPI ddrawex4_EnumSurfaces(IDirectDraw4 *iface, DWORD flags,
        DDSURFACEDESC2 *desc, void *ctx, LPDDENUMSURFACESCALLBACK2 cb)
624
{
625 626
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);
    struct enumsurfaces4_ctx cb_ctx;
627

628 629 630 631 632
    TRACE("iface %p, flags %#x, desc %p, ctx %p, cb %p.\n", iface, flags, desc, ctx, cb);

    cb_ctx.orig_cb = cb;
    cb_ctx.orig_ctx = ctx;
    return IDirectDraw4_EnumSurfaces(ddrawex->parent, flags, desc, &cb_ctx, enum_surfaces_wrapper);
633 634 635 636 637 638 639 640 641 642 643
}

struct enumsurfaces_ctx
{
    LPDDENUMSURFACESCALLBACK orig_cb;
    void *orig_ctx;
};

static HRESULT WINAPI
enum_surfaces_cb2(IDirectDrawSurface4 *surf4, DDSURFACEDESC2 *ddsd2, void *vctx)
{
644
    struct enumsurfaces_ctx *ctx = vctx;
645 646 647 648 649 650 651 652 653 654 655 656
    IDirectDrawSurface *surf1;
    DDSURFACEDESC ddsd;

    /* Keep the reference, it goes to the application */
    IDirectDrawSurface4_QueryInterface(surf4, &IID_IDirectDrawSurface, (void **) &surf1);
    /* Release the reference this function got */
    IDirectDrawSurface4_Release(surf4);

    DDSD2_to_DDSD(ddsd2, &ddsd);
    return ctx->orig_cb(surf1, &ddsd, ctx->orig_ctx);
}

657 658
static HRESULT WINAPI ddrawex3_EnumSurfaces(IDirectDraw3 *iface, DWORD flags,
        DDSURFACEDESC *desc, void *ctx, LPDDENUMSURFACESCALLBACK cb)
659
{
660
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);
661
    DDSURFACEDESC2 ddsd2;
662 663 664
    struct enumsurfaces_ctx cb_ctx;

    TRACE("iface %p, flags %#x, desc %p, ctx %p, cb %p.\n", iface, flags, desc, ctx, cb);
665

666 667 668 669
    DDSD_to_DDSD2(desc, &ddsd2);
    cb_ctx.orig_cb = cb;
    cb_ctx.orig_ctx = ctx;
    return ddrawex4_EnumSurfaces(&ddrawex->IDirectDraw4_iface, flags, &ddsd2, &cb_ctx, enum_surfaces_cb2);
670 671
}

672 673
static HRESULT WINAPI ddrawex2_EnumSurfaces(IDirectDraw2 *iface, DWORD flags,
        DDSURFACEDESC *desc, void *ctx, LPDDENUMSURFACESCALLBACK cb)
674
{
675 676 677 678 679
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, flags %#x, desc %p, ctx %p, cb %p.\n", iface, flags, desc, ctx, cb);

    return ddrawex3_EnumSurfaces(&ddrawex->IDirectDraw3_iface, flags, desc, ctx, cb);
680 681
}

682 683
static HRESULT WINAPI ddrawex1_EnumSurfaces(IDirectDraw *iface, DWORD flags,
        DDSURFACEDESC *desc, void *ctx, LPDDENUMSURFACESCALLBACK cb)
684
{
685 686 687 688 689
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, flags %#x, desc %p, ctx %p, cb %p.\n", iface, flags, desc, ctx, cb);

    return ddrawex3_EnumSurfaces(&ddrawex->IDirectDraw3_iface, flags, desc, ctx, cb);
690 691
}

692
static HRESULT WINAPI ddrawex4_FlipToGDISurface(IDirectDraw4 *iface)
693
{
694
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);
695

696 697 698
    TRACE("iface %p.\n", iface);

    return IDirectDraw4_FlipToGDISurface(ddrawex->parent);
699 700
}

701
static HRESULT WINAPI ddrawex3_FlipToGDISurface(IDirectDraw3 *iface)
702
{
703 704 705 706 707
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_FlipToGDISurface(&ddrawex->IDirectDraw4_iface);
708 709
}

710
static HRESULT WINAPI ddrawex2_FlipToGDISurface(IDirectDraw2 *iface)
711
{
712 713 714 715 716
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_FlipToGDISurface(&ddrawex->IDirectDraw4_iface);
717 718
}

719
static HRESULT WINAPI ddrawex1_FlipToGDISurface(IDirectDraw *iface)
720
{
721 722 723 724 725
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_FlipToGDISurface(&ddrawex->IDirectDraw4_iface);
726 727
}

728
static HRESULT WINAPI ddrawex4_GetCaps(IDirectDraw4 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
729
{
730 731 732 733 734
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);

    return IDirectDraw4_GetCaps(ddrawex->parent, driver_caps, hel_caps);
735 736
}

737
static HRESULT WINAPI ddrawex3_GetCaps(IDirectDraw3 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
738
{
739 740 741 742 743
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);

    return ddrawex4_GetCaps(&ddrawex->IDirectDraw4_iface, driver_caps, hel_caps);
744 745
}

746
static HRESULT WINAPI ddrawex2_GetCaps(IDirectDraw2 *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
747
{
748 749 750 751 752
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);

    return ddrawex4_GetCaps(&ddrawex->IDirectDraw4_iface, driver_caps, hel_caps);
753 754
}

755
static HRESULT WINAPI ddrawex1_GetCaps(IDirectDraw *iface, DDCAPS *driver_caps, DDCAPS *hel_caps)
756
{
757 758 759 760 761
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, driver_caps %p, hel_caps %p.\n", iface, driver_caps, hel_caps);

    return ddrawex4_GetCaps(&ddrawex->IDirectDraw4_iface, driver_caps, hel_caps);
762 763
}

764
static HRESULT WINAPI ddrawex4_GetDisplayMode(IDirectDraw4 *iface, DDSURFACEDESC2 *desc)
765
{
766 767 768 769 770
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p, desc %p.\n", iface, desc);

    return IDirectDraw4_GetDisplayMode(ddrawex->parent, desc);
771 772
}

773
static HRESULT WINAPI ddrawex3_GetDisplayMode(IDirectDraw3 *iface, DDSURFACEDESC *desc)
774
{
775
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);
776 777 778
    DDSURFACEDESC2 ddsd2;
    HRESULT hr;

779 780 781 782 783
    TRACE("iface %p, desc %p.\n", iface, desc);

    hr = ddrawex4_GetDisplayMode(&ddrawex->IDirectDraw4_iface, &ddsd2);
    DDSD2_to_DDSD(&ddsd2, desc);

784 785 786
    return hr;
}

787
static HRESULT WINAPI ddrawex2_GetDisplayMode(IDirectDraw2 *iface, DDSURFACEDESC *desc)
788
{
789 790 791 792 793
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, desc %p.\n", iface, desc);

    return ddrawex3_GetDisplayMode(&ddrawex->IDirectDraw3_iface, desc);
794 795
}

796
static HRESULT WINAPI ddrawex1_GetDisplayMode(IDirectDraw *iface, DDSURFACEDESC *desc)
797
{
798 799 800 801 802
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, desc %p.\n", iface, desc);

    return ddrawex3_GetDisplayMode(&ddrawex->IDirectDraw3_iface, desc);
803 804
}

805
static HRESULT WINAPI ddrawex4_GetFourCCCodes(IDirectDraw4 *iface, DWORD *code_count, DWORD *codes)
806
{
807 808 809 810 811
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p, code_count %p, codes %p.\n", iface, code_count, codes);

    return IDirectDraw4_GetFourCCCodes(ddrawex->parent, code_count, codes);
812 813
}

814
static HRESULT WINAPI ddrawex3_GetFourCCCodes(IDirectDraw3 *iface, DWORD *code_count, DWORD *codes)
815
{
816 817 818 819 820
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p, code_count %p, codes %p.\n", iface, code_count, codes);

    return ddrawex4_GetFourCCCodes(&ddrawex->IDirectDraw4_iface, code_count, codes);
821 822
}

823
static HRESULT WINAPI ddrawex2_GetFourCCCodes(IDirectDraw2 *iface, DWORD *code_count, DWORD *codes)
824
{
825 826 827 828 829
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, code_count %p, codes %p.\n", iface, code_count, codes);

    return ddrawex4_GetFourCCCodes(&ddrawex->IDirectDraw4_iface, code_count, codes);
830 831
}

832
static HRESULT WINAPI ddrawex1_GetFourCCCodes(IDirectDraw *iface, DWORD *code_count, DWORD *codes)
833
{
834 835 836 837 838
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, code_count %p, codes %p.\n", iface, code_count, codes);

    return ddrawex4_GetFourCCCodes(&ddrawex->IDirectDraw4_iface, code_count, codes);
839 840
}

841
static HRESULT WINAPI ddrawex4_GetGDISurface(IDirectDraw4 *iface, IDirectDrawSurface4 **gdi_surface)
842
{
843
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);
844 845 846
    IDirectDrawSurface4 *inner = NULL;
    HRESULT hr;

847 848 849
    TRACE("iface %p, gdi_surface %p.\n", iface, gdi_surface);

    if (FAILED(hr = IDirectDraw4_GetGDISurface(ddrawex->parent, &inner)))
850
    {
851 852
        *gdi_surface = NULL;
        return hr;
853
    }
854 855 856 857

    *gdi_surface = dds_get_outer(inner);
    IDirectDrawSurface4_AddRef(*gdi_surface);
    IDirectDrawSurface4_Release(inner);
858
    return hr;
859 860
}

861
static HRESULT WINAPI ddrawex3_GetGDISurface(IDirectDraw3 *iface, IDirectDrawSurface **gdi_surface)
862
{
863
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);
864 865 866
    IDirectDrawSurface4 *surf4;
    HRESULT hr;

867 868 869 870 871
    TRACE("iface %p, gdi_surface %p.\n", iface, gdi_surface);

    if (FAILED(hr = ddrawex4_GetGDISurface(&ddrawex->IDirectDraw4_iface, &surf4)))
    {
        *gdi_surface = NULL;
872 873 874
        return hr;
    }

875
    IDirectDrawSurface4_QueryInterface(surf4, &IID_IDirectDrawSurface, (void **)gdi_surface);
876 877 878 879
    IDirectDrawSurface4_Release(surf4);
    return hr;
}

880
static HRESULT WINAPI ddrawex2_GetGDISurface(IDirectDraw2 *iface, IDirectDrawSurface **gdi_surface)
881
{
882 883 884 885 886
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, gdi_surface %p.\n", iface, gdi_surface);

    return ddrawex3_GetGDISurface(&ddrawex->IDirectDraw3_iface, gdi_surface);
887 888
}

889
static HRESULT WINAPI ddrawex1_GetGDISurface(IDirectDraw *iface, IDirectDrawSurface **gdi_surface)
890
{
891 892 893 894 895
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, gdi_surface %p.\n", iface, gdi_surface);

    return ddrawex3_GetGDISurface(&ddrawex->IDirectDraw3_iface, gdi_surface);
896 897
}

898
static HRESULT WINAPI ddrawex4_GetMonitorFrequency(IDirectDraw4 *iface, DWORD *frequency)
899
{
900 901 902 903 904
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p, frequency %p.\n", iface, frequency);

    return IDirectDraw4_GetMonitorFrequency(ddrawex->parent, frequency);
905 906
}

907
static HRESULT WINAPI ddrawex3_GetMonitorFrequency(IDirectDraw3 *iface, DWORD *frequency)
908
{
909 910 911 912 913
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p, frequency %p.\n", iface, frequency);

    return ddrawex4_GetMonitorFrequency(&ddrawex->IDirectDraw4_iface, frequency);
914 915
}

916
static HRESULT WINAPI ddrawex2_GetMonitorFrequency(IDirectDraw2 *iface, DWORD *frequency)
917
{
918 919 920 921 922
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, frequency %p.\n", iface, frequency);

    return ddrawex4_GetMonitorFrequency(&ddrawex->IDirectDraw4_iface, frequency);
923 924
}

925
static HRESULT WINAPI ddrawex1_GetMonitorFrequency(IDirectDraw *iface, DWORD *frequency)
926
{
927 928 929 930 931
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, frequency %p.\n", iface, frequency);

    return ddrawex4_GetMonitorFrequency(&ddrawex->IDirectDraw4_iface, frequency);
932 933
}

934
static HRESULT WINAPI ddrawex4_GetScanLine(IDirectDraw4 *iface, DWORD *line)
935
{
936 937 938 939 940
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p, line %p.\n", iface, line);

    return IDirectDraw4_GetScanLine(ddrawex->parent, line);
941 942
}

943
static HRESULT WINAPI ddrawex3_GetScanLine(IDirectDraw3 *iface, DWORD *line)
944
{
945 946 947 948 949
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p, line %p.\n", iface, line);

    return ddrawex4_GetScanLine(&ddrawex->IDirectDraw4_iface, line);
950 951
}

952
static HRESULT WINAPI ddrawex2_GetScanLine(IDirectDraw2 *iface, DWORD *line)
953
{
954 955 956 957 958
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, line %p.\n", iface, line);

    return ddrawex4_GetScanLine(&ddrawex->IDirectDraw4_iface, line);
959 960
}

961
static HRESULT WINAPI ddrawex1_GetScanLine(IDirectDraw *iface, DWORD *line)
962
{
963 964 965 966 967
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, line %p.\n", iface, line);

    return ddrawex4_GetScanLine(&ddrawex->IDirectDraw4_iface, line);
968 969
}

970
static HRESULT WINAPI ddrawex4_GetVerticalBlankStatus(IDirectDraw4 *iface, BOOL *status)
971
{
972 973 974 975 976
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p, status %p.\n", iface, status);

    return IDirectDraw4_GetVerticalBlankStatus(ddrawex->parent, status);
977 978
}

979
static HRESULT WINAPI ddrawex3_GetVerticalBlankStatus(IDirectDraw3 *iface, BOOL *status)
980
{
981 982 983 984 985
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p, status %p.\n", iface, status);

    return ddrawex4_GetVerticalBlankStatus(&ddrawex->IDirectDraw4_iface, status);
986 987
}

988
static HRESULT WINAPI ddrawex2_GetVerticalBlankStatus(IDirectDraw2 *iface, BOOL *status)
989
{
990 991 992 993 994
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, status %p.\n", iface, status);

    return ddrawex4_GetVerticalBlankStatus(&ddrawex->IDirectDraw4_iface, status);
995 996
}

997
static HRESULT WINAPI ddrawex1_GetVerticalBlankStatus(IDirectDraw *iface, BOOL *status)
998
{
999 1000 1001 1002 1003
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, status %p.\n", iface, status);

    return ddrawex4_GetVerticalBlankStatus(&ddrawex->IDirectDraw4_iface, status);
1004 1005
}

1006
static HRESULT WINAPI ddrawex4_Initialize(IDirectDraw4 *iface, GUID *guid)
1007
{
1008 1009 1010 1011 1012
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));

    return IDirectDraw4_Initialize(ddrawex->parent, guid);
1013 1014
}

1015
static HRESULT WINAPI ddrawex3_Initialize(IDirectDraw3 *iface, GUID *guid)
1016
{
1017 1018 1019 1020 1021
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));

    return ddrawex4_Initialize(&ddrawex->IDirectDraw4_iface, guid);
1022 1023
}

1024
static HRESULT WINAPI ddrawex2_Initialize(IDirectDraw2 *iface, GUID *guid)
1025
{
1026 1027 1028 1029 1030
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));

    return ddrawex4_Initialize(&ddrawex->IDirectDraw4_iface, guid);
1031 1032
}

1033
static HRESULT WINAPI ddrawex1_Initialize(IDirectDraw *iface, GUID *guid)
1034
{
1035 1036 1037 1038 1039
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));

    return ddrawex4_Initialize(&ddrawex->IDirectDraw4_iface, guid);
1040 1041
}

1042
static HRESULT WINAPI ddrawex4_RestoreDisplayMode(IDirectDraw4 *iface)
1043
{
1044 1045 1046 1047 1048
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p.\n", iface);

    return IDirectDraw4_RestoreDisplayMode(ddrawex->parent);
1049 1050
}

1051
static HRESULT WINAPI ddrawex3_RestoreDisplayMode(IDirectDraw3 *iface)
1052
{
1053 1054 1055 1056 1057
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_RestoreDisplayMode(&ddrawex->IDirectDraw4_iface);
1058 1059
}

1060
static HRESULT WINAPI ddrawex2_RestoreDisplayMode(IDirectDraw2 *iface)
1061
{
1062 1063 1064 1065 1066
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_RestoreDisplayMode(&ddrawex->IDirectDraw4_iface);
1067 1068
}

1069
static HRESULT WINAPI ddrawex1_RestoreDisplayMode(IDirectDraw *iface)
1070
{
1071 1072 1073 1074 1075
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p.\n", iface);

    return ddrawex4_RestoreDisplayMode(&ddrawex->IDirectDraw4_iface);
1076 1077
}

1078
static HRESULT WINAPI ddrawex4_SetCooperativeLevel(IDirectDraw4 *iface, HWND window, DWORD flags)
1079
{
1080 1081 1082 1083 1084
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);

    return IDirectDraw4_SetCooperativeLevel(ddrawex->parent, window, flags);
1085 1086
}

1087
static HRESULT WINAPI ddrawex3_SetCooperativeLevel(IDirectDraw3 *iface, HWND window, DWORD flags)
1088
{
1089 1090 1091 1092 1093
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);

    return ddrawex4_SetCooperativeLevel(&ddrawex->IDirectDraw4_iface, window, flags);
1094 1095
}

1096
static HRESULT WINAPI ddrawex2_SetCooperativeLevel(IDirectDraw2 *iface, HWND window, DWORD flags)
1097
{
1098 1099 1100 1101 1102
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);

    return ddrawex4_SetCooperativeLevel(&ddrawex->IDirectDraw4_iface, window, flags);
1103 1104
}

1105
static HRESULT WINAPI ddrawex1_SetCooperativeLevel(IDirectDraw *iface, HWND window, DWORD flags)
1106
{
1107 1108 1109 1110 1111
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, window %p, flags %#x.\n", iface, window, flags);

    return ddrawex4_SetCooperativeLevel(&ddrawex->IDirectDraw4_iface, window, flags);
1112 1113
}

1114 1115
static HRESULT WINAPI ddrawex4_SetDisplayMode(IDirectDraw4 *iface, DWORD width,
        DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1116
{
1117 1118 1119 1120 1121 1122
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
            iface, width, height, bpp, refresh_rate, flags);

    return IDirectDraw4_SetDisplayMode(ddrawex->parent, width, height, bpp, refresh_rate, flags);
1123 1124
}

1125 1126
static HRESULT WINAPI ddrawex3_SetDisplayMode(IDirectDraw3 *iface, DWORD width,
        DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1127
{
1128 1129 1130 1131 1132 1133
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
            iface, width, height, bpp, refresh_rate, flags);

    return ddrawex4_SetDisplayMode(&ddrawex->IDirectDraw4_iface, width, height, bpp, refresh_rate, flags);
1134 1135
}

1136 1137
static HRESULT WINAPI ddrawex2_SetDisplayMode(IDirectDraw2 *iface, DWORD width,
        DWORD height, DWORD bpp, DWORD refresh_rate, DWORD flags)
1138
{
1139 1140 1141 1142 1143 1144
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, width %u, height %u, bpp %u, refresh_rate %u, flags %#x.\n",
            iface, width, height, bpp, refresh_rate, flags);

    return ddrawex4_SetDisplayMode(&ddrawex->IDirectDraw4_iface, width, height, bpp, refresh_rate, flags);
1145 1146
}

1147 1148
static HRESULT WINAPI ddrawex1_SetDisplayMode(IDirectDraw *iface, DWORD width,
        DWORD height, DWORD bpp)
1149
{
1150 1151 1152 1153 1154
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, width %u, height %u, bpp %u.\n", iface, width, height, bpp);

    return ddrawex4_SetDisplayMode(&ddrawex->IDirectDraw4_iface, width, height, bpp, 0, 0);
1155 1156
}

1157
static HRESULT WINAPI ddrawex4_WaitForVerticalBlank(IDirectDraw4 *iface, DWORD flags, HANDLE event)
1158
{
1159 1160 1161 1162 1163
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);

    return IDirectDraw4_WaitForVerticalBlank(ddrawex->parent, flags, event);
1164 1165
}

1166
static HRESULT WINAPI ddrawex3_WaitForVerticalBlank(IDirectDraw3 *iface, DWORD flags, HANDLE event)
1167
{
1168 1169 1170 1171 1172
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);

    TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);

    return ddrawex4_WaitForVerticalBlank(&ddrawex->IDirectDraw4_iface, flags, event);
1173 1174
}

1175
static HRESULT WINAPI ddrawex2_WaitForVerticalBlank(IDirectDraw2 *iface, DWORD flags, HANDLE event)
1176
{
1177 1178 1179 1180 1181
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);

    TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);

    return ddrawex4_WaitForVerticalBlank(&ddrawex->IDirectDraw4_iface, flags, event);
1182 1183
}

1184
static HRESULT WINAPI ddrawex1_WaitForVerticalBlank(IDirectDraw *iface, DWORD flags, HANDLE event)
1185
{
1186 1187 1188 1189 1190
    struct ddrawex *ddrawex = impl_from_IDirectDraw(iface);

    TRACE("iface %p, flags %#x, event %p.\n", iface, flags, event);

    return ddrawex4_WaitForVerticalBlank(&ddrawex->IDirectDraw4_iface, flags, event);
1191 1192
}

1193 1194
static HRESULT WINAPI ddrawex4_GetAvailableVidMem(IDirectDraw4 *iface,
        DDSCAPS2 *caps, DWORD *total, DWORD *free)
1195
{
1196 1197 1198 1199 1200
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);

    return IDirectDraw4_GetAvailableVidMem(ddrawex->parent, caps, total, free);
1201 1202
}

1203 1204
static HRESULT WINAPI ddrawex3_GetAvailableVidMem(IDirectDraw3 *iface,
        DDSCAPS *caps, DWORD *total, DWORD *free)
1205
{
1206
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);
1207
    DDSCAPS2 caps2;
1208 1209 1210

    TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);

1211
    memset(&caps2, 0, sizeof(caps2));
1212 1213
    caps2.dwCaps = caps->dwCaps;
    return ddrawex4_GetAvailableVidMem(&ddrawex->IDirectDraw4_iface, &caps2, total, free);
1214 1215
}

1216 1217
static HRESULT WINAPI ddrawex2_GetAvailableVidMem(IDirectDraw2 *iface,
        DDSCAPS *caps, DWORD *total, DWORD *free)
1218
{
1219
    struct ddrawex *ddrawex = impl_from_IDirectDraw2(iface);
1220
    DDSCAPS2 caps2;
1221 1222 1223

    TRACE("iface %p, caps %p, total %p, free %p.\n", iface, caps, total, free);

1224
    memset(&caps2, 0, sizeof(caps2));
1225 1226
    caps2.dwCaps = caps->dwCaps;
    return ddrawex4_GetAvailableVidMem(&ddrawex->IDirectDraw4_iface, &caps2, total, free);
1227 1228
}

1229 1230
static HRESULT WINAPI ddrawex4_GetSurfaceFromDC(IDirectDraw4 *iface,
        HDC dc, IDirectDrawSurface4 **surface)
1231
{
1232
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);
1233

1234 1235 1236
    TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);

    return IDirectDraw4_GetSurfaceFromDC(ddrawex->parent, dc, surface);
1237 1238
}

1239 1240
static HRESULT WINAPI ddrawex3_GetSurfaceFromDC(IDirectDraw3 *iface,
        HDC dc, IDirectDrawSurface **surface)
1241
{
1242
    struct ddrawex *ddrawex = impl_from_IDirectDraw3(iface);
1243 1244
    IDirectDrawSurface4 *surf4, *outer;
    IDirectDrawSurface *inner;
1245 1246
    HRESULT hr;

1247
    TRACE("iface %p, dc %p, surface %p.\n", iface, dc, surface);
1248

1249 1250 1251 1252
    if (!surface)
        return E_POINTER;

    if (FAILED(hr = IDirectDraw4_GetSurfaceFromDC(ddrawex->parent, dc, (IDirectDrawSurface4 **)&inner)))
1253
    {
1254
        *surface = NULL;
1255 1256
        return hr;
    }
1257 1258 1259 1260 1261

    hr = IDirectDrawSurface_QueryInterface(inner, &IID_IDirectDrawSurface4, (void **)&surf4);
    IDirectDrawSurface_Release(inner);
    if (FAILED(hr))
    {
1262
        *surface = NULL;
1263 1264 1265 1266
        return hr;
    }

    outer = dds_get_outer(surf4);
1267
    hr = IDirectDrawSurface4_QueryInterface(outer, &IID_IDirectDrawSurface, (void **)surface);
1268 1269 1270 1271
    IDirectDrawSurface4_Release(surf4);
    return hr;
}

1272
static HRESULT WINAPI ddrawex4_RestoreAllSurfaces(IDirectDraw4 *iface)
1273
{
1274 1275 1276 1277 1278
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p.\n", iface);

    return IDirectDraw4_RestoreAllSurfaces(ddrawex->parent);
1279 1280
}

1281
static HRESULT WINAPI ddrawex4_TestCooperativeLevel(IDirectDraw4 *iface)
1282
{
1283 1284 1285 1286 1287
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p.\n", iface);

    return IDirectDraw4_TestCooperativeLevel(ddrawex->parent);
1288 1289
}

1290 1291
static HRESULT WINAPI ddrawex4_GetDeviceIdentifier(IDirectDraw4 *iface,
        DDDEVICEIDENTIFIER *identifier, DWORD flags)
1292
{
1293 1294 1295 1296 1297
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(iface);

    TRACE("iface %p, identifier %p, flags %#x.\n", iface, identifier, flags);

    return IDirectDraw4_GetDeviceIdentifier(ddrawex->parent, identifier, flags);
1298 1299
}

1300
static const IDirectDraw4Vtbl ddrawex4_vtbl =
1301
{
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
    ddrawex4_QueryInterface,
    ddrawex4_AddRef,
    ddrawex4_Release,
    ddrawex4_Compact,
    ddrawex4_CreateClipper,
    ddrawex4_CreatePalette,
    ddrawex4_CreateSurface,
    ddrawex4_DuplicateSurface,
    ddrawex4_EnumDisplayModes,
    ddrawex4_EnumSurfaces,
    ddrawex4_FlipToGDISurface,
    ddrawex4_GetCaps,
    ddrawex4_GetDisplayMode,
    ddrawex4_GetFourCCCodes,
    ddrawex4_GetGDISurface,
    ddrawex4_GetMonitorFrequency,
    ddrawex4_GetScanLine,
    ddrawex4_GetVerticalBlankStatus,
    ddrawex4_Initialize,
    ddrawex4_RestoreDisplayMode,
    ddrawex4_SetCooperativeLevel,
    ddrawex4_SetDisplayMode,
    ddrawex4_WaitForVerticalBlank,
    ddrawex4_GetAvailableVidMem,
    ddrawex4_GetSurfaceFromDC,
    ddrawex4_RestoreAllSurfaces,
    ddrawex4_TestCooperativeLevel,
    ddrawex4_GetDeviceIdentifier,
1330 1331
};

1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
static const IDirectDraw3Vtbl ddrawex3_vtbl =
{
    ddrawex3_QueryInterface,
    ddrawex3_AddRef,
    ddrawex3_Release,
    ddrawex3_Compact,
    ddrawex3_CreateClipper,
    ddrawex3_CreatePalette,
    ddrawex3_CreateSurface,
    ddrawex3_DuplicateSurface,
    ddrawex3_EnumDisplayModes,
    ddrawex3_EnumSurfaces,
    ddrawex3_FlipToGDISurface,
    ddrawex3_GetCaps,
    ddrawex3_GetDisplayMode,
    ddrawex3_GetFourCCCodes,
    ddrawex3_GetGDISurface,
    ddrawex3_GetMonitorFrequency,
    ddrawex3_GetScanLine,
    ddrawex3_GetVerticalBlankStatus,
    ddrawex3_Initialize,
    ddrawex3_RestoreDisplayMode,
    ddrawex3_SetCooperativeLevel,
    ddrawex3_SetDisplayMode,
    ddrawex3_WaitForVerticalBlank,
    ddrawex3_GetAvailableVidMem,
    ddrawex3_GetSurfaceFromDC,
1359 1360
};

1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
static const IDirectDraw2Vtbl ddrawex2_vtbl =
{
    ddrawex2_QueryInterface,
    ddrawex2_AddRef,
    ddrawex2_Release,
    ddrawex2_Compact,
    ddrawex2_CreateClipper,
    ddrawex2_CreatePalette,
    ddrawex2_CreateSurface,
    ddrawex2_DuplicateSurface,
    ddrawex2_EnumDisplayModes,
    ddrawex2_EnumSurfaces,
    ddrawex2_FlipToGDISurface,
    ddrawex2_GetCaps,
    ddrawex2_GetDisplayMode,
    ddrawex2_GetFourCCCodes,
    ddrawex2_GetGDISurface,
    ddrawex2_GetMonitorFrequency,
    ddrawex2_GetScanLine,
    ddrawex2_GetVerticalBlankStatus,
    ddrawex2_Initialize,
    ddrawex2_RestoreDisplayMode,
    ddrawex2_SetCooperativeLevel,
    ddrawex2_SetDisplayMode,
    ddrawex2_WaitForVerticalBlank,
    ddrawex2_GetAvailableVidMem,
1387 1388
};

1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
static const IDirectDrawVtbl ddrawex1_vtbl =
{
    ddrawex1_QueryInterface,
    ddrawex1_AddRef,
    ddrawex1_Release,
    ddrawex1_Compact,
    ddrawex1_CreateClipper,
    ddrawex1_CreatePalette,
    ddrawex1_CreateSurface,
    ddrawex1_DuplicateSurface,
    ddrawex1_EnumDisplayModes,
    ddrawex1_EnumSurfaces,
    ddrawex1_FlipToGDISurface,
    ddrawex1_GetCaps,
    ddrawex1_GetDisplayMode,
    ddrawex1_GetFourCCCodes,
    ddrawex1_GetGDISurface,
    ddrawex1_GetMonitorFrequency,
    ddrawex1_GetScanLine,
    ddrawex1_GetVerticalBlankStatus,
    ddrawex1_Initialize,
    ddrawex1_RestoreDisplayMode,
    ddrawex1_SetCooperativeLevel,
    ddrawex1_SetDisplayMode,
    ddrawex1_WaitForVerticalBlank,
1414 1415
};

1416 1417
HRESULT WINAPI ddrawex_factory_CreateDirectDraw(IDirectDrawFactory *iface, GUID *guid, HWND window,
        DWORD coop_level, DWORD reserved, IUnknown *outer_unknown, IDirectDraw **ddraw)
1418 1419
{
    IDirectDraw *parent = NULL;
1420 1421
    struct ddrawex *object;
    HRESULT hr;
1422

1423 1424
    TRACE("iface %p, guid %s, window %p, coop_level %#x, reserved %#x, outer_unknown %p, ddraw %p.\n",
            iface, debugstr_guid(guid), window, coop_level, reserved, outer_unknown, ddraw);
1425

1426 1427
    if (outer_unknown)
        FIXME("Implement aggregation in ddrawex's IDirectDraw interface.\n");
1428

1429
    if (!(object = heap_alloc_zero(sizeof(*object))))
1430
        return E_OUTOFMEMORY;
1431

1432 1433 1434 1435 1436
    object->ref = 1;
    object->IDirectDraw_iface.lpVtbl = &ddrawex1_vtbl;
    object->IDirectDraw2_iface.lpVtbl = &ddrawex2_vtbl;
    object->IDirectDraw3_iface.lpVtbl = &ddrawex3_vtbl;
    object->IDirectDraw4_iface.lpVtbl = &ddrawex4_vtbl;
1437

1438 1439 1440 1441 1442 1443
    if (FAILED(hr = DirectDrawCreate(guid, &parent, NULL)))
        goto err;
    if (FAILED(hr = IDirectDraw_QueryInterface(parent, &IID_IDirectDraw4, (void **)&object->parent)))
        goto err;
    if (FAILED(hr = IDirectDraw_SetCooperativeLevel(&object->IDirectDraw_iface, window, coop_level)))
        goto err;
1444

1445
    *ddraw = &object->IDirectDraw_iface;
1446 1447 1448 1449
    IDirectDraw_Release(parent);
    return DD_OK;

err:
1450 1451 1452 1453 1454
    if (object && object->parent)
        IDirectDraw4_Release(object->parent);
    if (parent)
        IDirectDraw_Release(parent);
    heap_free(object);
1455
    *ddraw = NULL;
1456 1457
    return hr;
}
1458 1459 1460

IDirectDraw4 *dd_get_inner(IDirectDraw4 *outer)
{
1461
    struct ddrawex *ddrawex = impl_from_IDirectDraw4(outer);
1462

1463 1464 1465
    if (outer->lpVtbl != &ddrawex4_vtbl)
        return NULL;
    return ddrawex->parent;
1466
}