mingw build fixes (tested with i686-w64-mingw32 on linux-x86_64)
[ardour.git] / libs / surfaces / osc / osc.cc
index a531f6e41a0662290a35f2c52baed960038628e5..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/filesystem.h>
 #include <pbd/failed_constructor.h>
 
+#include "ardour/amp.h"
 #include "ardour/session.h"
 #include "ardour/route.h"
 #include "ardour/audio_track.h"
@@ -43,6 +44,7 @@
 #include "ardour/filesystem_paths.h"
 #include "ardour/panner.h"
 #include "ardour/plugin.h"
+#include "ardour/send.h"
 
 #include "osc.h"
 #include "osc_controllable.h"
@@ -70,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);
 }
@@ -112,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
@@ -165,6 +175,10 @@ OSC::start ()
                _port++;
                continue;
        }
+
+       if (!_osc_server) {
+               return 1;
+       }
        
 #ifdef ARDOUR_OSC_UNIX_SERVER
        
@@ -176,7 +190,7 @@ OSC::start ()
        int fd = mkstemp(tmpstr);
        
        if (fd >= 0 ) {
-               unlink (tmpstr);
+               ::g_unlink (tmpstr);
                close (fd);
                
                _osc_unix_server = lo_server_new (tmpstr, error_callback);
@@ -189,11 +203,11 @@ OSC::start ()
        
        PBD::info << "OSC @ " << get_server_url () << endmsg;
 
-       PBD::sys::path url_file;
+       std::string url_file;
 
        if (find_file_in_search_path (ardour_config_search_path(), "osc_url", url_file)) {
                
-               _osc_url_file = url_file.to_string();
+               _osc_url_file = url_file;
                ofstream urlfile;
                urlfile.open(_osc_url_file.c_str(), ios::trunc);
                
@@ -279,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
@@ -357,8 +371,8 @@ OSC::register_callbacks()
                REGISTER_CALLBACK (serv, "/ardour/routes/pan_stereo_width", "if", route_set_pan_stereo_width);
                REGISTER_CALLBACK (serv, "/ardour/routes/plugin/parameter", "iiif", route_plugin_parameter);
                REGISTER_CALLBACK (serv, "/ardour/routes/plugin/parameter/print", "iii", route_plugin_parameter_print);
-
-               
+               REGISTER_CALLBACK (serv, "/ardour/routes/send/gainabs", "iif", route_set_send_gain_abs);
+               REGISTER_CALLBACK (serv, "/ardour/routes/send/gaindB", "iif", route_set_send_gain_dB);
 
                /* still not-really-standardized query interface */
                //REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
@@ -892,6 +906,70 @@ OSC::route_set_pan_stereo_width (int rid, float pos)
 
 }
 
+int
+OSC::route_set_send_gain_abs (int rid, int sid, float val)
+{
+        if (!session) { 
+                return -1;
+        }
+
+        boost::shared_ptr<Route> r = session->route_by_remote_id (rid);  
+
+        if (!r) {
+                return -1;
+        }
+
+       /* revert to zero-based counting */
+
+       if (sid > 0) {
+               --sid;
+       }
+
+       boost::shared_ptr<Processor> p = r->nth_send (sid);
+       
+       if (p) {
+               boost::shared_ptr<Send> s = boost::dynamic_pointer_cast<Send>(p);
+               boost::shared_ptr<Amp> a = s->amp();
+               
+               if (a) {
+                       a->set_gain (val, this);
+               }
+       }
+       return 0;
+}
+
+int
+OSC::route_set_send_gain_dB (int rid, int sid, float val)
+{
+        if (!session) { 
+                return -1;
+        }
+
+        boost::shared_ptr<Route> r = session->route_by_remote_id (rid);  
+
+        if (!r) {
+                return -1;
+        }
+
+       /* revert to zero-based counting */
+
+       if (sid > 0) {
+               --sid;
+       }
+
+       boost::shared_ptr<Processor> p = r->nth_send (sid);
+       
+       if (p) {
+               boost::shared_ptr<Send> s = boost::dynamic_pointer_cast<Send>(p);
+               boost::shared_ptr<Amp> a = s->amp();
+               
+               if (a) {
+                       a->set_gain (dB_to_coefficient (val), this);
+               }
+       }
+       return 0;
+}
+
 int
 OSC::route_plugin_parameter (int rid, int piid, int par, float val)
 {
@@ -990,11 +1068,26 @@ OSC::route_plugin_parameter_print (int rid, int piid, int par)
 XMLNode& 
 OSC::get_state () 
 {
-       return *(new XMLNode ("OSC"));
+       XMLNode& node (ControlProtocol::get_state());
+
+       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;
 }