ecere/gui/drivers/XInterface: Setting global app focus
[sdk] / ecere / src / gui / drivers / XInterface.ec
index 7119cb9..7dda68d 100644 (file)
@@ -33,6 +33,10 @@ default:
 #define Time      X11Time
 #define KeyCode   X11KeyCode
 #define Picture   X11Picture
+#define Bool      X11Bool
+
+#define _XTYPEDEF_BOOL
+typedef int X11Bool;
 
 #include <X11/Xatom.h>
 #include <X11/Xlib.h>
@@ -48,6 +52,7 @@ default:
 #include <X11/extensions/Xrender.h>
 #include <X11/extensions/XShm.h>
 
+#undef Bool
 #undef Picture
 #undef Window
 #undef Cursor
@@ -106,7 +111,8 @@ 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_modal, app_selection, _net_supported
+   _net_wm_state_maximized_vert, _net_wm_state_maximized_horz, _net_wm_state_modal, app_selection, _net_supported,
+   _net_wm_state_skip_taskbar
 };
 
 static Atom atoms[AtomIdents];
@@ -153,14 +159,14 @@ static const char *atomNames[AtomIdents] = {
    "_NET_WM_STATE_MAXIMIZED_HORZ", // _net_wm_state_maximized_horz
    "_NET_WM_STATE_MODAL", // _net_wm_state_modal
    "APP_SELECTION",
-   "_NET_SUPPORTED"
+   "_NET_SUPPORTED",
+   "_NET_WM_STATE_SKIP_TASKBAR"
 };
 /*
 _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_TASKBAR, ATOM
 _NET_WM_STATE_SKIP_PAGER, ATOM
 _NET_WM_STATE_HIDDEN, ATOM
 _NET_WM_STATE_FULLSCREEN, ATOM
@@ -251,6 +257,7 @@ public:
    // Decorations Size
    Box decor;
    bool gotFrameExtents;
+   bool currentlyVisible;
 };
 
 bool XGetBorderWidths(Window window, Box box)
@@ -862,12 +869,12 @@ static uint E_CALL XEventThread(void * data)
 }
 */
 
-static Bool EventChecker(void *display, XEvent *event, char * data)
+static X11Bool EventChecker(void *display, XEvent *event, char * data)
 {
    return (!data || (event->type == (int) data)) && event->type != NoExpose && event->type != GraphicsExpose;
 }
 
-static Bool ConfigureNotifyChecker(void *display, XConfigureEvent *event, char * data)
+static X11Bool ConfigureNotifyChecker(void *display, XConfigureEvent *event, char * data)
 {
    return (!data || (event->window == (X11Window) data)) && event->type == ConfigureNotify;
 }
@@ -1362,12 +1369,6 @@ class XInterface : Interface
                }
             }
 
-            {
-               Atom protocols[2] = { atoms[wm_delete_window], atoms[wm_take_focus] };
-
-               XSetWMProtocols(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), protocols, 2);
-            }
-
             /*
             if(atomsSupported[_net_workarea])
                printf("Warning: _NET_WORKAREA extension not supported\n");
@@ -1616,7 +1617,7 @@ class XInterface : Interface
                      // 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, RevertToPointerRoot, CurrentTime);
+                     XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, CurrentTime);
                      button = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftButtonDown;
                      buttonDouble = __ecereVMethodID___ecereNameSpace__ecere__gui__Window_OnLeftDoubleClick;
                      whichButton = 0;
@@ -1758,15 +1759,10 @@ class XInterface : Interface
                {
                   XExposeEvent * event = (XExposeEvent *) thisEvent;
                   Box box;
-                  box.left = event->x - window.clientStart.x;
-                  box.top = event->y - window.clientStart.y;
+                  box.left = event->x;
+                  box.top = event->y;
                   box.right = box.left + event->width - 1;
                   box.bottom = box.top + event->height - 1;
-                  window.Update(box);
-                  box.left   += window.clientStart.x;
-                  box.top    += window.clientStart.y;
-                  box.right  += window.clientStart.x;
-                  box.bottom += window.clientStart.y;
                   window.UpdateDirty(box);
                   break;
                }
@@ -1812,6 +1808,8 @@ class XInterface : Interface
                }
                case FocusIn:
                {
+                  guiApp.SetAppFocus(true);
+
                   if(activeWindow != (X11Window)window.windowHandle)
                   {
                      XFocusChangeEvent *event = (XFocusChangeEvent *) thisEvent;
@@ -1844,6 +1842,9 @@ class XInterface : Interface
                }
                case FocusOut:
                {
+                  if((X11Window)window.windowHandle == activeWindow)
+                     guiApp.SetAppFocus(false);
+
 #ifdef _DEBUG
                   //printf("Processing a FocusOut Event for %s (%x)\n", window._class.name, window);
 #endif
@@ -1948,6 +1949,7 @@ class XInterface : Interface
                {
                   XConfigureEvent * event = (XConfigureEvent *) thisEvent;
                   bool unmaximized = false;
+                  if(!window.visible) 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)
 
@@ -2005,7 +2007,7 @@ class XInterface : Interface
                   {
                      bool offset = false;
                      int x, y, w, h;
-                     if(unmaximized)
+                     if(unmaximized && window.nativeDecorations)
                      {
                         if(window.nativeDecorations && RequestFrameExtents((X11Window)window.windowHandle))
                            WaitForFrameExtents(window);
@@ -2026,11 +2028,13 @@ class XInterface : Interface
                            XTranslateCoordinates(xGlobalDisplay, event->window,
                               RootWindow(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 0, 0,
                               &rootX, &rootY, &rootChild);
+
                            if(x != rootX || y != rootY)
                            {
+                              /*if(event->send_event)
+                                 offset = true;*/
                               x = rootX;
                               y = rootY;
