make it possible to build without GL via make DISABLE_GL=1
[sdk] / ecere / src / gui / drivers / XInterface.ec
index c2a45c1..3a95d38 100644 (file)
@@ -1,7 +1,7 @@
 namespace gui::drivers;
 
 import "instance"
-#if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
+#if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
 import "OpenGLDisplayDriver"
 #endif
 
@@ -18,12 +18,9 @@ default:
 #include <linux/joystick.h>
 #endif
 #include <sys/param.h>
-#ifdef BSD
 #include <stdlib.h>
-#else
-#include <malloc.h>
-#endif
 #include <unistd.h>
+#include <sys/select.h>
 
 //#include <stdio.h>
 //#include <stdlib.h>
@@ -43,8 +40,10 @@ default:
 #include <X11/Xutil.h>
 #include <X11/XKBlib.h>
 #include <X11/keysym.h>
-#include <sys/fcntl.h>
+#include <fcntl.h>
+#if !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
 #include <GL/glx.h>
+#endif
 #include <X11/extensions/Xrender.h>
 #include <X11/extensions/XShm.h>
 
@@ -85,8 +84,8 @@ static Semaphore xSemaphore { };
 static Mutex xMutex { };
 static bool fullScreenMode;
 static int desktopX = 0, desktopY = 0, desktopW = 0, desktopH = 0;
-int confineWindow;
-static int nullCursor;
+X11Window confineWindow;
+static X11Cursor nullCursor;
 static X11Window capturedWindow = None;
 static Window restrictedWindow = null;
 static bool gotAnXEvent = false;
@@ -103,8 +102,7 @@ static enum AtomIdents
    _net_wm_window_type_desktop, _net_wm_window_type_dialog, _net_wm_window_type_dock, _net_wm_window_type_dropdown_menu,
    _net_wm_window_type_menu, _net_wm_window_type_normal, _net_wm_window_type_popup_menu, _net_wm_window_type_splash,
    _net_wm_window_type_toolbar, _net_wm_window_type_utility, _net_workarea, _net_frame_extents, _net_request_frame_extents,
-   _net_wm_state_maximized_vert, _net_wm_state_maximized_horz, _net_wm_state_hidden,
-   app_selection
+   _net_wm_state_maximized_vert, _net_wm_state_maximized_horz, app_selection
 };
 
 static Atom atoms[AtomIdents];
@@ -148,7 +146,6 @@ static const char *atomNames[AtomIdents] = {
    "_NET_REQUEST_FRAME_EXTENTS", // _net_request_frame_extents
    "_NET_WM_STATE_MAXIMIZED_VERT", // _net_wm_state_maximized_vert
    "_NET_WM_STATE_MAXIMIZED_HORZ", // _net_wm_state_maximized_horz
-   "_NET_WM_STATE_HIDDEN", // _net_wm_state_hidden
    "APP_SELECTION"
 };
 
@@ -236,7 +233,8 @@ static void RepositionDesktop(bool updateChildren)
    X11Window x_root;
    int current = 0;   
    char *data = null;
-   int format, len, fill;
+   int format;
+   unsigned long len, fill;
    Atom type;
 
    w = XDisplayWidth(xGlobalDisplay, DefaultScreen(xGlobalDisplay));
