src/SpacenavWrapper.cpp
branchv_0
changeset 4 1383ad1c4f57
parent 3 6baa91ac3199
child 6 49560660d230
equal deleted inserted replaced
3:6baa91ac3199 4:1383ad1c4f57
       
     1 /**
       
     2  * Spacenav Demo Qt
       
     3  * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17  */
       
    18 
       
    19 #include <spnav.h>
       
    20 #include "SpacenavWrapper.h"
       
    21 
       
    22 SpacenavWrapper::SpacenavWrapper() {
       
    23 	// TODO: remove logging
       
    24 	if (spnav_open() == 0) {
       
    25 		std::wcout << L"connected through: AF_UNIX socket" << std::endl;
       
    26 	} else {
       
    27 
       
    28 		Display* display;
       
    29 		Window window;
       
    30 		unsigned long blackPixel;
       
    31 
       
    32 
       
    33 		if (!(display = XOpenDisplay(0))) {
       
    34 			std::wcout << L"unable to connect to the X server" << std::endl;
       
    35 			return; // TODO: throw exception
       
    36 		}
       
    37 
       
    38 		blackPixel = BlackPixel(display, DefaultScreen(display));
       
    39 		window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, blackPixel, blackPixel);
       
    40 
       
    41 		if (spnav_x11_open(display, window) == 0) {
       
    42 			std::wcout << L"connected through: X11" << std::endl;
       
    43 		} else {
       
    44 			std::wcout << L"unable to connect to the space navigator daemon" << std::endl;
       
    45 			return; // TODO: throw exception
       
    46 		}
       
    47 	}
       
    48 }
       
    49 
       
    50 SpacenavWrapper::~SpacenavWrapper() {
       
    51 	// TODO: remove logging
       
    52 	std::wcout << L"calling spnav_close() in ~SpacenavReceiver()" << std::endl;
       
    53 	spnav_close();
       
    54 }
       
    55 
       
    56 SpacenavWrapper::Event SpacenavWrapper::waitEvent() {
       
    57 	SpacenavWrapper::Event e;
       
    58 	spnav_event event;
       
    59 
       
    60 	if (spnav_wait_event(&event)) {
       
    61 
       
    62 		// TODO: copy fields
       
    63 
       
    64 
       
    65 		// TODO: remove logging
       
    66 		if (event.type == SPNAV_EVENT_MOTION) {
       
    67 			std::wcout << L"xxx motion event: t(" << event.motion.x << L", " << event.motion.y << L", " << event.motion.z << L") ";
       
    68 			std::wcout << L"r(" << event.motion.rx << L", " << event.motion.ry << L", " << event.motion.rz << L")" << std::endl;
       
    69 		} else { /* SPNAV_EVENT_BUTTON */
       
    70 			std::wcout << L"xxx button " << (event.button.press ? "press" : "release") << L" event b(" << event.button.bnum << L")" << std::endl;
       
    71 		}
       
    72 
       
    73 
       
    74 		return e;
       
    75 	} else {
       
    76 		// TODO: throw exception
       
    77 		return e;
       
    78 	}
       
    79 
       
    80 }