ecere: Initial Emscripten support
[sdk] / ecere / src / gui / drivers / XInterface.ec
index e5068be..a920b26 100644 (file)
@@ -1,7 +1,8 @@
+#define _Noreturn
 namespace gui::drivers;
 
 import "instance"
-#if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
+#if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D) && !defined(ECERE_NOGL) && !defined(__ODROID__)
 import "OpenGLDisplayDriver"
 #endif
 
@@ -22,6 +23,16 @@ default:
 #include <unistd.h>
 #include <sys/select.h>
 
+#if defined(__APPLE__)
+#define set _set
+#include <mach/mach.h>
+#include <mach/task.h>
+#include <mach/semaphore.h>
+#undef set
+#else
+#include <semaphore.h>
+#endif
+
 //#include <stdio.h>
 //#include <stdlib.h>
 //#include <string.h>
@@ -40,16 +51,24 @@ typedef int X11Bool;
 
 #include <X11/Xatom.h>
 #include <X11/Xlib.h>
+#if !defined(__EMSCRIPTEN__)
 #include <X11/Xresource.h>
+#endif
 #include <X11/Xutil.h>
 #include <X11/XKBlib.h>
 #include <X11/keysym.h>
 #include <X11/cursorfont.h>
 #include <fcntl.h>
-#if !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
+#if !defined(ECERE_NO3D) && !defined(ECERE_NOGL) //&& !defined(__ODROID__)
+#if defined(__EMSCRIPTEN__)
+#include <GL/glfw.h>
+#else
 #include <GL/glx.h>
 #endif
+#endif
+#if !defined(__EMSCRIPTEN__)
 #include <X11/extensions/Xrender.h>
+#endif
 #include <X11/extensions/XShm.h>
 
 #undef Bool
@@ -95,11 +114,18 @@ static X11Cursor nullCursor;
 static X11Window capturedWindow = None;
 static Window restrictedWindow = null;
 static bool gotAnXEvent = false;
-static XEvent xEvent;
+//static XEvent xEvent;
 static int joystickFD[4];
 static X11Window activeWindow;
 static X11Cursor systemCursors[SystemCursor];
 
+static Window acquiredInputWindow;
+static Point acquireStart;
+static Point lastMouse;
+static MouseButtons buttonsState;
+
+static bool keyStates[KeyCode];
+
 static enum NETWMStateAction { remove = 0, add = 1, toggle = 2 };
 
 static enum AtomIdents
@@ -112,7 +138,7 @@ static enum AtomIdents
    _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_modal, app_selection, _net_supported,
-   _net_wm_state_skip_taskbar
+   _net_wm_state_skip_taskbar, _net_wm_state_fullscreen, _net_wm_state_above, capsLock, numLock, scrollLock
 };
 
 static Atom atoms[AtomIdents];
@@ -160,19 +186,18 @@ static const char *atomNames[AtomIdents] = {
    "_NET_WM_STATE_MODAL", // _net_wm_state_modal
    "APP_SELECTION",
    "_NET_SUPPORTED",
-   "_NET_WM_STATE_SKIP_TASKBAR"
+   "_NET_WM_STATE_SKIP_TASKBAR",
+   "_NET_WM_STATE_FULLSCREEN",
+   "_NET_WM_STATE_ABOVE",
+   "Caps Lock",
+   "Num Lock",
+   "Scroll Lock"
 };
 /*
 _NET_WM_STATE_STICKY, ATOM
-_NET_WM_STATE_MAXIMIZED_VERT, ATOM
-_NET_WM_STATE_MAXIMIZED_HORZ, ATOM
 _NET_WM_STATE_SHADED, ATOM
 _NET_WM_STATE_SKIP_PAGER, ATOM
-_NET_WM_STATE_HIDDEN, ATOM
-_NET_WM_STATE_FULLSCREEN, ATOM
-_NET_WM_STATE_ABOVE, ATOM
 _NET_WM_STATE_BELOW, ATOM
-_NET_WM_STATE_DEMANDS_ATTENTION, ATOM
 */
 
 static bool autoRepeatDetectable;
@@ -189,10 +214,10 @@ static void SetNETWMState(X11Window windowHandle, bool throughRoot, NETWMStateAc
       int format;
       unsigned long count, fill;
       Atom type;
-      char * data = null;
-      uint state = WithdrawnState;
+      byte * data = null;
+      /*uint state = WithdrawnState;
 
-      /*if(XGetWindowProperty(xGlobalDisplay, windowHandle, atoms[wm_state], 0, 3, False,
+      if(XGetWindowProperty(xGlobalDisplay, windowHandle, atoms[wm_state], 0, 3, False,
                  atoms[wm_state], &type, &format, &count, &fill, &data) == Success && count)
       {
          state = *(uint *)data;
@@ -311,7 +336,7 @@ static void RepositionDesktop(bool updateChildren)
    Screen * x_screen = XDefaultScreenOfDisplay(xGlobalDisplay);
    X11Window x_root;
    int current = 0;
-   char *data = null;
+   byte *data = null;
    int format;
    unsigned long len, fill;
    Atom type;
@@ -336,11 +361,12 @@ static void RepositionDesktop(bool updateChildren)
 
       if(data)
       {
+         /*
          int desktops = 0;
          desktops = (int)*(long *)data;
 
-         //printf("_NET_NUMBER_OF_DESKTOPS is %d\n", desktops);
-
+         printf("_NET_NUMBER_OF_DESKTOPS is %d\n", desktops);
+         */
          XFree(data);
          data = null;
       }
@@ -404,11 +430,39 @@ static void RepositionDesktop(bool updateChildren)
 
    if(desktopX != x || desktopY != y || desktopW != w || desktopH != h)
    {
-      guiApp.SetDesktopPosition(x, y, w, h, updateChildren);
-      desktopX = x;
-      desktopY = y;
-      desktopW = w;
-      desktopH = h;
+      bool skip = false;
+      // Don't change the desktop area if another fullscreen application is running
+      // (Attemps to solve the debugging IDE being re-activated while switching debugged app between fullscreen/windowed on Cinnamon)
+      if(XGetWindowProperty(xGlobalDisplay, x_root, atoms[_net_active_window], 0, 32,
+                            False, AnyPropertyType, &type, &format, &len,
+                            &fill, &data) == Success && data)
+      {
+         X11Window active = *(long *)data;
+         XFree(data);
+         if(XGetWindowProperty(xGlobalDisplay, active, atoms[_net_wm_state], 0, 32, False,
+             XA_ATOM, &type, &format, &len, &fill, &data) == Success)
+         {
+            Atom * values = (Atom *) data;
+            int i;
+            for (i = 0; i < len; i++)
+            {
+               if(values[i] == atoms[_net_wm_state_fullscreen])
+               {
+                  skip = true;
+                  break;
+               }
+            }
+            XFree(data);
+         }
+      }
+      if(w && h && !skip)
+      {
+         guiApp.SetDesktopPosition(x, y, w, h, updateChildren);
+         desktopX = x;
+         desktopY = y;
+         desktopW = w;
+         desktopH = h;
+      }
    }
 }
 
