src/SpacenavReceiver.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 06 Mar 2019 20:28:34 +0100
branchv_0
changeset 1 2a3e9f07c128
child 2 21b0b2b0547e
permissions -rw-r--r--
receive spacenav events (in separate thread) and print them on stderr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Spacenav Demo Qt
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <iostream>
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <QObject>
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <QThread>
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <spnav.h>
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
Q_DECLARE_METATYPE(spnav_event)
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
class SpacenavReceiver : public QThread {
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
	Q_OBJECT
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
public:
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
	SpacenavReceiver() : QThread() {
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	}
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	virtual ~SpacenavReceiver() {
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
		spnav_close();
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	}
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	void run() {
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
		Display* display;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		Window window;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
		unsigned long blackPixel;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		spnav_event event;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		if (!(display = XOpenDisplay(0))) {
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
			std::wcout << L"unable to connect to the X server" << std::endl;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
			return; // TODO: throw exception
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		}
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		blackPixel = BlackPixel(display, DefaultScreen(display));
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, blackPixel, blackPixel);
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		if (spnav_x11_open(display, window) == -1) {
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
			std::wcout << L"unable to connect to the space navigator daemon" << std::endl;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
			return; // TODO: throw exception
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
			/**
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
			 * TODO: try also unix socket
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
			 * 
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
			 * 	if(spnav_open()==-1) {
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
			 * 	  	fprintf(stderr, "failed to connect to the space navigator daemon\n");
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
			 * 		return 1;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
			 * 	}
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
			 */
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		}
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		// For stopping this thread by pressing both buttons:
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		// bool pressed0 = false;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
		// bool pressed1 = false;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		while (spnav_wait_event(&event)) {
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
			if (event.type == SPNAV_EVENT_MOTION) {
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
				std::wcout << L"motion event: t(" << event.motion.x << L", " << event.motion.y << L", " << event.motion.z << L") ";
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
				std::wcout << L"r(" << event.motion.rx << L", " << event.motion.ry << L", " << event.motion.rz << L")" << std::endl;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
			} else { /* SPNAV_EVENT_BUTTON */
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
				std::wcout << L"button " << (event.button.press ? "press" : "release") << L" event b(" << event.button.bnum << L")" << std::endl;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
				// if (sev.button.bnum == 0) pressed0 = sev.button.press;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
				// if (sev.button.bnum == 1) pressed1 = sev.button.press;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
				// if (pressed0 && pressed1) break;
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
			}
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
			emit spacenavEvent(event);
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
		}
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
	}
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
signals:
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
	void spacenavEvent(spnav_event sev);
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
};