Rework Port class hierarchy a bit. Hopefully now simpler, and should
[ardour.git] / libs / ardour / ardour / port.h
1 /*
2     Copyright (C) 2009 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 __ardour_port_h__
21 #define __ardour_port_h__
22
23 #include "ardour/data_type.h"
24 #include "ardour/types.h"
25 #include <sigc++/trackable.h>
26 #include <jack/jack.h>
27 #include <string>
28 #include <set>
29 #include <vector>
30
31 namespace ARDOUR {
32
33 class AudioEngine;
34 class Buffer;   
35
36 class Port : public sigc::trackable
37 {
38 public:
39         enum Flags {
40                 IsInput = JackPortIsInput,
41                 IsOutput = JackPortIsOutput,
42         };
43
44         virtual ~Port ();
45
46         /** @return Port name (excluding prefix) */
47         std::string name () const {
48                 return _name;
49         }
50
51         int set_name (std::string const &);
52
53         /** @return flags */
54         Flags flags () const {
55                 return _flags;
56         }
57
58         /** @return true if this Port receives input, otherwise false */
59         bool receives_input () const {
60                 return _flags & IsInput;
61         }
62
63         /** @return true if this Port sends output, otherwise false */
64         bool sends_output () const {
65                 return _flags & IsOutput;
66         }
67
68         /* @return true if this port is visible outside Ardour (via JACK) */
69         bool external () const {
70                 return _jack_port != 0;
71         }
72
73         bool connected () const;
74         int disconnect_all ();
75         int get_connections (std::vector<std::string> &) const;
76
77         /* connection by name */
78         bool connected_to (std::string const &) const;
79         int connect (std::string const &);
80         int disconnect (std::string const &);
81
82         /* connection by Port* */
83         bool connected_to (Port *) const;
84         int connect (Port *);
85         int disconnect (Port *);
86
87         void ensure_monitor_input (bool);
88         bool monitoring_input () const;
89         nframes_t total_latency () const;
90         int reestablish ();
91         int reconnect ();
92         void set_latency (nframes_t);
93         void request_monitor_input (bool);
94         void make_external ();
95
96         virtual void reset ();
97
98         virtual DataType type () const = 0;
99         virtual void cycle_start (nframes_t, nframes_t) = 0;
100         virtual void cycle_end (nframes_t, nframes_t) = 0;
101         virtual Buffer& get_buffer (nframes_t, nframes_t) = 0;
102         virtual void flush_buffers (nframes_t, nframes_t) {}
103
104         static void set_engine (AudioEngine *);
105
106         sigc::signal<void, bool> MonitorInputChanged;
107
108 protected:
109         
110         Port (std::string const &, DataType, Flags, bool);
111
112         jack_port_t* _jack_port; ///< JACK port, or 0 if we don't have one
113         std::set<Port*> _connections; ///< internal Ports that we are connected to
114
115         static AudioEngine* _engine; ///< the AudioEngine
116         
117 private:
118         friend class AudioEngine;
119
120         void recompute_total_latency () const;
121         void do_make_external (DataType);
122         
123         /* XXX */
124         bool _last_monitor;
125         nframes_t _latency;
126
127         std::string _name; ///< port name (excluding prefix)
128         Flags _flags; ///< flags
129
130         /// list of JACK ports that we are connected to; we only keep this around
131         /// so that we can implement ::reconnect ()
132         std::set<std::string> _named_connections;
133 };
134
135 }
136
137 #endif