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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
36 typedef struct _X11DRIVERINFO {
40 struct _X11DRIVERINFO*lpNext;
41 } X11DRIVERINFO,*LPX11DRIVERINFO;
43 typedef struct _X11DEVICE {
44 LPX11DRIVERINFO lpInfo;
45 } X11DEVICE,*LPX11DEVICE;
49 static LPDDRAWI_DDRAWSURFACE_LCL X11DRV_DD_Primary;
50 static LPDDRAWI_DDRAWSURFACE_GBL X11DRV_DD_PrimaryGbl;
51 static HWND X11DRV_DD_PrimaryWnd;
52 static HBITMAP X11DRV_DD_PrimaryDIB;
53 static Drawable X11DRV_DD_PrimaryDrawable;
54 static ATOM X11DRV_DD_UserClass;
55 static UINT X11DRV_DD_GrabMessage;
56 static WNDPROC X11DRV_DD_GrabOldProcedure;
58 static void X11DRV_DDHAL_SetPalEntries(Colormap pal, DWORD dwBase, DWORD dwNumEntries,
59 LPPALETTEENTRY lpEntries);
61 static void SetPrimaryDIB(HBITMAP hBmp)
63 X11DRV_DD_PrimaryDIB = hBmp;
65 X11DRV_DD_PrimaryDrawable = X11DRV_get_pixmap( hBmp );
67 X11DRV_DD_PrimaryDrawable = 0;
71 static LRESULT WINAPI GrabWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
73 struct x11drv_thread_data *data = x11drv_thread_data();
75 if(message != X11DRV_DD_GrabMessage)
76 return CallWindowProcA(X11DRV_DD_GrabOldProcedure, hWnd, message, wParam, lParam);
78 TRACE("hwnd=%p, grab=%d\n", hWnd, wParam);
82 /* find the X11 window that ddraw uses */
83 Window win = X11DRV_get_whole_window(hWnd);
84 TRACE("X11 window: %ld\n", win);
86 TRACE("host off desktop\n");
91 XGrabPointer(data->display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
93 data->grab_window = win;
98 XUngrabPointer(data->display, CurrentTime);
100 data->grab_window = None;
106 static void GrabPointer(BOOL grab)
109 Window window = X11DRV_get_whole_window(GetFocus());
113 XSetInputFocus(thread_display(), window, RevertToParent, CurrentTime);
118 if(!X11DRV_DD_GrabMessage)
119 X11DRV_DD_GrabMessage = RegisterWindowMessageA("WINE_X11DRV_GRABPOINTER");
121 X11DRV_DD_GrabOldProcedure = (WNDPROC)SetWindowLongPtrA(X11DRV_DD_PrimaryWnd,
122 GWLP_WNDPROC, (LONG_PTR)GrabWndProc);
124 SendMessageW(X11DRV_DD_PrimaryWnd, X11DRV_DD_GrabMessage, grab, 0);
126 if(SetWindowLongPtrA(X11DRV_DD_PrimaryWnd, GWLP_WNDPROC,
127 (LONG_PTR)X11DRV_DD_GrabOldProcedure) != (LONG_PTR)GrabWndProc)
128 ERR("Window procedure has been changed!\n");
131 static DWORD PASCAL X11DRV_DDHAL_DestroyDriver(LPDDHAL_DESTROYDRIVERDATA data)
133 data->ddRVal = DD_OK;
134 return DDHAL_DRIVER_HANDLED;
137 static DWORD PASCAL X11DRV_DDHAL_CreateSurface(LPDDHAL_CREATESURFACEDATA data)
139 if (data->lpDDSurfaceDesc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
140 X11DRV_DD_Primary = *data->lplpSList;
141 X11DRV_DD_PrimaryWnd = (HWND)X11DRV_DD_Primary->lpSurfMore->lpDDRAWReserved;
142 X11DRV_DD_PrimaryGbl = X11DRV_DD_Primary->lpGbl;
143 SetPrimaryDIB((HBITMAP)GET_LPDDRAWSURFACE_GBL_MORE(X11DRV_DD_PrimaryGbl)->hKernelSurface);
144 X11DRV_DD_UserClass = GlobalFindAtomA("WINE_DDRAW");
145 if (dxgrab) GrabPointer(TRUE);
147 data->ddRVal = DD_OK;
148 return DDHAL_DRIVER_NOTHANDLED;
151 static DWORD PASCAL X11DRV_DDHAL_CreatePalette(LPDDHAL_CREATEPALETTEDATA data)
154 /* only makes sense to do anything if the X server is running at 8bpp,
155 * which few people do nowadays */
156 data->ddRVal = DD_OK;
157 return DDHAL_DRIVER_HANDLED;
160 static DDHAL_DDCALLBACKS hal_ddcallbacks = {
161 sizeof(DDHAL_DDCALLBACKS),
162 0x3ff, /* all callbacks are 32-bit */
163 X11DRV_DDHAL_DestroyDriver,
164 X11DRV_DDHAL_CreateSurface,
165 NULL, /* SetColorKey */
167 NULL, /* WaitForVerticalBlank */
168 NULL, /* CanCreateSurface */
169 X11DRV_DDHAL_CreatePalette,
170 NULL, /* GetScanLine */
171 NULL, /* SetExclusiveMode */
172 NULL /* FlipToGDISurface */
175 static DWORD PASCAL X11DRV_DDHAL_DestroySurface(LPDDHAL_DESTROYSURFACEDATA data)
177 if (data->lpDDSurface == X11DRV_DD_Primary) {
178 if (dxgrab) GrabPointer(FALSE);
179 X11DRV_DD_Primary = NULL;
180 X11DRV_DD_PrimaryWnd = 0;
181 X11DRV_DD_PrimaryGbl = NULL;
183 X11DRV_DD_UserClass = 0;
185 data->ddRVal = DD_OK;
186 return DDHAL_DRIVER_HANDLED;
189 static DWORD PASCAL X11DRV_DDHAL_SetPalette(LPDDHAL_SETPALETTEDATA data)
191 if (data->lpDDPalette && data->lpDDPalette->u1.dwReserved1) {
192 if (data->lpDDSurface == X11DRV_DD_Primary) {
194 /* we should probably find the ddraw window (maybe data->lpDD->lpExclusiveOwner->hWnd),
195 * and attach the palette to it
197 * Colormap pal = data->lpDDPalette->u1.dwReserved1;
201 data->ddRVal = DD_OK;
202 return DDHAL_DRIVER_HANDLED;
205 static DDHAL_DDSURFACECALLBACKS hal_ddsurfcallbacks = {
206 sizeof(DDHAL_DDSURFACECALLBACKS),
207 0x3fff, /* all callbacks are 32-bit */
208 X11DRV_DDHAL_DestroySurface,
210 NULL, /* SetClipList */
214 NULL, /* SetColorKey */
215 NULL, /* AddAttachedSurface */
216 NULL, /* GetBltStatus */
217 NULL, /* GetFlipStatus */
218 NULL, /* UpdateOverlay */
219 NULL, /* SetOverlayPosition */
220 NULL, /* reserved4 */
221 X11DRV_DDHAL_SetPalette
224 static DWORD PASCAL X11DRV_DDHAL_DestroyPalette(LPDDHAL_DESTROYPALETTEDATA data)
226 Colormap pal = data->lpDDPalette->u1.dwReserved1;
230 XFreeColormap(gdi_display, pal);
233 data->ddRVal = DD_OK;
234 return DDHAL_DRIVER_HANDLED;
237 static DWORD PASCAL X11DRV_DDHAL_SetPaletteEntries(LPDDHAL_SETENTRIESDATA data)
239 X11DRV_DDHAL_SetPalEntries(data->lpDDPalette->u1.dwReserved1,
240 data->dwBase, data->dwNumEntries,
242 data->ddRVal = DD_OK;
243 return DDHAL_DRIVER_HANDLED;
246 static DDHAL_DDPALETTECALLBACKS hal_ddpalcallbacks = {
247 sizeof(DDHAL_DDPALETTECALLBACKS),
248 0x3, /* all callbacks are 32-bit */
249 X11DRV_DDHAL_DestroyPalette,
250 X11DRV_DDHAL_SetPaletteEntries
253 static X11DEVICE x11device = {
257 static DWORD PASCAL X11DRV_DDHAL_GetDriverInfo(LPDDHAL_GETDRIVERINFODATA data)
259 LPX11DRIVERINFO info = x11device.lpInfo;
261 if (IsEqualGUID(&data->guidInfo, info->lpGuid)) {
262 DWORD dwSize = info->dwSize;
263 data->dwActualSize = dwSize;
264 if (data->dwExpectedSize < dwSize) dwSize = data->dwExpectedSize;
265 memcpy(data->lpvData, info->lpvData, dwSize);
266 data->ddRVal = DD_OK;
267 return DDHAL_DRIVER_HANDLED;
271 data->ddRVal = DDERR_CURRENTLYNOTAVAIL;
272 return DDHAL_DRIVER_HANDLED;
275 static DDHALINFO hal_info = {
278 &hal_ddsurfcallbacks,
283 { /* ddCaps (only stuff the HAL implements here) */
284 sizeof(DDCORECAPS), /* dwSize */
285 DDCAPS_GDI | DDCAPS_PALETTE, /* dwCaps */
286 DDCAPS2_CERTIFIED | DDCAPS2_NONLOCALVIDMEM | DDCAPS2_NOPAGELOCKREQUIRED | DDCAPS2_CANRENDERWINDOWED |
287 DDCAPS2_WIDESURFACES | DDCAPS2_PRIMARYGAMMA | DDCAPS2_FLIPNOVSYNC, /* dwCaps2 */
290 0, /* dwFXAlphaCaps */
291 DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE, /* dwPalCaps */
293 0, /* dwAlphaBltConstBitDepths */
294 0, /* dwAlphaBltPixelBitDepths */
295 0, /* dwAlphaBltSurfaceBitDepths */
296 0, /* dwAlphaOverlayBltConstBitDepths */
297 0, /* dwAlphaOverlayBltPixelBitDepths */
298 0, /* dwAlphaOverlayBltSurfaceBitDepths */
299 0, /* dwZBufferBitDepths */
300 16*1024*1024, /* dwVidMemTotal */
301 16*1024*1024, /* dwVidMemFree */
302 0, /* dwMaxVisibleOverlays */
303 0, /* dwCurrVisibleOverlays */
304 0, /* dwNumFourCCCodes */
305 0, /* dwAlignBoundarySrc */
306 0, /* dwAlignSizeSrc */
307 0, /* dwAlignBoundaryDest */
308 0, /* dwAlignSizeDest */
309 0, /* dwAlignStrideAlign */
312 DDSCAPS_BACKBUFFER | DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER |
313 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_PALETTE | DDSCAPS_PRIMARYSURFACE |
314 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY |
315 DDSCAPS_VISIBLE | DDSCAPS_LOCALVIDMEM | DDSCAPS_NONLOCALVIDMEM /* dwCaps */
317 0, /* dwMinOverlayStretch */
318 0, /* dwMaxOverlayStretch */
319 0, /* dwMinLiveVideoStretch */
320 0, /* dwMaxLiveVideoStretch */
321 0, /* dwMinHwCodecStretch */
322 0, /* dwMaxHwCodecStretch */
327 0, /* dwSVBCKeyCaps */
331 0, /* dwVSBCKeyCaps */
335 0, /* dwSSBCKeyCaps */
338 0, /* dwMaxVideoPorts */
339 0, /* dwCurrVideoPorts */
342 0, /* dwMonitorFrequency */
343 X11DRV_DDHAL_GetDriverInfo,
345 NULL, /* lpdwFourCC */
347 NULL, /* lpModeInfo */
348 DDHALINFO_ISPRIMARYDISPLAY | DDHALINFO_MODEXILLEGAL | DDHALINFO_GETDRIVERINFOSET, /* dwFlags */
351 0, /* lpD3DGlobalDriverData */
352 0, /* lpD3DHALCallbacks */
353 NULL /* lpDDExeBufCallbacks */
356 static LPDDHALDDRAWFNS ddraw_fns;
357 static DWORD ddraw_ver;
359 static void X11DRV_DDHAL_SetInfo(void)
361 (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
364 INT X11DRV_DCICommand(INT cbInput, const DCICMD *lpCmd, LPVOID lpOutData)
366 TRACE("(%d,(%d,%d,%d),%p)\n", cbInput, lpCmd->dwCommand,
367 lpCmd->dwParam1, lpCmd->dwParam2, lpOutData);
369 switch (lpCmd->dwCommand) {
370 case DDNEWCALLBACKFNS:
371 ddraw_fns = (LPDDHALDDRAWFNS)lpCmd->dwParam1;
375 LPDDVERSIONDATA lpVer = (LPDDVERSIONDATA)lpOutData;
376 ddraw_ver = lpCmd->dwParam1;
378 /* well, whatever... the DDK says so */
379 lpVer->dwHALVersion = DD_RUNTIME_VERSION;
382 case DDGET32BITDRIVERNAME:
384 LPDD32BITDRIVERDATA lpData = (LPDD32BITDRIVERDATA)lpOutData;
385 /* here, we could ask ddraw to load a separate DLL, that
386 * would contain the 32-bit ddraw HAL */
387 strcpy(lpData->szName,"x11drv");
388 /* the entry point named here should initialize our hal_info
389 * with 32-bit entry points (ignored for now) */
390 strcpy(lpData->szEntryPoint,"DriverInit");
391 lpData->dwContext = 0;
394 case DDCREATEDRIVEROBJECT:
396 LPDWORD lpInstance = (LPDWORD)lpOutData;
398 /* FIXME: get x11drv's hInstance */
399 X11DRV_Settings_CreateDriver(&hal_info);
401 /*X11DRV_GLX_CreateDriver(&hal_info);*/
404 (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
405 *lpInstance = hal_info.hInstance;
412 void X11DRV_DDHAL_SwitchMode(DWORD dwModeIndex, LPVOID fb_addr, LPVIDMEM fb_mem)
414 LPDDHALMODEINFO info = &hal_info.lpModeInfo[dwModeIndex];
416 hal_info.dwModeIndex = dwModeIndex;
417 hal_info.dwMonitorFrequency = info->wRefreshRate;
418 hal_info.vmiData.fpPrimary = (FLATPTR)fb_addr;
419 hal_info.vmiData.dwDisplayWidth = info->dwWidth;
420 hal_info.vmiData.dwDisplayHeight = info->dwHeight;
421 hal_info.vmiData.lDisplayPitch = info->lPitch;
422 hal_info.vmiData.ddpfDisplay.dwSize = info->dwBPP ? sizeof(hal_info.vmiData.ddpfDisplay) : 0;
423 hal_info.vmiData.ddpfDisplay.dwFlags = (info->wFlags & DDMODEINFO_PALETTIZED) ? DDPF_PALETTEINDEXED8 : 0;
424 hal_info.vmiData.ddpfDisplay.u1.dwRGBBitCount = (info->dwBPP > 24) ? 24 : info->dwBPP;
425 hal_info.vmiData.ddpfDisplay.u2.dwRBitMask = info->dwRBitMask;
426 hal_info.vmiData.ddpfDisplay.u3.dwGBitMask = info->dwGBitMask;
427 hal_info.vmiData.ddpfDisplay.u4.dwBBitMask = info->dwBBitMask;
428 hal_info.vmiData.dwNumHeaps = fb_mem ? 1 : 0;
429 hal_info.vmiData.pvmList = fb_mem;
431 X11DRV_DDHAL_SetInfo();
434 static void X11DRV_DDHAL_SetPalEntries(Colormap pal, DWORD dwBase, DWORD dwNumEntries,
435 LPPALETTEENTRY lpEntries)
442 c.flags = DoRed|DoGreen|DoBlue;
444 for (n=0; n<dwNumEntries; n++,c.pixel++) {
445 c.red = lpEntries[n].peRed << 8;
446 c.green = lpEntries[n].peGreen << 8;
447 c.blue = lpEntries[n].peBlue << 8;
448 XStoreColor(gdi_display, pal, &c);
450 XFlush(gdi_display); /* update display immediately */