2 * DirectDraw XVidMode interface
4 * Copyright 2001 TransGaming Technologies, Inc.
7 /* FIXME: ChangeDisplaySettings ought to be able to use this */
13 #include "ts_xf86vmode.h"
21 #include "debugtools.h"
23 DEFAULT_DEBUG_CHANNEL(x11drv);
25 #ifdef HAVE_LIBXXF86VM
27 extern int usexvidmode;
29 static int xf86vm_event, xf86vm_error, xf86vm_major, xf86vm_minor;
31 #ifdef X_XF86VidModeSetGammaRamp
32 static int xf86vm_gammaramp_size;
33 static BOOL xf86vm_use_gammaramp;
36 LPDDHALMODEINFO xf86vm_modes;
37 unsigned xf86vm_mode_count;
38 XF86VidModeModeInfo** modes;
40 static void convert_modeinfo( const XF86VidModeModeInfo *mode, LPDDHALMODEINFO info )
42 info->dwWidth = mode->hdisplay;
43 info->dwHeight = mode->vdisplay;
44 if (mode->htotal!=0 && mode->vtotal!=0)
45 info->wRefreshRate = mode->dotclock * 1000 / (mode->htotal * mode->vtotal);
47 info->wRefreshRate = 0;
48 TRACE(" width=%ld, height=%ld, refresh=%d\n",
49 info->dwWidth, info->dwHeight, info->wRefreshRate);
50 /* XVidMode cannot change display depths... */
51 /* let's not bother with filling out these then... */
58 info->dwAlphaBitMask = 0;
61 static void convert_modeline(int dotclock, const XF86VidModeModeLine *mode, LPDDHALMODEINFO info)
63 info->dwWidth = mode->hdisplay;
64 info->dwHeight = mode->vdisplay;
65 if (mode->htotal!=0 && mode->vtotal!=0)
66 info->wRefreshRate = dotclock * 1000 / (mode->htotal * mode->vtotal);
68 info->wRefreshRate = 0;
69 TRACE(" width=%ld, height=%ld, refresh=%d\n",
70 info->dwWidth, info->dwHeight, info->wRefreshRate);
71 /* XVidMode cannot change display depths... */
72 /* let's not bother with filling out these then... */
79 info->dwAlphaBitMask = 0;
82 void X11DRV_XF86VM_Init(void)
86 if (xf86vm_major) return; /* already initialized? */
88 /* if in desktop mode, don't use XVidMode */
89 if (root_window != DefaultRootWindow(gdi_display)) return;
91 if (!usexvidmode) return;
93 /* see if XVidMode is available */
94 if (!TSXF86VidModeQueryExtension(gdi_display, &xf86vm_event, &xf86vm_error)) return;
95 if (!TSXF86VidModeQueryVersion(gdi_display, &xf86vm_major, &xf86vm_minor)) return;
97 #ifdef X_XF86VidModeSetGammaRamp
98 if (xf86vm_major > 2 || (xf86vm_major == 2 && xf86vm_minor >= 1))
101 XF86VidModeGetGammaRampSize(gdi_display, DefaultScreen(gdi_display),
102 &xf86vm_gammaramp_size);
105 if (xf86vm_gammaramp_size == 256)
106 xf86vm_use_gammaramp = TRUE;
111 if (!TSXF86VidModeGetAllModeLines(gdi_display, DefaultScreen(gdi_display), &nmodes,
115 TRACE("XVidMode modes: count=%d\n", nmodes);
117 xf86vm_mode_count = nmodes;
118 xf86vm_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDHALMODEINFO) * nmodes);
120 /* convert modes to DDHALMODEINFO format */
121 for (i=0; i<nmodes; i++)
122 convert_modeinfo(modes[i], &xf86vm_modes[i]);
124 TRACE("Enabling XVidMode\n");
127 void X11DRV_XF86VM_Cleanup(void)
129 if (modes) TSXFree(modes);
132 int X11DRV_XF86VM_GetCurrentMode(void)
134 XF86VidModeModeLine line;
138 if (!xf86vm_modes) return 0; /* no XVidMode */
140 TRACE("Querying XVidMode current mode\n");
141 TSXF86VidModeGetModeLine(gdi_display, DefaultScreen(gdi_display), &dotclock, &line);
142 convert_modeline(dotclock, &line, &cmode);
143 for (i=0; i<xf86vm_mode_count; i++)
144 if (memcmp(&xf86vm_modes[i], &cmode, sizeof(cmode)) == 0) {
145 TRACE("mode=%d\n", i);
148 ERR("unknown mode, shouldn't happen\n");
149 return 0; /* return first mode */
152 void X11DRV_XF86VM_SetCurrentMode(int mode)
154 if (!xf86vm_modes) return; /* no XVidMode */
156 TSXF86VidModeSwitchToMode(gdi_display, DefaultScreen(gdi_display), modes[mode]);
157 #if 0 /* it is said that SetViewPort causes problems with some X servers */
158 TSXF86VidModeSetViewPort(gdi_display, DefaultScreen(gdi_display), 0, 0);
160 TSXWarpPointer(gdi_display, None, DefaultRootWindow(gdi_display), 0, 0, 0, 0, 0, 0);
162 TSXSync(gdi_display, False);
165 void X11DRV_XF86VM_SetExclusiveMode(int lock)
167 if (!xf86vm_modes) return; /* no XVidMode */
169 TSXF86VidModeLockModeSwitch(gdi_display, DefaultScreen(gdi_display), lock);
172 /* actual DirectDraw HAL stuff */
174 static DWORD PASCAL X11DRV_XF86VM_SetMode(LPDDHAL_SETMODEDATA data)
176 X11DRV_XF86VM_SetCurrentMode(data->dwModeIndex);
177 X11DRV_DDHAL_SwitchMode(data->dwModeIndex, NULL, NULL);
178 data->ddRVal = DD_OK;
179 return DDHAL_DRIVER_HANDLED;
182 int X11DRV_XF86VM_CreateDriver(LPDDHALINFO info)
184 if (!xf86vm_mode_count) return 0; /* no XVidMode */
186 info->dwNumModes = xf86vm_mode_count;
187 info->lpModeInfo = xf86vm_modes;
188 X11DRV_DDHAL_SwitchMode(X11DRV_XF86VM_GetCurrentMode(), NULL, NULL);
189 info->lpDDCallbacks->SetMode = X11DRV_XF86VM_SetMode;
194 /***** GAMMA CONTROL *****/
195 /* (only available in XF86VidMode 2.x) */
197 #ifdef X_XF86VidModeSetGamma
199 static void GenerateRampFromGamma(WORD ramp[256], float gamma)
201 float r_gamma = 1/gamma;
203 TRACE("gamma is %f\n", r_gamma);
204 for (i=0; i<256; i++)
205 ramp[i] = pow(i/255.0, r_gamma) * 65535.0;
208 static BOOL ComputeGammaFromRamp(WORD ramp[256], float *gamma)
210 float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
211 unsigned i, f, l, g_n, c;
215 ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
219 g_min = g_max = g_avg = 0.0;
220 /* check gamma ramp entries to estimate the gamma */
221 TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
222 for (i=1, g_n=0; i<255; i++) {
223 if (ramp[i] < f || ramp[i] > l) {
224 ERR("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
228 if (!c) continue; /* avoid log(0) */
230 /* normalize entry values into 0..1 range */
231 r_x = i/255.0; r_y = c / r_d;
232 /* compute logarithms of values */
233 r_lx = log(r_x); r_ly = log(r_y);
234 /* compute gamma for this entry */
236 /* compute differential (error estimate) for this entry */
237 /* some games use table-based logarithms that magnifies the error by 128 */
238 r_e = -r_lx * 128 / (c * r_lx * r_lx);
240 /* compute min & max while compensating for estimated error */
241 if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
242 if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
247 /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
250 ERR("no gamma data, shouldn't happen\n");
254 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
255 /* the bias could be because the app wanted something like a "red shift"
256 * like when you're hit in Quake, but XVidMode doesn't support it,
257 * so we have to reject a significant bias */
258 if (f && f > (pow(1/255.0, g_avg) * 65536.0)) {
259 ERR("low-biased gamma ramp (%d), rejected\n", f);
262 /* check that the gamma is reasonably uniform across the ramp */
263 if (g_max - g_min > 0.1) {
264 ERR("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
267 /* ok, now we're pretty sure we can set the desired gamma ramp,
273 #endif /* X_XF86VidModeSetGamma */
275 /* Hmm... should gamma control be available in desktop mode or not?
276 * I'll assume that it should */
278 BOOL X11DRV_XF86VM_GetGammaRamp(LPDDGAMMARAMP ramp)
280 #ifdef X_XF86VidModeSetGamma
281 XF86VidModeGamma gamma;
284 if (xf86vm_major < 2) return FALSE; /* no gamma control */
285 #ifdef X_XF86VidModeSetGammaRamp
286 else if (xf86vm_use_gammaramp)
290 ret = XF86VidModeGetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
291 ramp->red, ramp->green, ramp->blue);
299 ret = XF86VidModeGetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
302 GenerateRampFromGamma(ramp->red, gamma.red);
303 GenerateRampFromGamma(ramp->green, gamma.green);
304 GenerateRampFromGamma(ramp->blue, gamma.blue);
308 #endif /* X_XF86VidModeSetGamma */
312 BOOL X11DRV_XF86VM_SetGammaRamp(LPDDGAMMARAMP ramp)
314 #ifdef X_XF86VidModeSetGamma
315 XF86VidModeGamma gamma;
317 if (xf86vm_major < 2) return FALSE; /* no gamma control */
318 #ifdef X_XF86VidModeSetGammaRamp
319 else if (xf86vm_use_gammaramp)
323 ret = XF86VidModeSetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
324 ramp->red, ramp->green, ramp->blue);
331 if (ComputeGammaFromRamp(ramp->red, &gamma.red) &&
332 ComputeGammaFromRamp(ramp->green, &gamma.green) &&
333 ComputeGammaFromRamp(ramp->blue, &gamma.blue)) {
336 ret = XF86VidModeSetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
341 #endif /* X_XF86VidModeSetGamma */
345 #endif /* HAVE_LIBXXF86VM */
347 /***********************************************************************
348 * GetDeviceGammaRamp (X11DRV.@)
350 * FIXME: should move to somewhere appropriate, but probably not before
351 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
352 * they can include xvidmode.h directly
354 BOOL X11DRV_GetDeviceGammaRamp(DC *dc, LPVOID ramp)
356 #ifdef HAVE_LIBXXF86VM
357 return X11DRV_XF86VM_GetGammaRamp(ramp);
363 /***********************************************************************
364 * SetDeviceGammaRamp (X11DRV.@)
366 * FIXME: should move to somewhere appropriate, but probably not before
367 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
368 * they can include xvidmode.h directly
370 BOOL X11DRV_SetDeviceGammaRamp(DC *dc, LPVOID ramp)
372 #ifdef HAVE_LIBXXF86VM
373 return X11DRV_XF86VM_SetGammaRamp(ramp);