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