1
/**************************************************************************\
2
* Copyright (c) Kongsberg Oil & Gas Technologies AS
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are
9
* Redistributions of source code must retain the above copyright notice,
10
* this list of conditions and the following disclaimer.
12
* Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
16
* Neither the name of the copyright holder nor the names of its
17
* contributors may be used to endorse or promote products derived from
18
* this software without specific prior written permission.
20
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
\**************************************************************************/
34
#pragma warning(disable : 4267)
40
#include <Inventor/events/SoEvent.h>
41
#include <Inventor/events/SoMotion3Event.h>
42
#include <Inventor/events/SoSpaceballButtonEvent.h>
44
#ifdef HAVE_SPACENAV_LIB
49
#include "NativeEvent.h"
50
#include "devices/SpaceNavigatorDevice.h"
53
namespace SIM { namespace Coin3D { namespace Quarter {
54
class SpaceNavigatorDeviceP {
56
SpaceNavigatorDeviceP(SpaceNavigatorDevice * master) {
57
this->master = master;
58
this->hasdevice = false;
60
this->motionevent = new SoMotion3Event;
61
this->buttonevent = new SoSpaceballButtonEvent;
63
~SpaceNavigatorDeviceP() {
64
delete this->motionevent;
65
delete this->buttonevent;
68
static bool customEventFilter(void * message, long * result);
70
SpaceNavigatorDevice * master;
74
SoMotion3Event * motionevent;
75
SoSpaceballButtonEvent * buttonevent;
81
#define PRIVATE(obj) obj->pimpl
82
using namespace SIM::Coin3D::Quarter;
84
SpaceNavigatorDevice::SpaceNavigatorDevice()
86
PRIVATE(this) = new SpaceNavigatorDeviceP(this);
88
#ifdef HAVE_SPACENAV_LIB
89
PRIVATE(this)->hasdevice =
90
spnav_x11_open(QX11Info::display(), PRIVATE(this)->windowid) == -1 ? false : true;
92
// FIXME: Use a debugmessage mechanism instead? (20101020 handegar)
93
if (!PRIVATE(this)->hasdevice) {
94
fprintf(stderr, "Quarter:: Could not hook up to Spacenav device.\n");
97
#endif // HAVE_SPACENAV_LIB
100
SpaceNavigatorDevice::SpaceNavigatorDevice(QuarterWidget *quarter) :
103
PRIVATE(this) = new SpaceNavigatorDeviceP(this);
105
#ifdef HAVE_SPACENAV_LIB
106
PRIVATE(this)->hasdevice =
107
spnav_x11_open(QX11Info::display(), PRIVATE(this)->windowid) == -1 ? false : true;
109
// FIXME: Use a debugmessage mechanism instead? (20101020 handegar)
110
if (!PRIVATE(this)->hasdevice) {
111
fprintf(stderr, "Quarter:: Could not hook up to Spacenav device.\n");
114
#endif // HAVE_SPACENAV_LIB
117
SpaceNavigatorDevice::~SpaceNavigatorDevice()
119
delete PRIVATE(this);
124
SpaceNavigatorDevice::translateEvent(QEvent * event)
127
SoEvent * ret = nullptr;
129
#ifdef HAVE_SPACENAV_LIB
130
NativeEvent * ce = dynamic_cast<NativeEvent *>(event);
131
if (ce && ce->getEvent()) {
132
XEvent * xev = ce->getEvent();
135
if(spnav_x11_event(xev, &spev)) {
136
if(spev.type == SPNAV_EVENT_MOTION) {
138
const float axislen = sqrt(spev.motion.rx*spev.motion.rx +
139
spev.motion.ry*spev.motion.ry +
140
spev.motion.rz*spev.motion.rz);
142
const float half_angle = axislen * 0.5 * 0.001;
143
const float sin_half = sin(half_angle);
144
SbRotation rot((spev.motion.rx / axislen) * sin_half,
145
(spev.motion.ry / axislen) * sin_half,
146
(spev.motion.rz / axislen) * sin_half,
148
PRIVATE(this)->motionevent->setRotation(rot);
151
SbVec3f pos(spev.motion.x * 0.001,
152
spev.motion.y * 0.001,
153
spev.motion.z * 0.001);
154
PRIVATE(this)->motionevent->setTranslation(pos);
156
ret = PRIVATE(this)->motionevent;
158
else if (spev.type == SPNAV_EVENT_BUTTON){
159
if(spev.button.press) {
160
PRIVATE(this)->buttonevent->setState(SoButtonEvent::DOWN);
161
switch(spev.button.bnum) {
162
case 0: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON1);
164
case 1: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON2);
166
case 2: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON3);
168
case 3: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON4);
170
case 4: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON5);
172
case 5: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON6);
174
case 6: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON7);
176
case 7: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON8);
179
// FIXME: Which button corresponds to the
180
// SoSpaceballButtonEvent::PICK enum? (20101020 handegar)
185
PRIVATE(this)->buttonevent->setState(SoButtonEvent::UP);
188
ret = PRIVATE(this)->buttonevent;
191
// Unknown Spacenav event.
192
assert(0 && "Unknown event type");
196
#endif // HAVE_SPACENAV_LIB