- define additional shell paths for CSIDL_... constants
[wine] / dlls / x11drv / xvidmode.c
1 /*
2  * DirectDraw XVidMode interface
3  *
4  * Copyright 2001 TransGaming Technologies, Inc.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include <string.h>
23 #include <stdio.h>
24 #include <math.h>
25
26 #include <X11/Xlib.h>
27
28 #ifdef HAVE_LIBXXF86VM
29 #define XMD_H
30 #include "basetsd.h"
31 #include <X11/extensions/xf86vmode.h>
32 #endif /* HAVE_LIBXXF86VM */
33
34 #include "x11drv.h"
35 #include "x11ddraw.h"
36 #include "xvidmode.h"
37
38 #include "windef.h"
39 #include "wingdi.h"
40 #include "ddrawi.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(xvidmode);
44
45 #ifdef HAVE_LIBXXF86VM
46
47 extern int usexvidmode;
48
49 static int xf86vm_event, xf86vm_error, xf86vm_major, xf86vm_minor;
50
51 #ifdef X_XF86VidModeSetGammaRamp
52 static int xf86vm_gammaramp_size;
53 static BOOL xf86vm_use_gammaramp;
54 #endif /* X_XF86VidModeSetGammaRamp */
55
56 static LPDDHALMODEINFO dd_modes;
57 static unsigned int dd_mode_count;
58 static XF86VidModeModeInfo** real_xf86vm_modes;
59 static unsigned int real_xf86vm_mode_count;
60 static unsigned int xf86vm_initial_mode = 0;
61
62 static void convert_modeinfo( const XF86VidModeModeInfo *mode)
63 {
64   int rate;
65   if (mode->htotal!=0 && mode->vtotal!=0)
66       rate = mode->dotclock * 1000 / (mode->htotal * mode->vtotal);
67   else
68       rate = 0;
69   X11DRV_Settings_AddOneMode(mode->hdisplay, mode->vdisplay, 0, rate);
70 }
71
72 static void convert_modeline(int dotclock, const XF86VidModeModeLine *mode, LPDDHALMODEINFO info, unsigned int bpp)
73 {
74   info->dwWidth      = mode->hdisplay;
75   info->dwHeight     = mode->vdisplay;
76   if (mode->htotal!=0 && mode->vtotal!=0)
77       info->wRefreshRate = dotclock * 1000 / (mode->htotal * mode->vtotal);
78   else
79       info->wRefreshRate = 0;
80   TRACE(" width=%ld, height=%ld, refresh=%d\n",
81         info->dwWidth, info->dwHeight, info->wRefreshRate);
82   info->lPitch         = 0;
83   info->dwBPP          = bpp;
84   info->wFlags         = 0;
85   info->dwRBitMask     = 0;
86   info->dwGBitMask     = 0;
87   info->dwBBitMask     = 0;
88   info->dwAlphaBitMask = 0;
89 }
90
91 static int XVidModeErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
92 {
93     return 1;
94 }
95
96 static Bool in_desktop_mode;
97
98 int X11DRV_XF86VM_GetCurrentMode(void)
99 {
100   XF86VidModeModeLine line;
101   int dotclock, i;
102   DDHALMODEINFO cmode;
103   DWORD dwBpp = screen_depth;
104   if (dwBpp == 24) dwBpp = 32;
105
106   TRACE("Querying XVidMode current mode\n");
107   wine_tsx11_lock();
108   XF86VidModeGetModeLine(gdi_display, DefaultScreen(gdi_display), &dotclock, &line);
109   wine_tsx11_unlock();
110   convert_modeline(dotclock, &line, &cmode, dwBpp);
111   for (i=0; i<dd_mode_count; i++)
112     if (memcmp(&dd_modes[i], &cmode, sizeof(cmode)) == 0) {
113       TRACE("mode=%d\n", i);
114       return i;
115     }
116   ERR("In unknown mode, returning default\n");
117   return xf86vm_initial_mode;
118 }
119
120 void X11DRV_XF86VM_SetCurrentMode(int mode)
121 {
122   DWORD dwBpp = screen_depth;
123   if (dwBpp == 24) dwBpp = 32;
124   /* only set modes from the original color depth */
125   if (dwBpp != dd_modes[mode].dwBPP)
126   {
127       FIXME("Cannot change screen BPP from %ld to %ld\n", dwBpp, dd_modes[mode].dwBPP);
128   }
129   mode = mode % real_xf86vm_mode_count;
130
131   wine_tsx11_lock();
132   TRACE("Resizing X display to %dx%d\n", 
133         real_xf86vm_modes[mode]->hdisplay, real_xf86vm_modes[mode]->vdisplay);
134   XF86VidModeSwitchToMode(gdi_display, DefaultScreen(gdi_display), real_xf86vm_modes[mode]);
135 #if 0 /* FIXME */
136   SYSMETRICS_Set( SM_CXSCREEN, real_xf86vm_modes[mode]->hdisplay );
137   SYSMETRICS_Set( SM_CYSCREEN, real_xf86vm_modes[mode]->vdisplay );
138 #else
139   FIXME("Need to update SYSMETRICS after resizing display (now %dx%d)\n",
140         real_xf86vm_modes[mode]->hdisplay, real_xf86vm_modes[mode]->vdisplay);
141 #endif
142 #if 0 /* it is said that SetViewPort causes problems with some X servers */
143   XF86VidModeSetViewPort(gdi_display, DefaultScreen(gdi_display), 0, 0);
144 #else
145   XWarpPointer(gdi_display, None, DefaultRootWindow(gdi_display), 0, 0, 0, 0, 0, 0);
146 #endif
147   XSync(gdi_display, False);
148   wine_tsx11_unlock();
149 }
150
151 void X11DRV_XF86VM_Init(void)
152 {
153   Bool ok;
154   int nmodes, i;
155   DWORD dwBpp = screen_depth;
156   if (dwBpp == 24) dwBpp = 32;
157
158   in_desktop_mode = (root_window != DefaultRootWindow(gdi_display));
159
160   if (xf86vm_major) return; /* already initialized? */
161
162   if (!usexvidmode) return;
163
164   /* see if XVidMode is available */
165   wine_tsx11_lock();
166   ok = XF86VidModeQueryExtension(gdi_display, &xf86vm_event, &xf86vm_error);
167   if (ok)
168   {
169       X11DRV_expect_error(gdi_display, XVidModeErrorHandler, NULL);
170       ok = XF86VidModeQueryVersion(gdi_display, &xf86vm_major, &xf86vm_minor);
171       if (X11DRV_check_error()) ok = FALSE;
172   }
173   if (ok)
174   {
175 #ifdef X_XF86VidModeSetGammaRamp
176       if (xf86vm_major > 2 || (xf86vm_major == 2 && xf86vm_minor >= 1))
177       {
178           XF86VidModeGetGammaRampSize(gdi_display, DefaultScreen(gdi_display),
179                                       &xf86vm_gammaramp_size);
180           if (xf86vm_gammaramp_size == 256)
181               xf86vm_use_gammaramp = TRUE;
182       }
183 #endif /* X_XF86VidModeSetGammaRamp */
184
185       /* retrieve modes */
186       if (!in_desktop_mode) ok = XF86VidModeGetAllModeLines(gdi_display, DefaultScreen(gdi_display), &nmodes, &real_xf86vm_modes);
187   }
188   wine_tsx11_unlock();
189   if (!ok) return;
190
191   /* In desktop mode, do not switch resolution... But still use the Gamma ramp stuff */
192   if (in_desktop_mode) return;
193   
194   TRACE("XVidMode modes: count=%d\n", nmodes);
195
196   real_xf86vm_mode_count = nmodes;
197
198   dd_modes = X11DRV_Settings_SetHandlers("XF86VidMode", 
199                                          X11DRV_XF86VM_GetCurrentMode, 
200                                          X11DRV_XF86VM_SetCurrentMode, 
201                                          nmodes, 1);
202
203   /* convert modes to DDHALMODEINFO format */
204   for (i=0; i<real_xf86vm_mode_count; i++)
205   {
206       convert_modeinfo(real_xf86vm_modes[i]);
207   }
208   /* add modes for different color depths */
209   X11DRV_Settings_AddDepthModes();
210   dd_mode_count = X11DRV_Settings_GetModeCount();
211
212   TRACE("Available DD modes: count=%d\n", dd_mode_count);
213
214   /* store the current mode at the time we started */
215   xf86vm_initial_mode = X11DRV_XF86VM_GetCurrentMode();
216   X11DRV_Settings_SetDefaultMode(xf86vm_initial_mode);
217   
218   TRACE("Enabling XVidMode\n");
219 }
220
221 void X11DRV_XF86VM_Cleanup(void)
222 {
223   wine_tsx11_lock();
224   if (real_xf86vm_modes) XFree(real_xf86vm_modes);
225   wine_tsx11_unlock();
226 }
227
228 void X11DRV_XF86VM_SetExclusiveMode(int lock)
229 {
230   if (!dd_modes) return; /* no XVidMode */
231
232   wine_tsx11_lock();
233   XF86VidModeLockModeSwitch(gdi_display, DefaultScreen(gdi_display), lock);
234   wine_tsx11_unlock();
235 }
236
237 /***** GAMMA CONTROL *****/
238 /* (only available in XF86VidMode 2.x) */
239
240 #ifdef X_XF86VidModeSetGamma
241
242 static void GenerateRampFromGamma(WORD ramp[256], float gamma)
243 {
244   float r_gamma = 1/gamma;
245   unsigned i;
246   TRACE("gamma is %f\n", r_gamma);
247   for (i=0; i<256; i++)
248     ramp[i] = pow(i/255.0, r_gamma) * 65535.0;
249 }
250
251 static BOOL ComputeGammaFromRamp(WORD ramp[256], float *gamma)
252 {
253   float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
254   unsigned i, f, l, g_n, c;
255   f = ramp[0];
256   l = ramp[255];
257   if (f >= l) {
258     ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
259     return FALSE;
260   }
261   r_d = l - f;
262   g_min = g_max = g_avg = 0.0;
263   /* check gamma ramp entries to estimate the gamma */
264   TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
265   for (i=1, g_n=0; i<255; i++) {
266     if (ramp[i] < f || ramp[i] > l) {
267       ERR("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
268       return FALSE;
269     }
270     c = ramp[i] - f;
271     if (!c) continue; /* avoid log(0) */
272
273     /* normalize entry values into 0..1 range */
274     r_x = i/255.0; r_y = c / r_d;
275     /* compute logarithms of values */
276     r_lx = log(r_x); r_ly = log(r_y);
277     /* compute gamma for this entry */
278     r_v = r_ly / r_lx;
279     /* compute differential (error estimate) for this entry */
280     /* some games use table-based logarithms that magnifies the error by 128 */
281     r_e = -r_lx * 128 / (c * r_lx * r_lx);
282
283     /* compute min & max while compensating for estimated error */
284     if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
285     if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
286
287     /* add to average */
288     g_avg += r_v;
289     g_n++;
290     /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
291   }
292   if (!g_n) {
293     ERR("no gamma data, shouldn't happen\n");
294     return FALSE;
295   }
296   g_avg /= g_n;
297   TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
298   /* the bias could be because the app wanted something like a "red shift"
299    * like when you're hit in Quake, but XVidMode doesn't support it,
300    * so we have to reject a significant bias */
301   if (f && f > (pow(1/255.0, g_avg) * 65536.0)) {
302     ERR("low-biased gamma ramp (%d), rejected\n", f);
303     return FALSE;
304   }
305   /* check that the gamma is reasonably uniform across the ramp */
306   if (g_max - g_min > 0.1) {
307     ERR("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
308     return FALSE;
309   }
310   /* ok, now we're pretty sure we can set the desired gamma ramp,
311    * so go for it */
312   *gamma = 1/g_avg;
313   return TRUE;
314 }
315
316 #endif /* X_XF86VidModeSetGamma */
317
318 /* Hmm... should gamma control be available in desktop mode or not?
319  * I'll assume that it should */
320
321 BOOL X11DRV_XF86VM_GetGammaRamp(LPDDGAMMARAMP ramp)
322 {
323 #ifdef X_XF86VidModeSetGamma
324   XF86VidModeGamma gamma;
325   Bool ret;
326
327   if (xf86vm_major < 2) return FALSE; /* no gamma control */
328 #ifdef X_XF86VidModeSetGammaRamp
329   else if (xf86vm_use_gammaramp)
330   {
331       Bool ret;
332       wine_tsx11_lock();
333       ret = XF86VidModeGetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
334                                     ramp->red, ramp->green, ramp->blue);
335       wine_tsx11_unlock();
336       return ret;
337   }
338 #endif
339   else
340   {
341       wine_tsx11_lock();
342       ret = XF86VidModeGetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
343       wine_tsx11_unlock();
344       if (ret) {
345           GenerateRampFromGamma(ramp->red,   gamma.red);
346           GenerateRampFromGamma(ramp->green, gamma.green);
347           GenerateRampFromGamma(ramp->blue,  gamma.blue);
348           return TRUE;
349       }
350   }
351 #endif /* X_XF86VidModeSetGamma */
352   return FALSE;
353 }
354
355 BOOL X11DRV_XF86VM_SetGammaRamp(LPDDGAMMARAMP ramp)
356 {
357 #ifdef X_XF86VidModeSetGamma
358   XF86VidModeGamma gamma;
359
360   if (xf86vm_major < 2) return FALSE; /* no gamma control */
361 #ifdef X_XF86VidModeSetGammaRamp
362   else if (xf86vm_use_gammaramp)
363   {
364       Bool ret;
365       wine_tsx11_lock();
366       ret = XF86VidModeSetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
367                                     ramp->red, ramp->green, ramp->blue);
368       wine_tsx11_unlock();
369       return ret;
370   }
371 #endif
372   else
373   {
374       if (ComputeGammaFromRamp(ramp->red,   &gamma.red) &&
375           ComputeGammaFromRamp(ramp->green, &gamma.green) &&
376           ComputeGammaFromRamp(ramp->blue,  &gamma.blue)) {
377           Bool ret;
378           wine_tsx11_lock();
379           ret = XF86VidModeSetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
380           wine_tsx11_unlock();
381           return ret;
382       }
383   }
384 #endif /* X_XF86VidModeSetGamma */
385   return FALSE;
386 }
387
388 #endif /* HAVE_LIBXXF86VM */
389
390 /***********************************************************************
391  *              GetDeviceGammaRamp (X11DRV.@)
392  *
393  * FIXME: should move to somewhere appropriate, but probably not before
394  * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
395  * they can include xvidmode.h directly
396  */
397 BOOL X11DRV_GetDeviceGammaRamp(X11DRV_PDEVICE *physDev, LPVOID ramp)
398 {
399 #ifdef HAVE_LIBXXF86VM
400   return X11DRV_XF86VM_GetGammaRamp(ramp);
401 #else
402   return FALSE;
403 #endif
404 }
405
406 /***********************************************************************
407  *              SetDeviceGammaRamp (X11DRV.@)
408  *
409  * FIXME: should move to somewhere appropriate, but probably not before
410  * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
411  * they can include xvidmode.h directly
412  */
413 BOOL X11DRV_SetDeviceGammaRamp(X11DRV_PDEVICE *physDev, LPVOID ramp)
414 {
415 #ifdef HAVE_LIBXXF86VM
416   return X11DRV_XF86VM_SetGammaRamp(ramp);
417 #else
418   return FALSE;
419 #endif
420 }