Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[ardour.git] / libs / ardour / jack_port.cc
1 /*
2     Copyright (C) 2002-2006 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 #include <pbd/error.h>
21
22 #include <ardour/jack_port.h>
23 #include <ardour/audioengine.h>
24
25 #include "i18n.h"
26
27 using namespace ARDOUR;
28 using namespace PBD;
29 using namespace std;
30
31 JackPort::JackPort (const std::string& name, DataType type, Flags flgs) 
32         : Port (name, flgs), _port (0)
33 {
34         _port = jack_port_register (engine->jack(), name.c_str(), type.to_jack_type(), flgs, 0);
35
36         if (_port == 0) {
37                 throw failed_constructor();
38         }
39         
40         _flags = flgs;
41         _type  = type;
42         _name = jack_port_name (_port);
43 }
44
45 JackPort::~JackPort ()
46 {
47         cerr << "deleting jack port " << _name << endl;
48
49         jack_port_unregister (engine->jack(), _port);
50 }
51
52 int 
53 JackPort::set_name (const string& str)
54 {
55         int ret;
56
57         if ((ret = jack_port_set_name (_port, str.c_str())) == 0) {
58                 _name = str;
59         }
60         
61         return ret;
62 }
63
64 int
65 JackPort::disconnect ()
66 {
67         return jack_port_disconnect (engine->jack(), _port);
68 }       
69
70 nframes_t
71 JackPort::total_latency () const
72 {
73         return jack_port_get_total_latency (engine->jack(), _port);
74 }
75
76 int
77 JackPort::reestablish ()
78 {
79         string short_name;
80         
81         short_name = _name.substr (_name.find_last_of (':') + 1);
82
83         _port = jack_port_register (engine->jack(), short_name.c_str(), type().to_jack_type(), _flags, 0);
84
85         if (_port == 0) {
86                 error << string_compose (_("could not reregister %1"), _name) << endmsg;
87                 return -1;
88         }
89
90         reset ();
91         
92
93         return 0;
94 }
95
96 void
97 JackPort::recompute_total_latency () const
98 {
99 #ifdef HAVE_JACK_RECOMPUTE_LATENCY
100         jack_recompute_total_latency (engine->jack(), _port);
101 #endif
102 }
103
104 int
105 JackPort::reconnect ()
106 {
107         /* caller must hold process lock; intended to be used only after reestablish() */
108
109         for (set<string>::iterator i = _named_connections.begin(); i != _named_connections.end(); ++i) {
110                 if (connect (*i)) {
111                         return -1;
112                 }
113         }
114
115         return 0;
116 }
117
118 int
119 JackPort::connect (const std::string& other)
120 {
121         int ret;
122
123         if (_flags & IsOutput) {
124                 /* this is the source */
125                 ret = jack_connect (engine->jack(), _name.c_str(), other.c_str());
126         } else {
127                 ret = jack_connect (engine->jack(), other.c_str(), _name.c_str());
128         }
129
130         if (ret == 0) {
131                 _named_connections.insert (other);
132         }
133         
134         return ret;
135 }
136
137 int
138 JackPort::disconnect (const std::string& other)
139 {
140         int ret;
141
142         if (_flags & IsInput) {
143                 ret = jack_disconnect (engine->jack(), other.c_str(), _name.c_str());
144         } else {
145                 ret = jack_disconnect (engine->jack(), _name.c_str(), other.c_str());
146         }
147
148         set<string>::iterator i = _named_connections.find (other);
149
150         if (i != _named_connections.end()) {
151                 _named_connections.erase (i);
152         }
153
154         return ret;
155 }
156
157 int
158 JackPort::disconnect_all ()
159 {
160         _named_connections.clear ();
161         return jack_port_disconnect (engine->jack(), _port);
162 }
163
164 int
165 JackPort::get_connections (vector<string>& names) const
166 {
167         const char** cstrs =  jack_port_get_connections (_port);
168         int i;
169         
170         if (!cstrs) {
171                 return 0;
172         }
173         
174         for (i = 0; cstrs[i]; ++i) {
175                 names.push_back (string (cstrs[i]));
176         }
177
178         return i;
179 }