src/SpacenavReceiver.h
branchv_0
changeset 4 1383ad1c4f57
parent 3 6baa91ac3199
child 6 49560660d230
equal deleted inserted replaced
3:6baa91ac3199 4:1383ad1c4f57
     1 /**
     1 /**
     2  * Spacenav Demo Qt
     2  * Spacenav Demo Qt
     3  * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
     3  * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
     4  *
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     8  * (at your option) any later version.
    15  * You should have received a copy of the GNU General Public License
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    17  */
    18 #pragma once
    18 #pragma once
    19 
    19 
    20 #include <iostream>
       
    21 #include <QObject>
    20 #include <QObject>
    22 #include <QThread>
    21 #include <QThread>
    23 #include <spnav.h>
       
    24 
    22 
    25 Q_DECLARE_METATYPE(spnav_event)
    23 #include "SpacenavWrapper.h"
       
    24 
       
    25 Q_DECLARE_METATYPE(SpacenavWrapper::Event)
    26 
    26 
    27 class SpacenavReceiver : public QThread {
    27 class SpacenavReceiver : public QThread {
       
    28 	Q_OBJECT
       
    29 private:
       
    30 	SpacenavWrapper spnav;
    28 
    31 
    29 	Q_OBJECT
       
    30 public:
    32 public:
    31 
       
    32 
    33 
    33 	SpacenavReceiver() : QThread() {
    34 	SpacenavReceiver() : QThread() {
    34 	}
    35 	}
    35 
    36 
    36 	virtual ~SpacenavReceiver() {
       
    37 		std::wcout << L"calling spnav_close() in ~SpacenavReceiver()" << std::endl;
       
    38 		spnav_close();
       
    39 	}
       
    40 
       
    41 	void run() {
    37 	void run() {
    42 		spnav_event event;
    38 		while (true) {
    43 
    39 			SpacenavWrapper::Event e = spnav.waitEvent();
    44 		// Connect
    40 			emit spacenavEvent(e);
    45 		if (spnav_open() == 0) {
       
    46 			std::wcout << L"connected through: AF_UNIX socket" << std::endl;
       
    47 		} else {
       
    48 
       
    49 			Display* display;
       
    50 			Window window;
       
    51 			unsigned long blackPixel;
       
    52 
       
    53 
       
    54 			if (!(display = XOpenDisplay(0))) {
       
    55 				std::wcout << L"unable to connect to the X server" << std::endl;
       
    56 				return; // TODO: throw exception
       
    57 			}
       
    58 
       
    59 			blackPixel = BlackPixel(display, DefaultScreen(display));
       
    60 			window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, blackPixel, blackPixel);
       
    61 
       
    62 			if (spnav_x11_open(display, window) == 0) {
       
    63 				std::wcout << L"connected through: X11" << std::endl;
       
    64 			} else {
       
    65 				std::wcout << L"unable to connect to the space navigator daemon" << std::endl;
       
    66 				return; // TODO: throw exception
       
    67 			}
       
    68 		}
    41 		}
    69 
       
    70 		// For stopping this thread by pressing both buttons:
       
    71 		bool pressed0 = false;
       
    72 		bool pressed1 = false;
       
    73 
       
    74 		// Process events
       
    75 		while (spnav_wait_event(&event)) {
       
    76 			if (event.type == SPNAV_EVENT_MOTION) {
       
    77 				std::wcout << L"motion event: t(" << event.motion.x << L", " << event.motion.y << L", " << event.motion.z << L") ";
       
    78 				std::wcout << L"r(" << event.motion.rx << L", " << event.motion.ry << L", " << event.motion.rz << L")" << std::endl;
       
    79 
       
    80 			} else { /* SPNAV_EVENT_BUTTON */
       
    81 				std::wcout << L"button " << (event.button.press ? "press" : "release") << L" event b(" << event.button.bnum << L")" << std::endl;
       
    82 
       
    83 				if (event.button.bnum == 0) pressed0 = event.button.press;
       
    84 				if (event.button.bnum == 1) pressed1 = event.button.press;
       
    85 				if (pressed0 && pressed1) {
       
    86 					std::wcout << L"both buttons pressed → stop receiving events" << std::endl;
       
    87 					break;
       
    88 				}
       
    89 			}
       
    90 			emit spacenavEvent(event);
       
    91 		}
       
    92 
       
    93 	}
    42 	}
    94 
    43 
    95 signals:
    44 signals:
    96 	void spacenavEvent(spnav_event sev);
    45 	void spacenavEvent(SpacenavWrapper::Event e);
    97 };
    46 };