ecere: gfx/gui: support P/NaCl platform. add new Pepper interface driver for targetin...
[sdk] / ecere / src / gfx / drivers / XDisplayDriver.ec
index 2bdc7e4..e4401ff 100644 (file)
@@ -2,7 +2,7 @@ namespace gfx::drivers;
 
 import "instance"
 
-#if (defined(__unix__) || defined(__APPLE__)) && !defined(ECERE_MINIGLX)
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(ECERE_MINIGLX) && !defined(__EMSCRIPTEN__) && !defined(__pnacl__)
 
 default:
 
@@ -21,8 +21,10 @@ default:
 
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
+#if !defined(__EMSCRIPTEN__) && !defined(__pnacl__)
 #include <X11/extensions/shape.h>
 #include <X11/extensions/Xrender.h>
+#endif
 #include <X11/extensions/XShm.h>
 #include <sys/ipc.h>
 #include <sys/shm.h>
@@ -70,6 +72,7 @@ class XSurface : struct
    bool opaqueText;
    int xOffset;
    bool writingText;
+   bool writingOutline;
 
    ColorAlpha foreground, background;
    bool opaque;
@@ -98,12 +101,12 @@ XRenderPictFormat * GetXRenderFormat(PixelFormat pixelFormat, bool alphaBlend)
          break;
       case pixelFormat888:
          format = XRenderFindStandardFormat(xGlobalDisplay, alphaBlend ? PictStandardARGB32 : PictStandardRGB24);
-         // printf("R: %d G: %d B: %d\n", format->direct.red, format->direct.green, format->direct.blue); 
+         // printf("R: %d G: %d B: %d\n", format->direct.red, format->direct.green, format->direct.blue);
          break;
       case pixelFormat555:
       {
          XRenderPictFormat info = { 0 };
-         
+
          info.depth = 16;
          info.type = PictTypeDirect;
          info.direct.red = 10;
@@ -112,7 +115,7 @@ XRenderPictFormat * GetXRenderFormat(PixelFormat pixelFormat, bool alphaBlend)
          info.direct.redMask   = 0x1F;
          info.direct.greenMask = 0x1F;
          info.direct.blueMask  = 0x1F;
-         
+
          format = XRenderFindFormat(xGlobalDisplay,
             /*PictFormatDepth|*/PictFormatType| PictFormatAlpha|PictFormatRed|PictFormatGreen|PictFormatBlue|
             PictFormatRedMask|PictFormatGreenMask|PictFormatBlueMask|PictFormatAlphaMask, &info, 0);
@@ -133,8 +136,8 @@ XRenderPictFormat * GetXRenderFormat(PixelFormat pixelFormat, bool alphaBlend)
          info.direct.blueMask  = 0x1F;
          info.direct.alphaMask = 0;
          info.id = 0xba;
-         
-         format = XRenderFindFormat(xGlobalDisplay, 
+
+         format = XRenderFindFormat(xGlobalDisplay,
             /*PictFormatDepth|*/PictFormatType|PictFormatAlpha|PictFormatRed|PictFormatGreen|PictFormatBlue|
             PictFormatRedMask|PictFormatGreenMask|PictFormatBlueMask|PictFormatAlphaMask, &info, 0);
          break;
@@ -204,7 +207,7 @@ static bool ClipStretchCoords(Surface surface, Bitmap src, int *dx, int *dy, int
    {
       *w = Abs(*w);
       *sw = Abs(*sw);
-      *flip = true; 
+      *flip = true;
    }
 
    s2dw=(float)*w / *sw;
@@ -273,16 +276,16 @@ static void PutBitmapMask(Pixmap mask, Bitmap bitmap) {
    XImage image = {0};
    GC maskGC = XCreateGC(xGlobalDisplay, mask, 0, null);
    uint wordWidth = (bitmap.width+31) >> 5;
-   
+
    uint x,y;
    uint32 *b = new0 uint32[wordWidth * bitmap.height];
    uint32 f = 1;
-   
+
    XSetGraphicsExposures(xGlobalDisplay, maskGC, False);
-   
+
    image.width = bitmap.width;
    image.height = bitmap.height;
-   
+
    image.format = XYBitmap;
    #ifdef __BIG_ENDIAN__
    image.byte_order = MSBFirst;
@@ -294,18 +297,18 @@ static void PutBitmapMask(Pixmap mask, Bitmap bitmap) {
    image.bitmap_pad = 32;
    image.depth = 1;
    image.bytes_per_line = wordWidth << 2;
-   
+
    image.data = (char*)b;
-   
+
    XInitImage(&image);
-   
+
    switch(bitmap.pixelFormat) {
       case pixelFormat4: {
-         
+
       } break;
       case pixelFormat8: {
          byte *p = (byte*)bitmap.picture;
-         
+
          for(y = 0; y<bitmap.height; y++, p+=bitmap.stride) {
             for(x = 0; x<bitmap.width; x++) {
                if(p[x])
@@ -349,23 +352,23 @@ static void PutBitmapMask(Pixmap mask, Bitmap bitmap) {
          }
       } break;
       case pixelFormatAlpha: {
-         
+
       } break;
       case pixelFormatText: {
-         
+
       } break;
       case pixelFormatRGBA: {
-         
+
       } break;
    }
-   
+
    XSetForeground(xGlobalDisplay, maskGC, 1);
    XSetBackground(xGlobalDisplay, maskGC, 0);
-   
+
    XPutImage(xGlobalDisplay, mask, maskGC, &image,
       0, 0, 0, 0, //coordinates
       bitmap.width, bitmap.height);
-   
+
    XFreeGC(xGlobalDisplay, maskGC);
    delete image.data;
 }
@@ -414,7 +417,7 @@ class XDisplayDriver : DisplayDriver
             if(xDisplay.shminfoShape.shmaddr != (void *)-1)
                shmdt(xDisplay.shminfoShape.shmaddr);
             shmctl(xDisplay.shminfoShape.shmid, IPC_RMID, 0);
-         }                  
+         }
          XDestroyImage(xDisplay.shapeImage);
          xDisplay.shapeImage = None;
       }
@@ -502,7 +505,7 @@ class XDisplayDriver : DisplayDriver
                   if(xDisplay.shminfoShape.shmaddr != (void *)-1)
                      shmdt(xDisplay.shminfoShape.shmaddr);
                   shmctl(xDisplay.shminfoShape.shmid, IPC_RMID, 0);
-               }                  
+               }
                XDestroyImage(xDisplay.shapeImage);
                xDisplay.shapeImage = None;
             }
