1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
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
/*
 * COPYRIGHT:            See COPYING in the top level directory
 * PROJECT:              ReactOS kernel
 * FILE:                 dll/directx/ddraw/startup.c
 * PURPOSE:              DirectDraw Library
 * PROGRAMMER:           Magnus Olsen (greatlrd)
 *
 */

#include "rosdraw.h"

DDRAWI_DIRECTDRAW_GBL ddgbl;
DDRAWI_DDRAWSURFACE_GBL ddSurfGbl;

WCHAR classname[128];
WNDCLASSW wnd_class;


HRESULT WINAPI
Create_DirectDraw (LPGUID pGUID, LPDIRECTDRAW* pIface,
                   REFIID id, BOOL reenable)
{
    LPDDRAWI_DIRECTDRAW_INT This;

    DX_WINDBG_trace();
    BOOL linking = FALSE;

    if (pIface == NULL)
    {
        return DDERR_INVALIDPARAMS;
    }

    This = (LPDDRAWI_DIRECTDRAW_INT)*pIface;

    DX_STUB_str("Linking?\n")

    _SEH2_TRY
    {
        linking = This->lpLcl ? TRUE:FALSE;
    }
    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
    {
        linking = FALSE;
    }
    _SEH2_END;

    /* fixme linking too second link when we shall not doing it */
    if (!linking)
    {
        /* We do not have a DirectDraw interface, we need alloc it*/
        LPDDRAWI_DIRECTDRAW_INT memThis;

        DX_STUB_str("1. no linking\n");

        DxHeapMemAlloc(memThis, sizeof(DDRAWI_DIRECTDRAW_INT));
        if (memThis == NULL)
        {
            return DDERR_OUTOFMEMORY;
        }

        This = memThis;

        /* Fixme release memory alloc if we fail */

        DxHeapMemAlloc(This->lpLcl, sizeof(DDRAWI_DIRECTDRAW_LCL));
        if (This->lpLcl == NULL)
        {
            return DDERR_OUTOFMEMORY;
        }
    }
    else
    {
        /* We got the DirectDraw interface alloc and we need create the link */
        LPDDRAWI_DIRECTDRAW_INT  newThis;

        DX_STUB_str("2.linking\n");

        /* step 1.Alloc the new  DDRAWI_DIRECTDRAW_INT for the link */
        DxHeapMemAlloc(newThis, sizeof(DDRAWI_DIRECTDRAW_INT));
        if (newThis == NULL)
        {
            return DDERR_OUTOFMEMORY;
        }

        /* step 2 check if it not DDCREATE_HARDWAREONLY we got if so we fail */
        if ((pGUID) && (pGUID != (LPGUID)DDCREATE_HARDWAREONLY))<--- Assuming that condition 'pGUID' is not redundant
        {
            if (pGUID !=NULL)<--- Condition 'pGUID!=NULL' is always true
            {
                This = newThis;
                return DDERR_INVALIDDIRECTDRAWGUID;
            }
        }

        /* step 3 do the link the old interface are store in the new one */
        newThis->lpLink = This;

        /* step 4 we need create new local directdraw struct for the new linked interface */
        DxHeapMemAlloc(newThis->lpLcl, sizeof(DDRAWI_DIRECTDRAW_LCL));
        if (newThis->lpLcl == NULL)
        {
            This = newThis;
            return DDERR_OUTOFMEMORY;
        }

        This = newThis;
    }

    This->lpLcl->lpGbl = &ddgbl;

    *pIface = (LPDIRECTDRAW)This;

    /* Get right interface we whant */

    This->lpVtbl = 0;
    if (IsEqualGUID(&IID_IDirectDraw7, id))
    {
        /* DirectDraw7 Vtable */
        This->lpVtbl = &DirectDraw7_Vtable;
        This->lpLcl->dwLocalFlags = This->lpLcl->dwLocalFlags + DDRAWILCL_DIRECTDRAW7;
        *pIface = (LPDIRECTDRAW)&This->lpVtbl;
        Main_DirectDraw_AddRef(This);
    }
    else if (IsEqualGUID(&IID_IDirectDraw4, id))
    {
        /* DirectDraw4 Vtable */
        This->lpVtbl = &DirectDraw4_Vtable;
        *pIface = (LPDIRECTDRAW)&This->lpVtbl;
        Main_DirectDraw_AddRef(This);
    }
    else if (IsEqualGUID(&IID_IDirectDraw2, id))
    {
        /* DirectDraw2 Vtable */
        This->lpVtbl = &DirectDraw2_Vtable;
        *pIface = (LPDIRECTDRAW)&This->lpVtbl;
        Main_DirectDraw_AddRef(This);
    }
    else if (IsEqualGUID(&IID_IDirectDraw, id))
    {
        /* DirectDraw Vtable */
        This->lpVtbl = &DirectDraw_Vtable;
        *pIface = (LPDIRECTDRAW)&This->lpVtbl;
        Main_DirectDraw_AddRef(This);
    }

    if ( This->lpVtbl != 0)
    {
        DX_STUB_str("Got iface\n");

        if (StartDirectDraw((LPDIRECTDRAW)This, pGUID, FALSE) == DD_OK)
        {
            /*
            RtlZeroMemory(&wnd_class, sizeof(wnd_class));
            wnd_class.style = CS_HREDRAW | CS_VREDRAW;
            wnd_class.lpfnWndProc = DefWindowProcW;
            wnd_class.cbClsExtra = 0;
            wnd_class.cbWndExtra = 0;
            wnd_class.hInstance = GetModuleHandleW(0);
            wnd_class.hIcon = 0;
            wnd_class.hCursor = 0;
            wnd_class.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
            wnd_class.lpszMenuName = NULL;
            wnd_class.lpszClassName = classname;
            if(!RegisterClassW(&wnd_class))
            {
                DX_STUB_str("DDERR_GENERIC");
                return DDERR_GENERIC;
            }
            */
            This->lpLcl->hDD = ddgbl.hDD;
            return DD_OK;
        }
    }

    return DDERR_INVALIDPARAMS;
}


