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