kernel32: Update function docs for GetBinayType.
[wine] / dlls / ddraw / tests / ddrawmodes.c
1 /*
2  * Unit tests for ddraw functions
3  *
4  *
5  * Part of this test involves changing the screen resolution. But this is
6  * really disrupting if the user is doing something else and is not very nice
7  * to CRT screens. Plus, ideally it needs someone watching it to check that
8  * each mode displays correctly.
9  * So this is only done if the test is being run in interactive mode.
10  *
11  * Copyright (C) 2003 Sami Aario
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with this library; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27
28 #include <assert.h>
29 #include "wine/test.h"
30 #include "ddraw.h"
31
32 static LPDIRECTDRAW lpDD = NULL;
33 static LPDIRECTDRAWSURFACE lpDDSPrimary = NULL;
34 static LPDIRECTDRAWSURFACE lpDDSBack = NULL;
35 static WNDCLASS wc;
36 static HWND hwnd;
37 static int modes_cnt;
38 static int modes_size;
39 static LPDDSURFACEDESC modes;
40 static RECT rect_before_create;
41 static RECT rect_after_delete;
42
43 static HRESULT (WINAPI *pDirectDrawEnumerateA)(LPDDENUMCALLBACKA,LPVOID);
44 static HRESULT (WINAPI *pDirectDrawEnumerateW)(LPDDENUMCALLBACKW,LPVOID);
45 static HRESULT (WINAPI *pDirectDrawEnumerateExA)(LPDDENUMCALLBACKEXA,LPVOID,DWORD);
46 static HRESULT (WINAPI *pDirectDrawEnumerateExW)(LPDDENUMCALLBACKEXW,LPVOID,DWORD);
47
48 static void init_function_pointers(void)
49 {
50     HMODULE hmod = GetModuleHandleA("ddraw.dll");
51     pDirectDrawEnumerateA = (void*)GetProcAddress(hmod, "DirectDrawEnumerateA");
52     pDirectDrawEnumerateW = (void*)GetProcAddress(hmod, "DirectDrawEnumerateW");
53     pDirectDrawEnumerateExA = (void*)GetProcAddress(hmod, "DirectDrawEnumerateExA");
54     pDirectDrawEnumerateExW = (void*)GetProcAddress(hmod, "DirectDrawEnumerateExW");
55 }
56
57 static void createwindow(void)
58 {
59     wc.style = CS_HREDRAW | CS_VREDRAW;
60     wc.lpfnWndProc = DefWindowProcA;
61     wc.cbClsExtra = 0;
62     wc.cbWndExtra = 0;
63     wc.hInstance = GetModuleHandleA(0);
64     wc.hIcon = LoadIconA(wc.hInstance, IDI_APPLICATION);
65     wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
66     wc.hbrBackground = GetStockObject(BLACK_BRUSH);
67     wc.lpszMenuName = NULL;
68     wc.lpszClassName = "TestWindowClass";
69     if(!RegisterClassA(&wc))
70         assert(0);
71
72     hwnd = CreateWindowExA(0, "TestWindowClass", "TestWindowClass",
73         WS_POPUP, 0, 0,
74         GetSystemMetrics(SM_CXSCREEN),
75         GetSystemMetrics(SM_CYSCREEN),
76         NULL, NULL, GetModuleHandleA(0), NULL);
77     assert(hwnd != NULL);
78
79     ShowWindow(hwnd, SW_HIDE);
80     UpdateWindow(hwnd);
81     SetFocus(hwnd);
82 }
83
84 static BOOL createdirectdraw(void)
85 {
86     HRESULT rc;
87
88     SetRect(&rect_before_create, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
89
90     rc = DirectDrawCreate(NULL, &lpDD, NULL);
91     ok(rc==DD_OK || rc==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", rc);
92     if (!lpDD) {
93         trace("DirectDrawCreateEx() failed with an error %x\n", rc);
94         return FALSE;
95     }
96     return TRUE;
97 }
98
99
100 static void releasedirectdraw(void)
101 {
102     if( lpDD != NULL )
103     {
104         IDirectDraw_Release(lpDD);
105         lpDD = NULL;
106         SetRect(&rect_after_delete, 0, 0,
107                 GetSystemMetrics(SM_CXSCREEN),
108                 GetSystemMetrics(SM_CYSCREEN));
109         ok(EqualRect(&rect_before_create, &rect_after_delete) != 0,
110            "Original display mode was not restored\n");
111     }
112 }
113
114 static BOOL WINAPI crash_callbackA(GUID *lpGUID, LPSTR lpDriverDescription,
115                                   LPSTR lpDriverName, LPVOID lpContext)
116 {
117     *(volatile char*)0 = 2;
118     return TRUE;
119 }
120
121 static BOOL WINAPI test_nullcontext_callbackA(GUID *lpGUID, LPSTR lpDriverDescription,
122                                               LPSTR lpDriverName, LPVOID lpContext)
123 {
124     trace("test_nullcontext_callbackA: %p %s %s %p\n",
125           lpGUID, lpDriverDescription, lpDriverName, lpContext);
126
127     ok(!lpContext, "Expected NULL lpContext\n");
128
129     return TRUE;
130 }
131
132 static BOOL WINAPI test_context_callbackA(GUID *lpGUID, LPSTR lpDriverDescription,
133                                           LPSTR lpDriverName, LPVOID lpContext)
134 {
135     trace("test_context_callbackA: %p %s %s %p\n",
136           lpGUID, lpDriverDescription, lpDriverName, lpContext);
137
138     ok(lpContext == (LPVOID)0xdeadbeef, "Expected non-NULL lpContext\n");
139
140     return TRUE;
141 }
142
143 static void test_DirectDrawEnumerateA(void)
144 {
145     HRESULT ret;
146
147     if (!pDirectDrawEnumerateA)
148     {
149         win_skip("DirectDrawEnumerateA is not available\n");
150         return;
151     }
152
153     /* Test with NULL callback parameter. */
154     ret = pDirectDrawEnumerateA(NULL, NULL);
155     ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
156
157     /* Test with invalid callback parameter. */
158     ret = pDirectDrawEnumerateA((LPDDENUMCALLBACKA)0xdeadbeef, NULL);
159     ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
160
161     if (pDirectDrawEnumerateExA)
162     {
163         /* Test with callback that crashes. */
164         ret = pDirectDrawEnumerateA(crash_callbackA, NULL);
165         ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
166     }
167     else
168         win_skip("Test would crash on older ddraw\n");
169
170     /* Test with valid callback parameter and NULL context parameter. */
171     trace("Calling DirectDrawEnumerateA with test_nullcontext_callbackA callback and NULL context.\n");
172     ret = pDirectDrawEnumerateA(test_nullcontext_callbackA, NULL);
173     ok(ret == DD_OK, "Expected DD_OK, got %d\n", ret);
174
175     /* Test with valid callback parameter and valid context parameter. */
176     trace("Calling DirectDrawEnumerateA with test_context_callbackA callback and non-NULL context.\n");
177     ret = pDirectDrawEnumerateA(test_context_callbackA, (LPVOID)0xdeadbeef);
178     ok(ret == DD_OK, "Expected DD_OK, got %d\n", ret);
179 }
180
181 static BOOL WINAPI test_callbackW(GUID *lpGUID, LPWSTR lpDriverDescription,
182                                   LPWSTR lpDriverName, LPVOID lpContext)
183 {
184     ok(0, "The callback should not be invoked by DirectDrawEnumerateW\n");
185     return TRUE;
186 }
187
188 static void test_DirectDrawEnumerateW(void)
189 {
190     HRESULT ret;
191
192     if (!pDirectDrawEnumerateW)
193     {
194         win_skip("DirectDrawEnumerateW is not available\n");
195         return;
196     }
197
198     /* DirectDrawEnumerateW is not implemented on Windows. */
199
200     /* Test with NULL callback parameter. */
201     ret = pDirectDrawEnumerateW(NULL, NULL);
202     ok(ret == DDERR_INVALIDPARAMS ||
203        ret == DDERR_UNSUPPORTED, /* Older ddraw */
204        "Expected DDERR_INVALIDPARAMS or DDERR_UNSUPPORTED, got %d\n", ret);
205
206     /* Test with invalid callback parameter. */
207     ret = pDirectDrawEnumerateW((LPDDENUMCALLBACKW)0xdeadbeef, NULL);
208     ok(ret == DDERR_INVALIDPARAMS /* XP */ ||
209        ret == DDERR_UNSUPPORTED /* Win7 */,
210        "Expected DDERR_INVALIDPARAMS or DDERR_UNSUPPORTED, got %d\n", ret);
211
212     /* Test with valid callback parameter and NULL context parameter. */
213     ret = pDirectDrawEnumerateW(test_callbackW, NULL);
214     ok(ret == DDERR_UNSUPPORTED, "Expected DDERR_UNSUPPORTED, got %d\n", ret);
215 }
216
217 static BOOL WINAPI crash_callbackExA(GUID *lpGUID, LPSTR lpDriverDescription,
218                                      LPSTR lpDriverName, LPVOID lpContext,
219                                      HMONITOR hm)
220 {
221     *(volatile char*)0 = 2;
222     return TRUE;
223 }
224
225 static BOOL WINAPI test_nullcontext_callbackExA(GUID *lpGUID, LPSTR lpDriverDescription,
226                                                 LPSTR lpDriverName, LPVOID lpContext,
227                                                 HMONITOR hm)
228 {
229     trace("test_nullcontext_callbackExA: %p %s %s %p %p\n", lpGUID,
230           lpDriverDescription, lpDriverName, lpContext, hm);
231
232     ok(!lpContext, "Expected NULL lpContext\n");
233
234     return TRUE;
235 }
236
237 static BOOL WINAPI test_context_callbackExA(GUID *lpGUID, LPSTR lpDriverDescription,
238                                             LPSTR lpDriverName, LPVOID lpContext,
239                                             HMONITOR hm)
240 {
241     trace("test_context_callbackExA: %p %s %s %p %p\n", lpGUID,
242           lpDriverDescription, lpDriverName, lpContext, hm);
243
244     ok(lpContext == (LPVOID)0xdeadbeef, "Expected non-NULL lpContext\n");
245
246     return TRUE;
247 }
248
249 static void test_DirectDrawEnumerateExA(void)
250 {
251     HRESULT ret;
252
253     if (!pDirectDrawEnumerateExA)
254     {
255         win_skip("DirectDrawEnumerateExA is not available\n");
256         return;
257     }
258
259     /* Test with NULL callback parameter. */
260     ret = pDirectDrawEnumerateExA(NULL, NULL, 0);
261     ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
262
263     /* Test with invalid callback parameter. */
264     ret = pDirectDrawEnumerateExA((LPDDENUMCALLBACKEXA)0xdeadbeef, NULL, 0);
265     ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
266
267     /* Test with callback that crashes. */
268     ret = pDirectDrawEnumerateExA(crash_callbackExA, NULL, 0);
269     ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
270
271     /* Test with valid callback parameter and invalid flags */
272     ret = pDirectDrawEnumerateExA(test_nullcontext_callbackExA, NULL, ~0);
273     ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
274
275     /* Test with valid callback parameter and NULL context parameter. */
276     trace("Calling DirectDrawEnumerateExA with empty flags and NULL context.\n");
277     ret = pDirectDrawEnumerateExA(test_nullcontext_callbackExA, NULL, 0);
278     ok(ret == DD_OK, "Expected DD_OK, got %d\n", ret);
279
280     /* Test with valid callback parameter and non-NULL context parameter. */
281     trace("Calling DirectDrawEnumerateExA with empty flags and non-NULL context.\n");
282     ret = pDirectDrawEnumerateExA(test_context_callbackExA, (LPVOID)0xdeadbeef, 0);
283     ok(ret == DD_OK, "Expected DD_OK, got %d\n", ret);
284
285     /* Test with valid callback parameter, NULL context parameter, and all flags set. */
286     trace("Calling DirectDrawEnumerateExA with all flags set and NULL context.\n");
287     ret = pDirectDrawEnumerateExA(test_nullcontext_callbackExA, NULL,
288                                   DDENUM_ATTACHEDSECONDARYDEVICES |
289                                   DDENUM_DETACHEDSECONDARYDEVICES |
290                                   DDENUM_NONDISPLAYDEVICES);
291     ok(ret == DD_OK, "Expected DD_OK, got %d\n", ret);
292 }
293
294 static BOOL WINAPI test_callbackExW(GUID *lpGUID, LPWSTR lpDriverDescription,
295                                     LPWSTR lpDriverName, LPVOID lpContext,
296                                     HMONITOR hm)
297 {
298     ok(0, "The callback should not be invoked by DirectDrawEnumerateExW.\n");
299     return TRUE;
300 }
301
302 static void test_DirectDrawEnumerateExW(void)
303 {
304     HRESULT ret;
305
306     if (!pDirectDrawEnumerateExW)
307     {
308         win_skip("DirectDrawEnumerateExW is not available\n");
309         return;
310     }
311
312     /* DirectDrawEnumerateExW is not implemented on Windows. */
313
314     /* Test with NULL callback parameter. */
315     ret = pDirectDrawEnumerateExW(NULL, NULL, 0);
316     ok(ret == DDERR_UNSUPPORTED, "Expected DDERR_UNSUPPORTED, got %d\n", ret);
317
318     /* Test with invalid callback parameter. */
319     ret = pDirectDrawEnumerateExW((LPDDENUMCALLBACKEXW)0xdeadbeef, NULL, 0);
320     ok(ret == DDERR_UNSUPPORTED, "Expected DDERR_UNSUPPORTED, got %d\n", ret);
321
322     /* Test with valid callback parameter and invalid flags */
323     ret = pDirectDrawEnumerateExW(test_callbackExW, NULL, ~0);
324     ok(ret == DDERR_UNSUPPORTED, "Expected DDERR_UNSUPPORTED, got %d\n", ret);
325
326     /* Test with valid callback parameter and NULL context parameter. */
327     ret = pDirectDrawEnumerateExW(test_callbackExW, NULL, 0);
328     ok(ret == DDERR_UNSUPPORTED, "Expected DDERR_UNSUPPORTED, got %d\n", ret);
329
330     /* Test with valid callback parameter, NULL context parameter, and all flags set. */
331     ret = pDirectDrawEnumerateExW(test_callbackExW, NULL,
332                                   DDENUM_ATTACHEDSECONDARYDEVICES |
333                                   DDENUM_DETACHEDSECONDARYDEVICES |
334                                   DDENUM_NONDISPLAYDEVICES);
335     ok(ret == DDERR_UNSUPPORTED, "Expected DDERR_UNSUPPORTED, got %d\n", ret);
336 }
337
338 static void adddisplaymode(LPDDSURFACEDESC lpddsd)
339 {
340     if (!modes)
341         modes = HeapAlloc(GetProcessHeap(), 0, (modes_size = 2) * sizeof(DDSURFACEDESC));
342     if (modes_cnt == modes_size)
343         modes = HeapReAlloc(GetProcessHeap(), 0, modes, (modes_size *= 2) * sizeof(DDSURFACEDESC));
344     assert(modes);
345     modes[modes_cnt++] = *lpddsd;
346 }
347
348 static void flushdisplaymodes(void)
349 {
350     HeapFree(GetProcessHeap(), 0, modes);
351     modes = 0;
352     modes_cnt = modes_size = 0;
353 }
354
355 static HRESULT WINAPI enummodescallback(LPDDSURFACEDESC lpddsd, LPVOID lpContext)
356 {
357     trace("Width = %i, Height = %i, Refresh Rate = %i, Pitch = %i, flags =%02X\n",
358         lpddsd->dwWidth, lpddsd->dwHeight,
359           U2(*lpddsd).dwRefreshRate, U1(*lpddsd).lPitch, lpddsd->dwFlags);
360
361     /* Check that the pitch is valid if applicable */
362     if(lpddsd->dwFlags & DDSD_PITCH)
363     {
364         ok(U1(*lpddsd).lPitch != 0, "EnumDisplayModes callback with bad pitch\n");
365     }
366
367     /* Check that frequency is valid if applicable
368      *
369      * This fails on some Windows drivers or Windows versions, so it isn't important
370      * apparently
371     if(lpddsd->dwFlags & DDSD_REFRESHRATE)
372     {
373         ok(U2(*lpddsd).dwRefreshRate != 0, "EnumDisplayModes callback with bad refresh rate\n");
374     }
375      */
376
377     adddisplaymode(lpddsd);
378
379     return DDENUMRET_OK;
380 }
381
382 static void enumdisplaymodes(void)
383 {
384     DDSURFACEDESC ddsd;
385     HRESULT rc;
386
387     ZeroMemory(&ddsd, sizeof(DDSURFACEDESC));
388     ddsd.dwSize = sizeof(DDSURFACEDESC);
389     ddsd.dwFlags = DDSD_CAPS;
390     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
391
392     rc = IDirectDraw_EnumDisplayModes(lpDD,
393         DDEDM_STANDARDVGAMODES, &ddsd, 0, enummodescallback);
394     ok(rc==DD_OK || rc==E_INVALIDARG,"EnumDisplayModes returned: %x\n",rc);
395 }
396
397 static void setdisplaymode(int i)
398 {
399     HRESULT rc;
400     RECT orig_rect;
401
402     SetRect(&orig_rect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
403
404     rc = IDirectDraw_SetCooperativeLevel(lpDD,
405         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
406     ok(rc==DD_OK,"SetCooperativeLevel returned: %x\n",rc);
407     if (modes[i].dwFlags & DDSD_PIXELFORMAT)
408     {
409         if (modes[i].ddpfPixelFormat.dwFlags & DDPF_RGB)
410         {
411             rc = IDirectDraw_SetDisplayMode(lpDD,
412                 modes[i].dwWidth, modes[i].dwHeight,
413                 U1(modes[i].ddpfPixelFormat).dwRGBBitCount);
414             ok(DD_OK==rc || DDERR_UNSUPPORTED==rc,"SetDisplayMode returned: %x\n",rc);
415             if (rc == DD_OK)
416             {
417                 RECT r, scrn, test, virt;
418
419                 SetRect(&virt, 0, 0, GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN));
420                 OffsetRect(&virt, GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN));
421                 SetRect(&scrn, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
422                 trace("Mode (%dx%d) [%dx%d] (%d %d)x(%d %d)\n", modes[i].dwWidth, modes[i].dwHeight,
423                       scrn.right, scrn.bottom, virt.left, virt.top, virt.right, virt.bottom);
424                 if (!EqualRect(&scrn, &orig_rect))
425                 {
426                     HRESULT rect_result;
427
428                     /* Check that the client rect was resized */
429                     rc = GetClientRect(hwnd, &test);
430                     ok(rc!=0, "GetClientRect returned %x\n", rc);
431                     rc = EqualRect(&scrn, &test);
432                     todo_wine ok(rc!=0, "Fullscreen window has wrong size\n");
433
434                     /* Check that switching to normal cooperative level
435                        does not restore the display mode */
436                     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_NORMAL);
437                     ok(rc==DD_OK, "SetCooperativeLevel returned %x\n", rc);
438                     SetRect(&test, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
439                     rect_result = EqualRect(&scrn, &test);
440                     ok(rect_result!=0, "Setting cooperative level to DDSCL_NORMAL changed the display mode\n");
441
442                     /* Go back to fullscreen */
443                     rc = IDirectDraw_SetCooperativeLevel(lpDD,
444                         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
445                     ok(rc==DD_OK, "SetCooperativeLevel returned: %x\n",rc);
446
447                     /* If the display mode was changed, set the correct mode
448                        to avoid irrelevant failures */
449                     if (rect_result == 0)
450                     {
451                         rc = IDirectDraw_SetDisplayMode(lpDD,
452                             modes[i].dwWidth, modes[i].dwHeight,
453                             U1(modes[i].ddpfPixelFormat).dwRGBBitCount);
454                         ok(DD_OK==rc, "SetDisplayMode returned: %x\n",rc);
455                     }
456                 }
457                 ok(GetClipCursor(&r), "GetClipCursor() failed\n");
458                 /* ddraw sets clip rect here to the screen size, even for
459                    multiple monitors */
460                 ok(EqualRect(&r, &scrn), "Invalid clip rect: (%d %d) x (%d %d)\n",
461                    r.left, r.top, r.right, r.bottom);
462
463                 ok(ClipCursor(NULL), "ClipCursor() failed\n");
464                 ok(GetClipCursor(&r), "GetClipCursor() failed\n");
465                 ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n",
466                    r.left, r.top, r.right, r.bottom);
467
468                 rc = IDirectDraw_RestoreDisplayMode(lpDD);
469                 ok(DD_OK==rc,"RestoreDisplayMode returned: %x\n",rc);
470             }
471         }
472     }
473 }
474
475 static void createsurface(void)
476 {
477     DDSURFACEDESC ddsd;
478     DDSCAPS ddscaps;
479     HRESULT rc;
480
481     ddsd.dwSize = sizeof(ddsd);
482     ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
483     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
484         DDSCAPS_FLIP |
485         DDSCAPS_COMPLEX;
486     ddsd.dwBackBufferCount = 1;
487     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSPrimary, NULL );
488     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
489     ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
490     rc = IDirectDrawSurface_GetAttachedSurface(lpDDSPrimary, &ddscaps, &lpDDSBack);
491     ok(rc==DD_OK,"GetAttachedSurface returned: %x\n",rc);
492 }
493
494 static void destroysurface(void)
495 {
496     if( lpDDSPrimary != NULL )
497     {
498         IDirectDrawSurface_Release(lpDDSPrimary);
499         lpDDSPrimary = NULL;
500     }
501 }
502
503 static void testsurface(void)
504 {
505     const char* testMsg = "ddraw device context test";
506     HDC hdc;
507     HRESULT rc;
508
509     rc = IDirectDrawSurface_GetDC(lpDDSBack, &hdc);
510     ok(rc==DD_OK, "IDirectDrawSurface_GetDC returned: %x\n",rc);
511     SetBkColor(hdc, RGB(0, 0, 255));
512     SetTextColor(hdc, RGB(255, 255, 0));
513     TextOut(hdc, 0, 0, testMsg, lstrlen(testMsg));
514     IDirectDrawSurface_ReleaseDC(lpDDSBack, hdc);
515     ok(rc==DD_OK, "IDirectDrawSurface_ReleaseDC returned: %x\n",rc);
516
517     while (1)
518     {
519         rc = IDirectDrawSurface_Flip(lpDDSPrimary, NULL, DDFLIP_WAIT);
520         ok(rc==DD_OK || rc==DDERR_SURFACELOST, "IDirectDrawSurface_BltFast returned: %x\n",rc);
521
522         if (rc == DD_OK)
523         {
524             break;
525         }
526         else if (rc == DDERR_SURFACELOST)
527         {
528             rc = IDirectDrawSurface_Restore(lpDDSPrimary);
529             ok(rc==DD_OK, "IDirectDrawSurface_Restore returned: %x\n",rc);
530         }
531     }
532 }
533
534 static void testdisplaymodes(void)
535 {
536     int i;
537
538     for (i = 0; i < modes_cnt; ++i)
539     {
540         setdisplaymode(i);
541         createsurface();
542         testsurface();
543         destroysurface();
544     }
545 }
546
547 static void testcooperativelevels_normal(void)
548 {
549     HRESULT rc;
550     DDSURFACEDESC surfacedesc;
551     IDirectDrawSurface *surface = (IDirectDrawSurface *) 0xdeadbeef;
552
553     memset(&surfacedesc, 0, sizeof(surfacedesc));
554     surfacedesc.dwSize = sizeof(surfacedesc);
555     surfacedesc.ddpfPixelFormat.dwSize = sizeof(surfacedesc.ddpfPixelFormat);
556     surfacedesc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
557     surfacedesc.dwBackBufferCount = 1;
558     surfacedesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
559
560     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_SETFOCUSWINDOW | DDSCL_CREATEDEVICEWINDOW);
561     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW | DDSCL_CREATEDEVICEWINDOW) returned: %x\n",rc);
562
563     /* Do some tests with DDSCL_NORMAL mode */
564     /* Fullscreen mode + normal mode + exclusive mode */
565     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_NORMAL);
566     todo_wine ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_NORMAL) returned: %x\n",rc);
567     /* Try creating a double buffered primary in fullscreen + exclusive + normal mode */
568     rc = IDirectDraw_CreateSurface(lpDD, &surfacedesc, &surface, NULL);
569     if (rc == DDERR_UNSUPPORTEDMODE)
570         skip("Unsupported mode\n");
571     else
572     {
573         todo_wine ok(rc == DD_OK, "IDirectDraw_CreateSurface returned %08x\n", rc);
574         todo_wine ok(surface!=NULL, "Returned NULL surface pointer \n");
575     }
576     if(surface && surface != (IDirectDrawSurface *)0xdeadbeef) IDirectDrawSurface_Release(surface);
577     /* Exclusive mode + normal mode */
578     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_EXCLUSIVE | DDSCL_NORMAL);
579     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_NORMAL) returned: %x\n",rc);
580     /* Fullscreen mode + normal mode */
581     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_FULLSCREEN | DDSCL_NORMAL);
582     todo_wine ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_FULLSCREEN | DDSCL_NORMAL) returned: %x\n",rc);
583     /* Try creating a double buffered primary in fullscreen + normal mode */
584     rc = IDirectDraw_CreateSurface(lpDD, &surfacedesc, &surface, NULL);
585     if (rc == DDERR_UNSUPPORTEDMODE)
586         skip("Unsupported mode\n");
587     else
588     {
589         ok(rc == DDERR_NOEXCLUSIVEMODE, "IDirectDraw_CreateSurface returned %08x\n", rc);
590         ok(surface == NULL, "Returned surface pointer is %p\n", surface);
591     }
592     if(surface && surface != (IDirectDrawSurface *)0xdeadbeef) IDirectDrawSurface_Release(surface);
593     /* Normal mode */
594     rc = IDirectDraw_SetCooperativeLevel(lpDD,
595         hwnd, DDSCL_NORMAL);
596     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL) returned: %x\n",rc);
597
598     /* Try creating a double buffered primary in normal mode */
599     rc = IDirectDraw_CreateSurface(lpDD, &surfacedesc, &surface, NULL);
600     if (rc == DDERR_UNSUPPORTEDMODE)
601         skip("Unsupported mode\n");
602     else
603     {
604         ok(rc == DDERR_NOEXCLUSIVEMODE, "IDirectDraw_CreateSurface returned %08x\n", rc);
605         ok(surface == NULL, "Returned surface pointer is %p\n", surface);
606     }
607     if(surface && surface != (IDirectDrawSurface *)0xdeadbeef) IDirectDrawSurface_Release(surface);
608
609     /* Set the focus window */
610
611     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_SETFOCUSWINDOW | DDSCL_CREATEDEVICEWINDOW);
612     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW | DDSCL_CREATEDEVICEWINDOW) returned: %x\n",rc);
613
614     rc = IDirectDraw_SetCooperativeLevel(lpDD,
615         hwnd, DDSCL_SETFOCUSWINDOW);
616
617     if (rc == DDERR_INVALIDPARAMS)
618     {
619         win_skip("NT4/Win95 do not support cooperative levels DDSCL_SETDEVICEWINDOW and DDSCL_SETFOCUSWINDOW\n");
620         return;
621     }
622
623     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
624
625     /* Set the focus window a second time*/
626     rc = IDirectDraw_SetCooperativeLevel(lpDD,
627         hwnd, DDSCL_SETFOCUSWINDOW);
628     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) the second time returned: %x\n",rc);
629
630     /* Test DDSCL_SETFOCUSWINDOW with the other flags. They should all fail, except of DDSCL_NOWINDOWCHANGES */
631     rc = IDirectDraw_SetCooperativeLevel(lpDD,
632         hwnd, DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW);
633     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
634
635     rc = IDirectDraw_SetCooperativeLevel(lpDD,
636         hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW);
637     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
638
639     /* This one succeeds */
640     rc = IDirectDraw_SetCooperativeLevel(lpDD,
641         hwnd, DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW);
642     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
643
644     rc = IDirectDraw_SetCooperativeLevel(lpDD,
645         hwnd, DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW);
646     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
647
648     rc = IDirectDraw_SetCooperativeLevel(lpDD,
649         hwnd, DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW);
650     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
651
652     rc = IDirectDraw_SetCooperativeLevel(lpDD,
653         hwnd, DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW);
654     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
655
656     rc = IDirectDraw_SetCooperativeLevel(lpDD,
657         hwnd, DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW);
658     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
659
660     rc = IDirectDraw_SetCooperativeLevel(lpDD,
661         hwnd, DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW);
662     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
663
664     /* Set the device window without any other flags. Should give an error */
665     rc = IDirectDraw_SetCooperativeLevel(lpDD,
666         hwnd, DDSCL_SETDEVICEWINDOW);
667     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_SETDEVICEWINDOW) returned: %x\n",rc);
668
669     /* Set device window with DDSCL_NORMAL */
670     rc = IDirectDraw_SetCooperativeLevel(lpDD,
671         hwnd, DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW);
672     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW) returned: %x\n",rc);
673
674     /* Also set the focus window. Should give an error */
675     rc = IDirectDraw_SetCooperativeLevel(lpDD,
676         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW);
677     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
678
679     /* All done */
680 }
681
682 static void testcooperativelevels_exclusive(void)
683 {
684     BOOL success;
685     HRESULT rc;
686     RECT window_rect;
687
688     /* Do some tests with DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN mode */
689
690     /* First, resize the window so it is not the same size as any screen */
691     success = SetWindowPos(hwnd, 0, 0, 0, 281, 92, 0);
692     ok(success, "SetWindowPos failed\n");
693
694     /* Try to set exclusive mode only */
695     rc = IDirectDraw_SetCooperativeLevel(lpDD,
696         hwnd, DDSCL_EXCLUSIVE);
697     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE) returned: %x\n",rc);
698
699     /* Full screen mode only */
700     rc = IDirectDraw_SetCooperativeLevel(lpDD,
701         hwnd, DDSCL_FULLSCREEN);
702     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FULLSCREEN) returned: %x\n",rc);
703
704     /* Full screen mode + exclusive mode */
705     rc = IDirectDraw_SetCooperativeLevel(lpDD,
706         hwnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
707     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN) returned: %x\n",rc);
708     GetClientRect(hwnd, &window_rect);
709     /* rect_before_create is assumed to hold the screen rect */
710     rc = EqualRect(&rect_before_create, &window_rect);
711     todo_wine ok(rc!=0, "Fullscreen window has wrong size\n");
712
713     /* Set the focus window. Should fail */
714     rc = IDirectDraw_SetCooperativeLevel(lpDD,
715         hwnd, DDSCL_SETFOCUSWINDOW);
716     ok(rc==DDERR_HWNDALREADYSET ||
717        broken(rc==DDERR_INVALIDPARAMS) /* NT4/Win95 */,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
718
719
720     /* All done */
721 }
722
723 static void testddraw3(void)
724 {
725     const GUID My_IID_IDirectDraw3 = {
726         0x618f8ad4,
727         0x8b7a,
728         0x11d0,
729         { 0x8f,0xcc,0x0,0xc0,0x4f,0xd9,0x18,0x9d }
730     };
731     IDirectDraw3 *dd3;
732     HRESULT hr;
733     hr = IDirectDraw_QueryInterface(lpDD, &My_IID_IDirectDraw3, (void **) &dd3);
734     ok(hr == E_NOINTERFACE, "QueryInterface for IID_IDirectDraw3 returned 0x%08x, expected E_NOINTERFACE\n", hr);
735     if(SUCCEEDED(hr) && dd3) IDirectDraw3_Release(dd3);
736 }
737
738 static void testddraw7(void)
739 {
740     IDirectDraw7 *dd7;
741     HRESULT hr;
742     DDDEVICEIDENTIFIER2 *pdddi2;
743     DWORD dddi2Bytes;
744     DWORD *pend;
745
746     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
747     if (hr==E_NOINTERFACE)
748     {
749         win_skip("DirectDraw7 is not supported\n");
750         return;
751     }
752     ok(hr==DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", hr);
753
754     if (hr==DD_OK)
755     {
756          dddi2Bytes = FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD);
757
758          pdddi2 = HeapAlloc( GetProcessHeap(), 0, dddi2Bytes + 2*sizeof(DWORD) );
759          pend = (DWORD *)((char *)pdddi2 + dddi2Bytes);
760          pend[0] = 0xdeadbeef;
761          pend[1] = 0xdeadbeef;
762
763          hr = IDirectDraw7_GetDeviceIdentifier(dd7, pdddi2, 0);
764          ok(hr==DD_OK, "get device identifier failed with %08x\n", hr);
765
766          if (hr==DD_OK)
767          {
768              /* check how strings are copied into the structure */
769              ok(pdddi2->szDriver[MAX_DDDEVICEID_STRING - 1]==0, "szDriver not cleared\n");
770              ok(pdddi2->szDescription[MAX_DDDEVICEID_STRING - 1]==0, "szDescription not cleared\n");
771              /* verify that 8 byte structure size alignment will not overwrite memory */
772              ok(pend[0]==0xdeadbeef || broken(pend[0] != 0xdeadbeef), /* win2k */
773                 "memory beyond DDDEVICEIDENTIFIER2 overwritten\n");
774              ok(pend[1]==0xdeadbeef, "memory beyond DDDEVICEIDENTIFIER2 overwritten\n");
775          }
776
777          IDirectDraw_Release(dd7);
778          HeapFree( GetProcessHeap(), 0, pdddi2 );
779     }
780 }
781
782 START_TEST(ddrawmodes)
783 {
784     init_function_pointers();
785
786     createwindow();
787     if (!createdirectdraw())
788         return;
789
790     test_DirectDrawEnumerateA();
791     test_DirectDrawEnumerateW();
792     test_DirectDrawEnumerateExA();
793     test_DirectDrawEnumerateExW();
794
795     enumdisplaymodes();
796     if (winetest_interactive)
797         testdisplaymodes();
798     flushdisplaymodes();
799     testddraw3();
800     testddraw7();
801     releasedirectdraw();
802
803     createdirectdraw();
804     testcooperativelevels_normal();
805     releasedirectdraw();
806
807     createdirectdraw();
808     testcooperativelevels_exclusive();
809     releasedirectdraw();
810 }