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