Thanks to Lucho, text support in samples now starts to
[wine/dcerpc.git] / dlls / x11drv / xvidmode.c
blob5271784b2f40efc97ab3e5bde127072b8d39fd1a
1 /*
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
21 /* FIXME: ChangeDisplaySettings ought to be able to use this */
23 #include "config.h"
24 #include <string.h>
26 #include "ts_xlib.h"
28 #ifdef HAVE_LIBXXF86VM
29 #define XMD_H
30 #include "basetsd.h"
31 #include <X11/extensions/xf86vmode.h>
32 #endif /* HAVE_LIBXXF86VM */
33 #include "x11drv.h"
35 #include "x11ddraw.h"
36 #include "xvidmode.h"
38 #include "windef.h"
39 #include "wingdi.h"
40 #include "ddrawi.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
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
56 LPDDHALMODEINFO xf86vm_modes;
57 unsigned xf86vm_mode_count;
58 XF86VidModeModeInfo** modes;
60 static void convert_modeinfo( const XF86VidModeModeInfo *mode, LPDDHALMODEINFO info )
62 info->dwWidth = mode->hdisplay;
63 info->dwHeight = mode->vdisplay;
64 if (mode->htotal!=0 && mode->vtotal!=0)
65 info->wRefreshRate = mode->dotclock * 1000 / (mode->htotal * mode->vtotal);
66 else
67 info->wRefreshRate = 0;
68 TRACE(" width=%ld, height=%ld, refresh=%d\n",
69 info->dwWidth, info->dwHeight, info->wRefreshRate);
70 /* XVidMode cannot change display depths... */
71 /* let's not bother with filling out these then... */
72 info->lPitch = 0;
73 info->dwBPP = 0;
74 info->wFlags = 0;
75 info->dwRBitMask = 0;
76 info->dwGBitMask = 0;
77 info->dwBBitMask = 0;
78 info->dwAlphaBitMask = 0;
81 static void convert_modeline(int dotclock, const XF86VidModeModeLine *mode, LPDDHALMODEINFO info)
83 info->dwWidth = mode->hdisplay;
84 info->dwHeight = mode->vdisplay;
85 if (mode->htotal!=0 && mode->vtotal!=0)
86 info->wRefreshRate = dotclock * 1000 / (mode->htotal * mode->vtotal);
87 else
88 info->wRefreshRate = 0;
89 TRACE(" width=%ld, height=%ld, refresh=%d\n",
90 info->dwWidth, info->dwHeight, info->wRefreshRate);
91 /* XVidMode cannot change display depths... */
92 /* let's not bother with filling out these then... */
93 info->lPitch = 0;
94 info->dwBPP = 0;
95 info->wFlags = 0;
96 info->dwRBitMask = 0;
97 info->dwGBitMask = 0;
98 info->dwBBitMask = 0;
99 info->dwAlphaBitMask = 0;
102 static int XVidModeErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
104 return 1;
107 void X11DRV_XF86VM_Init(void)
109 int nmodes, i;
110 Bool ok;
112 if (xf86vm_major) return; /* already initialized? */
114 /* if in desktop mode, don't use XVidMode */
115 if (root_window != DefaultRootWindow(gdi_display)) return;
117 if (!usexvidmode) return;
119 /* see if XVidMode is available */
120 wine_tsx11_lock();
121 ok = XF86VidModeQueryExtension(gdi_display, &xf86vm_event, &xf86vm_error);
122 if (ok)
124 X11DRV_expect_error(gdi_display, XVidModeErrorHandler, NULL);
125 ok = XF86VidModeQueryVersion(gdi_display, &xf86vm_major, &xf86vm_minor);
126 if (X11DRV_check_error()) ok = FALSE;
128 if (ok)
130 #ifdef X_XF86VidModeSetGammaRamp
131 if (xf86vm_major > 2 || (xf86vm_major == 2 && xf86vm_minor >= 1))
133 XF86VidModeGetGammaRampSize(gdi_display, DefaultScreen(gdi_display),
134 &xf86vm_gammaramp_size);
135 if (xf86vm_gammaramp_size == 256)
136 xf86vm_use_gammaramp = TRUE;
138 #endif
140 /* retrieve modes */
141 ok = XF86VidModeGetAllModeLines(gdi_display, DefaultScreen(gdi_display), &nmodes, &modes);
143 wine_tsx11_unlock();
144 if (!ok) return;
146 TRACE("XVidMode modes: count=%d\n", nmodes);
148 xf86vm_mode_count = nmodes;
149 xf86vm_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDHALMODEINFO) * nmodes);
151 /* convert modes to DDHALMODEINFO format */
152 for (i=0; i<nmodes; i++)
153 convert_modeinfo(modes[i], &xf86vm_modes[i]);
154 TRACE("Enabling XVidMode\n");
157 void X11DRV_XF86VM_Cleanup(void)
159 if (modes) TSXFree(modes);
162 int X11DRV_XF86VM_GetCurrentMode(void)
164 XF86VidModeModeLine line;
165 int dotclock, i;
166 DDHALMODEINFO cmode;
168 if (!xf86vm_modes) return 0; /* no XVidMode */
170 TRACE("Querying XVidMode current mode\n");
171 wine_tsx11_lock();
172 XF86VidModeGetModeLine(gdi_display, DefaultScreen(gdi_display), &dotclock, &line);
173 wine_tsx11_unlock();
174 convert_modeline(dotclock, &line, &cmode);
175 for (i=0; i<xf86vm_mode_count; i++)
176 if (memcmp(&xf86vm_modes[i], &cmode, sizeof(cmode)) == 0) {
177 TRACE("mode=%d\n", i);
178 return i;
180 ERR("unknown mode, shouldn't happen\n");
181 return 0; /* return first mode */
184 void X11DRV_XF86VM_SetCurrentMode(int mode)
186 if (!xf86vm_modes) return; /* no XVidMode */
188 wine_tsx11_lock();
189 XF86VidModeSwitchToMode(gdi_display, DefaultScreen(gdi_display), modes[mode]);
190 #if 0 /* it is said that SetViewPort causes problems with some X servers */
191 XF86VidModeSetViewPort(gdi_display, DefaultScreen(gdi_display), 0, 0);
192 #else
193 XWarpPointer(gdi_display, None, DefaultRootWindow(gdi_display), 0, 0, 0, 0, 0, 0);
194 #endif
195 XSync(gdi_display, False);
196 wine_tsx11_unlock();
199 void X11DRV_XF86VM_SetExclusiveMode(int lock)
201 if (!xf86vm_modes) return; /* no XVidMode */
203 wine_tsx11_lock();
204 XF86VidModeLockModeSwitch(gdi_display, DefaultScreen(gdi_display), lock);
205 wine_tsx11_unlock();
208 /* actual DirectDraw HAL stuff */
210 static DWORD PASCAL X11DRV_XF86VM_SetMode(LPDDHAL_SETMODEDATA data)
212 X11DRV_XF86VM_SetCurrentMode(data->dwModeIndex);
213 X11DRV_DDHAL_SwitchMode(data->dwModeIndex, NULL, NULL);
214 data->ddRVal = DD_OK;
215 return DDHAL_DRIVER_HANDLED;
218 int X11DRV_XF86VM_CreateDriver(LPDDHALINFO info)
220 if (!xf86vm_mode_count) return 0; /* no XVidMode */
222 info->dwNumModes = xf86vm_mode_count;
223 info->lpModeInfo = xf86vm_modes;
224 X11DRV_DDHAL_SwitchMode(X11DRV_XF86VM_GetCurrentMode(), NULL, NULL);
225 info->lpDDCallbacks->SetMode = X11DRV_XF86VM_SetMode;
226 return TRUE;
230 /***** GAMMA CONTROL *****/
231 /* (only available in XF86VidMode 2.x) */
233 #ifdef X_XF86VidModeSetGamma
235 static void GenerateRampFromGamma(WORD ramp[256], float gamma)
237 float r_gamma = 1/gamma;
238 unsigned i;
239 TRACE("gamma is %f\n", r_gamma);
240 for (i=0; i<256; i++)
241 ramp[i] = pow(i/255.0, r_gamma) * 65535.0;
244 static BOOL ComputeGammaFromRamp(WORD ramp[256], float *gamma)
246 float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
247 unsigned i, f, l, g_n, c;
248 f = ramp[0];
249 l = ramp[255];
250 if (f >= l) {
251 ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
252 return FALSE;
254 r_d = l - f;
255 g_min = g_max = g_avg = 0.0;
256 /* check gamma ramp entries to estimate the gamma */
257 TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
258 for (i=1, g_n=0; i<255; i++) {
259 if (ramp[i] < f || ramp[i] > l) {
260 ERR("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
261 return FALSE;
263 c = ramp[i] - f;
264 if (!c) continue; /* avoid log(0) */
266 /* normalize entry values into 0..1 range */
267 r_x = i/255.0; r_y = c / r_d;
268 /* compute logarithms of values */
269 r_lx = log(r_x); r_ly = log(r_y);
270 /* compute gamma for this entry */
271 r_v = r_ly / r_lx;
272 /* compute differential (error estimate) for this entry */
273 /* some games use table-based logarithms that magnifies the error by 128 */
274 r_e = -r_lx * 128 / (c * r_lx * r_lx);
276 /* compute min & max while compensating for estimated error */
277 if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
278 if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
280 /* add to average */
281 g_avg += r_v;
282 g_n++;
283 /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
285 if (!g_n) {
286 ERR("no gamma data, shouldn't happen\n");
287 return FALSE;
289 g_avg /= g_n;
290 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
291 /* the bias could be because the app wanted something like a "red shift"
292 * like when you're hit in Quake, but XVidMode doesn't support it,
293 * so we have to reject a significant bias */
294 if (f && f > (pow(1/255.0, g_avg) * 65536.0)) {
295 ERR("low-biased gamma ramp (%d), rejected\n", f);
296 return FALSE;
298 /* check that the gamma is reasonably uniform across the ramp */
299 if (g_max - g_min > 0.1) {
300 ERR("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
301 return FALSE;
303 /* ok, now we're pretty sure we can set the desired gamma ramp,
304 * so go for it */
305 *gamma = 1/g_avg;
306 return TRUE;
309 #endif /* X_XF86VidModeSetGamma */
311 /* Hmm... should gamma control be available in desktop mode or not?
312 * I'll assume that it should */
314 BOOL X11DRV_XF86VM_GetGammaRamp(LPDDGAMMARAMP ramp)
316 #ifdef X_XF86VidModeSetGamma
317 XF86VidModeGamma gamma;
318 Bool ret;
320 if (xf86vm_major < 2) return FALSE; /* no gamma control */
321 #ifdef X_XF86VidModeSetGammaRamp
322 else if (xf86vm_use_gammaramp)
324 Bool ret;
325 wine_tsx11_lock();
326 ret = XF86VidModeGetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
327 ramp->red, ramp->green, ramp->blue);
328 wine_tsx11_unlock();
329 return ret;
331 #endif
332 else
334 wine_tsx11_lock();
335 ret = XF86VidModeGetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
336 wine_tsx11_unlock();
337 if (ret) {
338 GenerateRampFromGamma(ramp->red, gamma.red);
339 GenerateRampFromGamma(ramp->green, gamma.green);
340 GenerateRampFromGamma(ramp->blue, gamma.blue);
341 return TRUE;
344 #endif /* X_XF86VidModeSetGamma */
345 return FALSE;
348 BOOL X11DRV_XF86VM_SetGammaRamp(LPDDGAMMARAMP ramp)
350 #ifdef X_XF86VidModeSetGamma
351 XF86VidModeGamma gamma;
353 if (xf86vm_major < 2) return FALSE; /* no gamma control */
354 #ifdef X_XF86VidModeSetGammaRamp
355 else if (xf86vm_use_gammaramp)
357 Bool ret;
358 wine_tsx11_lock();
359 ret = XF86VidModeSetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
360 ramp->red, ramp->green, ramp->blue);
361 wine_tsx11_unlock();
362 return ret;
364 #endif
365 else
367 if (ComputeGammaFromRamp(ramp->red, &gamma.red) &&
368 ComputeGammaFromRamp(ramp->green, &gamma.green) &&
369 ComputeGammaFromRamp(ramp->blue, &gamma.blue)) {
370 Bool ret;
371 wine_tsx11_lock();
372 ret = XF86VidModeSetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
373 wine_tsx11_unlock();
374 return ret;
377 #endif /* X_XF86VidModeSetGamma */
378 return FALSE;
381 #endif /* HAVE_LIBXXF86VM */
383 /***********************************************************************
384 * GetDeviceGammaRamp (X11DRV.@)
386 * FIXME: should move to somewhere appropriate, but probably not before
387 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
388 * they can include xvidmode.h directly
390 BOOL X11DRV_GetDeviceGammaRamp(X11DRV_PDEVICE *physDev, LPVOID ramp)
392 #ifdef HAVE_LIBXXF86VM
393 return X11DRV_XF86VM_GetGammaRamp(ramp);
394 #else
395 return FALSE;
396 #endif
399 /***********************************************************************
400 * SetDeviceGammaRamp (X11DRV.@)
402 * FIXME: should move to somewhere appropriate, but probably not before
403 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
404 * they can include xvidmode.h directly
406 BOOL X11DRV_SetDeviceGammaRamp(X11DRV_PDEVICE *physDev, LPVOID ramp)
408 #ifdef HAVE_LIBXXF86VM
409 return X11DRV_XF86VM_SetGammaRamp(ramp);
410 #else
411 return FALSE;
412 #endif