-                              offset = true;
                            }
                         }
 
@@ -2084,7 +2088,7 @@ class XInterface : Interface
                      Window modalRoot;
                      XWindowData windowData;
                      bool laterFocus;
-                     activeWindow = (X11Window)window.windowHandle;
+                     //activeWindow = (X11Window)window.windowHandle;
 
                      timeStamp = (X11Time)event->data.l[1];
 
@@ -2154,7 +2158,9 @@ class XInterface : Interface
 #endif
 
                                  XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
-                                 XSetInputFocus(xGlobalDisplay, (X11Window)modalRoot.windowHandle, RevertToPointerRoot, timeStamp);
+                                 XSetInputFocus(xGlobalDisplay, (X11Window)modalRoot.windowHandle, RevertToParent, timeStamp);
+                                 guiApp.SetAppFocus(true);
+                                 activeWindow = (X11Window)window.windowHandle;
 
                                  //XFlush(xGlobalDisplay);
                                  //printf("Done.\n");
@@ -2162,7 +2168,9 @@ class XInterface : Interface
                            }
                            else
                            {
-                              XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, timeStamp);
+                              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)
                               {
@@ -2394,6 +2402,17 @@ class XInterface : Interface
          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);
+
+         {
+            XWMHints xwmHints;
+            xwmHints.flags = InputHint;
+            xwmHints.input = 0;
+            XSetWMHints(xGlobalDisplay, windowHandle, &xwmHints);
+         }
+         {
+            Atom protocols[2] = { atoms[wm_delete_window], atoms[wm_take_focus] };
+            XSetWMProtocols(xGlobalDisplay, windowHandle, protocols, 2);
+         }
       }
       /*
          Unsupported for now...
@@ -2459,18 +2478,33 @@ class XInterface : Interface
             }
 
             {
-               Atom hints[2] =
+               Atom hints[4];
+               int count;
+               if(parentWindow && window.interim)
+               {
+                  hints[0] = atoms[_net_wm_window_type_dropdown_menu];
+                  hints[1] = atoms[_net_wm_window_type_popup_menu];
+                  hints[2] = atoms[_net_wm_window_type_menu];
+                  count = 3;
+               }
+               else if(parentWindow)
+               {
+                  hints[0] = atoms[_net_wm_window_type_normal];
+                  SetNETWMState(windowHandle, false, add, atoms[_net_wm_state_skip_taskbar], 0);
+
+                  // Some WMs won't show a close button if dialog is set
+                  // Additionally, this casues jumping of all dialog windows on Cinnamon
+                  //hints[0] = atoms[_net_wm_window_type_dialog];
+                  count = 1;
+               }
+               else
                {
-                  parentWindow ? atoms[_net_wm_window_type_menu] : atoms[_net_wm_window_type_normal],
-                  parentWindow ? atoms[_net_wm_window_type_popup_menu] : 0
+                  hints[0] = atoms[_net_wm_window_type_normal];
+                  count = 1;
                };
-#if defined(__APPLE__) || defined(__FreeBSD__)
-               // Don't set this on non-interim windows for OS X...
-               if(parentWindow && window.interim)
-#endif
+               XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_window_type], XA_ATOM, 32,
+                  PropModeReplace, (unsigned char*)&hints, count);
 
-                  XChangeProperty(xGlobalDisplay, windowHandle, atoms[_net_wm_window_type], XA_ATOM, 32,
-                     PropModeReplace, (unsigned char*)&hints, parentWindow ? 2 : 1);
                {
                   XWMHints xwmHints;
                   xwmHints.flags = InputHint;
@@ -2483,7 +2517,6 @@ class XInterface : Interface
                }
 
                // Set Normal hints for minimum/maximum size
-               if(true)
                {
                   XSizeHints hints = { 0 };
                   hints.min_width = minW;
@@ -2587,16 +2620,12 @@ class XInterface : Interface
          }
       }
 
-      /*
       {
-         Atom protocolsAtom = XInternAtom(xGlobalDisplay, "WM_PROTOCOLS", False);
-         if ( protocolsAtom != None )
-         {
-            MWM_Hints hints = { MWM_HINTS_DECORATIONS|MWM_HINTS_FUNCTIONS, 0, 0, 0, 0 };
-            XChangeProperty(xGlobalDisplay, windowHandle, atoms[_motif_wm_hints], atoms[_motif_wm_hints], 32,
-               PropModeReplace, (unsigned char*)&hints, sizeof(hints)/4);
-         }
-      }*/
+         XWMHints wmHints = { 0 };
+         wmHints.input = True;
+         XSetWMHints(xGlobalDisplay, windowHandle, &wmHints);
+      }
+
       // XFlush(xGlobalDisplay);
       window.windowData = XWindowData { visualInfo, ic };
 
