e608301bbcd1d4ae1517236d8e4eb6494daa1596
[blender.git] / intern / ghost / intern / GHOST_NDOFManager.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s):
21  *   Mike Erwin
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 #include "GHOST_Debug.h"
27 #include "GHOST_NDOFManager.h"
28 #include "GHOST_EventNDOF.h"
29 #include "GHOST_EventKey.h"
30 #include "GHOST_WindowManager.h"
31 #include <string.h> // for memory functions
32 #include <stdio.h> // for error/info reporting
33 #include <math.h>
34
35 #ifdef DEBUG_NDOF_MOTION
36 // printable version of each GHOST_TProgress value
37 static const char* progress_string[] =
38         {"not started","starting","in progress","finishing","finished"};
39 #endif
40
41 #ifdef DEBUG_NDOF_BUTTONS
42 static const char* ndof_button_names[] = {
43         // used internally, never sent
44         "NDOF_BUTTON_NONE",
45         // these two are available from any 3Dconnexion device
46         "NDOF_BUTTON_MENU",
47         "NDOF_BUTTON_FIT",
48         // standard views
49         "NDOF_BUTTON_TOP",
50         "NDOF_BUTTON_BOTTOM",
51         "NDOF_BUTTON_LEFT",
52         "NDOF_BUTTON_RIGHT",
53         "NDOF_BUTTON_FRONT",
54         "NDOF_BUTTON_BACK",
55         // more views
56         "NDOF_BUTTON_ISO1",
57         "NDOF_BUTTON_ISO2",
58         // 90 degree rotations
59         "NDOF_BUTTON_ROLL_CW",
60         "NDOF_BUTTON_ROLL_CCW",
61         "NDOF_BUTTON_SPIN_CW",
62         "NDOF_BUTTON_SPIN_CCW",
63         "NDOF_BUTTON_TILT_CW",
64         "NDOF_BUTTON_TILT_CCW",
65         // device control
66         "NDOF_BUTTON_ROTATE",
67         "NDOF_BUTTON_PANZOOM",
68         "NDOF_BUTTON_DOMINANT",
69         "NDOF_BUTTON_PLUS",
70         "NDOF_BUTTON_MINUS",
71         // general-purpose buttons
72         "NDOF_BUTTON_1",
73         "NDOF_BUTTON_2",
74         "NDOF_BUTTON_3",
75         "NDOF_BUTTON_4",
76         "NDOF_BUTTON_5",
77         "NDOF_BUTTON_6",
78         "NDOF_BUTTON_7",
79         "NDOF_BUTTON_8",
80         "NDOF_BUTTON_9",
81         "NDOF_BUTTON_10",
82 };
83 #endif
84
85 static const NDOF_ButtonT SpaceNavigator_HID_map[] = {
86         NDOF_BUTTON_MENU,
87         NDOF_BUTTON_FIT
88 };
89
90 static const NDOF_ButtonT SpaceExplorer_HID_map[] = {
91         NDOF_BUTTON_1,
92         NDOF_BUTTON_2,
93         NDOF_BUTTON_TOP,
94         NDOF_BUTTON_LEFT,
95         NDOF_BUTTON_RIGHT,
96         NDOF_BUTTON_FRONT,
97         NDOF_BUTTON_NONE, // esc key
98         NDOF_BUTTON_NONE, // alt key
99         NDOF_BUTTON_NONE, // shift key
100         NDOF_BUTTON_NONE, // ctrl key
101         NDOF_BUTTON_FIT,
102         NDOF_BUTTON_MENU,
103         NDOF_BUTTON_PLUS,
104         NDOF_BUTTON_MINUS,
105         NDOF_BUTTON_ROTATE
106 };
107
108 static const NDOF_ButtonT SpacePilotPro_HID_map[] = {
109         NDOF_BUTTON_MENU,
110         NDOF_BUTTON_FIT,
111         NDOF_BUTTON_TOP,
112         NDOF_BUTTON_LEFT,
113         NDOF_BUTTON_RIGHT,
114         NDOF_BUTTON_FRONT,
115         NDOF_BUTTON_BOTTOM,
116         NDOF_BUTTON_BACK,
117         NDOF_BUTTON_ROLL_CW,
118         NDOF_BUTTON_ROLL_CCW,
119         NDOF_BUTTON_ISO1,
120         NDOF_BUTTON_ISO2,
121         NDOF_BUTTON_1,
122         NDOF_BUTTON_2,
123         NDOF_BUTTON_3,
124         NDOF_BUTTON_4,
125         NDOF_BUTTON_5,
126         NDOF_BUTTON_6,
127         NDOF_BUTTON_7,
128         NDOF_BUTTON_8,
129         NDOF_BUTTON_9,
130         NDOF_BUTTON_10,
131         NDOF_BUTTON_NONE, // esc key
132         NDOF_BUTTON_NONE, // alt key
133         NDOF_BUTTON_NONE, // shift key
134         NDOF_BUTTON_NONE, // ctrl key
135         NDOF_BUTTON_ROTATE,
136         NDOF_BUTTON_PANZOOM,
137         NDOF_BUTTON_DOMINANT,
138         NDOF_BUTTON_PLUS,
139         NDOF_BUTTON_MINUS
140 };
141
142 /* this is the older SpacePilot (sans Pro)
143  * thanks to polosson for the info in this table */
144 static const NDOF_ButtonT SpacePilot_HID_map[] = {
145         NDOF_BUTTON_1,
146         NDOF_BUTTON_2,
147         NDOF_BUTTON_3,
148         NDOF_BUTTON_4,
149         NDOF_BUTTON_5,
150         NDOF_BUTTON_6,
151         NDOF_BUTTON_TOP,
152         NDOF_BUTTON_LEFT,
153         NDOF_BUTTON_RIGHT,
154         NDOF_BUTTON_FRONT,
155         NDOF_BUTTON_NONE, // esc key
156         NDOF_BUTTON_NONE, // alt key
157         NDOF_BUTTON_NONE, // shift key
158         NDOF_BUTTON_NONE, // ctrl key
159         NDOF_BUTTON_FIT,
160         NDOF_BUTTON_MENU,
161         NDOF_BUTTON_PLUS,
162         NDOF_BUTTON_MINUS,
163         NDOF_BUTTON_DOMINANT,
164         NDOF_BUTTON_ROTATE,
165         NDOF_BUTTON_NONE // the CONFIG button -- what does it do?
166 };
167
168 GHOST_NDOFManager::GHOST_NDOFManager(GHOST_System& sys)
169         : m_system(sys)
170         , m_deviceType(NDOF_UnknownDevice) // each platform has its own device detection code
171         , m_buttonCount(0)
172         , m_buttonMask(0)
173         , m_buttons(0)
174         , m_motionTime(0)
175         , m_prevMotionTime(0)
176         , m_motionState(GHOST_kNotStarted)
177         , m_motionEventPending(false)
178         , m_deadZone(0.f)
179 {
180         // to avoid the rare situation where one triple is updated and
181         // the other is not, initialize them both here:
182         memset(m_translation, 0, sizeof(m_translation));
183         memset(m_rotation, 0, sizeof(m_rotation));
184
185 #ifdef WITH_INPUT_NDOF
186         GHOST_PRINT("WITH_INPUT_NDOF is defined!");
187 #else
188         GHOST_PRINT("WITH_INPUT_NDOF is not defined.");
189 #endif
190 }
191
192 bool GHOST_NDOFManager::setDevice(unsigned short vendor_id, unsigned short product_id)
193 {
194         // default to NDOF_UnknownDevice so rogue button events will get discarded
195         // "mystery device" owners can help build a HID_map for their hardware
196
197         switch (vendor_id) {
198                 case 0x046D: // Logitech (3Dconnexion)
199                         switch (product_id) {
200                                 // -- current devices --
201                                 case 0xC626:
202                                         puts("ndof: using SpaceNavigator");
203                                         m_deviceType = NDOF_SpaceNavigator;
204                                         m_buttonCount = 2;
205                                         break;
206                                 case 0xC628:
207                                         puts("ndof: using SpaceNavigator for Notebooks");
208                                         m_deviceType = NDOF_SpaceNavigator; // for Notebooks
209                                         m_buttonCount = 2;
210                                         break;
211                                 case 0xC627:
212                                         puts("ndof: using SpaceExplorer");
213                                         m_deviceType = NDOF_SpaceExplorer;
214                                         m_buttonCount = 15;
215                                         break;
216                                 case 0xC629:
217                                         puts("ndof: using SpacePilotPro");
218                                         m_deviceType = NDOF_SpacePilotPro;
219                                         m_buttonCount = 31;
220                                         break;
221
222                                         // -- older devices --
223                                 case 0xC625:
224                                         puts("ndof: using SpacePilot");
225                                         m_deviceType = NDOF_SpacePilot;
226                                         m_buttonCount = 21;
227                                         break;
228
229                                 case 0xC623:
230                                         puts("ndof: SpaceTraveler not supported, please file a bug report");
231                                         m_buttonCount = 8;
232                                         break;
233
234                                 default:
235                                         printf("ndof: unknown Logitech product %04hx\n", product_id);
236                         }
237                         break;
238                 default:
239                         printf("ndof: unknown device %04hx:%04hx\n", vendor_id, product_id);
240         }
241
242         if (m_deviceType == NDOF_UnknownDevice) {
243                 return false;
244         }
245         else {
246                 m_buttonMask = ~(-1 << m_buttonCount);
247
248 #ifdef DEBUG_NDOF_BUTTONS
249                 printf("ndof: %d buttons -> hex:%X\n", m_buttonCount, m_buttonMask);
250 #endif
251
252                 return true;
253         }
254 }
255
256 void GHOST_NDOFManager::updateTranslation(short t[3], GHOST_TUns64 time)
257 {
258         memcpy(m_translation, t, sizeof(m_translation));
259         m_motionTime = time;
260         m_motionEventPending = true;
261 }
262
263 void GHOST_NDOFManager::updateRotation(short r[3], GHOST_TUns64 time)
264 {
265         memcpy(m_rotation, r, sizeof(m_rotation));
266         m_motionTime = time;
267         m_motionEventPending = true;
268 }
269
270 void GHOST_NDOFManager::sendButtonEvent(NDOF_ButtonT button, bool press, GHOST_TUns64 time, GHOST_IWindow* window)
271 {
272         GHOST_EventNDOFButton* event = new GHOST_EventNDOFButton(time, window);
273         GHOST_TEventNDOFButtonData* data = (GHOST_TEventNDOFButtonData*) event->getData();
274
275         data->action = press ? GHOST_kPress : GHOST_kRelease;
276         data->button = button;
277
278 #ifdef DEBUG_NDOF_BUTTONS
279         printf("%s %s\n", ndof_button_names[button], press ? "pressed" : "released");
280 #endif
281
282         m_system.pushEvent(event);
283 }
284
285 void GHOST_NDOFManager::sendKeyEvent(GHOST_TKey key, bool press, GHOST_TUns64 time, GHOST_IWindow* window)
286 {
287         GHOST_TEventType type = press ? GHOST_kEventKeyDown : GHOST_kEventKeyUp;
288         GHOST_EventKey* event = new GHOST_EventKey(time, type, window, key);
289
290 #ifdef DEBUG_NDOF_BUTTONS
291         printf("keyboard %s\n", press ? "down" : "up");
292 #endif
293
294         m_system.pushEvent(event);
295 }
296
297 void GHOST_NDOFManager::updateButton(int button_number, bool press, GHOST_TUns64 time)
298 {
299         GHOST_IWindow* window = m_system.getWindowManager()->getActiveWindow();
300
301 #ifdef DEBUG_NDOF_BUTTONS
302         if (m_deviceType != NDOF_UnknownDevice)
303                 printf("ndof: button %d -> ", button_number);
304 #endif
305
306         switch (m_deviceType) {
307                 case NDOF_SpaceNavigator:
308                         sendButtonEvent(SpaceNavigator_HID_map[button_number], press, time, window);
309                         break;
310                 case NDOF_SpaceExplorer:
311                         switch (button_number) {
312                                 case 6: sendKeyEvent(GHOST_kKeyEsc, press, time, window); break;
313                                 case 7: sendKeyEvent(GHOST_kKeyLeftAlt, press, time, window); break;
314                                 case 8: sendKeyEvent(GHOST_kKeyLeftShift, press, time, window); break;
315                                 case 9: sendKeyEvent(GHOST_kKeyLeftControl, press, time, window); break;
316                                 default: sendButtonEvent(SpaceExplorer_HID_map[button_number], press, time, window);
317                         }
318                         break;
319                 case NDOF_SpacePilotPro:
320                         switch (button_number) {
321                                 case 22: sendKeyEvent(GHOST_kKeyEsc, press, time, window); break;
322                                 case 23: sendKeyEvent(GHOST_kKeyLeftAlt, press, time, window); break;
323                                 case 24: sendKeyEvent(GHOST_kKeyLeftShift, press, time, window); break;
324                                 case 25: sendKeyEvent(GHOST_kKeyLeftControl, press, time, window); break;
325                                 default: sendButtonEvent(SpacePilotPro_HID_map[button_number], press, time, window);
326                         }
327                         break;
328                 case NDOF_SpacePilot:
329                         switch (button_number) {
330                                 case 10: sendKeyEvent(GHOST_kKeyEsc, press, time, window); break;
331                                 case 11: sendKeyEvent(GHOST_kKeyLeftAlt, press, time, window); break;
332                                 case 12: sendKeyEvent(GHOST_kKeyLeftShift, press, time, window); break;
333                                 case 13: sendKeyEvent(GHOST_kKeyLeftControl, press, time, window); break;
334                                 case 20: puts("ndof: ignoring CONFIG button"); break;
335                                 default: sendButtonEvent(SpacePilot_HID_map[button_number], press, time, window);
336                         }
337                         break;
338                 case NDOF_UnknownDevice:
339                         printf("ndof: button %d on unknown device (ignoring)\n", button_number);
340         }
341
342         int mask = 1 << button_number;
343         if (press) {
344                 m_buttons |= mask; // set this button's bit
345         }
346         else {
347                 m_buttons &= ~mask; // clear this button's bit
348         }
349 }
350
351 void GHOST_NDOFManager::updateButtons(int button_bits, GHOST_TUns64 time)
352 {
353         button_bits &= m_buttonMask; // discard any "garbage" bits
354
355         int diff = m_buttons ^ button_bits;
356
357         for (int button_number = 0; button_number < m_buttonCount; ++button_number) {
358                 int mask = 1 << button_number;
359
360                 if (diff & mask) {
361                         bool press = button_bits & mask;
362                         updateButton(button_number, press, time);
363                 }
364         }
365 }
366
367 void GHOST_NDOFManager::setDeadZone(float dz)
368 {
369         if (dz < 0.f) {
370                 // negative values don't make sense, so clamp at zero
371                 dz = 0.f;
372         }
373         else if (dz > 0.5f) {
374                 // warn the rogue user/programmer, but allow it
375                 printf("ndof: dead zone of %.2f is rather high...\n", dz);
376         }
377         m_deadZone = dz;
378
379         printf("ndof: dead zone set to %.2f\n", dz);
380 }
381
382 static bool atHomePosition(GHOST_TEventNDOFMotionData* ndof)
383 {
384 #define HOME(foo) (ndof->foo == 0)
385         return HOME(tx) && HOME(ty) && HOME(tz) && HOME(rx) && HOME(ry) && HOME(rz);
386 #undef HOME
387 }
388
389 static bool nearHomePosition(GHOST_TEventNDOFMotionData* ndof, float threshold)
390 {
391         if (threshold == 0.f) {
392                 return atHomePosition(ndof);
393         }
394         else {
395 #define HOME1(foo) (fabsf(ndof->foo) < threshold)
396                 return HOME1(tx) && HOME1(ty) && HOME1(tz) && HOME1(rx) && HOME1(ry) && HOME1(rz);
397 #undef HOME1
398         }
399 }
400
401 bool GHOST_NDOFManager::sendMotionEvent()
402 {
403         if (!m_motionEventPending)
404                 return false;
405
406         m_motionEventPending = false; // any pending motion is handled right now
407
408         GHOST_IWindow* window = m_system.getWindowManager()->getActiveWindow();
409
410         if (window == NULL) {
411                 return false; // delivery will fail, so don't bother sending
412         }
413
414         GHOST_EventNDOFMotion* event = new GHOST_EventNDOFMotion(m_motionTime, window);
415         GHOST_TEventNDOFMotionData* data = (GHOST_TEventNDOFMotionData*) event->getData();
416
417         // scale axis values here to normalize them to around +/- 1
418         // they are scaled again for overall sensitivity in the WM based on user prefs
419
420         const float scale = 1.f / 350.f; // 3Dconnexion devices send +/- 350 usually
421
422         data->tx = scale * m_translation[0];
423         data->ty = scale * m_translation[1];
424         data->tz = scale * m_translation[2];
425
426         data->rx = scale * m_rotation[0];
427         data->ry = scale * m_rotation[1];
428         data->rz = scale * m_rotation[2];
429
430         data->dt = 0.001f * (m_motionTime - m_prevMotionTime); // in seconds
431
432         bool handMotion = !nearHomePosition(data, m_deadZone);
433
434         // determine what kind of motion event to send (Starting, InProgress, Finishing)
435         // and where that leaves this NDOF manager (NotStarted, InProgress, Finished)
436         switch (m_motionState) {
437                 case GHOST_kNotStarted:
438                 case GHOST_kFinished:
439                         if (handMotion) {
440                                 data->progress = GHOST_kStarting;
441                                 m_motionState = GHOST_kInProgress;
442                                 // prev motion time will be ancient, so just make up something reasonable
443                                 data->dt = 0.0125f;
444                         }
445                         else {
446                                 // send no event and keep current state
447                                 delete event;
448                                 return false;
449                         }
450                         break;
451                 case GHOST_kInProgress:
452                         if (handMotion) {
453                                 data->progress = GHOST_kInProgress;
454                                 // keep InProgress state
455                         }
456                         else {
457                                 data->progress = GHOST_kFinishing;
458                                 m_motionState = GHOST_kFinished;
459                         }
460                         break;
461         }
462
463 #ifdef DEBUG_NDOF_MOTION
464         printf("ndof motion sent -- %s\n", progress_string[data->progress]);
465
466         // show details about this motion event
467         printf("    T=(%.2f,%.2f,%.2f) R=(%.2f,%.2f,%.2f) dt=%.3f\n",
468                data->tx, data->ty, data->tz,
469                data->rx, data->ry, data->rz,
470                data->dt);
471 #endif
472
473         m_system.pushEvent(event);
474
475         m_prevMotionTime = m_motionTime;
476
477         return true;
478 }