move MidiPortManager from AudioEngine to Session
[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/midiport_manager.h"
36 #include "ardour/port.h"
37 #include "ardour/port_engine.h"
38
39 namespace ARDOUR {
40
41 class PortManager 
42 {
43   public:
44     typedef std::map<std::string,boost::shared_ptr<Port> > Ports;
45     typedef std::list<boost::shared_ptr<Port> > PortList;
46     
47     PortManager ();
48     virtual ~PortManager() {}
49
50     void set_port_engine (PortEngine& pe);
51     PortEngine& port_engine() { return *_impl; }
52
53     uint32_t port_name_size() const;
54     std::string my_name() const;
55
56     /* Port registration */
57     
58     boost::shared_ptr<Port> register_input_port (DataType, const std::string& portname, bool async = false);
59     boost::shared_ptr<Port> register_output_port (DataType, const std::string& portname, bool async = false);
60     int unregister_port (boost::shared_ptr<Port>);
61     
62     /* Port connectivity */
63     
64     int  connect (const std::string& source, const std::string& destination);
65     int  disconnect (const std::string& source, const std::string& destination);
66     int  disconnect (boost::shared_ptr<Port>);
67     int  reestablish_ports ();
68     int  reconnect_ports ();
69
70     bool  connected (const std::string&);
71     bool  connected_to (const std::string&, const std::string&);
72     bool  physically_connected (const std::string&);
73     int   get_connections (const std::string&, std::vector<std::string>&);
74
75     /* Naming */
76
77     boost::shared_ptr<Port> get_port_by_name (const std::string &);
78     void                    port_renamed (const std::string&, const std::string&);
79     std::string             make_port_name_relative (const std::string& name) const;
80     std::string             make_port_name_non_relative (const std::string& name) const;
81     bool                    port_is_mine (const std::string& fullname) const;
82
83     /* other Port management */
84     
85     bool      port_is_physical (const std::string&) const;
86     void      get_physical_outputs (DataType type, std::vector<std::string>&);
87     void      get_physical_inputs (DataType type, std::vector<std::string>&);
88     ChanCount n_physical_outputs () const;
89     ChanCount n_physical_inputs () const;
90
91     int get_ports (const std::string& port_name_pattern, DataType type, PortFlags flags, std::vector<std::string>&);
92     int get_ports (DataType, PortList&);
93
94     void remove_all_ports ();
95     
96     /* per-Port monitoring */
97     
98     bool can_request_input_monitoring () const;
99     void request_input_monitoring (const std::string&, bool) const;
100     void ensure_input_monitoring (const std::string&, bool) const;
101     
102     class PortRegistrationFailure : public std::exception {
103       public:
104         PortRegistrationFailure (std::string const & why = "")
105                 : reason (why) {}
106         
107         ~PortRegistrationFailure () throw () {}
108         
109         const char *what() const throw () { return reason.c_str(); }
110         
111       private:
112         std::string reason;
113     };
114
115     /* the port engine will invoke these callbacks when the time is right */
116     
117     void registration_callback ();
118     int graph_order_callback ();
119     void connect_callback (const std::string&, const std::string&, bool connection);
120
121     bool port_remove_in_progress() const { return _port_remove_in_progress; }
122
123     /** Emitted if the backend notifies us of a graph order event */
124     PBD::Signal0<void> GraphReordered;
125
126     /** Emitted if a Port is registered or unregistered */
127     PBD::Signal0<void> PortRegisteredOrUnregistered;
128     
129     /** Emitted if a Port is connected or disconnected.
130      *  The Port parameters are the ports being connected / disconnected, or 0 if they are not known to Ardour.
131      *  The std::string parameters are the (long) port names.
132      *  The bool parameter is true if ports were connected, or false for disconnected.
133      */
134     PBD::Signal5<void, boost::weak_ptr<Port>, std::string, boost::weak_ptr<Port>, std::string, bool> PortConnectedOrDisconnected;
135
136   protected:
137     boost::shared_ptr<PortEngine> _impl;
138     SerializedRCUManager<Ports> ports;
139     bool _port_remove_in_progress;
140
141     boost::shared_ptr<Port> register_port (DataType type, const std::string& portname, bool input, bool async = false);
142     void port_registration_failure (const std::string& portname);
143
144     /** List of ports to be used between ::cycle_start() and ::cycle_end()
145      */
146     boost::shared_ptr<Ports> _cycle_ports;
147
148     void fade_out (gain_t, gain_t, pframes_t);
149     void silence (pframes_t nframes);
150     void check_monitoring ();
151     /** Signal the start of an audio cycle.
152      * This MUST be called before any reading/writing for this cycle.
153      * Realtime safe.
154      */
155     void cycle_start (pframes_t nframes);
156         
157     /** Signal the end of an audio cycle.
158      * This signifies that the cycle began with @ref cycle_start has ended.
159      * This MUST be called at the end of each cycle.
160      * Realtime safe.
161      */
162     void cycle_end (pframes_t nframes);
163 };
164
165
166         
167 } // namespace
168
169 #endif /* __libardour_port_manager_h__ */