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
explicit 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(QuarterWidget* quarter) :
87
PRIVATE(this) = new SpaceNavigatorDeviceP(this);
89
#ifdef HAVE_SPACENAV_LIB
90
PRIVATE(this)->hasdevice =
91
spnav_x11_open(QX11Info::display(), PRIVATE(this)->windowid) == -1 ? false : true;
94
if (!PRIVATE(this)->hasdevice) {
95
fprintf(stderr, "Quarter:: Could not hook up to Spacenav device.\n");
101
SpaceNavigatorDevice::~SpaceNavigatorDevice()
103
delete PRIVATE(this);
108
SpaceNavigatorDevice::translateEvent(QEvent * event)
111
SoEvent * ret = nullptr;
113
#ifdef HAVE_SPACENAV_LIB
114
NativeEvent * ce = dynamic_cast<NativeEvent *>(event);
115
if (ce && ce->getEvent()) {
116
XEvent * xev = ce->getEvent();
119
if(spnav_x11_event(xev, &spev)) {
120
if(spev.type == SPNAV_EVENT_MOTION) {
122
const float axislen = sqrt(spev.motion.rx*spev.motion.rx +
123
spev.motion.ry*spev.motion.ry +
124
spev.motion.rz*spev.motion.rz);
126
const float half_angle = axislen * 0.5 * 0.001;
127
const float sin_half = sin(half_angle);
128
SbRotation rot((spev.motion.rx / axislen) * sin_half,
129
(spev.motion.ry / axislen) * sin_half,
130
(spev.motion.rz / axislen) * sin_half,
132
PRIVATE(this)->motionevent->setRotation(rot);
135
SbVec3f pos(spev.motion.x * 0.001,
136
spev.motion.y * 0.001,
137
spev.motion.z * 0.001);
138
PRIVATE(this)->motionevent->setTranslation(pos);
140
ret = PRIVATE(this)->motionevent;
142
else if (spev.type == SPNAV_EVENT_BUTTON){
143
if(spev.button.press) {
144
PRIVATE(this)->buttonevent->setState(SoButtonEvent::DOWN);
145
switch(spev.button.bnum) {
146
case 0: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON1);
148
case 1: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON2);
150
case 2: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON3);
152
case 3: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON4);
154
case 4: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON5);
156
case 5: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON6);
158
case 6: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON7);
160
case 7: PRIVATE(this)->buttonevent->setButton(SoSpaceballButtonEvent::BUTTON8);
169
PRIVATE(this)->buttonevent->setState(SoButtonEvent::UP);
172
ret = PRIVATE(this)->buttonevent;
176
assert(0 && "Unknown event type");