shared_ptr fixes
[ardour.git] / libs / surfaces / tranzport / tranzport_control_protocol.cc
index 30ca270593ba8ee8c49f31d1ce026b1051ec2af5..426c837b2ffc6e969f2ad490c7c6ef3b882af17f 100644 (file)
@@ -22,6 +22,8 @@
 #include <algorithm>
 #include <cmath>
 
+#define __STDC_FORMAT_MACROS
+#include <inttypes.h>
 #include <float.h>
 #include <sys/time.h>
 #include <errno.h>
@@ -39,6 +41,7 @@
 using namespace ARDOUR;
 using namespace std;
 using namespace sigc;
+using namespace PBD;
 
 #include "i18n.h"
 
@@ -66,16 +69,17 @@ slider_position_to_gain (double pos)
 
 
 TranzportControlProtocol::TranzportControlProtocol (Session& s)
-       : ControlProtocol  (s, X_("Tranzport")),
-         AbstractUI<TranzportRequest> (X_("Tranzport"), false)
-       
+       : ControlProtocol  (s, X_("Tranzport"))
 {
+       /* tranzport controls one track at a time */
+
+       set_route_table_size (1);
+       
        timeout = 60000;
        buttonmask = 0;
        _datawheel = 0;
        _device_status = STATUS_OFFLINE;
        udev = 0;
-       current_route = 0;
        current_track_id = 0;
        last_where = max_frames;
        wheel_mode = WheelTimeline;
@@ -103,6 +107,28 @@ TranzportControlProtocol::~TranzportControlProtocol ()
        set_active (false);
 }
 
