more preliminary NDOF handling stuff (untested)
[blender.git] / intern / ghost / intern / GHOST_SystemX11.cpp
index 5eab71eebf5d5a29303b640cc7776682ad80f8f6..a70162a5039d5a06a22ddc026de613872d8b2818 100644 (file)
  * ***** END GPL LICENSE BLOCK *****
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #include "GHOST_SystemX11.h"
 #include "GHOST_WindowX11.h"
 #include "GHOST_WindowManager.h"
 #include "GHOST_EventButton.h"
 #include "GHOST_EventWheel.h"
 #include "GHOST_EventNDOF.h"
-#include "GHOST_NDOFManager.h"
+#include "GHOST_NDOFManagerX11.h"
 #include "GHOST_DisplayManagerX11.h"
 
 #include "GHOST_Debug.h"
 
 #include <X11/Xatom.h>
 #include <X11/keysym.h>
-#include <X11/XKBlib.h> /* allow detectable autorepeate */
+#include <X11/XKBlib.h> /* allow detectable autorepeat */
 
 #ifdef __sgi
 
 #include <stdio.h> // for fprintf only
 #include <cstdlib> // for exit
 
+// [mce] these are for communication with the plugin
 typedef struct NDOFPlatformInfo {
        Display *display;
        Window window;
-       volatile GHOST_TEventNDOFData *currValues;
+//     volatile GHOST_TEventNDOFData *currValues;
        Atom cmdAtom;
        Atom motionAtom;
        Atom btnPressAtom;
@@ -83,7 +80,6 @@ typedef struct NDOFPlatformInfo {
 
 static NDOFPlatformInfo sNdofInfo = {NULL, 0, NULL, 0, 0, 0, 0};
 
-
 //these are for copy and select copy
 static char *txt_cut_buffer= NULL;
 static char *txt_select_buffer= NULL;
@@ -146,7 +142,7 @@ GHOST_SystemX11(
        m_start_time = GHOST_TUns64(tv.tv_sec*1000 + tv.tv_usec/1000);
        
        
-       /* use detectable autorepeate, mac and windows also do this */
+       /* use detectable autorepeat, mac and windows also do this */
        int use_xkb;
        int xkb_opcode, xkb_event, xkb_error;
        int xkb_major = XkbMajorVersion, xkb_minor = XkbMinorVersion;
@@ -172,6 +168,9 @@ init(
        GHOST_TSuccess success = GHOST_System::init();
 
        if (success) {
+
+               m_ndofManager = new GHOST_NDOFManagerX11;
+
                m_displayManager = new GHOST_DisplayManagerX11(this);
 
                if (m_displayManager) {
@@ -250,9 +249,6 @@ createWindow(
        GHOST_WindowX11 * window = 0;
        
        if (!m_display) return 0;
-       
-
-       
 
        window = new GHOST_WindowX11 (
                this,m_display,title, left, top, width, height, state, parentWindow, type, stereoVisual
@@ -536,38 +532,43 @@ GHOST_SystemX11::processEvent(XEvent *xe)
                }
 
                case ButtonPress:
-               {
-                       /* process wheel mouse events and break */
-                       if (xe->xbutton.button == 4) {
-                               g_event = new GHOST_EventWheel(getMilliSeconds(), window, 1);
-                               break;
-                       }
-                       if (xe->xbutton.button == 5) {
-                               g_event = new GHOST_EventWheel(getMilliSeconds(), window, -1);
-                               break;
-                       }
-               }
                case ButtonRelease:
                {
-
                        XButtonEvent & xbe = xe->xbutton;
                        GHOST_TButtonMask gbmask = GHOST_kButtonMaskLeft;
-                       switch (xbe.button) {
-                               case Button1 : gbmask = GHOST_kButtonMaskLeft; break;
-                               case Button3 : gbmask = GHOST_kButtonMaskRight; break;
-                               /* It seems events 6 and 7 are for horizontal scrolling.
-                                * you can re-order button mapping like this... (swaps 6,7 with 8,9)
-                                *   xmodmap -e "pointer = 1 2 3 4 5 8 9 6 7" 
-                                */
-                               case 8 : gbmask = GHOST_kButtonMaskButton4; break; /* Button4 is the wheel */
-                               case 9 : gbmask = GHOST_kButtonMaskButton5; break; /* Button5 is a wheel too */
-                               default:
-                               case Button2 : gbmask = GHOST_kButtonMaskMiddle; break;
-                       }
-                       
                        GHOST_TEventType type = (xbe.type == ButtonPress) ? 
                                GHOST_kEventButtonDown : GHOST_kEventButtonUp;
+
+                       /* process wheel mouse events and break, only pass on press events */
+                       if(xbe.button == Button4) {
+                               if(xbe.type == ButtonPress)
+                                       g_event = new GHOST_EventWheel(getMilliSeconds(), window, 1);
+                               break;
+                       }
+                       else if(xbe.button == Button5) {
+                               if(xbe.type == ButtonPress)
+                                       g_event = new GHOST_EventWheel(getMilliSeconds(), window, -1);
+                               break;
+                       }
                        
+                       /* process rest of normal mouse buttons */
+                       if(xbe.button == Button1)
+                               gbmask = GHOST_kButtonMaskLeft;
+                       else if(xbe.button == Button2)
+                               gbmask = GHOST_kButtonMaskMiddle;
+                       else if(xbe.button == Button3)
+                               gbmask = GHOST_kButtonMaskRight;
+                       /* It seems events 6 and 7 are for horizontal scrolling.
+                       * you can re-order button mapping like this... (swaps 6,7 with 8,9)
+                       *   xmodmap -e "pointer = 1 2 3 4 5 8 9 6 7" 
+                       */
+                       else if(xbe.button == 8)
+                               gbmask = GHOST_kButtonMaskButton4;
+                       else if(xbe.button == 9)
+                               gbmask = GHOST_kButtonMaskButton5;
+                       else
+                               break;
+
                        g_event = new
                        GHOST_EventButton(
                                getMilliSeconds(),
@@ -627,29 +628,49 @@ GHOST_SystemX11::processEvent(XEvent *xe)
                        } else 
 #endif
                        if (sNdofInfo.currValues) {
-                               static GHOST_TEventNDOFData data = {0,0,0,0,0,0,0,0,0,0,0};
+//                             static GHOST_TEventNDOFData data = {0,0,0,0,0,0,0,0,0,0,0};
                                if (xcme.message_type == sNdofInfo.motionAtom)
                                {
-                                       data.changed = 1;
-                                       data.delta = xcme.data.s[8] - data.time;
-                                       data.time = xcme.data.s[8];
-                                       data.tx = xcme.data.s[2] >> 2;
-                                       data.ty = xcme.data.s[3] >> 2;
-                                       data.tz = xcme.data.s[4] >> 2;
-                                       data.rx = xcme.data.s[5];
-                                       data.ry = xcme.data.s[6];
-                                       data.rz =-xcme.data.s[7];
-                                       g_event = new GHOST_EventNDOF(getMilliSeconds(),
-                                                                     GHOST_kEventNDOFMotion,
-                                                                     window, data);
+//                                     data.changed = 1;
+//                                     data.delta = xcme.data.s[8] - data.time;
+//                                     data.time = xcme.data.s[8];
+//                                     data.tx = xcme.data.s[2] >> 2;
+//                                     data.ty = xcme.data.s[3] >> 2;
+//                                     data.tz = xcme.data.s[4] >> 2;
+//                                     data.rx = xcme.data.s[5];
+//                                     data.ry = xcme.data.s[6];
+//                                     data.rz =-xcme.data.s[7];
+                                               
+                                       short t[3], r[3];
+                                       t[0] = xcme.data.s[2] >> 2;
+                                       t[1] = xcme.data.s[3] >> 2;
+                                       t[2] = xcme.data.s[4] >> 2;
+                                       r[0] = xcme.data.s[5];
+                                       r[1] = xcme.data.s[6];
+                                       r[2] =-xcme.data.s[7];
+
+                                       // [mce] look into this soon, as in find out why some values
+                                       // are shifted and where this message originates.
+
+                                       m_ndofManager->updateTranslation(t, getMilliseconds);
+                                       m_ndofManager->updateRotation(r, getMilliseconds);
+
+//                                     g_event = new GHOST_EventNDOF(getMilliSeconds(),
+//                                                                   GHOST_kEventNDOFMotion,
+//                                                                   window, data);
+
                                } else if (xcme.message_type == sNdofInfo.btnPressAtom) {
-                                       data.changed = 2;
-                                       data.delta = xcme.data.s[8] - data.time;
-                                       data.time = xcme.data.s[8];
-                                       data.buttons = xcme.data.s[2];
-                                       g_event = new GHOST_EventNDOF(getMilliSeconds(),
-                                                                     GHOST_kEventNDOFButton,
-                                                                     window, data);
+//                                     data.changed = 2;
+//                                     data.delta = xcme.data.s[8] - data.time;
+//                                     data.time = xcme.data.s[8];
+//                                     data.buttons = xcme.data.s[2];
+
+                                       unsigned short buttons = xcme.data.s[2];
+                                       m_ndofManager->updateButtons(buttons, getMilliseconds());
+
+//                                     g_event = new GHOST_EventNDOF(getMilliSeconds(),
+//                                                                   GHOST_kEventNDOFButton,
+//                                                                   window, data);
                                }
                        } else if (((Atom)xcme.data.l[0]) == m_wm_take_focus) {
                                XWindowAttributes attr;
@@ -813,6 +834,7 @@ GHOST_SystemX11::processEvent(XEvent *xe)
        }
 }
 
+/*
        void *
 GHOST_SystemX11::
 prepareNdofInfo(volatile GHOST_TEventNDOFData *currentNdofValues)
@@ -824,6 +846,7 @@ prepareNdofInfo(volatile GHOST_TEventNDOFData *currentNdofValues)
        sNdofInfo.currValues = currentNdofValues;
        return (void*)&sNdofInfo;
 }
+*/
 
        GHOST_TSuccess 
 GHOST_SystemX11::
@@ -965,7 +988,7 @@ GHOST_SystemX11::
 setCursorPosition(
        GHOST_TInt32 x,
        GHOST_TInt32 y
-) const {
+) {
 
        // This is a brute force move in screen coordinates
        // XWarpPointer does relative moves so first determine the
@@ -1458,19 +1481,19 @@ void GHOST_SystemX11::putClipboard(GHOST_TInt8 *buffer, bool selection) const
        }
 }
 
-GHOST_TUns8* GHOST_SystemX11::getSystemDir() const
+const GHOST_TUns8* GHOST_SystemX11::getSystemDir() const
 {
-
+       return (GHOST_TUns8*)"/usr/share/blender";
 }
 
-GHOST_TUns8* GHOST_SystemX11::getUserDir() const
+const GHOST_TUns8* GHOST_SystemX11::getUserDir() const
 {
-       char* path;
+       static char path[256];
        char* env = getenv("HOME");
        if(env) {
-               path = (char*) malloc(strlen(env) + 10); // "/.blender/"
-               strcat(path, env);
-               strcat(path, "/,blender/");
+               strncpy(path, env, 245);
+               path[245]=0;
+               strcat(path, "/.blender/");
                return (GHOST_TUns8*) path;
        } else {
                return NULL;