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