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