wined3d: Rename return values from D3D* to WINED3D*.
[wine] / dlls / x11drv / settings.c
1 /*
2  * Wine X11drv display settings functions
3  *
4  * Copyright 2003 Alexander James Pasadyn
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include <string.h>
23 #include <stdio.h>
24 #include "x11drv.h"
25 #include "x11ddraw.h"
26
27 #include "windef.h"
28 #include "wingdi.h"
29 #include "ddrawi.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(x11settings);
33
34 /*
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.
38  */
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};
44
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;
49
50 /* 
51  * Set the handlers for resolution changing functions
52  * and initialize the master list of modes
53  */
54 LPDDHALMODEINFO X11DRV_Settings_SetHandlers(const char *name, 
55                                  int (*pNewGCM)(void), 
56                                  void (*pNewSCM)(int), 
57                                  unsigned int nmodes, 
58                                  int reserve_depths)
59 {
60     handler_name = name;
61     pGetCurrentMode = pNewGCM;
62     pSetCurrentMode = pNewSCM;
63     TRACE("Resolution settings now handled by: %s\n", name);
64     if (reserve_depths)
65         /* leave room for other depths */
66         dd_max_modes = (3+1)*(nmodes);
67     else 
68         dd_max_modes = nmodes;
69
70     if (dd_modes) 
71     {
72         TRACE("Destroying old display modes array\n");
73         HeapFree(GetProcessHeap(), 0, dd_modes);
74     }
75     dd_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDHALMODEINFO) * dd_max_modes);
76     dd_mode_count = 0;
77     TRACE("Initialized new display modes array\n");
78     return dd_modes;
79 }
80
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)
83 {
84     LPDDHALMODEINFO info = &(dd_modes[dd_mode_count]);
85     DWORD dwBpp = screen_depth;
86     if (dd_mode_count >= dd_max_modes)
87     {
88         ERR("Maximum modes (%d) exceeded\n", dd_max_modes);
89         return;
90     }
91     if (dwBpp == 24) dwBpp = 32;
92     if (bpp == 0) bpp = dwBpp;
93     info->dwWidth        = width;
94     info->dwHeight       = height;
95     info->wRefreshRate   = freq;
96     info->lPitch         = 0;
97     info->dwBPP          = bpp;
98     info->wFlags         = 0;
99     info->dwRBitMask     = 0;
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);
105     dd_mode_count++;
106 }
107
108 /* copy all the current modes using the other color depths */
109 void X11DRV_Settings_AddDepthModes(void)
110 {
111     int i, j;
112     int existing_modes = dd_mode_count;
113     DWORD dwBpp = screen_depth;
114     if (dwBpp == 24) dwBpp = 32;
115     for (j=0; j<3; j++)
116     {
117         if (depths[j] != dwBpp)
118         {
119             for (i=0; i < existing_modes; i++)
120             {
121                 X11DRV_Settings_AddOneMode(dd_modes[i].dwWidth, dd_modes[i].dwHeight, 
122                                            depths[j], dd_modes[i].wRefreshRate);
123             }
124         }
125     }
126 }
127
128 /* set the default mode */
129 void X11DRV_Settings_SetDefaultMode(int mode)
130 {
131     dd_mode_default = mode;
132 }
133
134 /* return the number of modes that are initialized */
135 unsigned int X11DRV_Settings_GetModeCount(void)
136 {
137     return dd_mode_count;
138 }
139
140 /***********************************************************************
141  * Default handlers if resolution switching is not enabled
142  *
143  */
144 static int X11DRV_nores_GetCurrentMode(void)
145 {
146     return 0;
147 }
148 static void X11DRV_nores_SetCurrentMode(int mode)
149 {
150     TRACE("Ignoring mode change request\n");
151 }
152 /* default handler only gets the current X desktop resolution */
153 void X11DRV_Settings_Init(void)
154 {
155     X11DRV_Settings_SetHandlers("NoRes", 
156                                 X11DRV_nores_GetCurrentMode, 
157                                 X11DRV_nores_SetCurrentMode, 
158                                 1, 0);
159     X11DRV_Settings_AddOneMode(screen_width, screen_height, 0, 0);
160 }
161
162 /***********************************************************************
163  *              EnumDisplaySettingsEx  (X11DRV.@)
164  *
165  */
166 BOOL X11DRV_EnumDisplaySettingsEx( LPCWSTR name, DWORD n, LPDEVMODEW devmode, DWORD flags)
167 {
168     DWORD dwBpp = screen_depth;
169     if (dwBpp == 24) dwBpp = 32;
170     devmode->dmDisplayFlags = 0;
171     devmode->dmDisplayFrequency = 0;
172     devmode->dmSize = sizeof(DEVMODEW);
173     if (n == ENUM_CURRENT_SETTINGS)
174     {
175         TRACE("mode %ld (current) -- getting current mode (%s)\n", n, handler_name);
176         n = pGetCurrentMode();
177     }
178     if (n == ENUM_REGISTRY_SETTINGS)
179     {
180         TRACE("mode %ld (registry) -- getting default mode (%s)\n", n, handler_name);
181         n = dd_mode_default;
182     }
183     if (n < dd_mode_count)
184     {
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)
191         {
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);
196         }
197         else
198         {
199             TRACE("mode %ld -- %ldx%ldx%ldbpp (%s)\n", n,
200                   devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel, 
201                   handler_name);
202         }
203         return TRUE;
204     }
205     TRACE("mode %ld -- not present (%s)\n", n, handler_name);
206     return FALSE;
207 }
208
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)
211 {
212     BOOL first = TRUE;
213     char buf[128];
214     char *p = buf;
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);
218     *p = 0;
219     return wine_dbg_sprintf("%s", buf);
220 }
221 static const char * _DM_fields(DWORD fields)
222 {
223     BOOL first = TRUE;
224     char buf[128];
225     char *p = buf;
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);
228     *p = 0;
229     return wine_dbg_sprintf("%s", buf);
230 }
231 #undef _X_FIELD
232
233 /***********************************************************************
234  *              ChangeDisplaySettingsEx  (X11DRV.@)
235  *
236  */
237 LONG X11DRV_ChangeDisplaySettingsEx( LPCWSTR devname, LPDEVMODEW devmode,
238                                      HWND hwnd, DWORD flags, LPVOID lpvoid )
239 {
240     DWORD i, dwBpp;
241     DEVMODEW dm;
242
243     TRACE("(%s,%p,%p,0x%08lx,%p)\n",debugstr_w(devname),devmode,hwnd,flags,lpvoid);
244     TRACE("flags=%s\n",_CDS_flags(flags));
245     if (devmode)
246     {
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);
251     }
252     else
253     {
254         TRACE("Return to original display mode (%s)\n", handler_name);
255         if (!X11DRV_EnumDisplaySettingsEx(devname, dd_mode_default, &dm, 0))
256         {
257             ERR("Default mode not found!\n");
258             return DISP_CHANGE_BADMODE;
259         }
260         devmode = &dm;
261     }
262     dwBpp = (devmode->dmBitsPerPel == 24) ? 32 : devmode->dmBitsPerPel;
263
264     for (i = 0; i < dd_mode_count; i++)
265     {
266         if (devmode->dmFields & DM_BITSPERPEL)
267         {
268             if (dwBpp != dd_modes[i].dwBPP)
269                 continue;
270         }
271         if (devmode->dmFields & DM_PELSWIDTH)
272         {
273             if (devmode->dmPelsWidth != dd_modes[i].dwWidth)
274                 continue;
275         }
276         if (devmode->dmFields & DM_PELSHEIGHT)
277         {
278             if (devmode->dmPelsHeight != dd_modes[i].dwHeight)
279                 continue;
280         }
281         if ((devmode->dmFields & DM_DISPLAYFREQUENCY) && (dd_modes[i].wRefreshRate != 0))
282         {
283             if (devmode->dmDisplayFrequency != dd_modes[i].wRefreshRate)
284                 continue;
285         }
286         /* we have a valid mode */
287         TRACE("Requested display settings match mode %ld (%s)\n", i, handler_name);
288         if (!(flags & CDS_TEST))
289             pSetCurrentMode(i);
290         return DISP_CHANGE_SUCCESSFUL;
291     }
292
293     /* no valid modes found */
294     ERR("No matching mode found! (%s)\n", handler_name);
295     return DISP_CHANGE_BADMODE;
296 }
297
298
299
300
301 /***********************************************************************
302  * DirectDraw HAL interface
303  *
304  */
305 static DWORD PASCAL X11DRV_Settings_SetMode(LPDDHAL_SETMODEDATA data)
306 {
307     TRACE("Mode %ld requested by DDHAL (%s)\n", data->dwModeIndex, handler_name);
308     pSetCurrentMode(data->dwModeIndex);
309     X11DRV_DDHAL_SwitchMode(data->dwModeIndex, NULL, NULL);
310     data->ddRVal = DD_OK;
311     return DDHAL_DRIVER_HANDLED;
312 }
313 int X11DRV_Settings_CreateDriver(LPDDHALINFO info)
314 {
315     if (!dd_mode_count) return 0; /* no settings defined */
316
317     TRACE("Setting up display settings for DDRAW (%s)\n", handler_name);
318     info->dwNumModes = dd_mode_count;
319     info->lpModeInfo = dd_modes;
320     X11DRV_DDHAL_SwitchMode(pGetCurrentMode(), NULL, NULL);
321     info->lpDDCallbacks->SetMode = X11DRV_Settings_SetMode;
322     return TRUE;
323 }