@@ -2713,16 +2742,14 @@ class XInterface : Interface
             if(window.style.fixed && !window.style.sizable)
             {
                XSizeHints hints = { 0 };
+               long supplied;
+               XGetWMNormalHints(xGlobalDisplay, (X11Window)window.windowHandle, &hints, &supplied);
                hints.min_width = hints.max_width = w;
                hints.min_height = hints.max_height = h;
-               hints.flags |= PMinSize|PMaxSize|PPosition|PSize;
+               hints.flags |= PMinSize|PMaxSize;
                XSetWMNormalHints(xGlobalDisplay, (X11Window)window.windowHandle, &hints);
             }
          }
-#if defined(__APPLE__)
-//         if(window.created && !visible)
-  //          XUnmapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
-#endif
       }
    }
 
@@ -2770,13 +2797,18 @@ class XInterface : Interface
          window.nativeDecorations = false;
       if(!window.parent || !window.parent.display)
       {
+         XWindowData windowData = window.windowData;
          //Logf("Set root window state %d %s\n", state, window.name);
          if(visible)
          {
-            XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
-            WaitForViewableWindow(window);
-            if(window.creationActivation == activate && state != minimized)
-               ActivateRootWindow(window);
+            if(!windowData.currentlyVisible)
+            {
+               XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
+               windowData.currentlyVisible = true;
+               WaitForViewableWindow(window);
+               if(window.creationActivation == activate && state != minimized)
+                  ActivateRootWindow(window);
+            }
 
             if(state == minimized && atomsSupported[_net_wm_state])
             {
@@ -2850,7 +2882,10 @@ class XInterface : Interface
             }
          }
          else
+         {
             XUnmapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
+            windowData.currentlyVisible = false;
+         }
          //XFlush(xGlobalDisplay);
       }
    }
@@ -2867,10 +2902,15 @@ class XInterface : Interface
       {
          if(!window.style.hidden && window.created)
          {
+            XWindowData windowData = window.windowData;
             //printf("Activate root window %s\n", window._class.name);
+            if(!windowData.currentlyVisible)
+            {
+               XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
+               WaitForViewableWindow(window);
+               windowData.currentlyVisible = true;
+            }
             XRaiseWindow(xGlobalDisplay, (X11Window)window.windowHandle);
-            XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
-            WaitForViewableWindow(window);
             if(atomsSupported[_net_active_window])
             {
                XClientMessageEvent event = { 0 };
@@ -2891,11 +2931,11 @@ class XInterface : Interface
 
                XSendEvent(xGlobalDisplay, DefaultRootWindow(xGlobalDisplay), bool::false, SubstructureRedirectMask | SubstructureNotifyMask, (union _XEvent *)&event);
 //#if defined(__APPLE__)
-               XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, CurrentTime);
+               XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, CurrentTime);
 //#endif
             }
             else
-               XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToPointerRoot, CurrentTime);
+               XSetInputFocus(xGlobalDisplay, (X11Window)window.windowHandle, RevertToParent, CurrentTime);
          }
       }
    }
@@ -3007,7 +3047,7 @@ class XInterface : Interface
    {
       if(window.rootWindow.windowHandle)
          XDefineCursor(xGlobalDisplay, (X11Window) window.rootWindow.windowHandle,
-            cursor == -1 ? (X11Cursor)0 : systemCursors[(SystemCursor)cursor]);
+            cursor == -1 ? nullCursor : systemCursors[(SystemCursor)cursor]);
    }
 
    // --- Caret ---