/**
* Spacenav Demo Qt
* Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <spnav.h>
#include "SpacenavWrapper.h"
SpacenavWrapper::SpacenavWrapper() {
if (spnav_open() == 0) {
connectionStatus = SpacenavWrapper::ConnectionStatus::DOMAIN_SOCKET;
return;
} else {
Display* display;
Window window;
unsigned long blackPixel;
if (display = XOpenDisplay(0)) {
blackPixel = BlackPixel(display, DefaultScreen(display));
window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, blackPixel, blackPixel);
if (spnav_x11_open(display, window) == 0) {
connectionStatus = SpacenavWrapper::ConnectionStatus::X11;
return;
}
}
}
connectionStatus = SpacenavWrapper::ConnectionStatus::NONE;
}
SpacenavWrapper::~SpacenavWrapper() {
// TODO: remove logging
std::wcout << L"calling spnav_close() in ~SpacenavReceiver()" << std::endl;
spnav_close();
}
SpacenavWrapper::ConnectionStatus SpacenavWrapper::getConnectionStatus() {
return connectionStatus;
}
SpacenavWrapper::Event SpacenavWrapper::waitEvent() {
SpacenavWrapper::Event e;
spnav_event event;
if (spnav_wait_event(&event)) {
e.type = event.type == SPNAV_EVENT_MOTION ? SpacenavWrapper::Event::Type::MOTION : SpacenavWrapper::Event::Type::BUTTON;
if (event.type == SPNAV_EVENT_MOTION) {
e.motion.x = event.motion.x;
e.motion.y = event.motion.y;
e.motion.z = event.motion.z;
e.motion.rx = event.motion.rx;
e.motion.ry = event.motion.ry;
e.motion.rz = event.motion.rz;
e.motion.type = event.motion.type;
e.motion.period = event.motion.period;
} else { /* SPNAV_EVENT_BUTTON */
e.button.number = event.button.bnum;
e.button.pressed = event.button.press;
}
return e;
} else {
// TODO: throw exception
return e;
}
}