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