2 * Unit tests for monitor APIs
4 * Copyright 2005 Huw Davies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/test.h"
27 static LONG (WINAPI *pChangeDisplaySettingsExA)(LPCSTR, LPDEVMODEA, HWND, DWORD, LPVOID);
28 static BOOL (WINAPI *pEnumDisplayDevicesA)(LPCSTR,DWORD,LPDISPLAY_DEVICEA,DWORD);
29 static BOOL (WINAPI *pEnumDisplayMonitors)(HDC,LPRECT,MONITORENUMPROC,LPARAM);
30 static BOOL (WINAPI *pGetMonitorInfoA)(HMONITOR,LPMONITORINFO);
31 static HMONITOR (WINAPI *pMonitorFromPoint)(POINT,DWORD);
32 static HMONITOR (WINAPI *pMonitorFromWindow)(HWND,DWORD);
34 static void init_function_pointers(void)
36 hdll = GetModuleHandleA("user32.dll");
38 #define GET_PROC(func) \
39 p ## func = (void*)GetProcAddress(hdll, #func); \
41 trace("GetProcAddress(%s) failed\n", #func);
43 GET_PROC(ChangeDisplaySettingsExA)
44 GET_PROC(EnumDisplayDevicesA)
45 GET_PROC(EnumDisplayMonitors)
46 GET_PROC(GetMonitorInfoA)
47 GET_PROC(MonitorFromPoint)
48 GET_PROC(MonitorFromWindow)
53 static BOOL CALLBACK monitor_enum_proc(HMONITOR hmon, HDC hdc, LPRECT lprc,
57 char *primary = (char *)lparam;
59 mi.cbSize = sizeof(mi);
61 ok(pGetMonitorInfoA(hmon, (MONITORINFO*)&mi), "GetMonitorInfo failed\n");
62 if(mi.dwFlags == MONITORINFOF_PRIMARY)
63 strcpy(primary, mi.szDevice);
68 static void test_enumdisplaydevices(void)
71 char primary_device_name[32];
72 char primary_monitor_device_name[32];
73 DWORD primary_num = -1, num = 0;
76 if(pEnumDisplayDevicesA == NULL) return;
81 ret = pEnumDisplayDevicesA(NULL, num, &dd, 0);
82 ok(ret || num != 0, "EnumDisplayDevices fails with num == 0\n");
84 if(dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
86 strcpy(primary_device_name, dd.DeviceName);
89 if(dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
91 /* test creating DC */
92 dc = CreateDCA(dd.DeviceName, NULL, NULL, NULL);
93 ok(dc != NULL, "Failed to CreateDC(\"%s\") err=%d\n", dd.DeviceName, GetLastError());
98 ok(primary_num != -1, "Didn't get the primary device\n");
100 if(pEnumDisplayMonitors && pGetMonitorInfoA) {
101 ok(pEnumDisplayMonitors(NULL, NULL, monitor_enum_proc, (LPARAM)primary_monitor_device_name),
102 "EnumDisplayMonitors failed\n");
104 ok(!strcmp(primary_monitor_device_name, primary_device_name),
105 "monitor device name %s, device name %s\n", primary_monitor_device_name,
106 primary_device_name);
112 DWORD w, h, bpp, freq, fields;
116 static struct vid_mode vid_modes_test[] = {
117 {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY, 1},
118 {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY, 1},
119 {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL , 1},
120 {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT , 1},
121 {640, 480, 0, 0, DM_BITSPERPEL , 1},
122 {640, 480, 0, 0, DM_DISPLAYFREQUENCY, 1},
124 {0, 0, 0, 0, DM_PELSWIDTH, 1},
125 {0, 0, 0, 0, DM_PELSHEIGHT, 1},
127 {640, 480, 0, 0, DM_PELSWIDTH, 0},
128 {640, 480, 0, 0, DM_PELSHEIGHT, 0},
129 { 0, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT, 0},
130 {640, 0, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT, 0},
132 {0, 0, 0, 0, DM_DISPLAYFREQUENCY, 0},
134 #define vid_modes_cnt (sizeof(vid_modes_test) / sizeof(vid_modes_test[0]))
136 static void test_ChangeDisplaySettingsEx(void)
142 if (!pChangeDisplaySettingsExA)
144 skip("ChangeDisplaySettingsExA is not available\n");
148 memset(&dm, 0, sizeof(dm));
149 dm.dmSize = sizeof(dm);
151 for (i = 0; i < vid_modes_cnt; i++)
153 dm.dmPelsWidth = vid_modes_test[i].w;
154 dm.dmPelsHeight = vid_modes_test[i].h;
155 dm.dmBitsPerPel = vid_modes_test[i].bpp;
156 dm.dmDisplayFrequency = vid_modes_test[i].freq;
157 dm.dmFields = vid_modes_test[i].fields;
158 res = pChangeDisplaySettingsExA(NULL, &dm, NULL, CDS_FULLSCREEN, NULL);
159 ok(vid_modes_test[i].success ?
160 (res == DISP_CHANGE_SUCCESSFUL) :
161 (res == DISP_CHANGE_BADMODE || res == DISP_CHANGE_BADPARAM),
162 "Unexpected ChangeDisplaySettingsEx() return code for resolution[%d]: %d\n", i, res);
164 if (res == DISP_CHANGE_SUCCESSFUL)
168 SetRect(&virt, 0, 0, GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN));
169 OffsetRect(&virt, GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN));
171 /* Resolution change resets clip rect */
172 ok(GetClipCursor(&r), "GetClipCursor() failed\n");
173 ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
175 ok(ClipCursor(NULL), "ClipCursor() failed\n");
176 ok(GetClipCursor(&r), "GetClipCursor() failed\n");
177 ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
179 /* This should always work. Primary monitor is at (0,0) */
180 SetRect(&r1, 10, 10, 20, 20);
181 ok(ClipCursor(&r1), "ClipCursor() failed\n");
182 ok(GetClipCursor(&r), "GetClipCursor() failed\n");
183 ok(EqualRect(&r, &r1), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
185 SetRect(&r1, virt.left - 10, virt.top - 10, virt.right + 20, virt.bottom + 20);
186 ok(ClipCursor(&r1), "ClipCursor() failed\n");
187 ok(GetClipCursor(&r), "GetClipCursor() failed\n");
188 ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
191 res = pChangeDisplaySettingsExA(NULL, NULL, NULL, CDS_RESET, NULL);
192 ok(res == DISP_CHANGE_SUCCESSFUL, "Failed to reset default resolution: %d\n", res);
195 static void test_monitors(void)
197 HMONITOR monitor, primary;
201 primary = pMonitorFromPoint( pt, MONITOR_DEFAULTTOPRIMARY );
202 ok( primary != 0, "couldn't get primary monitor\n" );
204 monitor = pMonitorFromWindow( 0, MONITOR_DEFAULTTONULL );
205 ok( !monitor, "got %p, should not get a monitor for an invalid window\n", monitor );
206 monitor = pMonitorFromWindow( 0, MONITOR_DEFAULTTOPRIMARY );
207 ok( monitor == primary, "got %p, should get primary %p for MONITOR_DEFAULTTOPRIMARY\n", monitor, primary );
208 monitor = pMonitorFromWindow( 0, MONITOR_DEFAULTTONEAREST );
209 ok( monitor == primary, "got %p, should get primary %p for MONITOR_DEFAULTTONEAREST\n", monitor, primary );
215 init_function_pointers();
216 test_enumdisplaydevices();
217 if (winetest_interactive)
218 test_ChangeDisplaySettingsEx();
219 if (pMonitorFromPoint && pMonitorFromWindow)
222 skip("MonitorFromPoint and/or MonitorFromWindow are not available\n");