@@ -451,7 +505,6 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
    Key code;
    unichar ch = 0;
    KeySym keysym = NoSymbol;
-   Status status;
    int buflength = 0;
    static long bufsize = 16;
    static char *buf = NULL;
@@ -493,13 +546,21 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
 
    if(!buf)
       buf = malloc((uint)bufsize);
-   if(windowData && windowData.ic)
+
+   // TOCHECK: X*LookupString man page says we shouldn't invoke it for non KeyPress events
+   if(windowData && windowData.ic) // && event->type == KeyPress)
    {
-      buflength = XmbLookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
-      if (status == XBufferOverflow)
+      Status status;
+#ifdef X_HAVE_UTF8_STRING
+   #define LookupString Xutf8LookupString
+#else
+   #define LookupString XmbLookupString
+#endif
+      buflength = LookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
+      if(status == XBufferOverflow)
       {
          buf = realloc(buf, (uint)(bufsize = buflength));
-         buflength = XmbLookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
+         buflength = LookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
       }
       if(status != XLookupKeySym && status != XLookupBoth && release == 1)
          keysym = XLookupKeysym(event, 0);
@@ -774,7 +835,7 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
    }
    if(!windowData.ic)
    {
-      ch = (byte)Interface::TranslateKey(key, event->state & ShiftMask);
+      ch = (byte)Interface::TranslateKey(key, (event->state & ShiftMask) != 0);
       // 127 is delete, we don't treat that as a character (Use (SmartKey)key == del)
       if(ch == 128 || ch == 127) ch = 0;
    }
@@ -786,7 +847,18 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
    if(key != leftControl && key != rightControl && event->state & ControlMask)
       code.ctrl = true;
    if(key != leftAlt && key != rightAlt && event->state & Mod1Mask)
+   {
+      if(fullScreenMode && key == tab)
+      {
+         XUngrabKeyboard(xGlobalDisplay, CurrentTime);
+         guiApp.SetAppFocus(false);
+         SetNETWMState((X11Window)window.windowHandle, true, remove, atoms[_net_wm_state_fullscreen], 0);
+         XIconifyWindow(xGlobalDisplay, (X11Window)window.windowHandle, DefaultScreen(xGlobalDisplay));
+         if(acquiredInputWindow)
+            XInterface::SetMousePosition(guiApp.acquiredMouseX, guiApp.acquiredMouseY);
+      }
       code.alt = true;
+   }
 
 #ifdef __APPLE__
    if(key != leftAlt && key != rightAlt && event->state & (1<<13))
