kernel32: Moved device-related stubs from psapi to kernel32.
[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, hwnd2;
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 static int modes16bpp_cnt;
43 static int refresh_rate;
44 static int refresh_rate_cnt;
45
46 static HRESULT (WINAPI *pDirectDrawEnumerateA)(LPDDENUMCALLBACKA,LPVOID);
47 static HRESULT (WINAPI *pDirectDrawEnumerateW)(LPDDENUMCALLBACKW,LPVOID);
48 static HRESULT (WINAPI *pDirectDrawEnumerateExA)(LPDDENUMCALLBACKEXA,LPVOID,DWORD);
49 static HRESULT (WINAPI *pDirectDrawEnumerateExW)(LPDDENUMCALLBACKEXW,LPVOID,DWORD);
50
51 static void init_function_pointers(void)
52 {
53     HMODULE hmod = GetModuleHandleA("ddraw.dll");
54     pDirectDrawEnumerateA = (void*)GetProcAddress(hmod, "DirectDrawEnumerateA");
55     pDirectDrawEnumerateW = (void*)GetProcAddress(hmod, "DirectDrawEnumerateW");
56     pDirectDrawEnumerateExA = (void*)GetProcAddress(hmod, "DirectDrawEnumerateExA");
57     pDirectDrawEnumerateExW = (void*)GetProcAddress(hmod, "DirectDrawEnumerateExW");
58 }
59
60 static HWND createwindow(void)
61 {
62     HWND hwnd;
63
64     hwnd = CreateWindowExA(0, "TestWindowClass", "TestWindowClass",
65         WS_POPUP, 0, 0,
66         GetSystemMetrics(SM_CXSCREEN),
67         GetSystemMetrics(SM_CYSCREEN),
68         NULL, NULL, GetModuleHandleA(0), NULL);
69
70     ShowWindow(hwnd, SW_HIDE);
71     UpdateWindow(hwnd);
72     SetFocus(hwnd);
73
74     return hwnd;
75 }
76
77 static BOOL createdirectdraw(void)
78 {
79     HRESULT rc;
80
81     SetRect(&rect_before_create, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
82
83     rc = DirectDrawCreate(NULL, &lpDD, NULL);
84     ok(rc==DD_OK || rc==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %x\n", rc);
85     if (!lpDD) {
86         trace("DirectDrawCreateEx() failed with an error %x\n", rc);
87         return FALSE;
88     }
89     return TRUE;
90 }
91
92
93 static void releasedirectdraw(void)
94 {
95     if( lpDD != NULL )
96     {
97         IDirectDraw_Release(lpDD);
98         lpDD = NULL;
99         SetRect(&rect_after_delete, 0, 0,
100                 GetSystemMetrics(SM_CXSCREEN),
101                 GetSystemMetrics(SM_CYSCREEN));
102         ok(EqualRect(&rect_before_create, &rect_after_delete) != 0,
103            "Original display mode was not restored\n");
104     }
105 }
106
107 static BOOL WINAPI crash_callbackA(GUID *lpGUID, LPSTR lpDriverDescription,
108                                   LPSTR lpDriverName, LPVOID lpContext)
109 {
110     *(volatile char*)0 = 2;
111     return TRUE;
112 }
113
114 static BOOL WINAPI test_nullcontext_callbackA(GUID *lpGUID, LPSTR lpDriverDescription,
115                                               LPSTR lpDriverName, LPVOID lpContext)
116 {
117     trace("test_nullcontext_callbackA: %p %s %s %p\n",
118           lpGUID, lpDriverDescription, lpDriverName, lpContext);
119
120     ok(!lpContext, "Expected NULL lpContext\n");
121
122     return TRUE;
123 }
124
125 static BOOL WINAPI test_context_callbackA(GUID *lpGUID, LPSTR lpDriverDescription,
126                                           LPSTR lpDriverName, LPVOID lpContext)
127 {
128     trace("test_context_callbackA: %p %s %s %p\n",
129           lpGUID, lpDriverDescription, lpDriverName, lpContext);
130
131     ok(lpContext == (LPVOID)0xdeadbeef, "Expected non-NULL lpContext\n");
132
133     return TRUE;
134 }
135
136 static void test_DirectDrawEnumerateA(void)
137 {
138     HRESULT ret;
139
140     if (!pDirectDrawEnumerateA)
141     {
142         win_skip("DirectDrawEnumerateA is not available\n");
143         return;
144     }
145
146     /* Test with NULL callback parameter. */
147     ret = pDirectDrawEnumerateA(NULL, NULL);
148     ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
149
150     /* Test with invalid callback parameter. */
151     ret = pDirectDrawEnumerateA((LPDDENUMCALLBACKA)0xdeadbeef, NULL);
152     ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
153
154     if (pDirectDrawEnumerateExA)
155     {
156         /* Test with callback that crashes. */
157         ret = pDirectDrawEnumerateA(crash_callbackA, NULL);
158         ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
159     }
160     else
161         win_skip("Test would crash on older ddraw\n");
162
163     /* Test with valid callback parameter and NULL context parameter. */
164     trace("Calling DirectDrawEnumerateA with test_nullcontext_callbackA callback and NULL context.\n");
165     ret = pDirectDrawEnumerateA(test_nullcontext_callbackA, NULL);
166     ok(ret == DD_OK, "Expected DD_OK, got %d\n", ret);
167
168     /* Test with valid callback parameter and valid context parameter. */
169     trace("Calling DirectDrawEnumerateA with test_context_callbackA callback and non-NULL context.\n");
170     ret = pDirectDrawEnumerateA(test_context_callbackA, (LPVOID)0xdeadbeef);
171     ok(ret == DD_OK, "Expected DD_OK, got %d\n", ret);
172 }
173
174 static BOOL WINAPI test_callbackW(GUID *lpGUID, LPWSTR lpDriverDescription,
175                                   LPWSTR lpDriverName, LPVOID lpContext)
176 {
177     ok(0, "The callback should not be invoked by DirectDrawEnumerateW\n");
178     return TRUE;
179 }
180
181 static void test_DirectDrawEnumerateW(void)
182 {
183     HRESULT ret;
184
185     if (!pDirectDrawEnumerateW)
186     {
187         win_skip("DirectDrawEnumerateW is not available\n");
188         return;
189     }
190
191     /* DirectDrawEnumerateW is not implemented on Windows. */
192
193     /* Test with NULL callback parameter. */
194     ret = pDirectDrawEnumerateW(NULL, NULL);
195     ok(ret == DDERR_INVALIDPARAMS ||
196        ret == DDERR_UNSUPPORTED, /* Older ddraw */
197        "Expected DDERR_INVALIDPARAMS or DDERR_UNSUPPORTED, got %d\n", ret);
198
199     /* Test with invalid callback parameter. */
200     ret = pDirectDrawEnumerateW((LPDDENUMCALLBACKW)0xdeadbeef, NULL);
201     ok(ret == DDERR_INVALIDPARAMS /* XP */ ||
202        ret == DDERR_UNSUPPORTED /* Win7 */,
203        "Expected DDERR_INVALIDPARAMS or DDERR_UNSUPPORTED, got %d\n", ret);
204
205     /* Test with valid callback parameter and NULL context parameter. */
206     ret = pDirectDrawEnumerateW(test_callbackW, NULL);
207     ok(ret == DDERR_UNSUPPORTED, "Expected DDERR_UNSUPPORTED, got %d\n", ret);
208 }
209
210 static BOOL WINAPI crash_callbackExA(GUID *lpGUID, LPSTR lpDriverDescription,
211                                      LPSTR lpDriverName, LPVOID lpContext,
212                                      HMONITOR hm)
213 {
214     *(volatile char*)0 = 2;
215     return TRUE;
216 }
217
218 static BOOL WINAPI test_nullcontext_callbackExA(GUID *lpGUID, LPSTR lpDriverDescription,
219                                                 LPSTR lpDriverName, LPVOID lpContext,
220                                                 HMONITOR hm)
221 {
222     trace("test_nullcontext_callbackExA: %p %s %s %p %p\n", lpGUID,
223           lpDriverDescription, lpDriverName, lpContext, hm);
224
225     ok(!lpContext, "Expected NULL lpContext\n");
226
227     return TRUE;
228 }
229
230 static BOOL WINAPI test_context_callbackExA(GUID *lpGUID, LPSTR lpDriverDescription,
231                                             LPSTR lpDriverName, LPVOID lpContext,
232                                             HMONITOR hm)
233 {
234     trace("test_context_callbackExA: %p %s %s %p %p\n", lpGUID,
235           lpDriverDescription, lpDriverName, lpContext, hm);
236
237     ok(lpContext == (LPVOID)0xdeadbeef, "Expected non-NULL lpContext\n");
238
239     return TRUE;
240 }
241
242 static void test_DirectDrawEnumerateExA(void)
243 {
244     HRESULT ret;
245
246     if (!pDirectDrawEnumerateExA)
247     {
248         win_skip("DirectDrawEnumerateExA is not available\n");
249         return;
250     }
251
252     /* Test with NULL callback parameter. */
253     ret = pDirectDrawEnumerateExA(NULL, NULL, 0);
254     ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
255
256     /* Test with invalid callback parameter. */
257     ret = pDirectDrawEnumerateExA((LPDDENUMCALLBACKEXA)0xdeadbeef, NULL, 0);
258     ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
259
260     /* Test with callback that crashes. */
261     ret = pDirectDrawEnumerateExA(crash_callbackExA, NULL, 0);
262     ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
263
264     /* Test with valid callback parameter and invalid flags */
265     ret = pDirectDrawEnumerateExA(test_nullcontext_callbackExA, NULL, ~0);
266     ok(ret == DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, got %d\n", ret);
267
268     /* Test with valid callback parameter and NULL context parameter. */
269     trace("Calling DirectDrawEnumerateExA with empty flags and NULL context.\n");
270     ret = pDirectDrawEnumerateExA(test_nullcontext_callbackExA, NULL, 0);
271     ok(ret == DD_OK, "Expected DD_OK, got %d\n", ret);
272
273     /* Test with valid callback parameter and non-NULL context parameter. */
274     trace("Calling DirectDrawEnumerateExA with empty flags and non-NULL context.\n");
275     ret = pDirectDrawEnumerateExA(test_context_callbackExA, (LPVOID)0xdeadbeef, 0);
276     ok(ret == DD_OK, "Expected DD_OK, got %d\n", ret);
277
278     /* Test with valid callback parameter, NULL context parameter, and all flags set. */
279     trace("Calling DirectDrawEnumerateExA with all flags set and NULL context.\n");
280     ret = pDirectDrawEnumerateExA(test_nullcontext_callbackExA, NULL,
281                                   DDENUM_ATTACHEDSECONDARYDEVICES |
282                                   DDENUM_DETACHEDSECONDARYDEVICES |
283                                   DDENUM_NONDISPLAYDEVICES);
284     ok(ret == DD_OK, "Expected DD_OK, got %d\n", ret);
285 }
286
287 static BOOL WINAPI test_callbackExW(GUID *lpGUID, LPWSTR lpDriverDescription,
288                                     LPWSTR lpDriverName, LPVOID lpContext,
289                                     HMONITOR hm)
290 {
291     ok(0, "The callback should not be invoked by DirectDrawEnumerateExW.\n");
292     return TRUE;
293 }
294
295 static void test_DirectDrawEnumerateExW(void)
296 {
297     HRESULT ret;
298
299     if (!pDirectDrawEnumerateExW)
300     {
301         win_skip("DirectDrawEnumerateExW is not available\n");
302         return;
303     }
304
305     /* DirectDrawEnumerateExW is not implemented on Windows. */
306
307     /* Test with NULL callback parameter. */
308     ret = pDirectDrawEnumerateExW(NULL, NULL, 0);
309     ok(ret == DDERR_UNSUPPORTED, "Expected DDERR_UNSUPPORTED, got %d\n", ret);
310
311     /* Test with invalid callback parameter. */
312     ret = pDirectDrawEnumerateExW((LPDDENUMCALLBACKEXW)0xdeadbeef, NULL, 0);
313     ok(ret == DDERR_UNSUPPORTED, "Expected DDERR_UNSUPPORTED, got %d\n", ret);
314
315     /* Test with valid callback parameter and invalid flags */
316     ret = pDirectDrawEnumerateExW(test_callbackExW, NULL, ~0);
317     ok(ret == DDERR_UNSUPPORTED, "Expected DDERR_UNSUPPORTED, got %d\n", ret);
318
319     /* Test with valid callback parameter and NULL context parameter. */
320     ret = pDirectDrawEnumerateExW(test_callbackExW, NULL, 0);
321     ok(ret == DDERR_UNSUPPORTED, "Expected DDERR_UNSUPPORTED, got %d\n", ret);
322
323     /* Test with valid callback parameter, NULL context parameter, and all flags set. */
324     ret = pDirectDrawEnumerateExW(test_callbackExW, NULL,
325                                   DDENUM_ATTACHEDSECONDARYDEVICES |
326                                   DDENUM_DETACHEDSECONDARYDEVICES |
327                                   DDENUM_NONDISPLAYDEVICES);
328     ok(ret == DDERR_UNSUPPORTED, "Expected DDERR_UNSUPPORTED, got %d\n", ret);
329 }
330
331 static void adddisplaymode(LPDDSURFACEDESC lpddsd)
332 {
333     if (!modes)
334         modes = HeapAlloc(GetProcessHeap(), 0, (modes_size = 2) * sizeof(DDSURFACEDESC));
335     if (modes_cnt == modes_size)
336         modes = HeapReAlloc(GetProcessHeap(), 0, modes, (modes_size *= 2) * sizeof(DDSURFACEDESC));
337     assert(modes);
338     modes[modes_cnt++] = *lpddsd;
339 }
340
341 static void flushdisplaymodes(void)
342 {
343     HeapFree(GetProcessHeap(), 0, modes);
344     modes = 0;
345     modes_cnt = modes_size = 0;
346 }
347
348 static HRESULT WINAPI enummodescallback(LPDDSURFACEDESC lpddsd, LPVOID lpContext)
349 {
350     trace("Width = %i, Height = %i, bpp = %i, Refresh Rate = %i, Pitch = %i, flags =%02X\n",
351         lpddsd->dwWidth, lpddsd->dwHeight, U1(lpddsd->ddpfPixelFormat).dwRGBBitCount,
352           U2(*lpddsd).dwRefreshRate, U1(*lpddsd).lPitch, lpddsd->dwFlags);
353
354     /* Check that the pitch is valid if applicable */
355     if(lpddsd->dwFlags & DDSD_PITCH)
356     {
357         ok(U1(*lpddsd).lPitch != 0, "EnumDisplayModes callback with bad pitch\n");
358     }
359
360     /* Check that frequency is valid if applicable
361      *
362      * This fails on some Windows drivers or Windows versions, so it isn't important
363      * apparently
364     if(lpddsd->dwFlags & DDSD_REFRESHRATE)
365     {
366         ok(U2(*lpddsd).dwRefreshRate != 0, "EnumDisplayModes callback with bad refresh rate\n");
367     }
368      */
369
370     adddisplaymode(lpddsd);
371     if(U1(lpddsd->ddpfPixelFormat).dwRGBBitCount == 16)
372         modes16bpp_cnt++;
373
374     return DDENUMRET_OK;
375 }
376
377 static HRESULT WINAPI enummodescallback_16bit(LPDDSURFACEDESC lpddsd, LPVOID lpContext)
378 {
379     trace("Width = %i, Height = %i, bpp = %i, Refresh Rate = %i, Pitch = %i, flags =%02X\n",
380         lpddsd->dwWidth, lpddsd->dwHeight, U1(lpddsd->ddpfPixelFormat).dwRGBBitCount,
381           U2(*lpddsd).dwRefreshRate, U1(*lpddsd).lPitch, lpddsd->dwFlags);
382
383     ok(lpddsd->dwFlags == (DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE),
384             "Wrong surface description flags %02X\n", lpddsd->dwFlags);
385     ok(lpddsd->ddpfPixelFormat.dwFlags == DDPF_RGB, "Wrong pixel format flag %02X\n",
386             lpddsd->ddpfPixelFormat.dwFlags);
387     ok(U1(lpddsd->ddpfPixelFormat).dwRGBBitCount == 16, "Expected 16 bpp got %i\n",
388             U1(lpddsd->ddpfPixelFormat).dwRGBBitCount);
389
390     /* Check that the pitch is valid if applicable */
391     if(lpddsd->dwFlags & DDSD_PITCH)
392     {
393         ok(U1(*lpddsd).lPitch != 0, "EnumDisplayModes callback with bad pitch\n");
394     }
395
396     if(!refresh_rate)
397     {
398         if(U2(*lpddsd).dwRefreshRate )
399         {
400             refresh_rate = U2(*lpddsd).dwRefreshRate;
401             refresh_rate_cnt++;
402         }
403     }
404     else
405     {
406         if(refresh_rate == U2(*lpddsd).dwRefreshRate)
407             refresh_rate_cnt++;
408     }
409
410     modes16bpp_cnt++;
411
412     return DDENUMRET_OK;
413 }
414
415 static HRESULT WINAPI enummodescallback_count(LPDDSURFACEDESC lpddsd, LPVOID lpContext)
416 {
417     ok(lpddsd->dwFlags == (DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_REFRESHRATE),
418             "Wrong surface description flags %02X\n", lpddsd->dwFlags);
419
420     modes16bpp_cnt++;
421
422     return DDENUMRET_OK;
423 }
424
425 static void enumdisplaymodes(void)
426 {
427     DDSURFACEDESC ddsd;
428     HRESULT rc;
429     int count, refresh_count;
430
431     ZeroMemory(&ddsd, sizeof(DDSURFACEDESC));
432     ddsd.dwSize = sizeof(DDSURFACEDESC);
433     ddsd.dwFlags = DDSD_CAPS;
434     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
435
436     /* Flags parameter is reserved in very old ddraw versions (3 and older?) and must be 0 */
437     rc = IDirectDraw_EnumDisplayModes(lpDD, 0, &ddsd, 0, enummodescallback);
438     ok(rc==DD_OK, "EnumDisplayModes returned: %x\n",rc);
439
440     count = modes16bpp_cnt;
441
442     modes16bpp_cnt = 0;
443     ddsd.dwFlags = DDSD_PIXELFORMAT;
444     ddsd.ddpfPixelFormat.dwFlags = DDPF_RGB;
445     U1(ddsd.ddpfPixelFormat).dwRGBBitCount = 16;
446     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x7C00;
447     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x03E0;
448     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x001F;
449
450     rc = IDirectDraw_EnumDisplayModes(lpDD, 0, &ddsd, 0, enummodescallback_16bit);
451     ok(rc==DD_OK, "EnumDisplayModes returned: %x\n",rc);
452     ok(modes16bpp_cnt == count, "Expected %d modes got %d\n", count, modes16bpp_cnt);
453
454     modes16bpp_cnt = 0;
455     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0x0000;
456     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0000;
457     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x0000;
458
459     rc = IDirectDraw_EnumDisplayModes(lpDD, 0, &ddsd, 0, enummodescallback_16bit);
460     ok(rc==DD_OK, "EnumDisplayModes returned: %x\n",rc);
461     ok(modes16bpp_cnt == count, "Expected %d modes got %d\n", count, modes16bpp_cnt);
462
463     modes16bpp_cnt = 0;
464     U2(ddsd.ddpfPixelFormat).dwRBitMask = 0xF0F0;
465     U3(ddsd.ddpfPixelFormat).dwGBitMask = 0x0F00;
466     U4(ddsd.ddpfPixelFormat).dwBBitMask = 0x000F;
467
468     rc = IDirectDraw_EnumDisplayModes(lpDD, 0, &ddsd, 0, enummodescallback_16bit);
469     ok(rc==DD_OK, "EnumDisplayModes returned: %x\n",rc);
470     ok(modes16bpp_cnt == count, "Expected %d modes got %d\n", count, modes16bpp_cnt);
471
472
473     modes16bpp_cnt = 0;
474     ddsd.ddpfPixelFormat.dwFlags = DDPF_YUV;
475
476     rc = IDirectDraw_EnumDisplayModes(lpDD, 0, &ddsd, 0, enummodescallback_16bit);
477     ok(rc==DD_OK,"EnumDisplayModes returned: %x\n",rc);
478     ok(modes16bpp_cnt == count, "Expected %d modes got %d\n", count, modes16bpp_cnt);
479
480     modes16bpp_cnt = 0;
481     ddsd.ddpfPixelFormat.dwFlags = DDPF_PALETTEINDEXED8;
482
483     rc = IDirectDraw_EnumDisplayModes(lpDD, 0, &ddsd, 0, enummodescallback_16bit);
484     ok(rc==DD_OK,"EnumDisplayModes returned: %x\n",rc);
485     ok(modes16bpp_cnt == count, "Expected %d modes got %d\n", count, modes16bpp_cnt);
486
487     modes16bpp_cnt = 0;
488     ddsd.dwFlags = DDSD_PIXELFORMAT;
489     ddsd.ddpfPixelFormat.dwFlags = 0;
490
491     rc = IDirectDraw_EnumDisplayModes(lpDD, 0, &ddsd, 0, enummodescallback_16bit);
492     ok(rc==DD_OK,"EnumDisplayModes returned: %x\n",rc);
493     ok(modes16bpp_cnt == count, "Expected %d modes got %d\n", count, modes16bpp_cnt);
494
495     modes16bpp_cnt = 0;
496     ddsd.dwFlags = 0;
497
498     rc = IDirectDraw_EnumDisplayModes(lpDD, 0, &ddsd, 0, enummodescallback_count);
499     ok(rc==DD_OK,"EnumDisplayModes returned: %x\n",rc);
500     ok(modes16bpp_cnt == modes_cnt, "Expected %d modes got %d\n", modes_cnt, modes16bpp_cnt);
501
502     modes16bpp_cnt = 0;
503     ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_PITCH;
504     U1(ddsd).lPitch = 123;
505
506     rc = IDirectDraw_EnumDisplayModes(lpDD, 0, &ddsd, 0, enummodescallback_16bit);
507     ok(rc==DD_OK,"EnumDisplayModes returned: %x\n",rc);
508     ok(modes16bpp_cnt == count, "Expected %d modes got %d\n", count, modes16bpp_cnt);
509
510     modes16bpp_cnt = 0;
511     ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_REFRESHRATE;
512     U2(ddsd).dwRefreshRate = 1;
513
514     rc = IDirectDraw_EnumDisplayModes(lpDD, 0, &ddsd, 0, enummodescallback_16bit);
515     ok(rc==DD_OK,"EnumDisplayModes returned: %x\n",rc);
516     ok(modes16bpp_cnt == 0, "Expected 0 modes got %d\n", modes16bpp_cnt);
517
518     modes16bpp_cnt = 0;
519     ddsd.dwFlags = DDSD_PIXELFORMAT;
520
521     rc = IDirectDraw_EnumDisplayModes(lpDD, DDEDM_REFRESHRATES, &ddsd, 0, enummodescallback_16bit);
522     if(rc == DDERR_INVALIDPARAMS)
523     {
524         skip("Ddraw version too old. Skipping.\n");
525         return;
526     }
527     ok(rc==DD_OK,"EnumDisplayModes returned: %x\n",rc);
528     refresh_count = refresh_rate_cnt;
529
530     if(refresh_rate)
531     {
532         modes16bpp_cnt = 0;
533         ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_REFRESHRATE;
534         U2(ddsd).dwRefreshRate = refresh_rate;
535
536         rc = IDirectDraw_EnumDisplayModes(lpDD, 0, &ddsd, 0, enummodescallback_16bit);
537         ok(rc==DD_OK,"EnumDisplayModes returned: %x\n",rc);
538         ok(modes16bpp_cnt == refresh_count, "Expected %d modes got %d\n", refresh_count, modes16bpp_cnt);
539     }
540 }
541
542 static void setdisplaymode(int i)
543 {
544     HRESULT rc;
545     RECT orig_rect;
546
547     SetRect(&orig_rect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
548
549     rc = IDirectDraw_SetCooperativeLevel(lpDD,
550         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
551     ok(rc==DD_OK,"SetCooperativeLevel returned: %x\n",rc);
552     if (modes[i].dwFlags & DDSD_PIXELFORMAT)
553     {
554         if (modes[i].ddpfPixelFormat.dwFlags & DDPF_RGB)
555         {
556             rc = IDirectDraw_SetDisplayMode(lpDD,
557                 modes[i].dwWidth, modes[i].dwHeight,
558                 U1(modes[i].ddpfPixelFormat).dwRGBBitCount);
559             ok(DD_OK==rc || DDERR_UNSUPPORTED==rc,"SetDisplayMode returned: %x\n",rc);
560             if (rc == DD_OK)
561             {
562                 RECT r, scrn, test, virt;
563
564                 SetRect(&virt, 0, 0, GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN));
565                 OffsetRect(&virt, GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN));
566                 SetRect(&scrn, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
567                 trace("Mode (%dx%d) [%dx%d] (%d %d)x(%d %d)\n", modes[i].dwWidth, modes[i].dwHeight,
568                       scrn.right, scrn.bottom, virt.left, virt.top, virt.right, virt.bottom);
569                 if (!EqualRect(&scrn, &orig_rect))
570                 {
571                     HRESULT rect_result;
572
573                     /* Check that the client rect was resized */
574                     rc = GetClientRect(hwnd, &test);
575                     ok(rc!=0, "GetClientRect returned %x\n", rc);
576                     rc = EqualRect(&scrn, &test);
577                     todo_wine ok(rc!=0, "Fullscreen window has wrong size\n");
578
579                     /* Check that switching to normal cooperative level
580                        does not restore the display mode */
581                     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_NORMAL);
582                     ok(rc==DD_OK, "SetCooperativeLevel returned %x\n", rc);
583                     SetRect(&test, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
584                     rect_result = EqualRect(&scrn, &test);
585                     ok(rect_result!=0, "Setting cooperative level to DDSCL_NORMAL changed the display mode\n");
586
587                     /* Go back to fullscreen */
588                     rc = IDirectDraw_SetCooperativeLevel(lpDD,
589                         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
590                     ok(rc==DD_OK, "SetCooperativeLevel returned: %x\n",rc);
591
592                     /* If the display mode was changed, set the correct mode
593                        to avoid irrelevant failures */
594                     if (rect_result == 0)
595                     {
596                         rc = IDirectDraw_SetDisplayMode(lpDD,
597                             modes[i].dwWidth, modes[i].dwHeight,
598                             U1(modes[i].ddpfPixelFormat).dwRGBBitCount);
599                         ok(DD_OK==rc, "SetDisplayMode returned: %x\n",rc);
600                     }
601                 }
602                 ok(GetClipCursor(&r), "GetClipCursor() failed\n");
603                 /* ddraw sets clip rect here to the screen size, even for
604                    multiple monitors */
605                 ok(EqualRect(&r, &scrn), "Invalid clip rect: (%d %d) x (%d %d)\n",
606                    r.left, r.top, r.right, r.bottom);
607
608                 ok(ClipCursor(NULL), "ClipCursor() failed\n");
609                 ok(GetClipCursor(&r), "GetClipCursor() failed\n");
610                 ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n",
611                    r.left, r.top, r.right, r.bottom);
612
613                 rc = IDirectDraw_RestoreDisplayMode(lpDD);
614                 ok(DD_OK==rc,"RestoreDisplayMode returned: %x\n",rc);
615             }
616         }
617     }
618 }
619
620 static void createsurface(void)
621 {
622     DDSURFACEDESC ddsd;
623     DDSCAPS ddscaps;
624     HRESULT rc;
625
626     ddsd.dwSize = sizeof(ddsd);
627     ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
628     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
629         DDSCAPS_FLIP |
630         DDSCAPS_COMPLEX;
631     ddsd.dwBackBufferCount = 1;
632     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSPrimary, NULL );
633     ok(rc==DD_OK,"CreateSurface returned: %x\n",rc);
634     ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
635     rc = IDirectDrawSurface_GetAttachedSurface(lpDDSPrimary, &ddscaps, &lpDDSBack);
636     ok(rc==DD_OK,"GetAttachedSurface returned: %x\n",rc);
637 }
638
639 static void destroysurface(void)
640 {
641     if( lpDDSPrimary != NULL )
642     {
643         IDirectDrawSurface_Release(lpDDSPrimary);
644         lpDDSPrimary = NULL;
645     }
646 }
647
648 static void testsurface(void)
649 {
650     const char* testMsg = "ddraw device context test";
651     HDC hdc;
652     HRESULT rc;
653
654     rc = IDirectDrawSurface_GetDC(lpDDSBack, &hdc);
655     ok(rc==DD_OK, "IDirectDrawSurface_GetDC returned: %x\n",rc);
656     SetBkColor(hdc, RGB(0, 0, 255));
657     SetTextColor(hdc, RGB(255, 255, 0));
658     TextOut(hdc, 0, 0, testMsg, lstrlen(testMsg));
659     IDirectDrawSurface_ReleaseDC(lpDDSBack, hdc);
660     ok(rc==DD_OK, "IDirectDrawSurface_ReleaseDC returned: %x\n",rc);
661
662     while (1)
663     {
664         rc = IDirectDrawSurface_Flip(lpDDSPrimary, NULL, DDFLIP_WAIT);
665         ok(rc==DD_OK || rc==DDERR_SURFACELOST, "IDirectDrawSurface_BltFast returned: %x\n",rc);
666
667         if (rc == DD_OK)
668         {
669             break;
670         }
671         else if (rc == DDERR_SURFACELOST)
672         {
673             rc = IDirectDrawSurface_Restore(lpDDSPrimary);
674             ok(rc==DD_OK, "IDirectDrawSurface_Restore returned: %x\n",rc);
675         }
676     }
677 }
678
679 static void testdisplaymodes(void)
680 {
681     int i;
682
683     for (i = 0; i < modes_cnt; ++i)
684     {
685         setdisplaymode(i);
686         createsurface();
687         testsurface();
688         destroysurface();
689     }
690 }
691
692 static void testcooperativelevels_normal(void)
693 {
694     BOOL sfw;
695     HRESULT rc;
696     DDSURFACEDESC surfacedesc;
697     IDirectDrawSurface *surface = (IDirectDrawSurface *) 0xdeadbeef;
698
699     memset(&surfacedesc, 0, sizeof(surfacedesc));
700     surfacedesc.dwSize = sizeof(surfacedesc);
701     surfacedesc.ddpfPixelFormat.dwSize = sizeof(surfacedesc.ddpfPixelFormat);
702     surfacedesc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
703     surfacedesc.dwBackBufferCount = 1;
704     surfacedesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
705
706     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_SETFOCUSWINDOW | DDSCL_CREATEDEVICEWINDOW);
707     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW | DDSCL_CREATEDEVICEWINDOW) returned: %x\n",rc);
708
709     /* Do some tests with DDSCL_NORMAL mode */
710
711     /* Fullscreen mode + normal mode + exclusive mode */
712
713     rc = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_NORMAL);
714     ok(rc==DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, received: %x\n", rc);
715
716     sfw=FALSE;
717     if(hwnd2)
718         sfw=SetForegroundWindow(hwnd2);
719     else
720         skip("Failed to create the second window\n");
721
722     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_NORMAL);
723     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_NORMAL) returned: %x\n",rc);
724
725     if(sfw)
726         ok(GetForegroundWindow()==hwnd,"Expected the main windows (%p) for foreground, received the second one (%p)\n",hwnd, hwnd2);
727
728     /* Try creating a double buffered primary in fullscreen + exclusive + normal mode */
729     rc = IDirectDraw_CreateSurface(lpDD, &surfacedesc, &surface, NULL);
730
731     if (rc == DDERR_UNSUPPORTEDMODE)
732         skip("Unsupported mode\n");
733     else
734     {
735         ok(rc == DD_OK, "IDirectDraw_CreateSurface returned %08x\n", rc);
736         ok(surface!=NULL, "Returned NULL surface pointer\n");
737     }
738     if(surface && surface != (IDirectDrawSurface *)0xdeadbeef) IDirectDrawSurface_Release(surface);
739
740     /* Exclusive mode + normal mode */
741     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_EXCLUSIVE | DDSCL_NORMAL);
742     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_NORMAL) returned: %x\n",rc);
743
744     /* Fullscreen mode + normal mode */
745
746     sfw=FALSE;
747     if(hwnd2) sfw=SetForegroundWindow(hwnd2);
748
749     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_FULLSCREEN | DDSCL_NORMAL);
750     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_FULLSCREEN | DDSCL_NORMAL) returned: %x\n",rc);
751
752     if(sfw)
753         ok(GetForegroundWindow()==hwnd2,"Expected the second windows (%p) for foreground, received the main one (%p)\n",hwnd2, hwnd);
754
755     rc = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_FULLSCREEN | DDSCL_NORMAL);
756     ok(rc==DD_OK, "Expected DD_OK, received %x\n", rc);
757
758     /* Try creating a double buffered primary in fullscreen + normal mode */
759     rc = IDirectDraw_CreateSurface(lpDD, &surfacedesc, &surface, NULL);
760     if (rc == DDERR_UNSUPPORTEDMODE)
761         skip("Unsupported mode\n");
762     else
763     {
764         ok(rc == DDERR_NOEXCLUSIVEMODE, "IDirectDraw_CreateSurface returned %08x\n", rc);
765         ok(surface == NULL, "Returned surface pointer is %p\n", surface);
766     }
767
768     if(surface && surface != (IDirectDrawSurface *)0xdeadbeef) IDirectDrawSurface_Release(surface);
769
770     /* switching from Fullscreen mode to Normal mode */
771
772     sfw=FALSE;
773     if(hwnd2) sfw=SetForegroundWindow(hwnd2);
774
775     rc = IDirectDraw_SetCooperativeLevel(lpDD,
776         hwnd, DDSCL_NORMAL);
777     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL) returned: %x\n",rc);
778
779     if(sfw)
780         ok(GetForegroundWindow()==hwnd2,"Expected the second windows (%p) for foreground, received the main one (%p)\n",hwnd2, hwnd);
781
782     rc = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL);
783     ok(rc==DD_OK, "Expected DD_OK, received %x\n", rc);
784
785     /* Try creating a double buffered primary in normal mode */
786     rc = IDirectDraw_CreateSurface(lpDD, &surfacedesc, &surface, NULL);
787     if (rc == DDERR_UNSUPPORTEDMODE)
788         skip("Unsupported mode\n");
789     else
790     {
791         ok(rc == DDERR_NOEXCLUSIVEMODE, "IDirectDraw_CreateSurface returned %08x\n", rc);
792         ok(surface == NULL, "Returned surface pointer is %p\n", surface);
793     }
794     if(surface && surface != (IDirectDrawSurface *)0xdeadbeef) IDirectDrawSurface_Release(surface);
795
796     /* switching from Normal mode to Fullscreen + Normal mode */
797
798     sfw=FALSE;
799     if(hwnd2) sfw=SetForegroundWindow(hwnd2);
800
801     rc = IDirectDraw_SetCooperativeLevel(lpDD,
802         hwnd, DDSCL_NORMAL | DDSCL_FULLSCREEN);
803     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL | FULLSCREEN) returned: %x\n",rc);
804
805     if(sfw)
806         ok(GetForegroundWindow()==hwnd2,"Expected the second windows (%p) for foreground, received the main one (%p)\n",hwnd2, hwnd);
807
808     rc = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_NORMAL | DDSCL_FULLSCREEN);
809     ok(rc==DD_OK, "Expected DD_OK, received %x\n", rc);
810
811     /* Set the focus window */
812
813     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_SETFOCUSWINDOW | DDSCL_CREATEDEVICEWINDOW);
814     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW | DDSCL_CREATEDEVICEWINDOW) returned: %x\n",rc);
815
816     rc = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_SETFOCUSWINDOW);
817
818     if (rc == DDERR_INVALIDPARAMS)
819     {
820         win_skip("NT4/Win95 do not support cooperative levels DDSCL_SETDEVICEWINDOW and DDSCL_SETFOCUSWINDOW\n");
821         return;
822     }
823
824     ok(rc==DD_OK, "Expected DD_OK, received %x\n", rc);
825
826     rc = IDirectDraw_SetCooperativeLevel(lpDD,
827         hwnd, DDSCL_SETFOCUSWINDOW);
828
829     if (rc == DDERR_INVALIDPARAMS)
830     {
831         win_skip("NT4/Win95 do not support cooperative levels DDSCL_SETDEVICEWINDOW and DDSCL_SETFOCUSWINDOW\n");
832         return;
833     }
834
835     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
836
837     /* Set the focus window a second time*/
838     rc = IDirectDraw_SetCooperativeLevel(lpDD,
839         hwnd, DDSCL_SETFOCUSWINDOW);
840     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) the second time returned: %x\n",rc);
841
842     /* Test DDSCL_SETFOCUSWINDOW with the other flags. They should all fail, except of DDSCL_NOWINDOWCHANGES */
843     rc = IDirectDraw_SetCooperativeLevel(lpDD,
844         hwnd, DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW);
845     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
846
847     rc = IDirectDraw_SetCooperativeLevel(lpDD,
848         hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW);
849     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
850
851     /* This one succeeds */
852     rc = IDirectDraw_SetCooperativeLevel(lpDD,
853         hwnd, DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW);
854     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
855
856     rc = IDirectDraw_SetCooperativeLevel(lpDD,
857         hwnd, DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW);
858     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
859
860     rc = IDirectDraw_SetCooperativeLevel(lpDD,
861         hwnd, DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW);
862     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
863
864     rc = IDirectDraw_SetCooperativeLevel(lpDD,
865         hwnd, DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW);
866     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
867
868     rc = IDirectDraw_SetCooperativeLevel(lpDD,
869         hwnd, DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW);
870     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
871
872     rc = IDirectDraw_SetCooperativeLevel(lpDD,
873         hwnd, DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW);
874     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
875
876     /* Set the device window without any other flags. Should give an error */
877     rc = IDirectDraw_SetCooperativeLevel(lpDD,
878         hwnd, DDSCL_SETDEVICEWINDOW);
879     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_SETDEVICEWINDOW) returned: %x\n",rc);
880
881     /* Set device window with DDSCL_NORMAL */
882     rc = IDirectDraw_SetCooperativeLevel(lpDD,
883         hwnd, DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW);
884     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW) returned: %x\n",rc);
885
886     /* Also set the focus window. Should give an error */
887     rc = IDirectDraw_SetCooperativeLevel(lpDD,
888         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW);
889     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
890
891     /* All done */
892 }
893
894 static void testcooperativelevels_exclusive(void)
895 {
896     BOOL sfw, success;
897     HRESULT rc;
898     RECT window_rect;
899
900     /* Do some tests with DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN mode */
901
902     /* First, resize the window so it is not the same size as any screen */
903     success = SetWindowPos(hwnd, 0, 0, 0, 281, 92, 0);
904     ok(success, "SetWindowPos failed\n");
905
906     /* Try to set exclusive mode only */
907     rc = IDirectDraw_SetCooperativeLevel(lpDD,
908         hwnd, DDSCL_EXCLUSIVE);
909     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE) returned: %x\n",rc);
910
911     /* Full screen mode only */
912     rc = IDirectDraw_SetCooperativeLevel(lpDD,
913         hwnd, DDSCL_FULLSCREEN);
914     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FULLSCREEN) returned: %x\n",rc);
915
916     /* Full screen mode + exclusive mode */
917
918     rc = IDirectDraw_SetCooperativeLevel(lpDD, NULL, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
919     ok(rc==DDERR_INVALIDPARAMS, "Expected DDERR_INVALIDPARAMS, received %x\n", rc);
920
921     sfw=FALSE;
922     if(hwnd2)
923         sfw=SetForegroundWindow(hwnd2);
924     else
925         skip("Failed to create the second window\n");
926
927     rc = IDirectDraw_SetCooperativeLevel(lpDD,
928         hwnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
929     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN) returned: %x\n",rc);
930
931     if(sfw)
932         ok(GetForegroundWindow()==hwnd,"Expected the main windows (%p) for foreground, received the second one (%p)\n",hwnd, hwnd2);
933
934     /* rect_before_create is assumed to hold the screen rect */
935     GetClientRect(hwnd, &window_rect);
936     rc = EqualRect(&rect_before_create, &window_rect);
937     ok(rc, "Fullscreen window has wrong size.\n");
938
939     /* Set the focus window. Should fail */
940     rc = IDirectDraw_SetCooperativeLevel(lpDD,
941         hwnd, DDSCL_SETFOCUSWINDOW);
942     ok(rc==DDERR_HWNDALREADYSET ||
943        broken(rc==DDERR_INVALIDPARAMS) /* NT4/Win95 */,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
944
945
946     /* All done */
947 }
948
949 static void testddraw3(void)
950 {
951     const GUID My_IID_IDirectDraw3 = {
952         0x618f8ad4,
953         0x8b7a,
954         0x11d0,
955         { 0x8f,0xcc,0x0,0xc0,0x4f,0xd9,0x18,0x9d }
956     };
957     IDirectDraw3 *dd3;
958     HRESULT hr;
959     hr = IDirectDraw_QueryInterface(lpDD, &My_IID_IDirectDraw3, (void **) &dd3);
960     ok(hr == E_NOINTERFACE, "QueryInterface for IID_IDirectDraw3 returned 0x%08x, expected E_NOINTERFACE\n", hr);
961     if(SUCCEEDED(hr) && dd3) IDirectDraw3_Release(dd3);
962 }
963
964 static void testddraw7(void)
965 {
966     IDirectDraw7 *dd7;
967     HRESULT hr;
968     DDDEVICEIDENTIFIER2 *pdddi2;
969     DWORD dddi2Bytes;
970     DWORD *pend;
971
972     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
973     if (hr==E_NOINTERFACE)
974     {
975         win_skip("DirectDraw7 is not supported\n");
976         return;
977     }
978     ok(hr==DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", hr);
979
980     if (hr==DD_OK)
981     {
982          dddi2Bytes = FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD);
983
984          pdddi2 = HeapAlloc( GetProcessHeap(), 0, dddi2Bytes + 2*sizeof(DWORD) );
985          pend = (DWORD *)((char *)pdddi2 + dddi2Bytes);
986          pend[0] = 0xdeadbeef;
987          pend[1] = 0xdeadbeef;
988
989          hr = IDirectDraw7_GetDeviceIdentifier(dd7, pdddi2, 0);
990          ok(hr==DD_OK, "get device identifier failed with %08x\n", hr);
991
992          if (hr==DD_OK)
993          {
994              /* check how strings are copied into the structure */
995              ok(pdddi2->szDriver[MAX_DDDEVICEID_STRING - 1]==0, "szDriver not cleared\n");
996              ok(pdddi2->szDescription[MAX_DDDEVICEID_STRING - 1]==0, "szDescription not cleared\n");
997              /* verify that 8 byte structure size alignment will not overwrite memory */
998              ok(pend[0]==0xdeadbeef || broken(pend[0] != 0xdeadbeef), /* win2k */
999                 "memory beyond DDDEVICEIDENTIFIER2 overwritten\n");
1000              ok(pend[1]==0xdeadbeef, "memory beyond DDDEVICEIDENTIFIER2 overwritten\n");
1001          }
1002
1003          IDirectDraw_Release(dd7);
1004          HeapFree( GetProcessHeap(), 0, pdddi2 );
1005     }
1006 }
1007
1008 START_TEST(ddrawmodes)
1009 {
1010     init_function_pointers();
1011
1012     wc.style = CS_HREDRAW | CS_VREDRAW;
1013     wc.lpfnWndProc = DefWindowProcA;
1014     wc.cbClsExtra = 0;
1015     wc.cbWndExtra = 0;
1016     wc.hInstance = GetModuleHandleA(0);
1017     wc.hIcon = LoadIconA(wc.hInstance, IDI_APPLICATION);
1018     wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
1019     wc.hbrBackground = GetStockObject(BLACK_BRUSH);
1020     wc.lpszMenuName = NULL;
1021     wc.lpszClassName = "TestWindowClass";
1022     if (!RegisterClassA(&wc))
1023     {
1024         skip("RegisterClassA failed\n");
1025         return;
1026     }
1027
1028     hwnd2=createwindow();
1029     hwnd=createwindow();
1030
1031     if (!hwnd)
1032     {
1033         skip("Failed to create the main window\n");
1034         return;
1035     }
1036
1037     if (!createdirectdraw())
1038     {
1039         skip("Failed to create the direct draw object\n");
1040         return;
1041     }
1042
1043     test_DirectDrawEnumerateA();
1044     test_DirectDrawEnumerateW();
1045     test_DirectDrawEnumerateExA();
1046     test_DirectDrawEnumerateExW();
1047
1048     enumdisplaymodes();
1049     if (winetest_interactive)
1050         testdisplaymodes();
1051     flushdisplaymodes();
1052     testddraw3();
1053     testddraw7();
1054     releasedirectdraw();
1055
1056     createdirectdraw();
1057     testcooperativelevels_normal();
1058     releasedirectdraw();
1059
1060     createdirectdraw();
1061     testcooperativelevels_exclusive();
1062     releasedirectdraw();
1063 }