Fix build.
[ardour.git] / libs / midi++2 / manager.cc
1 /*
2     Copyright (C) 1998-99 Paul Barton-Davis 
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2 of the License, or
6     (at your option) any later version.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
17     $Id$
18 */
19
20 #include <fcntl.h>
21
22 #include <glib.h>
23
24 #include "pbd/error.h"
25
26 #include "midi++/types.h"
27 #include "midi++/manager.h"
28 #include "midi++/channel.h"
29 #include "midi++/port.h"
30 #include "midi++/mmc.h"
31
32 using namespace std;
33 using namespace MIDI;
34 using namespace PBD;
35
36 Manager *Manager::theManager = 0;
37
38 Manager::Manager (jack_client_t* jack) 
39 {
40         _mmc = new MachineControl (this, jack);
41         
42         _mtc_input_port = add_port (new MIDI::Port ("MTC in", Port::IsInput, jack));
43         _mtc_output_port = add_port (new MIDI::Port ("MTC out", Port::IsOutput, jack));
44         _midi_input_port = add_port (new MIDI::Port ("MIDI control in", Port::IsInput, jack));
45         _midi_output_port = add_port (new MIDI::Port ("MIDI control out", Port::IsOutput, jack));
46         _midi_clock_input_port = add_port (new MIDI::Port ("MIDI clock in", Port::IsInput, jack));
47         _midi_clock_output_port = add_port (new MIDI::Port ("MIDI clock out", Port::IsOutput, jack));
48 }
49
50 Manager::~Manager ()
51 {
52         delete _mmc;
53         
54         /* This will delete our MTC etc. ports */
55         for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
56                 delete *p;
57         }
58
59         if (theManager == this) {
60                 theManager = 0;
61         }
62 }
63
64 Port *
65 Manager::add_port (Port* p)
66 {
67         _ports.push_back (p);
68
69         PortsChanged (); /* EMIT SIGNAL */
70
71         return p;
72 }
73
74 void
75 Manager::cycle_start (pframes_t nframes)
76 {
77         for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
78                 (*p)->cycle_start (nframes);
79         }
80 }
81
82 void
83 Manager::cycle_end()
84 {
85         for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
86                 (*p)->cycle_end ();
87         }
88 }
89
90 /** Re-register ports that disappear on JACK shutdown */
91 void
92 Manager::reestablish (jack_client_t* jack)
93 {
94         for (PortList::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
95                 (*p)->reestablish (jack);
96         }
97 }
98
99 /** Re-connect ports after a reestablish () */
100 void
101 Manager::reconnect ()
102 {
103         for (PortList::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
104                 (*p)->reconnect ();
105         }
106 }
107
108 Port*
109 Manager::port (string const & n)
110 {
111         PortList::const_iterator p = _ports.begin();
112         while (p != _ports.end() && (*p)->name() != n) {
113                 ++p;
114         }
115
116         if (p == _ports.end()) {
117                 return 0;
118         }
119
120         return *p;
121 }
122
123 void
124 Manager::create (jack_client_t* jack)
125 {
126         assert (theManager == 0);
127         theManager = new Manager (jack);
128 }
129
130 void
131 Manager::set_port_states (list<XMLNode*> s)
132 {
133         for (list<XMLNode*>::iterator i = s.begin(); i != s.end(); ++i) {
134                 for (PortList::const_iterator j = _ports.begin(); j != _ports.end(); ++j) {
135                         (*j)->set_state (**i);
136                 }
137         }
138 }