ecere: Initial Emscripten support
[sdk] / ecere / src / gui / drivers / XInterface.ec
index e0e7fb5..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,7 +114,7 @@ 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];
@@ -172,7 +191,7 @@ static const char *atomNames[AtomIdents] = {
    "_NET_WM_STATE_ABOVE",
    "Caps Lock",
    "Num Lock",
-   "Scroll Lock",
+   "Scroll Lock"
 };
 /*
 _NET_WM_STATE_STICKY, ATOM
@@ -195,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;
@@ -317,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;
@@ -342,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;
       }
@@ -815,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;
    }
@@ -856,7 +876,8 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
    {
       int numBytes;
 
-      if(code < KeyCode::enumSize) keyStates[code] = false;
+      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);
@@ -867,7 +888,8 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
       int c;
       if(release == 0)
       {
-         if(code < KeyCode::enumSize) keyStates[code] = true;
+         if(key < KeyCode::enumSize)
+            keyStates[key] = true;
 
          if(windowData.ic && buflength)
          {
@@ -927,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)
@@ -1097,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)
@@ -1141,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,
@@ -1314,7 +1335,7 @@ static void X11UpdateState(Window window, bool * unmaximized)
       int format;
       unsigned long len, fill;
       Atom type;
-      char * data = null;
+      byte * data = null;
       if(XGetWindowProperty(xGlobalDisplay, (X11Window)window.systemHandle, atoms[_net_wm_state], 0, 32, False,
              XA_ATOM, &type, &format, &len, &fill, &data) == Success)
       {
@@ -1478,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);
 
@@ -1596,7 +1617,7 @@ class XInterface : Interface
 
    void Terminate()
    {
-      XEvent e = { 0 };
+      //XEvent e = { 0 };
       xTerminate = true;
 
       // WHY WAS THIS COMMENTED HERE?
@@ -1693,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);
                         }
                      }
@@ -1747,9 +1768,9 @@ 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)
@@ -1777,7 +1798,7 @@ class XInterface : Interface
                      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;
@@ -1789,14 +1810,17 @@ class XInterface : Interface
                   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;
@@ -1809,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)
                      {
@@ -1879,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)
@@ -1905,7 +1929,7 @@ class XInterface : Interface
                         event->x_root, event->y_root, &keyFlags, false, false);
                      delete window;
                      //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
-                     lastTime = (uint)event->time;
+                     //lastTime = (uint)event->time;
                   }
                   break;
                }
@@ -1979,14 +2003,13 @@ class XInterface : Interface
                      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);
-                        (xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, timeStamp);
+                     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;
 
@@ -1994,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);
@@ -2004,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;
@@ -2116,7 +2139,6 @@ class XInterface : Interface
                   else
 #endif
                   {
-                     XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
                      if(window.parent && window != window.parent.activeChild && window != guiApp.interimWindow) break;
                      incref window;
 
@@ -2142,11 +2164,12 @@ class XInterface : Interface
                {
                   XConfigureEvent * event = (XConfigureEvent *) thisEvent;
                   bool unmaximized = false;
+                  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)
-
-                  X11UpdateState(window, &unmaximized);
+                  X11UpdateState(window, &um);
+                  unmaximized = false;
                   {
                      bool offset = false;
                      int x, y, w, h;
@@ -2174,10 +2197,12 @@ 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;
+                              }
                            }
                         }
 
@@ -2193,7 +2218,7 @@ class XInterface : Interface
                         }
                      }
 
-                     window.Position(x, y, w, h, false /*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
@@ -2321,7 +2346,7 @@ class XInterface : Interface
                               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);
                               }
                            }
@@ -2377,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)
@@ -2406,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);
@@ -2432,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;
@@ -2531,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);
@@ -2682,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;
@@ -2728,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));
@@ -2864,7 +2906,7 @@ class XInterface : Interface
 
    // -- Window manipulation ---
 
-   void SetRootWindowCaption(Window window, char * name)
+   void SetRootWindowCaption(Window window, const char * name)
    {
       if(window.windowHandle)
       {
@@ -2880,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)
@@ -2921,7 +2967,7 @@ class XInterface : Interface
          x += desktopX;
          y += desktopY;
 
-         if(!fullScreenMode && (!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);
@@ -2931,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;
@@ -2947,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)
@@ -2984,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;
@@ -2995,10 +3043,13 @@ 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);
             }
 
@@ -3015,7 +3066,7 @@ class XInterface : Interface
 
             if(state == minimized && atomsSupported[_net_wm_state])
             {
-               uint iconic = IconicState;
+               //uint iconic = IconicState;
 
                // SetNETWMState(window.windowHandle, true, add, atoms[_net_wm_state_hidden], null);
                /*
@@ -3073,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
@@ -3092,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)
@@ -3110,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;
@@ -3275,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);
          }
       }
    }
@@ -3379,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;
@@ -3476,7 +3538,16 @@ class XInterface : Interface
          return (bool)state;
       }
       else
-         return keyStates[key.code];
+      {
+         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)
@@ -3521,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;
 }