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
28 #ifdef HAVE_LIBXXF86VM
31 #include <X11/extensions/xf86vmode.h>
32 #endif /* HAVE_LIBXXF86VM */
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(xvidmode);
45 #ifdef HAVE_LIBXXF86VM
47 extern int usexvidmode;
49 static int xf86vm_event, xf86vm_error, xf86vm_major, xf86vm_minor;
51 #ifdef X_XF86VidModeSetGammaRamp
52 static int xf86vm_gammaramp_size;
53 static BOOL xf86vm_use_gammaramp;
54 #endif /* X_XF86VidModeSetGammaRamp */
56 static LPDDHALMODEINFO dd_modes;
57 static unsigned int dd_mode_count;
58 static XF86VidModeModeInfo** real_xf86vm_modes;
59 static unsigned int real_xf86vm_mode_count;
60 static unsigned int xf86vm_initial_mode = 0;
62 static void convert_modeinfo( const XF86VidModeModeInfo *mode)
65 if (mode->htotal!=0 && mode->vtotal!=0)
66 rate = mode->dotclock * 1000 / (mode->htotal * mode->vtotal);
69 X11DRV_Settings_AddOneMode(mode->hdisplay, mode->vdisplay, 0, rate);
72 static void convert_modeline(int dotclock, const XF86VidModeModeLine *mode, LPDDHALMODEINFO info, unsigned int bpp)
74 info->dwWidth = mode->hdisplay;
75 info->dwHeight = mode->vdisplay;
76 if (mode->htotal!=0 && mode->vtotal!=0)
77 info->wRefreshRate = dotclock * 1000 / (mode->htotal * mode->vtotal);
79 info->wRefreshRate = 0;
80 TRACE(" width=%ld, height=%ld, refresh=%d\n",
81 info->dwWidth, info->dwHeight, info->wRefreshRate);
88 info->dwAlphaBitMask = 0;
91 static int XVidModeErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
96 static Bool in_desktop_mode;
98 int X11DRV_XF86VM_GetCurrentMode(void)
100 XF86VidModeModeLine line;
103 DWORD dwBpp = screen_depth;
104 if (dwBpp == 24) dwBpp = 32;
106 TRACE("Querying XVidMode current mode\n");
108 XF86VidModeGetModeLine(gdi_display, DefaultScreen(gdi_display), &dotclock, &line);
110 convert_modeline(dotclock, &line, &cmode, dwBpp);
111 for (i=0; i<dd_mode_count; i++)
112 if (memcmp(&dd_modes[i], &cmode, sizeof(cmode)) == 0) {
113 TRACE("mode=%d\n", i);
116 ERR("In unknown mode, returning default\n");
117 return xf86vm_initial_mode;
120 void X11DRV_XF86VM_SetCurrentMode(int mode)
122 DWORD dwBpp = screen_depth;
123 if (dwBpp == 24) dwBpp = 32;
124 /* only set modes from the original color depth */
125 if (dwBpp != dd_modes[mode].dwBPP)
127 FIXME("Cannot change screen BPP from %ld to %ld\n", dwBpp, dd_modes[mode].dwBPP);
129 mode = mode % real_xf86vm_mode_count;
132 TRACE("Resizing X display to %dx%d\n",
133 real_xf86vm_modes[mode]->hdisplay, real_xf86vm_modes[mode]->vdisplay);
134 XF86VidModeSwitchToMode(gdi_display, DefaultScreen(gdi_display), real_xf86vm_modes[mode]);
136 SYSMETRICS_Set( SM_CXSCREEN, real_xf86vm_modes[mode]->hdisplay );
137 SYSMETRICS_Set( SM_CYSCREEN, real_xf86vm_modes[mode]->vdisplay );
139 FIXME("Need to update SYSMETRICS after resizing display (now %dx%d)\n",
140 real_xf86vm_modes[mode]->hdisplay, real_xf86vm_modes[mode]->vdisplay);
142 #if 0 /* it is said that SetViewPort causes problems with some X servers */
143 XF86VidModeSetViewPort(gdi_display, DefaultScreen(gdi_display), 0, 0);
145 XWarpPointer(gdi_display, None, DefaultRootWindow(gdi_display), 0, 0, 0, 0, 0, 0);
147 XSync(gdi_display, False);
151 void X11DRV_XF86VM_Init(void)
155 DWORD dwBpp = screen_depth;
156 if (dwBpp == 24) dwBpp = 32;
158 in_desktop_mode = (root_window != DefaultRootWindow(gdi_display));
160 if (xf86vm_major) return; /* already initialized? */
162 if (!usexvidmode) return;
164 /* see if XVidMode is available */
166 ok = XF86VidModeQueryExtension(gdi_display, &xf86vm_event, &xf86vm_error);
169 X11DRV_expect_error(gdi_display, XVidModeErrorHandler, NULL);
170 ok = XF86VidModeQueryVersion(gdi_display, &xf86vm_major, &xf86vm_minor);
171 if (X11DRV_check_error()) ok = FALSE;
175 #ifdef X_XF86VidModeSetGammaRamp
176 if (xf86vm_major > 2 || (xf86vm_major == 2 && xf86vm_minor >= 1))
178 XF86VidModeGetGammaRampSize(gdi_display, DefaultScreen(gdi_display),
179 &xf86vm_gammaramp_size);
180 if (xf86vm_gammaramp_size == 256)
181 xf86vm_use_gammaramp = TRUE;
183 #endif /* X_XF86VidModeSetGammaRamp */
186 if (!in_desktop_mode) ok = XF86VidModeGetAllModeLines(gdi_display, DefaultScreen(gdi_display), &nmodes, &real_xf86vm_modes);
191 /* In desktop mode, do not switch resolution... But still use the Gamma ramp stuff */
192 if (in_desktop_mode) return;
194 TRACE("XVidMode modes: count=%d\n", nmodes);
196 real_xf86vm_mode_count = nmodes;
198 dd_modes = X11DRV_Settings_SetHandlers("XF86VidMode",
199 X11DRV_XF86VM_GetCurrentMode,
200 X11DRV_XF86VM_SetCurrentMode,
203 /* convert modes to DDHALMODEINFO format */
204 for (i=0; i<real_xf86vm_mode_count; i++)
206 convert_modeinfo(real_xf86vm_modes[i]);
208 /* add modes for different color depths */
209 X11DRV_Settings_AddDepthModes();
210 dd_mode_count = X11DRV_Settings_GetModeCount();
212 TRACE("Available DD modes: count=%d\n", dd_mode_count);
214 /* store the current mode at the time we started */
215 xf86vm_initial_mode = X11DRV_XF86VM_GetCurrentMode();
216 X11DRV_Settings_SetDefaultMode(xf86vm_initial_mode);
218 TRACE("Enabling XVidMode\n");
221 void X11DRV_XF86VM_Cleanup(void)
224 if (real_xf86vm_modes) XFree(real_xf86vm_modes);
228 void X11DRV_XF86VM_SetExclusiveMode(int lock)
230 if (!dd_modes) return; /* no XVidMode */
233 XF86VidModeLockModeSwitch(gdi_display, DefaultScreen(gdi_display), lock);
237 /***** GAMMA CONTROL *****/
238 /* (only available in XF86VidMode 2.x) */
240 #ifdef X_XF86VidModeSetGamma
242 static void GenerateRampFromGamma(WORD ramp[256], float gamma)
244 float r_gamma = 1/gamma;
246 TRACE("gamma is %f\n", r_gamma);
247 for (i=0; i<256; i++)
248 ramp[i] = pow(i/255.0, r_gamma) * 65535.0;
251 static BOOL ComputeGammaFromRamp(WORD ramp[256], float *gamma)
253 float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
254 unsigned i, f, l, g_n, c;
258 ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
262 g_min = g_max = g_avg = 0.0;
263 /* check gamma ramp entries to estimate the gamma */
264 TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
265 for (i=1, g_n=0; i<255; i++) {
266 if (ramp[i] < f || ramp[i] > l) {
267 ERR("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
271 if (!c) continue; /* avoid log(0) */
273 /* normalize entry values into 0..1 range */
274 r_x = i/255.0; r_y = c / r_d;
275 /* compute logarithms of values */
276 r_lx = log(r_x); r_ly = log(r_y);
277 /* compute gamma for this entry */
279 /* compute differential (error estimate) for this entry */
280 /* some games use table-based logarithms that magnifies the error by 128 */
281 r_e = -r_lx * 128 / (c * r_lx * r_lx);
283 /* compute min & max while compensating for estimated error */
284 if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
285 if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
290 /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
293 ERR("no gamma data, shouldn't happen\n");
297 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
298 /* the bias could be because the app wanted something like a "red shift"
299 * like when you're hit in Quake, but XVidMode doesn't support it,
300 * so we have to reject a significant bias */
301 if (f && f > (pow(1/255.0, g_avg) * 65536.0)) {
302 ERR("low-biased gamma ramp (%d), rejected\n", f);
305 /* check that the gamma is reasonably uniform across the ramp */
306 if (g_max - g_min > 0.1) {
307 ERR("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
310 /* ok, now we're pretty sure we can set the desired gamma ramp,
316 #endif /* X_XF86VidModeSetGamma */
318 /* Hmm... should gamma control be available in desktop mode or not?
319 * I'll assume that it should */
321 BOOL X11DRV_XF86VM_GetGammaRamp(LPDDGAMMARAMP ramp)
323 #ifdef X_XF86VidModeSetGamma
324 XF86VidModeGamma gamma;
327 if (xf86vm_major < 2) return FALSE; /* no gamma control */
328 #ifdef X_XF86VidModeSetGammaRamp
329 else if (xf86vm_use_gammaramp)
333 ret = XF86VidModeGetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
334 ramp->red, ramp->green, ramp->blue);
342 ret = XF86VidModeGetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
345 GenerateRampFromGamma(ramp->red, gamma.red);
346 GenerateRampFromGamma(ramp->green, gamma.green);
347 GenerateRampFromGamma(ramp->blue, gamma.blue);
351 #endif /* X_XF86VidModeSetGamma */
355 BOOL X11DRV_XF86VM_SetGammaRamp(LPDDGAMMARAMP ramp)
357 #ifdef X_XF86VidModeSetGamma
358 XF86VidModeGamma gamma;
360 if (xf86vm_major < 2) return FALSE; /* no gamma control */
361 #ifdef X_XF86VidModeSetGammaRamp
362 else if (xf86vm_use_gammaramp)
366 ret = XF86VidModeSetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
367 ramp->red, ramp->green, ramp->blue);
374 if (ComputeGammaFromRamp(ramp->red, &gamma.red) &&
375 ComputeGammaFromRamp(ramp->green, &gamma.green) &&
376 ComputeGammaFromRamp(ramp->blue, &gamma.blue)) {
379 ret = XF86VidModeSetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
384 #endif /* X_XF86VidModeSetGamma */
388 #endif /* HAVE_LIBXXF86VM */
390 /***********************************************************************
391 * GetDeviceGammaRamp (X11DRV.@)
393 * FIXME: should move to somewhere appropriate, but probably not before
394 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
395 * they can include xvidmode.h directly
397 BOOL X11DRV_GetDeviceGammaRamp(X11DRV_PDEVICE *physDev, LPVOID ramp)
399 #ifdef HAVE_LIBXXF86VM
400 return X11DRV_XF86VM_GetGammaRamp(ramp);
406 /***********************************************************************
407 * SetDeviceGammaRamp (X11DRV.@)
409 * FIXME: should move to somewhere appropriate, but probably not before
410 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
411 * they can include xvidmode.h directly
413 BOOL X11DRV_SetDeviceGammaRamp(X11DRV_PDEVICE *physDev, LPVOID ramp)
415 #ifdef HAVE_LIBXXF86VM
416 return X11DRV_XF86VM_SetGammaRamp(ramp);