2 * Wine X11drv display settings functions
4 * Copyright 2003 Alexander James Pasadyn
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
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(x11settings);
39 * The DDHALMODEINFO type is used to hold all the mode information.
40 * This is done because the array of DDHALMODEINFO structures must be
41 * created for use by DirectDraw anyway.
43 static struct x11drv_mode_info *dd_modes = NULL;
44 static unsigned int dd_mode_count = 0;
45 static unsigned int dd_max_modes = 0;
46 /* All Windows drivers seen so far either support 32 bit depths, or 24 bit depths, but never both. So if we have
47 * a 32 bit framebuffer, report 32 bit bpps, otherwise 24 bit ones.
49 static const unsigned int depths_24[] = {8, 16, 24};
50 static const unsigned int depths_32[] = {8, 16, 32};
52 /* pointers to functions that actually do the hard stuff */
53 static int (*pGetCurrentMode)(void);
54 static LONG (*pSetCurrentMode)(int mode);
55 static const char *handler_name;
58 * Set the handlers for resolution changing functions
59 * and initialize the master list of modes
61 struct x11drv_mode_info *X11DRV_Settings_SetHandlers(const char *name,
68 pGetCurrentMode = pNewGCM;
69 pSetCurrentMode = pNewSCM;
70 TRACE("Resolution settings now handled by: %s\n", name);
72 /* leave room for other depths */
73 dd_max_modes = (3+1)*(nmodes);
75 dd_max_modes = nmodes;
79 TRACE("Destroying old display modes array\n");
80 HeapFree(GetProcessHeap(), 0, dd_modes);
82 dd_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dd_modes) * dd_max_modes);
84 TRACE("Initialized new display modes array\n");
88 /* Add one mode to the master list */
89 void X11DRV_Settings_AddOneMode(unsigned int width, unsigned int height, unsigned int bpp, unsigned int freq)
91 struct x11drv_mode_info *info = &dd_modes[dd_mode_count];
92 DWORD dwBpp = screen_bpp;
93 if (dd_mode_count >= dd_max_modes)
95 ERR("Maximum modes (%d) exceeded\n", dd_max_modes);
98 if (bpp == 0) bpp = dwBpp;
100 info->height = height;
101 info->refresh_rate = freq;
103 TRACE("initialized mode %d: %dx%dx%d @%d Hz (%s)\n",
104 dd_mode_count, width, height, bpp, freq, handler_name);
108 /* copy all the current modes using the other color depths */
109 void X11DRV_Settings_AddDepthModes(void)
112 int existing_modes = dd_mode_count;
113 DWORD dwBpp = screen_bpp;
114 const DWORD *depths = screen_bpp == 32 ? depths_32 : depths_24;
118 if (depths[j] != dwBpp)
120 for (i=0; i < existing_modes; i++)
122 X11DRV_Settings_AddOneMode(dd_modes[i].width, dd_modes[i].height,
123 depths[j], dd_modes[i].refresh_rate);
129 /* return the number of modes that are initialized */
130 unsigned int X11DRV_Settings_GetModeCount(void)
132 return dd_mode_count;
135 /***********************************************************************
136 * Default handlers if resolution switching is not enabled
139 static int X11DRV_nores_GetCurrentMode(void)
144 static LONG X11DRV_nores_SetCurrentMode(int mode)
146 if (mode == 0) return DISP_CHANGE_SUCCESSFUL;
147 TRACE("Ignoring mode change request mode=%d\n", mode);
148 return DISP_CHANGE_FAILED;
151 /* default handler only gets the current X desktop resolution */
152 void X11DRV_Settings_Init(void)
154 X11DRV_Settings_SetHandlers("NoRes",
155 X11DRV_nores_GetCurrentMode,
156 X11DRV_nores_SetCurrentMode,
158 X11DRV_Settings_AddOneMode(screen_width, screen_height, 0, 60);
161 static BOOL get_display_device_reg_key(char *key, unsigned len)
163 static const char display_device_guid_prop[] = "__wine_display_device_guid";
164 static const char video_path[] = "System\\CurrentControlSet\\Control\\Video\\{";
165 static const char display0[] = "}\\0000";
168 assert(len >= sizeof(video_path) + sizeof(display0) + 40);
170 guid_atom = HandleToULong(GetPropA(GetDesktopWindow(), display_device_guid_prop));
171 if (!guid_atom) return FALSE;
173 memcpy(key, video_path, sizeof(video_path));
175 if (!GlobalGetAtomNameA(guid_atom, key + strlen(key), 40))
178 strcat(key, display0);
180 TRACE("display device key %s\n", wine_dbgstr_a(key));
184 static BOOL read_registry_settings(DEVMODEW *dm)
186 char wine_x11_reg_key[128];
193 if (!get_display_device_reg_key(wine_x11_reg_key, sizeof(wine_x11_reg_key)))
196 if (RegOpenKeyExA(HKEY_CURRENT_CONFIG, wine_x11_reg_key, 0, KEY_READ, &hkey))
199 #define query_value(name, data) \
200 size = sizeof(DWORD); \
201 if (RegQueryValueExA(hkey, name, 0, &type, (LPBYTE)(data), &size) || \
202 type != REG_DWORD || size != sizeof(DWORD)) \
205 query_value("DefaultSettings.BitsPerPel", &dm->dmBitsPerPel);
206 dm->dmFields |= DM_BITSPERPEL;
207 query_value("DefaultSettings.XResolution", &dm->dmPelsWidth);
208 dm->dmFields |= DM_PELSWIDTH;
209 query_value("DefaultSettings.YResolution", &dm->dmPelsHeight);
210 dm->dmFields |= DM_PELSHEIGHT;
211 query_value("DefaultSettings.VRefresh", &dm->dmDisplayFrequency);
212 dm->dmFields |= DM_DISPLAYFREQUENCY;
213 query_value("DefaultSettings.Flags", &dm->u2.dmDisplayFlags);
214 dm->dmFields |= DM_DISPLAYFLAGS;
215 query_value("DefaultSettings.XPanning", &dm->u1.s2.dmPosition.x);
216 query_value("DefaultSettings.YPanning", &dm->u1.s2.dmPosition.y);
217 query_value("DefaultSettings.Orientation", &dm->u1.s2.dmDisplayOrientation);
218 query_value("DefaultSettings.FixedOutput", &dm->u1.s2.dmDisplayFixedOutput);
226 static BOOL write_registry_settings(const DEVMODEW *dm)
228 char wine_x11_reg_key[128];
232 if (!get_display_device_reg_key(wine_x11_reg_key, sizeof(wine_x11_reg_key)))
235 if (RegCreateKeyExA(HKEY_CURRENT_CONFIG, wine_x11_reg_key, 0, NULL,
236 REG_OPTION_VOLATILE, KEY_WRITE, NULL, &hkey, NULL))
239 #define set_value(name, data) \
240 if (RegSetValueExA(hkey, name, 0, REG_DWORD, (const BYTE*)(data), sizeof(DWORD))) \
243 set_value("DefaultSettings.BitsPerPel", &dm->dmBitsPerPel);
244 set_value("DefaultSettings.XResolution", &dm->dmPelsWidth);
245 set_value("DefaultSettings.YResolution", &dm->dmPelsHeight);
246 set_value("DefaultSettings.VRefresh", &dm->dmDisplayFrequency);
247 set_value("DefaultSettings.Flags", &dm->u2.dmDisplayFlags);
248 set_value("DefaultSettings.XPanning", &dm->u1.s2.dmPosition.x);
249 set_value("DefaultSettings.YPanning", &dm->u1.s2.dmPosition.y);
250 set_value("DefaultSettings.Orientation", &dm->u1.s2.dmDisplayOrientation);
251 set_value("DefaultSettings.FixedOutput", &dm->u1.s2.dmDisplayFixedOutput);
259 /***********************************************************************
260 * EnumDisplaySettingsEx (X11DRV.@)
263 BOOL CDECL X11DRV_EnumDisplaySettingsEx( LPCWSTR name, DWORD n, LPDEVMODEW devmode, DWORD flags)
265 static const WCHAR dev_name[CCHDEVICENAME] =
266 { 'W','i','n','e',' ','X','1','1',' ','d','r','i','v','e','r',0 };
268 devmode->dmSize = FIELD_OFFSET(DEVMODEW, dmICMMethod);
269 devmode->dmSpecVersion = DM_SPECVERSION;
270 devmode->dmDriverVersion = DM_SPECVERSION;
271 memcpy(devmode->dmDeviceName, dev_name, sizeof(dev_name));
272 devmode->dmDriverExtra = 0;
273 devmode->u2.dmDisplayFlags = 0;
274 devmode->dmDisplayFrequency = 0;
275 devmode->u1.s2.dmPosition.x = 0;
276 devmode->u1.s2.dmPosition.y = 0;
277 devmode->u1.s2.dmDisplayOrientation = 0;
278 devmode->u1.s2.dmDisplayFixedOutput = 0;
280 if (n == ENUM_CURRENT_SETTINGS)
282 TRACE("mode %d (current) -- getting current mode (%s)\n", n, handler_name);
283 n = pGetCurrentMode();
285 if (n == ENUM_REGISTRY_SETTINGS)
287 TRACE("mode %d (registry) -- getting default mode (%s)\n", n, handler_name);
288 return read_registry_settings(devmode);
290 if (n < dd_mode_count)
292 devmode->dmPelsWidth = dd_modes[n].width;
293 devmode->dmPelsHeight = dd_modes[n].height;
294 devmode->dmBitsPerPel = dd_modes[n].bpp;
295 devmode->dmDisplayFrequency = dd_modes[n].refresh_rate;
296 devmode->dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL |
298 if (devmode->dmDisplayFrequency)
300 devmode->dmFields |= DM_DISPLAYFREQUENCY;
301 TRACE("mode %d -- %dx%dx%dbpp @%d Hz (%s)\n", n,
302 devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
303 devmode->dmDisplayFrequency, handler_name);
307 TRACE("mode %d -- %dx%dx%dbpp (%s)\n", n,
308 devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
313 TRACE("mode %d -- not present (%s)\n", n, handler_name);
314 SetLastError(ERROR_NO_MORE_FILES);
318 #define _X_FIELD(prefix, bits) if ((fields) & prefix##_##bits) {p+=sprintf(p, "%s%s", first ? "" : ",", #bits); first=FALSE;}
319 static const char * _CDS_flags(DWORD fields)
324 _X_FIELD(CDS,UPDATEREGISTRY);_X_FIELD(CDS,TEST);_X_FIELD(CDS,FULLSCREEN);
325 _X_FIELD(CDS,GLOBAL);_X_FIELD(CDS,SET_PRIMARY);_X_FIELD(CDS,RESET);
326 _X_FIELD(CDS,SETRECT);_X_FIELD(CDS,NORESET);
328 return wine_dbg_sprintf("%s", buf);
330 static const char * _DM_fields(DWORD fields)
335 _X_FIELD(DM,BITSPERPEL);_X_FIELD(DM,PELSWIDTH);_X_FIELD(DM,PELSHEIGHT);
336 _X_FIELD(DM,DISPLAYFLAGS);_X_FIELD(DM,DISPLAYFREQUENCY);_X_FIELD(DM,POSITION);
338 return wine_dbg_sprintf("%s", buf);
342 /***********************************************************************
343 * ChangeDisplaySettingsEx (X11DRV.@)
346 LONG CDECL X11DRV_ChangeDisplaySettingsEx( LPCWSTR devname, LPDEVMODEW devmode,
347 HWND hwnd, DWORD flags, LPVOID lpvoid )
351 BOOL def_mode = TRUE;
353 TRACE("(%s,%p,%p,0x%08x,%p)\n",debugstr_w(devname),devmode,hwnd,flags,lpvoid);
354 TRACE("flags=%s\n",_CDS_flags(flags));
357 /* this is the minimal dmSize that XP accepts */
358 if (devmode->dmSize < FIELD_OFFSET(DEVMODEW, dmFields))
359 return DISP_CHANGE_FAILED;
361 TRACE("DM_fields=%s\n",_DM_fields(devmode->dmFields));
362 TRACE("width=%d height=%d bpp=%d freq=%d (%s)\n",
363 devmode->dmPelsWidth,devmode->dmPelsHeight,
364 devmode->dmBitsPerPel,devmode->dmDisplayFrequency, handler_name);
366 dwBpp = devmode->dmBitsPerPel;
367 if (devmode->dmFields & DM_BITSPERPEL) def_mode &= !dwBpp;
368 if (devmode->dmFields & DM_PELSWIDTH) def_mode &= !devmode->dmPelsWidth;
369 if (devmode->dmFields & DM_PELSHEIGHT) def_mode &= !devmode->dmPelsHeight;
370 if (devmode->dmFields & DM_DISPLAYFREQUENCY) def_mode &= !devmode->dmDisplayFrequency;
373 if (def_mode || !dwBpp)
375 if (!X11DRV_EnumDisplaySettingsEx(devname, ENUM_REGISTRY_SETTINGS, &dm, 0))
377 ERR("Default mode not found!\n");
378 return DISP_CHANGE_BADMODE;
382 TRACE("Return to original display mode (%s)\n", handler_name);
385 dwBpp = dm.dmBitsPerPel;
388 if ((devmode->dmFields & (DM_PELSWIDTH | DM_PELSHEIGHT)) != (DM_PELSWIDTH | DM_PELSHEIGHT))
390 WARN("devmode doesn't specify the resolution: %04x\n", devmode->dmFields);
391 return DISP_CHANGE_BADMODE;
394 for (i = 0; i < dd_mode_count; i++)
396 if (devmode->dmFields & DM_BITSPERPEL)
398 if (dwBpp != dd_modes[i].bpp)
401 if (devmode->dmFields & DM_PELSWIDTH)
403 if (devmode->dmPelsWidth != dd_modes[i].width)
406 if (devmode->dmFields & DM_PELSHEIGHT)
408 if (devmode->dmPelsHeight != dd_modes[i].height)
411 if ((devmode->dmFields & DM_DISPLAYFREQUENCY) && (dd_modes[i].refresh_rate != 0) &&
412 devmode->dmDisplayFrequency != 0)
414 if (devmode->dmDisplayFrequency != dd_modes[i].refresh_rate)
417 /* we have a valid mode */
418 TRACE("Requested display settings match mode %d (%s)\n", i, handler_name);
420 if (flags & CDS_UPDATEREGISTRY)
421 write_registry_settings(devmode);
423 if (!(flags & (CDS_TEST | CDS_NORESET)))
424 return pSetCurrentMode(i);
426 return DISP_CHANGE_SUCCESSFUL;
429 /* no valid modes found */
430 ERR("No matching mode found %ux%ux%u @%u! (%s)\n",
431 devmode->dmPelsWidth, devmode->dmPelsHeight,
432 devmode->dmBitsPerPel, devmode->dmDisplayFrequency, handler_name);
433 return DISP_CHANGE_BADMODE;