d88143d1102e23510789f40b6d4019f4a01c73a9
[ardour.git] / libs / ardour / ardour / port_manager.h
1 /*
2     Copyright (C) 2013 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __libardour_port_manager_h__
21 #define __libardour_port_manager_h__
22
23 #include <vector>
24 #include <string>
25 #include <exception>
26 #include <map>
27
28 #include <stdint.h>
29
30 #include <boost/shared_ptr.hpp>
31
32 #include "pbd/rcu.h"
33
34 #include "ardour/chan_count.h"
35 #include "ardour/port.h"
36 #include "ardour/port_engine.h"
37
38 namespace ARDOUR {
39
40 class PortManager 
41 {
42   public:
43     typedef std::map<std::string,boost::shared_ptr<Port> > Ports;
44     
45     PortManager ();
46     virtual ~PortManager() {}
47
48     void set_port_engine (PortEngine& pe);
49     PortEngine& port_engine() { return *_impl; }
50     
51     /* Port registration */
52     
53     boost::shared_ptr<Port> register_input_port (DataType, const std::string& portname);
54     boost::shared_ptr<Port> register_output_port (DataType, const std::string& portname);
55     int unregister_port (boost::shared_ptr<Port>);
56     
57     /* Port connectivity */
58     
59     int  connect (const std::string& source, const std::string& destination);
60     int  disconnect (const std::string& source, const std::string& destination);
61     int  disconnect (boost::shared_ptr<Port>);
62     int  reestablish_ports ();
63     int  reconnect_ports ();
64
65     bool  connected (const std::string&);
66     bool  connected_to (const std::string&, const std::string&);
67     bool  physically_connected (const std::string&);
68     int   get_connections (const std::string&, std::vector<std::string>&);
69
70     /* Naming */
71
72     boost::shared_ptr<Port> get_port_by_name (const std::string &);
73     void                    port_renamed (const std::string&, const std::string&);
74     std::string             make_port_name_relative (const std::string& name) const;
75     std::string             make_port_name_non_relative (const std::string& name) const;
76     bool                    port_is_mine (const std::string& fullname) const;
77
78     /* other Port management */
79     
80     bool      port_is_physical (const std::string&) const;
81     void      get_physical_outputs (DataType type, std::vector<std::string>&);
82     void      get_physical_inputs (DataType type, std::vector<std::string>&);
83     ChanCount n_physical_outputs () const;
84     ChanCount n_physical_inputs () const;
85
86     int get_ports (const std::string& port_name_pattern, DataType type, PortFlags flags, std::vector<std::string>&);
87     
88     void remove_all_ports ();
89     
90     /* per-Port monitoring */
91     
92     bool can_request_input_monitoring () const;
93     void request_input_monitoring (const std::string&, bool) const;
94     void ensure_input_monitoring (const std::string&, bool) const;
95     
96     class PortRegistrationFailure : public std::exception {
97       public:
98         PortRegistrationFailure (std::string const & why = "")
99                 : reason (why) {}
100         
101         ~PortRegistrationFailure () throw () {}
102         
103         const char *what() const throw () { return reason.c_str(); }
104         
105       private:
106         std::string reason;
107     };
108
109     /* the port engine will invoke these callbacks when the time is right */
110     
111     void registration_callback ();
112     int graph_order_callback ();
113     void connect_callback (const std::string&, const std::string&, bool connection);
114
115     bool port_remove_in_progress() const { return _port_remove_in_progress; }
116
117     /** Emitted if a Port is registered or unregistered */
118     PBD::Signal0<void> PortRegisteredOrUnregistered;
119     
120     /** Emitted if a Port is connected or disconnected.
121      *  The Port parameters are the ports being connected / disconnected, or 0 if they are not known to Ardour.
122      *  The std::string parameters are the (long) port names.
123      *  The bool parameter is true if ports were connected, or false for disconnected.
124      */
125     PBD::Signal5<void, boost::weak_ptr<Port>, std::string, boost::weak_ptr<Port>, std::string, bool> PortConnectedOrDisconnected;
126
127   protected:
128     boost::shared_ptr<PortEngine> _impl;
129     SerializedRCUManager<Ports> ports;
130     bool _port_remove_in_progress;
131
132     boost::shared_ptr<Port> register_port (DataType type, const std::string& portname, bool input);
133     void port_registration_failure (const std::string& portname);
134 };
135         
136 } // namespace
137
138 #endif /* __libardour_port_manager_h__ */