Fixed buffer overflow.
[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   /* FIXME: Replace with SetWindowLongPtrA when available */
112   X11DRV_DD_GrabOldProcedure = (WNDPROC)SetWindowLongA(X11DRV_DD_PrimaryWnd,
113                                                        GWL_WNDPROC, (LONG)GrabWndProc);
114
115   SendMessageA(X11DRV_DD_PrimaryWnd, X11DRV_DD_GrabMessage, grab ? 1 : 0, 0);
116
117   /* FIXME: Replace with SetWindowLongPtrA when available */
118   if(SetWindowLongA(X11DRV_DD_PrimaryWnd, GWL_WNDPROC,
119                     (LONG)X11DRV_DD_GrabOldProcedure) != (LONG)GrabWndProc)
120     ERR("Window procedure has been changed!\n");
121 }
122
123 static DWORD PASCAL X11DRV_DDHAL_DestroyDriver(LPDDHAL_DESTROYDRIVERDATA data)
124 {
125   data->ddRVal = DD_OK;
126   return DDHAL_DRIVER_HANDLED;
127 }
128
129 static DWORD PASCAL X11DRV_DDHAL_CreateSurface(LPDDHAL_CREATESURFACEDATA data)
130 {
131   if (data->lpDDSurfaceDesc->ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) {
132     X11DRV_DD_Primary = *data->lplpSList;
133     X11DRV_DD_PrimaryWnd = (HWND)X11DRV_DD_Primary->lpSurfMore->lpDDRAWReserved;
134     X11DRV_DD_PrimaryGbl = X11DRV_DD_Primary->lpGbl;
135     SetPrimaryDIB((HBITMAP)GET_LPDDRAWSURFACE_GBL_MORE(X11DRV_DD_PrimaryGbl)->hKernelSurface);
136     X11DRV_DD_UserClass = GlobalFindAtomA("WINE_DDRAW");
137     if (dxgrab) GrabPointer(TRUE);
138   }
139   data->ddRVal = DD_OK;
140   return DDHAL_DRIVER_NOTHANDLED;
141 }
142
143 static DWORD PASCAL X11DRV_DDHAL_CreatePalette(LPDDHAL_CREATEPALETTEDATA data)
144 {
145   FIXME("stub\n");
146   /* only makes sense to do anything if the X server is running at 8bpp,
147    * which few people do nowadays */
148   data->ddRVal = DD_OK;
149   return DDHAL_DRIVER_HANDLED;
150 }
151
152 static DDHAL_DDCALLBACKS hal_ddcallbacks = {
153   sizeof(DDHAL_DDCALLBACKS),
154   0x3ff, /* all callbacks are 32-bit */
155   X11DRV_DDHAL_DestroyDriver,
156   X11DRV_DDHAL_CreateSurface,
157   NULL, /* SetColorKey */
158   NULL, /* SetMode */
159   NULL, /* WaitForVerticalBlank */
160   NULL, /* CanCreateSurface */
161   X11DRV_DDHAL_CreatePalette,
162   NULL, /* GetScanLine */
163   NULL, /* SetExclusiveMode */
164   NULL  /* FlipToGDISurface */
165 };
166
167 static DWORD PASCAL X11DRV_DDHAL_DestroySurface(LPDDHAL_DESTROYSURFACEDATA data)
168 {
169   if (data->lpDDSurface == X11DRV_DD_Primary) {
170     if (dxgrab) GrabPointer(FALSE);
171     X11DRV_DD_Primary = NULL;
172     X11DRV_DD_PrimaryWnd = 0;
173     X11DRV_DD_PrimaryGbl = NULL;
174     SetPrimaryDIB(0);
175     X11DRV_DD_UserClass = 0;
176   }
177   data->ddRVal = DD_OK;
178   return DDHAL_DRIVER_HANDLED;
179 }
180
181 static DWORD PASCAL X11DRV_DDHAL_SetPalette(LPDDHAL_SETPALETTEDATA data)
182 {
183   if (data->lpDDPalette && data->lpDDPalette->u1.dwReserved1) {
184     if (data->lpDDSurface == X11DRV_DD_Primary) {
185       FIXME("stub\n");
186       /* we should probably find the ddraw window (maybe data->lpDD->lpExclusiveOwner->hWnd),
187        * and attach the palette to it
188        *
189        * Colormap pal = data->lpDDPalette->u1.dwReserved1;
190        */
191     }
192   }
193   data->ddRVal = DD_OK;
194   return DDHAL_DRIVER_HANDLED;
195 }
196
197 static DDHAL_DDSURFACECALLBACKS hal_ddsurfcallbacks = {
198   sizeof(DDHAL_DDSURFACECALLBACKS),
199   0x3fff, /* all callbacks are 32-bit */
200   X11DRV_DDHAL_DestroySurface,
201   NULL, /* Flip */
202   NULL, /* SetClipList */
203   NULL, /* Lock */
204   NULL, /* Unlock */
205   NULL, /* Blt */
206   NULL, /* SetColorKey */
207   NULL, /* AddAttachedSurface */
208   NULL, /* GetBltStatus */
209   NULL, /* GetFlipStatus */
210   NULL, /* UpdateOverlay */
211   NULL, /* SetOverlayPosition */
212   NULL, /* reserved4 */
213   X11DRV_DDHAL_SetPalette
214 };
215
216 static DWORD PASCAL X11DRV_DDHAL_DestroyPalette(LPDDHAL_DESTROYPALETTEDATA data)
217 {
218   Colormap pal = data->lpDDPalette->u1.dwReserved1;
219   if (pal)
220   {
221       wine_tsx11_lock();
222       XFreeColormap(gdi_display, pal);
223       wine_tsx11_unlock();
224   }
225   data->ddRVal = DD_OK;
226   return DDHAL_DRIVER_HANDLED;
227 }
228
229 static DWORD PASCAL X11DRV_DDHAL_SetPaletteEntries(LPDDHAL_SETENTRIESDATA data)
230 {
231   X11DRV_DDHAL_SetPalEntries(data->lpDDPalette->u1.dwReserved1,
232                              data->dwBase, data->dwNumEntries,
233                              data->lpEntries);
234   data->ddRVal = DD_OK;
235   return DDHAL_DRIVER_HANDLED;
236 }
237
238 static DDHAL_DDPALETTECALLBACKS hal_ddpalcallbacks = {
239   sizeof(DDHAL_DDPALETTECALLBACKS),
240   0x3, /* all callbacks are 32-bit */
241   X11DRV_DDHAL_DestroyPalette,
242   X11DRV_DDHAL_SetPaletteEntries
243 };
244
245 static X11DEVICE x11device = {
246   NULL
247 };
248
249 static DWORD PASCAL X11DRV_DDHAL_GetDriverInfo(LPDDHAL_GETDRIVERINFODATA data)
250 {
251   LPX11DRIVERINFO info = x11device.lpInfo;
252   while (info) {
253     if (IsEqualGUID(&data->guidInfo, info->lpGuid)) {
254       DWORD dwSize = info->dwSize;
255       data->dwActualSize = dwSize;
256       if (data->dwExpectedSize < dwSize) dwSize = data->dwExpectedSize;
257       memcpy(data->lpvData, info->lpvData, dwSize);
258       data->ddRVal = DD_OK;
259       return DDHAL_DRIVER_HANDLED;
260     }
261     info = info->lpNext;
262   }
263   data->ddRVal = DDERR_CURRENTLYNOTAVAIL;
264   return DDHAL_DRIVER_HANDLED;
265 }
266
267 static DDHALINFO hal_info = {
268   sizeof(DDHALINFO),
269   &hal_ddcallbacks,
270   &hal_ddsurfcallbacks,
271   &hal_ddpalcallbacks,
272   {     /* vmiData */
273    0     /* fpPrimary */
274   },
275   {     /* ddCaps (only stuff the HAL implements here) */
276    sizeof(DDCORECAPS),                                                  /* dwSize */
277    DDCAPS_GDI | DDCAPS_PALETTE,                                         /* dwCaps */
278    DDCAPS2_CERTIFIED | DDCAPS2_NONLOCALVIDMEM | DDCAPS2_NOPAGELOCKREQUIRED | DDCAPS2_CANRENDERWINDOWED |
279    DDCAPS2_WIDESURFACES | DDCAPS2_PRIMARYGAMMA | DDCAPS2_FLIPNOVSYNC,   /* dwCaps2 */
280    0,                                                                   /* dwCKeyCaps */
281    0,                                                                   /* dwFXCaps */
282    0,                                                                   /* dwFXAlphaCaps */
283    DDPCAPS_8BIT | DDPCAPS_PRIMARYSURFACE,                               /* dwPalCaps */
284    0,                                                                   /* dwSVCaps */
285    0,                                                                   /* dwAlphaBltConstBitDepths */
286    0,                                                                   /* dwAlphaBltPixelBitDepths */
287    0,                                                                   /* dwAlphaBltSurfaceBitDepths */
288    0,                                                                   /* dwAlphaOverlayBltConstBitDepths */
289    0,                                                                   /* dwAlphaOverlayBltPixelBitDepths */
290    0,                                                                   /* dwAlphaOverlayBltSurfaceBitDepths */
291    0,                                                                   /* dwZBufferBitDepths */
292    16*1024*1024,                                                        /* dwVidMemTotal */
293    16*1024*1024,                                                        /* dwVidMemFree */
294    0,                                                                   /* dwMaxVisibleOverlays */
295    0,                                                                   /* dwCurrVisibleOverlays */
296    0,                                                                   /* dwNumFourCCCodes */
297    0,                                                                   /* dwAlignBoundarySrc */
298    0,                                                                   /* dwAlignSizeSrc */
299    0,                                                                   /* dwAlignBoundaryDest */
300    0,                                                                   /* dwAlignSizeDest */
301    0,                                                                   /* dwAlignStrideAlign */
302    {0},                                                                 /* dwRops */
303    {                                                                    /* ddsCaps */
304     DDSCAPS_BACKBUFFER | DDSCAPS_FLIP | DDSCAPS_FRONTBUFFER |
305     DDSCAPS_OFFSCREENPLAIN | DDSCAPS_PALETTE | DDSCAPS_PRIMARYSURFACE |
306     DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE | DDSCAPS_VIDEOMEMORY |
307     DDSCAPS_VISIBLE | DDSCAPS_LOCALVIDMEM | DDSCAPS_NONLOCALVIDMEM      /* dwCaps */
308    },
309    0,                                                                   /* dwMinOverlayStretch */
310    0,                                                                   /* dwMaxOverlayStretch */
311    0,                                                                   /* dwMinLiveVideoStretch */
312    0,                                                                   /* dwMaxLiveVideoStretch */
313    0,                                                                   /* dwMinHwCodecStretch */
314    0,                                                                   /* dwMaxHwCodecStretch */
315    0,                                                                   /* dwReserved1 */
316    0,                                                                   /* dwReserved2 */
317    0,                                                                   /* dwReserved2 */
318    0,                                                                   /* dwSVBCaps */
319    0,                                                                   /* dwSVBCKeyCaps */
320    0,                                                                   /* dwSVBFXCaps */
321    {0},                                                                 /* dwSVBRops */
322    0,                                                                   /* dwVSBCaps */
323    0,                                                                   /* dwVSBCKeyCaps */
324    0,                                                                   /* dwVSBFXCaps */
325    {0},                                                                 /* dwVSBRops */
326    0,                                                                   /* dwSSBCaps */
327    0,                                                                   /* dwSSBCKeyCaps */
328    0,                                                                   /* dwSSBFXCaps */
329    {0},                                                                 /* dwSSBRops */
330    0,                                                                   /* dwMaxVideoPorts */
331    0,                                                                   /* dwCurrVideoPorts */
332    0                                                                    /* dwSVBCaps */
333   },
334   0,    /* dwMonitorFrequency */
335   X11DRV_DDHAL_GetDriverInfo,
336   0,    /* dwModeIndex */
337   NULL, /* lpdwFourCC */
338   0,    /* dwNumModes */
339   NULL, /* lpModeInfo */
340   DDHALINFO_ISPRIMARYDISPLAY | DDHALINFO_MODEXILLEGAL | DDHALINFO_GETDRIVERINFOSET, /* dwFlags */
341   &x11device,
342   0,    /* hInstance */
343   0,    /* lpD3DGlobalDriverData */
344   0,    /* lpD3DHALCallbacks */
345   NULL  /* lpDDExeBufCallbacks */
346 };
347
348 static LPDDHALDDRAWFNS ddraw_fns;
349 static DWORD ddraw_ver;
350
351 static void X11DRV_DDHAL_SetInfo(void)
352 {
353   (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
354 }
355
356 INT X11DRV_DCICommand(INT cbInput, const DCICMD *lpCmd, LPVOID lpOutData)
357 {
358   TRACE("(%d,(%ld,%ld,%ld),%p)\n", cbInput, lpCmd->dwCommand,
359         lpCmd->dwParam1, lpCmd->dwParam2, lpOutData);
360
361   switch (lpCmd->dwCommand) {
362   case DDNEWCALLBACKFNS:
363     ddraw_fns = (LPDDHALDDRAWFNS)lpCmd->dwParam1;
364     return TRUE;
365   case DDVERSIONINFO:
366     {
367       LPDDVERSIONDATA lpVer = (LPDDVERSIONDATA)lpOutData;
368       ddraw_ver = lpCmd->dwParam1;
369       if (!lpVer) break;
370       /* well, whatever... the DDK says so */
371       lpVer->dwHALVersion = DD_RUNTIME_VERSION;
372     }
373     return TRUE;
374   case DDGET32BITDRIVERNAME:
375     {
376       LPDD32BITDRIVERDATA lpData = (LPDD32BITDRIVERDATA)lpOutData;
377       /* here, we could ask ddraw to load a separate DLL, that
378        * would contain the 32-bit ddraw HAL */
379       strcpy(lpData->szName,"x11drv");
380       /* the entry point named here should initialize our hal_info
381        * with 32-bit entry points (ignored for now) */
382       strcpy(lpData->szEntryPoint,"DriverInit");
383       lpData->dwContext = 0;
384     }
385     return TRUE;
386   case DDCREATEDRIVEROBJECT:
387     {
388       LPDWORD lpInstance = (LPDWORD)lpOutData;
389
390       /* FIXME: get x11drv's hInstance */
391 #ifdef HAVE_LIBXXF86DGA2
392       if (!X11DRV_XF86DGA2_CreateDriver(&hal_info))
393 #endif
394       {
395           X11DRV_Settings_CreateDriver(&hal_info);
396       }
397 #ifdef HAVE_OPENGL
398       /*X11DRV_GLX_CreateDriver(&hal_info);*/
399 #endif
400
401       (ddraw_fns->lpSetInfo)(&hal_info, FALSE);
402       *lpInstance = hal_info.hInstance;
403     }
404     return TRUE;
405   }
406   return 0;
407 }
408
409 void X11DRV_DDHAL_SwitchMode(DWORD dwModeIndex, LPVOID fb_addr, LPVIDMEM fb_mem)
410 {
411   LPDDHALMODEINFO info = &hal_info.lpModeInfo[dwModeIndex];
412
413   hal_info.dwModeIndex        = dwModeIndex;
414   hal_info.dwMonitorFrequency = info->wRefreshRate;
415   hal_info.vmiData.fpPrimary  = (FLATPTR)fb_addr;
416   hal_info.vmiData.dwDisplayWidth  = info->dwWidth;
417   hal_info.vmiData.dwDisplayHeight = info->dwHeight;
418   hal_info.vmiData.lDisplayPitch   = info->lPitch;
419   hal_info.vmiData.ddpfDisplay.dwSize = info->dwBPP ? sizeof(hal_info.vmiData.ddpfDisplay) : 0;
420   hal_info.vmiData.ddpfDisplay.dwFlags = (info->wFlags & DDMODEINFO_PALETTIZED) ? DDPF_PALETTEINDEXED8 : 0;
421   hal_info.vmiData.ddpfDisplay.u1.dwRGBBitCount = (info->dwBPP > 24) ? 24 : info->dwBPP;
422   hal_info.vmiData.ddpfDisplay.u2.dwRBitMask = info->dwRBitMask;
423   hal_info.vmiData.ddpfDisplay.u3.dwGBitMask = info->dwGBitMask;
424   hal_info.vmiData.ddpfDisplay.u4.dwBBitMask = info->dwBBitMask;
425   hal_info.vmiData.dwNumHeaps = fb_mem ? 1 : 0;
426   hal_info.vmiData.pvmList = fb_mem;
427
428   X11DRV_DDHAL_SetInfo();
429 }
430
431 void X11DRV_DDHAL_SetPalEntries(Colormap pal, DWORD dwBase, DWORD dwNumEntries,
432                                 LPPALETTEENTRY lpEntries)
433 {
434   XColor c;
435   int n;
436
437   if (pal) {
438     wine_tsx11_lock();
439     c.flags = DoRed|DoGreen|DoBlue;
440     c.pixel = dwBase;
441     for (n=0; n<dwNumEntries; n++,c.pixel++) {
442       c.red   = lpEntries[n].peRed   << 8;
443       c.green = lpEntries[n].peGreen << 8;
444       c.blue  = lpEntries[n].peBlue  << 8;
445       XStoreColor(gdi_display, pal, &c);
446     }
447     XFlush(gdi_display); /* update display immediately */
448     wine_tsx11_unlock();
449   }
450 }