src/SpacenavWrapper.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 07 Mar 2019 14:55:56 +0100
branchv_0
changeset 4 1383ad1c4f57
parent 3 src/SpacenavReceiver.h@6baa91ac3199
child 6 49560660d230
permissions -rw-r--r--
SpacenavWrapper

/**
 * 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() {
	// TODO: remove logging
	if (spnav_open() == 0) {
		std::wcout << L"connected through: AF_UNIX socket" << std::endl;
	} else {

		Display* display;
		Window window;
		unsigned long blackPixel;


		if (!(display = XOpenDisplay(0))) {
			std::wcout << L"unable to connect to the X server" << std::endl;
			return; // TODO: throw exception
		}

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

		if (spnav_x11_open(display, window) == 0) {
			std::wcout << L"connected through: X11" << std::endl;
		} else {
			std::wcout << L"unable to connect to the space navigator daemon" << std::endl;
			return; // TODO: throw exception
		}
	}
}

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

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

	if (spnav_wait_event(&event)) {

		// TODO: copy fields


		// TODO: remove logging
		if (event.type == SPNAV_EVENT_MOTION) {
			std::wcout << L"xxx motion event: t(" << event.motion.x << L", " << event.motion.y << L", " << event.motion.z << L") ";
			std::wcout << L"r(" << event.motion.rx << L", " << event.motion.ry << L", " << event.motion.rz << L")" << std::endl;
		} else { /* SPNAV_EVENT_BUTTON */
			std::wcout << L"xxx button " << (event.button.press ? "press" : "release") << L" event b(" << event.button.bnum << L")" << std::endl;
		}


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

}