From d11bc934965707c7d69ffd80c43de698687433d2 Mon Sep 17 00:00:00 2001 From: Alexandre Quessy Date: Fri, 14 Mar 2014 11:51:52 -0400 Subject: [PATCH] re-implement and fix osc receiver --- MainWindow.cpp | 5 ++++- OscInterface.cpp | 19 +++++++++++++----- OscInterface.h | 8 ++++---- OscReceiver.cpp | 30 ++++++++++++++++++++++------- OscReceiver.h | 1 + mapmap.pro | 50 +++++++++++++++++++++++++++++++++--------------- 6 files changed, 81 insertions(+), 32 deletions(-) diff --git a/MainWindow.cpp b/MainWindow.cpp index cbed161..fe1e476 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -68,6 +68,9 @@ MainWindow::~MainWindow() { delete mappingManager; // delete _facade; +#ifdef HAVE_OSC + delete osc_timer; +#endif // ifdef } void MainWindow::handlePaintItemSelectionChanged() @@ -1219,7 +1222,7 @@ void MainWindow::startOscReceiver() void MainWindow::pollOscInterface() { #ifdef HAVE_OSC - osc_interface->consume_commands(*_facade); + osc_interface->consume_commands(*this); #endif } diff --git a/OscInterface.cpp b/OscInterface.cpp index 67ecd43..db66cd1 100644 --- a/OscInterface.cpp +++ b/OscInterface.cpp @@ -21,7 +21,8 @@ */ #include "OscInterface.h" -#include "Facade.h" +#include "MainWindow.h" +#include "unused.h" #include OscInterface::OscInterface( @@ -43,6 +44,11 @@ OscInterface::OscInterface( } } +OscInterface::~OscInterface() +{ + // pass +} + /** * Handles /pong. Does nothing. */ @@ -87,7 +93,7 @@ void OscInterface::push_command(QVariantList command) * * Should be called when it's time to take action, before rendering a frame, for example. */ -void OscInterface::consume_commands(Facade &facade) +void OscInterface::consume_commands(MainWindow &main_window) { bool success = true; while (success) @@ -99,7 +105,7 @@ void OscInterface::consume_commands(Facade &facade) //if (is_verbose()) // std::cout << __FUNCTION__ << ": apply " << // command.first().toString().toStdString() << std::endl; - this->applyOscCommand(facade, command); + this->applyOscCommand(main_window, command); } } } @@ -183,8 +189,9 @@ static void printCommand(QVariantList &command) std::cout.flush(); } -void OscInterface::applyOscCommand(Facade &facade, QVariantList & command) +void OscInterface::applyOscCommand(MainWindow &main_window, QVariantList & command) { + UNUSED(main_window); bool VERBOSE = true; if (VERBOSE) @@ -209,7 +216,8 @@ void OscInterface::applyOscCommand(Facade &facade, QVariantList & command) std::string paint_id = command.at(2).toString().toStdString(); std::string image_uri = command.at(3).toString().toStdString(); //std::cout << "TODO load /image/uri " << image_id << " " << image_uri << std::endl; - facade.updateImagePaintUri(paint_id.c_str(), image_uri.c_str()); + // TODO: + //main_window.updateImagePaintUri(paint_id.c_str(), image_uri.c_str()); } //else if (path == "/add/quad") @@ -221,3 +229,4 @@ void OscInterface::applyOscCommand(Facade &facade, QVariantList & command) //else if (path == "/project/open") // open(); } + diff --git a/OscInterface.h b/OscInterface.h index f6616a0..27e5af3 100644 --- a/OscInterface.h +++ b/OscInterface.h @@ -28,7 +28,7 @@ #include "OscReceiver.h" #include -class Facade; // forward decl +class MainWindow; // forward decl /** * Open Sound Control sending and receiving for MapMap. @@ -40,7 +40,7 @@ class OscInterface OscInterface( //MainWindow* owner, const std::string &listen_port); - ~OscInterface() {} + ~OscInterface(); void start(); /** * Call this method from the main thread. @@ -48,7 +48,7 @@ class OscInterface * Each message is stored as a QVariantList. * [args] */ - void consume_commands(Facade &facade); + void consume_commands(MainWindow &main_window); private: bool is_verbose() { return false; } void push_command(QVariantList command); @@ -72,7 +72,7 @@ class OscInterface /* * In the main thread, handles the messages. */ - void applyOscCommand(Facade &facade, QVariantList & command); + void applyOscCommand(MainWindow &main_window, QVariantList & command); }; diff --git a/OscReceiver.cpp b/OscReceiver.cpp index 7f19119..a739d91 100644 --- a/OscReceiver.cpp +++ b/OscReceiver.cpp @@ -3,9 +3,13 @@ #include OscReceiver::OscReceiver(const std::string &port) : - port_(port), + port_(port), server_(lo_server_thread_new(port_.c_str(), error)) { + if (server_ == NULL) + server_is_ok_ = false; + else + server_is_ok_ = true; // #ifdef CONFIG_DEBUG // /* add method that will match any path and args */ // lo_server_thread_add_method(server_, NULL, NULL, genericHandler, this); @@ -20,20 +24,31 @@ OscReceiver::~OscReceiver() void OscReceiver::addHandler(const char *path, const char *types, lo_method_handler handler, void *userData) { - lo_server_thread_add_method(server_, path, types, handler, userData); + if (server_is_ok_) + lo_server_thread_add_method(server_, path, types, handler, userData); + else + std::cout << "Could not add OSC handler " << path << std::endl; } void OscReceiver::listen() { - int lo_fd = lo_server_get_socket_fd(server_); - if (lo_fd == 0) + if (server_is_ok_) { - std::cout << "OSC port " << port_ << " is already in use." << std::endl; + int lo_fd = lo_server_get_socket_fd(server_); + if (lo_fd == 0) + { + std::cout << "OSC port " << port_ << " is already in use." << std::endl; + } + else + { + std::cout << "Listening on port " << port_ << std::endl; + lo_server_thread_start(server_); + } } else { - std::cout << "Listening on port " << port_ << std::endl; - lo_server_thread_start(server_); + std::cout << "Could not start OSC receiver. Maybe that "; + std::cout << "OSC port " << port_ << " is already in use." << std::endl; } } @@ -69,3 +84,4 @@ std::string OscReceiver::toString() const { return "port:" + port_; } + diff --git a/OscReceiver.h b/OscReceiver.h index fc28720..cf1b7f9 100644 --- a/OscReceiver.h +++ b/OscReceiver.h @@ -18,6 +18,7 @@ class OscReceiver { void *user_data); private: std::string toString() const; + bool server_is_ok_; std::string port_; lo_server_thread server_; #ifdef CONFIG_DEBUG diff --git a/mapmap.pro b/mapmap.pro index 86d79c9..369e13e 100644 --- a/mapmap.pro +++ b/mapmap.pro @@ -2,7 +2,6 @@ CONFIG += qt debug TEMPLATE = app HEADERS = \ DestinationGLCanvas.h \ -# Facade.h \ MainApplication.h \ MainWindow.h \ Mapper.h \ @@ -10,8 +9,9 @@ HEADERS = \ Mapping.h \ MappingManager.h \ Math.h \ -# NameAllocator.h \ Paint.h \ + OscInterface.h \ + OscReceiver.h \ ProjectReader.h \ ProjectWriter.h \ Shape.h \ @@ -21,16 +21,16 @@ HEADERS = \ unused.h \ SOURCES = \ -# Controller.cpp \ DestinationGLCanvas.cpp \ -# Facade.cpp \ MainWindow.cpp \ MainApplication.cpp \ Mapper.cpp \ MapperGLCanvas.cpp \ Mapping.cpp \ MappingManager.cpp \ -# NameAllocator.cpp \ + OscInterface.cpp \ + OscReceiver.cpp \ + ProjectReader.h \ Paint.cpp \ ProjectReader.cpp \ ProjectWriter.cpp \ @@ -40,22 +40,42 @@ SOURCES = \ Util.cpp \ main.cpp -QT += gui opengl xml +QT += gui opengl xml RESOURCES = mapmap.qrc - TRANSLATIONS = mapmap_fr.ts - include(contrib/qtpropertybrowser/src/qtpropertybrowser.pri) - docs.depends = $(HEADERS) $(SOURCES) docs.commands = (cat Doxyfile; echo "INPUT = $?") | doxygen - QMAKE_EXTRA_TARGETS += docs -# mac -macx:LIBS += -framework OpenGL -framework GLUT -macx:QMAKE_CXXFLAGS += -D__MACOSX_CORE__ +# Linux-specific: +unix:!mac { + DEFINES += UNIX + # stricter build flags: + QMAKE_CXXFLAGS += -Wno-unused-result -Wfatal-errors + QMAKE_CXXFLAGS += -DHAVE_OSC + INCLUDEPATH += /usr/include/gstreamer-0.10 \ + /usr/local/include/gstreamer-0.10 \ + /usr/include/glib-2.0 \ + /usr/lib/x86_64-linux-gnu/glib-2.0/include \ + /usr/include/libxml2 + LIBS += \ + -lglut \ + -lGLU + -llo -lpthread \ + -lX11 \ + -lGLEW +} -# not mac -!macx:LIBS += -lglut -lGLU -!macx:QMAKE_CXXFLAGS += -Wno-unused-result -Wfatal-errors +# Mac OS X-specific: +mac { + DEFINES += MACOSX + INCLUDEPATH += /opt/local/include/ \ + /opt/local/include/libxml2 + LIBS += -L/opt/local/lib \ + -lGLEW + -framework OpenGL \ + -framework GLUT + QMAKE_CXXFLAGS += -D__MACOSX_CORE__ +}