support both: AF_UNIX and X11 connections v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 06 Mar 2019 23:19:10 +0100
branchv_0
changeset 3 6baa91ac3199
parent 2 21b0b2b0547e
child 4 1383ad1c4f57
support both: AF_UNIX and X11 connections
src/SpacenavReceiver.h
--- a/src/SpacenavReceiver.h	Wed Mar 06 21:08:34 2019 +0100
+++ b/src/SpacenavReceiver.h	Wed Mar 06 23:19:10 2019 +0100
@@ -39,38 +39,39 @@
 	}
 
 	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
-		}
+		// Connect
+		if (spnav_open() == 0) {
+			std::wcout << L"connected through: AF_UNIX socket" << std::endl;
+		} else {
 
-		blackPixel = BlackPixel(display, DefaultScreen(display));
-		window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, blackPixel, blackPixel);
+			Display* display;
+			Window window;
+			unsigned long 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
+			if (!(display = XOpenDisplay(0))) {
+				std::wcout << L"unable to connect to the X server" << 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;
-			 * 	}
-			 */
+			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
+			}
 		}
 
 		// 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") ";