switch to using boost::signals2 instead of sigc++, at least for libardour. not finish...
[ardour.git] / libs / surfaces / mackie / mackie_port.h
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 #ifndef mackie_port_h
19 #define mackie_port_h
20
21 #include "surface_port.h"
22
23 #include <midi++/types.h>
24 #include <boost/signals2.hpp>
25
26 #include <glibmm/thread.h>
27
28 #include "midi_byte_array.h"
29 #include "types.h"
30
31 namespace MIDI {
32         class Port;
33         class Parser;
34 }
35
36 class MackieControlProtocol;
37
38 namespace Mackie
39 {
40
41 class MackiePort : public SurfacePort
42 {
43 public:
44         enum port_type_t { mcu, ext };
45         enum emulation_t { none, mackie, bcf2000 };
46         
47         MackiePort( MackieControlProtocol & mcp, MIDI::Port & port, int number, port_type_t = mcu );
48         ~MackiePort();
49
50         virtual void open();
51         virtual void close();
52
53         /// MCU and extenders have different sysex headers
54         virtual const MidiByteArray & sysex_hdr() const;
55
56         /// Handle device initialisation
57         void handle_midi_sysex( MIDI::Parser &, MIDI::byte *, size_t count );
58
59         /// Handle all control messags
60         void handle_midi_any( MIDI::Parser &, MIDI::byte *, size_t count );
61         
62         Control & lookup_control( MIDI::byte *, size_t count );
63         
64         /// return the number of strips associated with this port
65         virtual int strips() const;
66
67         /// Block until the port has finished initialising, and then return
68         /// whether the intialisation succeeded
69         bool wait_for_init();
70         
71         emulation_t emulation() const { return _emulation; }
72         
73         /// Connect the any signal from the parser to handle_midi_any
74         /// unless it's already connected
75         void connect_any();
76         
77 protected:
78         /**
79                 The initialisation sequence is fairly complex. First a lock is acquired
80                 so that a condition can be used to signal the end of the init process.
81                 Then a sysex is sent to the device. The response to the sysex
82                 is handled by a switch in handle_midi_sysex which calls one of the
83                 other methods.
84                 
85                 However, windows DAWs ignore the documented init sequence and so we
86                 do too. Thanks to Essox for helping with this.
87                 
88                 So we use the version firmware to figure out what device is on
89                 the other end of the cable.
90         */
91         void init();
92
93         /**
94                 Once the device is initialised, finalise_init(true) is called, which
95                 releases the lock and signals the condition, and starts handling incoming
96                 messages. finalise_init(false) will also release the lock but doesn't
97                 start handling messages.
98         */
99         void finalise_init( bool yn );
100
101         MidiByteArray host_connection_query( MidiByteArray & bytes );
102         MidiByteArray host_connection_confirmation( const MidiByteArray & bytes );
103
104         /**
105                 Will set _emulation to what it thinks is correct, based
106                 on responses from the device. Or get/set parameters. Or
107                 environment variables. Or existence of a file.
108         */
109         void probe_emulation( const MidiByteArray & bytes );
110
111         /// Handle timeout events set for controls that don't emit
112         /// an off event
113         bool handle_control_timeout_event ( Control * );
114
115 private:
116         MackieControlProtocol & _mcp;
117         port_type_t _port_type;
118         boost::signals2::scoped_connection _any;
119         boost::signals2::scoped_connection _sysex;
120         emulation_t _emulation;
121
122         bool _initialising;
123         Glib::Cond init_cond;
124         Glib::Mutex init_mutex;
125 };
126
127 }
128
129 #endif