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