janitorial: Remove remaining NULL checks before free() (found by Smatch).
[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
41 static void createwindow(void)
42 {
43     wc.style = CS_HREDRAW | CS_VREDRAW;
44     wc.lpfnWndProc = DefWindowProcA;
45     wc.cbClsExtra = 0;
46     wc.cbWndExtra = 0;
47     wc.hInstance = GetModuleHandleA(0);
48     wc.hIcon = LoadIconA(wc.hInstance, IDI_APPLICATION);
49     wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
50     wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
51     wc.lpszMenuName = NULL;
52     wc.lpszClassName = "TestWindowClass";
53     if(!RegisterClassA(&wc))
54         assert(0);
55     
56     hwnd = CreateWindowExA(0, "TestWindowClass", "TestWindowClass",
57         WS_POPUP, 0, 0,
58         GetSystemMetrics(SM_CXSCREEN),
59         GetSystemMetrics(SM_CYSCREEN),
60         NULL, NULL, GetModuleHandleA(0), NULL);
61     assert(hwnd != NULL);
62     
63     ShowWindow(hwnd, SW_HIDE);
64     UpdateWindow(hwnd);
65     SetFocus(hwnd);
66     
67 }
68
69 static BOOL createdirectdraw(void)
70 {
71     HRESULT rc;
72
73     rc = DirectDrawCreate(NULL, &lpDD, NULL);
74     ok(rc==DD_OK || rc==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %lx\n", rc);
75     if (!lpDD) {
76         trace("DirectDrawCreateEx() failed with an error %lx\n", rc);
77         return FALSE;
78     }
79     return TRUE;
80 }
81
82
83 static void releasedirectdraw(void)
84 {
85         if( lpDD != NULL )
86         {
87                 IDirectDraw_Release(lpDD);
88                 lpDD = NULL;
89         }
90 }
91
92 static void adddisplaymode(LPDDSURFACEDESC lpddsd)
93 {
94     if (!modes) 
95         modes = malloc((modes_size = 2) * sizeof(DDSURFACEDESC));
96     if (modes_cnt == modes_size) 
97             modes = realloc(modes, (modes_size *= 2) * sizeof(DDSURFACEDESC));
98     assert(modes);
99     modes[modes_cnt++] = *lpddsd;
100 }
101
102 static void flushdisplaymodes(void)
103 {
104     free(modes);
105     modes = 0;
106     modes_cnt = modes_size = 0;
107 }
108
109 static HRESULT WINAPI enummodescallback(LPDDSURFACEDESC lpddsd, LPVOID lpContext)
110 {
111     trace("Width = %li, Height = %li, Refresh Rate = %li\r\n",
112         lpddsd->dwWidth, lpddsd->dwHeight,
113         U2(*lpddsd).dwRefreshRate);
114     adddisplaymode(lpddsd);
115
116     return DDENUMRET_OK;
117 }
118
119 static void enumdisplaymodes(void)
120 {
121     DDSURFACEDESC ddsd;
122     HRESULT rc;
123
124     ZeroMemory(&ddsd, sizeof(DDSURFACEDESC));
125     ddsd.dwSize = sizeof(DDSURFACEDESC);
126     ddsd.dwFlags = DDSD_CAPS;
127     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
128
129     rc = IDirectDraw_EnumDisplayModes(lpDD,
130         DDEDM_STANDARDVGAMODES, &ddsd, 0, enummodescallback);
131     ok(rc==DD_OK || rc==E_INVALIDARG,"EnumDisplayModes returned: %lx\n",rc);
132 }
133
134 static void setdisplaymode(int i)
135 {
136     HRESULT rc;
137
138     rc = IDirectDraw_SetCooperativeLevel(lpDD,
139         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
140     ok(rc==DD_OK,"SetCooperativeLevel returned: %lx\n",rc);
141     if (modes[i].dwFlags & DDSD_PIXELFORMAT)
142     {
143         if (modes[i].ddpfPixelFormat.dwFlags & DDPF_RGB)
144         {
145             rc = IDirectDraw_SetDisplayMode(lpDD,
146                 modes[i].dwWidth, modes[i].dwHeight,
147                 U1(modes[i].ddpfPixelFormat).dwRGBBitCount);
148             ok(DD_OK==rc || DDERR_UNSUPPORTED==rc,"SetDisplayMode returned: %lx\n",rc);
149             if (DD_OK==rc) {
150                 rc = IDirectDraw_RestoreDisplayMode(lpDD);
151                 ok(DD_OK==rc,"RestoreDisplayMode returned: %lx\n",rc);
152             }
153         }
154     }
155 }
156
157 static void createsurface(void)
158 {
159     DDSURFACEDESC ddsd;
160     DDSCAPS ddscaps;
161     HRESULT rc;
162     
163     ddsd.dwSize = sizeof(ddsd);
164     ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
165     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
166         DDSCAPS_FLIP |
167         DDSCAPS_COMPLEX;
168     ddsd.dwBackBufferCount = 1;
169     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSPrimary, NULL );
170     ok(rc==DD_OK,"CreateSurface returned: %lx\n",rc);
171     ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
172     rc = IDirectDrawSurface_GetAttachedSurface(lpDDSPrimary, &ddscaps, &lpDDSBack);
173     ok(rc==DD_OK,"GetAttachedSurface returned: %lx\n",rc);
174 }
175
176 static void destroysurface(void)
177 {
178     if( lpDDSPrimary != NULL )
179     {
180         IDirectDrawSurface_Release(lpDDSPrimary);
181         lpDDSPrimary = NULL;
182     }
183 }
184
185 static void testsurface(void)
186 {
187     const char* testMsg = "ddraw device context test";
188     HDC hdc;
189     HRESULT rc;
190     
191     rc = IDirectDrawSurface_GetDC(lpDDSBack, &hdc);
192     ok(rc==DD_OK, "IDirectDrawSurface_GetDC returned: %lx\n",rc);
193     SetBkColor(hdc, RGB(0, 0, 255));
194     SetTextColor(hdc, RGB(255, 255, 0));
195     TextOut(hdc, 0, 0, testMsg, lstrlen(testMsg));
196     IDirectDrawSurface_ReleaseDC(lpDDSBack, hdc);
197     ok(rc==DD_OK, "IDirectDrawSurface_ReleaseDC returned: %lx\n",rc);
198     
199     while (1)
200     {
201         rc = IDirectDrawSurface_Flip(lpDDSPrimary, NULL, DDFLIP_WAIT);
202         ok(rc==DD_OK || rc==DDERR_SURFACELOST, "IDirectDrawSurface_BltFast returned: %lx\n",rc);
203
204         if (rc == DD_OK)
205         {
206             break;
207         }
208         else if (rc == DDERR_SURFACELOST)
209         {
210             rc = IDirectDrawSurface_Restore(lpDDSPrimary);
211             ok(rc==DD_OK, "IDirectDrawSurface_Restore returned: %lx\n",rc);
212         }
213     }
214 }
215
216 static void testdisplaymodes(void)
217 {
218     int i;
219
220     for (i = 0; i < modes_cnt; ++i)
221     {
222         setdisplaymode(i);
223         createsurface();
224         testsurface();
225         destroysurface();
226     }
227 }
228
229 static void testcooperativelevels_normal(void)
230 {
231     HRESULT rc;
232     DDSURFACEDESC surfacedesc;
233     IDirectDrawSurface *surface = (IDirectDrawSurface *) 0xdeadbeef;
234
235     memset(&surfacedesc, 0, sizeof(surfacedesc));
236     surfacedesc.dwSize = sizeof(surfacedesc);
237     surfacedesc.ddpfPixelFormat.dwSize = sizeof(surfacedesc.ddpfPixelFormat);
238     surfacedesc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
239     surfacedesc.dwBackBufferCount = 1;
240     surfacedesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
241
242     /* Do some tests with DDSCL_NORMAL mode */
243
244     rc = IDirectDraw_SetCooperativeLevel(lpDD,
245         hwnd, DDSCL_NORMAL);
246     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL) returned: %lx\n",rc);
247
248     /* Try creating a double buffered primary in normal mode */
249     rc = IDirectDraw_CreateSurface(lpDD, &surfacedesc, &surface, NULL);
250     ok(rc == DDERR_NOEXCLUSIVEMODE, "IDirectDraw_CreateSurface returned %08lx\n", rc);
251     ok(surface == NULL, "Returned surface pointer is %p\n", surface);
252     if(surface) IDirectDrawSurface_Release(surface);
253
254     /* Set the focus window */
255     rc = IDirectDraw_SetCooperativeLevel(lpDD,
256         hwnd, DDSCL_SETFOCUSWINDOW);
257     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
258
259     /* Set the focus window a second time*/
260     rc = IDirectDraw_SetCooperativeLevel(lpDD,
261         hwnd, DDSCL_SETFOCUSWINDOW);
262     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) the second time returned: %lx\n",rc);
263
264     /* Test DDSCL_SETFOCUSWINDOW with the other flags. They should all fail, except of DDSCL_NOWINDOWCHANGES */
265     rc = IDirectDraw_SetCooperativeLevel(lpDD,
266         hwnd, DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW);
267     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
268
269     rc = IDirectDraw_SetCooperativeLevel(lpDD,
270         hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW);
271     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
272
273     /* This one succeeds */
274     rc = IDirectDraw_SetCooperativeLevel(lpDD,
275         hwnd, DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW);
276     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
277
278     rc = IDirectDraw_SetCooperativeLevel(lpDD,
279         hwnd, DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW);
280     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
281
282     rc = IDirectDraw_SetCooperativeLevel(lpDD,
283         hwnd, DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW);
284     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
285
286     rc = IDirectDraw_SetCooperativeLevel(lpDD,
287         hwnd, DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW);
288     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
289
290     rc = IDirectDraw_SetCooperativeLevel(lpDD,
291         hwnd, DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW);
292     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
293
294     rc = IDirectDraw_SetCooperativeLevel(lpDD,
295         hwnd, DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW);
296     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
297
298     /* Set the device window without any other flags. Should give an error */
299     rc = IDirectDraw_SetCooperativeLevel(lpDD,
300         hwnd, DDSCL_SETDEVICEWINDOW);
301     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_SETDEVICEWINDOW) returned: %lx\n",rc);
302
303     /* Set device window with DDSCL_NORMAL */
304     rc = IDirectDraw_SetCooperativeLevel(lpDD,
305         hwnd, DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW);
306     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW) returned: %lx\n",rc);
307  
308     /* Also set the focus window. Should give an error */
309     rc = IDirectDraw_SetCooperativeLevel(lpDD,
310         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW);
311     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
312
313     /* All done */
314 }
315
316 static void testcooperativelevels_exclusive(void)
317 {
318     HRESULT rc;
319
320     /* Do some tests with DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN mode */
321
322     /* Try to set exclusive mode only */
323     rc = IDirectDraw_SetCooperativeLevel(lpDD,
324         hwnd, DDSCL_EXCLUSIVE);
325     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE) returned: %lx\n",rc);
326
327     /* Full screen mode only */
328     rc = IDirectDraw_SetCooperativeLevel(lpDD,
329         hwnd, DDSCL_FULLSCREEN);
330     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FULLSCREEN) returned: %lx\n",rc);
331
332     /* Full screen mode + exclusive mode */
333     rc = IDirectDraw_SetCooperativeLevel(lpDD,
334         hwnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
335     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN) returned: %lx\n",rc);
336
337     /* Set the focus window. Should fail */
338     rc = IDirectDraw_SetCooperativeLevel(lpDD,
339         hwnd, DDSCL_SETFOCUSWINDOW);
340     ok(rc==DDERR_HWNDALREADYSET,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
341
342
343     /* All done */
344 }
345
346 START_TEST(ddrawmodes)
347 {
348     createwindow();
349     if (!createdirectdraw())
350         return;
351     enumdisplaymodes();
352     if (winetest_interactive)
353         testdisplaymodes();
354     flushdisplaymodes();
355     releasedirectdraw();
356
357     createdirectdraw();
358     testcooperativelevels_normal();
359     releasedirectdraw();
360
361     createdirectdraw();
362     testcooperativelevels_exclusive();
363     releasedirectdraw();
364 }