mcicda: Exclude unused headers.
[wine] / dlls / user32 / tests / monitor.c
1 /*
2  * Unit tests for monitor APIs
3  *
4  * Copyright 2005 Huw Davies
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "wine/test.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24 #include "winuser.h"
25
26 static HMODULE hdll;
27 static BOOL (WINAPI *pEnumDisplayDevicesA)(LPCSTR,DWORD,LPDISPLAY_DEVICEA,DWORD);
28 static BOOL (WINAPI *pEnumDisplayMonitors)(HDC,LPRECT,MONITORENUMPROC,LPARAM);
29 static BOOL (WINAPI *pGetMonitorInfoA)(HMONITOR,LPMONITORINFO);
30 static HMONITOR (WINAPI *pMonitorFromPoint)(POINT,DWORD);
31 static HMONITOR (WINAPI *pMonitorFromWindow)(HWND,DWORD);
32
33 static void init_function_pointers(void)
34 {
35     hdll = GetModuleHandleA("user32.dll");
36     pEnumDisplayDevicesA = (void*)GetProcAddress(hdll, "EnumDisplayDevicesA");
37     pEnumDisplayMonitors = (void*)GetProcAddress(hdll, "EnumDisplayMonitors");
38     pGetMonitorInfoA = (void*)GetProcAddress(hdll, "GetMonitorInfoA");
39     pMonitorFromPoint = (void*)GetProcAddress(hdll, "MonitorFromPoint");
40     pMonitorFromWindow = (void*)GetProcAddress(hdll, "MonitorFromWindow");
41 }
42
43 static BOOL CALLBACK monitor_enum_proc(HMONITOR hmon, HDC hdc, LPRECT lprc,
44                                        LPARAM lparam)
45 {
46     MONITORINFOEXA mi;
47     char *primary = (char *)lparam;
48
49     mi.cbSize = sizeof(mi);
50
51     ok(pGetMonitorInfoA(hmon, (MONITORINFO*)&mi), "GetMonitorInfo failed\n");
52     if(mi.dwFlags == MONITORINFOF_PRIMARY)
53         strcpy(primary, mi.szDevice);
54
55     return TRUE;
56 }
57
58 static void test_enumdisplaydevices(void)
59 {
60     DISPLAY_DEVICEA dd;
61     char primary_device_name[32];
62     char primary_monitor_device_name[32];
63     DWORD primary_num = -1, num = 0;
64
65     dd.cb = sizeof(dd);
66     if(pEnumDisplayDevicesA == NULL) return;
67     while(1)
68     {
69         BOOL ret;
70         HDC dc;
71         ret = pEnumDisplayDevicesA(NULL, num, &dd, 0);
72         ok(ret || num != 0, "EnumDisplayDevices fails with num == 0\n");
73         if(!ret) break;
74         if(dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
75         {
76             strcpy(primary_device_name, dd.DeviceName);
77             primary_num = num;
78         }
79         if(dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
80         {
81             /* test creating DC */
82             dc = CreateDCA(dd.DeviceName, NULL, NULL, NULL);
83             ok(dc != NULL, "Failed to CreateDC(\"%s\") err=%d\n", dd.DeviceName, GetLastError());
84             DeleteDC(dc);
85         }
86         num++;
87     }
88     ok(primary_num != -1, "Didn't get the primary device\n");
89
90     if(pEnumDisplayMonitors && pGetMonitorInfoA) {
91         ok(pEnumDisplayMonitors(NULL, NULL, monitor_enum_proc, (LPARAM)primary_monitor_device_name),
92            "EnumDisplayMonitors failed\n");
93
94         ok(!strcmp(primary_monitor_device_name, primary_device_name),
95            "monitor device name %s, device name %s\n", primary_monitor_device_name,
96            primary_device_name);
97     }
98 }
99
100 struct vid_mode
101 {
102     DWORD w, h, bpp, freq, fields;
103     LONG success;
104 };
105
106 static struct vid_mode vid_modes_test[] = {
107     {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY, 1},
108     {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT |                 DM_DISPLAYFREQUENCY, 1},
109     {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL                      , 1},
110     {640, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT                                      , 1},
111     {640, 480, 0, 0,                                DM_BITSPERPEL                      , 1},
112     {640, 480, 0, 0,                                                DM_DISPLAYFREQUENCY, 1},
113
114     {0, 0, 0, 0, DM_PELSWIDTH, 1},
115     {0, 0, 0, 0, DM_PELSHEIGHT, 1},
116
117     {640, 480, 0, 0, DM_PELSWIDTH, 0},
118     {640, 480, 0, 0, DM_PELSHEIGHT, 0},
119     {  0, 480, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT, 0},
120     {640,   0, 0, 0, DM_PELSWIDTH | DM_PELSHEIGHT, 0},
121
122     {0, 0, 0, 0, DM_DISPLAYFREQUENCY, 0},
123 };
124 #define vid_modes_cnt (sizeof(vid_modes_test) / sizeof(vid_modes_test[0]))
125
126 static void test_ChangeDisplaySettingsEx(void)
127 {
128     DEVMODE dm;
129     LONG res;
130     int i;
131
132     memset(&dm, 0, sizeof(dm));
133     dm.dmSize = sizeof(dm);
134
135     for (i = 0; i < vid_modes_cnt; i++)
136     {
137         dm.dmPelsWidth        = vid_modes_test[i].w;
138         dm.dmPelsHeight       = vid_modes_test[i].h;
139         dm.dmBitsPerPel       = vid_modes_test[i].bpp;
140         dm.dmDisplayFrequency = vid_modes_test[i].freq;
141         dm.dmFields           = vid_modes_test[i].fields;
142         res = ChangeDisplaySettingsEx(NULL, &dm, NULL, CDS_FULLSCREEN, NULL);
143         ok(vid_modes_test[i].success ?
144            (res == DISP_CHANGE_SUCCESSFUL) :
145            (res == DISP_CHANGE_BADMODE || res == DISP_CHANGE_BADPARAM),
146            "Unexpected ChangeDisplaySettingsEx() return code for resolution[%d]: %d\n", i, res);
147
148         if (res == DISP_CHANGE_SUCCESSFUL)
149         {
150             RECT r, r1, virt;
151
152             SetRect(&virt, 0, 0, GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN));
153             OffsetRect(&virt, GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN));
154
155             /* Resolution change resets clip rect */
156             ok(GetClipCursor(&r), "GetClipCursor() failed\n");
157             ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
158
159             ok(ClipCursor(NULL), "ClipCursor() failed\n");
160             ok(GetClipCursor(&r), "GetClipCursor() failed\n");
161             ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
162
163             /* This should always work. Primary monitor is at (0,0) */
164             SetRect(&r1, 10, 10, 20, 20);
165             ok(ClipCursor(&r1), "ClipCursor() failed\n");
166             ok(GetClipCursor(&r), "GetClipCursor() failed\n");
167             ok(EqualRect(&r, &r1), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
168
169             SetRect(&r1, virt.left - 10, virt.top - 10, virt.right + 20, virt.bottom + 20);
170             ok(ClipCursor(&r1), "ClipCursor() failed\n");
171             ok(GetClipCursor(&r), "GetClipCursor() failed\n");
172             ok(EqualRect(&r, &virt), "Invalid clip rect: (%d %d) x (%d %d)\n", r.left, r.top, r.right, r.bottom);
173         }
174     }
175     res = ChangeDisplaySettingsEx(NULL, NULL, NULL, CDS_RESET, NULL);
176     ok(res == DISP_CHANGE_SUCCESSFUL, "Failed to reset default resolution: %d\n", res);
177 }
178
179 static void test_monitors(void)
180 {
181     HMONITOR monitor, primary;
182     POINT pt;
183
184     pt.x = pt.y = 0;
185     primary = pMonitorFromPoint( pt, MONITOR_DEFAULTTOPRIMARY );
186     ok( primary != 0, "couldn't get primary monitor\n" );
187
188     monitor = pMonitorFromWindow( 0, MONITOR_DEFAULTTONULL );
189     ok( !monitor, "got %p, should not get a monitor for an invalid window\n", monitor );
190     monitor = pMonitorFromWindow( 0, MONITOR_DEFAULTTOPRIMARY );
191     ok( monitor == primary, "got %p, should get primary %p for MONITOR_DEFAULTTOPRIMARY\n", monitor, primary );
192     monitor = pMonitorFromWindow( 0, MONITOR_DEFAULTTONEAREST );
193     ok( monitor == primary, "got %p, should get primary %p for MONITOR_DEFAULTTONEAREST\n", monitor, primary );
194 }
195
196
197 START_TEST(monitor)
198 {
199     init_function_pointers();
200     test_enumdisplaydevices();
201     if (winetest_interactive)
202         test_ChangeDisplaySettingsEx();
203     if (pMonitorFromPoint && pMonitorFromWindow)
204         test_monitors();
205 }