@@ -517,6 +520,8 @@ class XDisplayDriver : DisplayDriver
                xDisplay.image = XShmCreateImage(xGlobalDisplay, xSystemVisual /*DefaultVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay))*/,
                   format->depth, ZPixmap, null, &xDisplay.shminfo, width, height);
             }
+            else if(display.useSharedMemory && !xSharedMemory)
+               printf("%s", $"Error: Requested shared memory but shared memory pixmaps are not supported by X server.\n");
 #endif
             if(xDisplay.image)
             {
@@ -538,7 +543,7 @@ class XDisplayDriver : DisplayDriver
                         xDisplay.bitmap.height = height;
                         xDisplay.bitmap.stride = xDisplay.image->bytes_per_line >> GetColorDepthShifts(display.pixelFormat);
                         xDisplay.bitmap.pixelFormat = display.pixelFormat;
-                        xDisplay.bitmap.picture = xDisplay.shminfo.shmaddr;
+                        xDisplay.bitmap.picture = (byte *)xDisplay.shminfo.shmaddr;
                         xDisplay.bitmap.size = width * height;
                         xDisplay.bitmap.sizeBytes = (uint)xDisplay.bitmap.size << GetColorDepthShifts(display.pixelFormat);
                      }
@@ -552,7 +557,7 @@ class XDisplayDriver : DisplayDriver
                      if(xDisplay.shminfo.shmaddr != (void *)-1)
                         shmdt(xDisplay.shminfo.shmaddr);
                      shmctl(xDisplay.shminfo.shmid, IPC_RMID, 0);
-                  }                  
+                  }
                   XDestroyImage(xDisplay.image);
                   xDisplay.image = None;
                }
@@ -587,7 +592,7 @@ class XDisplayDriver : DisplayDriver
                      if(xDisplay.shminfoShape.shmaddr != (void *)-1)
                         shmdt(xDisplay.shminfoShape.shmaddr);
                      shmctl(xDisplay.shminfoShape.shmid, IPC_RMID, 0);
-                  }                  
+                  }
                   XDestroyImage(xDisplay.shapeImage);
                   xDisplay.shapeImage = None;
                }
@@ -628,22 +633,25 @@ class XDisplayDriver : DisplayDriver
 
    void RestorePalette(Display display)
    {
-      XDisplay xDisplay = display.driverData;
+
    }
 
    void FreeBitmap(DisplaySystem displaySystem, Bitmap bitmap)
    {
       XBitmap xBitmap = bitmap.driverData;
-
-      if(xBitmap.pixmap)
-         XFreePixmap(xGlobalDisplay, xBitmap.pixmap);
-      if(xBitmap.mask)
-         XFreePixmap(xGlobalDisplay, xBitmap.mask);
-      if(xBitmap.picture)
-         XRenderFreePicture(xGlobalDisplay, xBitmap.picture);
-      if(xBitmap.maskPicture)
-         XRenderFreePicture(xGlobalDisplay, xBitmap.maskPicture);
-      delete xBitmap;
+      if(xBitmap)
+      {
+         if(xBitmap.pixmap)
+            XFreePixmap(xGlobalDisplay, xBitmap.pixmap);
+         if(xBitmap.mask)
+            XFreePixmap(xGlobalDisplay, xBitmap.mask);
+         if(xBitmap.picture)
+            XRenderFreePicture(xGlobalDisplay, xBitmap.picture);
+         if(xBitmap.maskPicture)
+            XRenderFreePicture(xGlobalDisplay, xBitmap.maskPicture);
+         delete xBitmap;
+         bitmap.driverData = null;
+      }
       ((subclass(DisplayDriver))class(LFBDisplayDriver)).FreeBitmap(displaySystem, bitmap);
    }
 
@@ -659,11 +667,11 @@ class XDisplayDriver : DisplayDriver
 
       //bitmap.Convert(null, pixelFormatRGBA, null);
       //bitmap.Convert(null, pixelFormat888, null);
