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
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(x11settings);
34 * The DDHALMODEINFO type is used to hold all the mode information.
35 * This is done because the array of DDHALMODEINFO structures must be
36 * created for use by DirectDraw anyway.
38 static LPDDHALMODEINFO dd_modes = NULL;
39 static unsigned int dd_mode_count = 0;
40 static unsigned int dd_max_modes = 0;
41 static int dd_mode_default = 0;
42 static const unsigned int depths[] = {8, 16, 32};
44 /* pointers to functions that actually do the hard stuff */
45 static int (*pGetCurrentMode)(void);
46 static void (*pSetCurrentMode)(int mode);
47 static const char *handler_name;
50 * Set the handlers for resolution changing functions
51 * and initialize the master list of modes
53 LPDDHALMODEINFO X11DRV_Settings_SetHandlers(const char *name,
60 pGetCurrentMode = pNewGCM;
61 pSetCurrentMode = pNewSCM;
62 TRACE("Resolution settings now handled by: %s\n", name);
64 /* leave room for other depths */
65 dd_max_modes = (3+1)*(nmodes);
67 dd_max_modes = nmodes;
71 TRACE("Destroying old display modes array\n");
72 HeapFree(GetProcessHeap(), 0, dd_modes);
74 dd_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDHALMODEINFO) * dd_max_modes);
76 TRACE("Initialized new display modes array\n");
80 /* Add one mode to the master list */
81 void X11DRV_Settings_AddOneMode(unsigned int width, unsigned int height, unsigned int bpp, unsigned int freq)
83 LPDDHALMODEINFO info = &(dd_modes[dd_mode_count]);
84 DWORD dwBpp = screen_depth;
85 if (dd_mode_count >= dd_max_modes)
87 ERR("Maximum modes (%d) exceeded\n", dd_max_modes);
90 if (dwBpp == 24) dwBpp = 32;
91 if (bpp == 0) bpp = dwBpp;
92 info->dwWidth = width;
93 info->dwHeight = height;
94 info->wRefreshRate = freq;
100 info->dwBBitMask = 0;
101 info->dwAlphaBitMask = 0;
102 TRACE("initialized mode %d: %dx%dx%d @%d Hz (%s)\n",
103 dd_mode_count, width, height, bpp, freq, handler_name);
107 /* copy all the current modes using the other color depths */
108 void X11DRV_Settings_AddDepthModes(void)
111 int existing_modes = dd_mode_count;
112 DWORD dwBpp = screen_depth;
113 if (dwBpp == 24) dwBpp = 32;
116 if (depths[j] != dwBpp)
118 for (i=0; i < existing_modes; i++)
120 X11DRV_Settings_AddOneMode(dd_modes[i].dwWidth, dd_modes[i].dwHeight,
121 depths[j], dd_modes[i].wRefreshRate);
127 /* set the default mode */
128 void X11DRV_Settings_SetDefaultMode(int mode)
130 dd_mode_default = mode;
133 /* return the number of modes that are initialized */
134 unsigned int X11DRV_Settings_GetModeCount(void)
136 return dd_mode_count;
139 /***********************************************************************
140 * Default handlers if resolution switching is not enabled
143 static int X11DRV_nores_GetCurrentMode(void)
147 static void X11DRV_nores_SetCurrentMode(int mode)
149 TRACE("Ignoring mode change request\n");
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, 0);
161 /***********************************************************************
162 * EnumDisplaySettingsEx (X11DRV.@)
165 BOOL X11DRV_EnumDisplaySettingsEx( LPCWSTR name, DWORD n, LPDEVMODEW devmode, DWORD flags)
167 DWORD dwBpp = screen_depth;
168 if (dwBpp == 24) dwBpp = 32;
169 devmode->dmDisplayFlags = 0;
170 devmode->dmDisplayFrequency = 0;
171 devmode->dmSize = sizeof(DEVMODEW);
172 if (n == ENUM_CURRENT_SETTINGS)
174 TRACE("mode %d (current) -- getting current mode (%s)\n", n, handler_name);
175 n = pGetCurrentMode();
177 if (n == ENUM_REGISTRY_SETTINGS)
179 TRACE("mode %d (registry) -- getting default mode (%s)\n", n, handler_name);
182 if (n < dd_mode_count)
184 devmode->dmPelsWidth = dd_modes[n].dwWidth;
185 devmode->dmPelsHeight = dd_modes[n].dwHeight;
186 devmode->dmBitsPerPel = dd_modes[n].dwBPP;
187 devmode->dmDisplayFrequency = dd_modes[n].wRefreshRate;
188 devmode->dmFields = (DM_PELSWIDTH|DM_PELSHEIGHT|DM_BITSPERPEL);
189 if (devmode->dmDisplayFrequency)
191 devmode->dmFields |= DM_DISPLAYFREQUENCY;
192 TRACE("mode %d -- %dx%dx%dbpp @%d Hz (%s)\n", n,
193 devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
194 devmode->dmDisplayFrequency, handler_name);
198 TRACE("mode %d -- %dx%dx%dbpp (%s)\n", n,
199 devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
204 TRACE("mode %d -- not present (%s)\n", n, handler_name);
208 #define _X_FIELD(prefix, bits) if ((fields) & prefix##_##bits) {p+=sprintf(p, "%s%s", first ? "" : ",", #bits); first=FALSE;}
209 static const char * _CDS_flags(DWORD fields)
214 _X_FIELD(CDS,UPDATEREGISTRY);_X_FIELD(CDS,TEST);_X_FIELD(CDS,FULLSCREEN);
215 _X_FIELD(CDS,GLOBAL);_X_FIELD(CDS,SET_PRIMARY);_X_FIELD(CDS,RESET);
216 _X_FIELD(CDS,SETRECT);_X_FIELD(CDS,NORESET);
218 return wine_dbg_sprintf("%s", buf);
220 static const char * _DM_fields(DWORD fields)
225 _X_FIELD(DM,BITSPERPEL);_X_FIELD(DM,PELSWIDTH);_X_FIELD(DM,PELSHEIGHT);
226 _X_FIELD(DM,DISPLAYFLAGS);_X_FIELD(DM,DISPLAYFREQUENCY);_X_FIELD(DM,POSITION);
228 return wine_dbg_sprintf("%s", buf);
232 /***********************************************************************
233 * ChangeDisplaySettingsEx (X11DRV.@)
236 LONG X11DRV_ChangeDisplaySettingsEx( LPCWSTR devname, LPDEVMODEW devmode,
237 HWND hwnd, DWORD flags, LPVOID lpvoid )
241 BOOL def_mode = TRUE;
243 TRACE("(%s,%p,%p,0x%08x,%p)\n",debugstr_w(devname),devmode,hwnd,flags,lpvoid);
244 TRACE("flags=%s\n",_CDS_flags(flags));
247 TRACE("DM_fields=%s\n",_DM_fields(devmode->dmFields));
248 TRACE("width=%d height=%d bpp=%d freq=%d (%s)\n",
249 devmode->dmPelsWidth,devmode->dmPelsHeight,
250 devmode->dmBitsPerPel,devmode->dmDisplayFrequency, handler_name);
252 dwBpp = (devmode->dmBitsPerPel == 24) ? 32 : devmode->dmBitsPerPel;
253 if (devmode->dmFields & DM_BITSPERPEL) def_mode &= !dwBpp;
254 if (devmode->dmFields & DM_PELSWIDTH) def_mode &= !devmode->dmPelsWidth;
255 if (devmode->dmFields & DM_PELSHEIGHT) def_mode &= !devmode->dmPelsHeight;
256 if (devmode->dmFields & DM_DISPLAYFREQUENCY) def_mode &= !devmode->dmDisplayFrequency;
261 TRACE("Return to original display mode (%s)\n", handler_name);
262 if (!X11DRV_EnumDisplaySettingsEx(devname, dd_mode_default, &dm, 0))
264 ERR("Default mode not found!\n");
265 return DISP_CHANGE_BADMODE;
269 dwBpp = !dwBpp ? dd_modes[dd_mode_default].dwBPP : dwBpp;
271 if ((devmode->dmFields & (DM_PELSWIDTH | DM_PELSHEIGHT)) != (DM_PELSWIDTH | DM_PELSHEIGHT))
272 return DISP_CHANGE_BADMODE;
274 for (i = 0; i < dd_mode_count; i++)
276 if (devmode->dmFields & DM_BITSPERPEL)
278 if (dwBpp != dd_modes[i].dwBPP)
281 if (devmode->dmFields & DM_PELSWIDTH)
283 if (devmode->dmPelsWidth != dd_modes[i].dwWidth)
286 if (devmode->dmFields & DM_PELSHEIGHT)
288 if (devmode->dmPelsHeight != dd_modes[i].dwHeight)
291 if ((devmode->dmFields & DM_DISPLAYFREQUENCY) && (dd_modes[i].wRefreshRate != 0) &&
292 devmode->dmDisplayFrequency != 0)
294 if (devmode->dmDisplayFrequency != dd_modes[i].wRefreshRate)
297 /* we have a valid mode */
298 TRACE("Requested display settings match mode %d (%s)\n", i, handler_name);
299 if (!(flags & CDS_TEST))
301 return DISP_CHANGE_SUCCESSFUL;
304 /* no valid modes found */
305 ERR("No matching mode found! (%s)\n", handler_name);
306 return DISP_CHANGE_BADMODE;
312 /***********************************************************************
313 * DirectDraw HAL interface
316 static DWORD PASCAL X11DRV_Settings_SetMode(LPDDHAL_SETMODEDATA data)
318 TRACE("Mode %d requested by DDHAL (%s)\n", data->dwModeIndex, handler_name);
319 pSetCurrentMode(data->dwModeIndex);
320 X11DRV_DDHAL_SwitchMode(data->dwModeIndex, NULL, NULL);
321 data->ddRVal = DD_OK;
322 return DDHAL_DRIVER_HANDLED;
324 int X11DRV_Settings_CreateDriver(LPDDHALINFO info)
326 if (!dd_mode_count) return 0; /* no settings defined */
328 TRACE("Setting up display settings for DDRAW (%s)\n", handler_name);
329 info->dwNumModes = dd_mode_count;
330 info->lpModeInfo = dd_modes;
331 X11DRV_DDHAL_SwitchMode(pGetCurrentMode(), NULL, NULL);
332 info->lpDDCallbacks->SetMode = X11DRV_Settings_SetMode;