both buttons pressed → stop receiving events v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 06 Mar 2019 21:08:34 +0100
branchv_0
changeset 2 21b0b2b0547e
parent 1 2a3e9f07c128
child 3 6baa91ac3199
both buttons pressed → stop receiving events
src/SpacenavReceiver.h
src/spacenav-demo-qt.cpp
--- a/src/SpacenavReceiver.h	Wed Mar 06 20:28:34 2019 +0100
+++ b/src/SpacenavReceiver.h	Wed Mar 06 21:08:34 2019 +0100
@@ -34,6 +34,7 @@
 	}
 
 	virtual ~SpacenavReceiver() {
+		std::wcout << L"calling spnav_close() in ~SpacenavReceiver()" << std::endl;
 		spnav_close();
 	}
 
@@ -67,8 +68,8 @@
 		}
 
 		// For stopping this thread by pressing both buttons:
-		// bool pressed0 = false;
-		// bool pressed1 = false;
+		bool pressed0 = false;
+		bool pressed1 = false;
 
 		while (spnav_wait_event(&event)) {
 			if (event.type == SPNAV_EVENT_MOTION) {
@@ -78,9 +79,12 @@
 			} 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;
+				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);
 		}
--- a/src/spacenav-demo-qt.cpp	Wed Mar 06 20:28:34 2019 +0100
+++ b/src/spacenav-demo-qt.cpp	Wed Mar 06 21:08:34 2019 +0100
@@ -19,6 +19,7 @@
 
 #include <QApplication>
 #include <QThread>
+#include <QMainWindow>
 
 #include <stdlib.h>
 #include <signal.h>
@@ -34,9 +35,19 @@
 
 	SpacenavReceiver t;
 	t.start();
-	
-	// SpacenavWindow w;
+
+	// QMainWindow w;
 	// w.show();
 
-	return app.exec();
+	int qtResultCode = app.exec();
+
+	if (t.isRunning()) {
+		std::wcerr << L"Background RelationalReader thread is still running → terminate()" << std::endl;
+		t.terminate();
+		std::wcerr << L"Background RelationalReader thread was terminated → wait()" << std::endl;
+		t.wait();
+		std::wcerr << L"Background RelationalReader thread wait() finished" << std::endl;
+	}
+
+	return qtResultCode;
 }