-      xBitmap.pixmap = 
-         XCreatePixmap(xGlobalDisplay, confineWindow /*(X11Window)display.window*/, bitmap.width, bitmap.height, 
+      xBitmap.pixmap =
+         XCreatePixmap(xGlobalDisplay, confineWindow /*(X11Window)display.window*/, bitmap.width, bitmap.height,
             (bitmap.pixelFormat == pixelFormatAlpha) ? 8 : (bitmap.alphaBlend ? 32 : ((bitmap.pixelFormat == pixelFormat888) ? 24 : xSystemDepth)));
       if(bitmap.transparent)
-         xBitmap.mask = 
+         xBitmap.mask =
             XCreatePixmap(xGlobalDisplay, confineWindow /*(X11Window)display.window*/, bitmap.width, bitmap.height, 1);
 
       {
@@ -683,7 +691,7 @@ class XDisplayDriver : DisplayDriver
 
       image.width = bitmap.width;
       image.height = bitmap.height;
-      
+
       if(bitmap.pixelFormat == pixelFormatAlpha)
       {
          image.depth = 8;
@@ -710,7 +718,7 @@ class XDisplayDriver : DisplayDriver
       image.green_mask = 0x7E0;
       image.blue_mask  = 0x1F;
       */
-      
+
       /*
       image.red_mask   = 0x0000FF;
       image.green_mask = 0x00FF00;
@@ -718,7 +726,7 @@ class XDisplayDriver : DisplayDriver
       */
       // image.red_mask  = 0xFFFF0000;
 
-      image.data = bitmap.picture;
+      image.data = (char *)bitmap.picture;
       XInitImage(&image);
 
       if(bitmap.pixelFormat != pixelFormatAlpha && bitmap.alphaBlend)
@@ -737,12 +745,12 @@ class XDisplayDriver : DisplayDriver
          }
       }
 
-      XPutImage(xGlobalDisplay, (Pixmap)xBitmap.pixmap, gc, &image, 
+      XPutImage(xGlobalDisplay, (Pixmap)xBitmap.pixmap, gc, &image,
          0, 0, 0, 0, bitmap.width,bitmap.height);
 
       if(bitmap.transparent && !bitmap.alphaBlend && bitmap.pixelFormat != pixelFormatAlpha)
          PutBitmapMask(xBitmap.mask, bitmap);
-      
+
       XFreeGC(xGlobalDisplay, gc);
 
       if(!bitmap.keepData)
@@ -756,7 +764,7 @@ class XDisplayDriver : DisplayDriver
    void ReleaseSurface(Display display, Surface surface)
    {
       XSurface xSurface = surface.driverData;
-      XDisplay xDisplay = display.driverData;
+      XDisplay xDisplay = display ? display.driverData : null;
       XRectangle rectangle;
 
       if(xSurface.colorPicture)
@@ -764,10 +772,12 @@ class XDisplayDriver : DisplayDriver
 
       rectangle.x = 0;
       rectangle.y = 0;
-      rectangle.width = (uint16)display.width;
-      rectangle.height = (uint16)display.height;
-
-      XRenderSetPictureClipRectangles(xGlobalDisplay, xDisplay.picture, 0, 0, &rectangle, 1);
+      if(display)
+      {
+         rectangle.width = (uint16)display.width;
+         rectangle.height = (uint16)display.height;
+         XRenderSetPictureClipRectangles(xGlobalDisplay, xDisplay.picture, 0, 0, &rectangle, 1);
+      }
 
       if(xSurface.colorPixmap)
          XFreePixmap(xGlobalDisplay, xSurface.colorPixmap);
@@ -788,7 +798,7 @@ class XDisplayDriver : DisplayDriver
          surface.unclippedBox = surface.box = clip;
 
          xSurface.pixmap = xBitmap.pixmap;
-         
+
          result = true;
       }
       return result;
@@ -834,7 +844,6 @@ class XDisplayDriver : DisplayDriver
       XSurface xSurface = surface.driverData;
       XDisplay xDisplay = display.driverData;
       bool changed = false;
-      XRectangle rectangle;
       if(clip != null)
       {
          Box box = clip;
@@ -854,14 +863,14 @@ class XDisplayDriver : DisplayDriver
       }
       if(changed)
       {
-         XRectangle rectangle = 
+         XRectangle rectangle =
          {
             (short)(surface.box.left + surface.offset.x),
             (short)(surface.box.top + surface.offset.y),
             (short)(surface.box.right - surface.box.left + 1),
             (short)(surface.box.bottom - surface.box.top + 1)
          };
-         XSetClipRectangles(xGlobalDisplay, xDisplay.gc, 
+         XSetClipRectangles(xGlobalDisplay, xDisplay.gc,
             0,0, &rectangle, 1, YXBanded);
       }
    }
@@ -899,11 +908,11 @@ class XDisplayDriver : DisplayDriver
 
       if(display)
          xImage = XGetImage(xGlobalDisplay, xDisplay.pixmap, sx, sy, sw, sh, MAXDWORD, ZPixmap);
-      else 
+      else
          xImage = XGetImage(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), sx, sy, sw, sh, MAXDWORD, ZPixmap);
 
       source.pixelFormat = format;
-      source.picture = xImage->data;
+      source.picture = (byte *)xImage->data;
       source.width = sw;
       source.height = sh;
       source.stride = xImage->bytes_per_line / 4;
@@ -930,9 +939,8 @@ class XDisplayDriver : DisplayDriver
    void SetForeground(Display display, Surface surface, ColorAlpha color)
    {
       XSurface xSurface = surface.driverData;
-      XDisplay xDisplay = display.driverData;
-      XRenderColor renderColor = { color.color.r * color.a, color.color.g * color.a, color.color.b * color.a, color.a * 255};
-      X11Picture colorPicture;
+      XDisplay xDisplay = display ? display.driverData : null;
+      XRenderColor renderColor = { (uint16)(color.color.r * color.a), (uint16)(color.color.g * color.a), (uint16)(color.color.b * color.a), (uint16)(color.a * 255) };
 
       if(xSurface.colorPicture)
          XRenderFreePicture(xGlobalDisplay, xSurface.colorPicture);
@@ -956,23 +964,25 @@ class XDisplayDriver : DisplayDriver
       // xSurface.foreground = ARGB(A(color),B(color),G(color),R(color));
       xSurface.foreground = color;
 
-      XSetForeground(xGlobalDisplay, xDisplay.gc, (xSystemPixelFormat == pixelFormat888) ? xSurface.foreground : 
-         ((xSystemPixelFormat == pixelFormat565) ? ((Color565)xSurface.foreground) : ((Color555)xSurface.foreground)));
+      if(xDisplay)
+         XSetForeground(xGlobalDisplay, xDisplay.gc, (xSystemPixelFormat == pixelFormat888) ? xSurface.foreground :
+            ((xSystemPixelFormat == pixelFormat565) ? ((Color565)xSurface.foreground) : ((Color555)xSurface.foreground)));
    }
 
    void SetBackground(Display display, Surface surface, ColorAlpha color)
    {
       XSurface xSurface = surface.driverData;
-      XDisplay xDisplay = display.driverData;
+      XDisplay xDisplay = display ? display.driverData : null;
       // xSurface.background = ARGB(A(color),B(color),G(color),R(color));
       xSurface.background = color;
 
-      XSetBackground(xGlobalDisplay, xDisplay.gc, (xSystemPixelFormat == pixelFormat888) ? xSurface.background : 
-         ((xSystemPixelFormat == pixelFormat565) ? ((Color565)xSurface.background) : ((Color555)xSurface.background)));
+      if(xDisplay)
+         XSetBackground(xGlobalDisplay, xDisplay.gc, (xSystemPixelFormat == pixelFormat888) ? xSurface.background :
+            ((xSystemPixelFormat == pixelFormat565) ? ((Color565)xSurface.background) : ((Color555)xSurface.background)));
    }
 
    ColorAlpha GetPixel(Display display, Surface surface, int x, int y)
-   {          
+   {
       return 0;
    }
 
@@ -991,9 +1001,9 @@ class XDisplayDriver : DisplayDriver
 
    void DrawLine(Display display, Surface surface, int x1, int y1, int x2, int y2)
    {
-      XDisplay xDisplay = display.driverData;
+      XDisplay xDisplay = display ? display.driverData : null;
       XSurface xSurface = surface.driverData;
-      if(xSurface.foreground.a < 255)
+      if(!xDisplay || xSurface.foreground.a < 255)
       {
          XTrapezoid traps[3];
          int nTraps = 0;
@@ -1014,7 +1024,7 @@ class XDisplayDriver : DisplayDriver
                {
                   XDoubleToFixed(y1 - width + offset), XDoubleToFixed(y1 + width + offset),
                   { { XDoubleToFixed(Min(x1, x2) - 0 + offset), XDoubleToFixed(y1 - width + offset) }, { XDoubleToFixed(Min(x1, x2) + 0.0 + offset), XDoubleToFixed(y1 + width+ offset) } },
-                  { { XDoubleToFixed(Max(x1, x2) - 0 + offset), XDoubleToFixed(y1 - width + offset) }, { XDoubleToFixed(Max(x1, x2) + 0.0 + offset), XDoubleToFixed(y1 + width+ offset) } },
+                  { { XDoubleToFixed(Max(x1, x2) - 0 + offset), XDoubleToFixed(y1 - width + offset) }, { XDoubleToFixed(Max(x1, x2) + 0.0 + offset), XDoubleToFixed(y1 + width+ offset) } }
                };
                traps[0] = trap1;
                nTraps = 1;
@@ -1029,7 +1039,7 @@ class XDisplayDriver : DisplayDriver
                {
                   XDoubleToFixed(Min(y1, y2) - 0.0 + offset), XDoubleToFixed(Max(y1, y2) + 0.0 + offset),
                   { { XDoubleToFixed(x1 - width + offset), XDoubleToFixed(Min(y1, y2) - 0.0 + offset) }, { XDoubleToFixed(x1 - width + offset), XDoubleToFixed(Max(y1, y2) + 0.0 + offset) } },
-                  { { XDoubleToFixed(x1 + width + offset), XDoubleToFixed(Min(y1, y2) - 0.0 + offset) }, { XDoubleToFixed(x1 + width + offset), XDoubleToFixed(Max(y1, y2) + 0.0 + offset) } },
+                  { { XDoubleToFixed(x1 + width + offset), XDoubleToFixed(Min(y1, y2) - 0.0 + offset) }, { XDoubleToFixed(x1 + width + offset), XDoubleToFixed(Max(y1, y2) + 0.0 + offset) } }
                };
                traps[0] = trap1;
                nTraps = 1;
@@ -1081,13 +1091,13 @@ class XDisplayDriver : DisplayDriver
                   {
                      XDoubleToFixed(D.y + offset), XDoubleToFixed(F.y + offset),
                      { { XDoubleToFixed(D.x + offset), XDoubleToFixed(D.y + offset) }, { XDoubleToFixed(F.x + offset), XDoubleToFixed(F.y + offset) } },
-                     { { XDoubleToFixed(E.x + offset), XDoubleToFixed(E.y + offset) }, { XDoubleToFixed(F.x + offset), XDoubleToFixed(F.y + offset) } },
+                     { { XDoubleToFixed(E.x + offset), XDoubleToFixed(E.y + offset) }, { XDoubleToFixed(F.x + offset), XDoubleToFixed(F.y + offset) } }
                   };
                   traps[0] = trap1;
                   traps[1] = trap2;
                   traps[2] = trap3;
                   nTraps = 3;
-               }   
+               }
             }
             else
             {
@@ -1121,21 +1131,21 @@ class XDisplayDriver : DisplayDriver
                   traps[1] = trap2;
                   traps[2] = trap3;
                   nTraps = 3;
-               }   
+               }
             }
             /*
             printf("Line: (%d, %d)-(%d, %d)\n", x1,y1, x2,y2);
             printf("Line: A = (%.2f, %.2f), B = (%.2f, %.2f), C = (%.2f, %.2f)\n", A.x,A.y, B.x,B.y, C.x,C.y);
             printf("Line: D = (%.2f, %.2f), E = (%.2f, %.2f), F = (%.2f, %.2f)\n", D.x,D.y, E.x,E.y, F.x,F.y);
-            printf("Trap1: top = %.2f, bottom = %.2f, left = (%.2f, %.2f)-(%.2f, %.2f), right = (%.2f, %.2f)-(%.2f, %.2f)\n", 
+            printf("Trap1: top = %.2f, bottom = %.2f, left = (%.2f, %.2f)-(%.2f, %.2f), right = (%.2f, %.2f)-(%.2f, %.2f)\n",
                traps[0].top / 65536.0, traps[0].bottom / 65536.0,
                traps[0].left.p1.x / 65536.0, traps[0].left.p1.y / 65536.0, traps[0].left.p2.x / 65536.0, traps[0].left.p2.y / 65536.0,
                traps[0].right.p1.x / 65536.0, traps[0].right.p1.y / 65536.0, traps[0].right.p2.x / 65536.0, traps[0].right.p2.y / 65536.0);
-            printf("Trap2: top = %.2f, bottom = %.2f, left = (%.2f, %.2f)-(%.2f, %.2f), right = (%.2f, %.2f)-(%.2f, %.2f)\n", 
+            printf("Trap2: top = %.2f, bottom = %.2f, left = (%.2f, %.2f)-(%.2f, %.2f), right = (%.2f, %.2f)-(%.2f, %.2f)\n",
                traps[1].top / 65536.0, traps[1].bottom / 65536.0,
                traps[1].left.p1.x / 65536.0,  traps[1].left.p1.y / 65536.0,  traps[1].left.p2.x / 65536.0,  traps[1].left.p2.y / 65536.0,
                traps[1].right.p1.x / 65536.0, traps[1].right.p1.y / 65536.0, traps[1].right.p2.x / 65536.0, traps[1].right.p2.y / 65536.0);
-            printf("Trap3: top = %.2f, bottom = %.2f, left = (%.2f, %.2f)-(%.2f, %.2f), right = (%.2f, %.2f)-(%.2f, %.2f)\n", 
+            printf("Trap3: top = %.2f, bottom = %.2f, left = (%.2f, %.2f)-(%.2f, %.2f), right = (%.2f, %.2f)-(%.2f, %.2f)\n",
                traps[2].top / 65536.0, traps[2].bottom / 65536.0,
                traps[2].left.p1.x / 65536.0,  traps[2].left.p1.y / 65536.0,  traps[2].left.p2.x / 65536.0,  traps[2].left.p2.y / 65536.0,
                traps[2].right.p1.x / 65536.0, traps[2].right.p1.y / 65536.0, traps[2].right.p2.x / 65536.0, traps[2].right.p2.y / 65536.0);
@@ -1154,10 +1164,10 @@ class XDisplayDriver : DisplayDriver
 
    void Rectangle(Display display, Surface surface,int x1,int y1,int x2,int y2)
    {
-      XDisplay xDisplay = display.driverData;
+      XDisplay xDisplay = display ? display.driverData : null;
       XSurface xSurface = surface.driverData;
-      
-      if(xSurface.foreground.a < 255)
+
+      if(!xDisplay || xSurface.foreground.a < 255)
       {
          DrawLine(display, surface,x1,y1,x2-1,y1);
          DrawLine(display, surface,x2,y1,x2,y2-1);
@@ -1173,16 +1183,16 @@ class XDisplayDriver : DisplayDriver
 
    void Area(Display display, Surface surface,int x1,int y1,int x2,int y2)
    {
-      XDisplay xDisplay = display.driverData;
+      XDisplay xDisplay = display ? display.driverData : null;
       XSurface xSurface = surface.driverData;
-      if(xSurface.background.a < 255)
+      if(!xDisplay || xSurface.background.a < 255)
       {
          XRenderColor renderColor =
          {
             (uint16)(xSurface.background.color.r * xSurface.background.a),
             (uint16)(xSurface.background.color.g * xSurface.background.a),
             (uint16)(xSurface.background.color.b * xSurface.background.a),
-            (uint16)xSurface.background.a * 255
+            (uint16)(xSurface.background.a * 255)
          };
          x1 += surface.offset.x;
          y1 += surface.offset.y;
@@ -1192,22 +1202,22 @@ class XDisplayDriver : DisplayDriver
       }
       else
       {
-         XSetForeground(xGlobalDisplay, xDisplay.gc, (xSystemPixelFormat == pixelFormat888) ? xSurface.background : 
+         XSetForeground(xGlobalDisplay, xDisplay.gc, (xSystemPixelFormat == pixelFormat888) ? xSurface.background :
             ((xSystemPixelFormat == pixelFormat565) ? ((Color565)xSurface.background) : ((Color555)xSurface.background)));
          XFillRectangle(xGlobalDisplay, (Pixmap) xDisplay.pixmap, xDisplay.gc,
             x1 + surface.offset.x,
             y1 + surface.offset.y,
             x2 - x1 + 1, y2 - y1 + 1);
-         XSetForeground(xGlobalDisplay, xDisplay.gc, (xSystemPixelFormat == pixelFormat888) ? xSurface.foreground : 
+         XSetForeground(xGlobalDisplay, xDisplay.gc, (xSystemPixelFormat == pixelFormat888) ? xSurface.foreground :
             ((xSystemPixelFormat == pixelFormat565) ? ((Color565)xSurface.foreground) : ((Color555)xSurface.foreground)));
       }
    }
-   
+
    void Clear(Display display, Surface surface, ClearType flags)
    {
       if(flags != depthBuffer)
       {
-         XDisplay xDisplay = display.driverData;
+         // XDisplay xDisplay = display.driverData;
          XSurface xSurface = surface.driverData;
          if(xSurface.background.a < 255)
          {
@@ -1220,7 +1230,7 @@ class XDisplayDriver : DisplayDriver
                (uint16)(xSurface.background.color.r * xSurface.background.a),
                (uint16)(xSurface.background.color.g * xSurface.background.a),
                (uint16)(xSurface.background.color.b * xSurface.background.a),
-               (uint16)xSurface.background.a * 255
+               (uint16)(xSurface.background.a * 255)
             };
             x1 += surface.offset.x;
             y1 += surface.offset.y;
@@ -1230,7 +1240,7 @@ class XDisplayDriver : DisplayDriver
          }
          else
             Area(display, surface,surface.box.left,surface.box.top,surface.box.right,surface.box.bottom);
-      }  
+      }
    }
 
    bool ConvertBitmap(DisplaySystem displaySystem, Bitmap src, PixelFormat format, ColorAlpha * palette)
@@ -1254,7 +1264,7 @@ class XDisplayDriver : DisplayDriver
             }
          }
 
-         bitmap.stride = stride;   
+         bitmap.stride = stride;
          bitmap.width = width;
          bitmap.height = height;
          bitmap.size = (uint)stride * (uint)height;
@@ -1301,7 +1311,7 @@ class XDisplayDriver : DisplayDriver
          }
          if(xSurface.xOffset)
          {
-            XTransform transform = 
+            XTransform transform =
             {
                {
                   { (int)(1.0f * (1<<16)), (int)(0.0f * (1<<16)), -(xSurface.xOffset << 10) },
@@ -1310,7 +1320,7 @@ class XDisplayDriver : DisplayDriver
                }
             };
             // printf("XOffset: %d\n", xSurface.xOffset);
-            XRenderSetPictureTransform(xGlobalDisplay, xBitmap.picture, &transform);            
+            XRenderSetPictureTransform(xGlobalDisplay, xBitmap.picture, &transform);
          }
 
          if(src.alphaBlend || display.alphaBlend)
@@ -1325,7 +1335,7 @@ class XDisplayDriver : DisplayDriver
                XRenderComposite(xGlobalDisplay, PictOpOver, xBitmap.picture, xBitmap.maskPicture, xSurface.picture, sx, sy, sx, sy, dx, dy, w, h);
          }
          else
-            XCopyArea(xGlobalDisplay, (Pixmap)xBitmap.pixmap, (Pixmap)xSurface.pixmap, xDisplay.gc, 
+            XCopyArea(xGlobalDisplay, (Pixmap)xBitmap.pixmap, (Pixmap)xSurface.pixmap, xDisplay.gc,
                sx, sy, w, h, dx, dy);
 
          if(xSurface.xOffset)
@@ -1359,12 +1369,11 @@ class XDisplayDriver : DisplayDriver
 
    void Stretch(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
    {
-      XDisplay xDisplay = display.driverData;
       XSurface xSurface = surface.driverData;
       XBitmap xBitmap = src.driverData;
       if(xBitmap)
       {
-         XTransform transform = 
+         XTransform transform =
          {
             {
                { (int)((float)sw / w * (1<<16)), (int)(0.0f * (1<<16)), (int)(0.0f * (1<<16)) },
@@ -1413,11 +1422,11 @@ class XDisplayDriver : DisplayDriver
    void BlitDI(Display display, Surface surface, Bitmap src, int dx, int dy, int sx, int sy, int w, int h)
    {
       XImage image = { 0 };
-      XDisplay xDisplay = display.driverData;
+      XDisplay xDisplay = display ? display.driverData : null;
       XSurface xSurface = surface.driverData;
       bool flip;
 
-      if(!src.picture || !ClipBlitCoords(surface, src, &dx, &dy, &sx, &sy, &w, &h, &flip))
+      if(!xDisplay || !src.picture || !ClipBlitCoords(surface, src, &dx, &dy, &sx, &sy, &w, &h, &flip))
          return;
 
       if(src.pixelFormat == display.pixelFormat)
@@ -1431,10 +1440,10 @@ class XDisplayDriver : DisplayDriver
          image.bytes_per_line = ((src.pixelFormat == pixelFormat888) ? 4 : 2) * src.stride;
          image.bits_per_pixel = (src.pixelFormat == pixelFormat888) ? 32 : 16;
 
-         image.data = src.picture;
+         image.data = (char *)src.picture;
          XInitImage(&image);
 
-         XPutImage(xGlobalDisplay, (Pixmap)xSurface.pixmap, xDisplay.gc, &image, 
+         XPutImage(xGlobalDisplay, (Pixmap)xSurface.pixmap, xDisplay.gc, &image,
             sx, sy, dx + surface.offset.x, dy + surface.offset.y, w,h);
       }
       else
@@ -1454,13 +1463,13 @@ class XDisplayDriver : DisplayDriver
          image.bytes_per_line = ((temp.pixelFormat == pixelFormat888) ? 4 : 2) * temp.stride;
          image.bits_per_pixel = (temp.pixelFormat == pixelFormat888) ? 32 : 16;
 
-         image.data = temp.picture;
+         image.data = (char *)temp.picture;
 
          XInitImage(&image);
          if(!src.transparent)
          {
             // printf("Stride: %d, dx: %d, dy: %d, w: %d, h: %d, %d\n", temp.stride, dx + surface.offset.x, dy + surface.offset.y, w,h, xSystemDepth);
-            XPutImage(xGlobalDisplay, (Pixmap)xSurface.pixmap, xDisplay.gc, &image, 
+            XPutImage(xGlobalDisplay, (Pixmap)xSurface.pixmap, xDisplay.gc, &image,
                0, 0, dx + surface.offset.x, dy + surface.offset.y, w,h);
          }
          else
@@ -1517,7 +1526,7 @@ class XDisplayDriver : DisplayDriver
                   }
                }
             }
-         
+
             XFreeGC(xGlobalDisplay, maskGC);
          #else
          GC gc = 0;
@@ -1530,7 +1539,7 @@ class XDisplayDriver : DisplayDriver
             XSetGraphicsExposures(xGlobalDisplay, gc, False);
 
             XPutImage(xGlobalDisplay, pixmap, gc, &image, 0, 0, 0, 0, w,h);
-            
+
          PutBitmapMask(mask, temp);
          #endif
 
@@ -1581,11 +1590,11 @@ class XDisplayDriver : DisplayDriver
          image.bytes_per_line = ((temp.pixelFormat == pixelFormat888) ? 4 : 2) * temp.stride;
          image.bits_per_pixel = (temp.pixelFormat == pixelFormat888) ? 32 : 16;
 
-         image.data = temp.picture;
+         image.data = (char *)temp.picture;
          XInitImage(&image);
-         
+
          // printf("Blitting DI\n");
-         XPutImage(xGlobalDisplay, (Pixmap)xSurface.pixmap, xDisplay.gc, &image, 
+         XPutImage(xGlobalDisplay, (Pixmap)xSurface.pixmap, xDisplay.gc, &image,
             0, 0, dx + surface.offset.x, dy + surface.offset.y, w,h);
 
          delete s;
@@ -1616,11 +1625,11 @@ class XDisplayDriver : DisplayDriver
          image.bytes_per_line = ((temp.pixelFormat == pixelFormat888) ? 4 : 2) * temp.stride;
          image.bits_per_pixel = (temp.pixelFormat == pixelFormat888) ? 32 : 16;;
 
-         image.data = temp.picture;
+         image.data = (char *)temp.picture;
          XInitImage(&image);
-         
+
          // printf("Blitting DI\n");
-         XPutImage(xGlobalDisplay, (Pixmap)xSurface.pixmap, xDisplay.gc, &image, 
+         XPutImage(xGlobalDisplay, (Pixmap)xSurface.pixmap, xDisplay.gc, &image,
             0, 0, dx + surface.offset.x, dy + surface.offset.y, w,h);
 
          delete s;
@@ -1628,7 +1637,7 @@ class XDisplayDriver : DisplayDriver
       }
    }
 
-   Font LoadFont(DisplaySystem displaySystem, char * faceName, float size, FontFlags flags)
+   Font LoadFont(DisplaySystem displaySystem, const char * faceName, float size, FontFlags flags)
    {
       Font font;
       font = ((subclass(DisplayDriver))class(LFBDisplayDriver)).LoadFont(displaySystem, faceName, size, flags);
@@ -1655,12 +1664,12 @@ class XDisplayDriver : DisplayDriver
    #define CHAR_WIDTH   6
    #define CHAR_HEIGHT  14
 
-   void WriteText(Display display, Surface surface, int x, int y, char * text, int len)
+   void WriteText(Display display, Surface surface, int x, int y, const char * text, int len)
    {
       XSurface xSurface = surface.driverData;
-      XDisplay xDisplay = display.driverData;
+      //XDisplay xDisplay = display.driverData;
       int tw, th;
-    
+
       ((subclass(DisplayDriver))class(LFBDisplayDriver)).TextExtent(display, surface, text, len, &tw, &th);
       if(xSurface.opaque)
       {
@@ -1682,18 +1691,18 @@ class XDisplayDriver : DisplayDriver
             (uint16)xSurface.background.a
          };
          //printf("Filling rectangle\n");
-         XRenderFillRectangle(xGlobalDisplay, PictOpSrc /*PictOpOver*/, xSurface.picture, &renderColor, 
+         XRenderFillRectangle(xGlobalDisplay, PictOpSrc /*PictOpOver*/, xSurface.picture, &renderColor,
             x + surface.offset.x, y + surface.offset.y, tw, th);
       }
       /*
-      XDrawString(xGlobalDisplay, (Pixmap)xDisplay.pixmap, xDisplay.gc, 
+      XDrawString(xGlobalDisplay, (Pixmap)xDisplay.pixmap, xDisplay.gc,
          x + surface.offset.x, y + surface.offset.y + 12, text, len);
       */
 
       ((subclass(DisplayDriver))class(LFBDisplayDriver)).WriteText(display, surface, x, y, text, len);
    }
 
