diff --git a/lib/modules/Protocols.pmod/X.pmod/Extensions.pmod b/lib/modules/Protocols.pmod/X.pmod/Extensions.pmod
index 274759bec62defbbbd6a50357d25a18a739b72f5..040e9cf13754b32642374f3902d31937aabf9caa 100644
--- a/lib/modules/Protocols.pmod/X.pmod/Extensions.pmod
+++ b/lib/modules/Protocols.pmod/X.pmod/Extensions.pmod
@@ -46,7 +46,6 @@ class Shape
 
   void post_init()
   {
-    werror("Shape extension initiated!\n");
   }
 
   void ShapeRectangles( object window, int xo, int yo,
diff --git a/lib/modules/Protocols.pmod/X.pmod/Requests.pmod b/lib/modules/Protocols.pmod/X.pmod/Requests.pmod
index bfd5a46beb9c686549b171bb0055d42cd3f3cbe4..c5dcba9208c8f435bd54ea14a68c75a90f18b552 100644
--- a/lib/modules/Protocols.pmod/X.pmod/Requests.pmod
+++ b/lib/modules/Protocols.pmod/X.pmod/Requests.pmod
@@ -53,6 +53,65 @@ class request
   }
 }
 
+class FreeRequest
+{
+  inherit request;
+  int id;
+  
+  string to_string()
+  {
+    return build_request(sprintf("%4c", id));
+  }
+}
+
+
+class CloseFont
+{
+  inherit FreeRequest;
+  constant reqType=46;
+}
+
+class FreePixmap
+{
+  inherit FreeRequest;
+  constant reqType=54;
+}
+
+class FreeGC
+{
+  inherit FreeRequest;
+  constant reqType=60;
+}
+
+class FreeColormap
+{
+  inherit FreeRequest;
+  constant reqType=79;
+}
+
+
+class FreeCursor
+{
+  inherit FreeRequest;
+  constant reqType=95;
+}
+
+class FreeColors
+{
+  inherit request;
+  constant reqType=79;
+  array colors;
+  int colormap;
+  int plane_mask;
+
+  string to_string()
+  {
+    return sprintf("%4c%4c%{%4c%}", colormap,plane_mask,colors);
+  }
+}
+
+
+
 class ResourceReq
 {
   inherit request;
@@ -489,12 +548,6 @@ class OpenFont
   }
 }
 
-class CloseFont
-{
-  inherit ResourceReq;
-  constant reqType = 46;
-}
-
 class QueryTextExtents
 {
   inherit request;
diff --git a/lib/modules/Protocols.pmod/X.pmod/Types.pmod b/lib/modules/Protocols.pmod/X.pmod/Types.pmod
index aee87ade667f086dbfac1a8cc3476b6ece57055a..49f8a41a953230bc56b93e7e9ba2219d0a129c96 100644
--- a/lib/modules/Protocols.pmod/X.pmod/Types.pmod
+++ b/lib/modules/Protocols.pmod/X.pmod/Types.pmod
@@ -8,6 +8,7 @@ class XResource
 {
   object display;
   int id;
+  int autofree = 1;
 
   void create(object d, int i)
   {
@@ -16,6 +17,20 @@ class XResource
 
     display->remember_id(id, this_object());
   }
+
+  void Free()
+  {
+    object req = this_object()->FreeRequest();
+    werror("free "+id+"\n");
+    req->id = id;
+    display->send_request( req );
+  }
+
+  void destroy()
+  {
+    if(autofree) Free();
+  }
+
 }
 
 class Font
@@ -66,6 +81,7 @@ class Font
 class Cursor
 {
   inherit XResource;
+  constant FreeRequest = Requests.FreeCursor;
 }
 
 class Visual
@@ -80,11 +96,18 @@ class Visual
   int redMask;
   int greenMask;
   int blueMask;
+
+  void create(mixed ... args)
+  {
+    ::create(@args);
+    autofree=0;
+  }
 }
 
 class GC
 {
   inherit XResource;
+  constant FreeRequest = Requests.FreeGC;
 
   mapping(string:mixed) values;
 
@@ -139,7 +162,7 @@ class Rectangle
 	error("Types.Rectangle(): To many arguments.\n");
       }
   }
