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
28 #ifdef HAVE_LIBXXF86VM
29 #include <X11/extensions/xf86vmode.h>
30 #endif /* HAVE_LIBXXF86VM */
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(xvidmode
);
42 #ifdef HAVE_LIBXXF86VM
44 extern int usexvidmode
;
46 static int xf86vm_event
, xf86vm_error
, xf86vm_major
, xf86vm_minor
;
48 #ifdef X_XF86VidModeSetGammaRamp
49 static int xf86vm_gammaramp_size
;
50 static BOOL xf86vm_use_gammaramp
;
51 #endif /* X_XF86VidModeSetGammaRamp */
53 static LPDDHALMODEINFO dd_modes
;
54 static unsigned int dd_mode_count
;
55 static XF86VidModeModeInfo
** real_xf86vm_modes
;
56 static unsigned int real_xf86vm_mode_count
;
58 static void convert_modeinfo( const XF86VidModeModeInfo
*mode
)
61 if (mode
->htotal
!=0 && mode
->vtotal
!=0)
62 rate
= mode
->dotclock
* 1000 / (mode
->htotal
* mode
->vtotal
);
65 X11DRV_Settings_AddOneMode(mode
->hdisplay
, mode
->vdisplay
, 0, rate
);
68 static void convert_modeline(int dotclock
, const XF86VidModeModeLine
*mode
, LPDDHALMODEINFO info
, unsigned int bpp
)
70 info
->dwWidth
= mode
->hdisplay
;
71 info
->dwHeight
= mode
->vdisplay
;
72 if (mode
->htotal
!=0 && mode
->vtotal
!=0)
73 info
->wRefreshRate
= dotclock
* 1000 / (mode
->htotal
* mode
->vtotal
);
75 info
->wRefreshRate
= 0;
76 TRACE(" width=%ld, height=%ld, refresh=%d\n",
77 info
->dwWidth
, info
->dwHeight
, info
->wRefreshRate
);
84 info
->dwAlphaBitMask
= 0;
87 static int XVidModeErrorHandler(Display
*dpy
, XErrorEvent
*event
, void *arg
)
92 int X11DRV_XF86VM_GetCurrentMode(void)
94 XF86VidModeModeLine line
;
98 DWORD dwBpp
= screen_depth
;
99 if (dwBpp
== 24) dwBpp
= 32;
101 TRACE("Querying XVidMode current mode\n");
103 XF86VidModeGetModeLine(gdi_display
, DefaultScreen(gdi_display
), &dotclock
, &line
);
105 convert_modeline(dotclock
, &line
, &cmode
, dwBpp
);
106 for (i
=0; i
<dd_mode_count
; i
++)
107 if (memcmp(&dd_modes
[i
], &cmode
, sizeof(cmode
)) == 0) {
108 TRACE("mode=%d\n", i
);
111 ERR("In unknown mode, returning default\n");
115 void X11DRV_XF86VM_SetCurrentMode(int mode
)
117 DWORD dwBpp
= screen_depth
;
118 if (dwBpp
== 24) dwBpp
= 32;
119 /* only set modes from the original color depth */
120 if (dwBpp
!= dd_modes
[mode
].dwBPP
)
122 FIXME("Cannot change screen BPP from %ld to %ld\n", dwBpp
, dd_modes
[mode
].dwBPP
);
124 mode
= mode
% real_xf86vm_mode_count
;
127 TRACE("Resizing X display to %dx%d\n",
128 real_xf86vm_modes
[mode
]->hdisplay
, real_xf86vm_modes
[mode
]->vdisplay
);
129 XF86VidModeSwitchToMode(gdi_display
, DefaultScreen(gdi_display
), real_xf86vm_modes
[mode
]);
130 #if 0 /* it is said that SetViewPort causes problems with some X servers */
131 XF86VidModeSetViewPort(gdi_display
, DefaultScreen(gdi_display
), 0, 0);
133 XWarpPointer(gdi_display
, None
, DefaultRootWindow(gdi_display
), 0, 0, 0, 0, 0, 0);
135 XSync(gdi_display
, False
);
137 X11DRV_handle_desktop_resize( real_xf86vm_modes
[mode
]->hdisplay
,
138 real_xf86vm_modes
[mode
]->vdisplay
);
141 void X11DRV_XF86VM_Init(void)
146 DWORD dwBpp
= screen_depth
;
147 if (dwBpp
== 24) dwBpp
= 32;
149 if (xf86vm_major
) return; /* already initialized? */
151 if (!usexvidmode
) return;
153 /* see if XVidMode is available */
155 ok
= XF86VidModeQueryExtension(gdi_display
, &xf86vm_event
, &xf86vm_error
);
158 X11DRV_expect_error(gdi_display
, XVidModeErrorHandler
, NULL
);
159 ok
= XF86VidModeQueryVersion(gdi_display
, &xf86vm_major
, &xf86vm_minor
);
160 if (X11DRV_check_error()) ok
= FALSE
;
164 #ifdef X_XF86VidModeSetGammaRamp
165 if (xf86vm_major
> 2 || (xf86vm_major
== 2 && xf86vm_minor
>= 1))
167 XF86VidModeGetGammaRampSize(gdi_display
, DefaultScreen(gdi_display
),
168 &xf86vm_gammaramp_size
);
169 if (xf86vm_gammaramp_size
== 256)
170 xf86vm_use_gammaramp
= TRUE
;
172 #endif /* X_XF86VidModeSetGammaRamp */
175 if (!using_wine_desktop
) ok
= XF86VidModeGetAllModeLines(gdi_display
, DefaultScreen(gdi_display
), &nmodes
, &real_xf86vm_modes
);
180 /* In desktop mode, do not switch resolution... But still use the Gamma ramp stuff */
181 if (using_wine_desktop
) return;
183 TRACE("XVidMode modes: count=%d\n", nmodes
);
185 real_xf86vm_mode_count
= nmodes
;
187 dd_modes
= X11DRV_Settings_SetHandlers("XF86VidMode",
188 X11DRV_XF86VM_GetCurrentMode
,
189 X11DRV_XF86VM_SetCurrentMode
,
192 /* convert modes to DDHALMODEINFO format */
193 for (i
=0; i
<real_xf86vm_mode_count
; i
++)
195 convert_modeinfo(real_xf86vm_modes
[i
]);
197 /* add modes for different color depths */
198 X11DRV_Settings_AddDepthModes();
199 dd_mode_count
= X11DRV_Settings_GetModeCount();
201 TRACE("Available DD modes: count=%d\n", dd_mode_count
);
203 /* the first mode in the list seems to be the default */
204 X11DRV_Settings_SetDefaultMode(0);
206 TRACE("Enabling XVidMode\n");
209 void X11DRV_XF86VM_Cleanup(void)
212 if (real_xf86vm_modes
) XFree(real_xf86vm_modes
);
216 void X11DRV_XF86VM_SetExclusiveMode(int lock
)
218 if (!dd_modes
) return; /* no XVidMode */
221 XF86VidModeLockModeSwitch(gdi_display
, DefaultScreen(gdi_display
), lock
);
225 /***** GAMMA CONTROL *****/
226 /* (only available in XF86VidMode 2.x) */
228 #ifdef X_XF86VidModeSetGamma
230 static void GenerateRampFromGamma(WORD ramp
[256], float gamma
)
232 float r_gamma
= 1/gamma
;
234 TRACE("gamma is %f\n", r_gamma
);
235 for (i
=0; i
<256; i
++)
236 ramp
[i
] = pow(i
/255.0, r_gamma
) * 65535.0;
239 static BOOL
ComputeGammaFromRamp(WORD ramp
[256], float *gamma
)
241 float r_x
, r_y
, r_lx
, r_ly
, r_d
, r_v
, r_e
, g_avg
, g_min
, g_max
;
242 unsigned i
, f
, l
, g_n
, c
;
246 ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f
, l
);
250 g_min
= g_max
= g_avg
= 0.0;
251 /* check gamma ramp entries to estimate the gamma */
252 TRACE("analyzing gamma ramp (%d->%d)\n", f
, l
);
253 for (i
=1, g_n
=0; i
<255; i
++) {
254 if (ramp
[i
] < f
|| ramp
[i
] > l
) {
255 ERR("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i
, ramp
[i
], f
, l
);
259 if (!c
) continue; /* avoid log(0) */
261 /* normalize entry values into 0..1 range */
262 r_x
= i
/255.0; r_y
= c
/ r_d
;
263 /* compute logarithms of values */
264 r_lx
= log(r_x
); r_ly
= log(r_y
);
265 /* compute gamma for this entry */
267 /* compute differential (error estimate) for this entry */
268 /* some games use table-based logarithms that magnifies the error by 128 */
269 r_e
= -r_lx
* 128 / (c
* r_lx
* r_lx
);
271 /* compute min & max while compensating for estimated error */
272 if (!g_n
|| g_min
> (r_v
+ r_e
)) g_min
= r_v
+ r_e
;
273 if (!g_n
|| g_max
< (r_v
- r_e
)) g_max
= r_v
- r_e
;
278 /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
281 ERR("no gamma data, shouldn't happen\n");
285 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f
, 65535-l
, g_avg
);
286 /* the bias could be because the app wanted something like a "red shift"
287 * like when you're hit in Quake, but XVidMode doesn't support it,
288 * so we have to reject a significant bias */
289 if (f
&& f
> (pow(1/255.0, g_avg
) * 65536.0)) {
290 ERR("low-biased gamma ramp (%d), rejected\n", f
);
293 /* check that the gamma is reasonably uniform across the ramp */
294 if (g_max
- g_min
> 0.1) {
295 ERR("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max
, g_min
, g_avg
);
298 /* ok, now we're pretty sure we can set the desired gamma ramp,
304 #endif /* X_XF86VidModeSetGamma */
306 /* Hmm... should gamma control be available in desktop mode or not?
307 * I'll assume that it should */
309 BOOL
X11DRV_XF86VM_GetGammaRamp(LPDDGAMMARAMP ramp
)
311 #ifdef X_XF86VidModeSetGamma
312 XF86VidModeGamma gamma
;
315 if (xf86vm_major
< 2) return FALSE
; /* no gamma control */
316 #ifdef X_XF86VidModeSetGammaRamp
317 else if (xf86vm_use_gammaramp
)
321 ret
= XF86VidModeGetGammaRamp(gdi_display
, DefaultScreen(gdi_display
), 256,
322 ramp
->red
, ramp
->green
, ramp
->blue
);
330 ret
= XF86VidModeGetGamma(gdi_display
, DefaultScreen(gdi_display
), &gamma
);
333 GenerateRampFromGamma(ramp
->red
, gamma
.red
);
334 GenerateRampFromGamma(ramp
->green
, gamma
.green
);
335 GenerateRampFromGamma(ramp
->blue
, gamma
.blue
);
339 #endif /* X_XF86VidModeSetGamma */
343 BOOL
X11DRV_XF86VM_SetGammaRamp(LPDDGAMMARAMP ramp
)
345 #ifdef X_XF86VidModeSetGamma
346 XF86VidModeGamma gamma
;
348 if (xf86vm_major
< 2) return FALSE
; /* no gamma control */
349 #ifdef X_XF86VidModeSetGammaRamp
350 else if (xf86vm_use_gammaramp
)
354 ret
= XF86VidModeSetGammaRamp(gdi_display
, DefaultScreen(gdi_display
), 256,
355 ramp
->red
, ramp
->green
, ramp
->blue
);
362 if (ComputeGammaFromRamp(ramp
->red
, &gamma
.red
) &&
363 ComputeGammaFromRamp(ramp
->green
, &gamma
.green
) &&
364 ComputeGammaFromRamp(ramp
->blue
, &gamma
.blue
)) {
367 ret
= XF86VidModeSetGamma(gdi_display
, DefaultScreen(gdi_display
), &gamma
);
372 #endif /* X_XF86VidModeSetGamma */
376 #endif /* HAVE_LIBXXF86VM */
378 /***********************************************************************
379 * GetDeviceGammaRamp (X11DRV.@)
381 * FIXME: should move to somewhere appropriate, but probably not before
382 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
383 * they can include xvidmode.h directly
385 BOOL
X11DRV_GetDeviceGammaRamp(X11DRV_PDEVICE
*physDev
, LPVOID ramp
)
387 #ifdef HAVE_LIBXXF86VM
388 return X11DRV_XF86VM_GetGammaRamp(ramp
);
394 /***********************************************************************
395 * SetDeviceGammaRamp (X11DRV.@)
397 * FIXME: should move to somewhere appropriate, but probably not before
398 * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
399 * they can include xvidmode.h directly
401 BOOL
X11DRV_SetDeviceGammaRamp(X11DRV_PDEVICE
*physDev
, LPVOID ramp
)
403 #ifdef HAVE_LIBXXF86VM
404 return X11DRV_XF86VM_SetGammaRamp(ramp
);