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;
54 X11DRV_DD_PrimaryDrawable = X11DRV_get_pixmap( hBmp );
56 X11DRV_DD_PrimaryDrawable = 0;
60 static LRESULT WINAPI GrabWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
62 struct x11drv_thread_data *data = x11drv_thread_data();
64 if(message != X11DRV_DD_GrabMessage)
65 return CallWindowProcA(X11DRV_DD_GrabOldProcedure, hWnd, message, wParam, lParam);
67 TRACE("hwnd=%p, grab=%d\n", hWnd, wParam);
71 /* find the X11 window that ddraw uses */
72 Window win = X11DRV_get_whole_window(hWnd);
73 TRACE("X11 window: %ld\n", win);
75 TRACE("host off desktop\n");
80 XGrabPointer(data->display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
82 data->grab_window = win;
87 XUngrabPointer(data->display, CurrentTime);
89 data->grab_window = None;
95 static void GrabPointer(BOOL grab)
98 Window window = X11DRV_get_whole_window(GetFocus());
102 XSetInputFocus(thread_display(), window, RevertToParent, CurrentTime);
107 if(!X11DRV_DD_GrabMessage)
108 X11DRV_DD_GrabMessage = RegisterWindowMessageA("WINE_X11DRV_GRABPOINTER");
110 X11DRV_DD_GrabOldProcedure = (WNDPROC)SetWindowLongPtrA(X11DRV_DD_PrimaryWnd,
111 GWLP_WNDPROC, (LONG_PTR)GrabWndProc);
113 SendMessageW(X11DRV_DD_PrimaryWnd, X11DRV_DD_GrabMessage, grab, 0);
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");
120 static DWORD PASCAL X11DRV_DDHAL_DestroyDriver(LPDDHAL_DESTROYDRIVERDATA data)
122 data->ddRVal = DD_OK;
123 return DDHAL_DRIVER_HANDLED;
126 static DWORD PASCAL X11DRV_DDHAL_CreateSurface(LPDDHAL_CREATESURFACEDATA data)
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);
136 data->ddRVal = DD_OK;
137 return DDHAL_DRIVER_NOTHANDLED;
140 static DWORD PASCAL X11DRV_DDHAL_CreatePalette(LPDDHAL_CREATEPALETTEDATA data)
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;
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 */
156 NULL, /* WaitForVerticalBlank */
157 NULL, /* CanCreateSurface */
158 X11DRV_DDHAL_CreatePalette,
159 NULL, /* GetScanLine */
160 NULL, /* SetExclusiveMode */
161 NULL /* FlipToGDISurface */
164 static DWORD PASCAL X11DRV_DDHAL_DestroySurface(LPDDHAL_DESTROYSURFACEDATA data)
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;
172 X11DRV_DD_UserClass = 0;
174 data->ddRVal = DD_OK;
175 return DDHAL_DRIVER_HANDLED;
178 static DWORD PASCAL X11DRV_DDHAL_SetPalette(LPDDHAL_SETPALETTEDATA data)
180 if (data->lpDDPalette && data->lpDDPalette->u1.dwReserved1) {
181 if (data->lpDDSurface == X11DRV_DD_Primary) {
183 /* we should probably find the ddraw window (maybe data->lpDD->lpExclusiveOwner->hWnd),
184 * and attach the palette to it
186 * Colormap pal = data->lpDDPalette->u1.dwReserved1;
190 data->ddRVal = DD_OK;
191 return DDHAL_DRIVER_HANDLED;
194 static DDHAL_DDSURFACECALLBACKS hal_ddsurfcallbacks = {
195 sizeof(DDHAL_DDSURFACECALLBACKS),
196 0x3fff, /* all callbacks are 32-bit */
197 X11DRV_DDHAL_DestroySurface,
199 NULL, /* SetClipList */
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
213 static DWORD PASCAL X11DRV_DDHAL_DestroyPalette(LPDDHAL_DESTROYPALETTEDATA data)
215 Colormap pal = data->lpDDPalette->u1.dwReserved1;
219 XFreeColormap(gdi_display, pal);
222 data->ddRVal = DD_OK;
223 return DDHAL_DRIVER_HANDLED;
226 static DWORD PASCAL X11DRV_DDHAL_SetPaletteEntries(LPDDHAL_SETENTRIESDATA data)
228 X11DRV_DDHAL_SetPalEntries(data->lpDDPalette->u1.dwReserved1,
229 data->dwBase, data->dwNumEntries,
231 data->ddRVal = DD_OK;
232 return DDHAL_DRIVER_HANDLED;
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
242 static X11DEVICE x11device = {
246 static DWORD PASCAL X11DRV_DDHAL_GetDriverInfo(LPDDHAL_GETDRIVERINFODATA data)
248 LPX11DRIVERINFO info = x11device.lpInfo;
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;
260 data->ddRVal = DDERR_CURRENTLYNOTAVAIL;
261 return DDHAL_DRIVER_HANDLED;
264 static DDHALINFO hal_info = {
267 &hal_ddsurfcallbacks,
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 */
279 0, /* dwFXAlphaCaps */
280 DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE, /* dwPalCaps */
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 */
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 */
306 0, /* dwMinOverlayStretch */
307 0, /* dwMaxOverlayStretch */
308 0, /* dwMinLiveVideoStretch */
309 0, /* dwMaxLiveVideoStretch */
310 0, /* dwMinHwCodecStretch */
311 0, /* dwMaxHwCodecStretch */
316 0, /* dwSVBCKeyCaps */
320 0, /* dwVSBCKeyCaps */
324 0, /* dwSSBCKeyCaps */
327 0, /* dwMaxVideoPorts */
328 0, /* dwCurrVideoPorts */
331 0, /* dwMonitorFrequency */
332 X11DRV_DDHAL_GetDriverInfo,
334 NULL, /* lpdwFourCC */
336 NULL, /* lpModeInfo */
337 DDHALINFO_ISPRIMARYDISPLAY | DDHALINFO_MODEXILLEGAL | DDHALINFO_GETDRIVERINFOSET, /* dwFlags */
340 0, /* lpD3DGlobalDriverData */
341 0, /* lpD3DHALCallbacks */
342 NULL /* lpDDExeBufCallbacks */
345 static LPDDHALDDRAWFNS ddraw_fns;
346 static DWORD ddraw_ver;
348 static void X11DRV_DDHAL_SetInfo(void)
350 (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
353 INT X11DRV_DCICommand(INT cbInput, const DCICMD *lpCmd, LPVOID lpOutData)
355 TRACE("(%d,(%ld,%ld,%ld),%p)\n", cbInput, lpCmd->dwCommand,
356 lpCmd->dwParam1, lpCmd->dwParam2, lpOutData);
358 switch (lpCmd->dwCommand) {
359 case DDNEWCALLBACKFNS:
360 ddraw_fns = (LPDDHALDDRAWFNS)lpCmd->dwParam1;
364 LPDDVERSIONDATA lpVer = (LPDDVERSIONDATA)lpOutData;
365 ddraw_ver = lpCmd->dwParam1;
367 /* well, whatever... the DDK says so */
368 lpVer->dwHALVersion = DD_RUNTIME_VERSION;
371 case DDGET32BITDRIVERNAME:
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;
383 case DDCREATEDRIVEROBJECT:
385 LPDWORD lpInstance = (LPDWORD)lpOutData;
387 /* FIXME: get x11drv's hInstance */
388 #ifdef HAVE_LIBXXF86DGA2
389 if (!X11DRV_XF86DGA2_CreateDriver(&hal_info))
392 X11DRV_Settings_CreateDriver(&hal_info);
395 /*X11DRV_GLX_CreateDriver(&hal_info);*/
398 (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
399 *lpInstance = hal_info.hInstance;
406 void X11DRV_DDHAL_SwitchMode(DWORD dwModeIndex, LPVOID fb_addr, LPVIDMEM fb_mem)
408 LPDDHALMODEINFO info = &hal_info.lpModeInfo[dwModeIndex];
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;
425 X11DRV_DDHAL_SetInfo();
428 void X11DRV_DDHAL_SetPalEntries(Colormap pal, DWORD dwBase, DWORD dwNumEntries,
429 LPPALETTEENTRY lpEntries)
436 c.flags = DoRed|DoGreen|DoBlue;
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);
444 XFlush(gdi_display); /* update display immediately */