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 LPDDHALMODEINFO xf86vm_modes;
32 unsigned xf86vm_mode_count;
33 XF86VidModeModeInfo** modes;
35 static void convert_modeinfo( const XF86VidModeModeInfo *mode, LPDDHALMODEINFO info )
37 info->dwWidth = mode->hdisplay;
38 info->dwHeight = mode->vdisplay;
39 if (mode->htotal!=0 && mode->vtotal!=0)
40 info->wRefreshRate = mode->dotclock * 1000 / (mode->htotal * mode->vtotal);
42 info->wRefreshRate = 0;
43 TRACE(" width=%ld, height=%ld, refresh=%d\n",
44 info->dwWidth, info->dwHeight, info->wRefreshRate);
45 /* XVidMode cannot change display depths... */
46 /* let's not bother with filling out these then... */
53 info->dwAlphaBitMask = 0;
56 static void convert_modeline(int dotclock, const XF86VidModeModeLine *mode, LPDDHALMODEINFO info)
58 info->dwWidth = mode->hdisplay;
59 info->dwHeight = mode->vdisplay;
60 if (mode->htotal!=0 && mode->vtotal!=0)
61 info->wRefreshRate = dotclock * 1000 / (mode->htotal * mode->vtotal);
63 info->wRefreshRate = 0;
64 TRACE(" width=%ld, height=%ld, refresh=%d\n",
65 info->dwWidth, info->dwHeight, info->wRefreshRate);
66 /* XVidMode cannot change display depths... */
67 /* let's not bother with filling out these then... */
74 info->dwAlphaBitMask = 0;
77 void X11DRV_XF86VM_Init(void)
81 if (xf86vm_major) return; /* already initialized? */
83 /* if in desktop mode, don't use XVidMode */
84 if (root_window != DefaultRootWindow(gdi_display)) return;
86 if (!usexvidmode) return;
88 /* see if XVidMode is available */
89 if (!TSXF86VidModeQueryExtension(gdi_display, &xf86vm_event, &xf86vm_error)) return;
90 if (!TSXF86VidModeQueryVersion(gdi_display, &xf86vm_major, &xf86vm_minor)) return;
93 if (!TSXF86VidModeGetAllModeLines(gdi_display, DefaultScreen(gdi_display), &nmodes,
97 TRACE("XVidMode modes: count=%d\n", nmodes);
99 xf86vm_mode_count = nmodes;
100 xf86vm_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDHALMODEINFO) * nmodes);
102 /* convert modes to DDHALMODEINFO format */
103 for (i=0; i<nmodes; i++)
104 convert_modeinfo(modes[i], &xf86vm_modes[i]);
106 TRACE("Enabling XVidMode\n");
109 void X11DRV_XF86VM_Cleanup(void)
111 if (modes) TSXFree(modes);
114 int X11DRV_XF86VM_GetCurrentMode(void)
116 XF86VidModeModeLine line;
120 if (!xf86vm_modes) return 0; /* no XVidMode */
122 TRACE("Querying XVidMode current mode\n");
123 TSXF86VidModeGetModeLine(gdi_display, DefaultScreen(gdi_display), &dotclock, &line);
124 convert_modeline(dotclock, &line, &cmode);
125 for (i=0; i<xf86vm_mode_count; i++)
126 if (memcmp(&xf86vm_modes[i], &cmode, sizeof(cmode)) == 0) {
127 TRACE("mode=%d\n", i);
130 ERR("unknown mode, shouldn't happen\n");
131 return 0; /* return first mode */
134 void X11DRV_XF86VM_SetCurrentMode(int mode)
136 if (!xf86vm_modes) return; /* no XVidMode */
138 TSXF86VidModeSwitchToMode(gdi_display, DefaultScreen(gdi_display), modes[mode]);
139 #if 0 /* it is said that SetViewPort causes problems with some X servers */
140 TSXF86VidModeSetViewPort(gdi_display, DefaultScreen(gdi_display), 0, 0);
142 TSXWarpPointer(gdi_display, None, DefaultRootWindow(gdi_display), 0, 0, 0, 0, 0, 0);
144 TSXSync(gdi_display, False);
147 void X11DRV_XF86VM_SetExclusiveMode(int lock)
149 if (!xf86vm_modes) return; /* no XVidMode */
151 TSXF86VidModeLockModeSwitch(gdi_display, DefaultScreen(gdi_display), lock);
154 /* actual DirectDraw HAL stuff */
156 static DWORD PASCAL X11DRV_XF86VM_SetMode(LPDDHAL_SETMODEDATA data)
158 X11DRV_XF86VM_SetCurrentMode(data->dwModeIndex);
159 X11DRV_DDHAL_SwitchMode(data->dwModeIndex, NULL, NULL);
160 data->ddRVal = DD_OK;
161 return DDHAL_DRIVER_HANDLED;
164 int X11DRV_XF86VM_CreateDriver(LPDDHALINFO info)
166 if (!xf86vm_mode_count) return 0; /* no XVidMode */
168 info->dwNumModes = xf86vm_mode_count;
169 info->lpModeInfo = xf86vm_modes;
170 X11DRV_DDHAL_SwitchMode(X11DRV_XF86VM_GetCurrentMode(), NULL, NULL);
171 info->lpDDCallbacks->SetMode = X11DRV_XF86VM_SetMode;
176 /***** GAMMA CONTROL *****/
177 /* (only available in XF86VidMode 2.x) */
179 #ifdef X_XF86VidModeSetGamma
181 static void GenerateRampFromGamma(WORD ramp[256], float gamma)
183 float r_gamma = 1/gamma;
185 TRACE("gamma is %f\n", r_gamma);
186 for (i=0; i<256; i++)
187 ramp[i] = pow(i/255.0, r_gamma) * 65535.0;
190 static BOOL ComputeGammaFromRamp(WORD ramp[256], float *gamma)
192 float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
193 unsigned i, f, l, g_n, c;
197 ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
201 g_min = g_max = g_avg = 0.0;
202 /* check gamma ramp entries to estimate the gamma */
203 TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
204 for (i=1, g_n=0; i<255; i++) {
205 if (ramp[i] < f || ramp[i] > l) {
206 ERR("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
210 if (!c) continue; /* avoid log(0) */
212 /* normalize entry values into 0..1 range */
213 r_x = i/255.0; r_y = c / r_d;
214 /* compute logarithms of values */
215 r_lx = log(r_x); r_ly = log(r_y);
216 /* compute gamma for this entry */
218 /* compute differential (error estimate) for this entry */
219 /* some games use table-based logarithms that magnifies the error by 128 */
220 r_e = -r_lx * 128 / (c * r_lx * r_lx);
222 /* compute min & max while compensating for estimated error */
223 if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
224 if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
229 /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
232 ERR("no gamma data, shouldn't happen\n");
236 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
237 /* the bias could be because the app wanted something like a "red shift"
238 * like when you're hit in Quake, but XVidMode doesn't support it,
239 * so we have to reject a significant bias */
240 if (f && f > (pow(1/255.0, g_avg) * 65536.0)) {
241 ERR("low-biased gamma ramp (%d), rejected\n", f);
244 /* check that the gamma is reasonably uniform across the ramp */
245 if (g_max - g_min > 0.1) {
246 ERR("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
249 /* ok, now we're pretty sure we can set the desired gamma ramp,
255 #endif /* X_XF86VidModeSetGamma */
257 /* Hmm... should gamma control be available in desktop mode or not?
258 * I'll assume that it should */
260 BOOL X11DRV_XF86VM_GetGammaRamp(LPDDGAMMARAMP ramp)
262 #ifdef X_XF86VidModeSetGamma
263 XF86VidModeGamma gamma;
266 if (xf86vm_major < 2) return FALSE; /* no gamma control */
268 ret = XF86VidModeGetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
271 GenerateRampFromGamma(ramp->red, gamma.red);
272 GenerateRampFromGamma(ramp->green, gamma.green);
273 GenerateRampFromGamma(ramp->blue, gamma.blue);
276 #endif /* X_XF86VidModeSetGamma */
280 BOOL X11DRV_XF86VM_SetGammaRamp(LPDDGAMMARAMP ramp)
282 #ifdef X_XF86VidModeSetGamma
283 XF86VidModeGamma gamma;
285 if (xf86vm_major < 2) return FALSE; /* no gamma control */
286 if (ComputeGammaFromRamp(ramp->red, &gamma.red) &&
287 ComputeGammaFromRamp(ramp->green, &gamma.green) &&
288 ComputeGammaFromRamp(ramp->blue, &gamma.blue)) {
291 ret = XF86VidModeSetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
295 #endif /* X_XF86VidModeSetGamma */
299 #endif /* HAVE_LIBXXF86VM */
301 /***********************************************************************
302 * GetDeviceGammaRamp (X11DRV.@)
304 * FIXME: should move to somewhere appropriate, but probably not before
305 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
306 * they can include xvidmode.h directly
308 BOOL X11DRV_GetDeviceGammaRamp(DC *dc, LPVOID ramp)
310 #ifdef HAVE_LIBXXF86VM
311 return X11DRV_XF86VM_GetGammaRamp(ramp);
317 /***********************************************************************
318 * SetDeviceGammaRamp (X11DRV.@)
320 * FIXME: should move to somewhere appropriate, but probably not before
321 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
322 * they can include xvidmode.h directly
324 BOOL X11DRV_SetDeviceGammaRamp(DC *dc, LPVOID ramp)
326 #ifdef HAVE_LIBXXF86VM
327 return X11DRV_XF86VM_SetGammaRamp(ramp);