--- a/src/SpacenavReceiver.h Wed Mar 06 23:19:10 2019 +0100
+++ b/src/SpacenavReceiver.h Thu Mar 07 14:55:56 2019 +0100
@@ -1,6 +1,6 @@
/**
* Spacenav Demo Qt
- * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
+ * 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
@@ -17,81 +17,30 @@
*/
#pragma once
-#include <iostream>
#include <QObject>
#include <QThread>
-#include <spnav.h>
-Q_DECLARE_METATYPE(spnav_event)
+#include "SpacenavWrapper.h"
+
+Q_DECLARE_METATYPE(SpacenavWrapper::Event)
class SpacenavReceiver : public QThread {
+ Q_OBJECT
+private:
+ SpacenavWrapper spnav;
- Q_OBJECT
public:
-
SpacenavReceiver() : QThread() {
}
- virtual ~SpacenavReceiver() {
- std::wcout << L"calling spnav_close() in ~SpacenavReceiver()" << std::endl;
- spnav_close();
- }
-
void run() {
- spnav_event event;
-
- // Connect
- if (spnav_open() == 0) {
- std::wcout << L"connected through: AF_UNIX socket" << std::endl;
- } else {
-
- Display* display;
- Window window;
- unsigned long blackPixel;
-
-
- if (!(display = XOpenDisplay(0))) {
- std::wcout << L"unable to connect to the X server" << std::endl;
- return; // TODO: throw exception
- }
-
- blackPixel = BlackPixel(display, DefaultScreen(display));
- window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, blackPixel, blackPixel);
-
- if (spnav_x11_open(display, window) == 0) {
- std::wcout << L"connected through: X11" << std::endl;
- } else {
- std::wcout << L"unable to connect to the space navigator daemon" << std::endl;
- return; // TODO: throw exception
- }
+ while (true) {
+ SpacenavWrapper::Event e = spnav.waitEvent();
+ emit spacenavEvent(e);
}
-
- // For stopping this thread by pressing both buttons:
- bool pressed0 = false;
- bool pressed1 = false;
-
- // Process events
- while (spnav_wait_event(&event)) {
- if (event.type == SPNAV_EVENT_MOTION) {
- std::wcout << L"motion event: t(" << event.motion.x << L", " << event.motion.y << L", " << event.motion.z << L") ";
- std::wcout << L"r(" << event.motion.rx << L", " << event.motion.ry << L", " << event.motion.rz << L")" << std::endl;
-
- } else { /* SPNAV_EVENT_BUTTON */
- std::wcout << L"button " << (event.button.press ? "press" : "release") << L" event b(" << event.button.bnum << L")" << std::endl;
-
- if (event.button.bnum == 0) pressed0 = event.button.press;
- if (event.button.bnum == 1) pressed1 = event.button.press;
- if (pressed0 && pressed1) {
- std::wcout << L"both buttons pressed → stop receiving events" << std::endl;
- break;
- }
- }
- emit spacenavEvent(event);
- }
-
}
signals:
- void spacenavEvent(spnav_event sev);
+ void spacenavEvent(SpacenavWrapper::Event e);
};
\ No newline at end of file