Fix compilation error reported by Stefan Leichter when using old
[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 "ts_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 #include "x11drv.h"
33
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(x11drv);
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
54
55 static LPDDHALMODEINFO xf86vm_modes;
56 static unsigned xf86vm_mode_count;
57 static XF86VidModeModeInfo** modes;
58 static unsigned int xf86vm_initial_mode;
59
60 static void convert_modeinfo( const XF86VidModeModeInfo *mode, LPDDHALMODEINFO info )
61 {
62   info->dwWidth      = mode->hdisplay;
63   info->dwHeight     = mode->vdisplay;
64   if (mode->htotal!=0 && mode->vtotal!=0)
65       info->wRefreshRate = mode->dotclock * 1000 / (mode->htotal * mode->vtotal);
66   else
67       info->wRefreshRate = 0;
68   TRACE(" width=%ld, height=%ld, refresh=%d\n",
69         info->dwWidth, info->dwHeight, info->wRefreshRate);
70   /* XVidMode cannot change display depths... */
71   /* let's not bother with filling out these then... */
72   info->lPitch         = 0;
73   info->dwBPP          = 0;
74   info->wFlags         = 0;
75   info->dwRBitMask     = 0;
76   info->dwGBitMask     = 0;
77   info->dwBBitMask     = 0;
78   info->dwAlphaBitMask = 0;
79 }
80
81 static void convert_modeline(int dotclock, const XF86VidModeModeLine *mode, LPDDHALMODEINFO info)
82 {
83   info->dwWidth      = mode->hdisplay;
84   info->dwHeight     = mode->vdisplay;
85   if (mode->htotal!=0 && mode->vtotal!=0)
86       info->wRefreshRate = dotclock * 1000 / (mode->htotal * mode->vtotal);
87   else
88       info->wRefreshRate = 0;
89   TRACE(" width=%ld, height=%ld, refresh=%d\n",
90         info->dwWidth, info->dwHeight, info->wRefreshRate);
91   /* XVidMode cannot change display depths... */
92   /* let's not bother with filling out these then... */
93   info->lPitch         = 0;
94   info->dwBPP          = 0;
95   info->wFlags         = 0;
96   info->dwRBitMask     = 0;
97   info->dwGBitMask     = 0;
98   info->dwBBitMask     = 0;
99   info->dwAlphaBitMask = 0;
100 }
101
102 static int XVidModeErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
103 {
104     return 1;
105 }
106
107 void X11DRV_XF86VM_Init(void)
108 {
109   int nmodes, i;
110   Bool ok;
111
112   if (xf86vm_major) return; /* already initialized? */
113
114   /* if in desktop mode, don't use XVidMode */
115   if (root_window != DefaultRootWindow(gdi_display)) return;
116
117   if (!usexvidmode) return;
118
119   /* see if XVidMode is available */
120   wine_tsx11_lock();
121   ok = XF86VidModeQueryExtension(gdi_display, &xf86vm_event, &xf86vm_error);
122   if (ok)
123   {
124       X11DRV_expect_error(gdi_display, XVidModeErrorHandler, NULL);
125       ok = XF86VidModeQueryVersion(gdi_display, &xf86vm_major, &xf86vm_minor);
126       if (X11DRV_check_error()) ok = FALSE;
127   }
128   if (ok)
129   {
130 #ifdef X_XF86VidModeSetGammaRamp
131       if (xf86vm_major > 2 || (xf86vm_major == 2 && xf86vm_minor >= 1))
132       {
133           XF86VidModeGetGammaRampSize(gdi_display, DefaultScreen(gdi_display),
134                                       &xf86vm_gammaramp_size);
135           if (xf86vm_gammaramp_size == 256)
136               xf86vm_use_gammaramp = TRUE;
137       }
138 #endif
139
140       /* retrieve modes */
141       ok = XF86VidModeGetAllModeLines(gdi_display, DefaultScreen(gdi_display), &nmodes, &modes);
142   }
143   wine_tsx11_unlock();
144   if (!ok) return;
145
146   TRACE("XVidMode modes: count=%d\n", nmodes);
147
148   xf86vm_mode_count = nmodes;
149   xf86vm_modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DDHALMODEINFO) * nmodes);
150
151   /* convert modes to DDHALMODEINFO format */
152   for (i=0; i<nmodes; i++)
153       convert_modeinfo(modes[i], &xf86vm_modes[i]);
154
155   /* store the current mode at the time we started */
156   xf86vm_initial_mode = X11DRV_XF86VM_GetCurrentMode();
157
158   TRACE("Enabling XVidMode\n");
159 }
160
161 void X11DRV_XF86VM_Cleanup(void)
162 {
163   if (modes) TSXFree(modes);
164 }
165
166 int X11DRV_XF86VM_GetCurrentMode(void)
167 {
168   XF86VidModeModeLine line;
169   int dotclock, i;
170   DDHALMODEINFO cmode;
171
172   if (!xf86vm_modes) return 0; /* no XVidMode */
173
174   TRACE("Querying XVidMode current mode\n");
175   wine_tsx11_lock();
176   XF86VidModeGetModeLine(gdi_display, DefaultScreen(gdi_display), &dotclock, &line);
177   wine_tsx11_unlock();
178   convert_modeline(dotclock, &line, &cmode);
179   for (i=0; i<xf86vm_mode_count; i++)
180     if (memcmp(&xf86vm_modes[i], &cmode, sizeof(cmode)) == 0) {
181       TRACE("mode=%d\n", i);
182       return i;
183     }
184   ERR("unknown mode, shouldn't happen\n");
185   return 0; /* return first mode */
186 }
187
188 void X11DRV_XF86VM_SetCurrentMode(int mode)
189 {
190   if (!xf86vm_modes) return; /* no XVidMode */
191
192   wine_tsx11_lock();
193   XF86VidModeSwitchToMode(gdi_display, DefaultScreen(gdi_display), modes[mode]);
194 #if 0 /* it is said that SetViewPort causes problems with some X servers */
195   XF86VidModeSetViewPort(gdi_display, DefaultScreen(gdi_display), 0, 0);
196 #else
197   XWarpPointer(gdi_display, None, DefaultRootWindow(gdi_display), 0, 0, 0, 0, 0, 0);
198 #endif
199   XSync(gdi_display, False);
200   wine_tsx11_unlock();
201 }
202
203 void X11DRV_XF86VM_SetExclusiveMode(int lock)
204 {
205   if (!xf86vm_modes) return; /* no XVidMode */
206
207   wine_tsx11_lock();
208   XF86VidModeLockModeSwitch(gdi_display, DefaultScreen(gdi_display), lock);
209   wine_tsx11_unlock();
210 }
211
212 /* actual DirectDraw HAL stuff */
213
214 static DWORD PASCAL X11DRV_XF86VM_SetMode(LPDDHAL_SETMODEDATA data)
215 {
216   X11DRV_XF86VM_SetCurrentMode(data->dwModeIndex);
217   X11DRV_DDHAL_SwitchMode(data->dwModeIndex, NULL, NULL);
218   data->ddRVal = DD_OK;
219   return DDHAL_DRIVER_HANDLED;
220 }
221
222 int X11DRV_XF86VM_CreateDriver(LPDDHALINFO info)
223 {
224   if (!xf86vm_mode_count) return 0; /* no XVidMode */
225
226   info->dwNumModes = xf86vm_mode_count;
227   info->lpModeInfo = xf86vm_modes;
228   X11DRV_DDHAL_SwitchMode(X11DRV_XF86VM_GetCurrentMode(), NULL, NULL);
229   info->lpDDCallbacks->SetMode = X11DRV_XF86VM_SetMode;
230   return TRUE;
231 }
232
233
234 /***** GAMMA CONTROL *****/
235 /* (only available in XF86VidMode 2.x) */
236
237 #ifdef X_XF86VidModeSetGamma
238
239 static void GenerateRampFromGamma(WORD ramp[256], float gamma)
240 {
241   float r_gamma = 1/gamma;
242   unsigned i;
243   TRACE("gamma is %f\n", r_gamma);
244   for (i=0; i<256; i++)
245     ramp[i] = pow(i/255.0, r_gamma) * 65535.0;
246 }
247
248 static BOOL ComputeGammaFromRamp(WORD ramp[256], float *gamma)
249 {
250   float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
251   unsigned i, f, l, g_n, c;
252   f = ramp[0];
253   l = ramp[255];
254   if (f >= l) {
255     ERR("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
256     return FALSE;
257   }
258   r_d = l - f;
259   g_min = g_max = g_avg = 0.0;
260   /* check gamma ramp entries to estimate the gamma */
261   TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
262   for (i=1, g_n=0; i<255; i++) {
263     if (ramp[i] < f || ramp[i] > l) {
264       ERR("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
265       return FALSE;
266     }
267     c = ramp[i] - f;
268     if (!c) continue; /* avoid log(0) */
269
270     /* normalize entry values into 0..1 range */
271     r_x = i/255.0; r_y = c / r_d;
272     /* compute logarithms of values */
273     r_lx = log(r_x); r_ly = log(r_y);
274     /* compute gamma for this entry */
275     r_v = r_ly / r_lx;
276     /* compute differential (error estimate) for this entry */
277     /* some games use table-based logarithms that magnifies the error by 128 */
278     r_e = -r_lx * 128 / (c * r_lx * r_lx);
279
280     /* compute min & max while compensating for estimated error */
281     if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
282     if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;
283
284     /* add to average */
285     g_avg += r_v;
286     g_n++;
287     /* TRACE("[%d]=%d, gamma=%f, error=%f\n", i, ramp[i], r_v, r_e); */
288   }
289   if (!g_n) {
290     ERR("no gamma data, shouldn't happen\n");
291     return FALSE;
292   }
293   g_avg /= g_n;
294   TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);
295   /* the bias could be because the app wanted something like a "red shift"
296    * like when you're hit in Quake, but XVidMode doesn't support it,
297    * so we have to reject a significant bias */
298   if (f && f > (pow(1/255.0, g_avg) * 65536.0)) {
299     ERR("low-biased gamma ramp (%d), rejected\n", f);
300     return FALSE;
301   }
302   /* check that the gamma is reasonably uniform across the ramp */
303   if (g_max - g_min > 0.1) {
304     ERR("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
305     return FALSE;
306   }
307   /* ok, now we're pretty sure we can set the desired gamma ramp,
308    * so go for it */
309   *gamma = 1/g_avg;
310   return TRUE;
311 }
312
313 #endif /* X_XF86VidModeSetGamma */
314
315 /* Hmm... should gamma control be available in desktop mode or not?
316  * I'll assume that it should */
317
318 BOOL X11DRV_XF86VM_GetGammaRamp(LPDDGAMMARAMP ramp)
319 {
320 #ifdef X_XF86VidModeSetGamma
321   XF86VidModeGamma gamma;
322   Bool ret;
323
324   if (xf86vm_major < 2) return FALSE; /* no gamma control */
325 #ifdef X_XF86VidModeSetGammaRamp
326   else if (xf86vm_use_gammaramp)
327   {
328       Bool ret;
329       wine_tsx11_lock();
330       ret = XF86VidModeGetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
331                                     ramp->red, ramp->green, ramp->blue);
332       wine_tsx11_unlock();
333       return ret;
334   }
335 #endif
336   else
337   {
338       wine_tsx11_lock();
339       ret = XF86VidModeGetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
340       wine_tsx11_unlock();
341       if (ret) {
342           GenerateRampFromGamma(ramp->red,   gamma.red);
343           GenerateRampFromGamma(ramp->green, gamma.green);
344           GenerateRampFromGamma(ramp->blue,  gamma.blue);
345           return TRUE;
346       }
347   }
348 #endif /* X_XF86VidModeSetGamma */
349   return FALSE;
350 }
351
352 BOOL X11DRV_XF86VM_SetGammaRamp(LPDDGAMMARAMP ramp)
353 {
354 #ifdef X_XF86VidModeSetGamma
355   XF86VidModeGamma gamma;
356
357   if (xf86vm_major < 2) return FALSE; /* no gamma control */
358 #ifdef X_XF86VidModeSetGammaRamp
359   else if (xf86vm_use_gammaramp)
360   {
361       Bool ret;
362       wine_tsx11_lock();
363       ret = XF86VidModeSetGammaRamp(gdi_display, DefaultScreen(gdi_display), 256,
364                                     ramp->red, ramp->green, ramp->blue);
365       wine_tsx11_unlock();
366       return ret;
367   }
368 #endif
369   else
370   {
371       if (ComputeGammaFromRamp(ramp->red,   &gamma.red) &&
372           ComputeGammaFromRamp(ramp->green, &gamma.green) &&
373           ComputeGammaFromRamp(ramp->blue,  &gamma.blue)) {
374           Bool ret;
375           wine_tsx11_lock();
376           ret = XF86VidModeSetGamma(gdi_display, DefaultScreen(gdi_display), &gamma);
377           wine_tsx11_unlock();
378           return ret;
379       }
380   }
381 #endif /* X_XF86VidModeSetGamma */
382   return FALSE;
383 }
384
385 #endif /* HAVE_LIBXXF86VM */
386
387 /***********************************************************************
388  *              GetDeviceGammaRamp (X11DRV.@)
389  *
390  * FIXME: should move to somewhere appropriate, but probably not before
391  * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
392  * they can include xvidmode.h directly
393  */
394 BOOL X11DRV_GetDeviceGammaRamp(X11DRV_PDEVICE *physDev, LPVOID ramp)
395 {
396 #ifdef HAVE_LIBXXF86VM
397   return X11DRV_XF86VM_GetGammaRamp(ramp);
398 #else
399   return FALSE;
400 #endif
401 }
402
403 /***********************************************************************
404  *              SetDeviceGammaRamp (X11DRV.@)
405  *
406  * FIXME: should move to somewhere appropriate, but probably not before
407  * the stuff in graphics/x11drv/ has been moved to dlls/x11drv, so that
408  * they can include xvidmode.h directly
409  */
410 BOOL X11DRV_SetDeviceGammaRamp(X11DRV_PDEVICE *physDev, LPVOID ramp)
411 {
412 #ifdef HAVE_LIBXXF86VM
413   return X11DRV_XF86VM_SetGammaRamp(ramp);
414 #else
415   return FALSE;
416 #endif
417 }
418
419 /***********************************************************************
420  *              EnumDisplaySettingsExW  (X11DRV.@)
421  */
422 BOOL X11DRV_EnumDisplaySettingsExW( LPCWSTR name, DWORD n, LPDEVMODEW devmode, DWORD flags)
423 {
424     DWORD dwBpp = screen_depth;
425     if (dwBpp == 24) dwBpp = 32;
426     devmode->dmDisplayFlags = 0;
427     devmode->dmDisplayFrequency = 85;
428     devmode->dmSize = sizeof(DEVMODEW);
429     if (n==0 || n == (DWORD)-1 || n == (DWORD)-2)
430     {
431         devmode->dmBitsPerPel = dwBpp;
432         devmode->dmPelsHeight = GetSystemMetrics(SM_CYSCREEN);
433         devmode->dmPelsWidth  = GetSystemMetrics(SM_CXSCREEN);
434         devmode->dmFields = (DM_PELSWIDTH|DM_PELSHEIGHT|DM_BITSPERPEL);
435         TRACE("mode %ld -- returning default %ldx%ldx%ldbpp\n", n,
436               devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel);
437         return TRUE;
438     }
439 #ifdef HAVE_LIBXXF86VM
440     if (n <= xf86vm_mode_count)
441     {
442         XF86VidModeModeInfo *mode;
443         mode = modes[n-1];
444         devmode->dmPelsWidth = mode->hdisplay;
445         devmode->dmPelsHeight = mode->vdisplay;
446         devmode->dmBitsPerPel = dwBpp;
447         devmode->dmDisplayFrequency = mode->dotclock * 1000 / (mode->htotal * mode->vtotal);
448         devmode->dmFields = (DM_PELSWIDTH|DM_PELSHEIGHT|DM_BITSPERPEL|DM_DISPLAYFREQUENCY);
449         TRACE("mode %ld -- %ldx%ldx%ldbpp\n", n,
450               devmode->dmPelsWidth, devmode->dmPelsHeight, devmode->dmBitsPerPel);
451         return TRUE;
452     }
453 #endif
454     TRACE("mode %ld -- not present\n", n);
455     return FALSE;
456 }
457
458
459
460 #define _X_FIELD(prefix, bits) if ((fields) & prefix##_##bits) {p+=sprintf(p, "%s%s", first ? "" : ",", #bits); first=FALSE;}
461 static const char * _CDS_flags(DWORD fields)
462 {
463     BOOL first = TRUE;
464     char buf[128];
465     char *p = buf;
466     _X_FIELD(CDS,UPDATEREGISTRY);_X_FIELD(CDS,TEST);_X_FIELD(CDS,FULLSCREEN);
467     _X_FIELD(CDS,GLOBAL);_X_FIELD(CDS,SET_PRIMARY);_X_FIELD(CDS,RESET);
468     _X_FIELD(CDS,SETRECT);_X_FIELD(CDS,NORESET);
469     return wine_dbg_sprintf("%s", buf);
470 }
471 static const char * _DM_fields(DWORD fields)
472 {
473     BOOL first = TRUE;
474     char buf[128];
475     char *p = buf;
476     _X_FIELD(DM,BITSPERPEL);_X_FIELD(DM,PELSWIDTH);_X_FIELD(DM,PELSHEIGHT);
477     _X_FIELD(DM,DISPLAYFLAGS);_X_FIELD(DM,DISPLAYFREQUENCY);_X_FIELD(DM,POSITION);
478     return wine_dbg_sprintf("%s", buf);
479 }
480 #undef _X_FIELD
481
482 /***********************************************************************
483  *              ChangeDisplaySettingsExW  (X11DRV.@)
484  */
485 LONG X11DRV_ChangeDisplaySettingsExW( LPCWSTR devname, LPDEVMODEW devmode,
486                                       HWND hwnd, DWORD flags, LPVOID lpvoid )
487 {
488     DWORD i;
489     DWORD dwBpp = screen_depth;
490     if (dwBpp == 24) dwBpp = 32;
491     TRACE("(%s,%p,%p,0x%08lx,%p\n",debugstr_w(devname),devmode,hwnd,flags,lpvoid);
492     TRACE("flags=%s\n",_CDS_flags(flags));
493     if (devmode==NULL)
494     {
495         TRACE("Return to original display mode\n");
496 #ifdef HAVE_LIBXXF86VM
497         X11DRV_XF86VM_SetCurrentMode(xf86vm_initial_mode);
498 #endif
499         return DISP_CHANGE_SUCCESSFUL;
500     }
501
502     if (TRACE_ON(x11drv))
503     {
504         TRACE("DM_fields=%s\n",_DM_fields(devmode->dmFields));
505         TRACE("width=%ld height=%ld bpp=%ld freq=%ld\n",
506               devmode->dmPelsWidth,devmode->dmPelsHeight,
507               devmode->dmBitsPerPel,devmode->dmDisplayFrequency);
508     }
509     if ((!(devmode->dmFields & DM_BITSPERPEL) || devmode->dmBitsPerPel == dwBpp) &&
510         (!(devmode->dmFields & DM_PELSWIDTH)  || devmode->dmPelsWidth  == GetSystemMetrics(SM_CXSCREEN)) &&
511         (!(devmode->dmFields & DM_PELSHEIGHT) || devmode->dmPelsHeight == GetSystemMetrics(SM_CYSCREEN)))
512     {
513         /* we have a valid mode */
514         TRACE("Requested mode matches current mode -- no change!\n");
515         return DISP_CHANGE_SUCCESSFUL;
516     }
517
518 #ifdef HAVE_LIBXXF86VM
519     for (i = 0; i < xf86vm_mode_count; i++)
520     {
521         XF86VidModeModeInfo *mode = modes[i];
522         if (devmode->dmFields & DM_BITSPERPEL)
523         {
524             if (devmode->dmBitsPerPel != dwBpp)
525                 continue;
526         }
527         if (devmode->dmFields & DM_PELSWIDTH)
528         {
529             if (devmode->dmPelsWidth != mode->hdisplay)
530                 continue;
531         }
532         if (devmode->dmFields & DM_PELSHEIGHT)
533         {
534             if (devmode->dmPelsHeight != mode->vdisplay)
535                 continue;
536         }
537         /* we have a valid mode */
538         TRACE("Matches mode %ld\n", i);
539         X11DRV_XF86VM_SetCurrentMode(i-1);
540 #if 0 /* FIXME */
541         SYSMETRICS_Set( SM_CXSCREEN, devmode->dmPelsWidth );
542         SYSMETRICS_Set( SM_CYSCREEN, devmode->dmPelsHeight );
543 #endif
544         return DISP_CHANGE_SUCCESSFUL;
545     }
546 #endif
547
548     /* no valid modes found */
549     ERR("No matching mode found!\n");
550     return DISP_CHANGE_BADMODE;
551 }