src/SpacenavWrapper.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 07 Mar 2019 18:33:22 +0100
branchv_0
changeset 9 d3716f03efcd
parent 6 49560660d230
child 11 aaa89fe98b63
permissions -rw-r--r--
display status – connection type (domain socket or X11)
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
4
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
1
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
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <spnav.h>
4
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    20
#include "SpacenavWrapper.h"
1
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
4
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    22
SpacenavWrapper::SpacenavWrapper() {
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    23
	if (spnav_open() == 0) {
9
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    24
		connectionStatus = SpacenavWrapper::ConnectionStatus::DOMAIN_SOCKET;
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    25
		return;
4
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    26
	} else {
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    27
		Display* display;
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    28
		Window window;
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    29
		unsigned long blackPixel;
3
6baa91ac3199 support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    30
9
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    31
		if (display = XOpenDisplay(0)) {
1
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
9
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    33
			blackPixel = BlackPixel(display, DefaultScreen(display));
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    34
			window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, blackPixel, blackPixel);
1
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
9
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    36
			if (spnav_x11_open(display, window) == 0) {
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    37
				connectionStatus = SpacenavWrapper::ConnectionStatus::X11;
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    38
				return;
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    39
			}
4
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    40
		}
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    41
	}
9
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    42
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    43
	connectionStatus = SpacenavWrapper::ConnectionStatus::NONE;
4
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    44
}
1
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
4
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    46
SpacenavWrapper::~SpacenavWrapper() {
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    47
	// TODO: remove logging
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    48
	std::wcout << L"calling spnav_close() in ~SpacenavReceiver()" << std::endl;
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    49
	spnav_close();
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    50
}
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    51
9
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    52
SpacenavWrapper::ConnectionStatus SpacenavWrapper::getConnectionStatus() {
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    53
	return connectionStatus;
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    54
}
d3716f03efcd display status – connection type (domain socket or X11)
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    55
4
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    56
SpacenavWrapper::Event SpacenavWrapper::waitEvent() {
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    57
	SpacenavWrapper::Event e;
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    58
	spnav_event event;
1
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
4
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    60
	if (spnav_wait_event(&event)) {
6
49560660d230 emit MotionEvent and ButtonEvent + display button status
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    61
		e.type = event.type == SPNAV_EVENT_MOTION ? SpacenavWrapper::Event::Type::MOTION : SpacenavWrapper::Event::Type::BUTTON;
4
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    62
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    63
		if (event.type == SPNAV_EVENT_MOTION) {
6
49560660d230 emit MotionEvent and ButtonEvent + display button status
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    64
			e.motion.x = event.motion.x;
49560660d230 emit MotionEvent and ButtonEvent + display button status
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    65
			e.motion.y = event.motion.y;
49560660d230 emit MotionEvent and ButtonEvent + display button status
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    66
			e.motion.z = event.motion.z;
49560660d230 emit MotionEvent and ButtonEvent + display button status
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    67
			e.motion.rx = event.motion.rx;
49560660d230 emit MotionEvent and ButtonEvent + display button status
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    68
			e.motion.ry = event.motion.ry;
49560660d230 emit MotionEvent and ButtonEvent + display button status
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    69
			e.motion.rz = event.motion.rz;
49560660d230 emit MotionEvent and ButtonEvent + display button status
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    70
			e.motion.type = event.motion.type;
49560660d230 emit MotionEvent and ButtonEvent + display button status
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    71
			e.motion.period = event.motion.period;
4
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    72
		} else { /* SPNAV_EVENT_BUTTON */
6
49560660d230 emit MotionEvent and ButtonEvent + display button status
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    73
			e.button.number = event.button.bnum;
49560660d230 emit MotionEvent and ButtonEvent + display button status
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    74
			e.button.pressed = event.button.press;
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
		}
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
4
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    77
		return e;
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    78
	} else {
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    79
		// TODO: throw exception
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    80
		return e;
1
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
	}
2a3e9f07c128 receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
4
1383ad1c4f57 SpacenavWrapper
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    83
}