oleaut32: Convert CustData to use standard linked lists.
[wine.git] / dlls / winex11.drv / settings.c
blob9c40225133cc5a5dd21ac390a7483b161f5aa6f0
1 /*
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
21 #include "config.h"
22 #include <string.h>
23 #include <stdio.h>
24 #include <assert.h>
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
29 #include "x11drv.h"
31 #include "windef.h"
32 #include "winreg.h"
33 #include "wingdi.h"
34 #include "ddrawi.h"
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,
63 int (*pNewGCM)(void),
64 LONG (*pNewSCM)(int),
65 unsigned int nmodes,
66 int reserve_depths)
68 handler_name = name;
69 pGetCurrentMode = pNewGCM;
70 pSetCurrentMode = pNewSCM;
71 TRACE("Resolution settings now handled by: %s\n", name);
72 if (reserve_depths)
73 /* leave room for other depths */
74 dd_max_modes = (3+1)*(nmodes);
75 else
76 dd_max_modes = nmodes;
78 if (dd_modes)
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);
84 dd_mode_count = 0;
85 TRACE("Initialized new display modes array\n");
86 return dd_modes;
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);
97 return;
99 if (bpp == 0) bpp = dwBpp;
100 info->dwWidth = width;
101 info->dwHeight = height;
102 info->wRefreshRate = freq;
103 info->lPitch = 0;
104 info->dwBPP = bpp;
105 info->wFlags = 0;
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);
112 dd_mode_count++;
115 /* copy all the current modes using the other color depths */
116 void X11DRV_Settings_AddDepthModes(void)
118 int i, j;
119 int existing_modes = dd_mode_count;
120 DWORD dwBpp = screen_bpp;
121 const DWORD *depths = screen_bpp == 32 ? depths_32 : depths_24;
123 for (j=0; j<3; j++)
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)
148 return 0;
151 static LONG X11DRV_nores_SetCurrentMode(int mode)
153 if (mode == 0) return DISP_CHANGE_SUCCESSFUL;
154 TRACE("Ignoring mode change request mode=%d\n", mode);
155 return DISP_CHANGE_FAILED;
158 /* default handler only gets the current X desktop resolution */
159 void X11DRV_Settings_Init(void)
161 X11DRV_Settings_SetHandlers("NoRes",
162 X11DRV_nores_GetCurrentMode,
163 X11DRV_nores_SetCurrentMode,
164 1, 0);
165 X11DRV_Settings_AddOneMode(screen_width, screen_height, 0, 60);
168 static BOOL get_display_device_reg_key(char *key, unsigned len)
170 static const char display_device_guid_prop[] = "__wine_display_device_guid";
171 static const char video_path[] = "System\\CurrentControlSet\\Control\\Video\\{";
172 static const char display0[] = "}\\0000";
173 ATOM guid_atom;
175 assert(len >= sizeof(video_path) + sizeof(display0) + 40);
177 guid_atom = HandleToULong(GetPropA(GetDesktopWindow(), display_device_guid_prop));
178 if (!guid_atom) return FALSE;
180 memcpy(key, video_path, sizeof(video_path));
182 if (!GlobalGetAtomNameA(guid_atom, key + strlen(key), 40))
183 return FALSE;
185 strcat(key, display0);
187 TRACE("display device key %s\n", wine_dbgstr_a(key));
188 return TRUE;
191 static BOOL read_registry_settings(DEVMODEW *dm)
193 char wine_x11_reg_key[128];
194 HKEY hkey;
195 DWORD type, size;
196 BOOL ret = TRUE;
198 dm->dmFields = 0;
200 if (!get_display_device_reg_key(wine_x11_reg_key, sizeof(wine_x11_reg_key)))
201 return FALSE;
203 if (RegOpenKeyExA(HKEY_CURRENT_CONFIG, wine_x11_reg_key, 0, KEY_READ, &hkey))
204 return FALSE;
206 #define query_value(name, data) \
207 size = sizeof(DWORD); \
208 if (RegQueryValueExA(hkey, name, 0, &type, (LPBYTE)(data), &size) || \
209 type != REG_DWORD || size != sizeof(DWORD)) \
210 ret = FALSE
212 query_value("DefaultSettings.BitsPerPel", &dm->dmBitsPerPel);
213 dm->dmFields |= DM_BITSPERPEL;
214 query_value("DefaultSettings.XResolution", &dm->dmPelsWidth);
215 dm->dmFields |= DM_PELSWIDTH;
216 query_value("DefaultSettings.YResolution", &dm->dmPelsHeight);
217 dm->dmFields |= DM_PELSHEIGHT;
218 query_value("DefaultSettings.VRefresh", &dm->dmDisplayFrequency);
219 dm->dmFields |= DM_DISPLAYFREQUENCY;
220 query_value("DefaultSettings.Flags", &dm->u2.dmDisplayFlags);
221 dm->dmFields |= DM_DISPLAYFLAGS;
222 query_value("DefaultSettings.XPanning", &dm->u1.s2.dmPosition.x);
223 query_value("DefaultSettings.YPanning", &dm->u1.s2.dmPosition.y);
224 query_value("DefaultSettings.Orientation", &dm->u1.s2.dmDisplayOrientation);
225 query_value("DefaultSettings.FixedOutput", &dm->u1.s2.dmDisplayFixedOutput);
227 #undef query_value
229 RegCloseKey(hkey);
230 return ret;
233 static BOOL write_registry_settings(const DEVMODEW *dm)
235 char wine_x11_reg_key[128];
236 HKEY hkey;
237 BOOL ret = TRUE;
239 if (!get_display_device_reg_key(wine_x11_reg_key, sizeof(wine_x11_reg_key)))
240 return FALSE;
242 if (RegCreateKeyExA(HKEY_CURRENT_CONFIG, wine_x11_reg_key, 0, NULL,
243 REG_OPTION_VOLATILE, KEY_WRITE, NULL, &hkey, NULL))
244 return FALSE;
246 #define set_value(name, data) \
247 if (RegSetValueExA(hkey, name, 0, REG_DWORD, (const BYTE*)(data), sizeof(DWORD))) \
248 ret = FALSE
250 set_value("DefaultSettings.BitsPerPel", &dm->dmBitsPerPel);
251 set_value("DefaultSettings.XResolution", &dm->dmPelsWidth);
252 set_value("DefaultSettings.YResolution", &dm->dmPelsHeight);
253 set_value("DefaultSettings.VRefresh", &dm->dmDisplayFrequency);
254 set_value("DefaultSettings.Flags", &dm->u2.dmDisplayFlags);
255 set_value("DefaultSettings.XPanning", &dm->u1.s2.dmPosition.x);
256 set_value("DefaultSettings.YPanning", &dm->u1.s2.dmPosition.y);
257 set_value("DefaultSettings.Orientation", &dm->u1.s2.dmDisplayOrientation);
258 set_value("DefaultSettings.FixedOutput", &dm->u1.s2.dmDisplayFixedOutput);
260 #undef set_value
262 RegCloseKey(hkey);
263 return ret;
266 /***********************************************************************
267 * EnumDisplaySettingsEx (X11DRV.@)
270 BOOL CDECL X11DRV_EnumDisplaySettingsEx( LPCWSTR name, DWORD n, LPDEVMODEW devmode, DWORD flags)
272 static const WCHAR dev_name[CCHDEVICENAME] =
273 { 'W','i','n','e',' ','X','1','1',' ','d','r','i','v','e','r',0 };
275 devmode->dmSize = FIELD_OFFSET(DEVMODEW, dmICMMethod);
276 devmode->dmSpecVersion = DM_SPECVERSION;
277 devmode->dmDriverVersion = DM_SPECVERSION;
278 memcpy(devmode->dmDeviceName, dev_name, sizeof(dev_name));
279 devmode->dmDriverExtra = 0;
280 devmode->u2.dmDisplayFlags = 0;
281 devmode->dmDisplayFrequency = 0;
282 devmode->u1.s2.dmPosition.x = 0;
283 devmode->u1.s2.dmPosition.y = 0;
284 devmode->u1.s2.dmDisplayOrientation = 0;
285 devmode->u1.s2.dmDisplayFixedOutput = 0;
287 if (n == ENUM_CURRENT_SETTINGS)
289 TRACE("mode %d (current) -- getting current mode (%s)\n", n, handler_name);
290 n = pGetCurrentMode();
292 if (n == ENUM_REGISTRY_SETTINGS)
294 TRACE("mode %d (registry) -- getting default mode (%s)\n", n, handler_name);
295 return read_registry_settings(devmode);
297 if (n < dd_mode_count)
299 devmode->dmPelsWidth = dd_modes[n].dwWidth;
300 devmode->dmPelsHeight = dd_modes[n].dwHeight;
301 devmode->dmBitsPerPel = dd_modes[n].dwBPP;
302 devmode->dmDisplayFrequency = dd_modes[n].wRefreshRate;
303 devmode->dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL |
304 DM_DISPLAYFLAGS;
305 if (devmode->dmDisplayFrequency)
307 devmode->dmFields |= DM_DISPLAYFREQUENCY;
308 TRACE("mode %d -- %dx%dx%dbpp @%d Hz (%s)\n", n,
309 devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
310 devmode->dmDisplayFrequency, handler_name);
312 else
314 TRACE("mode %d -- %dx%dx%dbpp (%s)\n", n,
315 devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
316 handler_name);
318 return TRUE;
320 TRACE("mode %d -- not present (%s)\n", n, handler_name);
321 SetLastError(ERROR_NO_MORE_FILES);
322 return FALSE;
325 #define _X_FIELD(prefix, bits) if ((fields) & prefix##_##bits) {p+=sprintf(p, "%s%s", first ? "" : ",", #bits); first=FALSE;}
326 static const char * _CDS_flags(DWORD fields)
328 BOOL first = TRUE;
329 char buf[128];
330 char *p = buf;
331 _X_FIELD(CDS,UPDATEREGISTRY);_X_FIELD(CDS,TEST);_X_FIELD(CDS,FULLSCREEN);
332 _X_FIELD(CDS,GLOBAL);_X_FIELD(CDS,SET_PRIMARY);_X_FIELD(CDS,RESET);
333 _X_FIELD(CDS,SETRECT);_X_FIELD(CDS,NORESET);
334 *p = 0;
335 return wine_dbg_sprintf("%s", buf);
337 static const char * _DM_fields(DWORD fields)
339 BOOL first = TRUE;
340 char buf[128];
341 char *p = buf;
342 _X_FIELD(DM,BITSPERPEL);_X_FIELD(DM,PELSWIDTH);_X_FIELD(DM,PELSHEIGHT);
343 _X_FIELD(DM,DISPLAYFLAGS);_X_FIELD(DM,DISPLAYFREQUENCY);_X_FIELD(DM,POSITION);
344 *p = 0;
345 return wine_dbg_sprintf("%s", buf);
347 #undef _X_FIELD
349 /***********************************************************************
350 * ChangeDisplaySettingsEx (X11DRV.@)
353 LONG CDECL X11DRV_ChangeDisplaySettingsEx( LPCWSTR devname, LPDEVMODEW devmode,
354 HWND hwnd, DWORD flags, LPVOID lpvoid )
356 DWORD i, dwBpp = 0;
357 DEVMODEW dm;
358 BOOL def_mode = TRUE;
360 TRACE("(%s,%p,%p,0x%08x,%p)\n",debugstr_w(devname),devmode,hwnd,flags,lpvoid);
361 TRACE("flags=%s\n",_CDS_flags(flags));
362 if (devmode)
364 /* this is the minimal dmSize that XP accepts */
365 if (devmode->dmSize < FIELD_OFFSET(DEVMODEW, dmFields))
366 return DISP_CHANGE_FAILED;
368 TRACE("DM_fields=%s\n",_DM_fields(devmode->dmFields));
369 TRACE("width=%d height=%d bpp=%d freq=%d (%s)\n",
370 devmode->dmPelsWidth,devmode->dmPelsHeight,
371 devmode->dmBitsPerPel,devmode->dmDisplayFrequency, handler_name);
373 dwBpp = devmode->dmBitsPerPel;
374 if (devmode->dmFields & DM_BITSPERPEL) def_mode &= !dwBpp;
375 if (devmode->dmFields & DM_PELSWIDTH) def_mode &= !devmode->dmPelsWidth;
376 if (devmode->dmFields & DM_PELSHEIGHT) def_mode &= !devmode->dmPelsHeight;
377 if (devmode->dmFields & DM_DISPLAYFREQUENCY) def_mode &= !devmode->dmDisplayFrequency;
380 if (def_mode || !dwBpp)
382 if (!X11DRV_EnumDisplaySettingsEx(devname, ENUM_REGISTRY_SETTINGS, &dm, 0))
384 ERR("Default mode not found!\n");
385 return DISP_CHANGE_BADMODE;
387 if (def_mode)
389 TRACE("Return to original display mode (%s)\n", handler_name);
390 devmode = &dm;
392 dwBpp = dm.dmBitsPerPel;
395 if ((devmode->dmFields & (DM_PELSWIDTH | DM_PELSHEIGHT)) != (DM_PELSWIDTH | DM_PELSHEIGHT))
397 WARN("devmode doesn't specify the resolution: %04x\n", devmode->dmFields);
398 return DISP_CHANGE_BADMODE;
401 for (i = 0; i < dd_mode_count; i++)
403 if (devmode->dmFields & DM_BITSPERPEL)
405 if (dwBpp != dd_modes[i].dwBPP)
406 continue;
408 if (devmode->dmFields & DM_PELSWIDTH)
410 if (devmode->dmPelsWidth != dd_modes[i].dwWidth)
411 continue;
413 if (devmode->dmFields & DM_PELSHEIGHT)
415 if (devmode->dmPelsHeight != dd_modes[i].dwHeight)
416 continue;
418 if ((devmode->dmFields & DM_DISPLAYFREQUENCY) && (dd_modes[i].wRefreshRate != 0) &&
419 devmode->dmDisplayFrequency != 0)
421 if (devmode->dmDisplayFrequency != dd_modes[i].wRefreshRate)
422 continue;
424 /* we have a valid mode */
425 TRACE("Requested display settings match mode %d (%s)\n", i, handler_name);
427 if (flags & CDS_UPDATEREGISTRY)
428 write_registry_settings(devmode);
430 if (!(flags & (CDS_TEST | CDS_NORESET)))
431 return pSetCurrentMode(i);
433 return DISP_CHANGE_SUCCESSFUL;
436 /* no valid modes found */
437 ERR("No matching mode found %ux%ux%u @%u! (%s)\n",
438 devmode->dmPelsWidth, devmode->dmPelsHeight,
439 devmode->dmBitsPerPel, devmode->dmDisplayFrequency, handler_name);
440 return DISP_CHANGE_BADMODE;
446 /***********************************************************************
447 * DirectDraw HAL interface
450 static DWORD PASCAL X11DRV_Settings_SetMode(LPDDHAL_SETMODEDATA data)
452 TRACE("Mode %d requested by DDHAL (%s)\n", data->dwModeIndex, handler_name);
453 switch (pSetCurrentMode(data->dwModeIndex))
455 case DISP_CHANGE_SUCCESSFUL:
456 X11DRV_DDHAL_SwitchMode(data->dwModeIndex, NULL, NULL);
457 data->ddRVal = DD_OK;
458 break;
459 case DISP_CHANGE_BADMODE:
460 data->ddRVal = DDERR_WRONGMODE;
461 break;
462 default:
463 data->ddRVal = DDERR_UNSUPPORTEDMODE;
465 return DDHAL_DRIVER_HANDLED;
467 int X11DRV_Settings_CreateDriver(LPDDHALINFO info)
469 if (!dd_mode_count) return 0; /* no settings defined */
471 TRACE("Setting up display settings for DDRAW (%s)\n", handler_name);
472 info->dwNumModes = dd_mode_count;
473 info->lpModeInfo = dd_modes;
474 X11DRV_DDHAL_SwitchMode(pGetCurrentMode(), NULL, NULL);
475 info->lpDDCallbacks->SetMode = X11DRV_Settings_SetMode;
476 return TRUE;