Forgot cleanup the code. in Main_setDisplayMode.
[reactos.git] / reactos / lib / ddraw / main / ddraw.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS
5 * FILE: lib/ddraw/main/ddraw.c
6 * PURPOSE: IDirectDraw7 Implementation
7 * PROGRAMMER: Magnus Olsen, Maarten Bosma
8 *
9 */
10
11 #include "rosdraw.h"
12
13
14 HRESULT WINAPI Main_DirectDraw_Initialize (LPDIRECTDRAW7 iface, LPGUID lpGUID)
15 {
16 IDirectDrawImpl* This = (IDirectDrawImpl*)iface;
17 HRESULT ret;
18
19 // this if it is not called by DirectDrawCreate
20 if(FALSE)
21 return DDERR_ALREADYINITIALIZED;
22
23 // save the parameter
24 This->lpGUID = lpGUID;
25
26 // get the HDC
27 This->hdc = GetWindowDC(GetDesktopWindow());
28 This->Height = GetDeviceCaps(This->hdc, VERTRES);
29 This->Width = GetDeviceCaps(This->hdc, HORZRES);
30 This->Bpp = GetDeviceCaps(This->hdc, BITSPIXEL);
31
32 // call software first
33 if((ret = Hal_DirectDraw_Initialize (iface)) != DD_OK)
34 return ret;
35
36 // ... then overwrite with hal
37 if((ret = Hel_DirectDraw_Initialize (iface)) != DD_OK)
38 return ret;
39
40 return DD_OK;
41 }
42
43 HRESULT WINAPI Main_DirectDraw_SetCooperativeLevel (LPDIRECTDRAW7 iface, HWND hwnd, DWORD cooplevel)
44 {
45 // TODO:
46 // - create a scaner that check which driver we should get the HDC from
47 // for now we always asume it is the active dirver that should be use.
48 // - allow more Flags
49
50 IDirectDrawImpl* This = (IDirectDrawImpl*)iface;
51 HRESULT ret;
52
53 // check the parameters
54 if (This->cooperative_level == cooplevel && This->window == hwnd)
55 return DD_OK;
56
57 if (This->window)
58 return DDERR_HWNDALREADYSET;
59
60 if (This->cooperative_level)
61 return DDERR_EXCLUSIVEMODEALREADYSET;
62
63 if ((cooplevel&DDSCL_EXCLUSIVE) && !(cooplevel&DDSCL_FULLSCREEN))
64 return DDERR_INVALIDPARAMS;
65
66 if (cooplevel&DDSCL_NORMAL && cooplevel&DDSCL_FULLSCREEN)
67 return DDERR_INVALIDPARAMS;
68
69 // set the data
70 This->window = hwnd;
71 This->hdc = GetDC(hwnd);
72 This->cooperative_level = cooplevel;
73
74
75
76 if((ret = Hal_DirectDraw_SetCooperativeLevel (iface)) != DD_OK)
77 return ret;
78
79 if((ret = Hel_DirectDraw_SetCooperativeLevel (iface)) != DD_OK)
80 return ret;
81
82 return DD_OK;
83 }
84
85 HRESULT WINAPI Main_DirectDraw_SetDisplayMode (LPDIRECTDRAW7 iface, DWORD dwWidth, DWORD dwHeight,
86 DWORD dwBPP, DWORD dwRefreshRate, DWORD dwFlags)
87 {
88 DWORD ret;
89 if((ret = Hal_DirectDraw_SetDisplayMode(iface, dwWidth, dwHeight,
90 dwBPP, dwRefreshRate, dwFlags)) == DD_OK)
91 {
92 return ret;
93 }
94
95 ret = Hel_DirectDraw_SetDisplayMode(iface, dwWidth, dwHeight, dwBPP, dwRefreshRate, dwFlags);
96
97 return ret;
98 }
99
100 ULONG WINAPI Main_DirectDraw_AddRef (LPDIRECTDRAW7 iface)
101 {
102 IDirectDrawImpl* This = (IDirectDrawImpl*)iface;
103 ULONG ref = InterlockedIncrement((PLONG)&This->DirectDrawGlobal.dwRefCnt);
104
105 return ref;
106 }
107
108 ULONG WINAPI Main_DirectDraw_Release (LPDIRECTDRAW7 iface)
109 {
110 IDirectDrawImpl* This = (IDirectDrawImpl*)iface;
111 ULONG ref = InterlockedDecrement((PLONG)&This->DirectDrawGlobal.dwRefCnt);
112
113 if (ref == 0)
114 {
115 // set resoltion back to the one in registry
116 if(This->cooperative_level & DDSCL_EXCLUSIVE)
117 ChangeDisplaySettings(NULL, 0);
118
119 HeapFree(GetProcessHeap(), 0, This);
120 }
121
122 return ref;
123 }
124
125 HRESULT WINAPI Main_DirectDraw_QueryInterface (
126 LPDIRECTDRAW7 iface, REFIID id, LPVOID *obj )
127 {
128 IDirectDrawImpl* This = (IDirectDrawImpl*)iface;
129
130 if (IsEqualGUID(&IID_IDirectDraw7, id))
131 {
132 *obj = &This->lpVtbl;
133 }
134 else if (IsEqualGUID(&IID_IDirectDraw, id))
135 {
136 *obj = &This->lpVtbl_v1;
137 }
138 else if (IsEqualGUID(&IID_IDirectDraw2, id))
139 {
140 *obj = &This->lpVtbl_v2;
141 }
142 else if (IsEqualGUID(&IID_IDirectDraw4, id))
143 {
144 *obj = &This->lpVtbl_v4;
145 }
146 else
147 {
148 *obj = NULL;
149 return E_NOINTERFACE;
150 }
151
152 Main_DirectDraw_AddRef(iface);
153 return S_OK;
154 }
155
156 HRESULT WINAPI Main_DirectDraw_CreateSurface (LPDIRECTDRAW7 iface, LPDDSURFACEDESC2 pDDSD,
157 LPDIRECTDRAWSURFACE7 *ppSurf, IUnknown *pUnkOuter)
158 {
159 if (pUnkOuter!=NULL)
160 return DDERR_INVALIDPARAMS;
161
162 if(sizeof(DDSURFACEDESC2)!=pDDSD->dwSize)
163 return DDERR_UNSUPPORTED;
164
165 // the nasty com stuff
166 IDirectDrawSurfaceImpl* That;
167
168 That = (IDirectDrawSurfaceImpl*)HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawSurfaceImpl));
169
170 if (That == NULL)
171 return E_OUTOFMEMORY;
172
173 ZeroMemory(That, sizeof(IDirectDrawSurfaceImpl));
174
175 That->lpVtbl = &DirectDrawSurface7_Vtable;
176 That->lpVtbl_v3 = &DDRAW_IDDS3_Thunk_VTable;
177 That->ref = 1;
178
179 *ppSurf = (LPDIRECTDRAWSURFACE7)That;
180
181 // the real surface object creation
182 return That->lpVtbl->Initialize (*ppSurf, (LPDIRECTDRAW)iface, pDDSD);
183 }
184
185 HRESULT WINAPI Main_DirectDraw_CreateClipper(LPDIRECTDRAW7 iface, DWORD dwFlags,
186 LPDIRECTDRAWCLIPPER *ppClipper, IUnknown *pUnkOuter)
187 {
188 if (pUnkOuter!=NULL)
189 return DDERR_INVALIDPARAMS;
190
191 IDirectDrawClipperImpl* That;
192 That = (IDirectDrawClipperImpl*)HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawClipperImpl));
193
194 if (That == NULL)
195 return E_OUTOFMEMORY;
196
197 ZeroMemory(That, sizeof(IDirectDrawClipperImpl));
198
199 That->lpVtbl = &DirectDrawClipper_Vtable;
200 That->ref = 1;
201 *ppClipper = (LPDIRECTDRAWCLIPPER)That;
202
203 return That->lpVtbl->Initialize (*ppClipper, (LPDIRECTDRAW)iface, dwFlags);
204 }
205
206 // This function is exported by the dll
207 HRESULT WINAPI DirectDrawCreateClipper (DWORD dwFlags,
208 LPDIRECTDRAWCLIPPER* lplpDDClipper, LPUNKNOWN pUnkOuter)
209 {
210 return Main_DirectDraw_CreateClipper(NULL, dwFlags, lplpDDClipper, pUnkOuter);
211 }
212
213 HRESULT WINAPI Main_DirectDraw_CreatePalette(LPDIRECTDRAW7 iface, DWORD dwFlags,
214 LPPALETTEENTRY palent, LPDIRECTDRAWPALETTE* ppPalette, LPUNKNOWN pUnkOuter)
215 {
216 if (pUnkOuter!=NULL)
217 return DDERR_INVALIDPARAMS;
218
219 IDirectDrawPaletteImpl* That;
220 That = (IDirectDrawPaletteImpl*)HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectDrawPaletteImpl));
221
222 if (That == NULL)
223 return E_OUTOFMEMORY;
224
225 ZeroMemory(That, sizeof(IDirectDrawPaletteImpl));
226
227 That->lpVtbl = &DirectDrawPalette_Vtable;
228 That->ref = 1;
229 *ppPalette = (LPDIRECTDRAWPALETTE)That;
230
231 return That->lpVtbl->Initialize (*ppPalette, (LPDIRECTDRAW)iface, dwFlags, palent);
232 }
233
234 /**** Stubs ****/
235
236 HRESULT WINAPI Main_DirectDraw_Compact(LPDIRECTDRAW7 iface)
237 {
238 DX_STUB;
239 }
240
241 HRESULT WINAPI Main_DirectDraw_DuplicateSurface(LPDIRECTDRAW7 iface, LPDIRECTDRAWSURFACE7 src,
242 LPDIRECTDRAWSURFACE7* dst)
243 {
244 DX_STUB;
245 }
246
247 HRESULT WINAPI Main_DirectDraw_EnumDisplayModes(LPDIRECTDRAW7 iface, DWORD dwFlags,
248 LPDDSURFACEDESC2 pDDSD, LPVOID context, LPDDENUMMODESCALLBACK2 callback)
249 {
250 DX_STUB;
251 }
252
253 HRESULT WINAPI Main_DirectDraw_EnumSurfaces(LPDIRECTDRAW7 iface, DWORD dwFlags,
254 LPDDSURFACEDESC2 lpDDSD2, LPVOID context,
255 LPDDENUMSURFACESCALLBACK7 callback)
256 {
257 DX_STUB;
258 }
259
260 HRESULT WINAPI Main_DirectDraw_FlipToGDISurface(LPDIRECTDRAW7 iface)
261 {
262 DWORD ret;
263
264 if((ret = Hal_DirectDraw_FlipToGDISurface( iface)) == DD_OK)
265 return ret;
266
267 if((ret = Hel_DirectDraw_FlipToGDISurface( iface)) == DD_OK)
268 return ret;
269
270 return DDERR_NOTINITIALIZED;
271 }
272
273 HRESULT WINAPI Main_DirectDraw_GetCaps(LPDIRECTDRAW7 iface, LPDDCAPS pDriverCaps,
274 LPDDCAPS pHELCaps)
275 {
276 DWORD status = DD_FALSE;
277 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
278
279 if (pDriverCaps != NULL)
280 {
281 RtlCopyMemory(pDriverCaps,&This->DirectDrawGlobal.ddCaps,sizeof(DDCORECAPS));
282 status = DD_OK;
283 }
284
285 if (pHELCaps != NULL)
286 {
287 RtlCopyMemory(pDriverCaps,&This->DirectDrawGlobal.ddHELCaps,sizeof(DDCORECAPS));
288 status = DD_OK;
289 }
290
291 /* Both caps mixed ?? */
292 /* DDCORECAPS ddBothCaps; */
293
294 return status;
295 }
296
297 HRESULT WINAPI Main_DirectDraw_GetDisplayMode(LPDIRECTDRAW7 iface, LPDDSURFACEDESC2 pDDSD)
298 {
299 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
300
301 if (pDDSD == NULL)
302 {
303 return DD_FALSE;
304 }
305
306 pDDSD->dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_PITCH | DDSD_PIXELFORMAT | DDSD_REFRESHRATE | DDSD_WIDTH;
307 pDDSD->dwHeight = This->DirectDrawGlobal.vmiData.dwDisplayHeight;
308 pDDSD->dwWidth = This->DirectDrawGlobal.vmiData.dwDisplayWidth;
309
310 /* FIXME Do not use DUMMYUNIONNAME1 some how union lPitch does not see by the compiler
311 but rest of the union are visable. more header problem ???
312 */
313 pDDSD->DUMMYUNIONNAME1.lPitch = This->DirectDrawGlobal.vmiData.lDisplayPitch;
314
315
316 /* have not check where I should get hold of this info yet
317 DWORD dwBackBufferCount;
318 */
319
320 pDDSD->dwRefreshRate = This->DirectDrawGlobal.dwMonitorFrequency;
321
322 /* have not check where I should get hold of this info yet
323 DWORD dwAlphaBitDepth;
324 DWORD dwReserved;
325 LPVOID lpSurface;
326 union
327 {
328 DDCOLORKEY ddckCKDestOverlay;
329 DWORD dwEmptyFaceColor;
330 }
331 DDCOLORKEY ddckCKDestBlt;
332 DDCOLORKEY ddckCKSrcOverlay;
333 DDCOLORKEY ddckCKSrcBlt;
334 */
335
336
337 RtlCopyMemory(&pDDSD->ddpfPixelFormat,&This->DirectDrawGlobal.vmiData.ddpfDisplay,sizeof(DDPIXELFORMAT));
338 RtlCopyMemory(&pDDSD->ddsCaps,&This->DirectDrawGlobal.ddCaps,sizeof(DDCORECAPS));
339
340 /* have not check where I should get hold of this info yet
341 DWORD dwTextureStage;
342 */
343
344 return DD_OK;
345 }
346
347
348 HRESULT WINAPI Main_DirectDraw_GetFourCCCodes(LPDIRECTDRAW7 iface, LPDWORD pNumCodes, LPDWORD pCodes)
349 {
350 DX_STUB;
351 }
352
353 HRESULT WINAPI Main_DirectDraw_GetGDISurface(LPDIRECTDRAW7 iface,
354 LPDIRECTDRAWSURFACE7 *lplpGDIDDSSurface)
355 {
356 DX_STUB;
357 }
358
359 HRESULT WINAPI Main_DirectDraw_GetMonitorFrequency(LPDIRECTDRAW7 iface,LPDWORD freq)
360 {
361 IDirectDrawImpl *This = (IDirectDrawImpl *)iface;
362
363 if (freq == NULL)
364 {
365 return DD_FALSE;
366 }
367
368 *freq = This->DirectDrawGlobal.dwMonitorFrequency;
369 return DD_OK;
370 }
371
372 HRESULT WINAPI Main_DirectDraw_GetScanLine(LPDIRECTDRAW7 iface, LPDWORD lpdwScanLine)
373 {
374 DWORD ret;
375
376 if((ret = Hal_DirectDraw_GetScanLine( iface, lpdwScanLine)) == DD_OK)
377 return ret;
378
379 if((ret = Hel_DirectDraw_GetScanLine( iface, lpdwScanLine)) == DD_OK)
380 return ret;
381
382 return DDERR_NOTINITIALIZED;
383 }
384
385 HRESULT WINAPI Main_DirectDraw_GetVerticalBlankStatus(LPDIRECTDRAW7 iface, LPBOOL status)
386 {
387 DX_STUB;
388 }
389
390 HRESULT WINAPI Main_DirectDraw_RestoreDisplayMode(LPDIRECTDRAW7 iface)
391 {
392 DX_STUB;
393 }
394
395 HRESULT WINAPI Main_DirectDraw_WaitForVerticalBlank(LPDIRECTDRAW7 iface, DWORD dwFlags,
396 HANDLE h)
397 {
398 DWORD ret;
399
400 if((ret = Hal_DirectDraw_WaitForVerticalBlank( iface, dwFlags, h)) == DD_OK)
401 return ret;
402
403 if((ret = Hel_DirectDraw_WaitForVerticalBlank( iface, dwFlags, h)) == DD_OK)
404 return ret;
405
406 return DDERR_NOTINITIALIZED;
407 }
408
409 HRESULT WINAPI Main_DirectDraw_GetAvailableVidMem(LPDIRECTDRAW7 iface, LPDDSCAPS2 ddscaps,
410 LPDWORD total, LPDWORD free)
411 {
412 DWORD ret;
413
414 if((ret = Hal_DirectDraw_GetAvailableVidMem (iface,ddscaps,total,free)) == DD_OK)
415 return ret;
416
417 if((ret = Hel_DirectDraw_GetAvailableVidMem (iface,ddscaps,total,free)) == DD_OK)
418 return ret;
419
420
421 return DDERR_NOTINITIALIZED;
422
423 }
424
425 HRESULT WINAPI Main_DirectDraw_GetSurfaceFromDC(LPDIRECTDRAW7 iface, HDC hdc,
426 LPDIRECTDRAWSURFACE7 *lpDDS)
427 {
428 DX_STUB;
429 }
430
431 HRESULT WINAPI Main_DirectDraw_RestoreAllSurfaces(LPDIRECTDRAW7 iface)
432 {
433 DX_STUB;
434 }
435
436 HRESULT WINAPI Main_DirectDraw_TestCooperativeLevel(LPDIRECTDRAW7 iface)
437 {
438 DX_STUB;
439 }
440
441 HRESULT WINAPI Main_DirectDraw_GetDeviceIdentifier(LPDIRECTDRAW7 iface,
442 LPDDDEVICEIDENTIFIER2 pDDDI, DWORD dwFlags)
443 {
444 DX_STUB;
445 }
446
447 HRESULT WINAPI Main_DirectDraw_StartModeTest(LPDIRECTDRAW7 iface, LPSIZE pModes,
448 DWORD dwNumModes, DWORD dwFlags)
449 {
450 DX_STUB;
451 }
452
453 HRESULT WINAPI Main_DirectDraw_EvaluateMode(LPDIRECTDRAW7 iface,DWORD a,DWORD* b)
454 {
455 DX_STUB;
456 }
457
458 IDirectDraw7Vtbl DirectDraw7_Vtable =
459 {
460 Main_DirectDraw_QueryInterface,
461 Main_DirectDraw_AddRef,
462 Main_DirectDraw_Release,
463 Main_DirectDraw_Compact,
464 Main_DirectDraw_CreateClipper,
465 Main_DirectDraw_CreatePalette,
466 Main_DirectDraw_CreateSurface,
467 Main_DirectDraw_DuplicateSurface,
468 Main_DirectDraw_EnumDisplayModes,
469 Main_DirectDraw_EnumSurfaces,
470 Main_DirectDraw_FlipToGDISurface,
471 Main_DirectDraw_GetCaps,
472 Main_DirectDraw_GetDisplayMode,
473 Main_DirectDraw_GetFourCCCodes,
474 Main_DirectDraw_GetGDISurface,
475 Main_DirectDraw_GetMonitorFrequency,
476 Main_DirectDraw_GetScanLine,
477 Main_DirectDraw_GetVerticalBlankStatus,
478 Main_DirectDraw_Initialize,
479 Main_DirectDraw_RestoreDisplayMode,
480 Main_DirectDraw_SetCooperativeLevel,
481 Main_DirectDraw_SetDisplayMode,
482 Main_DirectDraw_WaitForVerticalBlank,
483 Main_DirectDraw_GetAvailableVidMem,
484 Main_DirectDraw_GetSurfaceFromDC,
485 Main_DirectDraw_RestoreAllSurfaces,
486 Main_DirectDraw_TestCooperativeLevel,
487 Main_DirectDraw_GetDeviceIdentifier,
488 Main_DirectDraw_StartModeTest,
489 Main_DirectDraw_EvaluateMode
490 };