Added SystemParametersInfo unit test.
[wine] / dlls / x11drv / x11ddraw.c
1 /*
2  * DirectDraw driver interface
3  *
4  * Copyright 2001 TransGaming Technologies, Inc.
5  */
6
7 #include "config.h"
8
9 #include <string.h>
10
11 #include "ts_xlib.h"
12 #include "x11drv.h"
13 #include "x11ddraw.h"
14 #include "xvidmode.h"
15 #include "dga2.h"
16
17 #include "windef.h"
18 #include "wingdi.h"
19 #include "ddrawi.h"
20 #include "bitmap.h"
21 #include "debugtools.h"
22
23 DEFAULT_DEBUG_CHANNEL(x11drv);
24
25 extern int dxgrab;
26
27 LPDDRAWI_DDRAWSURFACE_LCL X11DRV_DD_Primary;
28 LPDDRAWI_DDRAWSURFACE_GBL X11DRV_DD_PrimaryGbl;
29 HWND X11DRV_DD_PrimaryWnd;
30 HBITMAP X11DRV_DD_PrimaryDIB;
31 Drawable X11DRV_DD_PrimaryDrawable;
32 ATOM X11DRV_DD_UserClass;
33 BOOL X11DRV_DD_IsDirect;
34 static UINT X11DRV_DD_GrabMessage;
35 static WNDPROC X11DRV_DD_GrabOldProcedure;
36
37 static void SetPrimaryDIB(HBITMAP hBmp)
38 {
39   X11DRV_DD_PrimaryDIB = hBmp;
40   if (hBmp) {
41     BITMAPOBJ *bmp;
42     bmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
43     X11DRV_DD_PrimaryDrawable = (Pixmap)bmp->physBitmap;
44     GDI_ReleaseObj( hBmp );
45   } else {
46     X11DRV_DD_PrimaryDrawable = 0;
47   }
48 }
49
50 static LRESULT WINAPI GrabWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
51
52   Display *display = thread_display();
53
54   if(message != X11DRV_DD_GrabMessage)
55     return CallWindowProcA(X11DRV_DD_GrabOldProcedure, hWnd, message, wParam, lParam);
56  
57   TRACE("hwnd=%d, grab=%d\n", hWnd, wParam);
58
59   if (wParam) 
60   {  
61     /* find the X11 window that ddraw uses */  
62     Window win = X11DRV_get_whole_window(hWnd);  
63     TRACE("X11 window: %ld\n", win);  
64     if (!win) {  
65       TRACE("host off desktop\n");  
66       win = root_window;  
67     }  
68
69     TSXGrabPointer(display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);  
70   }  
71   else 
72   {
73     TSXUngrabPointer(display, CurrentTime);  
74   } 
75
76   return 0; 
77
78
79 static void GrabPointer(BOOL grab)
80 {
81   if(grab) { 
82     Window window = X11DRV_get_whole_window(GetFocus()); 
83     if(window)
84       XSetInputFocus(thread_display(), window, RevertToParent, CurrentTime); 
85   } 
86
87   if(!X11DRV_DD_GrabMessage)
88     X11DRV_DD_GrabMessage = RegisterWindowMessageA("WINE_X11DRV_GRABPOINTER");
89
90   /* FIXME: Replace with SetWindowLongPtrA when available */
91   X11DRV_DD_GrabOldProcedure = (WNDPROC)SetWindowLongA(X11DRV_DD_PrimaryWnd,
92                                                        GWL_WNDPROC, (LONG)GrabWndProc);
93
94   SendMessageA(X11DRV_DD_PrimaryWnd, X11DRV_DD_GrabMessage, grab ? 1 : 0, 0);
95
96   /* FIXME: Replace with SetWindowLongPtrA when available */
97   if(SetWindowLongA(X11DRV_DD_PrimaryWnd, GWL_WNDPROC,
98                     (LONG)X11DRV_DD_GrabOldProcedure) != (LONG)GrabWndProc)
99     ERR("Window procedure has been changed!\n");
100 }
101
102 static DWORD PASCAL X11DRV_DDHAL_DestroyDriver(LPDDHAL_DESTROYDRIVERDATA data)
103 {
104   data->ddRVal = DD_OK;
105   return DDHAL_DRIVER_HANDLED;
106 }
107
108 static DWORD PASCAL X11DRV_DDHAL_CreateSurface(LPDDHAL_CREATESURFACEDATA data)
109 {
110   if (data->lpDDSurfaceDesc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
111     X11DRV_DD_Primary = *data->lplpSList;
112     X11DRV_DD_PrimaryWnd = (HWND)X11DRV_DD_Primary->lpSurfMore->lpDDRAWReserved;
113     X11DRV_DD_PrimaryGbl = X11DRV_DD_Primary->lpGbl;
114     SetPrimaryDIB(GET_LPDDRAWSURFACE_GBL_MORE(X11DRV_DD_PrimaryGbl)->hKernelSurface);
115     X11DRV_DD_UserClass = GlobalFindAtomA("WINE_DDRAW");
116     if (dxgrab) GrabPointer(TRUE);
117   }
118   data->ddRVal = DD_OK;
119   return DDHAL_DRIVER_NOTHANDLED;
120 }
121
122 static DWORD PASCAL X11DRV_DDHAL_CreatePalette(LPDDHAL_CREATEPALETTEDATA data)
123 {
124   FIXME("stub\n");
125   /* only makes sense to do anything if the X server is running at 8bpp,
126    * which few people do nowadays */
127   data->ddRVal = DD_OK;
128   return DDHAL_DRIVER_HANDLED;
129 }
130
131 static DDHAL_DDCALLBACKS hal_ddcallbacks = {
132   sizeof(DDHAL_DDCALLBACKS),
133   0x3ff, /* all callbacks are 32-bit */
134   X11DRV_DDHAL_DestroyDriver,
135   X11DRV_DDHAL_CreateSurface,
136   NULL, /* SetColorKey */
137   NULL, /* SetMode */
138   NULL, /* WaitForVerticalBlank */
139   NULL, /* CanCreateSurface */
140   X11DRV_DDHAL_CreatePalette,
141   NULL, /* GetScanLine */
142   NULL, /* SetExclusiveMode */
143   NULL  /* FlipToGDISurface */
144 };
145
146 static DWORD PASCAL X11DRV_DDHAL_DestroySurface(LPDDHAL_DESTROYSURFACEDATA data)
147 {
148   if (data->lpDDSurface == X11DRV_DD_Primary) {
149     if (dxgrab) GrabPointer(FALSE);
150     X11DRV_DD_Primary = NULL;
151     X11DRV_DD_PrimaryWnd = 0;
152     X11DRV_DD_PrimaryGbl = NULL;
153     SetPrimaryDIB(0);
154     X11DRV_DD_UserClass = 0;
155   }
156   data->ddRVal = DD_OK;
157   return DDHAL_DRIVER_HANDLED;
158 }
159
160 static DWORD PASCAL X11DRV_DDHAL_SetPalette(LPDDHAL_SETPALETTEDATA data)
161 {
162   if (data->lpDDPalette && data->lpDDPalette->u1.dwReserved1) {
163     if (data->lpDDSurface == X11DRV_DD_Primary) {
164       FIXME("stub\n");
165       /* we should probably find the ddraw window (maybe data->lpDD->lpExclusiveOwner->hWnd),
166        * and attach the palette to it
167        *
168        * Colormap pal = data->lpDDPalette->u1.dwReserved1;
169        */
170     }
171   }
172   data->ddRVal = DD_OK;
173   return DDHAL_DRIVER_HANDLED;
174 }
175
176 static DDHAL_DDSURFACECALLBACKS hal_ddsurfcallbacks = {
177   sizeof(DDHAL_DDSURFACECALLBACKS),
178   0x3fff, /* all callbacks are 32-bit */
179   X11DRV_DDHAL_DestroySurface,
180   NULL, /* Flip */
181   NULL, /* SetClipList */
182   NULL, /* Lock */
183   NULL, /* Unlock */
184   NULL, /* Blt */
185   NULL, /* SetColorKey */
186   NULL, /* AddAttachedSurface */
187   NULL, /* GetBltStatus */
188   NULL, /* GetFlipStatus */
189   NULL, /* UpdateOverlay */
190   NULL, /* SetOverlayPosition */
191   NULL, /* reserved4 */
192   X11DRV_DDHAL_SetPalette
193 };
194
195 static DWORD PASCAL X11DRV_DDHAL_DestroyPalette(LPDDHAL_DESTROYPALETTEDATA data)
196 {
197   Colormap pal = data->lpDDPalette->u1.dwReserved1;
198   if (pal) TSXFreeColormap(gdi_display, pal);
199   data->ddRVal = DD_OK;
200   return DDHAL_DRIVER_HANDLED;
201 }
202
203 static DWORD PASCAL X11DRV_DDHAL_SetPaletteEntries(LPDDHAL_SETENTRIESDATA data)
204 {
205   X11DRV_DDHAL_SetPalEntries(data->lpDDPalette->u1.dwReserved1,
206                              data->dwBase, data->dwNumEntries,
207                              data->lpEntries);
208   data->ddRVal = DD_OK;
209   return DDHAL_DRIVER_HANDLED;
210 }
211
212 static DDHAL_DDPALETTECALLBACKS hal_ddpalcallbacks = {
213   sizeof(DDHAL_DDPALETTECALLBACKS),
214   0x3, /* all callbacks are 32-bit */
215   X11DRV_DDHAL_DestroyPalette,
216   X11DRV_DDHAL_SetPaletteEntries
217 };
218
219 static X11DEVICE x11device = {
220   NULL
221 };
222
223 static DWORD PASCAL X11DRV_DDHAL_GetDriverInfo(LPDDHAL_GETDRIVERINFODATA data)
224 {
225   LPX11DRIVERINFO info = x11device.lpInfo;
226   while (info) {
227     if (IsEqualGUID(&data->guidInfo, info->lpGuid)) {
228       DWORD dwSize = info->dwSize;
229       data->dwActualSize = dwSize;
230       if (data->dwExpectedSize < dwSize) dwSize = data->dwExpectedSize;
231       memcpy(data->lpvData, info->lpvData, dwSize);
232       data->ddRVal = DD_OK;
233       return DDHAL_DRIVER_HANDLED;
234     }
235     info = info->lpNext;
236   }
237   data->ddRVal = DDERR_CURRENTLYNOTAVAIL;
238   return DDHAL_DRIVER_HANDLED;
239 }
240
241 static DDHALINFO hal_info = {
242   sizeof(DDHALINFO),
243   &hal_ddcallbacks,
244   &hal_ddsurfcallbacks,
245   &hal_ddpalcallbacks,
246   {     /* vmiData */
247    0     /* fpPrimary */
248   },
249   {     /* ddCaps (only stuff the HAL implements here) */
250    sizeof(DDCORECAPS),                                                  /* dwSize */
251    DDCAPS_GDI | DDCAPS_PALETTE,                                         /* dwCaps */
252    DDCAPS2_CERTIFIED | DDCAPS2_NONLOCALVIDMEM | DDCAPS2_NOPAGELOCKREQUIRED |
253    DDCAPS2_WIDESURFACES | DDCAPS2_PRIMARYGAMMA | DDCAPS2_FLIPNOVSYNC,   /* dwCaps2 */
254    0,                                                                   /* dwCKeyCaps */
255    0,                                                                   /* dwFXCaps */
256    0,                                                                   /* dwFXAlphaCaps */
257    DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE,                               /* dwPalCaps */
258    0,                                                                   /* dwSVCaps */
259    0,                                                                   /* dwAlphaBltConstBitDepths */
260    0,                                                                   /* dwAlphaBltPixelBitDepths */
261    0,                                                                   /* dwAlphaBltSurfaceBitDepths */
262    0,                                                                   /* dwAlphaOverlayBltConstBitDepths */
263    0,                                                                   /* dwAlphaOverlayBltPixelBitDepths */
264    0,                                                                   /* dwAlphaOverlayBltSurfaceBitDepths */
265    0,                                                                   /* dwZBufferBitDepths */
266    16*1024*1024,                                                        /* dwVidMemTotal */
267    16*1024*1024,                                                        /* dwVidMemFree */
268    0,                                                                   /* dwMaxVisibleOverlays */
269    0,                                                                   /* dwCurrVisibleOverlays */
270    0,                                                                   /* dwNumFourCCCodes */
271    0,                                                                   /* dwAlignBoundarySrc */
272    0,                                                                   /* dwAlignSizeSrc */
273    0,                                                                   /* dwAlignBoundaryDest */
274    0,                                                                   /* dwAlignSizeDest */
275    0,                                                                   /* dwAlignStrideAlign */
276    {0},                                                                 /* dwRops */
277    {                                                                    /* ddsCaps */
278     DDSCAPS_BACKBUFFER | DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER |
279     DDSCAPS_OFFSCREENPLAIN | DDSCAPS_PALETTE | DDSCAPS_PRIMARYSURFACE |
280     DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY |
281     DDSCAPS_VISIBLE | DDSCAPS_LOCALVIDMEM | DDSCAPS_NONLOCALVIDMEM      /* dwCaps */
282    },
283    0,                                                                   /* dwMinOverlayStretch */
284    0,                                                                   /* dwMaxOverlayStretch */
285    0,                                                                   /* dwMinLiveVideoStretch */
286    0,                                                                   /* dwMaxLiveVideoStretch */
287    0,                                                                   /* dwMinHwCodecStretch */
288    0,                                                                   /* dwMaxHwCodecStretch */
289    0,                                                                   /* dwReserved1 */
290    0,                                                                   /* dwReserved2 */
291    0,                                                                   /* dwReserved2 */
292    0,                                                                   /* dwSVBCaps */
293    0,                                                                   /* dwSVBCKeyCaps */
294    0,                                                                   /* dwSVBFXCaps */
295    {0},                                                                 /* dwSVBRops */
296    0,                                                                   /* dwVSBCaps */
297    0,                                                                   /* dwVSBCKeyCaps */
298    0,                                                                   /* dwVSBFXCaps */
299    {0},                                                                 /* dwVSBRops */
300    0,                                                                   /* dwSSBCaps */
301    0,                                                                   /* dwSSBCKeyCaps */
302    0,                                                                   /* dwSSBFXCaps */
303    {0},                                                                 /* dwSSBRops */
304    0,                                                                   /* dwMaxVideoPorts */
305    0,                                                                   /* dwCurrVideoPorts */
306    0                                                                    /* dwSVBCaps */
307   },
308   0,    /* dwMonitorFrequency */
309   X11DRV_DDHAL_GetDriverInfo,
310   0,    /* dwModeIndex */
311   NULL, /* lpdwFourCC */
312   0,    /* dwNumModes */
313   NULL, /* lpModeInfo */
314   DDHALINFO_ISPRIMARYDISPLAY | DDHALINFO_MODEXILLEGAL | DDHALINFO_GETDRIVERINFOSET, /* dwFlags */
315   &x11device,
316   0,    /* hInstance */
317   0,    /* lpD3DGlobalDriverData */
318   0,    /* lpD3DHALCallbacks */
319   NULL  /* lpDDExeBufCallbacks */
320 };
321
322 static LPDDHALDDRAWFNS ddraw_fns;
323 static DWORD ddraw_ver;
324
325 static void X11DRV_DDHAL_SetInfo(void)
326 {
327   (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
328 }
329
330 INT X11DRV_DCICommand(INT cbInput, const DCICMD *lpCmd, LPVOID lpOutData)
331 {
332   TRACE("(%d,(%ld,%ld,%ld),%p)\n", cbInput, lpCmd->dwCommand,
333         lpCmd->dwParam1, lpCmd->dwParam2, lpOutData);
334
335   switch (lpCmd->dwCommand) {
336   case DDNEWCALLBACKFNS:
337     ddraw_fns = (LPDDHALDDRAWFNS)lpCmd->dwParam1;
338     return TRUE;
339   case DDVERSIONINFO:
340     {
341       LPDDVERSIONDATA lpVer = (LPDDVERSIONDATA)lpOutData;
342       ddraw_ver = lpCmd->dwParam1;
343       if (!lpVer) break;
344       /* well, whatever... the DDK says so */
345       lpVer->dwHALVersion = DD_RUNTIME_VERSION;
346     }
347     return TRUE;
348   case DDGET32BITDRIVERNAME:
349     {
350       LPDD32BITDRIVERDATA lpData = (LPDD32BITDRIVERDATA)lpOutData;
351       /* here, we could ask ddraw to load a separate DLL, that
352        * would contain the 32-bit ddraw HAL */
353       strcpy(lpData->szName,"x11drv");
354       /* the entry point named here should initialize our hal_info
355        * with 32-bit entry points (ignored for now) */
356       strcpy(lpData->szEntryPoint,"DriverInit");
357       lpData->dwContext = 0;
358     }
359     return TRUE;
360   case DDCREATEDRIVEROBJECT:
361     {
362       LPDWORD lpInstance = (LPDWORD)lpOutData;
363
364       /* FIXME: get x11drv's hInstance */
365 #ifdef HAVE_LIBXXF86DGA2
366       if (!X11DRV_XF86DGA2_CreateDriver(&hal_info))
367 #endif
368       {
369 #ifdef HAVE_LIBXXF86VM
370         X11DRV_XF86VM_CreateDriver(&hal_info);
371 #endif
372       }
373 #ifdef HAVE_OPENGL
374       /*X11DRV_GLX_CreateDriver(&hal_info);*/
375 #endif
376
377       (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
378       *lpInstance = hal_info.hInstance;
379     }
380     return TRUE;
381   }
382   return 0;
383 }
384
385 void X11DRV_DDHAL_SwitchMode(DWORD dwModeIndex, LPVOID fb_addr, LPVIDMEM fb_mem)
386 {
387   LPDDHALMODEINFO info = &hal_info.lpModeInfo[dwModeIndex];
388
389   hal_info.dwModeIndex        = dwModeIndex;
390   hal_info.dwMonitorFrequency = info->wRefreshRate;
391   hal_info.vmiData.fpPrimary  = (FLATPTR)fb_addr;
392   hal_info.vmiData.dwDisplayWidth  = info->dwWidth;
393   hal_info.vmiData.dwDisplayHeight = info->dwHeight;
394   hal_info.vmiData.lDisplayPitch   = info->lPitch;
395   hal_info.vmiData.ddpfDisplay.dwSize = info->dwBPP ? sizeof(hal_info.vmiData.ddpfDisplay) : 0;
396   hal_info.vmiData.ddpfDisplay.dwFlags = (info->wFlags & DDMODEINFO_PALETTIZED) ? DDPF_PALETTEINDEXED8 : 0;
397   hal_info.vmiData.ddpfDisplay.u1.dwRGBBitCount = (info->dwBPP > 24) ? 24 : info->dwBPP;
398   hal_info.vmiData.ddpfDisplay.u2.dwRBitMask = info->dwRBitMask;
399   hal_info.vmiData.ddpfDisplay.u3.dwGBitMask = info->dwGBitMask;
400   hal_info.vmiData.ddpfDisplay.u4.dwBBitMask = info->dwBBitMask;
401   hal_info.vmiData.dwNumHeaps = fb_mem ? 1 : 0;
402   hal_info.vmiData.pvmList = fb_mem;
403
404   X11DRV_DDHAL_SetInfo();
405 }
406
407 void X11DRV_DDHAL_SetPalEntries(Colormap pal, DWORD dwBase, DWORD dwNumEntries,
408                                 LPPALETTEENTRY lpEntries)
409 {
410   XColor c;
411   int n;
412
413   if (pal) {
414     c.flags = DoRed|DoGreen|DoBlue;
415     c.pixel = dwBase;
416     for (n=0; n<dwNumEntries; n++,c.pixel++) {
417       c.red   = lpEntries[n].peRed   << 8;
418       c.green = lpEntries[n].peGreen << 8;
419       c.blue  = lpEntries[n].peBlue  << 8;
420       TSXStoreColor(gdi_display, pal, &c);
421     }
422     TSXFlush(gdi_display); /* update display immediately */
423   }
424 }