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
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(x11settings);
40 * The DDHALMODEINFO type is used to hold all the mode information.
41 * This is done because the array of DDHALMODEINFO structures must be
42 * created for use by DirectDraw anyway.
44 static LPDDHALMODEINFO dd_modes = NULL;
45 static unsigned int dd_mode_count = 0;
46 static unsigned int dd_max_modes = 0;
47 /* All Windows drivers seen so far either support 32 bit depths, or 24 bit depths, but never both. So if we have
48 * a 32 bit framebuffer, report 32 bit bpps, otherwise 24 bit ones.
50 static const unsigned int depths_24[] = {8, 16, 24};
51 static const unsigned int depths_32[] = {8, 16, 32};
53 /* pointers to functions that actually do the hard stuff */
54 static int (*pGetCurrentMode)(void);
55 static LONG (*pSetCurrentMode)(int mode);
56 static const char *handler_name;
59 * Set the handlers for resolution changing functions
60 * and initialize the master list of modes
62 LPDDHALMODEINFO X11DRV_Settings_SetHandlers(const char *name,
69 pGetCurrentMode = pNewGCM;
70 pSetCurrentMode = pNewSCM;
71 TRACE("Resolution settings now handled by: %s\n", name);
73 /* leave room for other depths */
74 dd_max_modes = (3+1)*(nmodes);
76 dd_max_modes = nmodes;
80 TRACE("Destroying old display modes array\n");
81 HeapFree(GetProcessHeap(), 0, dd_modes);
83 dd_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDHALMODEINFO) * dd_max_modes);
85 TRACE("Initialized new display modes array\n");
89 /* Add one mode to the master list */
90 void X11DRV_Settings_AddOneMode(unsigned int width, unsigned int height, unsigned int bpp, unsigned int freq)
92 LPDDHALMODEINFO info = &(dd_modes[dd_mode_count]);
93 DWORD dwBpp = screen_bpp;
94 if (dd_mode_count >= dd_max_modes)
96 ERR("Maximum modes (%d) exceeded\n", dd_max_modes);
99 if (bpp == 0) bpp = dwBpp;
100 info->dwWidth = width;
101 info->dwHeight = height;
102 info->wRefreshRate = freq;
106 info->dwRBitMask = 0;
107 info->dwGBitMask = 0;
108 info->dwBBitMask = 0;
109 info->dwAlphaBitMask = 0;
110 TRACE("initialized mode %d: %dx%dx%d @%d Hz (%s)\n",
111 dd_mode_count, width, height, bpp, freq, handler_name);
115 /* copy all the current modes using the other color depths */
116 void X11DRV_Settings_AddDepthModes(void)
119 int existing_modes = dd_mode_count;
120 DWORD dwBpp = screen_bpp;
121 const DWORD *depths = screen_bpp == 32 ? depths_32 : depths_24;
125 if (depths[j] != dwBpp)
127 for (i=0; i < existing_modes; i++)
129 X11DRV_Settings_AddOneMode(dd_modes[i].dwWidth, dd_modes[i].dwHeight,
130 depths[j], dd_modes[i].wRefreshRate);
136 /* return the number of modes that are initialized */
137 unsigned int X11DRV_Settings_GetModeCount(void)
139 return dd_mode_count;
142 /***********************************************************************
143 * Default handlers if resolution switching is not enabled
146 static int X11DRV_nores_GetCurrentMode(void)
151 static LONG X11DRV_nores_SetCurrentMode(int mode)
153 TRACE("Ignoring mode change request\n");
154 return DISP_CHANGE_FAILED;
157 /* default handler only gets the current X desktop resolution */
158 void X11DRV_Settings_Init(void)
160 X11DRV_Settings_SetHandlers("NoRes",
161 X11DRV_nores_GetCurrentMode,
162 X11DRV_nores_SetCurrentMode,
164 X11DRV_Settings_AddOneMode(screen_width, screen_height, 0, 60);
167 static BOOL get_display_device_reg_key(char *key, unsigned len)
169 static const char display_device_guid_prop[] = "__wine_display_device_guid";
170 static const char video_path[] = "System\\CurrentControlSet\\Control\\Video\\{";
171 static const char display0[] = "}\\0000";
174 assert(len >= sizeof(video_path) + sizeof(display0) + 40);
176 guid_atom = HandleToULong(GetPropA(GetDesktopWindow(), display_device_guid_prop));
177 if (!guid_atom) return FALSE;
179 memcpy(key, video_path, sizeof(video_path));
181 if (!GlobalGetAtomNameA(guid_atom, key + strlen(key), 40))
184 strcat(key, display0);
186 TRACE("display device key %s\n", wine_dbgstr_a(key));
190 static BOOL read_registry_settings(DEVMODEW *dm)
192 char wine_x11_reg_key[128];
199 if (!get_display_device_reg_key(wine_x11_reg_key, sizeof(wine_x11_reg_key)))
202 if (RegOpenKeyExA(HKEY_CURRENT_CONFIG, wine_x11_reg_key, 0, KEY_READ, &hkey))
205 #define query_value(name, data) \
206 size = sizeof(DWORD); \
207 if (RegQueryValueExA(hkey, name, 0, &type, (LPBYTE)(data), &size) || \
208 type != REG_DWORD || size != sizeof(DWORD)) \
211 query_value("DefaultSettings.BitsPerPel", &dm->dmBitsPerPel);
212 dm->dmFields |= DM_BITSPERPEL;
213 query_value("DefaultSettings.XResolution", &dm->dmPelsWidth);
214 dm->dmFields |= DM_PELSWIDTH;
215 query_value("DefaultSettings.YResolution", &dm->dmPelsHeight);
216 dm->dmFields |= DM_PELSHEIGHT;
217 query_value("DefaultSettings.VRefresh", &dm->dmDisplayFrequency);
218 dm->dmFields |= DM_DISPLAYFREQUENCY;
219 query_value("DefaultSettings.Flags", &dm->u2.dmDisplayFlags);
220 dm->dmFields |= DM_DISPLAYFLAGS;
221 query_value("DefaultSettings.XPanning", &dm->u1.s2.dmPosition.x);
222 query_value("DefaultSettings.YPanning", &dm->u1.s2.dmPosition.y);
223 query_value("DefaultSettings.Orientation", &dm->u1.s2.dmDisplayOrientation);
224 query_value("DefaultSettings.FixedOutput", &dm->u1.s2.dmDisplayFixedOutput);
232 static BOOL write_registry_settings(const DEVMODEW *dm)
234 char wine_x11_reg_key[128];
238 if (!get_display_device_reg_key(wine_x11_reg_key, sizeof(wine_x11_reg_key)))
241 if (RegCreateKeyExA(HKEY_CURRENT_CONFIG, wine_x11_reg_key, 0, NULL,
242 REG_OPTION_VOLATILE, KEY_WRITE, NULL, &hkey, NULL))
245 #define set_value(name, data) \
246 if (RegSetValueExA(hkey, name, 0, REG_DWORD, (LPBYTE)(data), sizeof(DWORD))) \
249 set_value("DefaultSettings.BitsPerPel", &dm->dmBitsPerPel);
250 set_value("DefaultSettings.XResolution", &dm->dmPelsWidth);
251 set_value("DefaultSettings.YResolution", &dm->dmPelsHeight);
252 set_value("DefaultSettings.VRefresh", &dm->dmDisplayFrequency);
253 set_value("DefaultSettings.Flags", &dm->u2.dmDisplayFlags);
254 set_value("DefaultSettings.XPanning", &dm->u1.s2.dmPosition.x);
255 set_value("DefaultSettings.YPanning", &dm->u1.s2.dmPosition.y);
256 set_value("DefaultSettings.Orientation", &dm->u1.s2.dmDisplayOrientation);
257 set_value("DefaultSettings.FixedOutput", &dm->u1.s2.dmDisplayFixedOutput);
265 /***********************************************************************
266 * EnumDisplaySettingsEx (X11DRV.@)
269 BOOL X11DRV_EnumDisplaySettingsEx( LPCWSTR name, DWORD n, LPDEVMODEW devmode, DWORD flags)
271 static const WCHAR dev_name[CCHDEVICENAME] =
272 { 'W','i','n','e',' ','X','1','1',' ','d','r','i','v','e','r',0 };
274 devmode->dmSize = sizeof(DEVMODEW);
275 devmode->dmSpecVersion = MAKEWORD(1,4);
276 devmode->dmDriverVersion = MAKEWORD(1,4);
277 memcpy(devmode->dmDeviceName, dev_name, sizeof(dev_name));
278 devmode->dmDriverExtra = 0;
279 devmode->u2.dmDisplayFlags = 0;
280 devmode->dmDisplayFrequency = 0;
281 devmode->u1.s2.dmPosition.x = 0;
282 devmode->u1.s2.dmPosition.y = 0;
283 devmode->u1.s2.dmDisplayOrientation = 0;
284 devmode->u1.s2.dmDisplayFixedOutput = 0;
286 if (n == ENUM_CURRENT_SETTINGS)
288 TRACE("mode %d (current) -- getting current mode (%s)\n", n, handler_name);
289 n = pGetCurrentMode();
291 if (n == ENUM_REGISTRY_SETTINGS)
293 TRACE("mode %d (registry) -- getting default mode (%s)\n", n, handler_name);
294 return read_registry_settings(devmode);
296 if (n < dd_mode_count)
298 devmode->dmPelsWidth = dd_modes[n].dwWidth;
299 devmode->dmPelsHeight = dd_modes[n].dwHeight;
300 devmode->dmBitsPerPel = dd_modes[n].dwBPP;
301 devmode->dmDisplayFrequency = dd_modes[n].wRefreshRate;
302 devmode->dmFields = (DM_PELSWIDTH|DM_PELSHEIGHT|DM_BITSPERPEL);
303 if (devmode->dmDisplayFrequency)
305 devmode->dmFields |= DM_DISPLAYFREQUENCY;
306 TRACE("mode %d -- %dx%dx%dbpp @%d Hz (%s)\n", n,
307 devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
308 devmode->dmDisplayFrequency, handler_name);
312 TRACE("mode %d -- %dx%dx%dbpp (%s)\n", n,
313 devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
318 TRACE("mode %d -- not present (%s)\n", n, handler_name);
319 SetLastError(ERROR_NO_MORE_FILES);
323 #define _X_FIELD(prefix, bits) if ((fields) & prefix##_##bits) {p+=sprintf(p, "%s%s", first ? "" : ",", #bits); first=FALSE;}
324 static const char * _CDS_flags(DWORD fields)
329 _X_FIELD(CDS,UPDATEREGISTRY);_X_FIELD(CDS,TEST);_X_FIELD(CDS,FULLSCREEN);
330 _X_FIELD(CDS,GLOBAL);_X_FIELD(CDS,SET_PRIMARY);_X_FIELD(CDS,RESET);
331 _X_FIELD(CDS,SETRECT);_X_FIELD(CDS,NORESET);
333 return wine_dbg_sprintf("%s", buf);
335 static const char * _DM_fields(DWORD fields)
340 _X_FIELD(DM,BITSPERPEL);_X_FIELD(DM,PELSWIDTH);_X_FIELD(DM,PELSHEIGHT);
341 _X_FIELD(DM,DISPLAYFLAGS);_X_FIELD(DM,DISPLAYFREQUENCY);_X_FIELD(DM,POSITION);
343 return wine_dbg_sprintf("%s", buf);
347 /***********************************************************************
348 * ChangeDisplaySettingsEx (X11DRV.@)
351 LONG X11DRV_ChangeDisplaySettingsEx( LPCWSTR devname, LPDEVMODEW devmode,
352 HWND hwnd, DWORD flags, LPVOID lpvoid )
356 BOOL def_mode = TRUE;
358 TRACE("(%s,%p,%p,0x%08x,%p)\n",debugstr_w(devname),devmode,hwnd,flags,lpvoid);
359 TRACE("flags=%s\n",_CDS_flags(flags));
362 /* this is the minimal dmSize that XP accepts */
363 if (devmode->dmSize < FIELD_OFFSET(DEVMODEW, dmFields))
364 return DISP_CHANGE_FAILED;
366 TRACE("DM_fields=%s\n",_DM_fields(devmode->dmFields));
367 TRACE("width=%d height=%d bpp=%d freq=%d (%s)\n",
368 devmode->dmPelsWidth,devmode->dmPelsHeight,
369 devmode->dmBitsPerPel,devmode->dmDisplayFrequency, handler_name);
371 dwBpp = devmode->dmBitsPerPel;
372 if (devmode->dmFields & DM_BITSPERPEL) def_mode &= !dwBpp;
373 if (devmode->dmFields & DM_PELSWIDTH) def_mode &= !devmode->dmPelsWidth;
374 if (devmode->dmFields & DM_PELSHEIGHT) def_mode &= !devmode->dmPelsHeight;
375 if (devmode->dmFields & DM_DISPLAYFREQUENCY) def_mode &= !devmode->dmDisplayFrequency;
378 if (def_mode || !dwBpp)
380 if (!X11DRV_EnumDisplaySettingsEx(devname, ENUM_REGISTRY_SETTINGS, &dm, 0))
382 ERR("Default mode not found!\n");
383 return DISP_CHANGE_BADMODE;
387 TRACE("Return to original display mode (%s)\n", handler_name);
390 dwBpp = dm.dmBitsPerPel;
393 if ((devmode->dmFields & (DM_PELSWIDTH | DM_PELSHEIGHT)) != (DM_PELSWIDTH | DM_PELSHEIGHT))
395 WARN("devmode doesn't specify the resolution: %04x\n", devmode->dmFields);
396 return DISP_CHANGE_BADMODE;
399 for (i = 0; i < dd_mode_count; i++)
401 if (devmode->dmFields & DM_BITSPERPEL)
403 if (dwBpp != dd_modes[i].dwBPP)
406 if (devmode->dmFields & DM_PELSWIDTH)
408 if (devmode->dmPelsWidth != dd_modes[i].dwWidth)
411 if (devmode->dmFields & DM_PELSHEIGHT)
413 if (devmode->dmPelsHeight != dd_modes[i].dwHeight)
416 if ((devmode->dmFields & DM_DISPLAYFREQUENCY) && (dd_modes[i].wRefreshRate != 0) &&
417 devmode->dmDisplayFrequency != 0)
419 if (devmode->dmDisplayFrequency != dd_modes[i].wRefreshRate)
422 /* we have a valid mode */
423 TRACE("Requested display settings match mode %d (%s)\n", i, handler_name);
425 if (flags & CDS_UPDATEREGISTRY)
426 write_registry_settings(devmode);
428 if (!(flags & (CDS_TEST | CDS_NORESET)))
429 return pSetCurrentMode(i);
431 return DISP_CHANGE_SUCCESSFUL;
434 /* no valid modes found */
435 ERR("No matching mode found %ux%ux%u @%u! (%s)\n",
436 devmode->dmPelsWidth, devmode->dmPelsHeight,
437 devmode->dmBitsPerPel, devmode->dmDisplayFrequency, handler_name);
438 return DISP_CHANGE_BADMODE;
444 /***********************************************************************
445 * DirectDraw HAL interface
448 static DWORD PASCAL X11DRV_Settings_SetMode(LPDDHAL_SETMODEDATA data)
450 TRACE("Mode %d requested by DDHAL (%s)\n", data->dwModeIndex, handler_name);
451 switch (pSetCurrentMode(data->dwModeIndex))
453 case DISP_CHANGE_SUCCESSFUL:
454 X11DRV_DDHAL_SwitchMode(data->dwModeIndex, NULL, NULL);
455 data->ddRVal = DD_OK;
457 case DISP_CHANGE_BADMODE:
458 data->ddRVal = DDERR_WRONGMODE;
461 data->ddRVal = DDERR_UNSUPPORTEDMODE;
463 return DDHAL_DRIVER_HANDLED;
465 int X11DRV_Settings_CreateDriver(LPDDHALINFO info)
467 if (!dd_mode_count) return 0; /* no settings defined */
469 TRACE("Setting up display settings for DDRAW (%s)\n", handler_name);
470 info->dwNumModes = dd_mode_count;
471 info->lpModeInfo = dd_modes;
472 X11DRV_DDHAL_SwitchMode(pGetCurrentMode(), NULL, NULL);
473 info->lpDDCallbacks->SetMode = X11DRV_Settings_SetMode;