source: osgVisual/src/manip_Spacemouse/manip_spaceMouse.cpp @ 94

Last change on this file since 94 was 88, checked in by Torben Dannhauer, 14 years ago

Moved memory leak detection from source file to headerfile. Its still in the class but at least not in the source file.

The leak detection works, but the false positives are not stopped.
Use Linux/Valgrind? to make your final leak detection beyond the easy first approach in MSVC

File size: 4.8 KB
Line 
1/* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer
2 *
3 * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * osgVisual requires for some proprietary modules a license from the correspondig manufacturer.
9 * You have to aquire licenses for all used proprietary modules.
10 *
11 * This library 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 * OpenSceneGraph Public License for more details.
15*/
16
17#include <manip_spaceMouse.h>
18
19#define _ATL_APARTMENT_THREADED
20#define _ATL_NO_AUTOMATIC_NAMESPACE
21
22#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // some CString constructors will be explicit
23
24#define _WIN32_DCOM
25
26#include <objbase.h>
27#include <atlbase.h>
28#include <atlcom.h>
29#include <atlwin.h>
30#include <atltypes.h>
31#include <atlctl.h>
32#include <atlhost.h>
33
34
35
36using namespace ATL;
37using namespace osgVisual;
38
39#import "progid:TDxInput.Device" no_namespace
40
41class SpaceMouse::SpaceMouseImpl
42{
43public:
44    enum SupportedStatus
45    {
46        DETECTION_PENDING,
47        SUPPORTED,
48        NOT_SUPPORTED
49    };
50
51    SpaceMouseImpl() : _supported(DETECTION_PENDING)
52    {
53    }
54
55    SupportedStatus isSupported() const
56    {
57        return(_supported);
58    }
59
60    int initialize()
61    {
62        HRESULT hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
63
64        // Create the device object
65        hr = _3DxDevice.CoCreateInstance(__uuidof(Device));
66        if (SUCCEEDED(hr))
67        {
68            CComPtr<ISimpleDevice> _3DxSimpleDevice;
69
70            hr = _3DxDevice.QueryInterface(&_3DxSimpleDevice);
71            if (SUCCEEDED(hr))
72            {
73                // Get the interfaces to the sensor and the keyboard;
74                _3DSensor   = _3DxSimpleDevice->Sensor;
75                _3DKeyboard = _3DxSimpleDevice->Keyboard;
76
77                // Associate a configuration with this device
78                hr = _3DxSimpleDevice->LoadPreferences(_T("RBAnimation"));
79                // Connect to the driver
80                hr = _3DxSimpleDevice->Connect();
81
82                _supported = SUPPORTED;
83
84                return(1); //Base::SUCCESS
85            }
86        }
87
88        _supported = NOT_SUPPORTED;
89
90        return(0);      //Base::FAILURE
91    }
92
93    void shutdown()
94    {
95        if (_3DSensor)
96            _3DSensor.Release();
97
98        if (_3DKeyboard)
99            _3DKeyboard.Release();
100
101        if (_3DxDevice)
102        {
103            _3DxDevice->Disconnect();
104            _3DxDevice.Release();
105        }
106    }
107
108
109    int getTranslations(double& dTX, double& dTY, double& dTZ)
110    {
111        try {
112            CComPtr<IVector3D> pTranslation = _3DSensor->Translation;
113
114            double dTransFactor = pTranslation->Length;
115            dTransFactor /= 5.0;
116
117            dTX = pTranslation->X * dTransFactor;
118            dTY = pTranslation->Y * dTransFactor;
119            dTZ = pTranslation->Z * dTransFactor;
120
121            pTranslation.Release();
122        }
123        catch (...)
124        {
125            return 0;   //Base::FAILURE
126        }
127
128        return 1;       //Base::SUCCESS
129    }
130
131    int getRotations(double& dRX, double& dRY, double& dRZ)
132    {
133        try {
134            CComPtr<IAngleAxis> pRotation = _3DSensor->Rotation;
135
136            double dRotAngle = pRotation->Angle;
137            dRotAngle /= 4000.0;
138
139            dRX = pRotation->X * dRotAngle;
140            dRY = pRotation->Y * dRotAngle;
141            dRZ = pRotation->Z * dRotAngle;
142
143            pRotation.Release();
144        }
145        catch (...)
146        {
147            return 0;   //Base::FAILURE
148        }
149
150        return 1;       //Base::SUCCESS
151    }
152
153private:
154    SupportedStatus _supported;
155
156    CComPtr<ISimpleDevice> _3DxDevice;
157    CComPtr<ISensor>       _3DSensor;
158    CComPtr<IKeyboard>     _3DKeyboard;
159};
160
161
162
163
164
165
166
167
168SpaceMouse::SpaceMouse() : _spaceMouseImpl(new SpaceMouseImpl)
169{
170}
171
172SpaceMouse::~SpaceMouse()
173{
174    delete _spaceMouseImpl;
175}
176
177int SpaceMouse::initialize()
178{
179    return(_spaceMouseImpl->initialize());
180}
181
182void SpaceMouse::shutdown()
183{
184    _spaceMouseImpl->shutdown();
185}
186
187int SpaceMouse::getTranslations(double& dX, double& dY, double& dZ)
188{
189    dX = 0;
190    dY = 0;
191    dZ = 0;
192    return(_spaceMouseImpl->getTranslations(dX, dY, dZ));
193}
194
195int SpaceMouse::getRotations(double& dX, double& dY, double& dZ)
196{
197    dX = 0;
198    dY = 0;
199    dZ = 0;
200    return(_spaceMouseImpl->getRotations(dX, dY, dZ));
201}
Note: See TracBrowser for help on using the repository browser.