ecere;gui;drivers;XInterface; (#850) fix for unity double menu bar and mouse pointer...
[sdk] / ecere / src / gui / drivers / XInterface.ec
index 5f9ea95..87225bc 100644 (file)
@@ -1,7 +1,7 @@
 namespace gui::drivers;
 
 import "instance"
-#if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
+#if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
 import "OpenGLDisplayDriver"
 #endif
 
@@ -18,12 +18,9 @@ default:
 #include <linux/joystick.h>
 #endif
 #include <sys/param.h>
-#ifdef BSD
 #include <stdlib.h>
-#else
-#include <malloc.h>
-#endif
 #include <unistd.h>
+#include <sys/select.h>
 
 //#include <stdio.h>
 //#include <stdlib.h>
@@ -43,8 +40,10 @@ default:
 #include <X11/Xutil.h>
 #include <X11/XKBlib.h>
 #include <X11/keysym.h>
-#include <sys/fcntl.h>
+#include <fcntl.h>
+#if !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
 #include <GL/glx.h>
+#endif
 #include <X11/extensions/Xrender.h>
 #include <X11/extensions/XShm.h>
 
@@ -85,8 +84,8 @@ static Semaphore xSemaphore { };
 static Mutex xMutex { };
 static bool fullScreenMode;
 static int desktopX = 0, desktopY = 0, desktopW = 0, desktopH = 0;
-int confineWindow;
-static int nullCursor;
+X11Window confineWindow;
+static X11Cursor nullCursor;
 static X11Window capturedWindow = None;
 static Window restrictedWindow = null;
 static bool gotAnXEvent = false;
@@ -103,8 +102,7 @@ static enum AtomIdents
    _net_wm_window_type_desktop, _net_wm_window_type_dialog, _net_wm_window_type_dock, _net_wm_window_type_dropdown_menu,
    _net_wm_window_type_menu, _net_wm_window_type_normal, _net_wm_window_type_popup_menu, _net_wm_window_type_splash,
    _net_wm_window_type_toolbar, _net_wm_window_type_utility, _net_workarea, _net_frame_extents, _net_request_frame_extents,
-   _net_wm_state_maximized_vert, _net_wm_state_maximized_horz, _net_wm_state_hidden,
-   app_selection
+   _net_wm_state_maximized_vert, _net_wm_state_maximized_horz, app_selection
 };
 
 static Atom atoms[AtomIdents];
@@ -148,7 +146,6 @@ static const char *atomNames[AtomIdents] = {
    "_NET_REQUEST_FRAME_EXTENTS", // _net_request_frame_extents
    "_NET_WM_STATE_MAXIMIZED_VERT", // _net_wm_state_maximized_vert
    "_NET_WM_STATE_MAXIMIZED_HORZ", // _net_wm_state_maximized_horz
-   "_NET_WM_STATE_HIDDEN", // _net_wm_state_hidden
    "APP_SELECTION"
 };
 
@@ -236,8 +233,14 @@ static void RepositionDesktop(bool updateChildren)
    X11Window x_root;
    int current = 0;   
    char *data = null;
-   int format, len, fill;
+   int format;
+   unsigned long len, fill;
    Atom type;
