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