@@ -803,6 +875,9 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
    if(release == 1)
    {
       int numBytes;
+
+      if(key < KeyCode::enumSize)
+         keyStates[key] = false;
       if(windowData && windowData.ic) ch = buflength ? UTF8GetChar(buf, &numBytes) : 0;
       if(ch == 127) ch = 0;
       // printf("Release! %d %d %d\n", keysym, code, ch);
@@ -813,6 +888,9 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
       int c;
       if(release == 0)
       {
+         if(key < KeyCode::enumSize)
+            keyStates[key] = true;
+
          if(windowData.ic && buflength)
          {
             for(c = 0; c<buflength;)
@@ -871,7 +949,7 @@ static uint E_CALL XEventThread(void * data)
 
 static X11Bool EventChecker(void *display, XEvent *event, char * data)
 {
-   return (!data || (event->type == (int) data)) && event->type != NoExpose && event->type != GraphicsExpose;
+   return (!data || (event->type == (int)(intptr_t) data)) && event->type != NoExpose && event->type != GraphicsExpose;
 }
 
 static X11Bool ConfigureNotifyChecker(void *display, XConfigureEvent *event, char * data)
@@ -879,6 +957,28 @@ static X11Bool ConfigureNotifyChecker(void *display, XConfigureEvent *event, cha
    return (!data || (event->window == (X11Window) data)) && event->type == ConfigureNotify;
 }
 
+static X11Bool FocusInChecker(void *display, XFocusChangeEvent *event, char * data)
+{
+   X11Bool result = False;
+   if(event->type == FocusIn)
+   {
+      Window window = null;
+      // --- This deadlocks and I think Xorg should fix this.
+      // XFindContext(xGlobalDisplay, event->window, windowContext, (XPointer *) &window);
+      if((X11Window)guiApp.desktop.windowHandle == event->window)
+         window = guiApp.desktop;
+      else
+      {
+         for(window = guiApp.desktop.firstChild; window; window = window.next)
+            if((X11Window)window.windowHandle == event->window)
+               break;
+      }
+      if(window)
+         result = True;
+   }
+   return result;
+}
+
 static enum FrameExtentSupport { unknown, working, broken };
 
 static FrameExtentSupport frameExtentSupported;
@@ -1019,7 +1119,6 @@ static void WaitForViewableWindow(Window window)
    while(true)
    {
       XWindowAttributes attributes = { 0 };
-      int result;
       if(!XGetWindowAttributes(xGlobalDisplay, (X11Window)window.windowHandle, &attributes))
          break;
       if(attributes.map_state == IsViewable)
@@ -1063,7 +1162,7 @@ static bool GetFrameExtents(Window window, bool update)
    int format;
    unsigned long len, fill;
    Atom type;
-   char * data = null;
+   byte * data = null;
 
    if(XGetWindowProperty(xGlobalDisplay, (X11Window)window.windowHandle,
       atoms[_net_frame_extents], 0, 4,
@@ -1123,13 +1222,13 @@ static bool GetFrameExtents(Window window, bool update)
    return result;
 }
 
-static bool WaitForFrameExtents(Window window)
+static bool WaitForFrameExtents(Window window, bool update)
 {
    int attempts = 0;
    //XFlush(xGlobalDisplay);
-   while(attempts++ < 10)
+   while(attempts++ < 40)
    {
-      if(GetFrameExtents(window, false)) return true;
+      if(GetFrameExtents(window, update)) return true;
       Sleep(1.0 / RESOLUTION);
    }
    return false;
@@ -1229,6 +1328,61 @@ static void SigIntHandler(int value)
       guiApp.desktop.Destroy(0);*/
 }
 
+static void X11UpdateState(Window window, bool * unmaximized)
+{
+   if(atomsSupported[_net_wm_state]) //window.nativeDecorations)
+   {
+      int format;
+      unsigned long len, fill;
+      Atom type;
+      byte * data = null;
+      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;
+         Atom * hints = (Atom *)data;
+         int c;
+         for(c = 0; c < len && hints[c]; c++)
+         {
+            if(hints[c] == atoms[_net_wm_state_maximized_vert])
+               maxVert = true;
+            else if(hints[c] == atoms[_net_wm_state_maximized_horz])
+               maxHorz = true;
+            else if(hints[c] == atoms[_net_wm_state_hidden])
+               isMinimized = true;
+         }
+         XFree(data);
+
+         if(maxVert && maxHorz)
+         {
+            if(window.state != maximized)
+            {
+               *&window.state = maximized;
+               if(!window.nativeDecorations)
+                  window.CreateSystemChildren();
+            }
+         }
+         else if(isMinimized)
+         {
+            if(window.state != minimized)
+            {
+               *&window.state = minimized;
+               if(!window.nativeDecorations)
+                  window.CreateSystemChildren();
+            }
+         }
+         else if(window.state != normal)
+         {
+            if(unmaximized && window.state == maximized)
+               *unmaximized = true;
+            *&window.state = normal;
+            if(!window.nativeDecorations)
+               window.CreateSystemChildren();
+         }
+      }
+   }
+}
+
 class XInterface : Interface
 {
    class_property(name) = "X";
@@ -1247,6 +1401,12 @@ class XInterface : Interface
 #endif
       xTerminate = false;
       xGlobalDisplay = XOpenDisplay(null);
+
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(__ANDROID__)
+      if(xGlobalDisplay)
+         XLockDisplay(xGlobalDisplay);
+#endif
+
       // XSynchronize(xGlobalDisplay, True);
       frameExtentSupported = unknown;
 
@@ -1255,19 +1415,20 @@ class XInterface : Interface
       joystickFD[2] = open("/dev/js2", O_RDONLY);
       joystickFD[3] = open("/dev/js3", O_RDONLY);
 
-      systemCursors[iBeam]    = XCreateFontCursor(xGlobalDisplay, XC_xterm);
-      systemCursors[cross]    = XCreateFontCursor(xGlobalDisplay, XC_tcross);
-      systemCursors[moving]   = XCreateFontCursor(xGlobalDisplay, XC_fleur);
-      systemCursors[sizeNESW] = XCreateFontCursor(xGlobalDisplay, XC_bottom_left_corner);
-      systemCursors[sizeNS]   = XCreateFontCursor(xGlobalDisplay, XC_sb_v_double_arrow);
-      systemCursors[sizeNWSE] = XCreateFontCursor(xGlobalDisplay, XC_bottom_right_corner);
-      systemCursors[sizeWE]   = XCreateFontCursor(xGlobalDisplay, XC_sb_h_double_arrow);
-      systemCursors[hand]     = XCreateFontCursor(xGlobalDisplay, XC_hand2);
-      systemCursors[arrow]    = XCreateFontCursor(xGlobalDisplay, XC_left_ptr);
-
       if(xGlobalDisplay)
       {
          XWindowAttributes attributes = { 0 };
+
+         systemCursors[iBeam]    = XCreateFontCursor(xGlobalDisplay, XC_xterm);
+         systemCursors[cross]    = XCreateFontCursor(xGlobalDisplay, XC_tcross);
+         systemCursors[moving]   = XCreateFontCursor(xGlobalDisplay, XC_fleur);
+         systemCursors[sizeNESW] = XCreateFontCursor(xGlobalDisplay, XC_bottom_left_corner);
+         systemCursors[sizeNS]   = XCreateFontCursor(xGlobalDisplay, XC_sb_v_double_arrow);
+         systemCursors[sizeNWSE] = XCreateFontCursor(xGlobalDisplay, XC_bottom_right_corner);
+         systemCursors[sizeWE]   = XCreateFontCursor(xGlobalDisplay, XC_sb_h_double_arrow);
+         systemCursors[hand]     = XCreateFontCursor(xGlobalDisplay, XC_hand2);
+         systemCursors[arrow]    = XCreateFontCursor(xGlobalDisplay, XC_left_ptr);
+
          XGetWindowAttributes(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), &attributes);
          xSystemDepth = attributes.depth;
          xSystemVisual = attributes.visual;
@@ -1338,7 +1499,7 @@ class XInterface : Interface
             Pixmap mask = XCreatePixmap(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), 1, 1, 1);
             XSetWindowAttributes attributes = { 0 };
 
-            XkbSetDetectableAutoRepeat(xGlobalDisplay, True, &autoRepeatDetectable);
+            XkbSetDetectableAutoRepeat(xGlobalDisplay, True, (int *)&autoRepeatDetectable);
 
             XInternAtoms(xGlobalDisplay, (char**)atomNames, AtomIdents::enumSize, False, atoms);
 
@@ -1456,7 +1617,7 @@ class XInterface : Interface
 
    void Terminate()
    {
-      XEvent e = { 0 };
+      //XEvent e = { 0 };
       xTerminate = true;
 
       // WHY WAS THIS COMMENTED HERE?
@@ -1553,7 +1714,7 @@ class XInterface : Interface
                         windowData = modalRoot ? modalRoot.windowData : window.windowData;
                         if(windowData && windowData.ic)
                         {
-                           // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
+                           // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, NULL);
                            XSetICFocus(windowData.ic);
                         }
                      }
@@ -1607,22 +1768,26 @@ class XInterface : Interface
                   static Point lastPos[3];
 
                   Modifiers keyFlags = 0;
-                  bool doubleClick;
-                  uint button, buttonDouble, whichButton;
-                  uint buttonMask;
+                  bool doubleClick = false;
+                  uint button = 0, buttonDouble = 0, whichButton;
+                  uint buttonMask = 0;
                   int x = event->x_root, y = event->y_root;
                   timeStamp = event->time;
                   if(event->button == Button1)
                   {
                      // Force a raise on click here to deal with confused active state preventing to bring the window up
-                     if(!atomsSupported[_net_active_window] && !window.isRemote)
-                        XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
-                     XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, CurrentTime);
+                     if(!fullScreenMode)
+                     {
+                        if(!atomsSupported[_net_active_window] && !window.isRemote)
+                           XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
+                        XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, CurrentTime);
+                     }
                      button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
                      buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
                      whichButton = 0;
                      buttonMask = Button1Mask;
                      keyFlags.left = true;
+                     buttonsState.left = true;
                   }
                   else if(event->button == Button3)
                   {
@@ -1631,26 +1796,31 @@ class XInterface : Interface
                      whichButton = 2;
                      buttonMask = Button3Mask;
                      keyFlags.right = true;
+                     buttonsState.right = true;
                   }
-                  else
+                  else if(event->button == Button2)
                   {
                      button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonDown;
                      buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleDoubleClick;
                      whichButton = 1;
                      buttonMask = Button2Mask;
                      keyFlags.middle = true;
+                     buttonsState.middle = true;
                   }
                   if(event->state & buttonMask)
                      break;
 
-                  doubleClick = event->time - lastTime[whichButton] < DBLCLICK_DELAY &&
-                     window == lastWindow[whichButton] &&
-                     Abs(event->x_root - lastPos[whichButton].x) < DBLCLICK_DELTA &&
-                     Abs(event->y_root - lastPos[whichButton].y) < DBLCLICK_DELTA;
-                  lastTime[whichButton] = doubleClick ? 0 : event->time;
-                  lastWindow[whichButton] = window;
-                  lastPos[whichButton].x = event->x_root;
-                  lastPos[whichButton].y = event->y_root;
+                  if(buttonMask)
+                  {
+                     doubleClick = event->time - lastTime[whichButton] < DBLCLICK_DELAY &&
+                        window == lastWindow[whichButton] &&
+                        Abs(event->x_root - lastPos[whichButton].x) < DBLCLICK_DELTA &&
+                        Abs(event->y_root - lastPos[whichButton].y) < DBLCLICK_DELTA;
+                     lastTime[whichButton] = doubleClick ? 0 : event->time;
+                     lastWindow[whichButton] = window;
+                     lastPos[whichButton].x = event->x_root;
+                     lastPos[whichButton].y = event->y_root;
+                  }
 
                   if(event->state & ShiftMask)     keyFlags.shift = true;
                   if(event->state & ControlMask)   keyFlags.ctrl = true;
@@ -1663,9 +1833,9 @@ class XInterface : Interface
                   incref window;
                   if(event->button == Button4 || event->button == Button5)
                   {
-                     window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, (event->button == Button4) ? wheelUp : wheelDown, 0);
+                     window.KeyMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnKeyHit, { modifiers = keyFlags, code = (event->button == Button4) ? wheelUp : wheelDown }, 0);
                   }