+   static double lastTime = 0, time;
+
+   time = GetTime();
+   if(desktopW && desktopH && time - lastTime < 1.5) return;
+   lastTime = time;
 
    w = XDisplayWidth(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
    h = XDisplayHeight(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
@@ -255,7 +258,7 @@ static void RepositionDesktop(bool updateChildren)
       if(data)
       {
          int desktops = 0;
-         desktops = *(int *)data;
+         desktops = (int)*(long *)data;
 
          //printf("_NET_NUMBER_OF_DESKTOPS is %d\n", desktops);
 
@@ -275,7 +278,7 @@ static void RepositionDesktop(bool updateChildren)
       
       if(data)
       {
-         current = *(int *)data;
+         current = (int)*(long *)data;
          XFree(data);
          data = null;
 
@@ -284,7 +287,7 @@ static void RepositionDesktop(bool updateChildren)
    }   
    if(atoms[_net_workarea] != None)
    {
-      int *workareas;
+      long *workareas;
 
       if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_workarea], 0, (4 * 32),
                             False, AnyPropertyType, &type, &format, &len,
@@ -306,14 +309,16 @@ static void RepositionDesktop(bool updateChildren)
   
       if(data)
       {
-         workareas = (int *)data;
+         workareas = (long *)data;
      
-         x = workareas[current * 4];
-         y = workareas[current * 4 + 1];
-         w = workareas[current * 4 + 2];
-         h = workareas[current * 4 + 3];   
+         x = (int)workareas[current * 4];
+         y = (int)workareas[current * 4 + 1];
+         w = (int)workareas[current * 4 + 2];
+         h = (int)workareas[current * 4 + 3];
 
          //printf("_NET_WORKAREA is x = %d, y = %d, w = %d, h = %d\n", x, y, w, h);
+         XFree(data);
+         data = null;
       }
       //   printf("Work Area width: %d, height %d\n", w, h);
    }
@@ -369,12 +374,12 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
    KeySym keysym = NoSymbol;
    Status status;
    int buflength = 0;
-   static int bufsize = 16;
+   static long bufsize = 16;
    static char *buf = NULL;
    XWindowData windowData = window.windowData;
    Key key = 0;
 
-   if(window.destroyed) return;
+   if(window.destroyed) return result;
 
    // Logf("Got 0x%x (%d)\n", keyCode, key);
 
@@ -408,14 +413,14 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
 */
 
    if(!buf)
-      buf = malloc(bufsize);
+      buf = malloc((uint)bufsize);
    if(windowData && windowData.ic)
    { 
-      buflength = XmbLookupString(windowData.ic, event, buf, bufsize, &keysym, &status);
+      buflength = XmbLookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
       if (status == XBufferOverflow)
       {
-         buf = realloc(buf, (bufsize = buflength));
-         buflength = XmbLookupString(windowData.ic, event, buf, bufsize, &keysym, &status);
+         buf = realloc(buf, (uint)(bufsize = buflength));
+         buflength = XmbLookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
       }
       if(status != XLookupKeySym && status != XLookupBoth && release == 1)
          keysym = XLookupKeysym(event, 0);
@@ -466,7 +471,7 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
 #ifdef __APPLE__
          case XK_Help:     key = insert; break;
 #endif
-         // case XK_Break:
+         case XK_Break:    key = Key { pauseBreak, ctrl = true }; break;
 #ifdef __APPLE__
          case XK_Mode_switch: key = leftAlt; break;
 #endif
@@ -792,7 +797,7 @@ static Bool EventChecker(void *display, XEvent *event, char * data)
 
 static Bool ConfigureNotifyChecker(void *display, XConfigureEvent *event, char * data)
 {
-   return (!data || ((event->window == (int) data)) && event->type == ConfigureNotify;
+   return ((!data || (event->window == (int) data)) && event->type == ConfigureNotify;
 }
 
 static uint timerDelay = MAXINT;
@@ -806,8 +811,8 @@ static uint XTimerThread(Thread thread)
    */
    for(;;)
    {
-      int result;
-      bool waitSemaphore = false;
+      //int result;
+      //bool waitSemaphore = false;
       fd_set readSet, writeSet, exceptSet;
       struct timeval tv = { (timerDelay == MAXINT) ? 0 : (timerDelay / 1000000), (timerDelay == MAXINT) ? (int)(1000000 / 18.2) : (timerDelay % 1000000) };
       
@@ -895,11 +900,11 @@ static int MyXIOErrorHandler(X11Display * display)
 
 struct MWM_Hints
 {
-  uint flags;
-  uint functions;
-  uint decorations;
-  int inputMode;
-  uint status;
+  unsigned long flags;
+  unsigned long functions;
+  unsigned long decorations;
+  long inputMode;
+  unsigned long status;
 };
 
 static void WaitForViewableWindow(Window window)
@@ -912,7 +917,7 @@ static void WaitForViewableWindow(Window window)
    {
       XWindowAttributes attributes = { 0 };
       int result;
-      if(!XGetWindowAttributes(xGlobalDisplay, (int)window.windowHandle, &attributes))
+      if(!XGetWindowAttributes(xGlobalDisplay, (X11Window)window.windowHandle, &attributes))
          break;
       if(attributes.map_state == IsViewable)
          break;
@@ -959,7 +964,7 @@ default:
 
 #include <sys/ipc.h>
 #include <sys/shm.h>
-#include <sys/signal.h>
+#include <signal.h>
 #include <locale.h>
 
 
@@ -1053,7 +1058,7 @@ class XInterface : Interface
             {
                XVisualInfo vinfo;
                XVisualInfo *vinfo_ret;
-               int numitems;
+               int numitems = 0;
               
                vinfo.visualid = XVisualIDFromVisual(xSystemVisual);
                vinfo_ret = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &vinfo, &numitems);
@@ -1449,7 +1454,7 @@ class XInterface : Interface
                   // if(event->time - lastTime > 15)
                   {
                      Modifiers keyFlags = 0;
-                     int x = event->x_root, y = event->y_root;
+                     // int x = event->x_root, y = event->y_root;
 
                      if(event->state & ShiftMask)     keyFlags.shift = true;
                      if(event->state & ControlMask)   keyFlags.ctrl = true;
@@ -1463,7 +1468,7 @@ class XInterface : Interface
                         event->x_root, event->y_root, &keyFlags, false, false);
                      delete window;
                      //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
-                     lastTime = event->time;
+                     lastTime = (uint)event->time;
                   }
                   break;
                }
@@ -1528,7 +1533,7 @@ class XInterface : Interface
                   XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
                   Window modalRoot = window.FindModal();
                   XWindowData windowData;
-                  activeWindow = (uint)window.windowHandle;
+                  activeWindow = (X11Window)window.windowHandle;
 
                   if(window.parent && window == window.parent.activeChild) break;
                   incref window;
@@ -1562,7 +1567,7 @@ class XInterface : Interface
                      break;
                   }
                   if(thisEvent->window == activeWindow)
-                     activeWindow = (uint)null;
+                     activeWindow = (X11Window)null;
 #if 0
                   if(XCheckTypedEvent(xGlobalDisplay, FocusIn, (XEvent *)thisEvent))
                   {
@@ -1662,10 +1667,11 @@ class XInterface : Interface
                   // TODO: Support _NET_REQUEST_FRAME_EXTENTS message / _NET_FRAME_EXTENTS property for decoration size awareness
                   if(window.nativeDecorations)
                   {
-                     int format, len, fill;
+                     int format;
+                     unsigned long len, fill;
                      Atom type;
                      char * data = null;
-                     if(XGetWindowProperty(xGlobalDisplay, (uint)window.systemHandle, atoms[_net_wm_state], 0, 32, False,
+                     if(XGetWindowProperty(xGlobalDisplay, (X11Window)window.systemHandle, atoms[_net_wm_state], 0, 32, False,
                             XA_ATOM, &type, &format, &len, &fill, &data) == Success)
                      {
                         bool maxVert = false, maxHorz = false, isMinimized = false;
@@ -1746,9 +1752,9 @@ class XInterface : Interface
                      Window modalRoot;
                      XWindowData windowData;
                      bool laterFocus;
-                     activeWindow = (uint)window.windowHandle;
+                     activeWindow = (X11Window)window.windowHandle;
 
-                     timeStamp = event->data.l[1];
+                     timeStamp = (int)event->data.l[1];
                      
                      windowData = window.windowData;
                      laterFocus = windowData.laterFocus;
@@ -1793,7 +1799,7 @@ class XInterface : Interface
                         {
                            if(modalRoot)
                            {
-                              XRaiseWindow(xGlobalDisplay, (int)modalRoot.windowHandle);
+                              XRaiseWindow(xGlobalDisplay, (X11Window)modalRoot.windowHandle);
                               WaitForViewableWindow(modalRoot);
                               if(atoms[_net_active_window])
                               {
@@ -1802,7 +1808,7 @@ class XInterface : Interface
                                  event.message_type = atoms[_net_active_window];
                                  event.display = xGlobalDisplay;
                                  event.serial = 0;
-                                 event.window = (uint)modalRoot.windowHandle;
+                                 event.window = (X11Window)modalRoot.windowHandle;
                                  event.send_event = 1;
                                  event.format = 32;
                                  event.data.l[0] = 0;
@@ -1816,7 +1822,7 @@ class XInterface : Interface
 #endif
 
                                  XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
-                                 XSetInputFocus(xGlobalDisplay, (int)modalRoot.windowHandle, RevertToPointerRoot, (uint)timeStamp);
+                                 XSetInputFocus(xGlobalDisplay, (X11Window)modalRoot.windowHandle, RevertToPointerRoot, (uint)timeStamp);
 
                                  //XFlush(xGlobalDisplay);
                                  //printf("Done.\n");
@@ -1824,7 +1830,7 @@ class XInterface : Interface
                            }
                            else
                            {
-                              XSetInputFocus(xGlobalDisplay, (int)window.windowHandle, RevertToPointerRoot, (uint)timeStamp);
+                              XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, (uint)timeStamp);
                               window.ExternalActivate(true, true, window, null); // lastActive); 
                               if(windowData && windowData.ic)
                               {
@@ -1849,37 +1855,37 @@ class XInterface : Interface
                   if(event->atom == atoms[_net_frame_extents] &&
                     event->state == PropertyNewValue && windowData)
                   {
-                     int format, len, fill;
+                     int format;
+                     unsigned long len, fill;
                      Atom type;
                      char * data = null;
 
-                     if(XGetWindowProperty(xGlobalDisplay, (uint)window.windowHandle,
+                     if(XGetWindowProperty(xGlobalDisplay, (X11Window)window.windowHandle,
                         atoms[_net_frame_extents], 0, 4,
                          False, XA_CARDINAL, &type, &format, &len,
                          &fill, &data) == Success && data)
                      {
-                        int *extents = (int *)data;
-                        bool hadFrameExtents = windowData.gotFrameExtents;
+                        long *extents = (long *)data;
+                        bool change = extents[0] != windowData.decor.left ||
+                                      extents[1] != windowData.decor.right ||
+                                      extents[2] != windowData.decor.top ||
+                                      extents[3] != windowData.decor.bottom;
                         windowData.decor =
                         {
-                           left = extents[0], right  = extents[1],
-                           top  = extents[2], bottom = extents[3]
+                           left = (int)extents[0], right  = (int)extents[1],
+                           top  = (int)extents[2], bottom = (int)extents[3]
                         };
                         windowData.gotFrameExtents = true;
+                        if(change)
                         {
                            int x, y, w, h;
                            window.ComputeAnchors(
                               window.normalAnchor,
                               window.normalSizeAnchor,
                               &x, &y, &w, &h);
-                           if(!hadFrameExtents) // || window.state == normal)
-                           {
-                              bool isMaximized = window.state == maximized;
-                              window.Position(x, y, w, h, true, true, true, true, false, true);
-                              UpdateRootWindow(window);
-                           }
+                           window.Position(x, y, w, h, true, true, true, true, false, false);
+                           UpdateRootWindow(window);
                         }
-
                         XFree(data);
                      }
                      else
@@ -1964,12 +1970,12 @@ class XInterface : Interface
       int depth;
       Visual * visual;
       XIC ic = null;
-      unsigned int mask = EVENT_MASK;
+      unsigned long mask = EVENT_MASK;
 
       attributes.override_redirect = window.interim ? True : False;
       attributes.event_mask = EVENT_MASK;
       //printf("%s\n", guiApp.defaultDisplayDriver);
-#if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
+#if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
       if(window.dispDriver == class(OpenGLDisplayDriver) || !strcmp(guiApp.defaultDisplayDriver, "OpenGL"))
       {
          int samples;
@@ -2097,22 +2103,22 @@ class XInterface : Interface
       else
       {
          if(window.windowHandle)
-            windowHandle = (int)window.windowHandle;
+            windowHandle = (X11Window)window.windowHandle;
          else
          {
-            X11Window parentWindow = (uint)null;
+            X11Window parentWindow = (X11Window)null;
 
             if(window.master.rootWindow && window.master.rootWindow != guiApp.desktop && (window._isModal || window.style.interim))
             {
                Window master = window.master;
                Window rootWindow = master.rootWindow;
 
-               parentWindow = rootWindow.is3D ? (uint)rootWindow.parent.windowHandle : (uint)rootWindow.windowHandle;
+               parentWindow = rootWindow.is3D ? (X11Window)rootWindow.parent.windowHandle : (X11Window)rootWindow.windowHandle;
 
                // parentWindow = window.master.rootWindow.is3D ? window.master.rootWindow.parent.windowHandle : window.master.rootWindow.windowHandle;
             }
             if(window.style.showInTaskBar)
-               parentWindow = (uint)null;
+               parentWindow = (X11Window)null;
 
             windowHandle = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), 
                0,0,1,1,0, depth, InputOutput, visual ? visual : CopyFromParent, 
@@ -2133,7 +2139,7 @@ class XInterface : Interface
                   parentWindow ? atoms[_net_wm_window_type_menu] : atoms[_net_wm_window_type_normal],
                   parentWindow ? atoms[_net_wm_window_type_popup_menu] : 0
                };
-#if defined(__APPLE__)
+#if defined(__APPLE__) || defined(__FreeBSD__)
                // Don't set this on non-interim windows for OS X...
                if(parentWindow && window.interim)
 #endif
@@ -2179,14 +2185,14 @@ class XInterface : Interface
          ic = XCreateIC(im, XNInputStyle,
             XIMStatusNothing | XIMPreeditPosition,
             XNPreeditAttributes, argList,
-            XNClientWindow, windowHandle, XNFocusWindow, windowHandle, 0);
+            XNClientWindow, windowHandle, XNFocusWindow, windowHandle, NULL);
          XFree(argList);
          // Should we free the fontset or not?
          // XFreeFontSet(xGlobalDisplay, fontset);
          if(ic)
             setICPosition = true;
          else
-            ic = XCreateIC(im, XNInputStyle, XIMStatusNothing | XIMPreeditNothing, XNClientWindow, windowHandle, XNFocusWindow, windowHandle, 0);
+            ic = XCreateIC(im, XNInputStyle, XIMStatusNothing | XIMPreeditNothing, XNClientWindow, windowHandle, XNFocusWindow, windowHandle, null);
       }
       if(ic)
       {
@@ -2198,7 +2204,7 @@ class XInterface : Interface
 
       if(capturedWindow == None && !restrictedWindow)
       {
-         XGrabPointer(xGlobalDisplay, (int)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
+         XGrabPointer(xGlobalDisplay, (X11Window)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
             GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
          XUngrabPointer(xGlobalDisplay, CurrentTime);
       }
@@ -2218,7 +2224,7 @@ class XInterface : Interface
                 0, 0, 0
             };
             XChangeProperty(xGlobalDisplay, windowHandle, atoms[_motif_wm_hints], atoms[_motif_wm_hints], 32,
-               PropModeReplace, (unsigned char*)&hints, sizeof(hints)/4);
+               PropModeReplace, (unsigned char*)&hints, 5);
          }
          if(atoms[_net_wm_pid] != None)
          {
@@ -2248,7 +2254,7 @@ class XInterface : Interface
 
       if(capturedWindow == None && !restrictedWindow)
       {
-         XGrabPointer(xGlobalDisplay, (int)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
+         XGrabPointer(xGlobalDisplay, (X11Window)windowHandle, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
             GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
          XUngrabPointer(xGlobalDisplay, CurrentTime);
       }
@@ -2261,7 +2267,7 @@ class XInterface : Interface
          event.message_type = atoms[_net_request_frame_extents];
          event.display = xGlobalDisplay;
          event.serial = 0;
-         event.window = (uint)windowHandle;
+         event.window = (X11Window)windowHandle;
          event.send_event = 1;
          window.windowHandle = (void *)windowHandle;
          event.format = 32;
@@ -2276,10 +2282,10 @@ class XInterface : Interface
       XEvent event;
 
       XDeleteContext(xGlobalDisplay, (XID)window, windowContext); 
-      XSaveContext(xGlobalDisplay, (int)window.windowHandle, windowContext, null);
-      XDestroyWindow(xGlobalDisplay, (int)window.windowHandle);
+      XSaveContext(xGlobalDisplay, (X11Window)window.windowHandle, windowContext, null);
+      XDestroyWindow(xGlobalDisplay, (X11Window)window.windowHandle);
       XSync(xGlobalDisplay, 0);
-      while(XCheckWindowEvent(xGlobalDisplay, (int)window.windowHandle, 0xFFFFFFFF, &event));
+      while(XCheckWindowEvent(xGlobalDisplay, (X11Window)window.windowHandle, 0xFFFFFFFF, &event));
       window.windowHandle = null;
       if(window.windowData)
       {
@@ -2301,9 +2307,9 @@ class XInterface : Interface
    {
       if(window.windowHandle)
       {
-         XChangeProperty(xGlobalDisplay, (int)window.windowHandle, atoms[_net_wm_name],
+         XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_name],
             atoms[utf8_string], 8, PropModeReplace, (byte *)name, name ? strlen(name) : 0);
-         XChangeProperty(xGlobalDisplay, (int)window.windowHandle, atoms[wm_name],
+         XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[wm_name],
             atoms[utf8_string], 8, PropModeReplace, (byte *)name, name ? strlen(name) : 0);
       }   
    }
@@ -2313,11 +2319,11 @@ class XInterface : Interface
       //Logf("Position root window %s\n", window.name);
       if(window.windowHandle && (!window.parent || !window.parent.display))
       {
-#if defined(__APPLE__)
+#if defined(__APPLE__) || defined(__FreeBSD__)
          bool visible = window.visible;
          if(window.visible)
          {
-            XMapWindow(xGlobalDisplay, (int)window.windowHandle);
+            XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
             WaitForViewableWindow(window);
          }
 #endif
@@ -2326,7 +2332,7 @@ class XInterface : Interface
          if(window.nativeDecorations)
          {
             XWindowData windowData = window.windowData;
-#if !defined(__APPLE__)
+#if !defined(__APPLE__) && !defined(__FreeBSD__)
             // TODO: How to handle frame extents not supported?
             if(!windowData.gotFrameExtents || window.state == maximized) return;
 #endif
@@ -2334,14 +2340,14 @@ class XInterface : Interface
             h -= window.size.h - window.clientSize.h;
          }
          if(move && resize)
-            XMoveResizeWindow(xGlobalDisplay, (int)window.windowHandle, x + desktopX, y + desktopY, w, h);
+            XMoveResizeWindow(xGlobalDisplay, (X11Window)window.windowHandle, x + desktopX, y + desktopY, w, h);
          else if(move)
-            XMoveWindow(xGlobalDisplay, (int)window.windowHandle, x + desktopX, y + desktopY);
+            XMoveWindow(xGlobalDisplay, (X11Window)window.windowHandle, x + desktopX, y + desktopY);
          else if(resize)
-            XResizeWindow(xGlobalDisplay, (int)window.windowHandle, w, h);
+            XResizeWindow(xGlobalDisplay, (X11Window)window.windowHandle, w, h);
 #if defined(__APPLE__)
 //         if(window.created && !visible)
-  //          XUnmapWindow(xGlobalDisplay, (int)window.windowHandle);
+  //          XUnmapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
 #endif
       }
    }
@@ -2391,8 +2397,8 @@ class XInterface : Interface
          //Logf("Set root window state %d %s\n", state, window.name);
          if(visible)
          {
-            XMapWindow(xGlobalDisplay, (int)window.windowHandle);
-#if defined(__APPLE__)
+            XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
+#if defined(__APPLE__) || defined(__FreeBSD__)
             WaitForViewableWindow(window);
 #endif
             if(window.creationActivation == activate && state != minimized)
@@ -2436,7 +2442,7 @@ class XInterface : Interface
                */
 
                // printf("Attempting to minimize %s\n", window._class.name);
-               XIconifyWindow(xGlobalDisplay, (uint)window.windowHandle, DefaultScreen(xGlobalDisplay));
+               XIconifyWindow(xGlobalDisplay, (X11Window)window.windowHandle, DefaultScreen(xGlobalDisplay));
             }
             else
             {
@@ -2445,7 +2451,7 @@ class XInterface : Interface
                   // With native decorations, we do it the first time
                   // or the WM (Gnome) is sticking it to the top/right!
                   XMoveResizeWindow(xGlobalDisplay, 
-                     (int)window.windowHandle,
+                     (X11Window)window.windowHandle,
                      window.position.x + desktopX,
                      window.position.y + desktopY,
                      window.nativeDecorations ? window.clientSize.w : window.size.w,
@@ -2460,7 +2466,7 @@ class XInterface : Interface
                   event.message_type = atoms[_net_wm_state];
                   event.display = xGlobalDisplay;
                   event.serial = 0;
-                  event.window = (uint)window.windowHandle;
+                  event.window = (X11Window)window.windowHandle;
                   event.send_event = 1;
                   event.format = 32;
                   event.data.l[0] = (state == maximized) ? 1 : 0;
@@ -2472,7 +2478,7 @@ class XInterface : Interface
             }
          }
          else
-            XUnmapWindow(xGlobalDisplay, (int)window.windowHandle);
+            XUnmapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
          //XFlush(xGlobalDisplay);
       }
    }
@@ -2485,7 +2491,7 @@ class XInterface : Interface
       event.message_type = atoms[_net_wm_state];
       event.display = xGlobalDisplay;
       event.serial = 0;
-      event.window = (uint)window.windowHandle;
+      event.window = (X11Window)window.windowHandle;
       event.send_event = 1;
       event.format = 32;
       event.data.l[0] = 1;
@@ -2500,8 +2506,8 @@ class XInterface : Interface
          if(!window.style.hidden && window.created)
          {
             //printf("Activate root window %s\n", window._class.name);
-            XRaiseWindow(xGlobalDisplay, (int)window.windowHandle);
-            XMapWindow(xGlobalDisplay, (int)window.windowHandle);
+            XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
+            XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
             WaitForViewableWindow(window);
             if(atoms[_net_active_window])
             {
@@ -2510,7 +2516,7 @@ class XInterface : Interface
                event.message_type = atoms[_net_active_window];
                event.display = xGlobalDisplay;
                event.serial = 0;
-               event.window = (uint)window.windowHandle;
+               event.window = (X11Window)window.windowHandle;
                event.send_event = 1;
                event.format = 32;
                event.data.l[0] = 0;
@@ -2528,11 +2534,11 @@ class XInterface : Interface
                
                XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
 //#if defined(__APPLE__)
-               XSetInputFocus(xGlobalDisplay, (int)window.windowHandle, RevertToPointerRoot, CurrentTime);
+               XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, CurrentTime);
 //#endif
             }
             else
-               XSetInputFocus(xGlobalDisplay, (int)window.windowHandle, RevertToPointerRoot, CurrentTime);
+               XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, CurrentTime);
          }
       }
    }
@@ -2553,7 +2559,7 @@ class XInterface : Interface
 
    void GetMousePosition(int *x, int *y)
    {
-      int rootWindow, childWindow;
+      X11Window rootWindow, childWindow;
       int mx, my;
       unsigned int state;
       ((GuiApplication)__thisModule.application).Lock();
@@ -2584,7 +2590,7 @@ class XInterface : Interface
             if(!restrictedWindow)
                XMapWindow(xGlobalDisplay, confineWindow);
 
-            XGrabPointer(xGlobalDisplay, (int) window.rootWindow.windowHandle, False,
+            XGrabPointer(xGlobalDisplay, (X11Window) window.rootWindow.windowHandle, False,
                ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
                GrabModeAsync, confineWindow, fullScreenMode ? nullCursor : None, CurrentTime);
 
@@ -2595,7 +2601,7 @@ class XInterface : Interface
       {
          if(capturedWindow != None)
          {
-            XGrabPointer(xGlobalDisplay, (int)capturedWindow,
+            XGrabPointer(xGlobalDisplay, (X11Window)capturedWindow,
                False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
                GrabModeAsync, None, fullScreenMode ? nullCursor : None, CurrentTime);
          }
@@ -2618,7 +2624,7 @@ class XInterface : Interface
       {
          if(!window.parent || !window.parent.display)
          {
-            XGrabPointer(xGlobalDisplay, (int)window.windowHandle,
+            XGrabPointer(xGlobalDisplay, (X11Window)window.windowHandle,
                False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
                GrabModeAsync, restrictedWindow ? confineWindow : None, fullScreenMode ? nullCursor : None, CurrentTime);
 
@@ -2628,7 +2634,7 @@ class XInterface : Interface
       else if(capturedWindow != None)
       {
          if(restrictedWindow)
-            XGrabPointer(xGlobalDisplay, (int) restrictedWindow.rootWindow.windowHandle, False, 
+            XGrabPointer(xGlobalDisplay, (X11Window) restrictedWindow.rootWindow.windowHandle, False, 
                ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync,
                GrabModeAsync, confineWindow, fullScreenMode ? nullCursor : None, CurrentTime);
          else
@@ -2645,7 +2651,7 @@ class XInterface : Interface
       //*XLockDisplay(xGlobalDisplay);
       if(cursor == -1)
       {
-         XDefineCursor(xGlobalDisplay, (int) guiApp.desktop.windowHandle, nullCursor);
+         XDefineCursor(xGlobalDisplay, (X11Window) guiApp.desktop.windowHandle, nullCursor);
       }
       //*XUnlockDisplay(xGlobalDisplay);
    }
@@ -2708,7 +2714,7 @@ class XInterface : Interface
             delete clipBoardData;
          else if(rootWindow)
           XSetSelectionOwner(xGlobalDisplay, atoms[clipboard], 
-               (int) rootWindow.windowHandle, CurrentTime);
+               (X11Window) rootWindow.windowHandle, CurrentTime);
          clipBoardData = clipBoard.text;
          clipBoard.text = null;
          result = true;
@@ -2741,28 +2747,33 @@ class XInterface : Interface
          }
          if(rootWindow)
          {
-            int selAtom = atoms[clipboard];
-            int owner = XGetSelectionOwner(xGlobalDisplay, selAtom);
+            Atom selAtom = atoms[clipboard];
+            X11Window owner = XGetSelectionOwner(xGlobalDisplay, selAtom);
             if(owner != None)
             {
-               int atom;
+               Atom atom;
                for(atom = atoms[utf8_string]; atom; atom = ((atom == atoms[utf8_string]) ? XA_STRING : 0))
                {
                   XEvent e;
-                  XConvertSelection(xGlobalDisplay, selAtom, atom, atoms[app_selection] /*None*/, (int) rootWindow.windowHandle, CurrentTime);
+                  XConvertSelection(xGlobalDisplay, selAtom, atom, atoms[app_selection] /*None*/, (X11Window) rootWindow.windowHandle, CurrentTime);
                   XIfEvent(xGlobalDisplay, (XEvent *) &e, EventChecker, (void *)SelectionNotify);
                   if(e.type == SelectionNotify)
                   {
                      XSelectionEvent * selection = (XSelectionEvent *) &e;
                      //printf("Got a SelectionNotify with %d (%s)\n", selection->_property, XGetAtomName(xGlobalDisplay, selection->_property));
-                     byte *data;
-                     uint len, size = 0, dummy;
+                     byte *data = null;
+                     unsigned long len, size = 0, dummy;
                      Atom type;
-                    int format;
-                     XGetWindowProperty(xGlobalDisplay, (int) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, 0, 0, AnyPropertyType, &type, &format, &len, &size, &data);
+                     int format;
+                     XGetWindowProperty(xGlobalDisplay, (X11Window) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, 0, False, AnyPropertyType, &type, &format, &len, &size, &data);
+                     if(data)
+                     {
+                        XFree(data);
+                        data = null;
+                     }
                      if(size > 0)
                      {
-                        if(XGetWindowProperty(xGlobalDisplay, (int) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0,size,0,
+                        if(XGetWindowProperty(xGlobalDisplay, (X11Window) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, size, False,
                               AnyPropertyType, &type,&format,&len, &dummy, &data) == Success)
                         {
                            clipBoard.text = new char[size+1];
@@ -2848,11 +2859,18 @@ class XInterface : Interface
          Bitmap bitmap { };
          if(bitmap.Load(resource.fileName, null, null))
          {
-            uint * icon = new uint[2 + bitmap.width * bitmap.height];
+            unsigned long * icon = new unsigned long[2 + bitmap.width * bitmap.height];
             bitmap.Convert(null, pixelFormat888, null);
             icon[0] = bitmap.width;
             icon[1] = bitmap.height;
-            memcpy(icon + 2, bitmap.picture, bitmap.width * bitmap.height * 4);
+            if(sizeof(long) != sizeof(uint32))
+            {
+               int c;
+               for(c = 0; c < bitmap.width * bitmap.height; c++)
+                  icon[c+2] = ((uint32 *)bitmap.picture)[c];
+            }
+            else
+               memcpy(icon + 2, bitmap.picture, bitmap.width * bitmap.height * sizeof(uint32));
             XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_icon],
                          XA_CARDINAL,32,PropModeReplace, (byte *)icon, 2+bitmap.width*bitmap.height);
            delete icon;