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