-                  else
+                  else if(button)
                   {
                      if(doubleClick)
                      {
@@ -1693,16 +1863,19 @@ class XInterface : Interface
                   {
                      button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonUp;
                      buttonMask = Button1Mask;
+                     buttonsState.left = false;
                   }
                   else if(event->button == Button3)
                   {
                      button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnRightButtonUp;
                      buttonMask = Button3Mask;
+                     buttonsState.right = false;
                   }
                   else
                   {
                      button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMiddleButtonUp;
                      buttonMask = Button2Mask;
+                     buttonsState.middle = false;
                   }
                   timeStamp = event->time;
                   if(!(event->state & buttonMask)) break;
@@ -1730,7 +1903,7 @@ class XInterface : Interface
                }
                case MotionNotify:
                {
-                  static uint lastTime = 0;
+                  //static uint lastTime = 0;
                   XMotionEvent * event = (XMotionEvent *) thisEvent;
                   while(XCheckIfEvent(xGlobalDisplay, (XEvent *)thisEvent, EventChecker, (void *)MotionNotify));
                   // if(event->time - lastTime > 15)
@@ -1747,11 +1920,16 @@ class XInterface : Interface
                      if(event->state & Button3Mask)   keyFlags.right = true;
                      //*XUnlockDisplay(xGlobalDisplay);
                      incref window;
+                     if(window == acquiredInputWindow)
+                     {
+                        lastMouse.x = event->x_root;
+                        lastMouse.y = event->y_root;
+                     }
                      window.MouseMessage(__ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnMouseMove,
                         event->x_root, event->y_root, &keyFlags, false, false);
                      delete window;
                      //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
-                     lastTime = (uint)event->time;
+                     //lastTime = (uint)event->time;
                   }
                   break;
                }
