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