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