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