@@ -255,7 +253,7 @@ static void RepositionDesktop(bool updateChildren)
       if(data)
       {
          int desktops = 0;
-         desktops = *(long *)data;
+         desktops = (int)*(long *)data;
 
          //printf("_NET_NUMBER_OF_DESKTOPS is %d\n", desktops);
 
@@ -275,7 +273,7 @@ static void RepositionDesktop(bool updateChildren)
       
       if(data)
       {
-         current = *(long *)data;
+         current = (int)*(long *)data;
          XFree(data);
          data = null;
 
@@ -308,12 +306,14 @@ static void RepositionDesktop(bool updateChildren)
       {
          workareas = (long *)data;
      
-         x = workareas[current * 4];
-         y = workareas[current * 4 + 1];
-         w = workareas[current * 4 + 2];
-         h = workareas[current * 4 + 3];   
+         x = (int)workareas[current * 4];
+         y = (int)workareas[current * 4 + 1];
+         w = (int)workareas[current * 4 + 2];
+         h = (int)workareas[current * 4 + 3];
 
          //printf("_NET_WORKAREA is x = %d, y = %d, w = %d, h = %d\n", x, y, w, h);
+         XFree(data);
+         data = null;
       }
       //   printf("Work Area width: %d, height %d\n", w, h);
    }
@@ -369,12 +369,12 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
    KeySym keysym = NoSymbol;
    Status status;
    int buflength = 0;
-   static int bufsize = 16;
+   static long bufsize = 16;
    static char *buf = NULL;
    XWindowData windowData = window.windowData;
    Key key = 0;
 
-   if(window.destroyed) return;
+   if(window.destroyed) return result;
 
    // Logf("Got 0x%x (%d)\n", keyCode, key);
 
@@ -408,14 +408,14 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
 */
 
    if(!buf)
-      buf = malloc(bufsize);
+      buf = malloc((uint)bufsize);
    if(windowData && windowData.ic)
    { 
-      buflength = XmbLookupString(windowData.ic, event, buf, bufsize, &keysym, &status);
+      buflength = XmbLookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
       if (status == XBufferOverflow)
       {
-         buf = realloc(buf, (bufsize = buflength));
-         buflength = XmbLookupString(windowData.ic, event, buf, bufsize, &keysym, &status);
+         buf = realloc(buf, (uint)(bufsize = buflength));
+         buflength = XmbLookupString(windowData.ic, event, buf, (int)bufsize, &keysym, &status);
       }
       if(status != XLookupKeySym && status != XLookupBoth && release == 1)
          keysym = XLookupKeysym(event, 0);
@@ -466,7 +466,7 @@ static bool ProcessKeyMessage(Window window, uint keyCode, int release, XKeyEven
 #ifdef __APPLE__
          case XK_Help:     key = insert; break;
 #endif
-         // case XK_Break:
+         case XK_Break:    key = Key { pauseBreak, ctrl = true }; break;
 #ifdef __APPLE__
          case XK_Mode_switch: key = leftAlt; break;
 #endif
@@ -792,7 +792,7 @@ static Bool EventChecker(void *display, XEvent *event, char * data)
 
 static Bool ConfigureNotifyChecker(void *display, XConfigureEvent *event, char * data)
 {
-   return (!data || ((event->window == (int) data)) && event->type == ConfigureNotify;
+   return ((!data || (event->window == (int) data)) && event->type == ConfigureNotify;
 }
 
 static uint timerDelay = MAXINT;
@@ -806,8 +806,8 @@ static uint XTimerThread(Thread thread)
    */
    for(;;)
    {
-      int result;
-      bool waitSemaphore = false;
+      //int result;
+      //bool waitSemaphore = false;
       fd_set readSet, writeSet, exceptSet;
       struct timeval tv = { (timerDelay == MAXINT) ? 0 : (timerDelay / 1000000), (timerDelay == MAXINT) ? (int)(1000000 / 18.2) : (timerDelay % 1000000) };
       
@@ -959,7 +959,7 @@ default:
 
 #include <sys/ipc.h>
 #include <sys/shm.h>
-#include <sys/signal.h>
+#include <signal.h>
 #include <locale.h>
 
 
@@ -1053,7 +1053,7 @@ class XInterface : Interface
             {
                XVisualInfo vinfo;
                XVisualInfo *vinfo_ret;
-               long numitems;
+               int numitems = 0;
               
                vinfo.visualid = XVisualIDFromVisual(xSystemVisual);
                vinfo_ret = XGetVisualInfo(xGlobalDisplay, VisualIDMask, &vinfo, &numitems);
@@ -1449,7 +1449,7 @@ class XInterface : Interface
                   // if(event->time - lastTime > 15)
                   {
                      Modifiers keyFlags = 0;
-                     int x = event->x_root, y = event->y_root;
+                     // int x = event->x_root, y = event->y_root;
 
                      if(event->state & ShiftMask)     keyFlags.shift = true;
                      if(event->state & ControlMask)   keyFlags.ctrl = true;
@@ -1463,7 +1463,7 @@ class XInterface : Interface
                         event->x_root, event->y_root, &keyFlags, false, false);
                      delete window;
                      //*if(xGlobalDisplay) XLockDisplay(xGlobalDisplay);
-                     lastTime = event->time;
+                     lastTime = (uint)event->time;
                   }
                   break;
                }
@@ -1662,7 +1662,8 @@ class XInterface : Interface
                   // TODO: Support _NET_REQUEST_FRAME_EXTENTS message / _NET_FRAME_EXTENTS property for decoration size awareness
                   if(window.nativeDecorations)
                   {
-                     long format, len, fill;
+                     int format;
+                     unsigned long len, fill;
                      Atom type;
                      char * data = null;
                      if(XGetWindowProperty(xGlobalDisplay, (X11Window)window.systemHandle, atoms[_net_wm_state], 0, 32, False,
@@ -1704,7 +1705,7 @@ class XInterface : Interface
                      //if(event->send_event)
                      {
                         X11Window rootChild;
-                        long rootX, rootY;
+                        int rootX, rootY;
                         XTranslateCoordinates(xGlobalDisplay, event->window,
                            RootWindow(xGlobalDisplay, DefaultScreen(xGlobalDisplay)), 0, 0, 
                            &rootX, &rootY, &rootChild);
@@ -1748,7 +1749,7 @@ class XInterface : Interface
                      bool laterFocus;
                      activeWindow = (X11Window)window.windowHandle;
 
-                     timeStamp = event->data.l[1];
+                     timeStamp = (int)event->data.l[1];
                      
                      windowData = window.windowData;
                      laterFocus = windowData.laterFocus;
@@ -1849,7 +1850,8 @@ class XInterface : Interface
                   if(event->atom == atoms[_net_frame_extents] &&
                     event->state == PropertyNewValue && windowData)
                   {
-                     long format, len, fill;
+                     int format;
+                     unsigned long len, fill;
                      Atom type;
                      char * data = null;
 
@@ -1862,8 +1864,8 @@ class XInterface : Interface
                         bool hadFrameExtents = windowData.gotFrameExtents;
                         windowData.decor =
                         {
-                           left = extents[0], right  = extents[1],
-                           top  = extents[2], bottom = extents[3]
+                           left = (int)extents[0], right  = (int)extents[1],
+                           top  = (int)extents[2], bottom = (int)extents[3]
                         };
                         windowData.gotFrameExtents = true;
                         {
@@ -1964,12 +1966,12 @@ class XInterface : Interface
       int depth;
       Visual * visual;
       XIC ic = null;
-      unsigned int mask = EVENT_MASK;
+      unsigned long mask = EVENT_MASK;
 
       attributes.override_redirect = window.interim ? True : False;
       attributes.event_mask = EVENT_MASK;
       //printf("%s\n", guiApp.defaultDisplayDriver);
-#if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D)
+#if !defined(ECERE_VANILLA) && !defined(ECERE_NO3D) && !defined(ECERE_NOGL)
       if(window.dispDriver == class(OpenGLDisplayDriver) || !strcmp(guiApp.defaultDisplayDriver, "OpenGL"))
       {
          int samples;
@@ -2058,7 +2060,7 @@ class XInterface : Interface
       }
       if(!visualInfo)
       {
-         long attrList[] = 
+         int attrList[] = 
          {
             GLX_USE_GL, GLX_DEPTH_SIZE, 1,
             GLX_RGBA, 
@@ -2133,7 +2135,7 @@ class XInterface : Interface
                   parentWindow ? atoms[_net_wm_window_type_menu] : atoms[_net_wm_window_type_normal],
                   parentWindow ? atoms[_net_wm_window_type_popup_menu] : 0
                };
-#if defined(__APPLE__)
+#if defined(__APPLE__) || defined(__FreeBSD__)
                // Don't set this on non-interim windows for OS X...
                if(parentWindow && window.interim)
 #endif
@@ -2179,7 +2181,7 @@ class XInterface : Interface
          ic = XCreateIC(im, XNInputStyle,
             XIMStatusNothing | XIMPreeditPosition,
             XNPreeditAttributes, argList,
-            XNClientWindow, windowHandle, XNFocusWindow, windowHandle, 0);
+            XNClientWindow, windowHandle, XNFocusWindow, windowHandle, NULL);
          XFree(argList);
          // Should we free the fontset or not?
          // XFreeFontSet(xGlobalDisplay, fontset);
@@ -2313,7 +2315,7 @@ class XInterface : Interface
       //Logf("Position root window %s\n", window.name);
       if(window.windowHandle && (!window.parent || !window.parent.display))
       {
-#if defined(__APPLE__)
+#if defined(__APPLE__) || defined(__FreeBSD__)
          bool visible = window.visible;
          if(window.visible)
          {
@@ -2326,7 +2328,7 @@ class XInterface : Interface
          if(window.nativeDecorations)
          {
             XWindowData windowData = window.windowData;
-#if !defined(__APPLE__)
+#if !defined(__APPLE__) && !defined(__FreeBSD__)
             // TODO: How to handle frame extents not supported?
             if(!windowData.gotFrameExtents || window.state == maximized) return;
 #endif
@@ -2392,7 +2394,7 @@ class XInterface : Interface
          if(visible)
          {
             XMapWindow(xGlobalDisplay, (X11Window)window.windowHandle);
-#if defined(__APPLE__)
+#if defined(__APPLE__) || defined(__FreeBSD__)
             WaitForViewableWindow(window);
 #endif
             if(window.creationActivation == activate && state != minimized)
@@ -2553,7 +2555,7 @@ class XInterface : Interface
 
    void GetMousePosition(int *x, int *y)
    {
-      int rootWindow, childWindow;
+      X11Window rootWindow, childWindow;
       int mx, my;
       unsigned int state;
       ((GuiApplication)__thisModule.application).Lock();
@@ -2741,11 +2743,11 @@ class XInterface : Interface
          }
          if(rootWindow)
          {
-            int selAtom = atoms[clipboard];
-            int owner = XGetSelectionOwner(xGlobalDisplay, selAtom);
+            Atom selAtom = atoms[clipboard];
+            X11Window owner = XGetSelectionOwner(xGlobalDisplay, selAtom);
             if(owner != None)
             {
-               long atom;
+               Atom atom;
                for(atom = atoms[utf8_string]; atom; atom = ((atom == atoms[utf8_string]) ? XA_STRING : 0))
                {
                   XEvent e;
@@ -2755,14 +2757,19 @@ class XInterface : Interface
                   {
                      XSelectionEvent * selection = (XSelectionEvent *) &e;
                      //printf("Got a SelectionNotify with %d (%s)\n", selection->_property, XGetAtomName(xGlobalDisplay, selection->_property));
-                     byte *data;
+                     byte *data = null;
                      unsigned long len, size = 0, dummy;
                      Atom type;
-                     long format;
-                     XGetWindowProperty(xGlobalDisplay, (X11Window) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, 0, 0, AnyPropertyType, &type, &format, &len, &size, &data);
+                     int format;
+                     XGetWindowProperty(xGlobalDisplay, (X11Window) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, 0, False, AnyPropertyType, &type, &format, &len, &size, &data);
+                     if(data)
+                     {
+                        XFree(data);
+                        data = null;
+                     }
                      if(size > 0)
                      {
-                        if(XGetWindowProperty(xGlobalDisplay, (X11Window) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0,size,0,
+                        if(XGetWindowProperty(xGlobalDisplay, (X11Window) rootWindow.windowHandle, selection->_property ? selection->_property : atom, 0, size, False,
                               AnyPropertyType, &type,&format,&len, &dummy, &data) == Success)
                         {
                            clipBoard.text = new char[size+1];
@@ -2852,7 +2859,14 @@ class XInterface : Interface
             bitmap.Convert(null, pixelFormat888, null);
             icon[0] = bitmap.width;
             icon[1] = bitmap.height;
-            memcpy(icon + 2, bitmap.picture, bitmap.width * bitmap.height * 4);
+            if(sizeof(long) != sizeof(uint32))
+            {
+               int c;
+               for(c = 0; c < bitmap.width * bitmap.height; c++)
+                  icon[c+2] = ((uint32 *)bitmap.picture)[c];
+            }
+            else
+               memcpy(icon + 2, bitmap.picture, bitmap.width * bitmap.height * sizeof(uint32));
             XChangeProperty(xGlobalDisplay, (X11Window)window.windowHandle, atoms[_net_wm_icon],
                          XA_CARDINAL,32,PropModeReplace, (byte *)icon, 2+bitmap.width*bitmap.height);
            delete icon;