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