-   void TextExtent(Display display, Surface surface, char * text, int len, int * width, int * height)
+   void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height)
    {
       XSurface xSurface = surface.driverData;
       /*
@@ -1706,7 +1715,7 @@ class XDisplayDriver : DisplayDriver
       FontExtent(display.displaySystem, xSurface.font, text, len, width, height);
    }
 
-   void FontExtent(DisplaySystem displaySystem, Font font, char * text, int len, int * width, int * height)
+   void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height)
    {
       /*
       if(width) *width = len * CHAR_WIDTH;
@@ -1722,7 +1731,6 @@ class XDisplayDriver : DisplayDriver
 
    void LineStipple(Display display, Surface surface, uint stipple)
    {
-      XSurface xSurface = surface.driverData;
       XDisplay xDisplay = display.driverData;
       char list[32] = { 0 };
       int count = 0;
@@ -1797,19 +1805,19 @@ class XDisplayDriver : DisplayDriver
          XSetClipRectangles(xGlobalDisplay, xDisplay.gc, 0,0, &rectangle, 1, YXBanded);
       }
       /*if(display.alphaBlend)
-         XRenderComposite(xGlobalDisplay, PictOpSrc, xDisplay.picture, None, xDisplay.windowPicture, box->left, box->top, 0, 0, box->left, box->top, 
+         XRenderComposite(xGlobalDisplay, PictOpSrc, xDisplay.picture, None, xDisplay.windowPicture, box->left, box->top, 0, 0, box->left, box->top,
             box->right - box->left + 1, box->bottom - box->top + 1);
       else*/
-         XCopyArea(xGlobalDisplay, (Pixmap)xDisplay.pixmap, (X11Window)display.window, xDisplay.gc /*windowGC*/, 
-            box->left, box->top, 
-            box->right - box->left + 1, 
+         XCopyArea(xGlobalDisplay, (Pixmap)xDisplay.pixmap, (X11Window)display.window, xDisplay.gc /*windowGC*/,
+            box->left, box->top,
+            box->right - box->left + 1,
             box->bottom - box->top + 1,
             box->left, box->top);
 
       if(display.alphaBlend)
       {
          Box * box = &xDisplay.updateBox;
-         XRenderComposite(xGlobalDisplay, PictOpSrc, xDisplay.picture, None, xDisplay.shapePicture, box->left, box->top, 0, 0, box->left, box->top, 
+         XRenderComposite(xGlobalDisplay, PictOpSrc, xDisplay.picture, None, xDisplay.shapePicture, box->left, box->top, 0, 0, box->left, box->top,
             box->right - box->left + 1, box->bottom - box->top + 1);
          #if !defined(__APPLE__) && !defined(__OLDX__)
          XShapeCombineMask(xGlobalDisplay, (X11Window)display.window, ShapeInput, 0, 0, xDisplay.shapePixmap, ShapeSet);
@@ -1834,13 +1842,13 @@ class XDisplayDriver : DisplayDriver
    }
 }
 
-default dllexport int __attribute__((stdcall)) IS_XGetPixmap(Bitmap bitmap)
+default dllexport long IS_XGetPixmap(Bitmap bitmap)
 {
    XBitmap xBitmap = bitmap.driverData;
    return xBitmap.pixmap;
 }
 
-default dllexport void __attribute__((stdcall)) IS_XGetSurfaceInfo(Surface surface, Pixmap * pixmap, GC * gc, int *x, int * y)
+default dllexport void IS_XGetSurfaceInfo(Surface surface, Pixmap * pixmap, GC * gc, int *x, int * y)
 {
    Display display = surface.display;
    XDisplay xDisplay = display.driverData;