merge from 2.0-ongoing @ 3581
[ardour.git] / libs / ardour / ardour / port.h
1 /*
2     Copyright (C) 2002 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 <set>
24 #include <vector>
25 #include <string>
26 #include <cstring>
27 #include <sigc++/signal.h>
28 #include <pbd/failed_constructor.h>
29 #include <ardour/ardour.h>
30 #include <ardour/data_type.h>
31 #include <jack/jack.h>
32
33 namespace ARDOUR {
34
35 class AudioEngine;
36 class Buffer;
37
38 /** Abstract base for ports
39  */
40 class Port : public virtual sigc::trackable {
41    public:
42         enum Flags {
43                 IsInput = JackPortIsInput,
44                 IsOutput = JackPortIsOutput,
45                 IsPhysical = JackPortIsPhysical,
46                 IsTerminal = JackPortIsTerminal,
47                 CanMonitor = JackPortCanMonitor
48         };
49
50         virtual ~Port();
51
52         std::string name() const { 
53                 return _name;
54         }
55
56         Flags flags() const {
57                 return _flags;
58         }
59
60         bool receives_input() const {
61                 return _flags & IsInput;
62         }
63
64         bool sends_output () const {
65                 return _flags & IsOutput;
66         }
67
68         bool can_monitor () const {
69                 return _flags & CanMonitor;
70         }
71
72         void enable_metering() {
73                 _metering++;
74         }
75         
76         void disable_metering () {
77                 if (_metering) { _metering--; }
78         }
79
80         virtual void cycle_start (nframes_t nframes, nframes_t offset) {}
81         virtual void cycle_end (nframes_t nframes, nframes_t offset) {}
82         virtual DataType type() const = 0;
83         virtual Buffer& get_buffer() = 0;
84
85         virtual bool connected () const;
86         virtual bool connected_to (const std::string& portname) const;
87         virtual int get_connections (std::vector<std::string>&) const;
88
89         virtual int connect (Port& other);
90         virtual int disconnect (Port& other);
91         virtual int disconnect_all ();
92         
93         virtual void reset ();
94         virtual int reestablish () {return 0; }
95         virtual int reconnect () { return 0; }
96
97         virtual int set_name (const std::string& str) {
98                 _name = str;
99                 return 0;
100         }
101
102         virtual std::string short_name() const = 0;
103         virtual bool monitoring_input () const = 0;
104         virtual void ensure_monitor_input (bool yn) = 0;
105         virtual void request_monitor_input (bool yn) = 0;
106         virtual nframes_t latency () const = 0;
107         virtual nframes_t total_latency () const = 0;
108         virtual void set_latency (nframes_t nframes) = 0;
109
110         sigc::signal<void,bool> MonitorInputChanged;
111         sigc::signal<void,bool> ClockSyncChanged;
112
113         static void set_engine (AudioEngine*);
114
115   protected:
116         friend class AudioEngine;
117
118         Port (const std::string& name, Flags flgs);
119
120         virtual void recompute_total_latency() const {}
121         
122         /* engine isn't supposed to access below here */
123
124         Flags          _flags;
125         std::string    _type;
126         std::string    _name;
127         unsigned short _metering;
128         bool           _last_monitor;
129         nframes_t      _latency;
130
131         std::set<Port*> _connections;
132
133         static AudioEngine* engine;
134 };
135
136 class PortConnectableByName {
137   public:
138         PortConnectableByName() {}
139         virtual ~PortConnectableByName() {}
140
141         virtual int connect (const std::string& other_name) = 0;
142         virtual int disconnect (const std::string& other_name) = 0;
143 };
144  
145 class PortFacade : public virtual Port, public PortConnectableByName { 
146   public:
147         PortFacade (const std::string& name, Flags flgs) : Port (name, flgs), _ext_port (0) {}
148         ~PortFacade() {}
149
150         void reset ();
151         int reestablish ();
152         int reconnect ();
153
154         int connect (Port& other);
155         int disconnect (Port& other);
156         int disconnect_all ();
157
158         int connect (const std::string& other_name);
159         int disconnect (const std::string& other_name);
160
161         bool connected () const;
162         bool connected_to (const std::string& portname) const;
163         int get_connections (std::vector<std::string>&) const;
164
165         std::string short_name() const;
166         int         set_name (const std::string& str);
167         bool        monitoring_input () const;
168         void        ensure_monitor_input (bool yn);
169         void        request_monitor_input (bool yn);
170         nframes_t   latency () const;
171         nframes_t   total_latency () const;
172         void        set_latency (nframes_t nframes);
173
174   protected:
175         Port* _ext_port;
176 };
177
178 } // namespace ARDOUR
179
180 #endif /* __ardour_port_h__ */