Renamed Mac file to exclude it from Windows build. Revoke this cowardly commit once...
[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): none yet.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22  
23 #include "GHOST_NDOFManagerCocoa.h"
24 #include "GHOST_SystemCocoa.h"
25
26 extern "C" {
27         #include <3DconnexionClient/ConnexionClientAPI.h>
28         #include <stdio.h>
29         }
30
31 static void SpaceNavAdded(io_connect_t connection)
32         {
33         printf("SpaceNav added\n");
34         }
35
36 static void SpaceNavRemoved(io_connect_t connection)
37         {
38         printf("SpaceNav removed\n");
39         }
40
41 static void SpaceNavEvent(io_connect_t connection, natural_t messageType, void *messageArgument)
42         {
43         GHOST_SystemCocoa* system = (GHOST_SystemCocoa*) GHOST_ISystem::getSystem();
44         GHOST_NDOFManager* manager = system->getNDOFManager();
45         switch (messageType)
46                 {
47                 case kConnexionMsgDeviceState:
48                         {
49                         ConnexionDeviceState* s = (ConnexionDeviceState*)messageArgument;
50                         switch (s->command)
51                                 {
52                                 case kConnexionCmdHandleAxis:
53                                         manager->updateTranslation(s->axis, s->time);
54                                         manager->updateRotation(s->axis + 3, s->time);
55                                         system->notifyExternalEventProcessed();
56                                         break;
57
58                                 case kConnexionCmdHandleButtons:
59                                         manager->updateButtons(s->buttons, s->time);
60                                         system->notifyExternalEventProcessed();
61                                         break;
62                                 }
63                         break;
64                         }
65                 case kConnexionMsgPrefsChanged:
66                         printf("prefs changed\n"); // this includes app switches
67                         break;
68                 case kConnexionMsgDoAction:
69                         printf("do action\n"); // no idea what this means
70                         // 'calibrate device' in System Prefs sends this
71                         break;
72                 default:
73                         printf("<!> mystery event\n");
74                 }
75         }
76
77 GHOST_NDOFManagerCocoa::GHOST_NDOFManagerCocoa(GHOST_System& sys)
78         : GHOST_NDOFManager(sys)
79         {
80         if (available())
81                 {
82                 OSErr error = InstallConnexionHandlers(SpaceNavEvent, SpaceNavAdded, SpaceNavRemoved);
83                 if (error)
84                         {
85                         printf("<!> error = %d\n", error);
86                         return;
87                         }
88
89                 // Pascal string *and* a four-letter constant. How old-skool.
90                 m_clientID = RegisterConnexionClient('blnd', (UInt8*) "\pBlender",
91                         kConnexionClientModeTakeOver, kConnexionMaskAll);
92
93                 printf("client id = %d\n", m_clientID);
94                 }
95         else
96                 {
97                 printf("<!> SpaceNav driver not found\n");
98                 // This isn't a hard error, just means the user doesn't have a SpaceNavigator.
99                 }
100         }
101
102 GHOST_NDOFManagerCocoa::~GHOST_NDOFManagerCocoa()
103         {
104         UnregisterConnexionClient(m_clientID);
105         CleanupConnexionHandlers();
106         }
107
108 bool GHOST_NDOFManagerCocoa::available()
109         {
110 //      extern OSErr InstallConnexionHandlers() __attribute__((weak_import));
111 // ^-- not needed since the entire framework is weak-linked
112         return InstallConnexionHandlers != NULL;
113         }