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