-  
+
   string to_string()
   {
     return sprintf("%2c%2c%2c%2c", x, y, width, height);
@@ -176,8 +199,31 @@ class Colormap
 {
   inherit XResource;
   object visual;
+  constant FreeRequest = Requests.FreeColormap;
   mapping alloced = ([]);
 
+  object FreeColors_req(int pixel)
+  {
+    object req = Requests.FreeColors();
+    req->colormap = id;
+    req->plane_mask=0;
+    req->colors = ({ pixel });
+    return req;
+  }
+
+  void FreeColor( int pixel )
+  {
+    display->send_request( FreeColors_req( pixel ) );
+  }
+
+  void Free()
+  {
+    foreach(values(alloced), mapping f) FreeColor( f->pixel );
+    ::Free();
+  }
+
+    
+
   int AllocColor(int r, int g, int b)
   {
     if(alloced[sprintf("%2c%2c%2c", r, g, b)])
@@ -205,8 +251,8 @@ class Colormap
 class Drawable
 {
   inherit XResource;
-
-  object colormap;
+  int depth;
+  object colormap, parent, visual;
 
   object CreateGC_req()
   {
@@ -243,9 +289,10 @@ class Drawable
   object CreatePixmap(int width, int height, int depth)
   {
     object req = CreatePixmap_req(width, height, depth);
-    display->send_request(req);
-    
-    return Pixmap(display, req->pid, this_object(),colormap);
+    display->send_request( req );
+    object p = Pixmap(display, req->pid, this_object(), colormap );
+    p->depth = depth;
+    return p;
   }
 
   object FillPoly_req(int gc, int shape, int coordMode, array(object) p)
@@ -362,8 +409,7 @@ class Drawable
 class Pixmap
 {
   inherit Drawable;
-
-  object parent;
+  constant FreeRequest = Requests.FreePixmap;
 
   // Init function.
   void create(mixed ... args)
@@ -377,14 +423,15 @@ class Pixmap
 class Window
 {
   inherit Drawable;
-  object visual, parent;
   int currentInputMask;
-
+  constant FreeRequest = lambda(){}; // FIXME!!
   mapping(string:array(function)) event_callbacks = ([ ]);
 
   int alt_gr, num_lock, shift, control, caps_lock;
   int meta, alt, super, hyper;
 
+  mapping attributes;
+
 #include "keysyms.h"
 
   void ReleaseKey( int sym )
@@ -565,11 +612,11 @@ class Window
 
   object CreateWindow(int x, int y, int width, int height,
 		      int border_width, mapping attributes,
-		      object|void visual, int|void depth,
+		      object|void visual, int|void d,
 		      int|void c_class)
   {
     object req = CreateWindow_req(x, y, width, height,
-				  border_width,depth,visual);
+				  border_width,d,visual);
     object c = colormap||parent->defaultColorMap;
 
     if (attributes)
@@ -581,8 +628,10 @@ class Window
     // object w = Window(display, req->wid);
     display->send_request(req);
     object w = new(display, req->wid, visual, this_object());
+    w->depth = d||depth||this_object()->rootDepth;
     w->colormap = c;
     w->currentInputMask = req->attributes->EventMask;
+    w->attributes = attributes;
     return w;
   }
     
@@ -614,6 +663,7 @@ class Window
 
   void ChangeAttributes(mapping m)
   {
+    attributes |= m;
     display->send_request(ChangeAttributes_req(m));
   }
 
@@ -645,20 +695,13 @@ class Window
     event_callbacks[type] = (event_callbacks[type] || ({ }) )
       + ({ f });
   }
-
-#if 0
-  function remove_event_callback(string type)
-  {
-    function f = event_callbacks[type];
-    m_delete(event_callbacks, type);
-  }
-#endif
   
   object SelectInput_req()
   {
     object req = Requests.ChangeWindowAttributes();
     req->window = id;
     req->attributes->EventMask = currentInputMask;
+    attributes->EventMask = currentInputMask;
     return req;
   }
 
@@ -896,6 +939,12 @@ class RootWindow
   int saveUnders;
   int rootDepth;
   mapping depths;
+
+  void create(mixed ... args)
+  {
+    ::create(@args);
+    autofree=0;
+  }
   
   object new(mixed ... args) /* Kludge */
   {
diff --git a/lib/modules/Protocols.pmod/X.pmod/XImage.pmod b/lib/modules/Protocols.pmod/X.pmod/XImage.pmod
index 55bd38049e0c07633248c581a8b29f89fde2e892..f35b1e99e229d33b23ed49234bafdc787fcb5e98 100644
--- a/lib/modules/Protocols.pmod/X.pmod/XImage.pmod
+++ b/lib/modules/Protocols.pmod/X.pmod/XImage.pmod
@@ -326,3 +326,51 @@ class PixmapImage
     set_drawable( p );
   }
 }
+
+object MakeShapeMask(object in, object alpha)
+{
+  object shape = in->CreatePixmap(alpha->xsize(),alpha->ysize(),1);
+  mapping f;
+  foreach(in->display->formats, f) if(f->depth == 1) break;
+  shape->PutImage( shape->CreateGC(), 1, 0, 0, alpha->xsize(), alpha->ysize(), 
+		   Image.X.encode_truecolor( alpha->invert(), 
+					     1, f->scanLinePad,
+					     !in->display->bitmapBitOrder, 
+					     1, 0, 0, 0, 0, 0), 0);
+  return shape;
+}
+
+void ShapedWindowImage(object in, object color, object alpha, int contour)
+{
+  object bgpm;
+  int width = color->xsize();
+  int height = color->ysize();
+
+  object bgpm = in->CreatePixmap(width, height, in->depth);
+  PixmapImage( bgpm )->set_image( color );
+  in->ChangeAttributes( (["BackPixmap":bgpm ]) );
+
+
+  mapping f;
+  object shape = in->CreatePixmap(alpha->xsize(),alpha->ysize(),1);
+  foreach(in->display->formats, f) if(f->depth == 1) break;
+  shape->PutImage( shape->CreateGC(), 1, 0, 0, alpha->xsize(), alpha->ysize(), 
+		   Image.X.encode_truecolor( alpha->invert(), 
+					     1, f->scanLinePad,
+					     !in->display->bitmapBitOrder, 
+					     1, 0, 0, 0, 0, 0), 0);
+  in->ShapeMask("both", 0, 0, "set", shape);
+  if(contour)
+  {
+    in->ShapeMask("bounding", -1, -1, "union", shape);
+    in->ShapeMask("bounding", -1, 1, "union", shape);
+    in->ShapeMask("bounding", 1, -1, "union", shape);
+    in->ShapeMask("bounding", 1, 1, "union", shape);
+    
+    in->ShapeMask("bounding", 1, 0, "union", shape);
+    in->ShapeMask("bounding", 0, 1, "union", shape);
+    in->ShapeMask("bounding", -1, 0, "union", shape);
+    in->ShapeMask("bounding", 0, -1, "union", shape);
+  }
+}
+                      
diff --git a/lib/modules/Protocols.pmod/X.pmod/Xlib.pmod b/lib/modules/Protocols.pmod/X.pmod/Xlib.pmod
index 2085da1486ebb27516eddb48bc79dae082d99f11..a43cc812997882ae81c0fa54e0db245b8cc2d210 100644
--- a/lib/modules/Protocols.pmod/X.pmod/Xlib.pmod
+++ b/lib/modules/Protocols.pmod/X.pmod/Xlib.pmod
@@ -347,6 +347,7 @@ class Display
 		object r = Types.RootWindow(this_object(), wid);
 		int cm = struct->get_uint(4);
 		r->colormap = r->defaultColorMap = Types.Colormap(this_object(), cm, 0);
+		r->colormap->autofree=0;
 		r->whitePixel = struct->get_uint(4);
 		r->blackPixel = struct->get_uint(4);
 		r->currentInputMask = struct->get_uint(4);
@@ -376,6 +377,7 @@ class Display
 		      {
 			int visualID = struct->get_uint(4);
 			object v = Types.Visual(this_object(), visualID);
+
 			v->depth = depth;
 			v->c_class = struct->get_uint(1);
 			v->bitsPerRGB = struct->get_uint(1);