+bool
+TranzportControlProtocol::probe ()
+{
+       struct usb_bus *bus;
+       struct usb_device *dev;
+
+       usb_init();
+       usb_find_busses();
+       usb_find_devices();
+
+       for (bus = usb_busses; bus; bus = bus->next) {
+
+               for(dev = bus->devices; dev; dev = dev->next) {
+                       if (dev->descriptor.idVendor == VENDORID && dev->descriptor.idProduct == PRODUCTID) {
+                               return true; 
+                       }
+               }
+       }
+
+       return false;
+}
+
 int
 TranzportControlProtocol::set_active (bool yn)
 {
@@ -139,11 +165,11 @@ TranzportControlProtocol::set_active (bool yn)
 void
 TranzportControlProtocol::show_track_gain ()
 {
-       if (current_route) {
-               gain_t g = current_route->gain();
+       if (route_table[0]) {
+               gain_t g = route_get_gain (0);
                if (g != last_track_gain) {
                        char buf[16];
-                       snprintf (buf, sizeof (buf), "%6.1fdB", coefficient_to_dB (current_route->effective_gain()));
+                       snprintf (buf, sizeof (buf), "%6.1fdB", coefficient_to_dB (route_get_effective_gain (0)));
                        print (0, 9, buf); 
                        last_track_gain = g;
                }
@@ -234,11 +260,11 @@ log_meter (float db)
 void
 TranzportControlProtocol::show_meter ()
 {
-       if (current_route == 0) {
+       if (route_table[0] == 0) {
                return;
        }
 
-       float level = current_route->peak_input_power (0);
+       float level = route_get_peak_input_power (0, 0);
        float fraction = log_meter (level);
 
        /* we draw using a choice of a sort of double colon-like character ("::") or a single, left-aligned ":".
@@ -294,29 +320,29 @@ TranzportControlProtocol::show_meter ()
 void
 TranzportControlProtocol::show_transport_time ()
 {
-       jack_nframes_t where = session.transport_frame();
+       jack_nframes_t where = session->transport_frame();
        
        if (where != last_where) {
 
                char buf[5];
-               SMPTE_Time smpte;
+               SMPTE::Time smpte;
 
-               session.smpte_time (where, smpte);
+               session->smpte_time (where, smpte);
                
                if (smpte.negative) {
-                       sprintf (buf, "-%02ld:", smpte.hours);
+                       sprintf (buf, "-%02" PRIu32 ":", smpte.hours);
                } else {
-                       sprintf (buf, " %02ld:", smpte.hours);
+                       sprintf (buf, " %02" PRIu32 ":", smpte.hours);
                }
                print (1, 8, buf);
 
-               sprintf (buf, "%02ld:", smpte.minutes);
+               sprintf (buf, "%02" PRIu32 ":", smpte.minutes);
                print (1, 12, buf);
 
-               sprintf (buf, "%02ld:", smpte.seconds);
+               sprintf (buf, "%02" PRIu32 ":", smpte.seconds);
                print (1, 15, buf);
 
-               sprintf (buf, "%02ld", smpte.frames);
+               sprintf (buf, "%02" PRIu32, smpte.frames);
                print (1, 18, buf);
 
                last_where = where;
@@ -370,10 +396,7 @@ TranzportControlProtocol::open_core (struct usb_device* dev)
        }
 
        if (usb_set_configuration (udev, 1) < 0) {
-               error << _("Tranzport: cannot configure USB interface") << endmsg;
-               usb_close (udev);
-               udev = 0;
-               return -1;
+               cerr << _("Tranzport: cannot configure USB interface") << endmsg;
        }
 
        return 0;
@@ -556,7 +579,7 @@ TranzportControlProtocol::monitor_work ()
        
        if ((err = pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam)) != 0) {
                // do we care? not particularly.
-               info << string_compose (_("%1: thread not running with realtime scheduling (%2)"), BaseUI::name(), strerror (errno)) << endmsg;
+               PBD::info << string_compose (_("%1: thread not running with realtime scheduling (%2)"), name(), strerror (errno)) << endmsg;
        } 
 
        pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
@@ -579,10 +602,6 @@ TranzportControlProtocol::monitor_work ()
                val = usb_interrupt_read (udev, READ_ENDPOINT, (char*) buf, 8, 10);
                pthread_testcancel();
 
-               /* any requests to handle? */
-
-               handle_ui_requests ();
-
                if (val == 8) {
                        process (buf);
                }
@@ -673,19 +692,19 @@ TranzportControlProtocol::update_state ()
 
        /* per track */
 
-       if (current_route) {
-               AudioTrack* at = dynamic_cast<AudioTrack*> (current_route);
+       if (route_table[0]) {
+               boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack> (route_table[0]);
                if (at && at->record_enabled()) {
                        pending_lights[LightTrackrec] = true;
                } else {
                        pending_lights[LightTrackrec] = false;
                }
-               if (current_route->muted()) {
+               if (route_get_muted (0)) {
                        pending_lights[LightTrackmute] = true;
                } else {
                        pending_lights[LightTrackmute] = false;
                }
-               if (current_route->soloed()) {
+               if (route_get_soloed (0)) {
                        pending_lights[LightTracksolo] = true;
                } else {
                        pending_lights[LightTracksolo] = false;
@@ -699,25 +718,25 @@ TranzportControlProtocol::update_state ()
 
        /* global */
 
-       if (session.get_auto_loop()) {
+       if (session->get_auto_loop()) {
                pending_lights[LightLoop] = true;
        } else {
                pending_lights[LightLoop] = false;
        }
 
-       if (session.get_punch_in() || session.get_punch_out()) {
+       if (session->get_punch_in() || session->get_punch_out()) {
                pending_lights[LightPunch] = true;
        } else {
                pending_lights[LightPunch] = false;
        }
 
-       if (session.get_record_enabled()) {
+       if (session->get_record_enabled()) {
                pending_lights[LightRecord] = true;
        } else {
                pending_lights[LightRecord] = false;
        }
 
-       if (session.soloing ()) {
+       if (session->soloing ()) {
                pending_lights[LightAnysolo] = true;
        } else {
                pending_lights[LightAnysolo] = false;
@@ -954,10 +973,10 @@ TranzportControlProtocol::process (uint8_t* buf)
 void
 TranzportControlProtocol::show_current_track ()
 {
-       if (current_route == 0) {
+       if (route_table[0] == 0) {
                print (0, 0, "--------");
        } else {
-               print (0, 0, current_route->name().substr (0, 8).c_str());
+               print (0, 0, route_get_name (0).substr (0, 8).c_str());
        }
 }
 
@@ -1009,10 +1028,7 @@ TranzportControlProtocol::button_event_trackrec_press (bool shifted)
        if (shifted) {
                toggle_all_rec_enables ();
        } else {
-               if (current_route) {
-                       AudioTrack* at = dynamic_cast<AudioTrack*>(current_route);
-                       at->set_record_enable (!at->record_enabled(), this);
-               }
+               route_set_rec_enable (0, !route_get_rec_enable (0));
        }
 }
 
@@ -1024,9 +1040,7 @@ TranzportControlProtocol::button_event_trackrec_release (bool shifted)
 void
 TranzportControlProtocol::button_event_trackmute_press (bool shifted)
 {
-       if (current_route) {
-               current_route->set_mute (!current_route->muted(), this);
-       }
+       route_set_muted (0, !route_get_muted (0));
 }
 
 void
@@ -1043,11 +1057,9 @@ TranzportControlProtocol::button_event_tracksolo_press (bool shifted)
        }
 
        if (shifted) {
-               session.set_all_solo (!session.soloing());
+               session->set_all_solo (!session->soloing());
        } else {
-               if (current_route) {
-                       current_route->set_solo (!current_route->soloed(), this);
-               }
+               route_set_soloed (0, !route_get_soloed (0));
        }
 }
 
@@ -1267,7 +1279,7 @@ TranzportControlProtocol::datawheel ()
 
                /* parameter control */
 
-               if (current_route) {
+               if (route_table[0]) {
                        switch (wheel_shift_mode) {
                        case WheelShiftGain:
                                if (_datawheel < WheelDirectionThreshold) {
@@ -1358,23 +1370,23 @@ TranzportControlProtocol::scrub ()
        last_wheel_motion = now;
        last_wheel_dir = dir;
        
-       move_at (speed * dir);
+       set_transport_speed (speed * dir);
 }
 
 void
 TranzportControlProtocol::shuttle ()
 {
        if (_datawheel < WheelDirectionThreshold) {
-               if (session.transport_speed() < 0) {
-                       session.request_transport_speed (1.0);
+               if (session->transport_speed() < 0) {
+                       session->request_transport_speed (1.0);
                } else {
-                       session.request_transport_speed (session.transport_speed() + 0.1);
+                       session->request_transport_speed (session->transport_speed() + 0.1);
                }
        } else {
-               if (session.transport_speed() > 0) {
-                       session.request_transport_speed (-1.0);
+               if (session->transport_speed() > 0) {
+                       session->request_transport_speed (-1.0);
                } else {
-                       session.request_transport_speed (session.transport_speed() - 0.1);
+                       session->request_transport_speed (session->transport_speed() - 0.1);
                }
        }
 }
@@ -1392,7 +1404,7 @@ TranzportControlProtocol::step_gain_up ()
                gain_fraction = 2.0;
        }
        
-       current_route->set_gain (slider_position_to_gain (gain_fraction), this);
+       route_set_gain (0, slider_position_to_gain (gain_fraction));
 }
 
 void
@@ -1408,7 +1420,7 @@ TranzportControlProtocol::step_gain_down ()
                gain_fraction = 0.0;
        }
        
-       current_route->set_gain (slider_position_to_gain (gain_fraction), this);
+       route_set_gain (0, slider_position_to_gain (gain_fraction));
 }
 
 void
@@ -1458,83 +1470,15 @@ TranzportControlProtocol::next_wheel_mode ()
 void
 TranzportControlProtocol::next_track ()
 {
-       uint32_t limit = session.nroutes();
-       uint32_t start = current_track_id;
-       Route* cr = current_route;
-
-       if (current_track_id == limit) {
-               current_track_id = 0;
-       } else {
-               current_track_id++;
-       }
-
-       while (current_track_id < limit) {
-               if ((cr = session.route_by_remote_id (current_track_id)) != 0) {
-                       break;
-               }
-               current_track_id++;
-       }
-
-       if (current_track_id == limit) {
-               current_track_id = 0;
-               while (current_track_id != start) {
-                       if ((cr = session.route_by_remote_id (current_track_id)) != 0) {
-                               break;
-                       }
-                       current_track_id++;
-               }
-       }
-
-       current_route = cr;
-       gain_fraction = gain_to_slider_position (current_route->effective_gain());
+       ControlProtocol::next_track (current_track_id);
+       gain_fraction = gain_to_slider_position (route_get_effective_gain (0));
 }
 
 void
 TranzportControlProtocol::prev_track ()
 {
-       uint32_t limit = session.nroutes() - 1;
-       uint32_t start = current_track_id;
-       Route* cr = current_route;
-
-       if (current_track_id == 0) {
-               current_track_id = session.nroutes() - 1;
-       } else {
-               current_track_id--;
-       }
-
-       while (current_track_id >= 0) {
-               if ((cr = session.route_by_remote_id (current_track_id)) != 0) {
-                       break;
-               }
-               current_track_id--;
-       }
-
-       if (current_track_id < 0) {
-               current_track_id = limit;
-               while (current_track_id > start) {
-                       if ((cr = session.route_by_remote_id (current_track_id)) != 0) {
-                               break;
-                       }
-                       current_track_id--;
-               }
-       }
-
-       current_route = cr;
-       gain_fraction = gain_to_slider_position (current_route->effective_gain());
-}
-
-void
-TranzportControlProtocol::set_current_track (Route* r)
-{
-       TranzportRequest* req = get_request (SetCurrentTrack);
-       
-       if (req == 0) {
-               return;
-       }
-
-       req->track = r;
-       
-       send_request (req);
+       ControlProtocol::prev_track (current_track_id);
+       gain_fraction = gain_to_slider_position (route_get_effective_gain (0));
 }
 
 void
@@ -1630,18 +1574,15 @@ TranzportControlProtocol::print (int row, int col, const char *text)
        }
 }      
 
-bool
-TranzportControlProtocol::caller_is_ui_thread ()
+XMLNode&
+TranzportControlProtocol::get_state () 
 {
-       return (pthread_self() == thread);
+       XMLNode* node = new XMLNode (_name); /* node name must match protocol name */
+       return *node;
 }
 
-void
-TranzportControlProtocol::do_request (TranzportRequest* req)
+int
+TranzportControlProtocol::set_state (const XMLNode& node)
 {
-       if (req->type == SetCurrentTrack) {
-               current_route = req->track;
-       } 
-
-       return;
+       return 0;
 }