Change the callback declarations to a safer format.
[wine] / graphics / x11drv / bitmap.c
index de17e60..665bd6c 100644 (file)
@@ -2,29 +2,27 @@
  * X11DRV bitmap objects
  *
  * Copyright 1993 Alexandre Julliard
+ *           1999 Noel Borthwick
  */
 
 #include "config.h"
 
-#ifndef X_DISPLAY_MISSING
-
 #include "ts_xlib.h"
 #include "ts_xutil.h"
 
 #include <stdio.h>
 #include <stdlib.h>
 #include "gdi.h"
-#include "callback.h"
-#include "dc.h"
 #include "bitmap.h"
 #include "heap.h"
-#include "monitor.h"
-#include "debug.h"
-#include "xmalloc.h"
-#include "local.h"
+#include "debugtools.h"
 #include "x11drv.h"
+#include "wingdi.h"
+#include "windef.h"
 #include "wine/winuser16.h"
 
+DEFAULT_DEBUG_CHANNEL(x11drv);
+
   /* GCs used for B&W and color bitmap operations */
 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
 
@@ -48,12 +46,10 @@ BOOL X11DRV_BITMAP_Init(void)
        TSXFreePixmap( display, tmpPixmap );
     }
 
-    if (MONITOR_GetDepth(&MONITOR_PrimaryMonitor) != 1)
+    if (X11DRV_GetDepth() != 1)
     {
-       if ((tmpPixmap = TSXCreatePixmap(display, 
-                                        X11DRV_GetXRootWindow(),
-                                        1, 1,
-                                        MONITOR_GetDepth(&MONITOR_PrimaryMonitor))))
+       if ((tmpPixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
+                                        1, 1, X11DRV_GetDepth())))
        {
            BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
            TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
@@ -70,107 +66,49 @@ HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
                                       BITMAPOBJ * bmp )
 {
     HRGN hrgn;
-    HBITMAP prevHandle = dc->w.hBitmap;
-    X11DRV_PHYSBITMAP *pbitmap;
+    HBITMAP prevHandle = dc->hBitmap;
     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
 
 
-    if (!(dc->w.flags & DC_MEMORY)) return 0;
+    if (!(dc->flags & DC_MEMORY)) return 0;
 
-    if(!bmp->DDBitmap)
+    if(!bmp->physBitmap)
         if(!X11DRV_CreateBitmap(hbitmap))
            return 0;
 
-    if(bmp->DDBitmap->funcs != dc->funcs) {
-        WARN(x11drv, "Trying to select non-X11 DDB into an X11 dc\n");
+    if(bmp->funcs != dc->funcs) {
+        WARN("Trying to select non-X11 DDB into an X11 dc\n");
        return 0;
     }
 
-    pbitmap = bmp->DDBitmap->physBitmap;
-
-    dc->w.totalExtent.left   = 0;
-    dc->w.totalExtent.top    = 0;
-    dc->w.totalExtent.right  = bmp->bitmap.bmWidth;
-    dc->w.totalExtent.bottom = bmp->bitmap.bmHeight;
-
-    if (dc->w.hVisRgn)
-       SetRectRgn( dc->w.hVisRgn, 0, 0,
-                     bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
-    else
-    { 
-       hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
-       if (!hrgn) return 0;
-       dc->w.hVisRgn    = hrgn;
-    }
+    hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
+    if (!hrgn) return 0;
+
+    dc->totalExtent.left   = 0;
+    dc->totalExtent.top    = 0;
+    dc->totalExtent.right  = bmp->bitmap.bmWidth;
+    dc->totalExtent.bottom = bmp->bitmap.bmHeight;
 
-    physDev->drawable = pbitmap->pixmap;
-    dc->w.hBitmap     = hbitmap;
+    physDev->drawable = (Pixmap)bmp->physBitmap;
+    dc->hBitmap     = hbitmap;
+
+    SelectVisRgn16( dc->hSelf, hrgn );
+    DeleteObject( hrgn );
 
       /* Change GC depth if needed */
 
-    if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
+    if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
     {
        TSXFreeGC( display, physDev->gc );
        physDev->gc = TSXCreateGC( display, physDev->drawable, 0, NULL );
-       dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
+       TSXSetGraphicsExposures( display, physDev->gc, False );
+       dc->bitsPerPixel = bmp->bitmap.bmBitsPixel;
         DC_InitDC( dc );
     }
-    else CLIPPING_UpdateGCRegion( dc );  /* Just update GC clip region */
     return prevHandle;
 }
 
 
-/***********************************************************************
- *           XPutImage_wrapper
- *
- * Wrapper to call XPutImage with CALL_LARGE_STACK.
- */
-
-struct XPutImage_descr
-{
-    BITMAPOBJ *bmp;
-    XImage    *image;
-    INT      width;
-    INT      height;
-};
-
-static int XPutImage_wrapper( const struct XPutImage_descr *descr )
-{
-    return XPutImage( display,
-              ((X11DRV_PHYSBITMAP *)descr->bmp->DDBitmap->physBitmap)->pixmap,
-              BITMAP_GC(descr->bmp),
-              descr->image, 0, 0, 0, 0, descr->width, descr->height );
-}
-
-
-/***************************************************************************
- *
- *     X11DRV_AllocBitmap
- *
- * Allocate DDBitmap and physBitmap
- *
- */
-X11DRV_PHYSBITMAP *X11DRV_AllocBitmap( BITMAPOBJ *bmp )
-{
-    X11DRV_PHYSBITMAP *pbitmap;
-
-    if(!(bmp->DDBitmap = HeapAlloc(GetProcessHeap(), 0, sizeof(DDBITMAP)))) {
-        WARN(x11drv, "Can't alloc DDBITMAP\n");
-       return NULL;
-    }
-
-    if(!(pbitmap = HeapAlloc(GetProcessHeap(), 0,sizeof(X11DRV_PHYSBITMAP)))) {
-        WARN(x11drv, "Can't alloc X11DRV_PHYSBITMAP\n");
-        HeapFree(GetProcessHeap(), 0, bmp->DDBitmap);
-       return NULL;
-    }
-
-    bmp->DDBitmap->physBitmap = pbitmap;
-    bmp->DDBitmap->funcs = DRIVER_FindDriver( "DISPLAY" );
-    return pbitmap;
-}
-
-
 /****************************************************************************
  *
  *       X11DRV_CreateBitmap
@@ -183,65 +121,52 @@ X11DRV_PHYSBITMAP *X11DRV_AllocBitmap( BITMAPOBJ *bmp )
 
 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
 {
-    X11DRV_PHYSBITMAP *pbitmap;
     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
 
     if(!bmp) {
-        WARN(x11drv, "Bad bitmap handle %08x\n", hbitmap);
+        WARN("Bad bitmap handle %08x\n", hbitmap);
        return FALSE;
     }
 
       /* Check parameters */
-    if (bmp->bitmap.bmPlanes != 1) return 0;
+    if (bmp->bitmap.bmPlanes != 1)
+    {
+        GDI_ReleaseObj( hbitmap );
+        return 0;
+    }
     if ((bmp->bitmap.bmBitsPixel != 1) && 
-       (bmp->bitmap.bmBitsPixel != MONITOR_GetDepth(&MONITOR_PrimaryMonitor))) {
-        ERR(x11drv, "Trying to make bitmap with planes=%d, bpp=%d\n",
+       (bmp->bitmap.bmBitsPixel != X11DRV_GetDepth()))
+    {
+        ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
            bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
-        GDI_HEAP_UNLOCK( hbitmap );
+        GDI_ReleaseObj( hbitmap );
        return FALSE;
     }
 
-    TRACE(x11drv, "(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
+    TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
          bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
 
-    pbitmap = X11DRV_AllocBitmap( bmp );
-    if(!pbitmap) return FALSE;
-
       /* Create the pixmap */
-    pbitmap->pixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(), bmp->bitmap.bmWidth,
-                             bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
-    if (!pbitmap->pixmap) {
-        WARN(x11drv, "Can't create Pixmap\n");
-        HeapFree(GetProcessHeap(), 0, bmp->DDBitmap->physBitmap);
-        HeapFree(GetProcessHeap(), 0, bmp->DDBitmap);
-       GDI_HEAP_UNLOCK( hbitmap );
+    if (!(bmp->physBitmap = (void *)TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
+                                                    bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
+                                                    bmp->bitmap.bmBitsPixel)))
+    {
+        WARN("Can't create Pixmap\n");
+       GDI_ReleaseObj( hbitmap );
        return FALSE;
     }
+    bmp->funcs = &X11DRV_DC_Funcs;
 
     if (bmp->bitmap.bmBits) /* Set bitmap bits */
        X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
                           bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
                           DDB_SET );
 
-    GDI_HEAP_UNLOCK( hbitmap );
+    GDI_ReleaseObj( hbitmap );
     return TRUE;
 }
 
 
-/***********************************************************************
- *           X11DRV_BITMAP_GetXImage
- *
- * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
- */
-XImage *X11DRV_BITMAP_GetXImage( const BITMAPOBJ *bmp )
-{
-    return XGetImage( display,
-                     ((X11DRV_PHYSBITMAP *)bmp->DDBitmap->physBitmap)->pixmap,
-                     0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
-                     AllPlanes, ZPixmap );
-}
-
-
 /***********************************************************************
  *           X11DRV_GetBitmapBits
  * 
@@ -253,17 +178,12 @@ static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
 {
     LONG old_height, height;
     XImage *image;
-    LPBYTE tbuf;
-    int        h, w, pad;
+    LPBYTE tbuf, startline;
+    int        h, w;
 
-    TRACE(x11drv, "(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
-
-    pad = BITMAP_GetPadding(bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel);
-
-    if (pad == -1)
-        return 0;
+    TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
 
-    EnterCriticalSection( &X11DRV_CritSection );
+    wine_tsx11_lock();
 
     /* Hack: change the bitmap height temporarily to avoid */
     /*       getting unnecessary bitmap rows. */
@@ -271,18 +191,20 @@ static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
     old_height = bmp->bitmap.bmHeight;
     height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
 
-    image = (XImage *)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage, bmp );
-
+    image = XGetImage( display, (Pixmap)bmp->physBitmap,
+                       0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
+                       AllPlanes, ZPixmap );
     bmp->bitmap.bmHeight = old_height;
 
     /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
 
-    tbuf = buffer;
+    startline = buffer;
     switch (bmp->bitmap.bmBitsPixel)
     {
     case 1:
         for (h=0;h<height;h++)
         {
+           tbuf = startline;
             *tbuf = 0;
             for (w=0;w<bmp->bitmap.bmWidth;w++)
             {
@@ -291,32 +213,35 @@ static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
                 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
                 if ((w&7) == 7) ++tbuf;
             }
-            tbuf += pad;
+           startline += bmp->bitmap.bmWidthBytes;
         }
         break;
     case 4:
         for (h=0;h<height;h++)
         {
+           tbuf = startline;
             for (w=0;w<bmp->bitmap.bmWidth;w++)
             {
                 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
                else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
             }
-            tbuf += pad;
+           startline += bmp->bitmap.bmWidthBytes;
         }
         break;
     case 8:
         for (h=0;h<height;h++)
         {
+           tbuf = startline;
             for (w=0;w<bmp->bitmap.bmWidth;w++)
                 *tbuf++ = XGetPixel(image,w,h);
-            tbuf += pad;
+           startline += bmp->bitmap.bmWidthBytes;
         }
         break;
     case 15:
     case 16:
         for (h=0;h<height;h++)
         {
+           tbuf = startline;
             for (w=0;w<bmp->bitmap.bmWidth;w++)
             {
                long pixel = XGetPixel(image,w,h);
@@ -324,11 +249,13 @@ static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
                *tbuf++ = pixel & 0xff;
                *tbuf++ = (pixel>>8) & 0xff;
             }
+           startline += bmp->bitmap.bmWidthBytes;
         }
         break;
     case 24:
         for (h=0;h<height;h++)
         {
+           tbuf = startline;
             for (w=0;w<bmp->bitmap.bmWidth;w++)
             {
                long pixel = XGetPixel(image,w,h);
@@ -337,13 +264,14 @@ static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
                *tbuf++ = (pixel>> 8) & 0xff;
                *tbuf++ = (pixel>>16) & 0xff;
            }
-            tbuf += pad;
+           startline += bmp->bitmap.bmWidthBytes;
        }
         break;
 
     case 32:
         for (h=0;h<height;h++)
         {
+           tbuf = startline;
             for (w=0;w<bmp->bitmap.bmWidth;w++)
             {
                long pixel = XGetPixel(image,w,h);
@@ -353,15 +281,14 @@ static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
                *tbuf++ = (pixel>>16) & 0xff;
                *tbuf++ = (pixel>>24) & 0xff;
            }
-            tbuf += pad;
+           startline += bmp->bitmap.bmWidthBytes;
        }
         break;
     default:
-        FIXME(x11drv, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
+        FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
     }
     XDestroyImage( image );
-    LeaveCriticalSection( &X11DRV_CritSection );
-
+    wine_tsx11_unlock();
     return count;
 }
 
@@ -376,111 +303,111 @@ static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
  */
 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
 {
-    struct XPutImage_descr descr;
     LONG height;
     XImage *image;
-    LPBYTE sbuf;
-    int        w, h, pad;
+    LPBYTE sbuf, startline;
+    int        w, h;
 
-    TRACE(x11drv, "(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
+    TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
     
-    pad = BITMAP_GetPadding(bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel);
-
-    if (pad == -1) 
-        return 0;
-       
-    sbuf = (LPBYTE)bits;
-
     height = count / bmp->bitmap.bmWidthBytes;
 
-    EnterCriticalSection( &X11DRV_CritSection );
-    image = XCreateImage( display, DefaultVisualOfScreen(X11DRV_GetXScreen()),
-                          bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
+    wine_tsx11_lock();
+    image = XCreateImage( display, X11DRV_GetVisual(), bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
                           bmp->bitmap.bmWidth, height, 32, 0 );
-    image->data = (LPBYTE)xmalloc(image->bytes_per_line * height);
+    if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
+    {
+        WARN("No memory to create image data.\n");
+        XDestroyImage( image );
+        wine_tsx11_unlock();
+        return 0;
+    }
     
     /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
-    sbuf = (LPBYTE)bits;
+    
+    startline = bits;
+
     switch (bmp->bitmap.bmBitsPixel)
     {
     case 1:
         for (h=0;h<height;h++)
         {
+           sbuf = startline;
             for (w=0;w<bmp->bitmap.bmWidth;w++)
             {
                 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
                 if ((w&7) == 7)
                     sbuf++;
             }
-            sbuf += pad;
+            startline += bmp->bitmap.bmWidthBytes;
         }
         break;
     case 4:
         for (h=0;h<height;h++)
         {
+           sbuf = startline;
             for (w=0;w<bmp->bitmap.bmWidth;w++)
             {
                 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
                 else XPutPixel( image, w, h, *sbuf++ & 0xf );
             }
-            sbuf += pad;
+            startline += bmp->bitmap.bmWidthBytes;
         }
         break;
     case 8:
         for (h=0;h<height;h++)
         {
+           sbuf = startline;
             for (w=0;w<bmp->bitmap.bmWidth;w++)
                 XPutPixel(image,w,h,*sbuf++);
-            sbuf += pad;
+            startline += bmp->bitmap.bmWidthBytes;
         }
         break;
     case 15:
     case 16:
         for (h=0;h<height;h++)
         {
+           sbuf = startline;
             for (w=0;w<bmp->bitmap.bmWidth;w++)
             {
                 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
                 sbuf+=2;
             }
+           startline += bmp->bitmap.bmWidthBytes;
         }
         break;
-    case 24: 
+    case 24:
         for (h=0;h<height;h++)
         {
+           sbuf = startline;
             for (w=0;w<bmp->bitmap.bmWidth;w++)
             {
                 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
                 sbuf += 3;
             }
-            sbuf += pad;
+            startline += bmp->bitmap.bmWidthBytes;
         }
         break;
     case 32: 
         for (h=0;h<height;h++)
         {
+           sbuf = startline;
             for (w=0;w<bmp->bitmap.bmWidth;w++)
             {
                 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
                 sbuf += 4;
             }
-            sbuf += pad;
+           startline += bmp->bitmap.bmWidthBytes;
         }
         break;
     default:
-      FIXME(x11drv, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
+      FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
 
     }
-
-    descr.bmp    = bmp;
-    descr.image  = image;
-    descr.width  = bmp->bitmap.bmWidth;
-    descr.height = height;
-
-    CALL_LARGE_STACK( XPutImage_wrapper, &descr );
+    XPutImage( display, (Pixmap)bmp->physBitmap, BITMAP_GC(bmp),
+               image, 0, 0, 0, 0, bmp->bitmap.bmWidth, height );
     XDestroyImage( image ); /* frees image->data too */
-    LeaveCriticalSection( &X11DRV_CritSection );
-    
+    wine_tsx11_unlock();
     return count;
 }
 
@@ -492,7 +419,7 @@ LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
     LONG ret;
     if(!bmp) {
-        WARN(x11drv, "Bad bitmap handle %08x\n", hbitmap);
+        WARN("Bad bitmap handle %08x\n", hbitmap);
        return FALSE;
     }
 
@@ -501,11 +428,10 @@ LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
     else if(flags == DDB_SET)
         ret = X11DRV_SetBitmapBits(bmp, bits, count);
     else {
-        ERR(x11drv, "Unknown flags value %d\n", flags);
+        ERR("Unknown flags value %d\n", flags);
        ret = 0;
     }
-    
-    GDI_HEAP_UNLOCK( hbitmap );
+    GDI_ReleaseObj( hbitmap );
     return ret;
 }
 
@@ -514,15 +440,137 @@ LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
  */
 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
 {
-    X11DRV_PHYSBITMAP *pbitmap = bmp->DDBitmap->physBitmap;
+    TSXFreePixmap( display, (Pixmap)bmp->physBitmap );
+    bmp->physBitmap = NULL;
+    bmp->funcs = NULL;
+    return TRUE;
+}
 
-    TSXFreePixmap( display, pbitmap->pixmap );
+/**************************************************************************
+ *             X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
+ *
+ *  Allocates an HBITMAP which references the Pixmap passed in.
+ *  Note: This function makes the bitmap an owner of the Pixmap so subsequently
+ *  calling DeleteObject on this will free the Pixmap as well.
+ */
+HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
+{
+    HBITMAP hBmp = 0;
+    BITMAPOBJ *pBmp = NULL;
+    Window root;
+    int x,y;               /* Unused */
+    unsigned border_width; /* Unused */
+    unsigned int depth, width, height;
+
+    /* Get the Pixmap dimensions and bit depth */
+    if ( 0 == TSXGetGeometry(display, pixmap, &root, &x, &y, &width, &height,
+                             &border_width, &depth) )
+        goto END;
+
+    TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
+          width, height, depth);
+    
+    /*
+     * Create an HBITMAP with the same dimensions and BPP as the pixmap,
+     * and make it a container for the pixmap passed.
+     */
+    hBmp = CreateBitmap( width, height, 1, depth, NULL );
+
+    pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
+    
+    pBmp->funcs = &X11DRV_DC_Funcs;
+    pBmp->physBitmap = (void *)pixmap;
+    GDI_ReleaseObj( hBmp );
 
-    HeapFree( GetProcessHeap(), 0, bmp->DDBitmap->physBitmap );
-    HeapFree( GetProcessHeap(), 0, bmp->DDBitmap );
-    bmp->DDBitmap = NULL;
+END:
+    TRACE("\tReturning HBITMAP %x\n", hBmp);
+    return hBmp;
+}
 
-    return TRUE;
+
+/**************************************************************************
+ *             X11DRV_BITMAP_CreateBitmapFromPixmap
+ *
+ *  Allocates an HBITMAP and copies the Pixmap data into it.
+ *  If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
+ */
+HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
+{
+    HBITMAP hBmp = 0, hBmpCopy = 0;
+    BITMAPOBJ *pBmp = NULL;
+    unsigned int width, height;
+
+    /* Allocate an HBITMAP which references the Pixmap passed to us */
+    hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
+    if (!hBmp)
+    {
+        TRACE("\tCould not create bitmap header for Pixmap\n");
+        goto END;
+    }
+
+    /* Get the bitmap dimensions */
+    width = pBmp->bitmap.bmWidth;
+    height = pBmp->bitmap.bmHeight;
+                 
+    hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
+
+    /* We can now get rid of the HBITMAP wrapper we created earlier.
+     * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
+     */
+    if (!bDeletePixmap)
+    {
+        /* Manually clear the bitmap internals to prevent the Pixmap 
+         * from being deleted by DeleteObject.
+         */
+        pBmp->physBitmap = NULL;
+        pBmp->funcs = NULL;
+    }
+    DeleteObject(hBmp);  
+
+END:
+    TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
+    return hBmpCopy;
+}
+
+
+/**************************************************************************
+ *                X11DRV_BITMAP_CreatePixmapFromBitmap
+ *
+ *    Creates a Pixmap from a bitmap
+ */
+Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
+{
+    HGLOBAL hPackedDIB = 0;
+    Pixmap pixmap = 0;
+
+    /*
+     * Create a packed DIB from the bitmap passed to us.
+     * A packed DIB contains a BITMAPINFO structure followed immediately by
+     * an optional color palette and the pixel data.
+     */
+    hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
+
+    /* Create a Pixmap from the packed DIB */
+    pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
+
+    /* Free the temporary packed DIB */
+    GlobalFree(hPackedDIB);
+
+    return pixmap;
+}
+
+
+/***********************************************************************
+ *           X11DRV_BITMAP_Pixmap
+ *
+ * This function exists solely for x11 driver of the window system.
+ */
+Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
+{
+    Pixmap pixmap;
+    BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
+    pixmap = (Pixmap)bmp->physBitmap;
+    GDI_ReleaseObj( hbitmap );
+    return pixmap;
 }
 
-#endif /* !defined(X_DISPLAY_MISSING) */