Reimplemented DXGrab with improvements; it no longer depends on
[wine/dcerpc.git] / dlls / x11drv / xvidmode.c
blob40980fa50c93b471c982b6c125c8269b035360d7
1 /*
2 * DirectDraw XVidMode interface
4 * Copyright 2001 TransGaming Technologies, Inc.
5 */
7 #include "config.h"
8 #include <string.h>
10 /* FIXME: ChangeDisplaySettings ought to be able to use this */
12 #ifdef HAVE_LIBXXF86VM
14 #include "ts_xlib.h"
15 #include "ts_xf86vmode.h"
16 #include "x11drv.h"
17 #include "x11ddraw.h"
18 #include "xvidmode.h"
20 #include "windef.h"
21 #include "wingdi.h"
22 #include "ddrawi.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... */ \
41 info->lPitch = 0; \
42 info->dwBPP = 0; \
43 info->wFlags = 0; \
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)
61 int nmodes, i;
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;
72 /* retrieve modes */
73 if (!TSXF86VidModeGetAllModeLines(display, DefaultScreen(display), &nmodes,
74 &modes))
75 return;
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;
97 int dotclock, i;
98 DDHALMODEINFO cmode;
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);
108 return 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);
121 #else
122 TSXWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, 0, 0);
123 #endif
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;
152 return TRUE;
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;
164 unsigned i;
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;
174 f = ramp[0];
175 l = ramp[255];
176 if (f >= l) {
177 ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
178 return FALSE;
180 r_d = l - f;
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);
187 return FALSE;
189 c = ramp[i] - f;
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 */
197 r_v = r_ly / r_lx;
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;
206 /* add to average */
207 g_avg += r_v;
208 g_n++;
209 /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
211 if (!g_n) {
212 ERR("no gamma data, shouldn't happen\n");
213 return FALSE;
215 g_avg /= g_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);
222 return FALSE;
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);
227 return FALSE;
229 /* ok, now we're pretty sure we can set the desired gamma ramp,
230 * so go for it */
231 *gamma = 1/g_avg;
232 return TRUE;
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;
244 Bool ret;
246 if (xf86vm_major < 2) return FALSE; /* no gamma control */
247 wine_tsx11_lock();
248 ret = XF86VidModeGetGamma(display, DefaultScreen(display), &gamma);
249 wine_tsx11_unlock();
250 if (ret) {
251 GenerateRampFromGamma(ramp->red, gamma.red);
252 GenerateRampFromGamma(ramp->green, gamma.green);
253 GenerateRampFromGamma(ramp->blue, gamma.blue);
254 return TRUE;
256 #endif /* X_XF86VidModeSetGamma */
257 return FALSE;
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)) {
269 Bool ret;
270 wine_tsx11_lock();
271 ret = XF86VidModeSetGamma(display, DefaultScreen(display), &gamma);
272 wine_tsx11_unlock();
273 return ret;
275 #endif /* X_XF86VidModeSetGamma */
276 return FALSE;
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);
288 #else
289 return FALSE;
290 #endif
293 BOOL X11DRV_SetDeviceGammaRamp(DC *dc, LPVOID ramp)
295 #ifdef HAVE_LIBXXF86VM
296 return X11DRV_XF86VM_SetGammaRamp(ramp);
297 #else
298 return FALSE;
299 #endif