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