HRESULT WINAPI
StartDirectDraw(LPDIRECTDRAW iface, LPGUID lpGuid, BOOL reenable)
{
    LPDDRAWI_DIRECTDRAW_INT This = (LPDDRAWI_DIRECTDRAW_INT)iface;
    DWORD hal_ret = DD_FALSE;
    DWORD hel_ret = DD_FALSE;
    DWORD devicetypes = 0;
    DWORD dwFlags = 0;


    DX_WINDBG_trace();


    /*
     * ddgbl.dwPDevice  is not longer in use in windows 2000 and higher
     * I am using it for device type
     * devicetypes = 1 : both hal and hel are enable
     * devicetypes = 2 : both hal are enable
     * devicetypes = 3 : both hel are enable
     * devicetypes = 4 :loading a guid drv from the register
     */

    ddgbl.lpDriverHandle = &ddgbl;
    ddgbl.hDDVxd = -1;

    if (reenable == FALSE)<--- First condition
    {
        if ((!IsBadReadPtr(This->lpLink,sizeof(LPDIRECTDRAW))) && (This->lpLink == NULL))
        {
            RtlZeroMemory(&ddgbl, sizeof(DDRAWI_DIRECTDRAW_GBL));
            This->lpLcl->lpGbl->dwRefCnt++;
            if (ddgbl.lpDDCBtmp == NULL)
            {
                // LPDDHAL_CALLBACKS
                DxHeapMemAlloc( ddgbl.lpDDCBtmp , sizeof(DDHAL_CALLBACKS));
                if (ddgbl.lpDDCBtmp == NULL)
                {
                    DX_STUB_str("Out of memory\n");
                    return DD_FALSE;
                }
            }
        }

        DxHeapMemAlloc(ddgbl.lpModeInfo, sizeof(DDHALMODEINFO));
        if (!ddgbl.lpModeInfo)
        {
            return DDERR_OUTOFMEMORY;
        }

    }
    /* Windows handler are by set of SetCooperLevel
     * so do not set it
     */

    if (reenable == FALSE)<--- Second condition
    {
        if (lpGuid == NULL)
        {
            devicetypes= 1;

            /* Create HDC for default, hal and hel driver */
            // This->lpLcl->hWnd = (ULONG_PTR) GetActiveWindow();
            This->lpLcl->hDC = (ULONG_PTR)CreateDCA("DISPLAY",NULL,NULL,NULL);

            /* cObsolete is undoc in msdn it being use in CreateDCA */
            RtlCopyMemory(&ddgbl.cObsolete,&"DISPLAY",7);
            RtlCopyMemory(&ddgbl.cDriverName,&"DISPLAY",7);
            dwFlags |= DDRAWI_DISPLAYDRV | DDRAWI_GDIDRV;
        }
        else if (lpGuid == (LPGUID) DDCREATE_HARDWAREONLY)
        {
            devicetypes = 2;
            /* Create HDC for default, hal driver */
            // This->lpLcl->hWnd =(ULONG_PTR) GetActiveWindow();
            This->lpLcl->hDC = (ULONG_PTR)CreateDCA("DISPLAY",NULL,NULL,NULL);

            /* cObsolete is undoc in msdn it being use in CreateDCA */
            RtlCopyMemory(&ddgbl.cObsolete,&"DISPLAY",7);
            RtlCopyMemory(&ddgbl.cDriverName,&"DISPLAY",7);
            dwFlags |= DDRAWI_DISPLAYDRV | DDRAWI_GDIDRV;
        }
        else if (lpGuid == (LPGUID) DDCREATE_EMULATIONONLY)
        {
            devicetypes = 3;

            /* Create HDC for default, hal and hel driver */
            //This->lpLcl->hWnd = (ULONG_PTR) GetActiveWindow();
            This->lpLcl->hDC = (ULONG_PTR)CreateDCA("DISPLAY",NULL,NULL,NULL);

            /* cObsolete is undoc in msdn it being use in CreateDCA */
            RtlCopyMemory(&ddgbl.cObsolete,&"DISPLAY",7);
            RtlCopyMemory(&ddgbl.cDriverName,&"DISPLAY",7);

            dwFlags |= DDRAWI_DISPLAYDRV | DDRAWI_GDIDRV;
        }
        else
        {
            /* FIXME : need getting driver from the GUID that have been pass in from
             * the register. we do not support that yet
             */
             devicetypes = 4;
             //This->lpLcl->hDC = (ULONG_PTR) NULL ;
             //This->lpLcl->hDC = (ULONG_PTR)CreateDCA("DISPLAY",NULL,NULL,NULL);
        }

        /*
        if ( (HDC)This->lpLcl->hDC == NULL)
        {
            DX_STUB_str("DDERR_OUTOFMEMORY\n");
            return DDERR_OUTOFMEMORY ;
        }
        */
    }

    This->lpLcl->lpDDCB = ddgbl.lpDDCBtmp;<--- This->lpLcl->lpDDCB is assigned

    /* Startup HEL and HAL */
    This->lpLcl->lpDDCB = This->lpLcl->lpGbl->lpDDCBtmp;<--- This->lpLcl->lpDDCB is overwritten
    This->lpLcl->dwProcessId = GetCurrentProcessId();
    switch (devicetypes)
    {
            case 2:
              hal_ret = StartDirectDrawHal(iface, reenable);
              This->lpLcl->lpDDCB->HELDD.dwFlags = 0;
              break;

            case 3:
              hel_ret = StartDirectDrawHel(iface, reenable);
              This->lpLcl->lpDDCB->HALDD.dwFlags = 0;
              break;

            default:
              hal_ret = StartDirectDrawHal(iface, reenable);
              hel_ret = StartDirectDrawHel(iface, reenable);
              break;
    }

    if (hal_ret!=DD_OK)
    {
        if (hel_ret!=DD_OK)
        {
            DX_STUB_str("DDERR_NODIRECTDRAWSUPPORT\n");
            return DDERR_NODIRECTDRAWSUPPORT;
        }
        dwFlags |= DDRAWI_NOHARDWARE;
        DX_STUB_str("No hardware support\n");
    }

    if (hel_ret!=DD_OK)
    {
        dwFlags |= DDRAWI_NOEMULATION;

    }
    else
    {
        dwFlags |= DDRAWI_EMULATIONINITIALIZED;
    }

    /* Fill some basic info for Surface */
    This->lpLcl->lpGbl->dwFlags =  This->lpLcl->lpGbl->dwFlags | dwFlags | DDRAWI_ATTACHEDTODESKTOP;
    This->lpLcl->lpDDCB = This->lpLcl->lpGbl->lpDDCBtmp;
    This->lpLcl->hDD = ddgbl.hDD;

    ddgbl.rectDevice.top = 0;
    ddgbl.rectDevice.left = 0;
    ddgbl.rectDevice.right = ddgbl.vmiData.dwDisplayWidth;
    ddgbl.rectDevice.bottom = ddgbl.vmiData.dwDisplayHeight;

    ddgbl.rectDesktop.top = 0;
    ddgbl.rectDesktop.left = 0;
    ddgbl.rectDesktop.right = ddgbl.vmiData.dwDisplayWidth;
    ddgbl.rectDesktop.bottom = ddgbl.vmiData.dwDisplayHeight;

    ddgbl.dwMonitorFrequency = GetDeviceCaps(GetWindowDC(NULL),VREFRESH);
    ddgbl.lpModeInfo->dwWidth      = ddgbl.vmiData.dwDisplayWidth;
    ddgbl.lpModeInfo->dwHeight     = ddgbl.vmiData.dwDisplayHeight;
    ddgbl.lpModeInfo->dwBPP        = ddgbl.vmiData.ddpfDisplay.dwRGBBitCount;
    ddgbl.lpModeInfo->lPitch       = ddgbl.vmiData.lDisplayPitch;
    ddgbl.lpModeInfo->wRefreshRate = ddgbl.dwMonitorFrequency;
    ddgbl.lpModeInfo->dwRBitMask = ddgbl.vmiData.ddpfDisplay.dwRBitMask;
    ddgbl.lpModeInfo->dwGBitMask = ddgbl.vmiData.ddpfDisplay.dwGBitMask;
    ddgbl.lpModeInfo->dwBBitMask = ddgbl.vmiData.ddpfDisplay.dwBBitMask;
    ddgbl.lpModeInfo->dwAlphaBitMask = ddgbl.vmiData.ddpfDisplay.dwRGBAlphaBitMask;

    return DD_OK;
}