@@ -1808,9 +1986,30 @@ class XInterface : Interface
                }
                case FocusIn:
                {
+                  lastMouse = acquireStart;
+
+                  guiApp.SetAppFocus(true);
+
+                  X11UpdateState(window, null);
+
+                  if(fullScreenMode)
+                  {
+                     if(acquiredInputWindow)
+                     {
+                        GetMousePosition(&guiApp.acquiredMouseX, &guiApp.acquiredMouseY);
+                        lastMouse = { window.size.w/2, window.size.h/2 };
+                        SetMousePosition(lastMouse.x, lastMouse.y);
+                     }
+                     XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
+                     SetNETWMState((X11Window)window.windowHandle, true, add, atoms[_net_wm_state_fullscreen], 0);
+                     XGrabKeyboard(xGlobalDisplay, (X11Window)window.windowHandle, False,  GrabModeAsync, GrabModeAsync, CurrentTime);
+                     XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, timeStamp);
+                     XInterface::UpdateRootWindow(window);
+                     break;
+                  }
+
                   if(activeWindow != (X11Window)window.windowHandle)
                   {
-                     XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
                      Window modalRoot = window.FindModal();
                      XWindowData windowData;
 
@@ -1818,7 +2017,7 @@ class XInterface : Interface
 
                      if(window.parent && window == window.parent.activeChild) break;
                      incref window;
-                     //if(window.creationActivation == activate)
+                     //if(window.creationActivation == activate && guiApp.desktop.active)
                      {
                         if(modalRoot)
                            modalRoot.ExternalActivate(true, true, window, null); // lastActive);
@@ -1828,7 +2027,7 @@ class XInterface : Interface
                      windowData = modalRoot ? modalRoot.windowData : window.windowData;
                      if(windowData && windowData.ic)
                      {
-                        // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
+                        // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, NULL);
                         XSetICFocus(windowData.ic);
                      }
                      //delete lastActive;
@@ -1844,10 +2043,32 @@ class XInterface : Interface
                   //printf("Processing a FocusOut Event for %s (%x)\n", window._class.name, window);
 #endif
 
+                  /*
                   if(XCheckTypedWindowEvent(xGlobalDisplay, thisEvent->window, FocusIn, (XEvent *)thisEvent))
                   {
                      break;
                   }
+                  */
+
+                  if(XCheckIfEvent(xGlobalDisplay, (XEvent *)thisEvent, (void *)FocusInChecker, null))
+                  {
+                     if(!fullScreenMode)
+                        XPutBackEvent(xGlobalDisplay, (XEvent *)thisEvent);
+                     break;
+                  }
+
+
+                  if(fullScreenMode || (X11Window)window.windowHandle == activeWindow)
+                     guiApp.SetAppFocus(false);
+
+                  if(fullScreenMode)
+                  {
+                     //XUngrabKeyboard(xGlobalDisplay, CurrentTime);
+                     //SetNETWMState((X11Window)window.windowHandle, true, remove, atoms[_net_wm_state_fullscreen], 0);
+                     //XIconifyWindow(xGlobalDisplay, (X11Window)window.windowHandle, DefaultScreen(xGlobalDisplay));
+                     break;
+                  }
+
                   if(thisEvent->window == activeWindow)
                      activeWindow = (X11Window)null;
 #if 0
@@ -1918,8 +2139,7 @@ class XInterface : Interface
                   else
 #endif
                   {
-                     XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
-                     if(window != window.parent.activeChild && window != guiApp.interimWindow) break;
+                     if(window.parent && window != window.parent.activeChild && window != guiApp.interimWindow) break;
                      incref window;
 
 #ifdef _DEBUG
@@ -1944,68 +2164,19 @@ class XInterface : Interface
                {
                   XConfigureEvent * event = (XConfigureEvent *) thisEvent;
                   bool unmaximized = false;
-                  if(!window.visible) break;
+                  bool um = false;
+                  if(!window.visible || fullScreenMode) break;
                   while(XCheckIfEvent(xGlobalDisplay, (XEvent *)thisEvent, (void *)ConfigureNotifyChecker, (void *)window.windowHandle));
                   //if(event->x - desktopX != window.position.x || event->y - desktopY != window.position.y || event->width != window.size.w || event->height != window.size.h)
-
-                  if(atomsSupported[_net_wm_state]) //window.nativeDecorations)
-                  {
-                     int format;
-                     unsigned long len, fill;
-                     Atom type;
-                     char * data = null;
-                     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;
-                        Atom * hints = (Atom *)data;
-                        int c;
-                        for(c = 0; c < len && hints[c]; c++)
-                        {
-                           if(hints[c] == atoms[_net_wm_state_maximized_vert])
-                              maxVert = true;
-                           else if(hints[c] == atoms[_net_wm_state_maximized_horz])
-                              maxHorz = true;
-                           else if(hints[c] == atoms[_net_wm_state_hidden])
-                              isMinimized = true;
-                        }
-                        XFree(data);
-
-                        if(maxVert && maxHorz)
-                        {
-                           if(window.state != maximized)
-                           {
-                              *&window.state = maximized;
-                              if(!window.nativeDecorations)
-                                 window.CreateSystemChildren();
-                           }
-                        }
-                        else if(isMinimized)
-                        {
-                           if(window.state != minimized)
-                           {
-                              *&window.state = minimized;
-                              if(!window.nativeDecorations)
-                                 window.CreateSystemChildren();
-                           }
-                        }
-                        else if(window.state != normal)
-                        {
-                           if(window.state == maximized)
-                              unmaximized = true;
-                           *&window.state = normal;
-                           if(!window.nativeDecorations)
-                              window.CreateSystemChildren();
-                        }
-                     }
-                  }
+                  X11UpdateState(window, &um);
+                  unmaximized = false;
                   {
                      bool offset = false;
                      int x, y, w, h;
-                     if(unmaximized && window.nativeDecorations)
+                     if(unmaximized && window.nativeDecorations && ((BorderBits)window.borderStyle).fixed)
                      {
                         if(window.nativeDecorations && RequestFrameExtents((X11Window)window.windowHandle))
-                           WaitForFrameExtents(window);
+                           WaitForFrameExtents(window, false);
 
                         // Ensure we set the normal size anchor when un-maximizing
                         window.ComputeAnchors(window.normalAnchor, window.normalSizeAnchor, &x, &y, &w, &h);
@@ -2026,33 +2197,28 @@ class XInterface : Interface
 
                            if(x != rootX || y != rootY)
                            {
-                              /*if(event->send_event)
-                                 offset = true;*/
-                              x = rootX;
-                              y = rootY;
+                              if(!event->send_event)
+                              {
+                                 // offset = true;
+                                 x = rootX;
+                                 y = rootY;
+                              }
                            }
                         }
 
                         x -= desktopX;
                         y -= desktopY;
 
-                        if(window.nativeDecorations && window.state != maximized)
+                        if(window.nativeDecorations && window.state != maximized && ((BorderBits)window.borderStyle).fixed)
                         {
                            x -= windowData.decor.left;
                            y -= windowData.decor.top;
                            w += windowData.decor.left + windowData.decor.right;
                            h += windowData.decor.top + windowData.decor.bottom;
-
-                           /*
-                           x -= window.clientStart.x;
-                           y -= window.clientStart.y - (window.hasMenuBar ? skinMenuHeight : 0);
-                           w += window.size.w - window.clientSize.w;
-                           h += window.size.h - window.clientSize.h;
-                           */
                         }
                      }
 
-                     window.Position(x, y, w, h, true, true, true, true, false, unmaximized);
+                     window.Position(x, y, w, h, um, true, true, true, false, unmaximized);
 
                      // Break the anchors for moveable/resizable windows
                      // Avoid doing this if the translation wasn't in sync as it will cause the window to move around
@@ -2083,6 +2249,16 @@ class XInterface : Interface
                      Window modalRoot;
                      XWindowData windowData;
                      bool laterFocus;
+
+                     if(fullScreenMode)
+                     {
+                        XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
+                        XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, timeStamp);
+                        XGrabKeyboard(xGlobalDisplay, (X11Window)window.windowHandle, False,  GrabModeAsync, GrabModeAsync, CurrentTime);
+                        guiApp.SetAppFocus(true);
+                        break;
+                     }
+
                      //activeWindow = (X11Window)window.windowHandle;
 
                      timeStamp = (X11Time)event->data.l[1];
@@ -2106,7 +2282,7 @@ class XInterface : Interface
                            XFocusChangeEvent *event = (XFocusChangeEvent *) &checkEvent;
                            Window window;
                            XFindContext(xGlobalDisplay, event->window, windowContext, (XPointer *) &window);
-                           if(window != window.parent.activeChild) break;
+                           if(window.parent && window != window.parent.activeChild) break;
                            incref window;
 
       #ifdef _DEBUG
@@ -2122,6 +2298,7 @@ class XInterface : Interface
                         }
                      }
 
+                     lastMouse = acquireStart;
                      modalRoot = window.FindModal();
                      windowData = modalRoot ? modalRoot.windowData : window.windowData;
                      if(windowData)
@@ -2154,6 +2331,7 @@ class XInterface : Interface
 
                                  XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
                                  XSetInputFocus(xGlobalDisplay, (X11Window)modalRoot.windowHandle, RevertToParent, timeStamp);
+                                 guiApp.SetAppFocus(true);
                                  activeWindow = (X11Window)window.windowHandle;
 
                                  //XFlush(xGlobalDisplay);
@@ -2163,11 +2341,12 @@ class XInterface : Interface
                            else
                            {
                               XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, timeStamp);
+                              guiApp.SetAppFocus(true);
                               activeWindow = (X11Window)window.windowHandle;
                               window.ExternalActivate(true, true, window, null); // lastActive);
                               if(windowData && windowData.ic)
                               {
-                                 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, 0);
+                                 // XSetICValues(ic, XNClientWindow, window.windowHandle, XNFocusWindow, window.windowHandle, NULL);
                                  //XSetICFocus(windowData.ic);
                               }
                            }
