Blender  V2.59
GHOST_NDOFManagerCocoa.mm
Go to the documentation of this file.
00001 /*
00002  * $Id: GHOST_NDOFManagerCocoa.mm 39307 2011-08-11 15:59:19Z nazgul $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * Contributor(s):
00021  *   Mike Erwin
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00026 #ifdef WITH_INPUT_NDOF
00027 
00028 #include "GHOST_NDOFManagerCocoa.h"
00029 #include "GHOST_SystemCocoa.h"
00030 
00031 extern "C" {
00032         #include <3DconnexionClient/ConnexionClientAPI.h>
00033         #include <stdio.h>
00034         }
00035 
00036 // static functions need to talk to these objects:
00037 static GHOST_SystemCocoa* ghost_system = NULL;
00038 static GHOST_NDOFManager* ndof_manager = NULL;
00039 
00040 // 3Dconnexion drivers before 10.x are "old"
00041 // not all buttons will work
00042 static bool has_old_driver = true;
00043 
00044 static void NDOF_DeviceAdded(io_connect_t connection)
00045 {
00046         printf("ndof: device added\n"); // change these: printf --> informational reports
00047 
00048 #if 0 // device preferences will be useful some day
00049         ConnexionDevicePrefs p;
00050         ConnexionGetCurrentDevicePrefs(kDevID_AnyDevice, &p);
00051 #endif
00052 
00053         // determine exactly which device is plugged in
00054         SInt32 result = 0;
00055         ConnexionControl(kConnexionCtlGetDeviceID, 0, &result);
00056         unsigned short vendorID = result >> 16;
00057         unsigned short productID = result & 0xffff;
00058 
00059         ndof_manager->setDevice(vendorID, productID);
00060 }
00061 
00062 static void NDOF_DeviceRemoved(io_connect_t connection)
00063 {
00064         printf("ndof: device removed\n");
00065 }
00066 
00067 static void NDOF_DeviceEvent(io_connect_t connection, natural_t messageType, void* messageArgument)
00068 {
00069         switch (messageType)
00070         {
00071                 case kConnexionMsgDeviceState:
00072                 {
00073                         ConnexionDeviceState* s = (ConnexionDeviceState*)messageArgument;
00074 
00075                         GHOST_TUns64 now = ghost_system->getMilliSeconds();
00076 
00077                         switch (s->command)
00078                         {
00079                                 case kConnexionCmdHandleAxis:
00080                                 {
00081                                         // convert to blender view coordinates
00082                                         short t[3] = {s->axis[0], -(s->axis[2]), s->axis[1]};
00083                                         short r[3] = {-(s->axis[3]), s->axis[5], -(s->axis[4])};
00084 
00085                                         ndof_manager->updateTranslation(t, now);
00086                                         ndof_manager->updateRotation(r, now);
00087 
00088                                         ghost_system->notifyExternalEventProcessed();
00089                                         break;
00090                                 }
00091                                 case kConnexionCmdHandleButtons:
00092                                 {
00093                                         int button_bits = has_old_driver ? s->buttons8 : s->buttons;
00094                                         ndof_manager->updateButtons(button_bits, now);
00095                                         ghost_system->notifyExternalEventProcessed();
00096                                         break;
00097                                 }
00098                                 case kConnexionCmdAppSpecific:
00099                                         printf("ndof: app-specific command, param = %hd, value = %d\n", s->param, s->value);
00100                                         break;
00101 
00102                                 default:
00103                                         printf("ndof: mystery device command %d\n", s->command);
00104                         }
00105                         break;
00106                 }
00107                 case kConnexionMsgPrefsChanged:
00108                         // printf("ndof: prefs changed\n"); // this includes app switches
00109                         // TODO: look through updated prefs for things blender cares about
00110                         break;
00111                 case kConnexionMsgCalibrateDevice:
00112                         printf("ndof: calibrate\n"); // but what should blender do?
00113                         break;
00114                 case kConnexionMsgDoMapping:
00115                         // printf("ndof: driver did something\n");
00116                         // sent when the driver itself consumes an NDOF event
00117                         // and performs whatever action is set in user prefs
00118                         // 3Dx header file says to ignore these
00119                         break;
00120                 default:
00121                         printf("ndof: mystery event %d\n", messageType);
00122         }
00123 }
00124 
00125 GHOST_NDOFManagerCocoa::GHOST_NDOFManagerCocoa(GHOST_System& sys)
00126     : GHOST_NDOFManager(sys)
00127 {
00128         if (available())
00129         {
00130                 // give static functions something to talk to:
00131                 ghost_system = dynamic_cast<GHOST_SystemCocoa*>(&sys);
00132                 ndof_manager = this;
00133 
00134                 OSErr error = InstallConnexionHandlers(NDOF_DeviceEvent, NDOF_DeviceAdded, NDOF_DeviceRemoved);
00135                 if (error) {
00136                         printf("ndof: error %d while installing handlers\n", error);
00137                         return;
00138                 }
00139 
00140                 // Pascal string *and* a four-letter constant. How old-skool.
00141                 m_clientID = RegisterConnexionClient('blnd', (UInt8*) "\007blender",
00142                                                      kConnexionClientModeTakeOver, kConnexionMaskAll);
00143 
00144                 // printf("ndof: client id = %d\n", m_clientID);
00145 
00146                 if (SetConnexionClientButtonMask != NULL) {
00147                         has_old_driver = false;
00148                         SetConnexionClientButtonMask(m_clientID, kConnexionMaskAllButtons);
00149                 }
00150                 else {
00151                         printf("ndof: old 3Dx driver installed, some buttons may not work\n");
00152                 }
00153         }
00154         else {
00155                 printf("ndof: 3Dx driver not found\n");
00156                 // This isn't a hard error, just means the user doesn't have a 3D mouse.
00157         }
00158 }
00159 
00160 GHOST_NDOFManagerCocoa::~GHOST_NDOFManagerCocoa()
00161 {
00162         if (available())
00163         {
00164                 UnregisterConnexionClient(m_clientID);
00165                 CleanupConnexionHandlers();
00166                 ghost_system = NULL;
00167                 ndof_manager = NULL;
00168         }
00169 }
00170 extern "C" {
00171         bool GHOST_NDOFManagerCocoa::available()
00172         {
00173                 extern OSErr InstallConnexionHandlers() __attribute__((weak_import));
00174                 // Make the linker happy for the framework check (see link below for more info)
00175                 // http://developer.apple.com/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html
00176                 return InstallConnexionHandlers != NULL;
00177                 // this means that the driver is installed and dynamically linked to blender
00178         }
00179 }
00180 #endif // WITH_INPUT_NDOF