finished Mac NDOF device detection, moved core device ID to base NDOFManager, clarifi...
[blender.git] / intern / ghost / intern / GHOST_NDOFManagerX11.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_NDOFManagerX11.h"
25 #include "GHOST_SystemX11.h"
26 #include <spnav.h>
27 #include <stdio.h>
28
29
30 GHOST_NDOFManagerX11::GHOST_NDOFManagerX11(GHOST_System& sys)
31         : GHOST_NDOFManager(sys)
32         {
33         if (spnav_open() != -1)
34                 {
35                 m_available = true;
36
37                 // determine exactly which device is plugged in
38
39                 #define MAX_LINE_LENGTH 100
40
41                 // look for USB devices with Logitech's vendor ID
42                 FILE* command_output = popen("lsusb -d 046d:","r");
43                 if (command_output)
44                         {
45                         char line[MAX_LINE_LENGTH] = {0};
46                         while (fgets(line, MAX_LINE_LENGTH, command_output))
47                                 {
48                                 unsigned short vendor_id = 0, product_id = 0;
49                                 if (sscanf(line, "Bus %*d Device %*d: ID %hx:%hx", &vendor_id, &product_id) == 2)
50                                         setDevice(vendor_id, product_id);
51                                 }
52                         pclose(command_output);
53                         }
54                 }
55         else
56                 {
57                 m_available = false;
58                 printf("ndof: spacenavd not found\n");
59                 // This isn't a hard error, just means the user doesn't have a 3D mouse.
60                 }
61         }
62
63 GHOST_NDOFManagerX11::~GHOST_NDOFManagerX11()
64         {
65         if (m_available)
66                 {
67                 spnav_remove_events(SPNAV_EVENT_ANY); // ask nuclear if this is needed
68                 spnav_close();
69                 }
70         }
71
72 bool GHOST_NDOFManagerX11::available()
73         {
74         return m_available;
75         }
76
77 bool GHOST_NDOFManagerX11::processEvents()
78         {
79         GHOST_TUns64 now = m_system.getMilliSeconds();
80
81         bool anyProcessed = false;
82         spnav_event e;
83         while (spnav_poll_event(&e))
84                 {
85                 switch (e.type)
86                         {
87                         case SPNAV_EVENT_MOTION:
88                                 {
89                                 short t[3] = {e.motion.x, e.motion.y, e.motion.z};
90                                 short r[3] = {e.motion.rx, e.motion.ry, e.motion.rz};
91
92                                 updateTranslation(t, now);
93                                 updateRotation(r, now);
94                                 break;
95                                 }
96                         case SPNAV_EVENT_BUTTON:
97                                 updateButton(e.button.bnum, e.button.press, now);
98                                 break;
99                         }
100                 anyProcessed = true;
101                 }
102         return anyProcessed;
103         }