Changed Adam Sacarny's email, and -debugmsg to --debugmsg.
[wine/gsoc_dplay.git] / dlls / x11drv / xvidmode.c
blob0faa8631b7168abe33ace0d437a245aea8b713d2
1 /*
2 * DirectDraw XVidMode interface
4 * Copyright 2001 TransGaming Technologies, Inc.
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
21 #include "config.h"
22 #include <string.h>
23 #include <stdio.h>
25 #include <X11/Xlib.h>
27 #ifdef HAVE_LIBXXF86VM
28 #define XMD_H
29 #include "basetsd.h"
30 #include <X11/extensions/xf86vmode.h>
31 #include "x11drv.h"
33 #include "x11ddraw.h"
34 #include "xvidmode.h"
36 #include "windef.h"
37 #include "wingdi.h"
38 #include "ddrawi.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(xvidmode);
43 extern int usexvidmode;
45 static int xf86vm_event, xf86vm_error, xf86vm_major, xf86vm_minor;
47 #ifdef X_XF86VidModeSetGammaRamp
48 static int xf86vm_gammaramp_size;
49 static BOOL xf86vm_use_gammaramp;
50 #endif /* X_XF86VidModeSetGammaRamp */
52 static LPDDHALMODEINFO dd_modes;
53 static unsigned int dd_mode_count;
54 static XF86VidModeModeInfo** real_xf86vm_modes;
55 static unsigned int real_xf86vm_mode_count;
56 static unsigned int xf86vm_initial_mode = 0;
58 static void convert_modeinfo( const XF86VidModeModeInfo *mode)
60 int rate;
61 if (mode->htotal!=0 && mode->vtotal!=0)
62 rate = mode->dotclock * 1000 / (mode->htotal * mode->vtotal);
63 else
64 rate = 0;
65 X11DRV_Settings_AddOneMode(mode->hdisplay, mode->vdisplay, 0, rate);
68 static void convert_modeline(int dotclock, const XF86VidModeModeLine *mode, LPDDHALMODEINFO info, unsigned int bpp)
70 info->dwWidth = mode->hdisplay;
71 info->dwHeight = mode->vdisplay;
72 if (mode->htotal!=0 && mode->vtotal!=0)
73 info->wRefreshRate = dotclock * 1000 / (mode->htotal * mode->vtotal);
74 else
75 info->wRefreshRate = 0;
76 TRACE(" width=%ld, height=%ld, refresh=%d\n",
77 info->dwWidth, info->dwHeight, info->wRefreshRate);
78 info->lPitch = 0;
79 info->dwBPP = bpp;
80 info->wFlags = 0;
81 info->dwRBitMask = 0;
82 info->dwGBitMask = 0;
83 info->dwBBitMask = 0;
84 info->dwAlphaBitMask = 0;
87 static int XVidModeErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
89 return 1;
92 static Bool in_desktop_mode;
94 int X11DRV_XF86VM_GetCurrentMode(void)
96 XF86VidModeModeLine line;
97 int dotclock, i;
98 DDHALMODEINFO cmode;
99 DWORD dwBpp = screen_depth;
100 if (dwBpp == 24) dwBpp = 32;
102 TRACE("Querying XVidMode current mode\n");
103 wine_tsx11_lock();
104 XF86VidModeGetModeLine(gdi_display, DefaultScreen(gdi_display), &dotclock, &line);
105 wine_tsx11_unlock();
106 convert_modeline(dotclock, &line, &cmode, dwBpp);
107 for (i=0; i<dd_mode_count; i++)
108 if (memcmp(&dd_modes[i], &cmode, sizeof(cmode)) == 0) {
109 TRACE("mode=%d\n", i);
110 return i;
112 ERR("In unknown mode, returning default\n");
113 return xf86vm_initial_mode;
116 void X11DRV_XF86VM_SetCurrentMode(int mode)
118 DWORD dwBpp = screen_depth;
119 if (dwBpp == 24) dwBpp = 32;
120 /* only set modes from the original color depth */
121 if (dwBpp != dd_modes[mode].dwBPP)
123 FIXME("Cannot change screen BPP from %ld to %ld\n", dwBpp, dd_modes[mode].dwBPP);
125 mode = mode % real_xf86vm_mode_count;
127 wine_tsx11_lock();
128 TRACE("Resizing X display to %dx%d\n",
129 real_xf86vm_modes[mode]->hdisplay, real_xf86vm_modes[mode]->vdisplay);
130 XF86VidModeSwitchToMode(gdi_display, DefaultScreen(gdi_display), real_xf86vm_modes[mode]);
131 #if 0 /* FIXME */
132 SYSMETRICS_Set( SM_CXSCREEN, real_xf86vm_modes[mode]->hdisplay );
133 SYSMETRICS_Set( SM_CYSCREEN, real_xf86vm_modes[mode]->vdisplay );
134 #else
135 FIXME("Need to update SYSMETRICS after resizing display (now %dx%d)\n",
136 real_xf86vm_modes[mode]->hdisplay, real_xf86vm_modes[mode]->vdisplay);
137 #endif
138 #if 0 /* it is said that SetViewPort causes problems with some X servers */
139 XF86VidModeSetViewPort(gdi_display, DefaultScreen(gdi_display), 0, 0);
140 #else
141 XWarpPointer(gdi_display, None, DefaultRootWindow(gdi_display), 0, 0, 0, 0, 0, 0);
142 #endif
143 XSync(gdi_display, False);
144 wine_tsx11_unlock();
147 void X11DRV_XF86VM_Init(void)
149 Bool ok;
150 int nmodes, i;
151 DWORD dwBpp = screen_depth;
152 if (dwBpp == 24) dwBpp = 32;
154 in_desktop_mode = (root_window != DefaultRootWindow(gdi_display));
156 if (xf86vm_major) return; /* already initialized? */
158 if (!usexvidmode) return;
160 /* see if XVidMode is available */
161 wine_tsx11_lock();
162 ok = XF86VidModeQueryExtension(gdi_display, &xf86vm_event, &xf86vm_error);
163 if (ok)
165 X11DRV_expect_error(gdi_display, XVidModeErrorHandler, NULL);
166 ok = XF86VidModeQueryVersion(gdi_display, &xf86vm_major, &xf86vm_minor);
167 if (X11DRV_check_error()) ok = FALSE;
169 if (ok)
171 #ifdef X_XF86VidModeSetGammaRamp
172 if (xf86vm_major > 2 || (xf86vm_major == 2 && xf86vm_minor >= 1))
174 XF86VidModeGetGammaRampSize(gdi_display, DefaultScreen(gdi_display),
175 &xf86vm_gammaramp_size);
176 if (xf86vm_gammaramp_size == 256)
177 xf86vm_use_gammaramp = TRUE;
179 #endif /* X_XF86VidModeSetGammaRamp */
181 /* retrieve modes */
182 if (!in_desktop_mode) ok = XF86VidModeGetAllModeLines(gdi_display, DefaultScreen(gdi_display), &nmodes, &real_xf86vm_modes);
184 wine_tsx11_unlock();
185 if (!ok) return;
187 /* In desktop mode, do not switch resolution... But still use the Gamma ramp stuff */
188 if (in_desktop_mode) return;
190 TRACE("XVidMode modes: count=%d\n", nmodes);
192 real_xf86vm_mode_count = nmodes;
194 dd_modes = X11DRV_Settings_SetHandlers("XF86VidMode",
195 X11DRV_XF86VM_GetCurrentMode,
196 X11DRV_XF86VM_SetCurrentMode,
197 nmodes, 1);
199 /* convert modes to DDHALMODEINFO format */
200 for (i=0; i<real_xf86vm_mode_count; i++)
202 convert_modeinfo(real_xf86vm_modes[i]);
204 /* add modes for different color depths */
205 X11DRV_Settings_AddDepthModes();
206 dd_mode_count = X11DRV_Settings_GetModeCount();
208 TRACE("Available DD modes: count=%d\n", dd_mode_count);
210 /* store the current mode at the time we started */
211 xf86vm_initial_mode = X11DRV_XF86VM_GetCurrentMode();
212 X11DRV_Settings_SetDefaultMode(xf86vm_initial_mode);
214 TRACE("Enabling XVidMode\n");
217 void X11DRV_XF86VM_Cleanup(void)
219 wine_tsx11_lock();
220 if (real_xf86vm_modes) XFree(real_xf86vm_modes);
221 wine_tsx11_unlock();
224 void X11DRV_XF86VM_SetExclusiveMode(int lock)
226 if (!dd_modes) return; /* no XVidMode */
228 wine_tsx11_lock();
229 XF86VidModeLockModeSwitch(gdi_display, DefaultScreen(gdi_display), lock);
230 wine_tsx11_unlock();
233 /***** GAMMA CONTROL *****/
234 /* (only available in XF86VidMode 2.x) */
236 #ifdef X_XF86VidModeSetGamma
238 static void GenerateRampFromGamma(WORD ramp[256], float gamma)
240 float r_gamma = 1/gamma;
241 unsigned i;
242 TRACE("gamma is %f\n", r_gamma);
243 for (i=0; i<256; i++)
244 ramp[i] = pow(i/255.0, r_gamma) * 65535.0;
247 static BOOL ComputeGammaFromRamp(WORD ramp[256], float *gamma)
249 float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
250 unsigned i, f, l, g_n, c;
251 f = ramp[0];
252 l = ramp[255];
253 if (f >= l) {
254 ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
255 return FALSE;
257 r_d = l - f;
258 g_min = g_max = g_avg = 0.0;
259 /* check gamma ramp entries to estimate the gamma */
260 TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
261 for (i=1, g_n=0; i<255; i++) {
262 if (ramp[i] < f || ramp[i] > l) {
263 ERR("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
264 return FALSE;
266 c = ramp[i] - f;
267 if (!c) continue; /* avoid log(0) */
269 /* normalize entry values into 0..1 range */
270 r_x = i/255.0; r_y = c / r_d;
271 /* compute logarithms of values */
272 r_lx = log(r_x); r_ly = log(r_y);
273 /* compute gamma for this entry */
274 r_v = r_ly / r_lx;
275 /* compute differential (error estimate) for this entry */
276 /* some games use table-based logarithms that magnifies the error by 128 */
277 r_e = -r_lx * 128 / (c * r_lx * r_lx);
279 /* compute min & max while compensating for estimated error */
280 if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
281 if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
283 /* add to average */
284 g_avg += r_v;
285 g_n++;
286 /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
288 if (!g_n) {
289 ERR("no gamma data, shouldn't happen\n");
290 return FALSE;
292 g_avg /= g_n;
293 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
294 /* the bias could be because the app wanted something like a "red shift"
295 * like when you're hit in Quake, but XVidMode doesn't support it,
296 * so we have to reject a significant bias */
297 if (f && f > (pow(1/255.0, g_avg) * 65536.0)) {
298 ERR("low-biased gamma ramp (%d), rejected\n", f);
299 return FALSE;
301 /* check that the gamma is reasonably uniform across the ramp */
302 if (g_max - g_min > 0.1) {
303 ERR("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
304 return FALSE;
306 /* ok, now we're pretty sure we can set the desired gamma ramp,
307 * so go for it */
308 *gamma = 1/g_avg;
309 return TRUE;
312 #endif /* X_XF86VidModeSetGamma */
314 /* Hmm... should gamma control be available in desktop mode or not?
315 * I'll assume that it should */
317 BOOL X11DRV_XF86VM_GetGammaRamp(LPDDGAMMARAMP ramp)
319 #ifdef X_XF86VidModeSetGamma
320 XF86VidModeGamma gamma;
321 Bool ret;
323 if (xf86vm_major < 2) return FALSE; /* no gamma control */
324 #ifdef X_XF86VidModeSetGammaRamp
325 else if (xf86vm_use_gammaramp)
327 Bool ret;
328 wine_tsx11_lock();
329 ret = XF86VidModeGetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
330 ramp->red, ramp->green, ramp->blue);
331 wine_tsx11_unlock();
332 return ret;
334 #endif
335 else
337 wine_tsx11_lock();
338 ret = XF86VidModeGetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
339 wine_tsx11_unlock();
340 if (ret) {
341 GenerateRampFromGamma(ramp->red, gamma.red);
342 GenerateRampFromGamma(ramp->green, gamma.green);
343 GenerateRampFromGamma(ramp->blue, gamma.blue);
344 return TRUE;
347 #endif /* X_XF86VidModeSetGamma */
348 return FALSE;
351 BOOL X11DRV_XF86VM_SetGammaRamp(LPDDGAMMARAMP ramp)
353 #ifdef X_XF86VidModeSetGamma
354 XF86VidModeGamma gamma;
356 if (xf86vm_major < 2) return FALSE; /* no gamma control */
357 #ifdef X_XF86VidModeSetGammaRamp
358 else if (xf86vm_use_gammaramp)
360 Bool ret;
361 wine_tsx11_lock();
362 ret = XF86VidModeSetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
363 ramp->red, ramp->green, ramp->blue);
364 wine_tsx11_unlock();
365 return ret;
367 #endif
368 else
370 if (ComputeGammaFromRamp(ramp->red, &gamma.red) &&
371 ComputeGammaFromRamp(ramp->green, &gamma.green) &&
372 ComputeGammaFromRamp(ramp->blue, &gamma.blue)) {
373 Bool ret;
374 wine_tsx11_lock();
375 ret = XF86VidModeSetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
376 wine_tsx11_unlock();
377 return ret;
380 #endif /* X_XF86VidModeSetGamma */
381 return FALSE;
384 #endif /* HAVE_LIBXXF86VM */
386 /***********************************************************************
387 * GetDeviceGammaRamp (X11DRV.@)
389 * FIXME: should move to somewhere appropriate, but probably not before
390 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
391 * they can include xvidmode.h directly
393 BOOL X11DRV_GetDeviceGammaRamp(X11DRV_PDEVICE *physDev, LPVOID ramp)
395 #ifdef HAVE_LIBXXF86VM
396 return X11DRV_XF86VM_GetGammaRamp(ramp);
397 #else
398 return FALSE;
399 #endif
402 /***********************************************************************
403 * SetDeviceGammaRamp (X11DRV.@)
405 * FIXME: should move to somewhere appropriate, but probably not before
406 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
407 * they can include xvidmode.h directly
409 BOOL X11DRV_SetDeviceGammaRamp(X11DRV_PDEVICE *physDev, LPVOID ramp)
411 #ifdef HAVE_LIBXXF86VM
412 return X11DRV_XF86VM_SetGammaRamp(ramp);
413 #else
414 return FALSE;
415 #endif