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)

/**
 * Spacenav Demo Qt
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <spnav.h>
#include "SpacenavWrapper.h"

SpacenavWrapper::SpacenavWrapper() {
	if (spnav_open() == 0) {
		connectionStatus = SpacenavWrapper::ConnectionStatus::DOMAIN_SOCKET;
		return;
	} else {
		Display* display;
		Window window;
		unsigned long blackPixel;

		if (display = XOpenDisplay(0)) {

			blackPixel = BlackPixel(display, DefaultScreen(display));
			window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, blackPixel, blackPixel);

			if (spnav_x11_open(display, window) == 0) {
				connectionStatus = SpacenavWrapper::ConnectionStatus::X11;
				return;
			}
		}
	}

	connectionStatus = SpacenavWrapper::ConnectionStatus::NONE;
}

SpacenavWrapper::~SpacenavWrapper() {
	// TODO: remove logging
	std::wcout << L"calling spnav_close() in ~SpacenavReceiver()" << std::endl;
	spnav_close();
}

SpacenavWrapper::ConnectionStatus SpacenavWrapper::getConnectionStatus() {
	return connectionStatus;
}

SpacenavWrapper::Event SpacenavWrapper::waitEvent() {
	SpacenavWrapper::Event e;
	spnav_event event;

	if (spnav_wait_event(&event)) {
		e.type = event.type == SPNAV_EVENT_MOTION ? SpacenavWrapper::Event::Type::MOTION : SpacenavWrapper::Event::Type::BUTTON;

		if (event.type == SPNAV_EVENT_MOTION) {
			e.motion.x = event.motion.x;
			e.motion.y = event.motion.y;
			e.motion.z = event.motion.z;
			e.motion.rx = event.motion.rx;
			e.motion.ry = event.motion.ry;
			e.motion.rz = event.motion.rz;
			e.motion.type = event.motion.type;
			e.motion.period = event.motion.period;
		} else { /* SPNAV_EVENT_BUTTON */
			e.button.number = event.button.bnum;
			e.button.pressed = event.button.press;
		}

		return e;
	} else {
		// TODO: throw exception
		return e;
	}

}