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