2 * DirectDraw driver interface
4 * Copyright 2001 TransGaming Technologies, Inc.
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.
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.
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
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
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;
50 static void SetPrimaryDIB(HBITMAP hBmp)
52 X11DRV_DD_PrimaryDIB = hBmp;
55 bmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
56 X11DRV_DD_PrimaryDrawable = (Pixmap)bmp->physBitmap;
57 GDI_ReleaseObj( hBmp );
59 X11DRV_DD_PrimaryDrawable = 0;
63 static LRESULT WINAPI GrabWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
65 Display *display = thread_display();
67 if(message != X11DRV_DD_GrabMessage)
68 return CallWindowProcA(X11DRV_DD_GrabOldProcedure, hWnd, message, wParam, lParam);
70 TRACE("hwnd=%p, grab=%d\n", hWnd, wParam);
74 /* find the X11 window that ddraw uses */
75 Window win = X11DRV_get_whole_window(hWnd);
76 TRACE("X11 window: %ld\n", win);
78 TRACE("host off desktop\n");
83 XGrabPointer(display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
89 XUngrabPointer(display, CurrentTime);
96 static void GrabPointer(BOOL grab)
99 Window window = X11DRV_get_whole_window(GetFocus());
101 XSetInputFocus(thread_display(), window, RevertToParent, CurrentTime);
104 if(!X11DRV_DD_GrabMessage)
105 X11DRV_DD_GrabMessage = RegisterWindowMessageA("WINE_X11DRV_GRABPOINTER");
107 X11DRV_DD_GrabOldProcedure = (WNDPROC)SetWindowLongPtrA(X11DRV_DD_PrimaryWnd,
108 GWLP_WNDPROC, (LONG_PTR)GrabWndProc);
110 SendMessageA(X11DRV_DD_PrimaryWnd, X11DRV_DD_GrabMessage, grab ? 1 : 0, 0);
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");
117 static DWORD PASCAL X11DRV_DDHAL_DestroyDriver(LPDDHAL_DESTROYDRIVERDATA data)
119 data->ddRVal = DD_OK;
120 return DDHAL_DRIVER_HANDLED;
123 static DWORD PASCAL X11DRV_DDHAL_CreateSurface(LPDDHAL_CREATESURFACEDATA data)
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);
133 data->ddRVal = DD_OK;
134 return DDHAL_DRIVER_NOTHANDLED;
137 static DWORD PASCAL X11DRV_DDHAL_CreatePalette(LPDDHAL_CREATEPALETTEDATA data)
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;
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 */
153 NULL, /* WaitForVerticalBlank */
154 NULL, /* CanCreateSurface */
155 X11DRV_DDHAL_CreatePalette,
156 NULL, /* GetScanLine */
157 NULL, /* SetExclusiveMode */
158 NULL /* FlipToGDISurface */
161 static DWORD PASCAL X11DRV_DDHAL_DestroySurface(LPDDHAL_DESTROYSURFACEDATA data)
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;
169 X11DRV_DD_UserClass = 0;
171 data->ddRVal = DD_OK;
172 return DDHAL_DRIVER_HANDLED;
175 static DWORD PASCAL X11DRV_DDHAL_SetPalette(LPDDHAL_SETPALETTEDATA data)
177 if (data->lpDDPalette && data->lpDDPalette->u1.dwReserved1) {
178 if (data->lpDDSurface == X11DRV_DD_Primary) {
180 /* we should probably find the ddraw window (maybe data->lpDD->lpExclusiveOwner->hWnd),
181 * and attach the palette to it
183 * Colormap pal = data->lpDDPalette->u1.dwReserved1;
187 data->ddRVal = DD_OK;
188 return DDHAL_DRIVER_HANDLED;
191 static DDHAL_DDSURFACECALLBACKS hal_ddsurfcallbacks = {
192 sizeof(DDHAL_DDSURFACECALLBACKS),
193 0x3fff, /* all callbacks are 32-bit */
194 X11DRV_DDHAL_DestroySurface,
196 NULL, /* SetClipList */
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
210 static DWORD PASCAL X11DRV_DDHAL_DestroyPalette(LPDDHAL_DESTROYPALETTEDATA data)
212 Colormap pal = data->lpDDPalette->u1.dwReserved1;
216 XFreeColormap(gdi_display, pal);
219 data->ddRVal = DD_OK;
220 return DDHAL_DRIVER_HANDLED;
223 static DWORD PASCAL X11DRV_DDHAL_SetPaletteEntries(LPDDHAL_SETENTRIESDATA data)
225 X11DRV_DDHAL_SetPalEntries(data->lpDDPalette->u1.dwReserved1,
226 data->dwBase, data->dwNumEntries,
228 data->ddRVal = DD_OK;
229 return DDHAL_DRIVER_HANDLED;
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
239 static X11DEVICE x11device = {
243 static DWORD PASCAL X11DRV_DDHAL_GetDriverInfo(LPDDHAL_GETDRIVERINFODATA data)
245 LPX11DRIVERINFO info = x11device.lpInfo;
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;
257 data->ddRVal = DDERR_CURRENTLYNOTAVAIL;
258 return DDHAL_DRIVER_HANDLED;
261 static DDHALINFO hal_info = {
264 &hal_ddsurfcallbacks,
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 */
276 0, /* dwFXAlphaCaps */
277 DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE, /* dwPalCaps */
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 */
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 */
303 0, /* dwMinOverlayStretch */
304 0, /* dwMaxOverlayStretch */
305 0, /* dwMinLiveVideoStretch */
306 0, /* dwMaxLiveVideoStretch */
307 0, /* dwMinHwCodecStretch */
308 0, /* dwMaxHwCodecStretch */
313 0, /* dwSVBCKeyCaps */
317 0, /* dwVSBCKeyCaps */
321 0, /* dwSSBCKeyCaps */
324 0, /* dwMaxVideoPorts */
325 0, /* dwCurrVideoPorts */
328 0, /* dwMonitorFrequency */
329 X11DRV_DDHAL_GetDriverInfo,
331 NULL, /* lpdwFourCC */
333 NULL, /* lpModeInfo */
334 DDHALINFO_ISPRIMARYDISPLAY | DDHALINFO_MODEXILLEGAL | DDHALINFO_GETDRIVERINFOSET, /* dwFlags */
337 0, /* lpD3DGlobalDriverData */
338 0, /* lpD3DHALCallbacks */
339 NULL /* lpDDExeBufCallbacks */
342 static LPDDHALDDRAWFNS ddraw_fns;
343 static DWORD ddraw_ver;
345 static void X11DRV_DDHAL_SetInfo(void)
347 (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
350 INT X11DRV_DCICommand(INT cbInput, const DCICMD *lpCmd, LPVOID lpOutData)
352 TRACE("(%d,(%ld,%ld,%ld),%p)\n", cbInput, lpCmd->dwCommand,
353 lpCmd->dwParam1, lpCmd->dwParam2, lpOutData);
355 switch (lpCmd->dwCommand) {
356 case DDNEWCALLBACKFNS:
357 ddraw_fns = (LPDDHALDDRAWFNS)lpCmd->dwParam1;
361 LPDDVERSIONDATA lpVer = (LPDDVERSIONDATA)lpOutData;
362 ddraw_ver = lpCmd->dwParam1;
364 /* well, whatever... the DDK says so */
365 lpVer->dwHALVersion = DD_RUNTIME_VERSION;
368 case DDGET32BITDRIVERNAME:
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;
380 case DDCREATEDRIVEROBJECT:
382 LPDWORD lpInstance = (LPDWORD)lpOutData;
384 /* FIXME: get x11drv's hInstance */
385 #ifdef HAVE_LIBXXF86DGA2
386 if (!X11DRV_XF86DGA2_CreateDriver(&hal_info))
389 X11DRV_Settings_CreateDriver(&hal_info);
392 /*X11DRV_GLX_CreateDriver(&hal_info);*/
395 (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
396 *lpInstance = hal_info.hInstance;
403 void X11DRV_DDHAL_SwitchMode(DWORD dwModeIndex, LPVOID fb_addr, LPVIDMEM fb_mem)
405 LPDDHALMODEINFO info = &hal_info.lpModeInfo[dwModeIndex];
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;
422 X11DRV_DDHAL_SetInfo();
425 void X11DRV_DDHAL_SetPalEntries(Colormap pal, DWORD dwBase, DWORD dwNumEntries,
426 LPPALETTEENTRY lpEntries)
433 c.flags = DoRed|DoGreen|DoBlue;
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);
441 XFlush(gdi_display); /* update display immediately */