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
22 #include "wine/port.h"
30 #ifdef HAVE_X11_EXTENSIONS_XF86VMODE_H
31 #include <X11/extensions/xf86vmode.h>
33 #ifdef HAVE_X11_EXTENSIONS_XF86VMPROTO_H
34 #include <X11/extensions/xf86vmproto.h>
39 #include "wine/debug.h"
40 #include "wine/library.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(xvidmode
);
44 #ifdef SONAME_LIBXXF86VM
46 extern BOOL usexvidmode
;
48 static int xf86vm_event
, xf86vm_error
, xf86vm_major
, xf86vm_minor
;
50 #ifdef X_XF86VidModeSetGammaRamp
51 static int xf86vm_gammaramp_size
;
52 static BOOL xf86vm_use_gammaramp
;
53 #endif /* X_XF86VidModeSetGammaRamp */
55 static struct x11drv_mode_info
*dd_modes
;
56 static unsigned int dd_mode_count
;
57 static XF86VidModeModeInfo
** real_xf86vm_modes
;
58 static unsigned int real_xf86vm_mode_count
;
60 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
61 MAKE_FUNCPTR(XF86VidModeGetAllModeLines
)
62 MAKE_FUNCPTR(XF86VidModeGetModeLine
)
63 MAKE_FUNCPTR(XF86VidModeLockModeSwitch
)
64 MAKE_FUNCPTR(XF86VidModeQueryExtension
)
65 MAKE_FUNCPTR(XF86VidModeQueryVersion
)
66 MAKE_FUNCPTR(XF86VidModeSetViewPort
)
67 MAKE_FUNCPTR(XF86VidModeSwitchToMode
)
68 #ifdef X_XF86VidModeSetGamma
69 MAKE_FUNCPTR(XF86VidModeGetGamma
)
70 MAKE_FUNCPTR(XF86VidModeSetGamma
)
72 #ifdef X_XF86VidModeSetGammaRamp
73 MAKE_FUNCPTR(XF86VidModeGetGammaRamp
)
74 MAKE_FUNCPTR(XF86VidModeGetGammaRampSize
)
75 MAKE_FUNCPTR(XF86VidModeSetGammaRamp
)
80 static void convert_modeinfo( const XF86VidModeModeInfo
*mode
)
83 if (mode
->htotal
!=0 && mode
->vtotal
!=0)
84 rate
= mode
->dotclock
* 1000 / (mode
->htotal
* mode
->vtotal
);
87 X11DRV_Settings_AddOneMode(mode
->hdisplay
, mode
->vdisplay
, 0, rate
);
90 static void convert_modeline(int dotclock
, const XF86VidModeModeLine
*mode
,
91 struct x11drv_mode_info
*info
, unsigned int bpp
)
93 info
->width
= mode
->hdisplay
;
94 info
->height
= mode
->vdisplay
;
95 if (mode
->htotal
!=0 && mode
->vtotal
!=0)
96 info
->refresh_rate
= dotclock
* 1000 / (mode
->htotal
* mode
->vtotal
);
98 info
->refresh_rate
= 0;
99 TRACE(" width=%d, height=%d, refresh=%d\n",
100 info
->width
, info
->height
, info
->refresh_rate
);
104 static int XVidModeErrorHandler(Display
*dpy
, XErrorEvent
*event
, void *arg
)
109 static int X11DRV_XF86VM_GetCurrentMode(void)
111 XF86VidModeModeLine line
;
114 struct x11drv_mode_info cmode
;
115 DWORD dwBpp
= screen_bpp
;
117 TRACE("Querying XVidMode current mode\n");
118 pXF86VidModeGetModeLine(gdi_display
, DefaultScreen(gdi_display
), &dotclock
, &line
);
119 convert_modeline(dotclock
, &line
, &cmode
, dwBpp
);
120 for (i
=0; i
<dd_mode_count
; i
++)
121 if (memcmp(&dd_modes
[i
], &cmode
, sizeof(cmode
)) == 0) {
122 TRACE("mode=%d\n", i
);
125 ERR("In unknown mode, returning default\n");
129 static LONG
X11DRV_XF86VM_SetCurrentMode(int mode
)
131 DWORD dwBpp
= screen_bpp
;
132 /* only set modes from the original color depth */
133 if (dwBpp
!= dd_modes
[mode
].bpp
)
135 FIXME("Cannot change screen BPP from %d to %d\n", dwBpp
, dd_modes
[mode
].bpp
);
137 mode
= mode
% real_xf86vm_mode_count
;
139 TRACE("Resizing X display to %dx%d\n",
140 real_xf86vm_modes
[mode
]->hdisplay
, real_xf86vm_modes
[mode
]->vdisplay
);
141 pXF86VidModeSwitchToMode(gdi_display
, DefaultScreen(gdi_display
), real_xf86vm_modes
[mode
]);
142 #if 0 /* it is said that SetViewPort causes problems with some X servers */
143 pXF86VidModeSetViewPort(gdi_display
, DefaultScreen(gdi_display
), 0, 0);
145 XWarpPointer(gdi_display
, None
, DefaultRootWindow(gdi_display
), 0, 0, 0, 0, 0, 0);
147 XSync(gdi_display
, False
);
148 X11DRV_resize_desktop( real_xf86vm_modes
[mode
]->hdisplay
, real_xf86vm_modes
[mode
]->vdisplay
);
149 return DISP_CHANGE_SUCCESSFUL
;
153 void X11DRV_XF86VM_Init(void)
155 void *xvidmode_handle
;
160 if (xf86vm_major
) return; /* already initialized? */
162 xvidmode_handle
= wine_dlopen(SONAME_LIBXXF86VM
, RTLD_NOW
, NULL
, 0);
163 if (!xvidmode_handle
)
165 TRACE("Unable to open %s, XVidMode disabled\n", SONAME_LIBXXF86VM
);
170 #define LOAD_FUNCPTR(f) \
171 if((p##f = wine_dlsym(xvidmode_handle, #f, NULL, 0)) == NULL) \
173 LOAD_FUNCPTR(XF86VidModeGetAllModeLines
)
174 LOAD_FUNCPTR(XF86VidModeGetModeLine
)
175 LOAD_FUNCPTR(XF86VidModeLockModeSwitch
)
176 LOAD_FUNCPTR(XF86VidModeQueryExtension
)
177 LOAD_FUNCPTR(XF86VidModeQueryVersion
)
178 LOAD_FUNCPTR(XF86VidModeSetViewPort
)
179 LOAD_FUNCPTR(XF86VidModeSwitchToMode
)
180 #ifdef X_XF86VidModeSetGamma
181 LOAD_FUNCPTR(XF86VidModeGetGamma
)
182 LOAD_FUNCPTR(XF86VidModeSetGamma
)
184 #ifdef X_XF86VidModeSetGammaRamp
185 LOAD_FUNCPTR(XF86VidModeGetGammaRamp
)
186 LOAD_FUNCPTR(XF86VidModeGetGammaRampSize
)
187 LOAD_FUNCPTR(XF86VidModeSetGammaRamp
)
191 /* see if XVidMode is available */
192 if (!pXF86VidModeQueryExtension(gdi_display
, &xf86vm_event
, &xf86vm_error
)) return;
194 X11DRV_expect_error(gdi_display
, XVidModeErrorHandler
, NULL
);
195 ok
= pXF86VidModeQueryVersion(gdi_display
, &xf86vm_major
, &xf86vm_minor
);
196 if (X11DRV_check_error() || !ok
) return;
198 #ifdef X_XF86VidModeSetGammaRamp
199 if (xf86vm_major
> 2 || (xf86vm_major
== 2 && xf86vm_minor
>= 1))
201 X11DRV_expect_error(gdi_display
, XVidModeErrorHandler
, NULL
);
202 pXF86VidModeGetGammaRampSize(gdi_display
, DefaultScreen(gdi_display
),
203 &xf86vm_gammaramp_size
);
204 if (X11DRV_check_error()) xf86vm_gammaramp_size
= 0;
205 if (xf86vm_gammaramp_size
== 256)
206 xf86vm_use_gammaramp
= TRUE
;
208 #endif /* X_XF86VidModeSetGammaRamp */
211 if (usexvidmode
&& root_window
== DefaultRootWindow( gdi_display
))
213 X11DRV_expect_error(gdi_display
, XVidModeErrorHandler
, NULL
);
214 ok
= pXF86VidModeGetAllModeLines(gdi_display
, DefaultScreen(gdi_display
), &nmodes
, &real_xf86vm_modes
);
215 if (X11DRV_check_error() || !ok
) return;
217 else return; /* In desktop mode, do not switch resolution... But still use the Gamma ramp stuff */
219 TRACE("XVidMode modes: count=%d\n", nmodes
);
221 real_xf86vm_mode_count
= nmodes
;
223 dd_modes
= X11DRV_Settings_SetHandlers("XF86VidMode",
224 X11DRV_XF86VM_GetCurrentMode
,
225 X11DRV_XF86VM_SetCurrentMode
,
228 /* convert modes to x11drv_mode_info format */
229 for (i
=0; i
<real_xf86vm_mode_count
; i
++)
231 convert_modeinfo(real_xf86vm_modes
[i
]);
233 /* add modes for different color depths */
234 X11DRV_Settings_AddDepthModes();
235 dd_mode_count
= X11DRV_Settings_GetModeCount();
237 TRACE("Available DD modes: count=%d\n", dd_mode_count
);
238 TRACE("Enabling XVidMode\n");
242 TRACE("Unable to load function pointers from %s, XVidMode disabled\n", SONAME_LIBXXF86VM
);
243 wine_dlclose(xvidmode_handle
, NULL
, 0);
244 xvidmode_handle
= NULL
;
248 /***** GAMMA CONTROL *****/
249 /* (only available in XF86VidMode 2.x) */
251 #ifdef X_XF86VidModeSetGamma
253 static void GenerateRampFromGamma(WORD ramp
[256], float gamma
)
255 float r_gamma
= 1/gamma
;
257 TRACE("gamma is %f\n", r_gamma
);
258 for (i
=0; i
<256; i
++)
259 ramp
[i
] = pow(i
/255.0, r_gamma
) * 65535.0;
262 static BOOL
ComputeGammaFromRamp(WORD ramp
[256], float *gamma
)
264 float r_x
, r_y
, r_lx
, r_ly
, r_d
, r_v
, r_e
, g_avg
, g_min
, g_max
;
265 unsigned i
, f
, l
, g_n
, c
;
269 ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f
, l
);
273 g_min
= g_max
= g_avg
= 0.0;
274 /* check gamma ramp entries to estimate the gamma */
275 TRACE("analyzing gamma ramp (%d->%d)\n", f
, l
);
276 for (i
=1, g_n
=0; i
<255; i
++) {
277 if (ramp
[i
] < f
|| ramp
[i
] > l
) {
278 ERR("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i
, ramp
[i
], f
, l
);
282 if (!c
) continue; /* avoid log(0) */
284 /* normalize entry values into 0..1 range */
285 r_x
= i
/255.0; r_y
= c
/ r_d
;
286 /* compute logarithms of values */
287 r_lx
= log(r_x
); r_ly
= log(r_y
);
288 /* compute gamma for this entry */
290 /* compute differential (error estimate) for this entry */
291 /* some games use table-based logarithms that magnifies the error by 128 */
292 r_e
= -r_lx
* 128 / (c
* r_lx
* r_lx
);
294 /* compute min & max while compensating for estimated error */
295 if (!g_n
|| g_min
> (r_v
+ r_e
)) g_min
= r_v
+ r_e
;
296 if (!g_n
|| g_max
< (r_v
- r_e
)) g_max
= r_v
- r_e
;
301 /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
304 ERR("no gamma data, shouldn't happen\n");
308 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f
, 65535-l
, g_avg
);
309 /* the bias could be because the app wanted something like a "red shift"
310 * like when you're hit in Quake, but XVidMode doesn't support it,
311 * so we have to reject a significant bias */
312 if (f
&& f
> (pow(1/255.0, g_avg
) * 65536.0)) {
313 ERR("low-biased gamma ramp (%d), rejected\n", f
);
316 /* check that the gamma is reasonably uniform across the ramp */
317 if (g_max
- g_min
> 12.8) {
318 ERR("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max
, g_min
, g_avg
);
321 /* check that the gamma is not too bright */
323 ERR("too bright gamma ( %5.3f), rejected\n", g_avg
);
326 /* ok, now we're pretty sure we can set the desired gamma ramp,
332 #endif /* X_XF86VidModeSetGamma */
334 /* Hmm... should gamma control be available in desktop mode or not?
335 * I'll assume that it should */
337 static BOOL
X11DRV_XF86VM_GetGammaRamp(struct x11drv_gamma_ramp
*ramp
)
339 #ifdef X_XF86VidModeSetGamma
340 XF86VidModeGamma gamma
;
342 if (xf86vm_major
< 2) return FALSE
; /* no gamma control */
343 #ifdef X_XF86VidModeSetGammaRamp
344 if (xf86vm_use_gammaramp
)
345 return pXF86VidModeGetGammaRamp(gdi_display
, DefaultScreen(gdi_display
), 256,
346 ramp
->red
, ramp
->green
, ramp
->blue
);
348 if (pXF86VidModeGetGamma(gdi_display
, DefaultScreen(gdi_display
), &gamma
))
350 GenerateRampFromGamma(ramp
->red
, gamma
.red
);
351 GenerateRampFromGamma(ramp
->green
, gamma
.green
);
352 GenerateRampFromGamma(ramp
->blue
, gamma
.blue
);
355 #endif /* X_XF86VidModeSetGamma */
359 static BOOL
X11DRV_XF86VM_SetGammaRamp(struct x11drv_gamma_ramp
*ramp
)
361 #ifdef X_XF86VidModeSetGamma
362 XF86VidModeGamma gamma
;
364 if (xf86vm_major
< 2 || !usexvidmode
) return FALSE
; /* no gamma control */
365 if (!ComputeGammaFromRamp(ramp
->red
, &gamma
.red
) || /* ramp validation */
366 !ComputeGammaFromRamp(ramp
->green
, &gamma
.green
) ||
367 !ComputeGammaFromRamp(ramp
->blue
, &gamma
.blue
)) return FALSE
;
368 #ifdef X_XF86VidModeSetGammaRamp
369 if (xf86vm_use_gammaramp
)
370 return pXF86VidModeSetGammaRamp(gdi_display
, DefaultScreen(gdi_display
), 256,
371 ramp
->red
, ramp
->green
, ramp
->blue
);
373 return pXF86VidModeSetGamma(gdi_display
, DefaultScreen(gdi_display
), &gamma
);
376 #endif /* X_XF86VidModeSetGamma */
379 #else /* SONAME_LIBXXF86VM */
381 void X11DRV_XF86VM_Init(void)
383 TRACE("XVidMode support not compiled in.\n");
386 #endif /* SONAME_LIBXXF86VM */
388 /***********************************************************************
389 * GetDeviceGammaRamp (X11DRV.@)
391 * FIXME: should move to somewhere appropriate, but probably not before
392 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
393 * they can include xvidmode.h directly
395 BOOL
X11DRV_GetDeviceGammaRamp(PHYSDEV dev
, LPVOID ramp
)
397 #ifdef SONAME_LIBXXF86VM
398 return X11DRV_XF86VM_GetGammaRamp(ramp
);
404 /***********************************************************************
405 * SetDeviceGammaRamp (X11DRV.@)
407 * FIXME: should move to somewhere appropriate, but probably not before
408 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
409 * they can include xvidmode.h directly
411 BOOL
X11DRV_SetDeviceGammaRamp(PHYSDEV dev
, LPVOID ramp
)
413 #ifdef SONAME_LIBXXF86VM
414 return X11DRV_XF86VM_SetGammaRamp(ramp
);