2 * DirectDraw driver interface
4 * Copyright 2001 TransGaming Technologies, Inc.
21 #include "debugtools.h"
23 DEFAULT_DEBUG_CHANNEL(x11drv);
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;
37 static void SetPrimaryDIB(HBITMAP hBmp)
39 X11DRV_DD_PrimaryDIB = hBmp;
42 bmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
43 X11DRV_DD_PrimaryDrawable = (Pixmap)bmp->physBitmap;
44 GDI_ReleaseObj( hBmp );
46 X11DRV_DD_PrimaryDrawable = 0;
50 static LRESULT WINAPI GrabWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
52 Display *display = thread_display();
54 if(message != X11DRV_DD_GrabMessage)
55 return CallWindowProcA(X11DRV_DD_GrabOldProcedure, hWnd, message, wParam, lParam);
57 TRACE("hwnd=%d, grab=%d\n", hWnd, wParam);
61 /* find the X11 window that ddraw uses */
62 Window win = X11DRV_get_whole_window(hWnd);
63 TRACE("X11 window: %ld\n", win);
65 TRACE("host off desktop\n");
69 TSXGrabPointer(display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
73 TSXUngrabPointer(display, CurrentTime);
79 static void GrabPointer(BOOL grab)
82 Window window = X11DRV_get_whole_window(GetFocus());
84 XSetInputFocus(thread_display(), window, RevertToParent, CurrentTime);
87 if(!X11DRV_DD_GrabMessage)
88 X11DRV_DD_GrabMessage = RegisterWindowMessageA("WINE_X11DRV_GRABPOINTER");
90 /* FIXME: Replace with SetWindowLongPtrA when available */
91 X11DRV_DD_GrabOldProcedure = (WNDPROC)SetWindowLongA(X11DRV_DD_PrimaryWnd,
92 GWL_WNDPROC, (LONG)GrabWndProc);
94 SendMessageA(X11DRV_DD_PrimaryWnd, X11DRV_DD_GrabMessage, grab ? 1 : 0, 0);
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");
102 static DWORD PASCAL X11DRV_DDHAL_DestroyDriver(LPDDHAL_DESTROYDRIVERDATA data)
104 data->ddRVal = DD_OK;
105 return DDHAL_DRIVER_HANDLED;
108 static DWORD PASCAL X11DRV_DDHAL_CreateSurface(LPDDHAL_CREATESURFACEDATA data)
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);
118 data->ddRVal = DD_OK;
119 return DDHAL_DRIVER_NOTHANDLED;
122 static DWORD PASCAL X11DRV_DDHAL_CreatePalette(LPDDHAL_CREATEPALETTEDATA data)
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;
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 */
138 NULL, /* WaitForVerticalBlank */
139 NULL, /* CanCreateSurface */
140 X11DRV_DDHAL_CreatePalette,
141 NULL, /* GetScanLine */
142 NULL, /* SetExclusiveMode */
143 NULL /* FlipToGDISurface */
146 static DWORD PASCAL X11DRV_DDHAL_DestroySurface(LPDDHAL_DESTROYSURFACEDATA data)
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;
154 X11DRV_DD_UserClass = 0;
156 data->ddRVal = DD_OK;
157 return DDHAL_DRIVER_HANDLED;
160 static DWORD PASCAL X11DRV_DDHAL_SetPalette(LPDDHAL_SETPALETTEDATA data)
162 if (data->lpDDPalette && data->lpDDPalette->u1.dwReserved1) {
163 if (data->lpDDSurface == X11DRV_DD_Primary) {
165 /* we should probably find the ddraw window (maybe data->lpDD->lpExclusiveOwner->hWnd),
166 * and attach the palette to it
168 * Colormap pal = data->lpDDPalette->u1.dwReserved1;
172 data->ddRVal = DD_OK;
173 return DDHAL_DRIVER_HANDLED;
176 static DDHAL_DDSURFACECALLBACKS hal_ddsurfcallbacks = {
177 sizeof(DDHAL_DDSURFACECALLBACKS),
178 0x3fff, /* all callbacks are 32-bit */
179 X11DRV_DDHAL_DestroySurface,
181 NULL, /* SetClipList */
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
195 static DWORD PASCAL X11DRV_DDHAL_DestroyPalette(LPDDHAL_DESTROYPALETTEDATA data)
197 Colormap pal = data->lpDDPalette->u1.dwReserved1;
198 if (pal) TSXFreeColormap(gdi_display, pal);
199 data->ddRVal = DD_OK;
200 return DDHAL_DRIVER_HANDLED;
203 static DWORD PASCAL X11DRV_DDHAL_SetPaletteEntries(LPDDHAL_SETENTRIESDATA data)
205 X11DRV_DDHAL_SetPalEntries(data->lpDDPalette->u1.dwReserved1,
206 data->dwBase, data->dwNumEntries,
208 data->ddRVal = DD_OK;
209 return DDHAL_DRIVER_HANDLED;
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
219 static X11DEVICE x11device = {
223 static DWORD PASCAL X11DRV_DDHAL_GetDriverInfo(LPDDHAL_GETDRIVERINFODATA data)
225 LPX11DRIVERINFO info = x11device.lpInfo;
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;
237 data->ddRVal = DDERR_CURRENTLYNOTAVAIL;
238 return DDHAL_DRIVER_HANDLED;
241 static DDHALINFO hal_info = {
244 &hal_ddsurfcallbacks,
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 */
256 0, /* dwFXAlphaCaps */
257 DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE, /* dwPalCaps */
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 */
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 */
283 0, /* dwMinOverlayStretch */
284 0, /* dwMaxOverlayStretch */
285 0, /* dwMinLiveVideoStretch */
286 0, /* dwMaxLiveVideoStretch */
287 0, /* dwMinHwCodecStretch */
288 0, /* dwMaxHwCodecStretch */
293 0, /* dwSVBCKeyCaps */
297 0, /* dwVSBCKeyCaps */
301 0, /* dwSSBCKeyCaps */
304 0, /* dwMaxVideoPorts */
305 0, /* dwCurrVideoPorts */
308 0, /* dwMonitorFrequency */
309 X11DRV_DDHAL_GetDriverInfo,
311 NULL, /* lpdwFourCC */
313 NULL, /* lpModeInfo */
314 DDHALINFO_ISPRIMARYDISPLAY | DDHALINFO_MODEXILLEGAL | DDHALINFO_GETDRIVERINFOSET, /* dwFlags */
317 0, /* lpD3DGlobalDriverData */
318 0, /* lpD3DHALCallbacks */
319 NULL /* lpDDExeBufCallbacks */
322 static LPDDHALDDRAWFNS ddraw_fns;
323 static DWORD ddraw_ver;
325 static void X11DRV_DDHAL_SetInfo(void)
327 (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
330 INT X11DRV_DCICommand(INT cbInput, const DCICMD *lpCmd, LPVOID lpOutData)
332 TRACE("(%d,(%ld,%ld,%ld),%p)\n", cbInput, lpCmd->dwCommand,
333 lpCmd->dwParam1, lpCmd->dwParam2, lpOutData);
335 switch (lpCmd->dwCommand) {
336 case DDNEWCALLBACKFNS:
337 ddraw_fns = (LPDDHALDDRAWFNS)lpCmd->dwParam1;
341 LPDDVERSIONDATA lpVer = (LPDDVERSIONDATA)lpOutData;
342 ddraw_ver = lpCmd->dwParam1;
344 /* well, whatever... the DDK says so */
345 lpVer->dwHALVersion = DD_RUNTIME_VERSION;
348 case DDGET32BITDRIVERNAME:
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;
360 case DDCREATEDRIVEROBJECT:
362 LPDWORD lpInstance = (LPDWORD)lpOutData;
364 /* FIXME: get x11drv's hInstance */
365 #ifdef HAVE_LIBXXF86DGA2
366 if (!X11DRV_XF86DGA2_CreateDriver(&hal_info))
369 #ifdef HAVE_LIBXXF86VM
370 X11DRV_XF86VM_CreateDriver(&hal_info);
374 /*X11DRV_GLX_CreateDriver(&hal_info);*/
377 (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
378 *lpInstance = hal_info.hInstance;
385 void X11DRV_DDHAL_SwitchMode(DWORD dwModeIndex, LPVOID fb_addr, LPVIDMEM fb_mem)
387 LPDDHALMODEINFO info = &hal_info.lpModeInfo[dwModeIndex];
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;
404 X11DRV_DDHAL_SetInfo();
407 void X11DRV_DDHAL_SetPalEntries(Colormap pal, DWORD dwBase, DWORD dwNumEntries,
408 LPPALETTEENTRY lpEntries)
414 c.flags = DoRed|DoGreen|DoBlue;
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);
422 TSXFlush(gdi_display); /* update display immediately */