minor cleanup of Mac ndof manager
[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 some day
45         ConnexionDevicePrefs p;
46         ConnexionGetCurrentDevicePrefs(kDevID_AnyDevice, &p);
47 #endif
48
49         // determine exactly which device is plugged in
50         SInt32 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                                         // convert to blender view coordinates
78                                         short t[3] = {s->axis[0], -(s->axis[2]), s->axis[1]};
79                                         short r[3] = {-(s->axis[3]), s->axis[5], -(s->axis[4])};
80
81                                         ndof_manager->updateTranslation(t, now);
82                                         ndof_manager->updateRotation(r, now);
83
84                                         ghost_system->notifyExternalEventProcessed();
85                                         break;
86                                         }
87                                 case kConnexionCmdHandleButtons:
88                                         {
89                                         int button_bits = has_old_driver ? s->buttons8 : s->buttons;
90                                         ndof_manager->updateButtons(button_bits, now);
91                                         ghost_system->notifyExternalEventProcessed();
92                                         break;
93                                         }
94                                 case kConnexionCmdAppSpecific:
95                                         printf("ndof: app-specific command, param = %hd, value = %d\n", s->param, s->value);
96                                         break;
97
98                                 default:
99                                         printf("ndof: mystery device command %d\n", s->command);
100                                 }
101                         break;
102                         }
103                 case kConnexionMsgPrefsChanged:
104                         // printf("ndof: prefs changed\n"); // this includes app switches
105                         // TODO: look through updated prefs for things blender cares about
106                         break;
107                 case kConnexionMsgCalibrateDevice:
108                         printf("ndof: calibrate\n"); // but what should blender do?
109                         break;
110                 case kConnexionMsgDoMapping:
111                         // printf("ndof: driver did something\n");
112                         // sent when the driver itself consumes an NDOF event
113                         // and performs whatever action is set in user prefs
114                         // 3Dx header file says to ignore these
115                         break;
116                 default:
117                         printf("ndof: mystery event %d\n", messageType);
118                 }
119         }
120
121 GHOST_NDOFManagerCocoa::GHOST_NDOFManagerCocoa(GHOST_System& sys)
122         : GHOST_NDOFManager(sys)
123         {
124         if (available())
125                 {
126                 // give static functions something to talk to:
127                 ghost_system = dynamic_cast<GHOST_SystemCocoa*>(&sys);
128                 ndof_manager = this;
129
130                 OSErr error = InstallConnexionHandlers(NDOF_DeviceEvent, NDOF_DeviceAdded, NDOF_DeviceRemoved);
131                 if (error)
132                         {
133                         printf("ndof: error %d while installing handlers\n", error);
134                         return;
135                         }
136
137                 // Pascal string *and* a four-letter constant. How old-skool.
138                 m_clientID = RegisterConnexionClient('blnd', (UInt8*) "\007blender",
139                         kConnexionClientModeTakeOver, kConnexionMaskAll);
140
141                 // printf("ndof: client id = %d\n", m_clientID);
142
143                 if (SetConnexionClientButtonMask != NULL)
144                         {
145                         has_old_driver = false;
146                         SetConnexionClientButtonMask(m_clientID, kConnexionMaskAllButtons);
147                         }
148                 else
149                         printf("ndof: old 3Dx driver installed, some buttons may not work\n");
150                 }
151         else
152                 {
153                 printf("ndof: 3Dx driver not found\n");
154                 // This isn't a hard error, just means the user doesn't have a 3D mouse.
155                 }
156         }
157
158 GHOST_NDOFManagerCocoa::~GHOST_NDOFManagerCocoa()
159         {
160         UnregisterConnexionClient(m_clientID);
161         CleanupConnexionHandlers();
162         ghost_system = NULL;
163         ndof_manager = NULL;
164         }
165
166 bool GHOST_NDOFManagerCocoa::available()
167         {
168         // extern OSErr InstallConnexionHandlers() __attribute__((weak_import));
169         // ^^ not needed since the entire framework is weak-linked
170         return InstallConnexionHandlers != NULL;
171         // this means that the driver is installed and dynamically linked to blender
172         }