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