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