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
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
38 typedef struct _X11DRIVERINFO {
42 struct _X11DRIVERINFO*lpNext;
43 } X11DRIVERINFO,*LPX11DRIVERINFO;
45 typedef struct _X11DEVICE {
46 LPX11DRIVERINFO lpInfo;
47 } 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=%ld\n", hWnd, wParam);
84 /* find the X11 window that ddraw uses */
85 Window win = X11DRV_get_whole_window(hWnd);
86 TRACE("X11 window: %ld\n", win);
88 TRACE("host off desktop\n");
93 XGrabPointer(data->display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
95 data->grab_window = win;
100 XUngrabPointer(data->display, CurrentTime);
102 data->grab_window = None;
108 static void GrabPointer(BOOL grab)
111 Display *display = thread_display();
112 Window window = X11DRV_get_whole_window(GetFocus());
113 if(window && display)
116 XSetInputFocus(display, window, RevertToParent, CurrentTime);
121 if(!X11DRV_DD_GrabMessage)
122 X11DRV_DD_GrabMessage = RegisterWindowMessageA("WINE_X11DRV_GRABPOINTER");
124 X11DRV_DD_GrabOldProcedure = (WNDPROC)SetWindowLongPtrA(X11DRV_DD_PrimaryWnd,
125 GWLP_WNDPROC, (LONG_PTR)GrabWndProc);
127 SendMessageW(X11DRV_DD_PrimaryWnd, X11DRV_DD_GrabMessage, grab, 0);
129 if(SetWindowLongPtrA(X11DRV_DD_PrimaryWnd, GWLP_WNDPROC,
130 (LONG_PTR)X11DRV_DD_GrabOldProcedure) != (LONG_PTR)GrabWndProc)
131 ERR("Window procedure has been changed!\n");
134 static DWORD PASCAL X11DRV_DDHAL_DestroyDriver(LPDDHAL_DESTROYDRIVERDATA data)
136 data->ddRVal = DD_OK;
137 return DDHAL_DRIVER_HANDLED;
140 static DWORD PASCAL X11DRV_DDHAL_CreateSurface(LPDDHAL_CREATESURFACEDATA data)
142 if (data->lpDDSurfaceDesc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
143 X11DRV_DD_Primary = *data->lplpSList;
144 X11DRV_DD_PrimaryWnd = X11DRV_DD_Primary->lpSurfMore->lpDDRAWReserved;
145 X11DRV_DD_PrimaryGbl = X11DRV_DD_Primary->lpGbl;
146 SetPrimaryDIB((HBITMAP)GET_LPDDRAWSURFACE_GBL_MORE(X11DRV_DD_PrimaryGbl)->hKernelSurface);
147 X11DRV_DD_UserClass = GlobalFindAtomA("WINE_DDRAW");
148 if (dxgrab) GrabPointer(TRUE);
150 data->ddRVal = DD_OK;
151 return DDHAL_DRIVER_NOTHANDLED;
154 static DWORD PASCAL X11DRV_DDHAL_CreatePalette(LPDDHAL_CREATEPALETTEDATA data)
157 /* only makes sense to do anything if the X server is running at 8bpp,
158 * which few people do nowadays */
159 data->ddRVal = DD_OK;
160 return DDHAL_DRIVER_HANDLED;
163 static DDHAL_DDCALLBACKS hal_ddcallbacks = {
164 sizeof(DDHAL_DDCALLBACKS),
165 0x3ff, /* all callbacks are 32-bit */
166 X11DRV_DDHAL_DestroyDriver,
167 X11DRV_DDHAL_CreateSurface,
168 NULL, /* SetColorKey */
170 NULL, /* WaitForVerticalBlank */
171 NULL, /* CanCreateSurface */
172 X11DRV_DDHAL_CreatePalette,
173 NULL, /* GetScanLine */
174 NULL, /* SetExclusiveMode */
175 NULL /* FlipToGDISurface */
178 static DWORD PASCAL X11DRV_DDHAL_DestroySurface(LPDDHAL_DESTROYSURFACEDATA data)
180 if (data->lpDDSurface == X11DRV_DD_Primary) {
181 if (dxgrab) GrabPointer(FALSE);
182 X11DRV_DD_Primary = NULL;
183 X11DRV_DD_PrimaryWnd = 0;
184 X11DRV_DD_PrimaryGbl = NULL;
186 X11DRV_DD_UserClass = 0;
188 data->ddRVal = DD_OK;
189 return DDHAL_DRIVER_HANDLED;
192 static DWORD PASCAL X11DRV_DDHAL_SetPalette(LPDDHAL_SETPALETTEDATA data)
194 if (data->lpDDPalette && data->lpDDPalette->u1.dwReserved1) {
195 if (data->lpDDSurface == X11DRV_DD_Primary) {
197 /* we should probably find the ddraw window (maybe data->lpDD->lpExclusiveOwner->hWnd),
198 * and attach the palette to it
200 * Colormap pal = data->lpDDPalette->u1.dwReserved1;
204 data->ddRVal = DD_OK;
205 return DDHAL_DRIVER_HANDLED;
208 static DDHAL_DDSURFACECALLBACKS hal_ddsurfcallbacks = {
209 sizeof(DDHAL_DDSURFACECALLBACKS),
210 0x3fff, /* all callbacks are 32-bit */
211 X11DRV_DDHAL_DestroySurface,
213 NULL, /* SetClipList */
217 NULL, /* SetColorKey */
218 NULL, /* AddAttachedSurface */
219 NULL, /* GetBltStatus */
220 NULL, /* GetFlipStatus */
221 NULL, /* UpdateOverlay */
222 NULL, /* SetOverlayPosition */
223 NULL, /* reserved4 */
224 X11DRV_DDHAL_SetPalette
227 static DWORD PASCAL X11DRV_DDHAL_DestroyPalette(LPDDHAL_DESTROYPALETTEDATA data)
229 Colormap pal = data->lpDDPalette->u1.dwReserved1;
233 XFreeColormap(gdi_display, pal);
236 data->ddRVal = DD_OK;
237 return DDHAL_DRIVER_HANDLED;
240 static DWORD PASCAL X11DRV_DDHAL_SetPaletteEntries(LPDDHAL_SETENTRIESDATA data)
242 X11DRV_DDHAL_SetPalEntries(data->lpDDPalette->u1.dwReserved1,
243 data->dwBase, data->dwNumEntries,
245 data->ddRVal = DD_OK;
246 return DDHAL_DRIVER_HANDLED;
249 static DDHAL_DDPALETTECALLBACKS hal_ddpalcallbacks = {
250 sizeof(DDHAL_DDPALETTECALLBACKS),
251 0x3, /* all callbacks are 32-bit */
252 X11DRV_DDHAL_DestroyPalette,
253 X11DRV_DDHAL_SetPaletteEntries
256 static X11DEVICE x11device = {
260 static DWORD PASCAL X11DRV_DDHAL_GetDriverInfo(LPDDHAL_GETDRIVERINFODATA data)
262 LPX11DRIVERINFO info = x11device.lpInfo;
264 if (IsEqualGUID(&data->guidInfo, info->lpGuid)) {
265 DWORD dwSize = info->dwSize;
266 data->dwActualSize = dwSize;
267 if (data->dwExpectedSize < dwSize) dwSize = data->dwExpectedSize;
268 memcpy(data->lpvData, info->lpvData, dwSize);
269 data->ddRVal = DD_OK;
270 return DDHAL_DRIVER_HANDLED;
274 data->ddRVal = DDERR_CURRENTLYNOTAVAIL;
275 return DDHAL_DRIVER_HANDLED;
278 static DDHALINFO hal_info = {
281 &hal_ddsurfcallbacks,
286 { /* ddCaps (only stuff the HAL implements here) */
287 sizeof(DDCORECAPS), /* dwSize */
288 DDCAPS_GDI | DDCAPS_PALETTE, /* dwCaps */
289 DDCAPS2_CERTIFIED | DDCAPS2_NONLOCALVIDMEM | DDCAPS2_NOPAGELOCKREQUIRED | DDCAPS2_CANRENDERWINDOWED |
290 DDCAPS2_WIDESURFACES | DDCAPS2_PRIMARYGAMMA | DDCAPS2_FLIPNOVSYNC, /* dwCaps2 */
293 0, /* dwFXAlphaCaps */
294 DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE, /* dwPalCaps */
296 0, /* dwAlphaBltConstBitDepths */
297 0, /* dwAlphaBltPixelBitDepths */
298 0, /* dwAlphaBltSurfaceBitDepths */
299 0, /* dwAlphaOverlayBltConstBitDepths */
300 0, /* dwAlphaOverlayBltPixelBitDepths */
301 0, /* dwAlphaOverlayBltSurfaceBitDepths */
302 0, /* dwZBufferBitDepths */
303 16*1024*1024, /* dwVidMemTotal */
304 16*1024*1024, /* dwVidMemFree */
305 0, /* dwMaxVisibleOverlays */
306 0, /* dwCurrVisibleOverlays */
307 0, /* dwNumFourCCCodes */
308 0, /* dwAlignBoundarySrc */
309 0, /* dwAlignSizeSrc */
310 0, /* dwAlignBoundaryDest */
311 0, /* dwAlignSizeDest */
312 0, /* dwAlignStrideAlign */
315 DDSCAPS_BACKBUFFER | DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER |
316 DDSCAPS_OFFSCREENPLAIN | DDSCAPS_PALETTE | DDSCAPS_PRIMARYSURFACE |
317 DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY |
318 DDSCAPS_VISIBLE | DDSCAPS_LOCALVIDMEM | DDSCAPS_NONLOCALVIDMEM /* dwCaps */
320 0, /* dwMinOverlayStretch */
321 0, /* dwMaxOverlayStretch */
322 0, /* dwMinLiveVideoStretch */
323 0, /* dwMaxLiveVideoStretch */
324 0, /* dwMinHwCodecStretch */
325 0, /* dwMaxHwCodecStretch */
330 0, /* dwSVBCKeyCaps */
334 0, /* dwVSBCKeyCaps */
338 0, /* dwSSBCKeyCaps */
341 0, /* dwMaxVideoPorts */
342 0, /* dwCurrVideoPorts */
345 0, /* dwMonitorFrequency */
346 X11DRV_DDHAL_GetDriverInfo,
348 NULL, /* lpdwFourCC */
350 NULL, /* lpModeInfo */
351 DDHALINFO_ISPRIMARYDISPLAY | DDHALINFO_MODEXILLEGAL | DDHALINFO_GETDRIVERINFOSET, /* dwFlags */
354 0, /* lpD3DGlobalDriverData */
355 0, /* lpD3DHALCallbacks */
356 NULL /* lpDDExeBufCallbacks */
359 static LPDDHALDDRAWFNS ddraw_fns;
360 static DWORD ddraw_ver;
362 static void X11DRV_DDHAL_SetInfo(void)
364 (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
367 INT X11DRV_DCICommand(INT cbInput, const DCICMD *lpCmd, LPVOID lpOutData)
369 TRACE("(%d,(%d,%d,%d),%p)\n", cbInput, lpCmd->dwCommand,
370 lpCmd->dwParam1, lpCmd->dwParam2, lpOutData);
372 switch (lpCmd->dwCommand) {
373 case DDNEWCALLBACKFNS:
374 ddraw_fns = (LPDDHALDDRAWFNS)lpCmd->dwParam1;
378 LPDDVERSIONDATA lpVer = lpOutData;
379 ddraw_ver = lpCmd->dwParam1;
381 /* well, whatever... the DDK says so */
382 lpVer->dwHALVersion = DD_RUNTIME_VERSION;
385 case DDGET32BITDRIVERNAME:
387 LPDD32BITDRIVERDATA lpData = (LPDD32BITDRIVERDATA)lpOutData;
388 /* here, we could ask ddraw to load a separate DLL, that
389 * would contain the 32-bit ddraw HAL */
390 strcpy(lpData->szName,"x11drv");
391 /* the entry point named here should initialize our hal_info
392 * with 32-bit entry points (ignored for now) */
393 strcpy(lpData->szEntryPoint,"DriverInit");
394 lpData->dwContext = 0;
397 case DDCREATEDRIVEROBJECT:
399 LPDWORD lpInstance = lpOutData;
401 /* FIXME: get x11drv's hInstance */
402 X11DRV_Settings_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 */