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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(x11settings);
35 * The DDHALMODEINFO type is used to hold all the mode information.
36 * This is done because the array of DDHALMODEINFO structures must be
37 * created for use by DirectDraw anyway.
39 static LPDDHALMODEINFO dd_modes = NULL;
40 static unsigned int dd_mode_count = 0;
41 static unsigned int dd_max_modes = 0;
42 static int dd_mode_default = 0;
43 static const unsigned int depths[] = {8, 16, 32};
45 /* pointers to functions that actually do the hard stuff */
46 static int (*pGetCurrentMode)(void);
47 static void (*pSetCurrentMode)(int mode);
48 static const char *handler_name;
51 * Set the handlers for resolution changing functions
52 * and initialize the master list of modes
54 LPDDHALMODEINFO X11DRV_Settings_SetHandlers(const char *name,
61 pGetCurrentMode = pNewGCM;
62 pSetCurrentMode = pNewSCM;
63 TRACE("Resolution settings now handled by: %s\n", name);
65 /* leave room for other depths */
66 dd_max_modes = (3+1)*(nmodes);
68 dd_max_modes = nmodes;
72 TRACE("Destroying old display modes array\n");
73 HeapFree(GetProcessHeap(), 0, dd_modes);
75 dd_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDHALMODEINFO) * dd_max_modes);
77 TRACE("Initialized new display modes array\n");
81 /* Add one mode to the master list */
82 void X11DRV_Settings_AddOneMode(unsigned int width, unsigned int height, unsigned int bpp, unsigned int freq)
84 LPDDHALMODEINFO info = &(dd_modes[dd_mode_count]);
85 DWORD dwBpp = screen_depth;
86 if (dd_mode_count >= dd_max_modes)
88 ERR("Maximum modes (%d) exceeded\n", dd_max_modes);
91 if (dwBpp == 24) dwBpp = 32;
92 if (bpp == 0) bpp = dwBpp;
93 info->dwWidth = width;
94 info->dwHeight = height;
95 info->wRefreshRate = freq;
100 info->dwGBitMask = 0;
101 info->dwBBitMask = 0;
102 info->dwAlphaBitMask = 0;
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_depth;
114 if (dwBpp == 24) dwBpp = 32;
117 if (depths[j] != dwBpp)
119 for (i=0; i < existing_modes; i++)
121 X11DRV_Settings_AddOneMode(dd_modes[i].dwWidth, dd_modes[i].dwHeight,
122 depths[j], dd_modes[i].wRefreshRate);
128 /* set the default mode */
129 void X11DRV_Settings_SetDefaultMode(int mode)
131 dd_mode_default = mode;
134 /* return the number of modes that are initialized */
135 unsigned int X11DRV_Settings_GetModeCount(void)
137 return dd_mode_count;
140 /***********************************************************************
141 * Default handlers if resolution switching is not enabled
144 static int X11DRV_nores_GetCurrentMode(void)
148 static void X11DRV_nores_SetCurrentMode(int mode)
150 TRACE("Ignoring mode change request\n");
152 /* default handler only gets the current X desktop resolution */
153 void X11DRV_Settings_Init(void)
155 X11DRV_Settings_SetHandlers("NoRes",
156 X11DRV_nores_GetCurrentMode,
157 X11DRV_nores_SetCurrentMode,
159 X11DRV_Settings_AddOneMode(screen_width, screen_height, 0, 0);
162 /***********************************************************************
163 * EnumDisplaySettingsExW (X11DRV.@)
166 BOOL X11DRV_EnumDisplaySettingsExW( LPCWSTR name, DWORD n, LPDEVMODEW devmode, DWORD flags)
168 DWORD dwBpp = screen_depth;
169 if (dwBpp == 24) dwBpp = 32;
170 devmode->dmDisplayFlags = 0;
171 devmode->dmDisplayFrequency = 0;
172 devmode->dmSize = sizeof(DEVMODEW);
175 TRACE("mode %ld (current) -- getting current mode (%s)\n", n, handler_name);
176 n = pGetCurrentMode();
180 TRACE("mode %ld (registry) -- getting default mode (%s)\n", n, handler_name);
183 if (n < dd_mode_count)
185 devmode->dmPelsWidth = dd_modes[n].dwWidth;
186 devmode->dmPelsHeight = dd_modes[n].dwHeight;
187 devmode->dmBitsPerPel = dd_modes[n].dwBPP;
188 devmode->dmDisplayFrequency = dd_modes[n].wRefreshRate;
189 devmode->dmFields = (DM_PELSWIDTH|DM_PELSHEIGHT|DM_BITSPERPEL);
190 if (devmode->dmDisplayFrequency)
192 devmode->dmFields |= DM_DISPLAYFREQUENCY;
193 TRACE("mode %ld -- %ldx%ldx%ldbpp @%ld Hz (%s)\n", n,
194 devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
195 devmode->dmDisplayFrequency, handler_name);
199 TRACE("mode %ld -- %ldx%ldx%ldbpp (%s)\n", n,
200 devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel,
205 TRACE("mode %ld -- not present (%s)\n", n, handler_name);
209 #define _X_FIELD(prefix, bits) if ((fields) & prefix##_##bits) {p+=sprintf(p, "%s%s", first ? "" : ",", #bits); first=FALSE;}
210 static const char * _CDS_flags(DWORD fields)
215 _X_FIELD(CDS,UPDATEREGISTRY);_X_FIELD(CDS,TEST);_X_FIELD(CDS,FULLSCREEN);
216 _X_FIELD(CDS,GLOBAL);_X_FIELD(CDS,SET_PRIMARY);_X_FIELD(CDS,RESET);
217 _X_FIELD(CDS,SETRECT);_X_FIELD(CDS,NORESET);
219 return wine_dbg_sprintf("%s", buf);
221 static const char * _DM_fields(DWORD fields)
226 _X_FIELD(DM,BITSPERPEL);_X_FIELD(DM,PELSWIDTH);_X_FIELD(DM,PELSHEIGHT);
227 _X_FIELD(DM,DISPLAYFLAGS);_X_FIELD(DM,DISPLAYFREQUENCY);_X_FIELD(DM,POSITION);
229 return wine_dbg_sprintf("%s", buf);
233 /***********************************************************************
234 * ChangeDisplaySettingsExW (X11DRV.@)
237 LONG X11DRV_ChangeDisplaySettingsExW( LPCWSTR devname, LPDEVMODEW devmode,
238 HWND hwnd, DWORD flags, LPVOID lpvoid )
243 TRACE("(%s,%p,%p,0x%08lx,%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=%ld height=%ld bpp=%ld freq=%ld (%s)\n",
249 devmode->dmPelsWidth,devmode->dmPelsHeight,
250 devmode->dmBitsPerPel,devmode->dmDisplayFrequency, handler_name);
254 TRACE("Return to original display mode (%s)\n", handler_name);
255 if (!X11DRV_EnumDisplaySettingsExW(devname, dd_mode_default, &dm, 0))
257 ERR("Default mode not found!\n");
258 return DISP_CHANGE_BADMODE;
263 for (i = 0; i < dd_mode_count; i++)
265 if (devmode->dmFields & DM_BITSPERPEL)
267 if (devmode->dmBitsPerPel != dd_modes[i].dwBPP)
270 if (devmode->dmFields & DM_PELSWIDTH)
272 if (devmode->dmPelsWidth != dd_modes[i].dwWidth)
275 if (devmode->dmFields & DM_PELSHEIGHT)
277 if (devmode->dmPelsHeight != dd_modes[i].dwHeight)
280 if (devmode->dmFields & DM_DISPLAYFREQUENCY)
282 if (devmode->dmDisplayFrequency != dd_modes[i].wRefreshRate)
285 /* we have a valid mode */
286 TRACE("Requested display settings match mode %ld (%s)\n", i, handler_name);
287 if (!(flags & CDS_TEST))
289 return DISP_CHANGE_SUCCESSFUL;
292 /* no valid modes found */
293 ERR("No matching mode found! (%s)\n", handler_name);
294 return DISP_CHANGE_BADMODE;
300 /***********************************************************************
301 * DirectDraw HAL interface
304 static DWORD PASCAL X11DRV_Settings_SetMode(LPDDHAL_SETMODEDATA data)
306 TRACE("Mode %ld requested by DDHAL (%s)\n", data->dwModeIndex, handler_name);
307 pSetCurrentMode(data->dwModeIndex);
308 X11DRV_DDHAL_SwitchMode(data->dwModeIndex, NULL, NULL);
309 data->ddRVal = DD_OK;
310 return DDHAL_DRIVER_HANDLED;
312 int X11DRV_Settings_CreateDriver(LPDDHALINFO info)
314 if (!dd_mode_count) return 0; /* no settings defined */
316 TRACE("Setting up display settings for DDRAW (%s)\n", handler_name);
317 info->dwNumModes = dd_mode_count;
318 info->lpModeInfo = dd_modes;
319 X11DRV_DDHAL_SwitchMode(pGetCurrentMode(), NULL, NULL);
320 info->lpDDCallbacks->SetMode = X11DRV_Settings_SetMode;