winex11: Get rid of the glGetIntegerv wrapper.
[wine/multimedia.git] / dlls / winex11.drv / settings.c
blob9512c64f1fbcd6ae196b0a0a4b04781c0c3bca08
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 "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,
62 int (*pNewGCM)(void),
63 LONG (*pNewSCM)(int),
64 unsigned int nmodes,
65 int reserve_depths)
67 handler_name = name;
68 pGetCurrentMode = pNewGCM;
69 pSetCurrentMode = pNewSCM;
70 TRACE("Resolution settings now handled by: %s\n", name);
71 if (reserve_depths)
72 /* leave room for other depths */
73 dd_max_modes = (3+1)*(nmodes);
74 else
75 dd_max_modes = nmodes;
77 if (dd_modes)
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);
83 dd_mode_count = 0;
84 TRACE("Initialized new display modes array\n");
85 return dd_modes;
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);
96 return;
98 if (bpp == 0) bpp = dwBpp;
99 info->width = width;
100 info->height = height;
101 info->refresh_rate = freq;
102 info->bpp = bpp;
103 TRACE("initialized mode %d: %dx%dx%d @%d Hz (%s)\n",
104 dd_mode_count, width, height, bpp, freq, handler_name);
105 dd_mode_count++;
108 /* copy all the current modes using the other color depths */
109 void X11DRV_Settings_AddDepthModes(void)
111 int i, j;
112 int existing_modes = dd_mode_count;
113 DWORD dwBpp = screen_bpp;
114 const DWORD *depths = screen_bpp == 32 ? depths_32 : depths_24;
116 for (j=0; j<3; j++)
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)
141 return 0;
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,
157 1, 0);
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";
166 ATOM guid_atom;
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))
176 return FALSE;
178 strcat(key, display0);
180 TRACE("display device key %s\n", wine_dbgstr_a(key));
181 return TRUE;
184 static BOOL read_registry_settings(DEVMODEW *dm)
186 char wine_x11_reg_key[128];
187 HKEY hkey;
188 DWORD type, size;
189 BOOL ret = TRUE;
191 dm->dmFields = 0;
193 if (!get_display_device_reg_key(wine_x11_reg_key, sizeof(wine_x11_reg_key)))
194 return FALSE;
196 if (RegOpenKeyExA(HKEY_CURRENT_CONFIG, wine_x11_reg_key, 0, KEY_READ, &hkey))
197 return FALSE;
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)) \
203 ret = FALSE
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);
220 #undef query_value
222 RegCloseKey(hkey);
223 return ret;
226 static BOOL write_registry_settings(const DEVMODEW *dm)
228 char wine_x11_reg_key[128];
229 HKEY hkey;
230 BOOL ret = TRUE;
232 if (!get_display_device_reg_key(wine_x11_reg_key, sizeof(wine_x11_reg_key)))
233 return FALSE;
235 if (RegCreateKeyExA(HKEY_CURRENT_CONFIG, wine_x11_reg_key, 0, NULL,
236 REG_OPTION_VOLATILE, KEY_WRITE, NULL, &hkey, NULL))
237 return FALSE;
239 #define set_value(name, data) \
240 if (RegSetValueExA(hkey, name, 0, REG_DWORD, (const BYTE*)(data), sizeof(DWORD))) \
241 ret = FALSE
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);
253 #undef set_value
255 RegCloseKey(hkey);
256 return ret;
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 |
297 DM_DISPLAYFLAGS;
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);
305 else
307 TRACE("mode %d -- %dx%dx%dbpp (%s)\n", n,
308 devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
309 handler_name);
311 return TRUE;
313 TRACE("mode %d -- not present (%s)\n", n, handler_name);
314 SetLastError(ERROR_NO_MORE_FILES);
315 return FALSE;
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)
321 BOOL first = TRUE;
322 char buf[128];
323 char *p = buf;
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);
327 *p = 0;
328 return wine_dbg_sprintf("%s", buf);
330 static const char * _DM_fields(DWORD fields)
332 BOOL first = TRUE;
333 char buf[128];
334 char *p = buf;
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);
337 *p = 0;
338 return wine_dbg_sprintf("%s", buf);
340 #undef _X_FIELD
342 /***********************************************************************
343 * ChangeDisplaySettingsEx (X11DRV.@)
346 LONG CDECL X11DRV_ChangeDisplaySettingsEx( LPCWSTR devname, LPDEVMODEW devmode,
347 HWND hwnd, DWORD flags, LPVOID lpvoid )
349 DWORD i, dwBpp = 0;
350 DEVMODEW dm;
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));
355 if (devmode)
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;
380 if (def_mode)
382 TRACE("Return to original display mode (%s)\n", handler_name);
383 devmode = &dm;
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)
399 continue;
401 if (devmode->dmFields & DM_PELSWIDTH)
403 if (devmode->dmPelsWidth != dd_modes[i].width)
404 continue;
406 if (devmode->dmFields & DM_PELSHEIGHT)
408 if (devmode->dmPelsHeight != dd_modes[i].height)
409 continue;
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)
415 continue;
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;