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
27 #ifdef HAVE_LIBXXF86VM
30 #include <X11/extensions/xf86vmode.h>
31 #endif /* HAVE_LIBXXF86VM */
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
44 #ifdef HAVE_LIBXXF86VM
46 extern int 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;
55 static LPDDHALMODEINFO xf86vm_modes;
56 static unsigned xf86vm_mode_count;
57 static XF86VidModeModeInfo** modes;
58 static unsigned int xf86vm_initial_mode;
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);
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... */
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);
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... */
99 info->dwAlphaBitMask = 0;
102 static int XVidModeErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
107 void X11DRV_XF86VM_Init(void)
111 Bool in_desktop_mode = (root_window != DefaultRootWindow(gdi_display));
113 if (xf86vm_major) return; /* already initialized? */
115 if (!usexvidmode) return;
117 /* see if XVidMode is available */
119 ok = XF86VidModeQueryExtension(gdi_display, &xf86vm_event, &xf86vm_error);
122 X11DRV_expect_error(gdi_display, XVidModeErrorHandler, NULL);
123 ok = XF86VidModeQueryVersion(gdi_display, &xf86vm_major, &xf86vm_minor);
124 if (X11DRV_check_error()) ok = FALSE;
128 #ifdef X_XF86VidModeSetGammaRamp
129 if (xf86vm_major > 2 || (xf86vm_major == 2 && xf86vm_minor >= 1))
131 XF86VidModeGetGammaRampSize(gdi_display, DefaultScreen(gdi_display),
132 &xf86vm_gammaramp_size);
133 if (xf86vm_gammaramp_size == 256)
134 xf86vm_use_gammaramp = TRUE;
139 if (!in_desktop_mode) ok = XF86VidModeGetAllModeLines(gdi_display, DefaultScreen(gdi_display), &nmodes, &modes);
144 /* In desktop mode, do not switch resolution... But still use the Gamma ramp stuff */
145 if (in_desktop_mode) return;
147 TRACE("XVidMode modes: count=%d\n", nmodes);
149 xf86vm_mode_count = nmodes;
150 xf86vm_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDHALMODEINFO) * nmodes);
152 /* convert modes to DDHALMODEINFO format */
153 for (i=0; i<nmodes; i++)
154 convert_modeinfo(modes[i], &xf86vm_modes[i]);
156 /* store the current mode at the time we started */
157 xf86vm_initial_mode = X11DRV_XF86VM_GetCurrentMode();
159 TRACE("Enabling XVidMode\n");
162 void X11DRV_XF86VM_Cleanup(void)
164 if (modes) TSXFree(modes);
167 int X11DRV_XF86VM_GetCurrentMode(void)
169 XF86VidModeModeLine line;
173 if (!xf86vm_modes) return 0; /* no XVidMode */
175 TRACE("Querying XVidMode current mode\n");
177 XF86VidModeGetModeLine(gdi_display, DefaultScreen(gdi_display), &dotclock, &line);
179 convert_modeline(dotclock, &line, &cmode);
180 for (i=0; i<xf86vm_mode_count; i++)
181 if (memcmp(&xf86vm_modes[i], &cmode, sizeof(cmode)) == 0) {
182 TRACE("mode=%d\n", i);
185 ERR("unknown mode, shouldn't happen\n");
186 return 0; /* return first mode */
189 void X11DRV_XF86VM_SetCurrentMode(int mode)
191 if (!xf86vm_modes) return; /* no XVidMode */
194 XF86VidModeSwitchToMode(gdi_display, DefaultScreen(gdi_display), modes[mode]);
195 #if 0 /* it is said that SetViewPort causes problems with some X servers */
196 XF86VidModeSetViewPort(gdi_display, DefaultScreen(gdi_display), 0, 0);
198 XWarpPointer(gdi_display, None, DefaultRootWindow(gdi_display), 0, 0, 0, 0, 0, 0);
200 XSync(gdi_display, False);
204 void X11DRV_XF86VM_SetExclusiveMode(int lock)
206 if (!xf86vm_modes) return; /* no XVidMode */
209 XF86VidModeLockModeSwitch(gdi_display, DefaultScreen(gdi_display), lock);
213 /* actual DirectDraw HAL stuff */
215 static DWORD PASCAL X11DRV_XF86VM_SetMode(LPDDHAL_SETMODEDATA data)
217 X11DRV_XF86VM_SetCurrentMode(data->dwModeIndex);
218 X11DRV_DDHAL_SwitchMode(data->dwModeIndex, NULL, NULL);
219 data->ddRVal = DD_OK;
220 return DDHAL_DRIVER_HANDLED;
223 int X11DRV_XF86VM_CreateDriver(LPDDHALINFO info)
225 if (!xf86vm_mode_count) return 0; /* no XVidMode */
227 info->dwNumModes = xf86vm_mode_count;
228 info->lpModeInfo = xf86vm_modes;
229 X11DRV_DDHAL_SwitchMode(X11DRV_XF86VM_GetCurrentMode(), NULL, NULL);
230 info->lpDDCallbacks->SetMode = X11DRV_XF86VM_SetMode;
235 /***** GAMMA CONTROL *****/
236 /* (only available in XF86VidMode 2.x) */
238 #ifdef X_XF86VidModeSetGamma
240 static void GenerateRampFromGamma(WORD ramp[256], float gamma)
242 float r_gamma = 1/gamma;
244 TRACE("gamma is %f\n", r_gamma);
245 for (i=0; i<256; i++)
246 ramp[i] = pow(i/255.0, r_gamma) * 65535.0;
249 static BOOL ComputeGammaFromRamp(WORD ramp[256], float *gamma)
251 float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
252 unsigned i, f, l, g_n, c;
256 ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
260 g_min = g_max = g_avg = 0.0;
261 /* check gamma ramp entries to estimate the gamma */
262 TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
263 for (i=1, g_n=0; i<255; i++) {
264 if (ramp[i] < f || ramp[i] > l) {
265 ERR("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
269 if (!c) continue; /* avoid log(0) */
271 /* normalize entry values into 0..1 range */
272 r_x = i/255.0; r_y = c / r_d;
273 /* compute logarithms of values */
274 r_lx = log(r_x); r_ly = log(r_y);
275 /* compute gamma for this entry */
277 /* compute differential (error estimate) for this entry */
278 /* some games use table-based logarithms that magnifies the error by 128 */
279 r_e = -r_lx * 128 / (c * r_lx * r_lx);
281 /* compute min & max while compensating for estimated error */
282 if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
283 if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
288 /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
291 ERR("no gamma data, shouldn't happen\n");
295 TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
296 /* the bias could be because the app wanted something like a "red shift"
297 * like when you're hit in Quake, but XVidMode doesn't support it,
298 * so we have to reject a significant bias */
299 if (f && f > (pow(1/255.0, g_avg) * 65536.0)) {
300 ERR("low-biased gamma ramp (%d), rejected\n", f);
303 /* check that the gamma is reasonably uniform across the ramp */
304 if (g_max - g_min > 0.1) {
305 ERR("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
308 /* ok, now we're pretty sure we can set the desired gamma ramp,
314 #endif /* X_XF86VidModeSetGamma */
316 /* Hmm... should gamma control be available in desktop mode or not?
317 * I'll assume that it should */
319 BOOL X11DRV_XF86VM_GetGammaRamp(LPDDGAMMARAMP ramp)
321 #ifdef X_XF86VidModeSetGamma
322 XF86VidModeGamma gamma;
325 if (xf86vm_major < 2) return FALSE; /* no gamma control */
326 #ifdef X_XF86VidModeSetGammaRamp
327 else if (xf86vm_use_gammaramp)
331 ret = XF86VidModeGetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
332 ramp->red, ramp->green, ramp->blue);
340 ret = XF86VidModeGetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
343 GenerateRampFromGamma(ramp->red, gamma.red);
344 GenerateRampFromGamma(ramp->green, gamma.green);
345 GenerateRampFromGamma(ramp->blue, gamma.blue);
349 #endif /* X_XF86VidModeSetGamma */
353 BOOL X11DRV_XF86VM_SetGammaRamp(LPDDGAMMARAMP ramp)
355 #ifdef X_XF86VidModeSetGamma
356 XF86VidModeGamma gamma;
358 if (xf86vm_major < 2) return FALSE; /* no gamma control */
359 #ifdef X_XF86VidModeSetGammaRamp
360 else if (xf86vm_use_gammaramp)
364 ret = XF86VidModeSetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
365 ramp->red, ramp->green, ramp->blue);
372 if (ComputeGammaFromRamp(ramp->red, &gamma.red) &&
373 ComputeGammaFromRamp(ramp->green, &gamma.green) &&
374 ComputeGammaFromRamp(ramp->blue, &gamma.blue)) {
377 ret = XF86VidModeSetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
382 #endif /* X_XF86VidModeSetGamma */
386 #endif /* HAVE_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(X11DRV_PDEVICE *physDev, LPVOID ramp)
397 #ifdef HAVE_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(X11DRV_PDEVICE *physDev, LPVOID ramp)
413 #ifdef HAVE_LIBXXF86VM
414 return X11DRV_XF86VM_SetGammaRamp(ramp);
420 /***********************************************************************
421 * EnumDisplaySettingsExW (X11DRV.@)
423 BOOL X11DRV_EnumDisplaySettingsExW( LPCWSTR name, DWORD n, LPDEVMODEW devmode, DWORD flags)
425 DWORD dwBpp = screen_depth;
426 if (dwBpp == 24) dwBpp = 32;
427 devmode->dmDisplayFlags = 0;
428 devmode->dmDisplayFrequency = 85;
429 devmode->dmSize = sizeof(DEVMODEW);
430 if (n==0 || n == (DWORD)-1 || n == (DWORD)-2)
432 devmode->dmBitsPerPel = dwBpp;
433 devmode->dmPelsHeight = GetSystemMetrics(SM_CYSCREEN);
434 devmode->dmPelsWidth = GetSystemMetrics(SM_CXSCREEN);
435 devmode->dmFields = (DM_PELSWIDTH|DM_PELSHEIGHT|DM_BITSPERPEL);
436 TRACE("mode %ld -- returning default %ldx%ldx%ldbpp\n", n,
437 devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel);
440 #ifdef HAVE_LIBXXF86VM
441 if (n <= xf86vm_mode_count)
443 XF86VidModeModeInfo *mode;
445 devmode->dmPelsWidth = mode->hdisplay;
446 devmode->dmPelsHeight = mode->vdisplay;
447 devmode->dmBitsPerPel = dwBpp;
448 devmode->dmDisplayFrequency = mode->dotclock * 1000 / (mode->htotal * mode->vtotal);
449 devmode->dmFields = (DM_PELSWIDTH|DM_PELSHEIGHT|DM_BITSPERPEL|DM_DISPLAYFREQUENCY);
450 TRACE("mode %ld -- %ldx%ldx%ldbpp\n", n,
451 devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel);
455 TRACE("mode %ld -- not present\n", n);
461 #define _X_FIELD(prefix, bits) if ((fields) & prefix##_##bits) {p+=sprintf(p, "%s%s", first ? "" : ",", #bits); first=FALSE;}
462 static const char * _CDS_flags(DWORD fields)
467 _X_FIELD(CDS,UPDATEREGISTRY);_X_FIELD(CDS,TEST);_X_FIELD(CDS,FULLSCREEN);
468 _X_FIELD(CDS,GLOBAL);_X_FIELD(CDS,SET_PRIMARY);_X_FIELD(CDS,RESET);
469 _X_FIELD(CDS,SETRECT);_X_FIELD(CDS,NORESET);
471 return wine_dbg_sprintf("%s", buf);
473 static const char * _DM_fields(DWORD fields)
478 _X_FIELD(DM,BITSPERPEL);_X_FIELD(DM,PELSWIDTH);_X_FIELD(DM,PELSHEIGHT);
479 _X_FIELD(DM,DISPLAYFLAGS);_X_FIELD(DM,DISPLAYFREQUENCY);_X_FIELD(DM,POSITION);
481 return wine_dbg_sprintf("%s", buf);
485 /***********************************************************************
486 * ChangeDisplaySettingsExW (X11DRV.@)
488 LONG X11DRV_ChangeDisplaySettingsExW( LPCWSTR devname, LPDEVMODEW devmode,
489 HWND hwnd, DWORD flags, LPVOID lpvoid )
492 DWORD dwBpp = screen_depth;
493 if (dwBpp == 24) dwBpp = 32;
494 TRACE("(%s,%p,%p,0x%08lx,%p\n",debugstr_w(devname),devmode,hwnd,flags,lpvoid);
495 TRACE("flags=%s\n",_CDS_flags(flags));
498 TRACE("Return to original display mode\n");
499 #ifdef HAVE_LIBXXF86VM
500 X11DRV_XF86VM_SetCurrentMode(xf86vm_initial_mode);
502 return DISP_CHANGE_SUCCESSFUL;
505 if (TRACE_ON(x11drv))
507 TRACE("DM_fields=%s\n",_DM_fields(devmode->dmFields));
508 TRACE("width=%ld height=%ld bpp=%ld freq=%ld\n",
509 devmode->dmPelsWidth,devmode->dmPelsHeight,
510 devmode->dmBitsPerPel,devmode->dmDisplayFrequency);
512 if ((!(devmode->dmFields & DM_BITSPERPEL) || devmode->dmBitsPerPel == dwBpp) &&
513 (!(devmode->dmFields & DM_PELSWIDTH) || devmode->dmPelsWidth == GetSystemMetrics(SM_CXSCREEN)) &&
514 (!(devmode->dmFields & DM_PELSHEIGHT) || devmode->dmPelsHeight == GetSystemMetrics(SM_CYSCREEN)))
516 /* we have a valid mode */
517 TRACE("Requested mode matches current mode -- no change!\n");
518 return DISP_CHANGE_SUCCESSFUL;
521 #ifdef HAVE_LIBXXF86VM
522 for (i = 0; i < xf86vm_mode_count; i++)
524 XF86VidModeModeInfo *mode = modes[i];
525 if (devmode->dmFields & DM_BITSPERPEL)
527 if (devmode->dmBitsPerPel != dwBpp)
530 if (devmode->dmFields & DM_PELSWIDTH)
532 if (devmode->dmPelsWidth != mode->hdisplay)
535 if (devmode->dmFields & DM_PELSHEIGHT)
537 if (devmode->dmPelsHeight != mode->vdisplay)
540 /* we have a valid mode */
541 TRACE("Matches mode %ld\n", i);
542 X11DRV_XF86VM_SetCurrentMode(i);
544 SYSMETRICS_Set( SM_CXSCREEN, devmode->dmPelsWidth );
545 SYSMETRICS_Set( SM_CYSCREEN, devmode->dmPelsHeight );
547 return DISP_CHANGE_SUCCESSFUL;
551 /* no valid modes found */
552 ERR("No matching mode found!\n");
553 return DISP_CHANGE_BADMODE;