src/SpacenavReceiver.h
branchv_0
changeset 1 2a3e9f07c128
child 2 21b0b2b0547e
equal deleted inserted replaced
0:37aa134ae57b 1:2a3e9f07c128
       
     1 /**
       
     2  * Spacenav Demo Qt
       
     3  * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     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
       
     7  * the Free Software Foundation, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    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/>.
       
    17  */
       
    18 #pragma once
       
    19 
       
    20 #include <iostream>
       
    21 #include <QObject>
       
    22 #include <QThread>
       
    23 #include <spnav.h>
       
    24 
       
    25 Q_DECLARE_METATYPE(spnav_event)
       
    26 
       
    27 class SpacenavReceiver : public QThread {
       
    28 
       
    29 	Q_OBJECT
       
    30 public:
       
    31 
       
    32 
       
    33 	SpacenavReceiver() : QThread() {
       
    34 	}
       
    35 
       
    36 	virtual ~SpacenavReceiver() {
       
    37 		spnav_close();
       
    38 	}
       
    39 
       
    40 	void run() {
       
    41 		Display* display;
       
    42 		Window window;
       
    43 		unsigned long blackPixel;
       
    44 
       
    45 		spnav_event event;
       
    46 
       
    47 		if (!(display = XOpenDisplay(0))) {
       
    48 			std::wcout << L"unable to connect to the X server" << std::endl;
       
    49 			return; // TODO: throw exception
       
    50 		}
       
    51 
       
    52 		blackPixel = BlackPixel(display, DefaultScreen(display));
       
    53 		window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, blackPixel, blackPixel);
       
    54 
       
    55 		if (spnav_x11_open(display, window) == -1) {
       
    56 			std::wcout << L"unable to connect to the space navigator daemon" << std::endl;
       
    57 			return; // TODO: throw exception
       
    58 
       
    59 			/**
       
    60 			 * TODO: try also unix socket
       
    61 			 * 
       
    62 			 * 	if(spnav_open()==-1) {
       
    63 			 * 	  	fprintf(stderr, "failed to connect to the space navigator daemon\n");
       
    64 			 * 		return 1;
       
    65 			 * 	}
       
    66 			 */
       
    67 		}
       
    68 
       
    69 		// For stopping this thread by pressing both buttons:
       
    70 		// bool pressed0 = false;
       
    71 		// bool pressed1 = false;
       
    72 
       
    73 		while (spnav_wait_event(&event)) {
       
    74 			if (event.type == SPNAV_EVENT_MOTION) {
       
    75 				std::wcout << L"motion event: t(" << event.motion.x << L", " << event.motion.y << L", " << event.motion.z << L") ";
       
    76 				std::wcout << L"r(" << event.motion.rx << L", " << event.motion.ry << L", " << event.motion.rz << L")" << std::endl;
       
    77 
       
    78 			} else { /* SPNAV_EVENT_BUTTON */
       
    79 				std::wcout << L"button " << (event.button.press ? "press" : "release") << L" event b(" << event.button.bnum << L")" << std::endl;
       
    80 
       
    81 				// if (sev.button.bnum == 0) pressed0 = sev.button.press;
       
    82 				// if (sev.button.bnum == 1) pressed1 = sev.button.press;
       
    83 				// if (pressed0 && pressed1) break;
       
    84 			}
       
    85 			emit spacenavEvent(event);
       
    86 		}
       
    87 
       
    88 	}
       
    89 
       
    90 signals:
       
    91 	void spacenavEvent(spnav_event sev);
       
    92 };