Stop trying to talk to device on startup. Remap some bcf buttons.
[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 #include "surface_port.h"
19
20 #include "mackie_control_exception.h"
21 #include "controls.h"
22
23 #include <midi++/types.h>
24 #include <midi++/port.h>
25 #include <sigc++/sigc++.h>
26 #include <boost/shared_array.hpp>
27
28 #include "i18n.h"
29
30 #include <sstream>
31 #include <cstring>
32 #include <cerrno>
33
34 using namespace std;
35 using namespace Mackie;
36
37 SurfacePort::SurfacePort( MIDI::Port & port, int number )
38 : _port( port ), _number( number ), _active( false )
39 {
40 }
41
42 MidiByteArray SurfacePort::read()
43 {
44         const int max_buf_size = 512;
45         MIDI::byte buf[max_buf_size];
46         MidiByteArray retval;
47
48         // return nothing read if the lock isn't acquired
49         Glib::RecMutex::Lock lock( _rwlock, Glib::TRY_LOCK );
50         if ( !lock.locked() )
51         {
52                 cout << "SurfacePort::read not locked" << endl;
53                 return retval;
54         }
55         
56         // read port and copy to return value
57         int nread = port().read( buf, sizeof (buf) );
58
59         if (nread >= 0) {
60                 retval.copy( nread, buf );
61                 if ((size_t) nread == sizeof (buf))
62                 {
63                         retval << read();
64                 }
65         }
66         else
67         {
68                 if ( errno != EAGAIN )
69                 {
70                         char buf[512];
71                         char * msg = strerror_r( errno, buf, 512 );
72                         
73                         ostringstream os;
74                         os << "Surface: error reading from port: " << port().name() << ": " << errno << " " << msg;
75                         cout << os.str() << endl;
76                         inactive_event();
77                         throw MackieControlException( os.str() );
78                 }
79         }
80         return retval;
81 }
82
83 void SurfacePort::write( const MidiByteArray & mba )
84 {
85         //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write: " << mba << endl;
86         //cout << "SurfacePort::write: " << mba << endl;
87         Glib::RecMutex::Lock lock( _rwlock );
88         int count = port().write( mba.bytes().get(), mba.size() );
89         if ( count != (int)mba.size() )
90         {
91                 if ( errno != EAGAIN )
92                 {
93                         char buf[512];
94                         char * msg = strerror_r( errno, buf, 512 );
95                         
96                         ostringstream os;
97                         os << "Surface: couldn't write to port " << port().name() << ": " << errno << " " << msg;
98                         cout << os.str();
99                         inactive_event();
100                         throw MackieControlException( os.str() );
101                 }
102         }
103         //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write " << count << endl;
104 }
105
106 void SurfacePort::write_sysex( const MidiByteArray & mba )
107 {
108         MidiByteArray buf;
109         buf << sysex_hdr() << mba << MIDI::eox;
110         write( buf );
111 }
112
113 void SurfacePort::write_sysex( MIDI::byte msg )
114 {
115         MidiByteArray buf;
116         buf << sysex_hdr() << msg << MIDI::eox;
117         write( buf );
118 }
119
120 // This should be moved to midi++ at some point
121 ostream & operator << ( ostream & os, const MIDI::Port & port )
122 {
123         os << "device: " << port.device();
124         os << "; ";
125         os << "name: " << port.name();
126         os << "; ";
127         os << "type: " << port.type();
128         os << "; ";
129         os << "mode: " << port.mode();
130         os << "; ";
131         os << "ok: " << port.ok();
132         os << "; ";
133         os << "number: " << port.number();
134         os << "; ";
135         return os;
136 }
137
138 ostream & Mackie::operator << ( ostream & os, const SurfacePort & port )
139 {
140         os << "{ ";
141         os << "device: " << port.port().device();
142         os << "; ";
143         os << "name: " << port.port().name();
144         os << "; ";
145         os << "number: " << port.number();
146         os << " }";
147         return os;
148 }