# HG changeset patch # User František Kučera # Date 1551966956 -3600 # Node ID 1383ad1c4f5780a7750d3de5198e202cc554dbf1 # Parent 6baa91ac319918d790c52b32be76b160831623fa SpacenavWrapper diff -r 6baa91ac3199 -r 1383ad1c4f57 CMakeLists.txt --- a/CMakeLists.txt Wed Mar 06 23:19:10 2019 +0100 +++ b/CMakeLists.txt Thu Mar 07 14:55:56 2019 +0100 @@ -1,5 +1,5 @@ # Spacenav Demo Qt -# Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) +# 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 diff -r 6baa91ac3199 -r 1383ad1c4f57 nbproject/configurations.xml --- a/nbproject/configurations.xml Wed Mar 06 23:19:10 2019 +0100 +++ b/nbproject/configurations.xml Thu Mar 07 14:55:56 2019 +0100 @@ -49,6 +49,13 @@ + + + + mocs_compilation.cpp + + + @@ -96,8 +103,9 @@ false - - + + @@ -124,6 +132,13 @@ ex="false" tool="1" flavor2="8"> + + + + @@ -131,7 +146,7 @@ ex="false" tool="1" flavor2="8"> - + @@ -141,6 +156,18 @@ + + + + build/Debug/src + src + build/Debug/src/xxx_autogen/include + + + xxx_EXPORTS + + + @@ -199,8 +226,31 @@ + + + + build/Debug/src + src + build/Debug/src/spacenav-demo-qt_autogen/include + /usr/include/x86_64-linux-gnu/qt5 + /usr/include/x86_64-linux-gnu/qt5/QtWidgets + /usr/include/x86_64-linux-gnu/qt5/QtGui + /usr/include/x86_64-linux-gnu/qt5/QtCore + /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ + + + QT_CORE_LIB + QT_GUI_LIB + QT_WIDGETS_LIB + + + + + + + - + @@ -272,10 +322,20 @@ + + + + + + + + + + diff -r 6baa91ac3199 -r 1383ad1c4f57 src/CMakeLists.txt --- a/src/CMakeLists.txt Wed Mar 06 23:19:10 2019 +0100 +++ b/src/CMakeLists.txt Thu Mar 07 14:55:56 2019 +0100 @@ -1,5 +1,5 @@ # Spacenav Demo Qt -# Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) +# 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 @@ -32,9 +32,9 @@ add_executable( ${EXECUTABLE_FILE} # QObjects must be listed here (including them from other files is not enough) + SpacenavWindow.h SpacenavReceiver.h - #SpacenavWindow.ui - #SpacenavWindow.cpp + SpacenavWrapper.cpp spacenav-demo-qt.cpp ) diff -r 6baa91ac3199 -r 1383ad1c4f57 src/SpacenavReceiver.h --- a/src/SpacenavReceiver.h Wed Mar 06 23:19:10 2019 +0100 +++ b/src/SpacenavReceiver.h Thu Mar 07 14:55:56 2019 +0100 @@ -1,6 +1,6 @@ /** * Spacenav Demo Qt - * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) + * 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 @@ -17,81 +17,30 @@ */ #pragma once -#include #include #include -#include -Q_DECLARE_METATYPE(spnav_event) +#include "SpacenavWrapper.h" + +Q_DECLARE_METATYPE(SpacenavWrapper::Event) class SpacenavReceiver : public QThread { + Q_OBJECT +private: + SpacenavWrapper spnav; - Q_OBJECT public: - SpacenavReceiver() : QThread() { } - virtual ~SpacenavReceiver() { - std::wcout << L"calling spnav_close() in ~SpacenavReceiver()" << std::endl; - spnav_close(); - } - void run() { - spnav_event event; - - // Connect - 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 - } + while (true) { + SpacenavWrapper::Event e = spnav.waitEvent(); + emit spacenavEvent(e); } - - // For stopping this thread by pressing both buttons: - bool pressed0 = false; - bool pressed1 = false; - - // Process events - while (spnav_wait_event(&event)) { - if (event.type == SPNAV_EVENT_MOTION) { - std::wcout << L"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"button " << (event.button.press ? "press" : "release") << L" event b(" << event.button.bnum << L")" << std::endl; - - if (event.button.bnum == 0) pressed0 = event.button.press; - if (event.button.bnum == 1) pressed1 = event.button.press; - if (pressed0 && pressed1) { - std::wcout << L"both buttons pressed → stop receiving events" << std::endl; - break; - } - } - emit spacenavEvent(event); - } - } signals: - void spacenavEvent(spnav_event sev); + void spacenavEvent(SpacenavWrapper::Event e); }; \ No newline at end of file diff -r 6baa91ac3199 -r 1383ad1c4f57 src/SpacenavWindow.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SpacenavWindow.h Thu Mar 07 14:55:56 2019 +0100 @@ -0,0 +1,32 @@ +/** + * 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 . + */ +#pragma once + +#include + +#include "SpacenavWrapper.h" + +class SpacenavWindow : public QMainWindow { + + Q_OBJECT +public: +public slots: + void spacenavEvent(SpacenavWrapper::Event e) { + setWindowTitle("event!"); + } +}; \ No newline at end of file diff -r 6baa91ac3199 -r 1383ad1c4f57 src/SpacenavWrapper.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SpacenavWrapper.cpp Thu Mar 07 14:55:56 2019 +0100 @@ -0,0 +1,80 @@ +/** + * 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 . + */ + +#include +#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; + } + +} diff -r 6baa91ac3199 -r 1383ad1c4f57 src/SpacenavWrapper.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/SpacenavWrapper.h Thu Mar 07 14:55:56 2019 +0100 @@ -0,0 +1,41 @@ +/** + * 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 . + */ +#pragma once + +#include + +/** + * This wrapper separates spnav.h and X headers from the rest of the application. + * Some definitions like Status would collide with Qt if mixed in a single file with QObjects and processed by MOC. + * + * This wrapper also abstracts from the connection type – tries AF_UNIX socket first and then X11. + */ +class SpacenavWrapper { +public: + + class Event { + }; + + SpacenavWrapper(); + + virtual ~SpacenavWrapper(); + + Event waitEvent(); + + +}; \ No newline at end of file diff -r 6baa91ac3199 -r 1383ad1c4f57 src/spacenav-demo-qt.cpp --- a/src/spacenav-demo-qt.cpp Wed Mar 06 23:19:10 2019 +0100 +++ b/src/spacenav-demo-qt.cpp Thu Mar 07 14:55:56 2019 +0100 @@ -1,6 +1,6 @@ /** * Spacenav Demo Qt - * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) + * 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 @@ -19,26 +19,22 @@ #include #include -#include - -#include -#include -#include -#include #include "SpacenavReceiver.h" -//#include "SpacenavWindow.h" +#include "SpacenavWindow.h" int main(int argc, char**argv) { setlocale(LC_ALL, ""); QApplication app(argc, argv); SpacenavReceiver t; - t.start(); + + SpacenavWindow w; + w.show(); - // QMainWindow w; - // w.show(); + QObject::connect(&t, &SpacenavReceiver::spacenavEvent, &w, &SpacenavWindow::spacenavEvent, Qt::ConnectionType::QueuedConnection); + t.start(); int qtResultCode = app.exec(); if (t.isRunning()) {