ecere/gui/drivers/XInterface: Fixed uninitialized warnings
[sdk] / ecere / src / gui / drivers / XInterface.ec
index b74ed32..e40f6a6 100644 (file)
@@ -1,11 +1,11 @@
 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
 
-#if (defined(__unix__) || defined(__APPLE__)) && !defined(ECERE_MINIGLX)
+#if (defined(__unix__) || defined(__APPLE__)) && !defined(ECERE_MINIGLX) && !defined(__EMSCRIPTEN__)
 
 #undef __BLOCKS__
 default:
@@ -22,6 +22,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 +50,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
@@ -1749,9 +1767,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)
@@ -1779,7 +1797,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;
@@ -1791,14 +1809,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;
@@ -1811,9 +1832,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)
                      {
@@ -1995,7 +2016,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);
@@ -2142,11 +2163,11 @@ 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;
@@ -2196,7 +2217,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
@@ -2435,7 +2456,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;
@@ -2534,6 +2555,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);
@@ -2887,7 +2922,12 @@ class XInterface : Interface
       if(window.windowHandle && (!window.parent || !window.parent.display))
       {
          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)
@@ -2926,7 +2966,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);
@@ -2936,7 +2976,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;
@@ -2989,6 +3029,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;
@@ -3000,10 +3042,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);
             }
 
@@ -3078,8 +3123,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
@@ -3097,12 +3144,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)
@@ -3115,6 +3168,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;