src/SpacenavReceiver.h
author Františ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
permissions -rw-r--r--
support both: AF_UNIX and X11 connections
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() {
2
21b0b2b0547e both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    37
		std::wcout << L"calling spnav_close() in ~SpacenavReceiver()" << std::endl;
1
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		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
    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
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	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
    42
		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
    43
3
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    44
		// Connect
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    45
		if (spnav_open() == 0) {
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    46
			std::wcout << L"connected through: AF_UNIX socket" << std::endl;
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    47
		} else {
1
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
3
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    49
			Display* display;
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    50
			Window window;
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    51
			unsigned long blackPixel;
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    52
1
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
3
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    54
			if (!(display = XOpenDisplay(0))) {
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    55
				std::wcout << L"unable to connect to the X server" << std::endl;
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    56
				return; // TODO: throw exception
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    57
			}
1
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
3
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    59
			blackPixel = BlackPixel(display, DefaultScreen(display));
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    60
			window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, blackPixel, blackPixel);
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    61
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    62
			if (spnav_x11_open(display, window) == 0) {
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    63
				std::wcout << L"connected through: X11" << std::endl;
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    64
			} else {
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    65
				std::wcout << L"unable to connect to the space navigator daemon" << std::endl;
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    66
				return; // TODO: throw exception
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    67
			}
1
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
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		// For stopping this thread by pressing both buttons:
2
21b0b2b0547e both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    71
		bool pressed0 = false;
21b0b2b0547e both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    72
		bool pressed1 = false;
1
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
3
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    74
		// Process events
1
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		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
    76
			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
    77
				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
    78
				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
    79
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
			} 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
    81
				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
    82
2
21b0b2b0547e both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    83
				if (event.button.bnum == 0) pressed0 = event.button.press;
21b0b2b0547e both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    84
				if (event.button.bnum == 1) pressed1 = event.button.press;
21b0b2b0547e both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    85
				if (pressed0 && pressed1) {
21b0b2b0547e both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    86
					std::wcout << L"both buttons pressed → stop receiving events" << std::endl;
21b0b2b0547e both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    87
					break;
21b0b2b0547e both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    88
				}
1
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
			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
    91
		}
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
	}
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
signals:
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
	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
    97
};