finished Mac NDOF device detection, moved core device ID to base NDOFManager, clarifi...
authorMike Erwin <significant.bit@gmail.com>
Fri, 17 Jun 2011 19:48:26 +0000 (19:48 +0000)
committerMike Erwin <significant.bit@gmail.com>
Fri, 17 Jun 2011 19:48:26 +0000 (19:48 +0000)
intern/ghost/intern/GHOST_NDOFManager.cpp
intern/ghost/intern/GHOST_NDOFManager.h
intern/ghost/intern/GHOST_NDOFManagerCocoa.h
intern/ghost/intern/GHOST_NDOFManagerCocoa.mm
intern/ghost/intern/GHOST_NDOFManagerX11.cpp
intern/ghost/intern/GHOST_NDOFManagerX11.h

index 5c2ca658fdb86d48ddd105aabb43041eca7ff9e9..a2b9a53a51c15f52111c937e44186f4eb62c52f4 100644 (file)
@@ -146,6 +146,24 @@ GHOST_NDOFManager::GHOST_NDOFManager(GHOST_System& sys)
        memset(m_rotation, 0, sizeof(m_rotation));
        }
 
+void GHOST_NDOFManager::setDevice(unsigned short vendor_id, unsigned short product_id)
+       {
+       switch (vendor_id)
+               {
+               case 0x046d: // Logitech (3Dconnexion)
+                       switch (product_id)
+                               {
+                               case 0xc626: m_deviceType = NDOF_SpaceNavigator; break;
+                               case 0xc627: m_deviceType = NDOF_SpaceExplorer; break;
+                               case 0xc629: m_deviceType = NDOF_SpacePilotPro; break;
+                               default: printf("ndof: unknown Logitech product %04hx\n", product_id);
+                               }
+                       break;
+               default:
+                       printf("ndof: unknown vendor %04hx\n", vendor_id);
+               }
+       }
+
 void GHOST_NDOFManager::updateTranslation(short t[3], GHOST_TUns64 time)
        {
        memcpy(m_translation, t, sizeof(m_translation));
@@ -192,7 +210,7 @@ void GHOST_NDOFManager::updateButton(int button_number, bool press, GHOST_TUns64
        GHOST_IWindow* window = m_system.getWindowManager()->getActiveWindow();
 
        #ifdef DEBUG_NDOF_BUTTONS
-       printf("button %d -> ", button_number);
+       printf("ndof: button %d -> ", button_number);
        #endif
 
        switch (m_deviceType)
@@ -221,7 +239,7 @@ void GHOST_NDOFManager::updateButton(int button_number, bool press, GHOST_TUns64
                                }
                        break;
                case NDOF_UnknownDevice:
-                       printf("button %d on unknown device (not sent)\n", button_number);
+                       printf("ndof: button %d on unknown device (ignoring)\n", button_number);
                }
 
        int mask = 1 << button_number;
@@ -276,7 +294,7 @@ bool GHOST_NDOFManager::sendMotionEvent()
        m_prevMotionTime = m_motionTime;
 
        #ifdef DEBUG_NDOF_MOTION
-       printf("sending T=(%.2f,%.2f,%.2f) R=(%.2f,%.2f,%.2f) dt=%.3f\n",
+       printf("ndof: T=(%.2f,%.2f,%.2f) R=(%.2f,%.2f,%.2f) dt=%.3f\n",
                data->tx, data->ty, data->tz, data->rx, data->ry, data->rz, data->dt);
        #endif
 
index 564b0f6f2393af16bc01394bf31079f74e027f35..846b10fb4549f441667acdee4117c18c909eff11 100644 (file)
@@ -88,22 +88,31 @@ public:
        // does not imply that a device is plugged in or being used
        virtual bool available() = 0;
 
-       // the latest raw data from the device
+       // each platform's device detection should call this
+       // use standard USB/HID identifiers
+       void setDevice(unsigned short vendor_id, unsigned short product_id);
+
+       // the latest raw axis data from the device
        void updateTranslation(short t[3], GHOST_TUns64 time);
        void updateRotation(short r[3], GHOST_TUns64 time);
-       // send events immediately for changed buttons
+
+       // the latest raw button data from the device
+       // use HID button encoding (not NDOF_ButtonT)
        void updateButton(int button_number, bool press, GHOST_TUns64 time);
        void updateButtons(int button_bits, GHOST_TUns64 time);
+       // NDOFButton events are sent immediately
 
-       // processes most recent raw data into an NDOFMotion event and sends it
+       // processes and sends most recent raw data as an NDOFMotion event
        // returns whether an event was sent
        bool sendMotionEvent();
 
+protected:
+
 private:
        void sendButtonEvent(NDOF_ButtonT, bool press, GHOST_TUns64 time, GHOST_IWindow*);
        void sendKeyEvent(GHOST_TKey, bool press, GHOST_TUns64 time, GHOST_IWindow*);
 
-protected:
+
        GHOST_System& m_system;
 
        NDOF_DeviceT m_deviceType;
index efe840bab4ad66cd84bb399f2a87344f436ff1aa..d8711e915f6805311b84e38251a464714a4a4e04 100644 (file)
@@ -26,7 +26,7 @@
 
 #include "GHOST_NDOFManager.h"
 
-// Event capture is handled within the NDOFManager on Macintosh,
+// Event capture is handled within the NDOF manager on Macintosh,
 // so there's no need for SystemCocoa to look for them.
 
 class GHOST_NDOFManagerCocoa : public GHOST_NDOFManager
index 23848c80803ea999c984042384866df0b99f5123..b92e8112c024b780bac90484dbf5a98053e111f4 100644 (file)
@@ -29,11 +29,15 @@ extern "C" {
        #include <stdio.h>
        }
 
+// static functions need to talk to these objects:
+static GHOST_SystemCocoa* ghost_system = NULL;
+static GHOST_NDOFManager* ndof_manager = NULL;
+
 static void NDOF_DeviceAdded(io_connect_t connection)
        {
-       printf("ndof device added\n"); // change these: printf --> informational reports
+       printf("ndof: device added\n"); // change these: printf --> informational reports
 
-       // determine exactly which device is plugged in
+#if 0 // device preferences will be useful soon, but not for hardware model detection
        ConnexionDevicePrefs p;
        ConnexionGetCurrentDevicePrefs(kDevID_AnyDevice, &p);
        printf("device type %d: %s\n", p.deviceID,
@@ -41,72 +45,72 @@ static void NDOF_DeviceAdded(io_connect_t connection)
                p.deviceID == kDevID_SpaceNavigatorNB ? "SpaceNavigator for Notebooks" :
                p.deviceID == kDevID_SpaceExplorer ? "SpaceExplorer" :
                "unknown");
+#endif
 
-       // try a more "standard" way
+       // determine exactly which device is plugged in
        int result = 0;
        ConnexionControl(kConnexionCtlGetDeviceID, 0, &result);
        unsigned short vendorID = result >> 16;
        unsigned short productID = result & 0xffff;
-       printf("vendor %04hx:%04hx product\n", vendorID, productID);
+
+       ndof_manager->setDevice(vendorID, productID);
        }
 
 static void NDOF_DeviceRemoved(io_connect_t connection)
        {
-       printf("ndof device removed\n");
+       printf("ndof: device removed\n");
        }
 
 static void NDOF_DeviceEvent(io_connect_t connection, natural_t messageType, void* messageArgument)
        {
-       GHOST_SystemCocoa* system = (GHOST_SystemCocoa*) GHOST_ISystem::getSystem();
-       GHOST_NDOFManager* manager = system->getNDOFManager();
        switch (messageType)
                {
                case kConnexionMsgDeviceState:
                        {
                        ConnexionDeviceState* s = (ConnexionDeviceState*)messageArgument;
 
-                       GHOST_TUns64 now = system->getMilliSeconds();
+                       GHOST_TUns64 now = ghost_system->getMilliSeconds();
 
                        switch (s->command)
                                {
                                case kConnexionCmdHandleAxis:
-                                       manager->updateTranslation(s->axis, now);
-                                       manager->updateRotation(s->axis + 3, now);
-                                       system->notifyExternalEventProcessed();
+                                       ndof_manager->updateTranslation(s->axis, now);
+                                       ndof_manager->updateRotation(s->axis + 3, now);
+                                       ghost_system->notifyExternalEventProcessed();
                                        break;
 
                                case kConnexionCmdHandleButtons:
 
                                        // s->buttons field has only 16 bits, not enough for SpacePilotPro
                                        // look at raw USB report for more button bits
-                                       printf("button bits = [");
+                                       printf("ndof: button bits = [");
                                        for (int i = 0; i < 8; ++i)
                                                printf("%02x", s->report[i]);
                                        printf("]\n");
 
-                                       manager->updateButtons(s->buttons, now);
-                                       system->notifyExternalEventProcessed();
+                                       ndof_manager->updateButtons(s->buttons, now);
+                                       ghost_system->notifyExternalEventProcessed();
                                        break;
 
                                case kConnexionCmdAppSpecific:
-                                       printf("app-specific command: param=%hd value=%d\n", s->param, s->value);
+                                       printf("ndof: app-specific command, param = %hd, value = %d\n", s->param, s->value);
                                        break;
 
                                default:
-                                       printf("<!> mystery command %d\n", s->command);
+                                       printf("ndof: mystery device command %d\n", s->command);
                                }
                        break;
                        }
                case kConnexionMsgPrefsChanged:
-                       printf("prefs changed\n"); // this includes app switches
+                       printf("ndof: prefs changed\n"); // this includes app switches
                        break;
                case kConnexionMsgDoAction:
-                       printf("do action\n"); // no idea what this means
+                       printf("ndof: do action\n"); // no idea what this means
                        // 'calibrate device' in System Prefs sends this
                        // 3Dx header file says to ignore these
                        break;
                default:
-                       printf("<!> mystery event\n");
+                       printf("ndof: mystery event %d\n", messageType);
                }
        }
 
@@ -115,10 +119,14 @@ GHOST_NDOFManagerCocoa::GHOST_NDOFManagerCocoa(GHOST_System& sys)
        {
        if (available())
                {
+               // give static functions something to talk to:
+               ghost_system = dynamic_cast<GHOST_SystemCocoa*>(&sys);
+               ndof_manager = this;
+
                OSErr error = InstallConnexionHandlers(NDOF_DeviceEvent, NDOF_DeviceAdded, NDOF_DeviceRemoved);
                if (error)
                        {
-                       printf("<!> error = %d\n", error);
+                       printf("ndof: error %d while installing handlers\n", error);
                        return;
                        }
 
@@ -126,12 +134,12 @@ GHOST_NDOFManagerCocoa::GHOST_NDOFManagerCocoa(GHOST_System& sys)
                m_clientID = RegisterConnexionClient('blnd', (UInt8*) "\pblender",
                        kConnexionClientModeTakeOver, kConnexionMaskAll);
 
-               printf("client id = %d\n", m_clientID);
+               printf("ndof: client id = %d\n", m_clientID);
                }
        else
                {
-               printf("<!> SpaceNav driver not found\n");
-               // This isn't a hard error, just means the user doesn't have a SpaceNavigator.
+               printf("ndof: 3Dx driver not found\n");
+               // This isn't a hard error, just means the user doesn't have a 3D mouse.
                }
        }
 
@@ -139,6 +147,8 @@ GHOST_NDOFManagerCocoa::~GHOST_NDOFManagerCocoa()
        {
        UnregisterConnexionClient(m_clientID);
        CleanupConnexionHandlers();
+       ghost_system = NULL;
+       ndof_manager = NULL;
        }
 
 bool GHOST_NDOFManagerCocoa::available()
index 49372e8502d9ff5a6e2853251f89d07412d1e68a..40a0a75ab82b0347473297166090d1850fa5aa11 100644 (file)
@@ -47,26 +47,16 @@ GHOST_NDOFManagerX11::GHOST_NDOFManagerX11(GHOST_System& sys)
                                {
                                unsigned short vendor_id = 0, product_id = 0;
                                if (sscanf(line, "Bus %*d Device %*d: ID %hx:%hx", &vendor_id, &product_id) == 2)
-                                       {
-                                       // the following code will live in the base class
-                                       // once all platforms have device detection
-                                       switch (product_id)
-                                               {
-                                               case 0xc626: m_deviceType = NDOF_SpaceNavigator; break;
-                                               case 0xc627: m_deviceType = NDOF_SpaceExplorer; break;
-                                               case 0xc629: m_deviceType = NDOF_SpacePilotPro; break;
-                                               default: printf("unknown product ID: %04x\n", product_id);
-                                               }
-                                       }
+                                       setDevice(vendor_id, product_id);
                                }
                        pclose(command_output);
                        }
                }
        else
                {
-               printf("<!> SpaceNav driver not found\n");
-               // This isn't a hard error, just means the user doesn't have a SpaceNavigator.
                m_available = false;
+               printf("ndof: spacenavd not found\n");
+               // This isn't a hard error, just means the user doesn't have a 3D mouse.
                }
        }
 
index cf6f1d00cbad5de30d94db36464d548f6146b422..def311d51e2ce19ca0f5ca8461783bd995478ca0 100644 (file)
@@ -26,6 +26,9 @@
 
 #include "GHOST_NDOFManager.h"
 
+// Event capture is handled within the NDOF manager on Linux,
+// so there's no need for SystemX11 to look for them.
+
 class GHOST_NDOFManagerX11 : public GHOST_NDOFManager
        {
 public: