receive spacenav events (in separate thread) and print them on stderr v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 06 Mar 2019 20:28:34 +0100
branchv_0
changeset 1 2a3e9f07c128
parent 0 37aa134ae57b
child 2 21b0b2b0547e
receive spacenav events (in separate thread) and print them on stderr
nbproject/configurations.xml
src/CMakeLists.txt
src/SpacenavReceiver.h
src/spacenav-demo-qt.cpp
--- a/nbproject/configurations.xml	Wed Mar 06 16:21:18 2019 +0100
+++ b/nbproject/configurations.xml	Wed Mar 06 20:28:34 2019 +0100
@@ -49,6 +49,13 @@
     </logicalFolder>
     <df root="." name="0">
       <df name="build">
+        <df name="qt-debug">
+          <df name="src">
+            <df name="spacenav-demo-qt_autogen">
+              <in>mocs_compilation.cpp</in>
+            </df>
+          </df>
+        </df>
         <df name="Release">
           <df name="src">
             <df name="spacenav-demo-qt_autogen">
@@ -90,6 +97,8 @@
       </toolsSet>
       <flagsDictionary>
         <element flagsID="0" commonFlags="-fsanitize=address -fPIC -std=gnu++11"/>
+        <element flagsID="1"
+                 commonFlags="-mtune=generic -march=x86-64 -std=gnu++11 -fsanitize=address -fPIC -fstack-protector-strong"/>
       </flagsDictionary>
       <codeAssistance>
       </codeAssistance>
@@ -99,23 +108,6 @@
           <buildCommand>${MAKE} -f Makefile</buildCommand>
           <cleanCommand>${MAKE} -f Makefile clean</cleanCommand>
           <executablePath>build/Debug/src/spacenav-demo-qt</executablePath>
-          <ccTool>
-            <incDir>
-              <pElem>build/Debug/src</pElem>
-              <pElem>src</pElem>
-              <pElem>build/Debug/src/spacenav-demo-qt_autogen/include</pElem>
-              <pElem>/usr/include/x86_64-linux-gnu/qt5</pElem>
-              <pElem>/usr/include/x86_64-linux-gnu/qt5/QtWidgets</pElem>
-              <pElem>/usr/include/x86_64-linux-gnu/qt5/QtGui</pElem>
-              <pElem>/usr/include/x86_64-linux-gnu/qt5/QtCore</pElem>
-              <pElem>/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++</pElem>
-            </incDir>
-            <preprocessorList>
-              <Elem>QT_CORE_LIB</Elem>
-              <Elem>QT_GUI_LIB</Elem>
-              <Elem>QT_WIDGETS_LIB</Elem>
-            </preprocessorList>
-          </ccTool>
         </makeTool>
         <preBuild>
           <preBuildCommandWorkingDir>build/Debug</preBuildCommandWorkingDir>
@@ -135,11 +127,76 @@
         <ccTool flags="0">
         </ccTool>
       </item>
+      <item path="build/qt-debug/src/spacenav-demo-qt_autogen/mocs_compilation.cpp"
+            ex="false"
+            tool="1"
+            flavor2="8">
+        <ccTool flags="1">
+        </ccTool>
+      </item>
+      <folder path="0/build">
+        <ccTool>
+          <incDir>
+            <pElem>build/qt-debug/src</pElem>
+          </incDir>
+        </ccTool>
+      </folder>
+      <folder path="0/src">
+        <ccTool>
+          <incDir>
+            <pElem>build/Debug/src</pElem>
+            <pElem>src</pElem>
+            <pElem>build/Debug/src/spacenav-demo-qt_autogen/include</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtWidgets</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtGui</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtCore</pElem>
+            <pElem>/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++</pElem>
+          </incDir>
+          <preprocessorList>
+            <Elem>QT_CORE_LIB</Elem>
+            <Elem>QT_GUI_LIB</Elem>
+            <Elem>QT_WIDGETS_LIB</Elem>
+          </preprocessorList>
+        </ccTool>
+      </folder>
       <folder path="Modules">
         <ccTool>
           <incDir>
+            <pElem>build/Debug/src</pElem>
+            <pElem>src</pElem>
+            <pElem>build/Debug/src/spacenav-demo-qt_autogen/include</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtWidgets</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtGui</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtCore</pElem>
+            <pElem>/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++</pElem>
             <pElem>/usr/include</pElem>
           </incDir>