HRESULT WINAPI
StartDirectDrawHel(LPDIRECTDRAW iface, BOOL reenable)
{
    LPDDRAWI_DIRECTDRAW_INT This = (LPDDRAWI_DIRECTDRAW_INT)iface;

    if (reenable == FALSE)
    {
        if (ddgbl.lpDDCBtmp == NULL)
        {
            DxHeapMemAlloc(ddgbl.lpDDCBtmp, sizeof(DDHAL_CALLBACKS));
            if ( ddgbl.lpDDCBtmp == NULL)
            {
                return DD_FALSE;
            }
        }
    }

    ddgbl.lpDDCBtmp->HELDD.CanCreateSurface     = HelDdCanCreateSurface;
    ddgbl.lpDDCBtmp->HELDD.CreateSurface        = HelDdCreateSurface;
    ddgbl.lpDDCBtmp->HELDD.CreatePalette        = HelDdCreatePalette;
    ddgbl.lpDDCBtmp->HELDD.DestroyDriver        = HelDdDestroyDriver;
    ddgbl.lpDDCBtmp->HELDD.FlipToGDISurface     = HelDdFlipToGDISurface;
    ddgbl.lpDDCBtmp->HELDD.GetScanLine          = HelDdGetScanLine;
    ddgbl.lpDDCBtmp->HELDD.SetColorKey          = HelDdSetColorKey;
    ddgbl.lpDDCBtmp->HELDD.SetExclusiveMode     = HelDdSetExclusiveMode;
    ddgbl.lpDDCBtmp->HELDD.SetMode              = HelDdSetMode;
    ddgbl.lpDDCBtmp->HELDD.WaitForVerticalBlank = HelDdWaitForVerticalBlank;

    ddgbl.lpDDCBtmp->HELDD.dwFlags =  DDHAL_CB32_CANCREATESURFACE     |
                                          DDHAL_CB32_CREATESURFACE        |
                                          DDHAL_CB32_CREATEPALETTE        |
                                          DDHAL_CB32_DESTROYDRIVER        |
                                          DDHAL_CB32_FLIPTOGDISURFACE     |
                                          DDHAL_CB32_GETSCANLINE          |
                                          DDHAL_CB32_SETCOLORKEY          |
                                          DDHAL_CB32_SETEXCLUSIVEMODE     |
                                          DDHAL_CB32_SETMODE              |
                                          DDHAL_CB32_WAITFORVERTICALBLANK ;

    ddgbl.lpDDCBtmp->HELDD.dwSize = sizeof(This->lpLcl->lpDDCB->HELDD);

    ddgbl.lpDDCBtmp->HELDDSurface.AddAttachedSurface = HelDdSurfAddAttachedSurface;
    ddgbl.lpDDCBtmp->HELDDSurface.Blt = HelDdSurfBlt;
    ddgbl.lpDDCBtmp->HELDDSurface.DestroySurface = HelDdSurfDestroySurface;
    ddgbl.lpDDCBtmp->HELDDSurface.Flip = HelDdSurfFlip;
    ddgbl.lpDDCBtmp->HELDDSurface.GetBltStatus = HelDdSurfGetBltStatus;
    ddgbl.lpDDCBtmp->HELDDSurface.GetFlipStatus = HelDdSurfGetFlipStatus;
    ddgbl.lpDDCBtmp->HELDDSurface.Lock = HelDdSurfLock;
    ddgbl.lpDDCBtmp->HELDDSurface.reserved4 = HelDdSurfreserved4;
    ddgbl.lpDDCBtmp->HELDDSurface.SetClipList = HelDdSurfSetClipList;
    ddgbl.lpDDCBtmp->HELDDSurface.SetColorKey = HelDdSurfSetColorKey;
    ddgbl.lpDDCBtmp->HELDDSurface.SetOverlayPosition = HelDdSurfSetOverlayPosition;
    ddgbl.lpDDCBtmp->HELDDSurface.SetPalette = HelDdSurfSetPalette;
    ddgbl.lpDDCBtmp->HELDDSurface.Unlock = HelDdSurfUnlock;
    ddgbl.lpDDCBtmp->HELDDSurface.UpdateOverlay = HelDdSurfUpdateOverlay;
    ddgbl.lpDDCBtmp->HELDDSurface.dwFlags = DDHAL_SURFCB32_ADDATTACHEDSURFACE |
                                                DDHAL_SURFCB32_BLT                |
                                                DDHAL_SURFCB32_DESTROYSURFACE     |
                                                DDHAL_SURFCB32_FLIP               |
                                                DDHAL_SURFCB32_GETBLTSTATUS       |
                                                DDHAL_SURFCB32_GETFLIPSTATUS      |
                                                DDHAL_SURFCB32_LOCK               |
                                                DDHAL_SURFCB32_RESERVED4          |
                                                DDHAL_SURFCB32_SETCLIPLIST        |
                                                DDHAL_SURFCB32_SETCOLORKEY        |
                                                DDHAL_SURFCB32_SETOVERLAYPOSITION |
                                                DDHAL_SURFCB32_SETPALETTE         |
                                                DDHAL_SURFCB32_UNLOCK             |
                                                DDHAL_SURFCB32_UPDATEOVERLAY;

    ddgbl.lpDDCBtmp->HELDDSurface.dwSize = sizeof(This->lpLcl->lpDDCB->HELDDSurface);

    /*
    This->lpLcl->lpDDCB->HELDDPalette.DestroyPalette  = HelDdPalDestroyPalette;
    This->lpLcl->lpDDCB->HELDDPalette.SetEntries = HelDdPalSetEntries;
    This->lpLcl->lpDDCB->HELDDPalette.dwSize = sizeof(This->lpLcl->lpDDCB->HELDDPalette);
    */

    /*
    This->lpLcl->lpDDCB->HELDDExeBuf.CanCreateExecuteBuffer = HelDdExeCanCreateExecuteBuffer;
    This->lpLcl->lpDDCB->HELDDExeBuf.CreateExecuteBuffer = HelDdExeCreateExecuteBuffer;
    This->lpLcl->lpDDCB->HELDDExeBuf.DestroyExecuteBuffer = HelDdExeDestroyExecuteBuffer;
    This->lpLcl->lpDDCB->HELDDExeBuf.LockExecuteBuffer = HelDdExeLockExecuteBuffer;
    This->lpLcl->lpDDCB->HELDDExeBuf.UnlockExecuteBuffer = HelDdExeUnlockExecuteBuffer;
    */

    return DD_OK;
}


