Merging r38898 through r38940 from trunk into soc-2011-tomato
[blender.git] / intern / ghost / intern / GHOST_NDOFManagerCocoa.mm
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s):
21  *   Mike Erwin
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25  
26 #include "GHOST_NDOFManagerCocoa.h"
27 #include "GHOST_SystemCocoa.h"
28
29 extern "C" {
30         #include <3DconnexionClient/ConnexionClientAPI.h>
31         #include <stdio.h>
32         }
33
34 // static functions need to talk to these objects:
35 static GHOST_SystemCocoa* ghost_system = NULL;
36 static GHOST_NDOFManager* ndof_manager = NULL;
37
38 // 3Dconnexion drivers before 10.x are "old"
39 // not all buttons will work
40 static bool has_old_driver = true;
41
42 static void NDOF_DeviceAdded(io_connect_t connection)
43         {
44         printf("ndof: device added\n"); // change these: printf --> informational reports
45
46 #if 0 // device preferences will be useful some day
47         ConnexionDevicePrefs p;
48         ConnexionGetCurrentDevicePrefs(kDevID_AnyDevice, &p);
49 #endif
50
51         // determine exactly which device is plugged in
52         SInt32 result = 0;
53         ConnexionControl(kConnexionCtlGetDeviceID, 0, &result);
54         unsigned short vendorID = result >> 16;
55         unsigned short productID = result & 0xffff;
56
57         ndof_manager->setDevice(vendorID, productID);
58         }
59
60 static void NDOF_DeviceRemoved(io_connect_t connection)
61         {
62         printf("ndof: device removed\n");
63         }
64
65 static void NDOF_DeviceEvent(io_connect_t connection, natural_t messageType, void* messageArgument)
66         {
67         switch (messageType)
68                 {
69                 case kConnexionMsgDeviceState:
70                         {
71                         ConnexionDeviceState* s = (ConnexionDeviceState*)messageArgument;
72
73                         GHOST_TUns64 now = ghost_system->getMilliSeconds();
74
75                         switch (s->command)
76                                 {
77                                 case kConnexionCmdHandleAxis:
78                                         {
79                                         // convert to blender view coordinates
80                                         short t[3] = {s->axis[0], -(s->axis[2]), s->axis[1]};
81                                         short r[3] = {-(s->axis[3]), s->axis[5], -(s->axis[4])};
82
83                                         ndof_manager->updateTranslation(t, now);
84                                         ndof_manager->updateRotation(r, now);
85
86                                         ghost_system->notifyExternalEventProcessed();
87                                         break;
88                                         }
89                                 case kConnexionCmdHandleButtons:
90                                         {
91                                         int button_bits = has_old_driver ? s->buttons8 : s->buttons;
92                                         ndof_manager->updateButtons(button_bits, now);
93                                         ghost_system->notifyExternalEventProcessed();
94                                         break;
95                                         }
96                                 case kConnexionCmdAppSpecific:
97                                         printf("ndof: app-specific command, param = %hd, value = %d\n", s->param, s->value);
98                                         break;
99
100                                 default:
101                                         printf("ndof: mystery device command %d\n", s->command);
102                                 }
103                         break;
104                         }
105                 case kConnexionMsgPrefsChanged:
106                         // printf("ndof: prefs changed\n"); // this includes app switches
107                         // TODO: look through updated prefs for things blender cares about
108                         break;
109                 case kConnexionMsgCalibrateDevice:
110                         printf("ndof: calibrate\n"); // but what should blender do?
111                         break;
112                 case kConnexionMsgDoMapping:
113                         // printf("ndof: driver did something\n");
114                         // sent when the driver itself consumes an NDOF event
115                         // and performs whatever action is set in user prefs
116                         // 3Dx header file says to ignore these
117                         break;
118                 default:
119                         printf("ndof: mystery event %d\n", messageType);
120                 }
121         }
122
123 GHOST_NDOFManagerCocoa::GHOST_NDOFManagerCocoa(GHOST_System& sys)
124         : GHOST_NDOFManager(sys)
125         {
126         if (available())
127                 {
128                 // give static functions something to talk to:
129                 ghost_system = dynamic_cast<GHOST_SystemCocoa*>(&sys);
130                 ndof_manager = this;
131
132                 OSErr error = InstallConnexionHandlers(NDOF_DeviceEvent, NDOF_DeviceAdded, NDOF_DeviceRemoved);
133                 if (error)
134                         {
135                         printf("ndof: error %d while installing handlers\n", error);
136                         return;
137                         }
138
139                 // Pascal string *and* a four-letter constant. How old-skool.
140                 m_clientID = RegisterConnexionClient('blnd', (UInt8*) "\007blender",
141                         kConnexionClientModeTakeOver, kConnexionMaskAll);
142
143                 // printf("ndof: client id = %d\n", m_clientID);
144
145                 if (SetConnexionClientButtonMask != NULL)
146                         {
147                         has_old_driver = false;
148                         SetConnexionClientButtonMask(m_clientID, kConnexionMaskAllButtons);
149                         }
150                 else
151                         printf("ndof: old 3Dx driver installed, some buttons may not work\n");
152                 }
153         else
154                 {
155                 printf("ndof: 3Dx driver not found\n");
156                 // This isn't a hard error, just means the user doesn't have a 3D mouse.
157                 }
158         }
159
160 GHOST_NDOFManagerCocoa::~GHOST_NDOFManagerCocoa()
161         {
162         UnregisterConnexionClient(m_clientID);
163         CleanupConnexionHandlers();
164         ghost_system = NULL;
165         ndof_manager = NULL;
166         }
167
168 bool GHOST_NDOFManagerCocoa::available()
169         {
170         // extern OSErr InstallConnexionHandlers() __attribute__((weak_import));
171         // ^^ not needed since the entire framework is weak-linked
172         return InstallConnexionHandlers != NULL;
173         // this means that the driver is installed and dynamically linked to blender
174         }