+          <preprocessorList>
+            <Elem>QT_CORE_LIB</Elem>
+            <Elem>QT_GUI_LIB</Elem>
+            <Elem>QT_WIDGETS_LIB</Elem>
+          </preprocessorList>
+        </ccTool>
+      </folder>
+      <folder path="spacenav-demo-qt_autogen">
+        <ccTool>
+          <incDir>
+            <pElem>build/Debug/src</pElem>
+            <pElem>src</pElem>
+            <pElem>build/Debug/src/spacenav-demo-qt_autogen/include</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtWidgets</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtGui</pElem>
+            <pElem>/usr/include/x86_64-linux-gnu/qt5/QtCore</pElem>
+            <pElem>/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++</pElem>
+          </incDir>
+          <preprocessorList>
+            <Elem>QT_CORE_LIB</Elem>
+            <Elem>QT_GUI_LIB</Elem>
+            <Elem>QT_WIDGETS_LIB</Elem>
+          </preprocessorList>
         </ccTool>
       </folder>
       <item path="src/spacenav-demo-qt.cpp" ex="false" tool="1" flavor2="8">
--- a/src/CMakeLists.txt	Wed Mar 06 16:21:18 2019 +0100
+++ b/src/CMakeLists.txt	Wed Mar 06 20:28:34 2019 +0100
@@ -32,6 +32,9 @@
 add_executable(
 	${EXECUTABLE_FILE}
 	# QObjects must be listed here (including them from other files is not enough)
+	SpacenavReceiver.h
+	#SpacenavWindow.ui
+	#SpacenavWindow.cpp
 	spacenav-demo-qt.cpp
 )
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/SpacenavReceiver.h	Wed Mar 06 20:28:34 2019 +0100
@@ -0,0 +1,92 @@
+/**
+ * Spacenav Demo Qt
+ * Copyright © 2018 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, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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/>.
+ */
+#pragma once
+
+#include <iostream>
+#include <QObject>
+#include <QThread>
+#include <spnav.h>
+
+Q_DECLARE_METATYPE(spnav_event)
+
+class SpacenavReceiver : public QThread {
+
+	Q_OBJECT
+public:
+
+
+	SpacenavReceiver() : QThread() {
+	}
+
+	virtual ~SpacenavReceiver() {
+		spnav_close();
+	}
+
+	void run() {
+		Display* display;
+		Window window;
+		unsigned long blackPixel;
+
+		spnav_event event;
+
+		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) == -1) {
+			std::wcout << L"unable to connect to the space navigator daemon" << std::endl;
+			return; // TODO: throw exception
+
+			/**
+			 * TODO: try also unix socket
+			 * 
+			 * 	if(spnav_open()==-1) {
+			 * 	  	fprintf(stderr, "failed to connect to the space navigator daemon\n");
+			 * 		return 1;
+			 * 	}
+			 */
+		}
+
+		// For stopping this thread by pressing both buttons:
+		// bool pressed0 = false;
+		// bool pressed1 = false;
+
+		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 (sev.button.bnum == 0) pressed0 = sev.button.press;
+				// if (sev.button.bnum == 1) pressed1 = sev.button.press;
+				// if (pressed0 && pressed1) break;
+			}
+			emit spacenavEvent(event);
+		}
+
+	}
+
+signals:
+	void spacenavEvent(spnav_event sev);
+};
\ No newline at end of file
--- a/src/spacenav-demo-qt.cpp	Wed Mar 06 16:21:18 2019 +0100
+++ b/src/spacenav-demo-qt.cpp	Wed Mar 06 20:28:34 2019 +0100
@@ -25,9 +25,18 @@
 #include <X11/Xlib.h>
 #include <spnav.h>
 
+#include "SpacenavReceiver.h"
+//#include "SpacenavWindow.h"
+
 int main(int argc, char**argv) {
 	setlocale(LC_ALL, "");
 	QApplication app(argc, argv);
 
-	return 0;
+	SpacenavReceiver t;
+	t.start();
+	
+	// SpacenavWindow w;
+	// w.show();
+
+	return app.exec();
 }