Blender V4.3
GHOST_NDOFManagerCocoa.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2010-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#define DEBUG_NDOF_DRIVER false
6
9
10#include <dlfcn.h>
11#include <stdint.h>
12
13#if DEBUG_NDOF_DRIVER
14# include <cstdio>
15#endif
16
17/* Static callback functions need to talk to these objects: */
20
21static uint16_t clientID = 0;
22
23static bool driver_loaded = false;
24static bool has_old_driver =
25 false; /* 3Dconnexion drivers before 10 beta 4 are "old", not all buttons will work. */
26static bool has_new_driver =
27 false; /* drivers >= 10.2.2 are "new", and can process events on a separate thread. */
28
29/* Replicate just enough of the 3Dx API for our uses, not everything the driver provides. */
30
31#define kConnexionClientModeTakeOver 1
32#define kConnexionMaskAll 0x3fff
33#define kConnexionMaskAllButtons 0xffffffff
34#define kConnexionCmdHandleButtons 2
35#define kConnexionCmdHandleAxis 3
36#define kConnexionCmdAppSpecific 10
37#define kConnexionMsgDeviceState '3dSR'
38#define kConnexionCtlGetDeviceID '3did'
39
40#pragma pack(push, 2) /* Just this struct. */
54#pragma pack(pop)
55
56/* Callback functions: */
57typedef void (*AddedHandler)(uint32_t);
58typedef void (*RemovedHandler)(uint32_t);
59typedef void (*MessageHandler)(uint32_t, uint32_t msg_type, void *msg_arg);
60
61/* Driver functions: */
66 const char *name,
67 uint16_t mode,
72 uint32_t message,
73 int32_t param,
75
76#define DECLARE_FUNC(name) name##_ptr name = nullptr
77
78DECLARE_FUNC(SetConnexionHandlers);
79DECLARE_FUNC(InstallConnexionHandlers);
80DECLARE_FUNC(CleanupConnexionHandlers);
81DECLARE_FUNC(RegisterConnexionClient);
82DECLARE_FUNC(SetConnexionClientButtonMask);
83DECLARE_FUNC(UnregisterConnexionClient);
84DECLARE_FUNC(ConnexionClientControl);
85
86static void *load_func(void *module, const char *func_name)
87{
88 void *func = dlsym(module, func_name);
89
90#if DEBUG_NDOF_DRIVER
91 if (func) {
92 printf("'%s' loaded :D\n", func_name);
93 }
94 else {
95 printf("<!> %s\n", dlerror());
96 }
97#endif
98
99 return func;
100}
101
102#define LOAD_FUNC(name) name = (name##_ptr)load_func(module, #name)
103
104static void *module; /* Handle to the whole driver. */
105
107{
108 if (driver_loaded) {
109 return true;
110 }
111
112 module = dlopen("/Library/Frameworks/3DconnexionClient.framework/3DconnexionClient",
113 RTLD_LAZY | RTLD_LOCAL);
114
115 if (module) {
116 LOAD_FUNC(SetConnexionHandlers);
117
118 if (SetConnexionHandlers != nullptr) {
119 driver_loaded = true;
120 has_new_driver = true;
121 }
122 else {
123 LOAD_FUNC(InstallConnexionHandlers);
124
125 driver_loaded = (InstallConnexionHandlers != nullptr);
126 }
127
128 if (driver_loaded) {
129 LOAD_FUNC(CleanupConnexionHandlers);
130 LOAD_FUNC(RegisterConnexionClient);
131 LOAD_FUNC(SetConnexionClientButtonMask);
132 LOAD_FUNC(UnregisterConnexionClient);
133 LOAD_FUNC(ConnexionClientControl);
134
135 has_old_driver = (SetConnexionClientButtonMask == nullptr);
136 }
137 }
138#if DEBUG_NDOF_DRIVER
139 else {
140 printf("<!> %s\n", dlerror());
141 }
142
143 printf("loaded: %s\n", driver_loaded ? "YES" : "NO");
144 printf("old: %s\n", has_old_driver ? "YES" : "NO");
145 printf("new: %s\n", has_new_driver ? "YES" : "NO");
146#endif
147
148 return driver_loaded;
149}
150
151static void unload_driver()
152{
153 dlclose(module);
154}
155
156static void DeviceAdded(uint32_t /*unused*/)
157{
158#if DEBUG_NDOF_DRIVER
159 printf("ndof: device added\n");
160#endif
161
162 /* Determine exactly which device is plugged in. */
164 ConnexionClientControl(clientID, kConnexionCtlGetDeviceID, 0, &result);
165 int16_t vendorID = result >> 16;
166 int16_t productID = result & 0xffff;
167
168 ndof_manager->setDevice(vendorID, productID);
169}
170
171static void DeviceRemoved(uint32_t /*unused*/)
172{
173#if DEBUG_NDOF_DRIVER
174 printf("ndof: device removed\n");
175#endif
176}
177
178static void DeviceEvent(uint32_t /*unused*/, uint32_t msg_type, void *msg_arg)
179{
180 if (msg_type == kConnexionMsgDeviceState) {
182
183 /* Device state is broadcast to all clients; only react if sent to us. */
184 if (s->client == clientID) {
185 /* TODO: is s->time compatible with GHOST timestamps? if so use that instead. */
187
188 switch (s->command) {
190 /* convert to blender view coordinates. */
191 const int t[3] = {s->axis[0], -(s->axis[2]), s->axis[1]};
192 const int r[3] = {-(s->axis[3]), s->axis[5], -(s->axis[4])};
193
196
198 break;
199 }
201 int button_bits = has_old_driver ? s->buttons8 : s->buttons;
202#ifdef DEBUG_NDOF_BUTTONS
203 printf("button bits: 0x%08x\n", button_bits);
204#endif
205 ndof_manager->updateButtonsBitmask(button_bits, now);
207 break;
208 }
209#if DEBUG_NDOF_DRIVER
211 printf("ndof: app-specific command, param = %hd, value = %d\n", s->param, s->value);
212 break;
213
214 default:
215 printf("ndof: mystery device command %d\n", s->command);
216#endif
217 }
218 }
219 }
220}
221
223{
224 if (load_driver_functions()) {
225 /* Give static functions something to talk to: */
226 ghost_system = dynamic_cast<GHOST_SystemCocoa *>(&sys);
227 ndof_manager = this;
228
230 if (has_new_driver) {
231 const bool separate_thread = false; /* TODO: rework Mac event handler to allow this. */
232 error = SetConnexionHandlers(DeviceEvent, DeviceAdded, DeviceRemoved, separate_thread);
233 }
234 else {
235 error = InstallConnexionHandlers(DeviceEvent, DeviceAdded, DeviceRemoved);
236 }
237
238 if (error) {
239#if DEBUG_NDOF_DRIVER
240 printf("ndof: error %d while setting up handlers\n", error);
241#endif
242 return;
243 }
244
245 /* Pascal string *and* a four-letter constant. How old-school. */
246 clientID = RegisterConnexionClient(
247 'blnd', "\007blender", kConnexionClientModeTakeOver, kConnexionMaskAll);
248
249 if (!has_old_driver) {
250 SetConnexionClientButtonMask(clientID, kConnexionMaskAllButtons);
251 }
252 }
253}
254
256{
257 if (driver_loaded) {
258 UnregisterConnexionClient(clientID);
259 CleanupConnexionHandlers();
261
262 ghost_system = nullptr;
263 ndof_manager = nullptr;
264 }
265}
266
static void DeviceAdded(uint32_t)
void(* CleanupConnexionHandlers_ptr)()
static GHOST_NDOFManager * ndof_manager
static void DeviceRemoved(uint32_t)
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 * module
static bool has_old_driver
static uint16_t clientID
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
static void * load_func(void *module, const char *func_name)
#define kConnexionMaskAllButtons
static void DeviceEvent(uint32_t, uint32_t msg_type, void *msg_arg)
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
void updateButtonsBitmask(int button_bits, uint64_t time)
void updateTranslation(const int t[3], uint64_t time)
void updateRotation(const int r[3], uint64_t time)
bool setDevice(unsigned short vendor_id, unsigned short product_id)
uint64_t getMilliSeconds() const override
#define printf
ccl_device_inline float4 mask(const int4 mask, const float4 a)
static void error(const char *str)
signed short int16_t
Definition stdint.h:76
unsigned short uint16_t
Definition stdint.h:79
unsigned int uint32_t
Definition stdint.h:80
signed int int32_t
Definition stdint.h:77
unsigned char uint8_t
Definition stdint.h:78
unsigned __int64 uint64_t
Definition stdint.h:90