@@ -2185,7 +2364,7 @@ class XInterface : Interface
                {
                   XWindowData windowData = window.windowData;
                   XPropertyEvent * event = (XPropertyEvent *) thisEvent;
-                  if(event->atom == atoms[_net_frame_extents] &&
+                  if(!fullScreenMode && event->atom == atoms[_net_frame_extents] &&
                     event->state == PropertyNewValue && windowData)
                   {
                      if(!GetFrameExtents(window, true))
@@ -2223,11 +2402,11 @@ class XInterface : Interface
       //*XUnlockDisplay(xGlobalDisplay);
    }
 
-   char ** GraphicsDrivers(int * numDrivers)
+   const char ** GraphicsDrivers(int * numDrivers)
    {
-      static char *graphicsDrivers[] = { "X", "OpenGL" };
+      static const char *graphicsDrivers[] = { "X", "OpenGL" };
       *numDrivers = sizeof(graphicsDrivers) / sizeof(char *);
-      return (char **)graphicsDrivers;
+      return (const char **)graphicsDrivers;
    }
 
    void GetCurrentMode(bool * fullScreen, int * resolution, int * colorDepth, int * refreshRate)
@@ -2252,8 +2431,8 @@ class XInterface : Interface
       }
       else
       {
-         static bool firstTime = true;
-         firstTime = false;
+         //static bool firstTime = true;
+         //firstTime = false;
          desktopX = desktopY = desktopW = desktopH = 0;
 
          RepositionDesktop(false);
@@ -2278,7 +2457,7 @@ class XInterface : Interface
       attributes.override_redirect = (window.interim || (!atomsSupported[_net_wm_state] && !window.nativeDecorations)) ? True : False;
       attributes.event_mask = EVENT_MASK;
       //printf("%s\n", guiApp.defaultDisplayDriver);
-#if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
+#if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D) && !defined(ECERE_NOGL) && !defined(__ODROID__)
       if(window.dispDriver == class(OpenGLDisplayDriver) || !strcmp(guiApp.defaultDisplayDriver, "OpenGL"))
       {
          int samples;
@@ -2377,6 +2556,20 @@ class XInterface : Interface
          };
          visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrList);
       }
+#elif defined(__ODROID__)
+      if(!visualInfo)
+      {
+         // System visual not working on ODROID?
+         int attrList[] =
+         {
+            GLX_USE_GL, GLX_DEPTH_SIZE, 1,
+            GLX_RGBA,
+            GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
+            GLX_DOUBLEBUFFER,
+            None
+         };
+         visualInfo = glXChooseVisual(xGlobalDisplay, DefaultScreen(xGlobalDisplay), attrList);
+      }
 #endif
       depth = visualInfo ? visualInfo->depth : (window.alphaBlend ? 32 : xSystemDepth);
       visual = visualInfo ? visualInfo->visual : (window.alphaBlend ? FindFullColorVisual (xGlobalDisplay, &depth) : xSystemVisual);
@@ -2392,9 +2585,39 @@ class XInterface : Interface
 
       if(fullScreenMode)
       {
-         windowHandle = XCreateWindow(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay),
-            0,0,guiApp.desktop.size.w,guiApp.desktop.size.h,0, depth, InputOutput, visual ? visual : CopyFromParent,
-            CWEventMask | (visual ? (CWColormap | CWBorderPixel) : 0)/*| CWOverrideRedirect*/, &attributes);
+         windowHandle = XCreateWindow(xGlobalDisplay,
+               DefaultRootWindow(xGlobalDisplay),
+               0,0,
+               XDisplayWidth(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),
+               XDisplayHeight(xGlobalDisplay, DefaultScreen(xGlobalDisplay)),
+               0, depth, InputOutput, visual ? visual : CopyFromParent,
+               CWEventMask | (visual ? (CWColormap | CWBorderPixel) : 0) | CWOverrideRedirect,
+               &attributes);
+
+         {
+            XSizeHints hints = { 0 };
+            XSetWMNormalHints(xGlobalDisplay, windowHandle, &hints);
+         }
+
+         {
+            String caption = window.caption;
+            XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_name],
+               atoms[utf8_string], 8, PropModeReplace, (byte *)window.caption, caption ? strlen(caption) : 0);
+            XChangeProperty(xGlobalDisplay, windowHandle, atoms[wm_name],
+               atoms[utf8_string], 8, PropModeReplace, (byte *)window.caption, caption ? strlen(caption) : 0);
+         }
+
+         SetNETWMState((X11Window)windowHandle, false, add, atoms[_net_wm_state_fullscreen], 0);
+         //SetNETWMState((X11Window)windowHandle, false, add, atoms[_net_wm_state_above], 0);
+         {
+            Atom hints[4];
+            int count;
+
+            hints[0] = atoms[_net_wm_window_type_normal];
+            count = 1;
+            XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_window_type], XA_ATOM, 32,
+               PropModeReplace, (unsigned char*)&hints, count);
+         }
 
          {
             XWMHints xwmHints;
@@ -2498,6 +2721,9 @@ class XInterface : Interface
                XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_window_type], XA_ATOM, 32,
                   PropModeReplace, (unsigned char*)&hints, count);
 
