# HG changeset patch # User František Kučera # Date 1552592374 -3600 # Node ID 980a27d138f7a805c5c975de0aa9ce65d0d4c6f4 # Parent a874deb6a536a1291c25dab8737729e438e5d6b4 publish events to all clients connected over unix domain socket diff -r a874deb6a536 -r 980a27d138f7 nbproject/configurations.xml --- a/nbproject/configurations.xml Thu Mar 14 17:41:43 2019 +0100 +++ b/nbproject/configurations.xml Thu Mar 14 20:39:34 2019 +0100 @@ -113,10 +113,12 @@ /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++ + /usr/include/x86_64-linux-gnu/qt5/QtNetwork QT_CORE_LIB QT_GUI_LIB + QT_NETWORK_LIB QT_WIDGETS_LIB @@ -146,9 +148,9 @@ - - - + + + diff -r a874deb6a536 -r 980a27d138f7 src/CMakeLists.txt --- a/src/CMakeLists.txt Thu Mar 14 17:41:43 2019 +0100 +++ b/src/CMakeLists.txt Thu Mar 14 20:39:34 2019 +0100 @@ -21,7 +21,7 @@ set(CMAKE_AUTOMOC ON) # Instruct CMake to run moc automatically when needed set(CMAKE_AUTOUIC ON) # Create code from a list of Qt designer ui files find_package(Qt5Widgets CONFIG REQUIRED) # Find the QtWidgets library -# find_package(Qt5Charts CONFIG REQUIRED) +find_package(Qt5Network CONFIG REQUIRED) # Add ASan AddressSanitizer set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address") @@ -41,7 +41,7 @@ # Link libraries: target_link_libraries(${EXECUTABLE_FILE} -lX11 -lm -lspnav) target_link_libraries(${EXECUTABLE_FILE} Qt5::Widgets) -# target_link_libraries(${EXECUTABLE_FILE} Qt5::Charts) +target_link_libraries(${EXECUTABLE_FILE} Qt5::Network) set_property(TARGET ${EXECUTABLE_FILE} PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE) install(TARGETS ${EXECUTABLE_FILE} DESTINATION bin) diff -r a874deb6a536 -r 980a27d138f7 src/SimulatorSocketServer.cpp --- a/src/SimulatorSocketServer.cpp Thu Mar 14 17:41:43 2019 +0100 +++ b/src/SimulatorSocketServer.cpp Thu Mar 14 20:39:34 2019 +0100 @@ -22,7 +22,28 @@ #include "ButtonEvent.h" #include "MotionEvent.h" +enum SocketEventType : int32_t { + Motion, + ButtonPress, + ButtonRelease +}; + SimulatorSocketServer::SimulatorSocketServer() { + QString path = "/tmp/spacenav-simulator.sock"; + + bool listnening = server->listen(path); + + if (listnening) { + std::wcout << L"Listening on: " << path.toStdWString() << std::endl; + + connect(server, &QLocalServer::newConnection, [path, this]() { + std::wcout << L"New connection on: " << path.toStdWString() << std::endl; + connections.push_back(server->nextPendingConnection()); + }); + + } else { + std::wcout << L"Unable to listen on: " << path.toStdWString() << std::endl; // TODO: throw exception + } } SimulatorSocketServer::SimulatorSocketServer(const SimulatorSocketServer& orig) { @@ -32,19 +53,36 @@ } void SimulatorSocketServer::publishButtonEvent(const ButtonEvent e) { - std::wcout << L"buttonEvent(" << e.number << L", " << (e.pressed ? L"pressed" : L"released") << L")" << std::endl; + std::array data = {0}; + data[0] = e.pressed ? SocketEventType::ButtonPress : SocketEventType::ButtonRelease; + data[1] = e.number; + publish(data); + + } void SimulatorSocketServer::publishMotionEvent(const MotionEvent e) { - std::wcout << L"motionEvent( m = [" - << e.x << L", " - << e.y << L", " - << e.z << L"], " - << L"r = [" - << e.rx << L", " - << e.ry << L", " - << e.rz << L"], " - << L"period = " << e.period - << L")" << std::endl; + std::array data = {0}; + int i = 0; + data[i++] = SocketEventType::Motion; + data[i++] = e.x; + data[i++] = e.y; + data[i++] = e.z; + data[i++] = e.rx; + data[i++] = e.ry; + data[i++] = e.rz; + data[i++] = e.period; + publish(data); + // TODO: custom sensitivity for each connection +} +void SimulatorSocketServer::publish(const std::array &data) { + for (QLocalSocket* socket : connections) { + if (socket->isValid()) { + socket->write((char*) &data, sizeof data); + socket->flush(); + } else { + // TODO: remove invalid socket + } + } } diff -r a874deb6a536 -r 980a27d138f7 src/SimulatorSocketServer.h --- a/src/SimulatorSocketServer.h Thu Mar 14 17:41:43 2019 +0100 +++ b/src/SimulatorSocketServer.h Thu Mar 14 20:39:34 2019 +0100 @@ -17,7 +17,13 @@ */ #pragma once +#include +#include +#include + #include +#include +#include #include "MotionEvent.h" #include "ButtonEvent.h" @@ -32,5 +38,7 @@ void publishMotionEvent(const MotionEvent e); void publishButtonEvent(const ButtonEvent e); private: - + QLocalServer* server = new QLocalServer(this); + std::vector connections; + void publish(const std::array &data); };