mingw build fixes (tested with i686-w64-mingw32 on linux-x86_64)
[ardour.git] / libs / surfaces / osc / osc.cc
index d02626f4af7e32d59917c65b1c128917d2cabcb6..8bc791a1ee5d762bc8115f8bf0e8941ebe6b3fba 100644 (file)
 #include <cerrno>
 #include <algorithm>
 
-#include <sys/poll.h>
 #include <unistd.h>
 #include <fcntl.h>
 
+#include <glib/gstdio.h>
 #include <glibmm/miscutils.h>
 
+#include <pbd/convert.h>
 #include <pbd/pthread_utils.h>
 #include <pbd/file_utils.h>
 #include <pbd/failed_constructor.h>
@@ -71,22 +72,20 @@ static void error_callback(int, const char *, const char *)
 #endif
 
 OSC::OSC (Session& s, uint32_t port)
-       : ControlProtocol (s, "OSC")
+       : ControlProtocol (s, X_("Open Sound Control (OSC)"))
        , AbstractUI<OSCUIRequest> ("osc")
+       , local_server (0)
+       , remote_server (0)
        , _port(port)
+       , _ok (true)
+       , _shutdown (false)
+       , _osc_server (0)
+       , _osc_unix_server (0)
+       , _namespace_root ("/ardour")
+       , _send_route_changes (true)
 {
        _instance = this;
-       _shutdown = false;
-       _osc_server = 0;
-       _osc_unix_server = 0;
-       _namespace_root = "/ardour";
-       _send_route_changes = true;
 
-       /* glibmm hack */
-       local_server = 0;
-       remote_server = 0;
-
-       // "Application Hooks"
        session_loaded (s);
        session->Exported.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::session_exported, this, _1, _2), this);
 }
@@ -113,11 +112,21 @@ OSC::do_request (OSCUIRequest* req)
 int
 OSC::set_active (bool yn)
 {
-       if (yn) {
-               return start ();
-       } else {
-               return stop ();
+       if (yn != active()) {
+
+               if (yn) {
+                       if (start ()) {
+                               return -1;
+                       }
+               } else {
+                       if (stop ()) {
+                               return -1;
+                       }
+               }
+               
        }
+
+       return ControlProtocol::set_active (yn);
 }
 
 bool
@@ -170,9 +179,6 @@ OSC::start ()
        if (!_osc_server) {
                return 1;
        }
-
-       int fd = lo_server_get_socket_fd (_osc_server);
-       fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
        
 #ifdef ARDOUR_OSC_UNIX_SERVER
        
@@ -181,18 +187,16 @@ OSC::start ()
        // attempt to create unix socket server too
        
        snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
-       fd = mkstemp(tmpstr);
+       int fd = mkstemp(tmpstr);
        
        if (fd >= 0 ) {
-               unlink (tmpstr);
+               ::g_unlink (tmpstr);
                close (fd);
                
                _osc_unix_server = lo_server_new (tmpstr, error_callback);
                
                if (_osc_unix_server) {
                        _osc_unix_socket_path = tmpstr;
-                       fd = lo_server_get_socket_fd (_osc_unix_server)
-                       fcntl(fdx, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
                }
        }
 #endif
@@ -232,23 +236,19 @@ OSC::thread_init ()
        pthread_set_name (X_("OSC"));
 
        if (_osc_unix_server) {
-               const int fd = lo_server_get_socket_fd (_osc_unix_server);
                Glib::RefPtr<IOSource> src = IOSource::create (lo_server_get_socket_fd (_osc_unix_server), IO_IN|IO_HUP|IO_ERR);
                src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_unix_server));
                src->attach (_main_loop->get_context());
                local_server = src->gobj();
                g_source_ref (local_server);
-               fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
        }
 
        if (_osc_server) {
-               const int fd = lo_server_get_socket_fd (_osc_server);
-               Glib::RefPtr<IOSource> src  = IOSource::create (fd, IO_IN|IO_HUP|IO_ERR);
+               Glib::RefPtr<IOSource> src  = IOSource::create (lo_server_get_socket_fd (_osc_server), IO_IN|IO_HUP|IO_ERR);
                src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_server));
                src->attach (_main_loop->get_context());
                remote_server = src->gobj();
                g_source_ref (remote_server);
-               fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
        }
 
        PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("OSC"), 2048);
@@ -293,11 +293,11 @@ OSC::stop ()
        }
        
        if (!_osc_unix_socket_path.empty()) {
-               unlink (_osc_unix_socket_path.c_str());
+               ::g_unlink (_osc_unix_socket_path.c_str());
        }
        
        if (!_osc_url_file.empty() ) {
-               unlink (_osc_url_file.c_str() );
+               ::g_unlink (_osc_url_file.c_str() );
        }
 
        // Delete any active route observers
@@ -1068,16 +1068,26 @@ OSC::route_plugin_parameter_print (int rid, int piid, int par)
 XMLNode& 
 OSC::get_state () 
 {
-       XMLNode* node = new XMLNode ("Protocol"); 
+       XMLNode& node (ControlProtocol::get_state());
 
-       node->add_property (X_("name"), "Open Sound Control (OSC)");
-       node->add_property (X_("feedback"), _send_route_changes ? "1" : "0");
-
-       return *node;
+       node.add_property (X_("feedback"), _send_route_changes ? "1" : "0");
+       return node;
 }
 
 int 
-OSC::set_state (const XMLNode&, int /*version*/)
+OSC::set_state (const XMLNode& node, int /*version*/)
 {
+       const XMLProperty* prop = node.property (X_("feedback"));
+
+       if (prop) {
+               if (PBD::string_is_affirmative (prop->value())) {
+                       _send_route_changes = true;
+               } else {
+                       _send_route_changes = false;
+               }
+       } else {
+               /* leave it alone */
+       }
+             
        return 0;
 }