2 * DirectDraw XVidMode interface
4 * Copyright 2001 TransGaming Technologies, Inc.
10 /* FIXME: ChangeDisplaySettings ought to be able to use this */
12 #ifdef HAVE_LIBXXF86VM
15 #include "ts_xf86vmode.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(x11drv);
27 static int xf86vm_event, xf86vm_error, xf86vm_major, xf86vm_minor;
29 LPDDHALMODEINFO xf86vm_modes;
30 unsigned xf86vm_mode_count;
31 XF86VidModeModeInfo** modes;
33 #define CONVERT_MODE(dotclock) \
34 info->dwWidth = mode->hdisplay; \
35 info->dwHeight = mode->vdisplay; \
36 info->wRefreshRate = dotclock * 1000 / (mode->htotal * mode->vtotal); \
37 TRACE(" width=%ld, height=%ld, refresh=%d\n", \
38 info->dwWidth, info->dwHeight, info->wRefreshRate); \
39 /* XVidMode cannot change display depths... */ \
40 /* let's not bother with filling out these then... */ \
44 info->dwRBitMask = 0; \
45 info->dwGBitMask = 0; \
46 info->dwBBitMask = 0; \
47 info->dwAlphaBitMask = 0;
49 static void convert_modeinfo(XF86VidModeModeInfo *mode, LPDDHALMODEINFO info)
51 CONVERT_MODE(mode->dotclock)
54 static void convert_modeline(int dotclock, XF86VidModeModeLine *mode, LPDDHALMODEINFO info)
56 CONVERT_MODE(dotclock)
59 void X11DRV_XF86VM_Init(void)
63 if (xf86vm_major) return; /* already initialized? */
65 /* see if XVidMode is available */
66 if (!TSXF86VidModeQueryExtension(display, &xf86vm_event, &xf86vm_error)) return;
67 if (!TSXF86VidModeQueryVersion(display, &xf86vm_major, &xf86vm_minor)) return;
69 /* if in desktop mode, don't use XVidMode */
70 if (X11DRV_GetXRootWindow() != DefaultRootWindow(display)) return;
73 if (!TSXF86VidModeGetAllModeLines(display, DefaultScreen(display), &nmodes,
77 TRACE("XVidMode modes: count=%d\n", nmodes);
79 xf86vm_mode_count = nmodes;
80 xf86vm_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDHALMODEINFO) * nmodes);
82 /* convert modes to DDHALMODEINFO format */
83 for (i=0; i<nmodes; i++)
84 convert_modeinfo(modes[i], &xf86vm_modes[i]);
86 TRACE("Enabling XVidMode\n");
89 void X11DRV_XF86VM_Cleanup(void)
91 if (modes) TSXFree(modes);
94 int X11DRV_XF86VM_GetCurrentMode(void)
96 XF86VidModeModeLine line;
100 if (!xf86vm_modes) return 0; /* no XVidMode */
102 TRACE("Querying XVidMode current mode\n");
103 TSXF86VidModeGetModeLine(display, DefaultScreen(display), &dotclock, &line);
104 convert_modeline(dotclock, &line, &cmode);
105 for (i=0; i<xf86vm_mode_count; i++)
106 if (memcmp(&xf86vm_modes[i], &cmode, sizeof(cmode)) == 0) {
107 TRACE("mode=%d\n", i);
110 ERR("unknown mode, shouldn't happen\n");
111 return 0; /* return first mode */
114 void X11DRV_XF86VM_SetCurrentMode(int mode)
116 if (!xf86vm_modes) return; /* no XVidMode */
118 TSXF86VidModeSwitchToMode(display, DefaultScreen(display), modes[mode]);
119 #if 0 /* it is said that SetViewPort causes problems with some X servers */
120 TSXF86VidModeSetViewPort(display, DefaultScreen(display), 0, 0);
122 TSXWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, 0, 0);
124 TSXSync(display, False);
127 void X11DRV_XF86VM_SetExclusiveMode(int lock)
129 if (!xf86vm_modes) return; /* no XVidMode */
131 TSXF86VidModeLockModeSwitch(display, DefaultScreen(display), lock);
134 /* actual DirectDraw HAL stuff */
136 static DWORD PASCAL X11DRV_XF86VM_SetMode(LPDDHAL_SETMODEDATA data)
138 X11DRV_XF86VM_SetCurrentMode(data->dwModeIndex);
139 X11DRV_DDHAL_SwitchMode(data->dwModeIndex, NULL, NULL);
140 data->ddRVal = DD_OK;
141 return DDHAL_DRIVER_HANDLED;
144 int X11DRV_XF86VM_CreateDriver(LPDDHALINFO info)
146 if (!xf86vm_mode_count) return 0; /* no XVidMode */
148 info->dwNumModes = xf86vm_mode_count;
149 info->lpModeInfo = xf86vm_modes;
150 X11DRV_DDHAL_SwitchMode(X11DRV_XF86VM_GetCurrentMode(), NULL, NULL);
151 info->lpDDCallbacks->SetMode = X11DRV_XF86VM_SetMode;
156 /***** GAMMA CONTROL *****/
157 /* (only available in XF86VidMode 2.x) */
159 #ifdef X_XF86VidModeSetGamma
161 static void GenerateRampFromGamma(WORD ramp[256], float gamma)
163 float r_gamma = 1/gamma;
165 TRACE("gamma is %f\n", r_gamma);
166 for (i=0; i<256; i++)
167 ramp[i] = pow(i/255.0, r_gamma) * 65535.0;
170 static BOOL ComputeGammaFromRamp(WORD ramp[256], float *gamma)
172 float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
173 unsigned i, f, l, g_n, c;
177 ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
181 g_min = g_max = g_avg = 0.0;
182 /* check gamma ramp entries to estimate the gamma */
183 TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
184 for (i=1, g_n=0; i<255; i++) {
185 if (ramp[i] < f || ramp[i] > l) {
186 ERR("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
190 if (!c) continue; /* avoid log(0) */
192 /* normalize entry values into 0..1 range */
193 r_x = i/255.0; r_y = c / r_d;
194 /* compute logarithms of values */
195 r_lx = log(r_x); r_ly = log(r_y);
196 /* compute gamma for this entry */
198 /* compute differential (error estimate) for this entry */
199 /* some games use table-based logarithms that magnifies the error by 128 */
200 r_e = -r_lx * 128 / (c * r_lx * r_lx);
202 /* compute min & max while compensating for estimated error */
203 if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
204 if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
209 /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
212 ERR("no gamma data, shouldn't happen\n");
216 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
217 /* the bias could be because the app wanted something like a "red shift"
218 * like when you're hit in Quake, but XVidMode doesn't support it,
219 * so we have to reject a significant bias */
220 if (f && f > (pow(1/255.0, g_avg) * 65536.0)) {
221 ERR("low-biased gamma ramp (%d), rejected\n", f);
224 /* check that the gamma is reasonably uniform across the ramp */
225 if (g_max - g_min > 0.1) {
226 ERR("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
229 /* ok, now we're pretty sure we can set the desired gamma ramp,
235 #endif /* X_XF86VidModeSetGamma */
237 /* Hmm... should gamma control be available in desktop mode or not?
238 * I'll assume that it should */
240 BOOL X11DRV_XF86VM_GetGammaRamp(LPDDGAMMARAMP ramp)
242 #ifdef X_XF86VidModeSetGamma
243 XF86VidModeGamma gamma;
246 if (xf86vm_major < 2) return FALSE; /* no gamma control */
248 ret = XF86VidModeGetGamma(display, DefaultScreen(display), &gamma);
251 GenerateRampFromGamma(ramp->red, gamma.red);
252 GenerateRampFromGamma(ramp->green, gamma.green);
253 GenerateRampFromGamma(ramp->blue, gamma.blue);
256 #endif /* X_XF86VidModeSetGamma */
260 BOOL X11DRV_XF86VM_SetGammaRamp(LPDDGAMMARAMP ramp)
262 #ifdef X_XF86VidModeSetGamma
263 XF86VidModeGamma gamma;
265 if (xf86vm_major < 2) return FALSE; /* no gamma control */
266 if (ComputeGammaFromRamp(ramp->red, &gamma.red) &&
267 ComputeGammaFromRamp(ramp->green, &gamma.green) &&
268 ComputeGammaFromRamp(ramp->blue, &gamma.blue)) {
271 ret = XF86VidModeSetGamma(display, DefaultScreen(display), &gamma);
275 #endif /* X_XF86VidModeSetGamma */
279 #endif /* HAVE_LIBXXF86VM */
281 /* FIXME: should move these somewhere appropriate, but probably not before
282 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
283 * they can include xvidmode.h directly */
284 BOOL X11DRV_GetDeviceGammaRamp(DC *dc, LPVOID ramp)
286 #ifdef HAVE_LIBXXF86VM
287 return X11DRV_XF86VM_GetGammaRamp(ramp);
293 BOOL X11DRV_SetDeviceGammaRamp(DC *dc, LPVOID ramp)
295 #ifdef HAVE_LIBXXF86VM
296 return X11DRV_XF86VM_SetGammaRamp(ramp);