Blender  V2.93
GHOST_NDOFManagerCocoa.mm
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #define DEBUG_NDOF_DRIVER false
18 
19 #include "GHOST_NDOFManagerCocoa.h"
20 #include "GHOST_SystemCocoa.h"
21 
22 #include <dlfcn.h>
23 #include <stdint.h>
24 
25 #if DEBUG_NDOF_DRIVER
26 # include <cstdio>
27 #endif
28 
29 // static callback functions need to talk to these objects:
32 
33 static uint16_t clientID = 0;
34 
35 static bool driver_loaded = false;
36 static bool has_old_driver =
37  false; // 3Dconnexion drivers before 10 beta 4 are "old", not all buttons will work
38 static bool has_new_driver =
39  false; // drivers >= 10.2.2 are "new", and can process events on a separate thread
40 
41 // replicate just enough of the 3Dx API for our uses, not everything the driver provides
42 
43 #define kConnexionClientModeTakeOver 1
44 #define kConnexionMaskAll 0x3fff
45 #define kConnexionMaskAllButtons 0xffffffff
46 #define kConnexionCmdHandleButtons 2
47 #define kConnexionCmdHandleAxis 3
48 #define kConnexionCmdAppSpecific 10
49 #define kConnexionMsgDeviceState '3dSR'
50 #define kConnexionCtlGetDeviceID '3did'
51 
52 #pragma pack(push, 2) // just this struct
61  uint16_t buttons8; // obsolete! (pre-10.x drivers)
62  int16_t axis[6]; // tx, ty, tz, rx, ry, rz
65 };
66 #pragma pack(pop)
67 
68 // callback functions:
69 typedef void (*AddedHandler)(uint32_t);
70 typedef void (*RemovedHandler)(uint32_t);
71 typedef void (*MessageHandler)(uint32_t, uint32_t msg_type, void *msg_arg);
72 
73 // driver functions:
76 typedef void (*CleanupConnexionHandlers_ptr)();
78  const char *name,
79  uint16_t mode,
80  uint32_t mask);
84  uint32_t message,
85  int32_t param,
86  int32_t *result);
87 
88 #define DECLARE_FUNC(name) name##_ptr name = NULL
89 
90 DECLARE_FUNC(SetConnexionHandlers);
91 DECLARE_FUNC(InstallConnexionHandlers);
92 DECLARE_FUNC(CleanupConnexionHandlers);
93 DECLARE_FUNC(RegisterConnexionClient);
94 DECLARE_FUNC(SetConnexionClientButtonMask);
95 DECLARE_FUNC(UnregisterConnexionClient);
96 DECLARE_FUNC(ConnexionClientControl);
97 
98 static void *load_func(void *module, const char *func_name)
99 {
100  void *func = dlsym(module, func_name);
101 
102 #if DEBUG_NDOF_DRIVER
103  if (func) {
104  printf("'%s' loaded :D\n", func_name);
105  }
106  else {
107  printf("<!> %s\n", dlerror());
108  }
109 #endif
110 
111  return func;
112 }
113 
114 #define LOAD_FUNC(name) name = (name##_ptr)load_func(module, #name)
115 
116 static void *module; // handle to the whole driver
117 
119 {
120  if (driver_loaded) {
121  return true;
122  }
123 
124  module = dlopen("/Library/Frameworks/3DconnexionClient.framework/3DconnexionClient",
125  RTLD_LAZY | RTLD_LOCAL);
126 
127  if (module) {
128  LOAD_FUNC(SetConnexionHandlers);
129 
130  if (SetConnexionHandlers != NULL) {
131  driver_loaded = true;
132  has_new_driver = true;
133  }
134  else {
135  LOAD_FUNC(InstallConnexionHandlers);
136 
137  driver_loaded = (InstallConnexionHandlers != NULL);
138  }
139 
140  if (driver_loaded) {
141  LOAD_FUNC(CleanupConnexionHandlers);
142  LOAD_FUNC(RegisterConnexionClient);
143  LOAD_FUNC(SetConnexionClientButtonMask);
144  LOAD_FUNC(UnregisterConnexionClient);
145  LOAD_FUNC(ConnexionClientControl);
146 
147  has_old_driver = (SetConnexionClientButtonMask == NULL);
148  }
149  }
150 #if DEBUG_NDOF_DRIVER
151  else {
152  printf("<!> %s\n", dlerror());
153  }
154 
155  printf("loaded: %s\n", driver_loaded ? "YES" : "NO");
156  printf("old: %s\n", has_old_driver ? "YES" : "NO");
157  printf("new: %s\n", has_new_driver ? "YES" : "NO");
158 #endif
159 
160  return driver_loaded;
161 }
162 
163 static void unload_driver()
164 {
165  dlclose(module);
166 }
167 
168 static void DeviceAdded(uint32_t unused)
169 {
170 #if DEBUG_NDOF_DRIVER
171  printf("ndof: device added\n");
172 #endif
173 
174  // determine exactly which device is plugged in
175  int32_t result;
176  ConnexionClientControl(clientID, kConnexionCtlGetDeviceID, 0, &result);
177  int16_t vendorID = result >> 16;
178  int16_t productID = result & 0xffff;
179 
180  ndof_manager->setDevice(vendorID, productID);
181 }
182 
183 static void DeviceRemoved(uint32_t unused)
184 {
185 #if DEBUG_NDOF_DRIVER
186  printf("ndof: device removed\n");
187 #endif
188 }
189 
190 static void DeviceEvent(uint32_t unused, uint32_t msg_type, void *msg_arg)
191 {
192  if (msg_type == kConnexionMsgDeviceState) {
194 
195  // device state is broadcast to all clients; only react if sent to us
196  if (s->client == clientID) {
197  // TODO: is s->time compatible with GHOST timestamps? if so use that instead.
199 
200  switch (s->command) {
202  // convert to blender view coordinates
203  const int t[3] = {s->axis[0], -(s->axis[2]), s->axis[1]};
204  const int r[3] = {-(s->axis[3]), s->axis[5], -(s->axis[4])};
205 
208 
210  break;
211  }
213  int button_bits = has_old_driver ? s->buttons8 : s->buttons;
214 #ifdef DEBUG_NDOF_BUTTONS
215  printf("button bits: 0x%08x\n", button_bits);
216 #endif
217  ndof_manager->updateButtons(button_bits, now);
219  break;
220  }
221 #if DEBUG_NDOF_DRIVER
223  printf("ndof: app-specific command, param = %hd, value = %d\n", s->param, s->value);
224  break;
225 
226  default:
227  printf("ndof: mystery device command %d\n", s->command);
228 #endif
229  }
230  }
231  }
232 }
233 
235 {
236  if (load_driver_functions()) {
237  // give static functions something to talk to:
238  ghost_system = dynamic_cast<GHOST_SystemCocoa *>(&sys);
239  ndof_manager = this;
240 
241  uint16_t error;
242  if (has_new_driver) {
243  const bool separate_thread = false; // TODO: rework Mac event handler to allow this
244  error = SetConnexionHandlers(DeviceEvent, DeviceAdded, DeviceRemoved, separate_thread);
245  }
246  else {
247  error = InstallConnexionHandlers(DeviceEvent, DeviceAdded, DeviceRemoved);
248  }
249 
250  if (error) {
251 #if DEBUG_NDOF_DRIVER
252  printf("ndof: error %d while setting up handlers\n", error);
253 #endif
254  return;
255  }
256 
257  // Pascal string *and* a four-letter constant. How old-skool.
258  clientID = RegisterConnexionClient(
259  'blnd', "\007blender", kConnexionClientModeTakeOver, kConnexionMaskAll);
260 
261  if (!has_old_driver) {
262  SetConnexionClientButtonMask(clientID, kConnexionMaskAllButtons);
263  }
264  }
265 }
266 
268 {
269  if (driver_loaded) {
270  UnregisterConnexionClient(clientID);
271  CleanupConnexionHandlers();
272  unload_driver();
273 
274  ghost_system = NULL;
275  ndof_manager = NULL;
276  }
277 }
278 
280 {
281  return driver_loaded;
282 }
void(* CleanupConnexionHandlers_ptr)()
static GHOST_NDOFManager * ndof_manager
static void DeviceAdded(uint32_t unused)
uint16_t(* RegisterConnexionClient_ptr)(uint32_t signature, const char *name, uint16_t mode, uint32_t mask)
int16_t(* InstallConnexionHandlers_ptr)(MessageHandler, AddedHandler, RemovedHandler)
#define kConnexionCmdAppSpecific
int16_t(* SetConnexionHandlers_ptr)(MessageHandler, AddedHandler, RemovedHandler, bool)
void(* SetConnexionClientButtonMask_ptr)(uint16_t clientID, uint32_t buttonMask)
#define kConnexionCmdHandleButtons
static bool load_driver_functions()
void(* RemovedHandler)(uint32_t)
static void unload_driver()
static void * load_func(void *module, const char *func_name)
static void * module
static bool has_old_driver
static uint16_t clientID
static void DeviceEvent(uint32_t unused, uint32_t msg_type, void *msg_arg)
static void DeviceRemoved(uint32_t unused)
void(* MessageHandler)(uint32_t, uint32_t msg_type, void *msg_arg)
static bool driver_loaded
#define DECLARE_FUNC(name)
#define kConnexionMsgDeviceState
#define kConnexionMaskAll
static bool has_new_driver
#define kConnexionCtlGetDeviceID
#define kConnexionCmdHandleAxis
#define kConnexionMaskAllButtons
void(* AddedHandler)(uint32_t)
void(* UnregisterConnexionClient_ptr)(uint16_t clientID)
#define kConnexionClientModeTakeOver
#define LOAD_FUNC(name)
int16_t(* ConnexionClientControl_ptr)(uint16_t clientID, uint32_t message, int32_t param, int32_t *result)
static GHOST_SystemCocoa * ghost_system
unsigned long long GHOST_TUns64
Definition: GHOST_Types.h:86
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
GHOST_NDOFManagerCocoa(GHOST_System &)
void updateTranslation(const int t[3], GHOST_TUns64 time)
void updateButtons(int button_bits, GHOST_TUns64 time)
bool setDevice(unsigned short vendor_id, unsigned short product_id)
void updateRotation(const int r[3], GHOST_TUns64 time)
GHOST_TUns64 getMilliSeconds() const
static void error(const char *str)
Definition: meshlaplacian.c:65
signed short int16_t
Definition: stdint.h:79
unsigned short uint16_t
Definition: stdint.h:82
unsigned int uint32_t
Definition: stdint.h:83
signed int int32_t
Definition: stdint.h:80
unsigned char uint8_t
Definition: stdint.h:81
unsigned __int64 uint64_t
Definition: stdint.h:93
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)