Removed a few dependencies on kernel32 functions.
[wine] / dlls / ddraw / dsurface / x11.c
1 /*              DirectDrawSurface Xlib implementation
2  *
3  * Copyright 1997-2000 Marcus Meissner
4  * Copyright 1998-2000 Lionel Ulmer (most of Direct3D stuff)
5  */
6 #include "config.h"
7 #include "winerror.h"
8
9 #include <unistd.h>
10 #include <assert.h>
11 #include <fcntl.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15
16 #include "options.h"
17 #include "debugtools.h"
18 #include "x11_private.h"
19 #include "bitmap.h"
20 #include "win.h"
21 #include "d3d.h"
22
23 #ifdef HAVE_OPENGL
24 /* for d3d texture stuff */
25 # include "mesa_private.h"
26 #endif
27
28 DEFAULT_DEBUG_CHANNEL(ddraw);
29
30 #define VISIBLE(x) (SDDSCAPS(x) & (DDSCAPS_VISIBLE|DDSCAPS_PRIMARYSURFACE))
31
32 #define DDPRIVATE(x) x11_dd_private *ddpriv = ((x11_dd_private*)(x)->d->private)
33 #define DPPRIVATE(x) x11_dp_private *dppriv = ((x11_dp_private*)(x)->private)
34 #define DSPRIVATE(x) x11_ds_private *dspriv = ((x11_ds_private*)(x)->private)
35
36 static BYTE Xlib_TouchData(LPVOID data)
37 {
38     /* this is a function so it doesn't get optimized out */
39     return *(BYTE*)data;
40 }
41
42 /******************************************************************************
43  *              IDirectDrawSurface methods
44  *
45  * Since DDS3 and DDS2 are supersets of DDS, we implement DDS3 and let
46  * DDS and DDS2 use those functions. (Function calls did not change (except
47  * using different DirectDrawSurfaceX version), just added flags and functions)
48  */
49 HRESULT WINAPI Xlib_IDirectDrawSurface4Impl_QueryInterface(
50     LPDIRECTDRAWSURFACE4 iface,REFIID refiid,LPVOID *obj
51 ) {
52     ICOM_THIS(IDirectDrawSurface4Impl,iface);
53
54     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
55     
56     /* All DirectDrawSurface versions (1, 2, 3 and 4) use
57      * the same interface. And IUnknown does that too of course.
58      */
59     if ( IsEqualGUID( &IID_IDirectDrawSurface4, refiid )        ||
60          IsEqualGUID( &IID_IDirectDrawSurface3, refiid )        ||
61          IsEqualGUID( &IID_IDirectDrawSurface2, refiid )        ||
62          IsEqualGUID( &IID_IDirectDrawSurface,  refiid )        ||
63          IsEqualGUID( &IID_IUnknown,            refiid )
64     ) {
65             *obj = This;
66             IDirectDrawSurface4_AddRef(iface);
67
68             TRACE("  Creating IDirectDrawSurface interface (%p)\n", *obj);
69             return S_OK;
70     }
71 #ifdef HAVE_OPENGL
72     if ( IsEqualGUID( &IID_IDirect3DTexture2, refiid ) ) {
73         /* Texture interface */
74         *obj = d3dtexture2_create(This);
75         IDirectDrawSurface4_AddRef(iface);
76         TRACE("  Creating IDirect3DTexture2 interface (%p)\n", *obj);
77         return S_OK;
78     }
79     if ( IsEqualGUID( &IID_IDirect3DTexture, refiid ) ) {
80         /* Texture interface */
81         *obj = d3dtexture_create(This);
82         IDirectDrawSurface4_AddRef(iface);
83         TRACE("  Creating IDirect3DTexture interface (%p)\n", *obj);
84         return S_OK;
85     }
86 #else
87     if ( IsEqualGUID( &IID_IDirect3DTexture2, refiid ) ||
88          IsEqualGUID( &IID_IDirect3DTexture, refiid )
89        )
90     {
91        ERR( "Cannot provide 3D support without OpenGL/Mesa installed\n" );
92     }
93 #endif /* HAVE_OPENGL */
94     FIXME("(%p):interface for IID %s NOT found!\n",This,debugstr_guid(refiid));
95     return OLE_E_ENUM_NOMORE;
96 }
97
98 HRESULT WINAPI Xlib_IDirectDrawSurface4Impl_Lock(
99     LPDIRECTDRAWSURFACE4 iface,LPRECT lprect,LPDDSURFACEDESC lpddsd,DWORD flags, HANDLE hnd
100 ) {
101     ICOM_THIS(IDirectDrawSurface4Impl,iface);
102     DSPRIVATE(This);
103     DDPRIVATE(This->s.ddraw);
104
105     /* DO NOT AddRef the surface! Lock/Unlock are NOT guaranteed to come in 
106      * matched pairs! - Marcus Meissner 20000509 */
107     TRACE("(%p)->Lock(%p,%p,%08lx,%08lx) ret=%p\n",This,lprect,lpddsd,flags,(DWORD)hnd,__builtin_return_address(0));
108     if (flags & ~(DDLOCK_WAIT|DDLOCK_READONLY|DDLOCK_WRITEONLY))
109         WARN("(%p)->Lock(%p,%p,%08lx,%08lx)\n",
110                      This,lprect,lpddsd,flags,(DWORD)hnd);
111
112     /* First, copy the Surface description */
113     *lpddsd = This->s.surface_desc;
114     TRACE("locked surface: height=%ld, width=%ld, pitch=%ld\n",
115           lpddsd->dwHeight,lpddsd->dwWidth,lpddsd->lPitch);
116
117     /* If asked only for a part, change the surface pointer */
118     if (lprect) {
119         TRACE(" lprect: %dx%d-%dx%d\n",
120                 lprect->top,lprect->left,lprect->bottom,lprect->right
121         );
122         if ((lprect->top < 0) ||
123             (lprect->left < 0) ||
124             (lprect->bottom < 0) ||
125             (lprect->right < 0)) {
126           ERR(" Negative values in LPRECT !!!\n");
127           return DDERR_INVALIDPARAMS;
128         }
129         lpddsd->u1.lpSurface=(LPVOID)((char*)This->s.surface_desc.u1.lpSurface+
130                 (lprect->top*This->s.surface_desc.lPitch) +
131                 lprect->left*GET_BPP(This->s.surface_desc));
132     } else
133         assert(This->s.surface_desc.u1.lpSurface);
134     /* wait for any previous operations to complete */
135 #ifdef HAVE_LIBXXSHM
136     if (dspriv->info.image && VISIBLE(This) && ddpriv->xshm_active) {
137 /*
138         int compl = InterlockedExchange( &(ddpriv->xshm_compl), 0 );
139         if (compl) X11DRV_EVENT_WaitShmCompletion( compl );
140 */
141         X11DRV_EVENT_WaitShmCompletions( ddpriv->drawable );
142     }
143 #endif
144
145     /* If part of a visible 'clipped' surface, copy what is seen on the
146        screen to the surface */
147     if ((dspriv->info.image && VISIBLE(This)) &&
148         (This->s.lpClipper)) {
149           HWND hWnd = ((IDirectDrawClipperImpl *) This->s.lpClipper)->hWnd;
150           WND *wndPtr = WIN_FindWndPtr(hWnd);
151           Drawable drawable = X11DRV_WND_GetXWindow(wndPtr);
152           int width = wndPtr->rectClient.right - wndPtr->rectClient.left;
153           int height = wndPtr->rectClient.bottom - wndPtr->rectClient.top;
154           /* Now, get the source / destination coordinates */
155           int dest_x = wndPtr->rectClient.left;
156           int dest_y = wndPtr->rectClient.top;
157
158           if (!drawable) { /* we are running in -desktop mode */
159               drawable = X11DRV_WND_GetXWindow(WIN_GetDesktop());
160               /* FIXME: not sure whether these are the right offsets */
161               dest_x+=wndPtr->rectWindow.left;
162               dest_y+=wndPtr->rectWindow.top;
163               WIN_ReleaseDesktop();
164           }
165
166           TSXGetSubImage(display, drawable, 0, 0, width, height, 0xFFFFFFFF,
167                        ZPixmap, dspriv->info.image, dest_x, dest_y);
168           
169           WIN_ReleaseWndPtr(wndPtr);
170     }
171     
172     return DD_OK;
173 }
174
175 static void Xlib_copy_surface_on_screen(IDirectDrawSurface4Impl* This) {
176   DSPRIVATE(This);
177   DDPRIVATE(This->s.ddraw);
178   Drawable drawable = ddpriv->drawable;
179   POINT adjust[2] = {{0, 0}, {0, 0}};
180   SIZE imgsiz;
181
182   /* Get XImage size */
183   imgsiz.cx = dspriv->info.image->width;
184   imgsiz.cy = dspriv->info.image->height;
185
186   if (This->s.lpClipper) {
187     HWND hWnd = ((IDirectDrawClipperImpl *) This->s.lpClipper)->hWnd;
188     SIZE csiz;
189     WND *wndPtr = WIN_FindWndPtr(hWnd);
190     drawable = X11DRV_WND_GetXWindow(wndPtr);
191     
192     MapWindowPoints(hWnd, 0, adjust, 2);
193
194     imgsiz.cx -= adjust[0].x;
195     imgsiz.cy -= adjust[0].y;
196     /* (note: the rectWindow here should be the X window's interior rect, in
197      *  case anyone thinks otherwise while rewriting managed mode) */
198     adjust[1].x -= wndPtr->rectWindow.left;
199     adjust[1].y -= wndPtr->rectWindow.top;
200     csiz.cx = wndPtr->rectClient.right - wndPtr->rectClient.left;
201     csiz.cy = wndPtr->rectClient.bottom - wndPtr->rectClient.top;
202     if (csiz.cx < imgsiz.cx) imgsiz.cx = csiz.cx;
203     if (csiz.cy < imgsiz.cy) imgsiz.cy = csiz.cy;
204     
205     TRACE("adjust: hwnd=%08x, surface %ldx%ld, drawable %ldx%ld\n", hWnd,
206           adjust[0].x, adjust[0].y,
207           adjust[1].x,adjust[1].y);
208     
209     WIN_ReleaseWndPtr(wndPtr);
210   }
211
212   if (!drawable) {
213     WND *tmpWnd = WIN_FindWndPtr(This->s.ddraw->d->window);
214     drawable = X11DRV_WND_GetXWindow(tmpWnd);
215     WIN_ReleaseWndPtr(tmpWnd);
216
217     /* We don't have a context for this window. Host off the desktop */
218     if( !drawable ) {
219         FIXME("Have to use Desktop Root Window??? Bummer.\n");
220         drawable = X11DRV_WND_GetXWindow(WIN_GetDesktop());
221         WIN_ReleaseDesktop();
222     }
223     ddpriv->drawable = drawable;
224   }
225   
226   if (This->s.ddraw->d->pixel_convert != NULL)
227     This->s.ddraw->d->pixel_convert(This->s.surface_desc.u1.lpSurface,
228                                    dspriv->info.image->data,
229                                    This->s.surface_desc.dwWidth,
230                                    This->s.surface_desc.dwHeight,
231                                    This->s.surface_desc.lPitch,
232                                    This->s.palette);
233
234   /* if the DIB section is in GdiMod state, we must
235    * touch the surface to get any updates from the DIB */
236   Xlib_TouchData(dspriv->info.image->data);
237 #ifdef HAVE_LIBXXSHM
238     if (ddpriv->xshm_active) {
239 /*
240         X11DRV_EVENT_WaitReplaceShmCompletion( &(ddpriv->xshm_compl), This->s.ddraw->d.drawable );
241 */
242         /* let WaitShmCompletions track 'em for now */
243         /* (you may want to track it again whenever you implement DX7's partial
244         * surface locking, where threads have concurrent access) */
245         X11DRV_EVENT_PrepareShmCompletion( ddpriv->drawable );
246         TSXShmPutImage(display,
247                        drawable,
248                        DefaultGCOfScreen(X11DRV_GetXScreen()),
249                        dspriv->info.image,
250                        adjust[0].x, adjust[0].y, adjust[1].x, adjust[1].y,
251                        imgsiz.cx, imgsiz.cy,
252                        True
253                        );
254         /* make sure the image is transferred ASAP */
255         TSXFlush(display);
256     } else
257 #endif
258         TSXPutImage(display,
259                     drawable,
260                     DefaultGCOfScreen(X11DRV_GetXScreen()),
261                     dspriv->info.image,
262                     adjust[0].x, adjust[0].y, adjust[1].x, adjust[1].y,
263                     imgsiz.cx, imgsiz.cy
264                     );
265 }
266
267 HRESULT WINAPI Xlib_IDirectDrawSurface4Impl_Unlock(
268     LPDIRECTDRAWSURFACE4 iface,LPVOID surface
269 ) {
270     ICOM_THIS(IDirectDrawSurface4Impl,iface);
271     DDPRIVATE(This->s.ddraw);
272     DSPRIVATE(This);
273     TRACE("(%p)->Unlock(%p)\n",This,surface);
274
275     /*if (!This->s.ddraw->d.paintable)
276         return DD_OK; */
277
278     /* Only redraw the screen when unlocking the buffer that is on screen */
279     if (dspriv->info.image && VISIBLE(This)) {
280         Xlib_copy_surface_on_screen(This);
281         if (This->s.palette) {
282             DPPRIVATE(This->s.palette);
283             if(dppriv->cm)
284                 TSXSetWindowColormap(display,ddpriv->drawable,dppriv->cm);
285         }
286     }
287     /* DO NOT Release the surface! Lock/Unlock are NOT guaranteed to come in 
288      * matched pairs! - Marcus Meissner 20000509 */
289     return DD_OK;
290 }
291
292 #ifdef HAVE_XVIDEO
293 static void Xlib_copy_overlay_on_screen(IDirectDrawSurface4Impl* This) {
294   DSPRIVATE(This);
295   DDPRIVATE(This->s.ddraw);
296   Drawable drawable = ddpriv->drawable;
297
298   if (!drawable) {
299     WND *tmpWnd = WIN_FindWndPtr(This->s.ddraw->d->window);
300     drawable = X11DRV_WND_GetXWindow(tmpWnd);
301     WIN_ReleaseWndPtr(tmpWnd);
302
303     /* We don't have a context for this window. Host off the desktop */
304     if( !drawable ) {
305         FIXME("Have to use Desktop Root Window??? Bummer.\n");
306         drawable = X11DRV_WND_GetXWindow(WIN_GetDesktop());
307         WIN_ReleaseDesktop();
308     }
309     ddpriv->drawable = drawable;
310   }
311
312 #ifdef HAVE_LIBXXSHM
313     if (ddpriv->xshm_active) {
314         /* let WaitShmCompletions track 'em for now */
315         /* (you may want to track it again whenever you implement DX7's partial
316         * surface locking, where threads have concurrent access) */
317         X11DRV_EVENT_PrepareShmCompletion( ddpriv->drawable );
318         TSXvShmPutImage(display, ddpriv->port_id, drawable, DefaultGCOfScreen(X11DRV_GetXScreen()),
319                         dspriv->info.overlay.image,
320                         dspriv->info.overlay.src_rect.left, dspriv->info.overlay.src_rect.top,
321                         dspriv->info.overlay.src_rect.right - dspriv->info.overlay.src_rect.left,
322                         dspriv->info.overlay.src_rect.bottom - dspriv->info.overlay.src_rect.top,
323                         dspriv->info.overlay.dst_rect.left, dspriv->info.overlay.dst_rect.top,
324                         dspriv->info.overlay.dst_rect.right - dspriv->info.overlay.dst_rect.left,
325                         dspriv->info.overlay.dst_rect.bottom - dspriv->info.overlay.dst_rect.top,
326                         True);
327         /* make sure the image is transferred ASAP */
328         TSXFlush(display);
329     } else
330 #endif
331       TSXvPutImage(display, ddpriv->port_id, drawable, DefaultGCOfScreen(X11DRV_GetXScreen()),
332                    dspriv->info.overlay.image,
333                    dspriv->info.overlay.src_rect.left, dspriv->info.overlay.src_rect.top,
334                    dspriv->info.overlay.src_rect.right - dspriv->info.overlay.src_rect.left,
335                    dspriv->info.overlay.src_rect.bottom - dspriv->info.overlay.src_rect.top,
336                    dspriv->info.overlay.dst_rect.left, dspriv->info.overlay.dst_rect.top,
337                    dspriv->info.overlay.dst_rect.right - dspriv->info.overlay.dst_rect.left,
338                    dspriv->info.overlay.dst_rect.bottom - dspriv->info.overlay.dst_rect.top);
339 }
340 #endif
341
342 HRESULT WINAPI Xlib_IDirectDrawSurface4Impl_Flip(
343     LPDIRECTDRAWSURFACE4 iface,LPDIRECTDRAWSURFACE4 flipto,DWORD dwFlags
344 ) {
345     ICOM_THIS(IDirectDrawSurface4Impl,iface);
346     XImage      *image;
347     DDPRIVATE(This->s.ddraw);
348     DSPRIVATE(This);
349     x11_ds_private      *fspriv;
350     LPBYTE      surf;
351     IDirectDrawSurface4Impl* iflipto=(IDirectDrawSurface4Impl*)flipto;
352
353     TRACE("(%p)->Flip(%p,%08lx)\n",This,iflipto,dwFlags);
354     if ((!This->s.ddraw->d->paintable) && (dspriv->is_overlay == FALSE))
355         return DD_OK;
356     
357     iflipto = _common_find_flipto(This,iflipto);
358     fspriv = (x11_ds_private*)iflipto->private;
359
360     /* We need to switch the lowlevel surfaces, for xlib this is: */
361     /* The surface pointer */
362     surf                                = This->s.surface_desc.u1.lpSurface;
363     This->s.surface_desc.u1.lpSurface   = iflipto->s.surface_desc.u1.lpSurface;
364     iflipto->s.surface_desc.u1.lpSurface        = surf;
365
366     /* the associated ximage
367
368        NOTE : for XVideo, the pointer to the XvImage is at the same position
369               in memory than the standard XImage. This means that this code
370               still works :-)
371     */
372     image               = dspriv->info.image;
373     dspriv->info.image  = fspriv->info.image;
374     fspriv->info.image  = image;
375
376     if (dspriv->opengl_flip) {
377 #ifdef HAVE_OPENGL
378       ENTER_GL();
379       glXSwapBuffers(display, ddpriv->drawable);
380       LEAVE_GL();
381 #endif
382     } else if (dspriv->is_overlay) {
383 #ifdef HAVE_XVIDEO
384       if (dspriv->info.overlay.shown)
385         Xlib_copy_overlay_on_screen(This);
386 #else
387       ERR("Why was this code activated WITHOUT XVideo support ?\n");
388 #endif
389     } else {
390 #ifdef HAVE_LIBXXSHM
391       if (ddpriv->xshm_active) {
392         /*
393            int compl = InterlockedExchange( &(ddpriv->xshm_compl), 0 );
394            if (compl) X11DRV_EVENT_WaitShmCompletion( compl );
395            */
396         X11DRV_EVENT_WaitShmCompletions( ddpriv->drawable );
397       }
398 #endif
399       Xlib_copy_surface_on_screen(This);
400       if (iflipto->s.palette) {
401         DPPRIVATE(iflipto->s.palette);
402         if (dppriv->cm)
403           TSXSetWindowColormap(display,ddpriv->drawable,dppriv->cm);
404       }
405     }
406     return DD_OK;
407 }
408
409 /* The IDirectDrawSurface4::SetPalette method attaches the specified
410  * DirectDrawPalette object to a surface. The surface uses this palette for all
411  * subsequent operations. The palette change takes place immediately.
412  */
413 HRESULT WINAPI Xlib_IDirectDrawSurface4Impl_SetPalette(
414     LPDIRECTDRAWSURFACE4 iface,LPDIRECTDRAWPALETTE pal
415 ) {
416     ICOM_THIS(IDirectDrawSurface4Impl,iface);
417     DDPRIVATE(This->s.ddraw);
418     IDirectDrawPaletteImpl* ipal=(IDirectDrawPaletteImpl*)pal;
419     x11_dp_private      *dppriv;
420     int i;
421
422     TRACE("(%p)->(%p)\n",This,ipal);
423
424     if (ipal == NULL) {
425         if( This->s.palette != NULL )
426             IDirectDrawPalette_Release((IDirectDrawPalette*)This->s.palette);
427         This->s.palette = ipal;
428         return DD_OK;
429     }
430     dppriv = (x11_dp_private*)ipal->private;
431
432     if (!dppriv->cm &&
433         (This->s.ddraw->d->screen_pixelformat.u.dwRGBBitCount<=8)
434     ) {
435         dppriv->cm = TSXCreateColormap(
436             display,
437             ddpriv->drawable,
438             DefaultVisualOfScreen(X11DRV_GetXScreen()),
439             AllocAll
440         );
441         if (!Options.managed)
442             TSXInstallColormap(display,dppriv->cm);
443
444         for (i=0;i<256;i++) {
445             XColor xc;
446
447             xc.red              = ipal->palents[i].peRed<<8;
448             xc.blue             = ipal->palents[i].peBlue<<8;
449             xc.green    = ipal->palents[i].peGreen<<8;
450             xc.flags    = DoRed|DoBlue|DoGreen;
451             xc.pixel    = i;
452             TSXStoreColor(display,dppriv->cm,&xc);
453         }
454         TSXInstallColormap(display,dppriv->cm);
455     }
456     /* According to spec, we are only supposed to 
457      * AddRef if this is not the same palette.
458      */
459     if ( This->s.palette != ipal ) {
460         if( ipal != NULL )
461             IDirectDrawPalette_AddRef( (IDirectDrawPalette*)ipal );
462         if( This->s.palette != NULL )
463             IDirectDrawPalette_Release( (IDirectDrawPalette*)This->s.palette );
464         This->s.palette = ipal; 
465         /* Perform the refresh, only if a palette was created */
466         if (dppriv->cm)
467           TSXSetWindowColormap(display,ddpriv->drawable,dppriv->cm);
468
469         if (This->s.hdc != 0) {
470             /* hack: set the DIBsection color map */
471             BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(This->s.DIBsection, BITMAP_MAGIC);
472             X11DRV_DIBSECTION *dib = (X11DRV_DIBSECTION *)bmp->dib;
473             dib->colorMap = This->s.palette ? This->s.palette->screen_palents : NULL;
474             GDI_ReleaseObj(This->s.DIBsection);
475         }
476     }
477     return DD_OK;
478 }
479
480 ULONG WINAPI Xlib_IDirectDrawSurface4Impl_Release(LPDIRECTDRAWSURFACE4 iface) {
481     ICOM_THIS(IDirectDrawSurface4Impl,iface);
482     DSPRIVATE(This);
483     DDPRIVATE(This->s.ddraw);
484
485     TRACE( "(%p)->() decrementing from %lu.\n", This, This->ref );
486     if (--(This->ref))
487         return This->ref;
488
489     IDirectDraw2_Release((IDirectDraw2*)This->s.ddraw);
490
491     /* This frees the program-side surface. In some cases it had been
492      * allocated with MEM_SYSTEM, so it does not get 'really' freed
493      */
494     VirtualFree(This->s.surface_desc.u1.lpSurface, 0, MEM_RELEASE);
495
496     /* Now free the XImages and the respective screen-side surfaces */
497     if (dspriv->info.image != NULL) {
498         if (dspriv->info.image->data != This->s.surface_desc.u1.lpSurface)
499             VirtualFree(dspriv->info.image->data, 0, MEM_RELEASE);
500 #ifdef HAVE_LIBXXSHM
501         if (ddpriv->xshm_active) {
502             TSXShmDetach(display, &(dspriv->shminfo));
503             if (This->s.surface_desc.ddsCaps.dwCaps & DDSCAPS_OVERLAY) {
504               TSXFree(dspriv->info.image);
505             } else {
506               TSXDestroyImage(dspriv->info.image);
507             }
508             shmdt(dspriv->shminfo.shmaddr);
509         } else 
510 #endif
511         {
512           if (This->s.surface_desc.ddsCaps.dwCaps & DDSCAPS_OVERLAY) {
513             TSXFree(dspriv->info.image);
514           } else {
515             /* normal X Image memory was never allocated by X, but always by 
516              * ourselves -> Don't let X free our imagedata.
517              */
518             dspriv->info.image->data = NULL;
519             TSXDestroyImage(dspriv->info.image);
520           }
521         }
522         dspriv->info.image = 0;
523     }
524
525     if (This->s.palette)
526         IDirectDrawPalette_Release((IDirectDrawPalette*)This->s.palette);
527
528     /* Free the DIBSection (if any) */
529     if (This->s.hdc != 0) {
530         /* hack: restore the original DIBsection color map */
531         BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(This->s.DIBsection, BITMAP_MAGIC);
532         X11DRV_DIBSECTION *dib = (X11DRV_DIBSECTION *)bmp->dib;
533         dib->colorMap = dspriv->oldDIBmap;
534         GDI_ReleaseObj(This->s.DIBsection);
535
536         SelectObject(This->s.hdc, This->s.holdbitmap);
537         DeleteDC(This->s.hdc);
538         DeleteObject(This->s.DIBsection);
539     }
540
541     /* Free the clipper if present */
542     if(This->s.lpClipper)
543         IDirectDrawClipper_Release(This->s.lpClipper);
544     HeapFree(GetProcessHeap(),0,This->private);
545     HeapFree(GetProcessHeap(),0,This);
546     return S_OK;
547 }
548
549 HRESULT WINAPI Xlib_IDirectDrawSurface4Impl_GetDC(LPDIRECTDRAWSURFACE4 iface,HDC* lphdc) {
550     ICOM_THIS(IDirectDrawSurface4Impl,iface);
551     DSPRIVATE(This);
552     int was_ok = This->s.hdc != 0;
553     HRESULT result = IDirectDrawSurface4Impl_GetDC(iface,lphdc);
554     if (This->s.hdc && !was_ok) {
555         /* hack: take over the DIBsection color map */
556         BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(This->s.DIBsection, BITMAP_MAGIC);
557         X11DRV_DIBSECTION *dib = (X11DRV_DIBSECTION *)bmp->dib;
558         dspriv->oldDIBmap = dib->colorMap;
559         dib->colorMap = This->s.palette ? This->s.palette->screen_palents : NULL;
560         GDI_ReleaseObj(This->s.DIBsection);
561     }
562     return result;
563 }
564
565 #ifdef HAVE_XVIDEO
566 typedef struct {
567   BOOL shown;
568   LPRECT src_rect;
569   LPRECT dst_rect;
570   LPDIRECTDRAWSURFACE dest_surface;
571 } UpdateOverlayEnumerate;
572
573 static HRESULT WINAPI enum_func(LPDIRECTDRAWSURFACE lpDDSurface,
574                                 LPDDSURFACEDESC lpDDSurfaceDesc,  
575                                 LPVOID lpContext) {
576   ICOM_THIS(IDirectDrawSurface4Impl,lpDDSurface);
577   DSPRIVATE(This);
578   UpdateOverlayEnumerate *ctx = (UpdateOverlayEnumerate *) lpContext;
579
580   if ((lpDDSurfaceDesc->ddsCaps.dwCaps) & DDSCAPS_BACKBUFFER) {
581     TRACE("Upgrading surface %p\n", lpDDSurface);
582
583     if (ctx->shown) {
584       dspriv->info.overlay.shown = TRUE;
585       dspriv->info.overlay.src_rect = *(ctx->src_rect);
586       dspriv->info.overlay.dst_rect = *(ctx->dst_rect);
587       dspriv->info.overlay.dest_surface = ctx->dest_surface;
588     } else {
589       dspriv->info.overlay.shown = FALSE;
590     }
591   }
592   
593   return DDENUMRET_OK;
594 }
595 #endif
596
597 HRESULT WINAPI Xlib_IDirectDrawSurface4Impl_UpdateOverlay(
598     LPDIRECTDRAWSURFACE4 iface, LPRECT lpSrcRect,
599     LPDIRECTDRAWSURFACE4 lpDDDestSurface, LPRECT lpDestRect, DWORD dwFlags,
600     LPDDOVERLAYFX lpDDOverlayFx
601 ) {
602   ICOM_THIS(IDirectDrawSurface4Impl,iface);
603 #ifdef HAVE_XVIDEO
604   DSPRIVATE(This);
605   DDPRIVATE(This->s.ddraw);
606
607   if (ddpriv->xvideo_active) {
608     TRACE("(%p)->(%p,%p,%p,0x%08lx,%p)\n", This,
609           lpSrcRect, lpDDDestSurface, lpDestRect, dwFlags, lpDDOverlayFx );
610
611     if (TRACE_ON(ddraw)) {
612       DPRINTF(" - dwFlags : ");
613       _dump_DDOVERLAY(dwFlags);
614       
615       if (lpSrcRect)  DPRINTF(" - src  rectangle :%dx%d-%dx%d\n",lpSrcRect->left,lpSrcRect->top,
616                               lpSrcRect->right,lpSrcRect->bottom);
617       if (lpDestRect) DPRINTF(" - dest rectangle :%dx%d-%dx%d\n",lpDestRect->left,lpDestRect->top,
618                               lpDestRect->right,lpDestRect->bottom);
619     }
620     
621     if (dwFlags & DDOVER_SHOW) {
622       UpdateOverlayEnumerate ctx;
623       
624       dwFlags &= ~DDOVER_SHOW;
625
626       if ((lpSrcRect == NULL) || (lpDestRect == NULL)) {
627         FIXME("This is NOT supported yet...\n");
628         return DD_OK;
629       }
630       
631       /* Set the shown BOOL to TRUE and update the rectangles */
632       dspriv->info.overlay.shown = TRUE;
633       dspriv->info.overlay.src_rect = *lpSrcRect;
634       dspriv->info.overlay.dst_rect = *lpDestRect;
635       dspriv->info.overlay.dest_surface = (LPDIRECTDRAWSURFACE) lpDDDestSurface;
636
637       /* Now the same for the backbuffers */
638       ctx.shown = TRUE;
639       ctx.src_rect = lpSrcRect;
640       ctx.dst_rect = lpDestRect;
641       ctx.dest_surface = (LPDIRECTDRAWSURFACE) lpDDDestSurface;
642
643       IDirectDrawSurface4Impl_EnumAttachedSurfaces(iface, &ctx, enum_func);
644     } else if (dwFlags & DDOVER_HIDE) {
645       UpdateOverlayEnumerate ctx;
646       
647       dwFlags &= ~DDOVER_HIDE;
648
649       /* Set the shown BOOL to FALSE for all overlays */
650       dspriv->info.overlay.shown = FALSE;
651       ctx.shown = FALSE;
652       IDirectDrawSurface4Impl_EnumAttachedSurfaces(iface, &ctx, enum_func);
653     }
654
655     if (dwFlags && TRACE_ON(ddraw)) {
656       WARN("Unsupported flags : ");
657       _dump_DDOVERLAY(dwFlags);   
658     }
659   } else
660 #endif
661     FIXME("(%p)->(%p,%p,%p,0x%08lx,%p) not supported without XVideo !\n", This,
662           lpSrcRect, lpDDDestSurface, lpDestRect, dwFlags, lpDDOverlayFx );  
663
664   return DD_OK;
665 }
666
667 ICOM_VTABLE(IDirectDrawSurface4) xlib_dds4vt = 
668 {
669     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
670     Xlib_IDirectDrawSurface4Impl_QueryInterface,
671     IDirectDrawSurface4Impl_AddRef,
672     Xlib_IDirectDrawSurface4Impl_Release,
673     IDirectDrawSurface4Impl_AddAttachedSurface,
674     IDirectDrawSurface4Impl_AddOverlayDirtyRect,
675     IDirectDrawSurface4Impl_Blt,
676     IDirectDrawSurface4Impl_BltBatch,
677     IDirectDrawSurface4Impl_BltFast,
678     IDirectDrawSurface4Impl_DeleteAttachedSurface,
679     IDirectDrawSurface4Impl_EnumAttachedSurfaces,
680     IDirectDrawSurface4Impl_EnumOverlayZOrders,
681     Xlib_IDirectDrawSurface4Impl_Flip,
682     IDirectDrawSurface4Impl_GetAttachedSurface,
683     IDirectDrawSurface4Impl_GetBltStatus,
684     IDirectDrawSurface4Impl_GetCaps,
685     IDirectDrawSurface4Impl_GetClipper,
686     IDirectDrawSurface4Impl_GetColorKey,
687     Xlib_IDirectDrawSurface4Impl_GetDC,
688     IDirectDrawSurface4Impl_GetFlipStatus,
689     IDirectDrawSurface4Impl_GetOverlayPosition,
690     IDirectDrawSurface4Impl_GetPalette,
691     IDirectDrawSurface4Impl_GetPixelFormat,
692     IDirectDrawSurface4Impl_GetSurfaceDesc,
693     IDirectDrawSurface4Impl_Initialize,
694     IDirectDrawSurface4Impl_IsLost,
695     Xlib_IDirectDrawSurface4Impl_Lock,
696     IDirectDrawSurface4Impl_ReleaseDC,
697     IDirectDrawSurface4Impl_Restore,
698     IDirectDrawSurface4Impl_SetClipper,
699     IDirectDrawSurface4Impl_SetColorKey,
700     IDirectDrawSurface4Impl_SetOverlayPosition,
701     Xlib_IDirectDrawSurface4Impl_SetPalette,
702     Xlib_IDirectDrawSurface4Impl_Unlock,
703     Xlib_IDirectDrawSurface4Impl_UpdateOverlay,
704     IDirectDrawSurface4Impl_UpdateOverlayDisplay,
705     IDirectDrawSurface4Impl_UpdateOverlayZOrder,
706     IDirectDrawSurface4Impl_GetDDInterface,
707     IDirectDrawSurface4Impl_PageLock,
708     IDirectDrawSurface4Impl_PageUnlock,
709     IDirectDrawSurface4Impl_SetSurfaceDesc,
710     IDirectDrawSurface4Impl_SetPrivateData,
711     IDirectDrawSurface4Impl_GetPrivateData,
712     IDirectDrawSurface4Impl_FreePrivateData,
713     IDirectDrawSurface4Impl_GetUniquenessValue,
714     IDirectDrawSurface4Impl_ChangeUniquenessValue
715 };