kernel32: Silence a fixme in GetNativeSystemInfo.
[wine/multimedia.git] / dlls / winex11.drv / xvidmode.c
blob49c48b3f85ced2baca83493cf5aa682df9fb8772
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include <string.h>
23 #include <stdio.h>
24 #include <math.h>
26 #include "x11drv.h"
28 #ifdef HAVE_LIBXXF86VM
29 #include <X11/extensions/xf86vmode.h>
30 #endif /* HAVE_LIBXXF86VM */
32 #include "xvidmode.h"
34 #include "windef.h"
35 #include "wingdi.h"
36 #include "ddrawi.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(xvidmode);
41 #ifdef HAVE_LIBXXF86VM
43 extern int usexvidmode;
45 static int xf86vm_event, xf86vm_error, xf86vm_major, xf86vm_minor;
47 #ifdef X_XF86VidModeSetGammaRamp
48 static int xf86vm_gammaramp_size;
49 static BOOL xf86vm_use_gammaramp;
50 #endif /* X_XF86VidModeSetGammaRamp */
52 static LPDDHALMODEINFO dd_modes;
53 static unsigned int dd_mode_count;
54 static XF86VidModeModeInfo** real_xf86vm_modes;
55 static unsigned int real_xf86vm_mode_count;
57 static void convert_modeinfo( const XF86VidModeModeInfo *mode)
59 int rate;
60 if (mode->htotal!=0 && mode->vtotal!=0)
61 rate = mode->dotclock * 1000 / (mode->htotal * mode->vtotal);
62 else
63 rate = 0;
64 X11DRV_Settings_AddOneMode(mode->hdisplay, mode->vdisplay, 0, rate);
67 static void convert_modeline(int dotclock, const XF86VidModeModeLine *mode, LPDDHALMODEINFO info, unsigned int bpp)
69 info->dwWidth = mode->hdisplay;
70 info->dwHeight = mode->vdisplay;
71 if (mode->htotal!=0 && mode->vtotal!=0)
72 info->wRefreshRate = dotclock * 1000 / (mode->htotal * mode->vtotal);
73 else
74 info->wRefreshRate = 0;
75 TRACE(" width=%d, height=%d, refresh=%d\n",
76 info->dwWidth, info->dwHeight, info->wRefreshRate);
77 info->lPitch = 0;
78 info->dwBPP = bpp;
79 info->wFlags = 0;
80 info->dwRBitMask = 0;
81 info->dwGBitMask = 0;
82 info->dwBBitMask = 0;
83 info->dwAlphaBitMask = 0;
86 static int XVidModeErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
88 return 1;
91 static int X11DRV_XF86VM_GetCurrentMode(void)
93 XF86VidModeModeLine line;
94 int dotclock;
95 unsigned int i;
96 DDHALMODEINFO cmode;
97 DWORD dwBpp = screen_bpp;
99 TRACE("Querying XVidMode current mode\n");
100 wine_tsx11_lock();
101 XF86VidModeGetModeLine(gdi_display, DefaultScreen(gdi_display), &dotclock, &line);
102 wine_tsx11_unlock();
103 convert_modeline(dotclock, &line, &cmode, dwBpp);
104 for (i=0; i<dd_mode_count; i++)
105 if (memcmp(&dd_modes[i], &cmode, sizeof(cmode)) == 0) {
106 TRACE("mode=%d\n", i);
107 return i;
109 ERR("In unknown mode, returning default\n");
110 return 0;
113 static LONG X11DRV_XF86VM_SetCurrentMode(int mode)
115 DWORD dwBpp = screen_bpp;
116 /* only set modes from the original color depth */
117 if (dwBpp != dd_modes[mode].dwBPP)
119 FIXME("Cannot change screen BPP from %d to %d\n", dwBpp, dd_modes[mode].dwBPP);
121 mode = mode % real_xf86vm_mode_count;
123 wine_tsx11_lock();
124 TRACE("Resizing X display to %dx%d\n",
125 real_xf86vm_modes[mode]->hdisplay, real_xf86vm_modes[mode]->vdisplay);
126 XF86VidModeSwitchToMode(gdi_display, DefaultScreen(gdi_display), real_xf86vm_modes[mode]);
127 #if 0 /* it is said that SetViewPort causes problems with some X servers */
128 XF86VidModeSetViewPort(gdi_display, DefaultScreen(gdi_display), 0, 0);
129 #else
130 XWarpPointer(gdi_display, None, DefaultRootWindow(gdi_display), 0, 0, 0, 0, 0, 0);
131 #endif
132 XSync(gdi_display, False);
133 wine_tsx11_unlock();
134 X11DRV_handle_desktop_resize( real_xf86vm_modes[mode]->hdisplay,
135 real_xf86vm_modes[mode]->vdisplay );
136 return DISP_CHANGE_SUCCESSFUL;
139 void X11DRV_XF86VM_Init(void)
141 Bool ok;
142 int nmodes;
143 unsigned int i;
145 if (xf86vm_major) return; /* already initialized? */
147 /* see if XVidMode is available */
148 wine_tsx11_lock();
149 ok = XF86VidModeQueryExtension(gdi_display, &xf86vm_event, &xf86vm_error);
150 if (ok)
152 X11DRV_expect_error(gdi_display, XVidModeErrorHandler, NULL);
153 ok = XF86VidModeQueryVersion(gdi_display, &xf86vm_major, &xf86vm_minor);
154 if (X11DRV_check_error()) ok = FALSE;
156 if (ok)
158 #ifdef X_XF86VidModeSetGammaRamp
159 if (xf86vm_major > 2 || (xf86vm_major == 2 && xf86vm_minor >= 1))
161 XF86VidModeGetGammaRampSize(gdi_display, DefaultScreen(gdi_display),
162 &xf86vm_gammaramp_size);
163 if (xf86vm_gammaramp_size == 256)
164 xf86vm_use_gammaramp = TRUE;
166 #endif /* X_XF86VidModeSetGammaRamp */
168 /* retrieve modes */
169 if (usexvidmode && root_window == DefaultRootWindow( gdi_display ))
170 ok = XF86VidModeGetAllModeLines(gdi_display, DefaultScreen(gdi_display), &nmodes, &real_xf86vm_modes);
171 else
172 ok = FALSE; /* In desktop mode, do not switch resolution... But still use the Gamma ramp stuff */
174 wine_tsx11_unlock();
175 if (!ok) return;
177 TRACE("XVidMode modes: count=%d\n", nmodes);
179 real_xf86vm_mode_count = nmodes;
181 dd_modes = X11DRV_Settings_SetHandlers("XF86VidMode",
182 X11DRV_XF86VM_GetCurrentMode,
183 X11DRV_XF86VM_SetCurrentMode,
184 nmodes, 1);
186 /* convert modes to DDHALMODEINFO format */
187 for (i=0; i<real_xf86vm_mode_count; i++)
189 convert_modeinfo(real_xf86vm_modes[i]);
191 /* add modes for different color depths */
192 X11DRV_Settings_AddDepthModes();
193 dd_mode_count = X11DRV_Settings_GetModeCount();
195 TRACE("Available DD modes: count=%d\n", dd_mode_count);
196 TRACE("Enabling XVidMode\n");
199 void X11DRV_XF86VM_Cleanup(void)
201 wine_tsx11_lock();
202 if (real_xf86vm_modes) XFree(real_xf86vm_modes);
203 wine_tsx11_unlock();
206 void X11DRV_XF86VM_SetExclusiveMode(int lock)
208 if (!dd_modes) return; /* no XVidMode */
210 wine_tsx11_lock();
211 XF86VidModeLockModeSwitch(gdi_display, DefaultScreen(gdi_display), lock);
212 wine_tsx11_unlock();
215 /***** GAMMA CONTROL *****/
216 /* (only available in XF86VidMode 2.x) */
218 #ifdef X_XF86VidModeSetGamma
220 static void GenerateRampFromGamma(WORD ramp[256], float gamma)
222 float r_gamma = 1/gamma;
223 unsigned i;
224 TRACE("gamma is %f\n", r_gamma);
225 for (i=0; i<256; i++)
226 ramp[i] = pow(i/255.0, r_gamma) * 65535.0;
229 static BOOL ComputeGammaFromRamp(WORD ramp[256], float *gamma)
231 float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
232 unsigned i, f, l, g_n, c;
233 f = ramp[0];
234 l = ramp[255];
235 if (f >= l) {
236 ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
237 return FALSE;
239 r_d = l - f;
240 g_min = g_max = g_avg = 0.0;
241 /* check gamma ramp entries to estimate the gamma */
242 TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
243 for (i=1, g_n=0; i<255; i++) {
244 if (ramp[i] < f || ramp[i] > l) {
245 ERR("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
246 return FALSE;
248 c = ramp[i] - f;
249 if (!c) continue; /* avoid log(0) */
251 /* normalize entry values into 0..1 range */
252 r_x = i/255.0; r_y = c / r_d;
253 /* compute logarithms of values */
254 r_lx = log(r_x); r_ly = log(r_y);
255 /* compute gamma for this entry */
256 r_v = r_ly / r_lx;
257 /* compute differential (error estimate) for this entry */
258 /* some games use table-based logarithms that magnifies the error by 128 */
259 r_e = -r_lx * 128 / (c * r_lx * r_lx);
261 /* compute min & max while compensating for estimated error */
262 if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
263 if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
265 /* add to average */
266 g_avg += r_v;
267 g_n++;
268 /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
270 if (!g_n) {
271 ERR("no gamma data, shouldn't happen\n");
272 return FALSE;
274 g_avg /= g_n;
275 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
276 /* the bias could be because the app wanted something like a "red shift"
277 * like when you're hit in Quake, but XVidMode doesn't support it,
278 * so we have to reject a significant bias */
279 if (f && f > (pow(1/255.0, g_avg) * 65536.0)) {
280 ERR("low-biased gamma ramp (%d), rejected\n", f);
281 return FALSE;
283 /* check that the gamma is reasonably uniform across the ramp */
284 if (g_max - g_min > 0.1) {
285 ERR("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
286 return FALSE;
288 /* ok, now we're pretty sure we can set the desired gamma ramp,
289 * so go for it */
290 *gamma = 1/g_avg;
291 return TRUE;
294 #endif /* X_XF86VidModeSetGamma */
296 /* Hmm... should gamma control be available in desktop mode or not?
297 * I'll assume that it should */
299 BOOL X11DRV_XF86VM_GetGammaRamp(LPDDGAMMARAMP ramp)
301 #ifdef X_XF86VidModeSetGamma
302 XF86VidModeGamma gamma;
303 Bool ret;
305 if (xf86vm_major < 2) return FALSE; /* no gamma control */
306 #ifdef X_XF86VidModeSetGammaRamp
307 else if (xf86vm_use_gammaramp)
309 Bool ret;
310 wine_tsx11_lock();
311 ret = XF86VidModeGetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
312 ramp->red, ramp->green, ramp->blue);
313 wine_tsx11_unlock();
314 return ret;
316 #endif
317 else
319 wine_tsx11_lock();
320 ret = XF86VidModeGetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
321 wine_tsx11_unlock();
322 if (ret) {
323 GenerateRampFromGamma(ramp->red, gamma.red);
324 GenerateRampFromGamma(ramp->green, gamma.green);
325 GenerateRampFromGamma(ramp->blue, gamma.blue);
326 return TRUE;
329 #endif /* X_XF86VidModeSetGamma */
330 return FALSE;
333 BOOL X11DRV_XF86VM_SetGammaRamp(LPDDGAMMARAMP ramp)
335 #ifdef X_XF86VidModeSetGamma
336 XF86VidModeGamma gamma;
338 if (xf86vm_major < 2) return FALSE; /* no gamma control */
339 #ifdef X_XF86VidModeSetGammaRamp
340 else if (xf86vm_use_gammaramp)
342 Bool ret;
343 wine_tsx11_lock();
344 ret = XF86VidModeSetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
345 ramp->red, ramp->green, ramp->blue);
346 wine_tsx11_unlock();
347 return ret;
349 #endif
350 else
352 if (ComputeGammaFromRamp(ramp->red, &gamma.red) &&
353 ComputeGammaFromRamp(ramp->green, &gamma.green) &&
354 ComputeGammaFromRamp(ramp->blue, &gamma.blue)) {
355 Bool ret;
356 wine_tsx11_lock();
357 ret = XF86VidModeSetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
358 wine_tsx11_unlock();
359 return ret;
362 #endif /* X_XF86VidModeSetGamma */
363 return FALSE;
366 #endif /* HAVE_LIBXXF86VM */
368 /***********************************************************************
369 * GetDeviceGammaRamp (X11DRV.@)
371 * FIXME: should move to somewhere appropriate, but probably not before
372 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
373 * they can include xvidmode.h directly
375 BOOL X11DRV_GetDeviceGammaRamp(X11DRV_PDEVICE *physDev, LPVOID ramp)
377 #ifdef HAVE_LIBXXF86VM
378 return X11DRV_XF86VM_GetGammaRamp(ramp);
379 #else
380 return FALSE;
381 #endif
384 /***********************************************************************
385 * SetDeviceGammaRamp (X11DRV.@)
387 * FIXME: should move to somewhere appropriate, but probably not before
388 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
389 * they can include xvidmode.h directly
391 BOOL X11DRV_SetDeviceGammaRamp(X11DRV_PDEVICE *physDev, LPVOID ramp)
393 #ifdef HAVE_LIBXXF86VM
394 return X11DRV_XF86VM_SetGammaRamp(ramp);
395 #else
396 return FALSE;
397 #endif