+               if(window.stayOnTop)
+                  SetNETWMState((X11Window)windowHandle, false, add, atoms[_net_wm_state_above], 0);
+
                {
                   XWMHints xwmHints;
                   xwmHints.flags = InputHint;
@@ -2544,7 +2770,7 @@ class XInterface : Interface
          int num_missing_charsets = 0;
          char *default_string;
          XFontSet fontset;
-         XRectangle area = { 0, 0,  400, 400 };
+         //XRectangle area = { 0, 0,  400, 400 };
          XVaNestedList argList;
 
          // sprintf(fontString, "-*-%s-*-r-*-*-*-%d-*-*-*-*-*-*", "Helvetica" /*window.font.faceName*/, (int)(window.font.size * 20));
@@ -2589,14 +2815,16 @@ class XInterface : Interface
       {
          if ( atoms[_motif_wm_hints] != None )
          {
+            BorderBits borderBits = (BorderBits)window.borderStyle;
+            bool hasTitleBar = borderBits.fixed;
             MWM_Hints hints
             {
-               (window.nativeDecorations ? 0 : MWM_HINTS_DECORATIONS)|MWM_HINTS_FUNCTIONS,
+               ((window.nativeDecorations && hasTitleBar) ? 0 : MWM_HINTS_DECORATIONS)|MWM_HINTS_FUNCTIONS,
                (window.hasClose ? MWM_FUNC_CLOSE : 0) |
-               (window.hasMaximize ? MWM_FUNC_MAXIMIZE : 0) |
-               (window.hasMinimize ? MWM_FUNC_MINIMIZE : 0) |
-               ((window.moveable || ((BorderBits)window.borderStyle).fixed) ? MWM_FUNC_MOVE : 0) |
-               (((BorderBits)window.borderStyle).sizable ? MWM_FUNC_RESIZE : 0),
+               (fullScreenMode || window.hasMaximize ? MWM_FUNC_MAXIMIZE : 0) |
+               (fullScreenMode || window.hasMinimize ? MWM_FUNC_MINIMIZE : 0) |
+               ((fullScreenMode || window.moveable || hasTitleBar) ? MWM_FUNC_MOVE : 0) |
+               (fullScreenMode || borderBits.sizable ? MWM_FUNC_RESIZE : 0),
                 0, 0, 0
             };
             XChangeProperty(xGlobalDisplay, windowHandle, atoms[_motif_wm_hints], atoms[_motif_wm_hints], 32,
@@ -2633,15 +2861,28 @@ class XInterface : Interface
          XUngrabPointer(xGlobalDisplay, CurrentTime);
       }
 
-      if(!window.nativeDecorations || !RequestFrameExtents(windowHandle))
+      if(fullScreenMode || !window.nativeDecorations ||  !((BorderBits)window.borderStyle).fixed || !RequestFrameExtents(windowHandle))
          ((XWindowData)window.windowData).gotFrameExtents = true;
 
+      window.windowHandle = (void *)windowHandle;
+      if(window.state != maximized)
+         WaitForFrameExtents(window, true);
+
+      //GetFrameExtents(window, true);
+
+      if(fullScreenMode)
+      {
+         XMapWindow(xGlobalDisplay, windowHandle);
+         XGrabKeyboard(xGlobalDisplay, windowHandle, False,  GrabModeAsync, GrabModeAsync, CurrentTime);
+      }
       return (void *)windowHandle;
    }
 
    void DestroyRootWindow(Window window)
    {
       XEvent event;
+      if(window == acquiredInputWindow)
+         delete acquiredInputWindow;
 
       XDeleteContext(xGlobalDisplay, (XID)window, windowContext);
       XSaveContext(xGlobalDisplay, (X11Window)window.windowHandle, windowContext, null);
@@ -2665,7 +2906,7 @@ class XInterface : Interface
 
    // -- Window manipulation ---
 
-   void SetRootWindowCaption(Window window, char * name)
+   void SetRootWindowCaption(Window window, const char * name)
    {
       if(window.windowHandle)
       {
@@ -2681,9 +2922,13 @@ class XInterface : Interface
       //Logf("Position root window %s\n", window.name);
       if(window.windowHandle && (!window.parent || !window.parent.display))
       {
-         bool visible = window.visible;
          if(window.visible && window.created)
+         {
+            long t = (window.creationActivation == activate && guiApp.desktop.active) ? (int)timeStamp : 0;
+            XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_user_time],
+              XA_CARDINAL,32,PropModeReplace, (byte *)&t, 1);
             XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
+         }
          if(window.state == minimized && atomsSupported[_net_wm_state]) return;
 
          if(window.nativeDecorations)
@@ -2695,7 +2940,7 @@ class XInterface : Interface
                // && window.state != maximized -- required for Cinnamon on Mint 14/15
             if(!windowData.gotFrameExtents && window.state != maximized)
             {
-               if(WaitForFrameExtents(window))
+               if(WaitForFrameExtents(window, false))
                {
                   x += windowData.decor.left;
                   y += windowData.decor.top ;
@@ -2722,7 +2967,7 @@ class XInterface : Interface
          x += desktopX;
          y += desktopY;
 
-         if(!atomsSupported[_net_wm_state] || window.state != maximized)
+         if(!fullScreenMode && (!atomsSupported[_net_wm_state] || (window.state != maximized || !window.visible)))
          {
             if(move && resize)
                XMoveResizeWindow(xGlobalDisplay, (X11Window)window.windowHandle, x, y, w, h);
@@ -2732,7 +2977,7 @@ class XInterface : Interface
                XResizeWindow(xGlobalDisplay, (X11Window)window.windowHandle, w, h);
 
             // Reset min/max for fixed size windows on WMs not looking at MWM_FUNC_RESIZE (e.g. Cinnamon)
-            if(window.style.fixed && !window.style.sizable)
+            if(window.style.fixed && !window.style.sizable && window.state != maximized)
             {
                XSizeHints hints = { 0 };
                long supplied;
@@ -2748,7 +2993,7 @@ class XInterface : Interface
 
    void OrderRootWindow(Window window, bool topMost)
    {
-
+      SetNETWMState((X11Window)window.windowHandle, false, topMost ? add : remove, atoms[_net_wm_state_above], 0);
    }
 
    void SetRootWindowColor(Window window)
@@ -2785,6 +3030,8 @@ class XInterface : Interface
 
    void SetRootWindowState(Window window, WindowState state, bool visible)
    {
+      WindowState curState = window.state;
+      *&window.state = state;
       // Old WM (e.g. TWM), use built-in decorations
       if(!atomsSupported[_net_wm_state])
          window.nativeDecorations = false;
@@ -2796,16 +3043,30 @@ class XInterface : Interface
          {
             if(!windowData.currentlyVisible)
             {
+               long t = (window.creationActivation == activate && guiApp.desktop.active) ? timeStamp : 0;
+               XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_user_time],
+                 XA_CARDINAL,32,PropModeReplace, (byte *)&t, 1);
                XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
                windowData.currentlyVisible = true;
                WaitForViewableWindow(window);
-               if(window.creationActivation == activate && state != minimized)
+               if(window.creationActivation == activate && guiApp.desktop.active && state != minimized)
                   ActivateRootWindow(window);
             }
 
+            if(fullScreenMode && state != minimized)
+            {
+               int w = XDisplayWidth(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
+               int h = XDisplayHeight(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
+               SetNETWMState((X11Window)window.windowHandle, true, add, atoms[_net_wm_state_fullscreen], 0);
+               XMoveResizeWindow(xGlobalDisplay, (X11Window)window.windowHandle, 0, 0, w, h);
+
+               guiApp.SetDesktopPosition(0, 0, w, h, true);
+               window.Position(0, 0, w, h, true, true, true, true, false, false);
+            }
+
             if(state == minimized && atomsSupported[_net_wm_state])
             {
-               uint iconic = IconicState;
+               //uint iconic = IconicState;
 
                // SetNETWMState(window.windowHandle, true, add, atoms[_net_wm_state_hidden], null);
                /*
@@ -2826,9 +3087,10 @@ class XInterface : Interface
                */
 
                // printf("Attempting to minimize %s\n", window._class.name);
-               XIconifyWindow(xGlobalDisplay, (X11Window)window.windowHandle, DefaultScreen(xGlobalDisplay));
+               if(!fullScreenMode)
+                  XIconifyWindow(xGlobalDisplay, (X11Window)window.windowHandle, DefaultScreen(xGlobalDisplay));
             }
-            else
+            else if(!fullScreenMode)
             {
                //((XWindowData)window.windowData).gotFrameExtents && (!window.nativeDecorations || window.state == state))
                if(!atomsSupported[_net_wm_state] || (!((XWindowData)window.windowData).gotFrameExtents && window.state == maximized))
@@ -2862,8 +3124,10 @@ class XInterface : Interface
                if(atomsSupported[_net_wm_state])
                {
                   // Maximize / Restore the window
-                  SetNETWMState((X11Window)window.windowHandle, true, state == maximized ? add: remove,
-                     atoms[_net_wm_state_maximized_vert], atoms[_net_wm_state_maximized_horz]);
+                  if(curState != state)
+                     SetNETWMState((X11Window)window.windowHandle, true, state == maximized ? add : remove,
+                        atoms[_net_wm_state_maximized_vert], atoms[_net_wm_state_maximized_horz]);
+
                   if(state == maximized)
                   {
                      // Prevent the code in ConfigureNotify to think the window has been unmaximized
@@ -2881,12 +3145,18 @@ class XInterface : Interface
          }
          //XFlush(xGlobalDisplay);
       }
+      *&window.state = curState;
    }
 
    void FlashRootWindow(Window window)
    {
+      void * hwnd = window.windowHandle;
+      Window master = window.master, rootWindow = (master && master != guiApp.desktop) ? master.rootWindow : null;
+      if(!window.style.showInTaskBar && rootWindow && (window._isModal || window.style.interim))
+         hwnd = rootWindow.windowHandle;
+
       // printf("Attempting to flash root window\n");
-      SetNETWMState((X11Window)window.windowHandle, true, add, atoms[_net_wm_state_demands_attention], 0);
+      SetNETWMState((X11Window)hwnd, true, add, atoms[_net_wm_state_demands_attention], 0);
    }
 
    void ActivateRootWindow(Window window)
@@ -2899,6 +3169,9 @@ class XInterface : Interface
             //printf("Activate root window %s\n", window._class.name);
             if(!windowData.currentlyVisible)
             {
+               long t = (window.creationActivation == activate && guiApp.desktop.active) ? timeStamp : 0;
+               XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_user_time],
+                 XA_CARDINAL,32,PropModeReplace, (byte *)&t, 1);
                XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
                WaitForViewableWindow(window);
                windowData.currentlyVisible = true;
@@ -2915,7 +3188,10 @@ class XInterface : Interface
                event.send_event = 1;
                event.format = 32;
                event.data.l[0] = /*0*/ 1;
-               event.data.l[1] = timeStamp;
+               // WMs will complain about using CurrentTime here, but when ActivateRootWindow() is called we really need to take over,
+               // otherwise a debugged application stays on top of the IDE when we hit a breakpoint (there was no user interaction with the IDE,
+               // but it really should be activated)
+               event.data.l[1] = CurrentTime; //timeStamp;
                event.data.l[2] = activeWindow; //guiApp.desktop.activeChild.windowHandle;
 
 #ifdef _DEBUG
@@ -2962,7 +3238,8 @@ class XInterface : Interface
 
    void SetMousePosition(int x, int y)
    {
-
+      XWarpPointer(xGlobalDisplay, None, DefaultRootWindow(xGlobalDisplay), 0, 0, 0, 0, x, y);
+      XFlush(xGlobalDisplay);
    }
 
    void SetMouseRange(Window window, Box box)
@@ -3040,7 +3317,7 @@ class XInterface : Interface
    {
       if(window.rootWindow.windowHandle)
          XDefineCursor(xGlobalDisplay, (X11Window) window.rootWindow.windowHandle,
-            cursor == -1 ? nullCursor : systemCursors[(SystemCursor)cursor]);
+            (acquiredInputWindow || cursor == -1) ? nullCursor : systemCursors[(SystemCursor)cursor]);
    }
 
    // --- Caret ---
@@ -3060,7 +3337,7 @@ class XInterface : Interface
                (short)(caretOwner.caretPos.y - caretOwner.scroll.y + caretOwner.absPosition.y - window.absPosition.y)
             };
             XVaNestedList argList = XVaCreateNestedList(0, XNSpotLocation, &cursor_location, NULL);
-            XSetICValues(windowData.ic, XNPreeditAttributes, argList, 0);
+            XSetICValues(windowData.ic, XNPreeditAttributes, argList, NULL);
          }
       }
    }
@@ -3164,7 +3441,7 @@ class XInterface : Interface
                               AnyPropertyType, &type,&format,&len, &dummy, &data) == Success)
                         {
                            clipBoard.text = new char[size+1];
-                           strncpy(clipBoard.text, data, size);
+                           strncpy(clipBoard.text, (char *)data, size);
                            clipBoard.text[size] = '\0';
                            XFree(data);
                            result = true;
@@ -3189,14 +3466,36 @@ class XInterface : Interface
 
    bool AcquireInput(Window window, bool state)
    {
-      return false;
+      if(state && window)
+      {
+         GetMousePosition(&acquireStart.x, &acquireStart.y);
+         lastMouse = acquireStart;
+         acquiredInputWindow = window;
+         incref acquiredInputWindow;
+      }
+      else if(acquiredInputWindow)
+         delete acquiredInputWindow;
+      return true;
    }
 
    bool GetMouseState(MouseButtons * buttons, int * x, int * y)
    {
       bool result = false;
-      if(x) *x = 0;
-      if(y) *y = 0;
+      if(acquiredInputWindow && guiApp.desktop.active)
+      {
+         if(x) *x = lastMouse.x - acquireStart.x;
+         if(y) *y = lastMouse.y - acquireStart.y;
+         *buttons = buttonsState;
+         SetMousePosition(acquireStart.x, acquireStart.y);
+         lastMouse = acquireStart;
+         result = true;
+      }
+      else
+      {
+         if(x) *x = 0;
+         if(y) *y = 0;
+         if(buttons) *buttons = 0;
+      }
       return result;
    }
 
@@ -3222,8 +3521,33 @@ class XInterface : Interface
 
    bool GetKeyState(Key key)
    {
-      int keyState = 0;
-      return keyState;
+      if(key == capsState || key == numState || key == scrollState)
+      {
+         Atom atom = None;
+         X11Bool state = 0;
+         int idx = 0;
+         switch(key)
+         {
+            case capsState:    atom = atoms[capsLock];   idx = 0; break;
+            case numState:     atom = atoms[numLock];    idx = 1; break;
+            case scrollState:  atom = atoms[scrollLock]; idx = 2; break;
+         }
+         /*XkbGetIndicatorState(xGlobalDisplay, XkbUseCoreKbd, &state);
+         state = (state & (1 << idx)) ? 1 : 0;*/
+         XkbGetNamedIndicator(xGlobalDisplay, /*XkbUseCoreKbd,*/ atom, &idx, &state, NULL, NULL);
+         return (bool)state;
+      }
+      else
+      {
+         if(key == alt)
+            return keyStates[leftAlt] || keyStates[rightAlt];
+         else if(key == shift)
+            return keyStates[leftShift] || keyStates[rightShift];
+         else if(key == control)
+            return keyStates[leftControl] || keyStates[rightControl];
+         else
+            return keyStates[key.code];
+      }
    }
 
    void SetTimerResolution(uint hertz)
@@ -3268,12 +3592,12 @@ class XInterface : Interface
    }
 }
 
-default dllexport void * __attribute__((stdcall)) IS_XGetDisplay()
+default dllexport void * IS_XGetDisplay()
 {
    return xGlobalDisplay;
 }
 
-default dllexport void * __attribute__((stdcall)) IS_XGetWindow(Window window)
+default dllexport void * IS_XGetWindow(Window window)
 {
    return window.windowHandle ? window.windowHandle : window.rootWindow.windowHandle;
 }