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