6f3876c1d960e5c755e36187f54f11c48a1b4c1c
[blender-staging.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * Contributor(s): none yet.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 #include <stdio.h> /* just for printf */
24
25 #include "GHOST_NDOFManager.h"
26
27
28 // the variable is outside the class because it must be accessed from plugin
29 static volatile GHOST_TEventNDOFData currentNdofValues = {0,0,0,0,0,0,0,0,0,0,0};
30
31 #if !defined(_WIN32) && !defined(__APPLE__)
32 #include "GHOST_SystemX11.h"
33 #endif
34
35 namespace
36 {
37     GHOST_NDOFLibraryInit_fp ndofLibraryInit = 0;
38     GHOST_NDOFLibraryShutdown_fp ndofLibraryShutdown = 0;
39     GHOST_NDOFDeviceOpen_fp ndofDeviceOpen = 0;
40 }
41
42 GHOST_NDOFManager::GHOST_NDOFManager()
43 {
44     m_DeviceHandle = 0;
45
46     // discover the API from the plugin
47     ndofLibraryInit = 0;
48     ndofLibraryShutdown = 0;
49     ndofDeviceOpen = 0;
50 }
51
52 GHOST_NDOFManager::~GHOST_NDOFManager()
53 {
54     if (ndofLibraryShutdown)
55         ndofLibraryShutdown(m_DeviceHandle);
56
57     m_DeviceHandle = 0;
58 }
59
60
61 int
62 GHOST_NDOFManager::deviceOpen(GHOST_IWindow* window,
63         GHOST_NDOFLibraryInit_fp setNdofLibraryInit, 
64         GHOST_NDOFLibraryShutdown_fp setNdofLibraryShutdown,
65         GHOST_NDOFDeviceOpen_fp setNdofDeviceOpen)
66 {
67         int Pid;
68         
69     ndofLibraryInit = setNdofLibraryInit;
70     ndofLibraryShutdown = setNdofLibraryShutdown;
71     ndofDeviceOpen = setNdofDeviceOpen;
72
73     if (ndofLibraryInit  && ndofDeviceOpen)
74     {
75         Pid= ndofLibraryInit();
76         printf("%i client \n", Pid);
77                 #if defined(_WIN32) || defined(__APPLE__)
78                         m_DeviceHandle = ndofDeviceOpen((void *)&currentNdofValues);    
79                 #else
80                         GHOST_SystemX11 *sys;
81                         sys = static_cast<GHOST_SystemX11*>(GHOST_ISystem::getSystem());
82                         void *ndofInfo = sys->prepareNdofInfo(&currentNdofValues);
83                         m_DeviceHandle = ndofDeviceOpen(ndofInfo);
84                 #endif
85                  return (Pid > 0) ? 0 : 1;
86                         
87         } else
88                 return 1;
89 }
90
91
92 bool 
93 GHOST_NDOFManager::available() const
94
95     return m_DeviceHandle != 0; 
96 }
97
98 bool 
99 GHOST_NDOFManager::event_present() const
100
101     if( currentNdofValues.changed >0) {
102                 printf("time %llu but%u x%i y%i z%i rx%i ry%i rz%i \n"  ,                       
103                                 currentNdofValues.time,         currentNdofValues.buttons,
104                                 currentNdofValues.tx,currentNdofValues.ty,currentNdofValues.tz,
105                                 currentNdofValues.rx,currentNdofValues.ry,currentNdofValues.rz);
106         return true;
107         }else
108         return false;
109
110 }
111
112 void        GHOST_NDOFManager::GHOST_NDOFGetDatas(GHOST_TEventNDOFData &datas) const
113 {
114         datas.tx = currentNdofValues.tx;
115         datas.ty = currentNdofValues.ty;
116         datas.tz = currentNdofValues.tz;
117         datas.rx = currentNdofValues.rx;
118         datas.ry = currentNdofValues.ry;
119         datas.rz = currentNdofValues.rz;
120         datas.buttons = currentNdofValues.buttons;
121         datas.client = currentNdofValues.client;
122         datas.address = currentNdofValues.address;
123         datas.time = currentNdofValues.time;
124         datas.delta = currentNdofValues.delta;
125 }