HRESULT WINAPI
StartDirectDrawHal(LPDIRECTDRAW iface, BOOL reenable)
{
    LPDWORD mpFourCC = NULL;
    DDHALINFO mHALInfo;
    BOOL newmode = FALSE;
    LPDDSURFACEDESC mpTextures;
    D3DHAL_CALLBACKS mD3dCallbacks;
    D3DHAL_GLOBALDRIVERDATA mD3dDriverData;
    DDHAL_DDEXEBUFCALLBACKS mD3dBufferCallbacks;
    LPDDRAWI_DIRECTDRAW_INT This = (LPDDRAWI_DIRECTDRAW_INT)iface;
    DDHAL_GETDRIVERINFODATA DdGetDriverInfo = { 0 };

    DX_WINDBG_trace();

    RtlZeroMemory(&mHALInfo, sizeof(DDHALINFO));
    RtlZeroMemory(&mD3dCallbacks, sizeof(D3DHAL_CALLBACKS));
    RtlZeroMemory(&mD3dDriverData, sizeof(D3DHAL_GLOBALDRIVERDATA));
    RtlZeroMemory(&mD3dBufferCallbacks, sizeof(DDHAL_DDEXEBUFCALLBACKS));

    if (reenable == FALSE)
    {
        if (ddgbl.lpDDCBtmp == NULL)
        {
            DxHeapMemAlloc(ddgbl.lpDDCBtmp, sizeof(DDHAL_CALLBACKS));
            if ( ddgbl.lpDDCBtmp == NULL)
            {
                return DD_FALSE;
            }
        }
    }
    else
    {
        RtlZeroMemory(ddgbl.lpDDCBtmp,sizeof(DDHAL_CALLBACKS));
    }

    /*
     *  Startup DX HAL step one of three
     */
    if (!DdCreateDirectDrawObject(This->lpLcl->lpGbl, (HDC)This->lpLcl->hDC))
    {
       DxHeapMemFree(ddgbl.lpDDCBtmp);
       return DD_FALSE;
    }

    /* Some card disable the dx after it have been created so
     * we are force reenabling it
     */
    if (!DdReenableDirectDrawObject(This->lpLcl->lpGbl, &newmode))
    {
      DxHeapMemFree(ddgbl.lpDDCBtmp);
      return DD_FALSE;
    }

    if (!DdQueryDirectDrawObject(This->lpLcl->lpGbl,
                                 &mHALInfo,
                                 &ddgbl.lpDDCBtmp->HALDD,
                                 &ddgbl.lpDDCBtmp->HALDDSurface,
                                 &ddgbl.lpDDCBtmp->HALDDPalette,
                                 &mD3dCallbacks,
                                 &mD3dDriverData,
                                 &mD3dBufferCallbacks,
                                 NULL,
                                 mpFourCC,
                                 NULL))
    {
      DxHeapMemFree(This->lpLcl->lpGbl->lpModeInfo);
      DxHeapMemFree(ddgbl.lpDDCBtmp);
      // FIXME Close DX first and second call
      return DD_FALSE;
    }

    /* Alloc mpFourCC */
    if (This->lpLcl->lpGbl->lpdwFourCC != NULL)
    {
        DxHeapMemFree(This->lpLcl->lpGbl->lpdwFourCC);
    }

    if (mHALInfo.ddCaps.dwNumFourCCCodes > 0 )
    {

        DxHeapMemAlloc(mpFourCC, sizeof(DWORD) * (mHALInfo.ddCaps.dwNumFourCCCodes + 2));

        if (mpFourCC == NULL)
        {
            DxHeapMemFree(ddgbl.lpDDCBtmp);
            // FIXME Close DX first and second call
            return DD_FALSE;
        }
    }

    /* Alloc mpTextures */
#if 0
    DX_STUB_str("1 Here\n");

    if (This->lpLcl->lpGbl->texture != NULL)
    {
        DxHeapMemFree(This->lpLcl->lpGbl->texture;
    }

    mpTextures = NULL;
    if (mD3dDriverData.dwNumTextureFormats > 0)
    {
        mpTextures = (DDSURFACEDESC*) DxHeapMemAlloc(sizeof(DDSURFACEDESC) * mD3dDriverData.dwNumTextureFormats);
        if (mpTextures == NULL)
        {
            DxHeapMemFree(mpFourCC);
            DxHeapMemFree(ddgbl.lpDDCBtmp);
            // FIXME Close DX first and second call
        }
    }

    DX_STUB_str("2 Here\n");

#else
      mpTextures = NULL;
#endif


    /* Get all basic data from the driver */
    if (!DdQueryDirectDrawObject(
                                 This->lpLcl->lpGbl,
                                 &mHALInfo,
                                 &ddgbl.lpDDCBtmp->HALDD,
                                 &ddgbl.lpDDCBtmp->HALDDSurface,
                                 &ddgbl.lpDDCBtmp->HALDDPalette,
                                 &mD3dCallbacks,
                                 &mD3dDriverData,
                                 &ddgbl.lpDDCBtmp->HALDDExeBuf,
                                 (DDSURFACEDESC*)mpTextures,
                                 mpFourCC,
                                 NULL))
    {
        DxHeapMemFree(mpFourCC);
        DxHeapMemFree(mpTextures);
        DxHeapMemFree(ddgbl.lpDDCBtmp);
        // FIXME Close DX first and second call
        return DD_FALSE;
    }

    memcpy(&ddgbl.vmiData, &mHALInfo.vmiData,sizeof(VIDMEMINFO));


    memcpy(&ddgbl.ddCaps,  &mHALInfo.ddCaps,sizeof(DDCORECAPS));

    This->lpLcl->lpGbl->dwNumFourCC        = mHALInfo.ddCaps.dwNumFourCCCodes;
    This->lpLcl->lpGbl->lpdwFourCC         = mpFourCC;
    // This->lpLcl->lpGbl->dwMonitorFrequency = mHALInfo.dwMonitorFrequency;     // 0
    This->lpLcl->lpGbl->dwModeIndex        = mHALInfo.dwModeIndex;
    // This->lpLcl->lpGbl->dwNumModes         = mHALInfo.dwNumModes;
    // This->lpLcl->lpGbl->lpModeInfo         = mHALInfo.lpModeInfo;

    /* FIXME convert mpTextures to DDHALMODEINFO */
    // DxHeapMemFree( mpTextures);

    /* FIXME D3D setup mD3dCallbacks and mD3dDriverData */




    if (mHALInfo.dwFlags & DDHALINFO_GETDRIVERINFOSET)
    {
        DdGetDriverInfo.dwSize = sizeof (DDHAL_GETDRIVERINFODATA);
        DdGetDriverInfo.guidInfo = GUID_MiscellaneousCallbacks;
        DdGetDriverInfo.lpvData = (PVOID)&ddgbl.lpDDCBtmp->HALDDMiscellaneous;
        DdGetDriverInfo.dwExpectedSize = sizeof (DDHAL_DDMISCELLANEOUSCALLBACKS);

        if(mHALInfo.GetDriverInfo (&DdGetDriverInfo) == DDHAL_DRIVER_NOTHANDLED || DdGetDriverInfo.ddRVal != DD_OK)
        {
            DxHeapMemFree(mpFourCC);
            DxHeapMemFree(mpTextures);
            DxHeapMemFree(ddgbl.lpDDCBtmp);
            // FIXME Close DX fristcall and second call
            return DD_FALSE;
        }

        RtlZeroMemory(&DdGetDriverInfo, sizeof(DDHAL_GETDRIVERINFODATA));
        DdGetDriverInfo.dwSize = sizeof (DDHAL_GETDRIVERINFODATA);
        DdGetDriverInfo.guidInfo = GUID_Miscellaneous2Callbacks;

        /* FIXME
        DdGetDriverInfo.lpvData = (PVOID)&ddgbl.lpDDCBtmp->HALDDMiscellaneous;
        DdGetDriverInfo.dwExpectedSize = sizeof (DDHAL_DDMISCELLANEOUS2CALLBACKS);

        if(mHALInfo.GetDriverInfo (&DdGetDriverInfo) == DDHAL_DRIVER_NOTHANDLED || DdGetDriverInfo.ddRVal != DD_OK)
        {
            DxHeapMemFree(mpFourCC);
            DxHeapMemFree(mpTextures);
            DxHeapMemFree(ddgbl.lpDDCBtmp);
            // FIXME Close DX fristcall and second call
            return DD_FALSE;
        }
        DD_MISCELLANEOUS2CALLBACKS
        {
            DWORD                dwSize;
            DWORD                dwFlags;
            PDD_ALPHABLT         AlphaBlt;  // unsuse according to msdn and always set to NULL
            PDD_CREATESURFACEEX  CreateSurfaceEx;
            PDD_GETDRIVERSTATE   GetDriverState;
            PDD_DESTROYDDLOCAL   DestroyDDLocal;
        }
          DDHAL_MISC2CB32_CREATESURFACEEX
          DDHAL_MISC2CB32_GETDRIVERSTATE
          DDHAL_MISC2CB32_DESTROYDDLOCAL
        */
    }

    if (mHALInfo.dwFlags & DDHALINFO_GETDRIVERINFO2)
    {
        This->lpLcl->lpGbl->dwFlags = This->lpLcl->lpGbl->dwFlags | DDRAWI_DRIVERINFO2;
    }


    return DD_OK;
}