summaryrefslogtreecommitdiff
path: root/libs/surfaces/mackie/mackie_port.h
blob: 0a705e65f367fb84b8d547ee5894ab22a959a46e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
	Copyright (C) 2006,2007 John Anderson

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef mackie_port_h
#define mackie_port_h

#include "surface_port.h"

#include <midi++/types.h>
#include <sigc++/signal.h>
#include <sigc++/connection.h>

#include <glibmm/thread.h>

#include "midi_byte_array.h"
#include "types.h"

namespace MIDI {
	class Port;
	class Parser;
}

class MackieControlProtocol;

namespace Mackie
{

class MackiePort : public SurfacePort
{
public:
	enum port_type_t { mcu, ext };
	enum emulation_t { none, mackie, bcf2000 };
	
	MackiePort( MackieControlProtocol & mcp, MIDI::Port & port, int number, port_type_t = mcu );
	~MackiePort();

	virtual void open();
	virtual void close();

	/// MCU and extenders have different sysex headers
	virtual const MidiByteArray & sysex_hdr() const;

	/// Handle device initialisation
	void handle_midi_sysex( MIDI::Parser &, MIDI::byte *, size_t );

	/// Handle all control messags
	void handle_midi_any( MIDI::Parser &, MIDI::byte *, size_t );
	
	Control & lookup_control( const MidiByteArray & bytes );
	
	/// return the number of strips associated with this port
	virtual int strips() const;

	/// Block until the port has finished initialising, and then return
	/// whether the intialisation succeeded
	bool wait_for_init();
	
	emulation_t emulation() const { return _emulation; }
	
	/// Connect the any signal from the parser to handle_midi_any
	/// unless it's already connected
	void connect_any();
	
protected:
	/**
		The initialisation sequence is fairly complex. First a lock is acquired
		so that a condition can be used to signal the end of the init process.
		Then a sysex is sent to the device. The response to the sysex
		is handled by a switch in handle_midi_sysex which calls one of the
		other methods.
		
		However, windows DAWs ignore the documented init sequence and so we
		do too. Thanks to Essox for helping with this.
		
		So we use the version firmware to figure out what device is on
		the other end of the cable.
	*/
	void init();

	/**
		Once the device is initialised, finalise_init(true) is called, which
		releases the lock and signals the condition, and starts handling incoming
		messages. finalise_init(false) will also release the lock but doesn't
		start handling messages.
	*/
	void finalise_init( bool yn );

	MidiByteArray host_connection_query( MidiByteArray & bytes );
	MidiByteArray host_connection_confirmation( const MidiByteArray & bytes );

	/**
		Will set _emulation to what it thinks is correct, based
		on responses from the device. Or get/set parameters. Or
		environment variables. Or existence of a file.
	*/
	void probe_emulation( const MidiByteArray & bytes );

private:
	MackieControlProtocol & _mcp;
	port_type_t _port_type;
	sigc::connection _any;
	sigc::connection _sysex;
	emulation_t _emulation;

	bool _initialising;
	Glib::Cond init_cond;
	Glib::Mutex init_mutex;
};

}

#endif