MCP: a fistful of improvements. probably best to just try it and see what it broken...
[ardour.git] / libs / surfaces / mackie / surface_port.cc
1 /*
2         Copyright (C) 2006,2007 John Anderson
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 #include <sstream>
20 #include <cstring>
21 #include <cerrno>
22
23 #include <sigc++/sigc++.h>
24 #include <boost/shared_array.hpp>
25
26 #include "midi++/types.h"
27 #include "midi++/port.h"
28 #include "midi++/manager.h"
29
30 #include "ardour/debug.h"
31 #include "ardour/rc_configuration.h"
32 #include "ardour/session.h"
33 #include "ardour/audioengine.h"
34
35 #include "controls.h"
36 #include "mackie_control_protocol.h"
37 #include "surface.h"
38 #include "surface_port.h"
39
40
41 #include "i18n.h"
42
43 using namespace std;
44 using namespace Mackie;
45 using namespace PBD;
46
47 /** @param input_port Input MIDI::Port; this object takes responsibility for
48  *  adding & removing it from the MIDI::Manager and destroying it.  @param
49  *  output_port Output MIDI::Port; responsibility similarly taken.
50  */
51 SurfacePort::SurfacePort (Surface& s)
52         : _surface (&s)
53 {
54         jack_client_t* jack = MackieControlProtocol::instance()->get_session().engine().jack();
55
56         _input_port = new MIDI::Port (string_compose (_("%1 in"),  _surface->name()), MIDI::Port::IsInput, jack);
57         _output_port =new MIDI::Port (string_compose (_("%1 out"), _surface->name()), MIDI::Port::IsOutput, jack);
58
59         /* MackieControl has its own thread for handling input from the input
60          * port, and we don't want anything handling output from the output
61          * port. This stops the Generic MIDI UI event loop in ardour from
62          * attempting to handle these ports.
63          */
64
65         _input_port->set_centrally_parsed (false);
66         _output_port->set_centrally_parsed (false);
67         
68         MIDI::Manager * mm = MIDI::Manager::instance();
69
70         mm->add_port (_input_port);
71         mm->add_port (_output_port);
72 }
73
74 SurfacePort::~SurfacePort()
75 {
76         MIDI::Manager* mm = MIDI::Manager::instance ();
77         
78         if (_input_port) {
79                 mm->remove_port (_input_port);
80                 delete _input_port;
81         }
82
83         if (_output_port) {
84                 mm->remove_port (_output_port);
85                 delete _output_port;
86         }
87 }
88
89 // wrapper for one day when strerror_r is working properly
90 string fetch_errmsg (int error_number)
91 {
92         char * msg = strerror (error_number);
93         return msg;
94 }
95         
96 int 
97 SurfacePort::write (const MidiByteArray & mba)
98 {
99         if (mba.empty()) {
100                 return 0;
101         }
102
103         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 write %2\n", output_port().name(), mba));
104
105         int count = output_port().write (mba.bytes().get(), mba.size(), 0);
106
107         if  (count != (int)mba.size()) {
108
109                 if  (errno == 0) {
110
111                         cout << "port overflow on " << output_port().name() << ". Did not write all of " << mba << endl;
112
113                 } else if  (errno != EAGAIN) {
114                         ostringstream os;
115                         os << "Surface: couldn't write to port " << output_port().name();
116                         os << ", error: " << fetch_errmsg (errno) << "(" << errno << ")";
117                         cout << os.str() << endl;
118                 }
119
120                 return -1;
121         }
122
123         return 0;
124 }
125
126 ostream & 
127 Mackie::operator <<  (ostream & os, const SurfacePort & port)
128 {
129         os << "{ ";
130         os << "name: " << port.input_port().name() << " " << port.output_port().name();
131         os << "; ";
132         os << " }";
133         return os;
134 }
135