Removed W->A from DEFWND_ImmIsUIMessageW.
[wine] / dlls / x11drv / x11ddraw.c
1 /*
2  * DirectDraw driver interface
3  *
4  * Copyright 2001 TransGaming Technologies, Inc.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24 #include <X11/Xlib.h>
25
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28 #include "x11drv.h"
29 #include "x11ddraw.h"
30 #include "xvidmode.h"
31 #include "xrandr.h"
32 #include "dga2.h"
33
34 #include "windef.h"
35 #include "wingdi.h"
36 #include "ddrawi.h"
37 #include "gdi.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
41
42 extern int dxgrab;
43
44 LPDDRAWI_DDRAWSURFACE_LCL X11DRV_DD_Primary;
45 LPDDRAWI_DDRAWSURFACE_GBL X11DRV_DD_PrimaryGbl;
46 HWND X11DRV_DD_PrimaryWnd;
47 HBITMAP X11DRV_DD_PrimaryDIB;
48 Drawable X11DRV_DD_PrimaryDrawable;
49 ATOM X11DRV_DD_UserClass;
50 BOOL X11DRV_DD_IsDirect;
51 static UINT X11DRV_DD_GrabMessage;
52 static WNDPROC X11DRV_DD_GrabOldProcedure;
53
54 static void SetPrimaryDIB(HBITMAP hBmp)
55 {
56   X11DRV_DD_PrimaryDIB = hBmp;
57   if (hBmp) {
58     BITMAPOBJ *bmp;
59     bmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
60     X11DRV_DD_PrimaryDrawable = (Pixmap)bmp->physBitmap;
61     GDI_ReleaseObj( hBmp );
62   } else {
63     X11DRV_DD_PrimaryDrawable = 0;
64   }
65 }
66
67 static LRESULT WINAPI GrabWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
68 {
69   Display *display = thread_display();
70
71   if(message != X11DRV_DD_GrabMessage)
72     return CallWindowProcA(X11DRV_DD_GrabOldProcedure, hWnd, message, wParam, lParam);
73
74   TRACE("hwnd=%p, grab=%d\n", hWnd, wParam);
75
76   if (wParam)
77   {
78     /* find the X11 window that ddraw uses */
79     Window win = X11DRV_get_whole_window(hWnd);
80     TRACE("X11 window: %ld\n", win);
81     if (!win) {
82       TRACE("host off desktop\n");
83       win = root_window;
84     }
85
86     wine_tsx11_lock();
87     XGrabPointer(display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
88     wine_tsx11_unlock();
89   }
90   else
91   {
92     wine_tsx11_lock();
93     XUngrabPointer(display, CurrentTime);
94     wine_tsx11_unlock();
95   }
96
97   return 0;
98 }
99
100 static void GrabPointer(BOOL grab)
101 {
102   if(grab) {
103     Window window = X11DRV_get_whole_window(GetFocus());
104     if(window)
105       XSetInputFocus(thread_display(), window, RevertToParent, CurrentTime);
106   }
107
108   if(!X11DRV_DD_GrabMessage)
109     X11DRV_DD_GrabMessage = RegisterWindowMessageA("WINE_X11DRV_GRABPOINTER");
110
111   X11DRV_DD_GrabOldProcedure = (WNDPROC)SetWindowLongPtrA(X11DRV_DD_PrimaryWnd,
112                                                        GWLP_WNDPROC, (LONG_PTR)GrabWndProc);
113
114   SendMessageA(X11DRV_DD_PrimaryWnd, X11DRV_DD_GrabMessage, grab ? 1 : 0, 0);
115
116   if(SetWindowLongPtrA(X11DRV_DD_PrimaryWnd, GWLP_WNDPROC,
117                     (LONG_PTR)X11DRV_DD_GrabOldProcedure) != (LONG_PTR)GrabWndProc)
118     ERR("Window procedure has been changed!\n");
119 }
120
121 static DWORD PASCAL X11DRV_DDHAL_DestroyDriver(LPDDHAL_DESTROYDRIVERDATA data)
122 {
123   data->ddRVal = DD_OK;
124   return DDHAL_DRIVER_HANDLED;
125 }
126
127 static DWORD PASCAL X11DRV_DDHAL_CreateSurface(LPDDHAL_CREATESURFACEDATA data)
128 {
129   if (data->lpDDSurfaceDesc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
130     X11DRV_DD_Primary = *data->lplpSList;
131     X11DRV_DD_PrimaryWnd = (HWND)X11DRV_DD_Primary->lpSurfMore->lpDDRAWReserved;
132     X11DRV_DD_PrimaryGbl = X11DRV_DD_Primary->lpGbl;
133     SetPrimaryDIB((HBITMAP)GET_LPDDRAWSURFACE_GBL_MORE(X11DRV_DD_PrimaryGbl)->hKernelSurface);
134     X11DRV_DD_UserClass = GlobalFindAtomA("WINE_DDRAW");
135     if (dxgrab) GrabPointer(TRUE);
136   }
137   data->ddRVal = DD_OK;
138   return DDHAL_DRIVER_NOTHANDLED;
139 }
140
141 static DWORD PASCAL X11DRV_DDHAL_CreatePalette(LPDDHAL_CREATEPALETTEDATA data)
142 {
143   FIXME("stub\n");
144   /* only makes sense to do anything if the X server is running at 8bpp,
145    * which few people do nowadays */
146   data->ddRVal = DD_OK;
147   return DDHAL_DRIVER_HANDLED;
148 }
149
150 static DDHAL_DDCALLBACKS hal_ddcallbacks = {
151   sizeof(DDHAL_DDCALLBACKS),
152   0x3ff, /* all callbacks are 32-bit */
153   X11DRV_DDHAL_DestroyDriver,
154   X11DRV_DDHAL_CreateSurface,
155   NULL, /* SetColorKey */
156   NULL, /* SetMode */
157   NULL, /* WaitForVerticalBlank */
158   NULL, /* CanCreateSurface */
159   X11DRV_DDHAL_CreatePalette,
160   NULL, /* GetScanLine */
161   NULL, /* SetExclusiveMode */
162   NULL  /* FlipToGDISurface */
163 };
164
165 static DWORD PASCAL X11DRV_DDHAL_DestroySurface(LPDDHAL_DESTROYSURFACEDATA data)
166 {
167   if (data->lpDDSurface == X11DRV_DD_Primary) {
168     if (dxgrab) GrabPointer(FALSE);
169     X11DRV_DD_Primary = NULL;
170     X11DRV_DD_PrimaryWnd = 0;
171     X11DRV_DD_PrimaryGbl = NULL;
172     SetPrimaryDIB(0);
173     X11DRV_DD_UserClass = 0;
174   }
175   data->ddRVal = DD_OK;
176   return DDHAL_DRIVER_HANDLED;
177 }
178
179 static DWORD PASCAL X11DRV_DDHAL_SetPalette(LPDDHAL_SETPALETTEDATA data)
180 {
181   if (data->lpDDPalette && data->lpDDPalette->u1.dwReserved1) {
182     if (data->lpDDSurface == X11DRV_DD_Primary) {
183       FIXME("stub\n");
184       /* we should probably find the ddraw window (maybe data->lpDD->lpExclusiveOwner->hWnd),
185        * and attach the palette to it
186        *
187        * Colormap pal = data->lpDDPalette->u1.dwReserved1;
188        */
189     }
190   }
191   data->ddRVal = DD_OK;
192   return DDHAL_DRIVER_HANDLED;
193 }
194
195 static DDHAL_DDSURFACECALLBACKS hal_ddsurfcallbacks = {
196   sizeof(DDHAL_DDSURFACECALLBACKS),
197   0x3fff, /* all callbacks are 32-bit */
198   X11DRV_DDHAL_DestroySurface,
199   NULL, /* Flip */
200   NULL, /* SetClipList */
201   NULL, /* Lock */
202   NULL, /* Unlock */
203   NULL, /* Blt */
204   NULL, /* SetColorKey */
205   NULL, /* AddAttachedSurface */
206   NULL, /* GetBltStatus */
207   NULL, /* GetFlipStatus */
208   NULL, /* UpdateOverlay */
209   NULL, /* SetOverlayPosition */
210   NULL, /* reserved4 */
211   X11DRV_DDHAL_SetPalette
212 };
213
214 static DWORD PASCAL X11DRV_DDHAL_DestroyPalette(LPDDHAL_DESTROYPALETTEDATA data)
215 {
216   Colormap pal = data->lpDDPalette->u1.dwReserved1;
217   if (pal)
218   {
219       wine_tsx11_lock();
220       XFreeColormap(gdi_display, pal);
221       wine_tsx11_unlock();
222   }
223   data->ddRVal = DD_OK;
224   return DDHAL_DRIVER_HANDLED;
225 }
226
227 static DWORD PASCAL X11DRV_DDHAL_SetPaletteEntries(LPDDHAL_SETENTRIESDATA data)
228 {
229   X11DRV_DDHAL_SetPalEntries(data->lpDDPalette->u1.dwReserved1,
230                              data->dwBase, data->dwNumEntries,
231                              data->lpEntries);
232   data->ddRVal = DD_OK;
233   return DDHAL_DRIVER_HANDLED;
234 }
235
236 static DDHAL_DDPALETTECALLBACKS hal_ddpalcallbacks = {
237   sizeof(DDHAL_DDPALETTECALLBACKS),
238   0x3, /* all callbacks are 32-bit */
239   X11DRV_DDHAL_DestroyPalette,
240   X11DRV_DDHAL_SetPaletteEntries
241 };
242
243 static X11DEVICE x11device = {
244   NULL
245 };
246
247 static DWORD PASCAL X11DRV_DDHAL_GetDriverInfo(LPDDHAL_GETDRIVERINFODATA data)
248 {
249   LPX11DRIVERINFO info = x11device.lpInfo;
250   while (info) {
251     if (IsEqualGUID(&data->guidInfo, info->lpGuid)) {
252       DWORD dwSize = info->dwSize;
253       data->dwActualSize = dwSize;
254       if (data->dwExpectedSize < dwSize) dwSize = data->dwExpectedSize;
255       memcpy(data->lpvData, info->lpvData, dwSize);
256       data->ddRVal = DD_OK;
257       return DDHAL_DRIVER_HANDLED;
258     }
259     info = info->lpNext;
260   }
261   data->ddRVal = DDERR_CURRENTLYNOTAVAIL;
262   return DDHAL_DRIVER_HANDLED;
263 }
264
265 static DDHALINFO hal_info = {
266   sizeof(DDHALINFO),
267   &hal_ddcallbacks,
268   &hal_ddsurfcallbacks,
269   &hal_ddpalcallbacks,
270   {     /* vmiData */
271    0     /* fpPrimary */
272   },
273   {     /* ddCaps (only stuff the HAL implements here) */
274    sizeof(DDCORECAPS),                                                  /* dwSize */
275    DDCAPS_GDI | DDCAPS_PALETTE,                                         /* dwCaps */
276    DDCAPS2_CERTIFIED | DDCAPS2_NONLOCALVIDMEM | DDCAPS2_NOPAGELOCKREQUIRED | DDCAPS2_CANRENDERWINDOWED |
277    DDCAPS2_WIDESURFACES | DDCAPS2_PRIMARYGAMMA | DDCAPS2_FLIPNOVSYNC,   /* dwCaps2 */
278    0,                                                                   /* dwCKeyCaps */
279    0,                                                                   /* dwFXCaps */
280    0,                                                                   /* dwFXAlphaCaps */
281    DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE,                               /* dwPalCaps */
282    0,                                                                   /* dwSVCaps */
283    0,                                                                   /* dwAlphaBltConstBitDepths */
284    0,                                                                   /* dwAlphaBltPixelBitDepths */
285    0,                                                                   /* dwAlphaBltSurfaceBitDepths */
286    0,                                                                   /* dwAlphaOverlayBltConstBitDepths */
287    0,                                                                   /* dwAlphaOverlayBltPixelBitDepths */
288    0,                                                                   /* dwAlphaOverlayBltSurfaceBitDepths */
289    0,                                                                   /* dwZBufferBitDepths */
290    16*1024*1024,                                                        /* dwVidMemTotal */
291    16*1024*1024,                                                        /* dwVidMemFree */
292    0,                                                                   /* dwMaxVisibleOverlays */
293    0,                                                                   /* dwCurrVisibleOverlays */
294    0,                                                                   /* dwNumFourCCCodes */
295    0,                                                                   /* dwAlignBoundarySrc */
296    0,                                                                   /* dwAlignSizeSrc */
297    0,                                                                   /* dwAlignBoundaryDest */
298    0,                                                                   /* dwAlignSizeDest */
299    0,                                                                   /* dwAlignStrideAlign */
300    {0},                                                                 /* dwRops */
301    {                                                                    /* ddsCaps */
302     DDSCAPS_BACKBUFFER | DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER |
303     DDSCAPS_OFFSCREENPLAIN | DDSCAPS_PALETTE | DDSCAPS_PRIMARYSURFACE |
304     DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY |
305     DDSCAPS_VISIBLE | DDSCAPS_LOCALVIDMEM | DDSCAPS_NONLOCALVIDMEM      /* dwCaps */
306    },
307    0,                                                                   /* dwMinOverlayStretch */
308    0,                                                                   /* dwMaxOverlayStretch */
309    0,                                                                   /* dwMinLiveVideoStretch */
310    0,                                                                   /* dwMaxLiveVideoStretch */
311    0,                                                                   /* dwMinHwCodecStretch */
312    0,                                                                   /* dwMaxHwCodecStretch */
313    0,                                                                   /* dwReserved1 */
314    0,                                                                   /* dwReserved2 */
315    0,                                                                   /* dwReserved2 */
316    0,                                                                   /* dwSVBCaps */
317    0,                                                                   /* dwSVBCKeyCaps */
318    0,                                                                   /* dwSVBFXCaps */
319    {0},                                                                 /* dwSVBRops */
320    0,                                                                   /* dwVSBCaps */
321    0,                                                                   /* dwVSBCKeyCaps */
322    0,                                                                   /* dwVSBFXCaps */
323    {0},                                                                 /* dwVSBRops */
324    0,                                                                   /* dwSSBCaps */
325    0,                                                                   /* dwSSBCKeyCaps */
326    0,                                                                   /* dwSSBFXCaps */
327    {0},                                                                 /* dwSSBRops */
328    0,                                                                   /* dwMaxVideoPorts */
329    0,                                                                   /* dwCurrVideoPorts */
330    0                                                                    /* dwSVBCaps */
331   },
332   0,    /* dwMonitorFrequency */
333   X11DRV_DDHAL_GetDriverInfo,
334   0,    /* dwModeIndex */
335   NULL, /* lpdwFourCC */
336   0,    /* dwNumModes */
337   NULL, /* lpModeInfo */
338   DDHALINFO_ISPRIMARYDISPLAY | DDHALINFO_MODEXILLEGAL | DDHALINFO_GETDRIVERINFOSET, /* dwFlags */
339   &x11device,
340   0,    /* hInstance */
341   0,    /* lpD3DGlobalDriverData */
342   0,    /* lpD3DHALCallbacks */
343   NULL  /* lpDDExeBufCallbacks */
344 };
345
346 static LPDDHALDDRAWFNS ddraw_fns;
347 static DWORD ddraw_ver;
348
349 static void X11DRV_DDHAL_SetInfo(void)
350 {
351   (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
352 }
353
354 INT X11DRV_DCICommand(INT cbInput, const DCICMD *lpCmd, LPVOID lpOutData)
355 {
356   TRACE("(%d,(%ld,%ld,%ld),%p)\n", cbInput, lpCmd->dwCommand,
357         lpCmd->dwParam1, lpCmd->dwParam2, lpOutData);
358
359   switch (lpCmd->dwCommand) {
360   case DDNEWCALLBACKFNS:
361     ddraw_fns = (LPDDHALDDRAWFNS)lpCmd->dwParam1;
362     return TRUE;
363   case DDVERSIONINFO:
364     {
365       LPDDVERSIONDATA lpVer = (LPDDVERSIONDATA)lpOutData;
366       ddraw_ver = lpCmd->dwParam1;
367       if (!lpVer) break;
368       /* well, whatever... the DDK says so */
369       lpVer->dwHALVersion = DD_RUNTIME_VERSION;
370     }
371     return TRUE;
372   case DDGET32BITDRIVERNAME:
373     {
374       LPDD32BITDRIVERDATA lpData = (LPDD32BITDRIVERDATA)lpOutData;
375       /* here, we could ask ddraw to load a separate DLL, that
376        * would contain the 32-bit ddraw HAL */
377       strcpy(lpData->szName,"x11drv");
378       /* the entry point named here should initialize our hal_info
379        * with 32-bit entry points (ignored for now) */
380       strcpy(lpData->szEntryPoint,"DriverInit");
381       lpData->dwContext = 0;
382     }
383     return TRUE;
384   case DDCREATEDRIVEROBJECT:
385     {
386       LPDWORD lpInstance = (LPDWORD)lpOutData;
387
388       /* FIXME: get x11drv's hInstance */
389 #ifdef HAVE_LIBXXF86DGA2
390       if (!X11DRV_XF86DGA2_CreateDriver(&hal_info))
391 #endif
392       {
393           X11DRV_Settings_CreateDriver(&hal_info);
394       }
395 #ifdef HAVE_OPENGL
396       /*X11DRV_GLX_CreateDriver(&hal_info);*/
397 #endif
398
399       (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
400       *lpInstance = hal_info.hInstance;
401     }
402     return TRUE;
403   }
404   return 0;
405 }
406
407 void X11DRV_DDHAL_SwitchMode(DWORD dwModeIndex, LPVOID fb_addr, LPVIDMEM fb_mem)
408 {
409   LPDDHALMODEINFO info = &hal_info.lpModeInfo[dwModeIndex];
410
411   hal_info.dwModeIndex        = dwModeIndex;
412   hal_info.dwMonitorFrequency = info->wRefreshRate;
413   hal_info.vmiData.fpPrimary  = (FLATPTR)fb_addr;
414   hal_info.vmiData.dwDisplayWidth  = info->dwWidth;
415   hal_info.vmiData.dwDisplayHeight = info->dwHeight;
416   hal_info.vmiData.lDisplayPitch   = info->lPitch;
417   hal_info.vmiData.ddpfDisplay.dwSize = info->dwBPP ? sizeof(hal_info.vmiData.ddpfDisplay) : 0;
418   hal_info.vmiData.ddpfDisplay.dwFlags = (info->wFlags & DDMODEINFO_PALETTIZED) ? DDPF_PALETTEINDEXED8 : 0;
419   hal_info.vmiData.ddpfDisplay.u1.dwRGBBitCount = (info->dwBPP > 24) ? 24 : info->dwBPP;
420   hal_info.vmiData.ddpfDisplay.u2.dwRBitMask = info->dwRBitMask;
421   hal_info.vmiData.ddpfDisplay.u3.dwGBitMask = info->dwGBitMask;
422   hal_info.vmiData.ddpfDisplay.u4.dwBBitMask = info->dwBBitMask;
423   hal_info.vmiData.dwNumHeaps = fb_mem ? 1 : 0;
424   hal_info.vmiData.pvmList = fb_mem;
425
426   X11DRV_DDHAL_SetInfo();
427 }
428
429 void X11DRV_DDHAL_SetPalEntries(Colormap pal, DWORD dwBase, DWORD dwNumEntries,
430                                 LPPALETTEENTRY lpEntries)
431 {
432   XColor c;
433   unsigned int n;
434
435   if (pal) {
436     wine_tsx11_lock();
437     c.flags = DoRed|DoGreen|DoBlue;
438     c.pixel = dwBase;
439     for (n=0; n<dwNumEntries; n++,c.pixel++) {
440       c.red   = lpEntries[n].peRed   << 8;
441       c.green = lpEntries[n].peGreen << 8;
442       c.blue  = lpEntries[n].peBlue  << 8;
443       XStoreColor(gdi_display, pal, &c);
444     }
445     XFlush(gdi_display); /* update display immediately */
446     wine_tsx11_unlock();
447   }
448 }