jscript: Added VBArray.dimensions() implementation.
[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     /* Do some tests with DDSCL_NORMAL mode */
561     /* Fullscreen mode + normal mode + exclusive mode */
562     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_NORMAL);
563     todo_wine ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_NORMAL) returned: %x\n",rc);
564     /* Try creating a double buffered primary in fullscreen + exclusive + normal mode */
565     rc = IDirectDraw_CreateSurface(lpDD, &surfacedesc, &surface, NULL);
566     if (rc == DDERR_UNSUPPORTEDMODE)
567         skip("Unsupported mode\n");
568     else
569     {
570         todo_wine ok(rc == DD_OK, "IDirectDraw_CreateSurface returned %08x\n", rc);
571         todo_wine ok(surface!=NULL, "Returned NULL surface pointer \n");
572     }
573     if(surface && surface != (IDirectDrawSurface *)0xdeadbeef) IDirectDrawSurface_Release(surface);
574     /* Exclusive mode + normal mode */
575     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_EXCLUSIVE | DDSCL_NORMAL);
576     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_NORMAL) returned: %x\n",rc);
577     /* Fullscreen mode + normal mode */
578     rc = IDirectDraw_SetCooperativeLevel(lpDD, hwnd, DDSCL_FULLSCREEN | DDSCL_NORMAL);
579     todo_wine ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_FULLSCREEN | DDSCL_NORMAL) returned: %x\n",rc);
580     /* Try creating a double buffered primary in fullscreen + normal mode */
581     rc = IDirectDraw_CreateSurface(lpDD, &surfacedesc, &surface, NULL);
582     if (rc == DDERR_UNSUPPORTEDMODE)
583         skip("Unsupported mode\n");
584     else
585     {
586         ok(rc == DDERR_NOEXCLUSIVEMODE, "IDirectDraw_CreateSurface returned %08x\n", rc);
587         ok(surface == NULL, "Returned surface pointer is %p\n", surface);
588     }
589     if(surface && surface != (IDirectDrawSurface *)0xdeadbeef) IDirectDrawSurface_Release(surface);
590     /* Normal mode */
591     rc = IDirectDraw_SetCooperativeLevel(lpDD,
592         hwnd, DDSCL_NORMAL);
593     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL) returned: %x\n",rc);
594
595     /* Try creating a double buffered primary in normal mode */
596     rc = IDirectDraw_CreateSurface(lpDD, &surfacedesc, &surface, NULL);
597     if (rc == DDERR_UNSUPPORTEDMODE)
598         skip("Unsupported mode\n");
599     else
600     {
601         ok(rc == DDERR_NOEXCLUSIVEMODE, "IDirectDraw_CreateSurface returned %08x\n", rc);
602         ok(surface == NULL, "Returned surface pointer is %p\n", surface);
603     }
604     if(surface && surface != (IDirectDrawSurface *)0xdeadbeef) IDirectDrawSurface_Release(surface);
605
606     /* Set the focus window */
607     rc = IDirectDraw_SetCooperativeLevel(lpDD,
608         hwnd, DDSCL_SETFOCUSWINDOW);
609
610     if (rc == DDERR_INVALIDPARAMS)
611     {
612         win_skip("NT4/Win95 do not support cooperative levels DDSCL_SETDEVICEWINDOW and DDSCL_SETFOCUSWINDOW\n");
613         return;
614     }
615
616     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
617
618     /* Set the focus window a second time*/
619     rc = IDirectDraw_SetCooperativeLevel(lpDD,
620         hwnd, DDSCL_SETFOCUSWINDOW);
621     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) the second time returned: %x\n",rc);
622
623     /* Test DDSCL_SETFOCUSWINDOW with the other flags. They should all fail, except of DDSCL_NOWINDOWCHANGES */
624     rc = IDirectDraw_SetCooperativeLevel(lpDD,
625         hwnd, DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW);
626     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
627
628     rc = IDirectDraw_SetCooperativeLevel(lpDD,
629         hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW);
630     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
631
632     /* This one succeeds */
633     rc = IDirectDraw_SetCooperativeLevel(lpDD,
634         hwnd, DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW);
635     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
636
637     rc = IDirectDraw_SetCooperativeLevel(lpDD,
638         hwnd, DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW);
639     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
640
641     rc = IDirectDraw_SetCooperativeLevel(lpDD,
642         hwnd, DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW);
643     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
644
645     rc = IDirectDraw_SetCooperativeLevel(lpDD,
646         hwnd, DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW);
647     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
648
649     rc = IDirectDraw_SetCooperativeLevel(lpDD,
650         hwnd, DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW);
651     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
652
653     rc = IDirectDraw_SetCooperativeLevel(lpDD,
654         hwnd, DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW);
655     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
656
657     /* Set the device window without any other flags. Should give an error */
658     rc = IDirectDraw_SetCooperativeLevel(lpDD,
659         hwnd, DDSCL_SETDEVICEWINDOW);
660     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_SETDEVICEWINDOW) returned: %x\n",rc);
661
662     /* Set device window with DDSCL_NORMAL */
663     rc = IDirectDraw_SetCooperativeLevel(lpDD,
664         hwnd, DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW);
665     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW) returned: %x\n",rc);
666
667     /* Also set the focus window. Should give an error */
668     rc = IDirectDraw_SetCooperativeLevel(lpDD,
669         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW);
670     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
671
672     /* All done */
673 }
674
675 static void testcooperativelevels_exclusive(void)
676 {
677     BOOL success;
678     HRESULT rc;
679     RECT window_rect;
680
681     /* Do some tests with DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN mode */
682
683     /* First, resize the window so it is not the same size as any screen */
684     success = SetWindowPos(hwnd, 0, 0, 0, 281, 92, 0);
685     ok(success, "SetWindowPos failed\n");
686
687     /* Try to set exclusive mode only */
688     rc = IDirectDraw_SetCooperativeLevel(lpDD,
689         hwnd, DDSCL_EXCLUSIVE);
690     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE) returned: %x\n",rc);
691
692     /* Full screen mode only */
693     rc = IDirectDraw_SetCooperativeLevel(lpDD,
694         hwnd, DDSCL_FULLSCREEN);
695     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FULLSCREEN) returned: %x\n",rc);
696
697     /* Full screen mode + exclusive mode */
698     rc = IDirectDraw_SetCooperativeLevel(lpDD,
699         hwnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
700     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN) returned: %x\n",rc);
701     GetClientRect(hwnd, &window_rect);
702     /* rect_before_create is assumed to hold the screen rect */
703     rc = EqualRect(&rect_before_create, &window_rect);
704     todo_wine ok(rc!=0, "Fullscreen window has wrong size\n");
705
706     /* Set the focus window. Should fail */
707     rc = IDirectDraw_SetCooperativeLevel(lpDD,
708         hwnd, DDSCL_SETFOCUSWINDOW);
709     ok(rc==DDERR_HWNDALREADYSET ||
710        broken(rc==DDERR_INVALIDPARAMS) /* NT4/Win95 */,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %x\n",rc);
711
712
713     /* All done */
714 }
715
716 static void testddraw3(void)
717 {
718     const GUID My_IID_IDirectDraw3 = {
719         0x618f8ad4,
720         0x8b7a,
721         0x11d0,
722         { 0x8f,0xcc,0x0,0xc0,0x4f,0xd9,0x18,0x9d }
723     };
724     IDirectDraw3 *dd3;
725     HRESULT hr;
726     hr = IDirectDraw_QueryInterface(lpDD, &My_IID_IDirectDraw3, (void **) &dd3);
727     ok(hr == E_NOINTERFACE, "QueryInterface for IID_IDirectDraw3 returned 0x%08x, expected E_NOINTERFACE\n", hr);
728     if(SUCCEEDED(hr) && dd3) IDirectDraw3_Release(dd3);
729 }
730
731 static void testddraw7(void)
732 {
733     IDirectDraw7 *dd7;
734     HRESULT hr;
735     DDDEVICEIDENTIFIER2 *pdddi2;
736     DWORD dddi2Bytes;
737     DWORD *pend;
738
739     hr = IDirectDraw_QueryInterface(lpDD, &IID_IDirectDraw7, (void **) &dd7);
740     if (hr==E_NOINTERFACE)
741     {
742         win_skip("DirectDraw7 is not supported\n");
743         return;
744     }
745     ok(hr==DD_OK, "IDirectDraw7_QueryInterface returned %08x\n", hr);
746
747     if (hr==DD_OK)
748     {
749          dddi2Bytes = FIELD_OFFSET(DDDEVICEIDENTIFIER2, dwWHQLLevel) + sizeof(DWORD);
750
751          pdddi2 = HeapAlloc( GetProcessHeap(), 0, dddi2Bytes + 2*sizeof(DWORD) );
752          pend = (DWORD *)((char *)pdddi2 + dddi2Bytes);
753          pend[0] = 0xdeadbeef;
754          pend[1] = 0xdeadbeef;
755
756          hr = IDirectDraw7_GetDeviceIdentifier(dd7, pdddi2, 0);
757          ok(hr==DD_OK, "get device identifier failed with %08x\n", hr);
758
759          if (hr==DD_OK)
760          {
761              /* check how strings are copied into the structure */
762              ok(pdddi2->szDriver[MAX_DDDEVICEID_STRING - 1]==0, "szDriver not cleared\n");
763              ok(pdddi2->szDescription[MAX_DDDEVICEID_STRING - 1]==0, "szDescription not cleared\n");
764              /* verify that 8 byte structure size alignment will not overwrite memory */
765              ok(pend[0]==0xdeadbeef || broken(pend[0] != 0xdeadbeef), /* win2k */
766                 "memory beyond DDDEVICEIDENTIFIER2 overwritten\n");
767              ok(pend[1]==0xdeadbeef, "memory beyond DDDEVICEIDENTIFIER2 overwritten\n");
768          }
769
770          IDirectDraw_Release(dd7);
771          HeapFree( GetProcessHeap(), 0, pdddi2 );
772     }
773 }
774
775 START_TEST(ddrawmodes)
776 {
777     init_function_pointers();
778
779     createwindow();
780     if (!createdirectdraw())
781         return;
782
783     test_DirectDrawEnumerateA();
784     test_DirectDrawEnumerateW();
785     test_DirectDrawEnumerateExA();
786     test_DirectDrawEnumerateExW();
787
788     enumdisplaymodes();
789     if (winetest_interactive)
790         testdisplaymodes();
791     flushdisplaymodes();
792     testddraw3();
793     testddraw7();
794     releasedirectdraw();
795
796     createdirectdraw();
797     testcooperativelevels_normal();
798     releasedirectdraw();
799
800     createdirectdraw();
801     testcooperativelevels_exclusive();
802     releasedirectdraw();
803 }