350ee561f6b753c1c901cb25aa7b850f1f4263e1
[ardour.git] / libs / surfaces / mackie / surface_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 surface_port_h
19 #define surface_port_h
20
21 #include <glibmm/thread.h>
22
23 #include "pbd/signals.h"
24 #include "midi_byte_array.h"
25 #include "types.h"
26
27 namespace MIDI {
28         class Port;
29 }
30
31 namespace Mackie
32 {
33
34 /**
35         Make a relationship between a midi port and a Mackie device.
36 */
37 class SurfacePort : public PBD::ScopedConnectionList
38 {
39 public:
40         SurfacePort (MIDI::Port & input_port, MIDI::Port & output_port, int number);
41         virtual ~SurfacePort();
42         
43         // when this is successful, active() should return true
44         virtual void open() = 0;
45         
46         // subclasses should call this before doing their own close
47         virtual void close() = 0;
48
49         /// read bytes from the port. They'll either end up in the
50         /// parser, or if that's not active they'll be returned
51         virtual MidiByteArray read();
52         
53         /// an easier way to output bytes via midi
54         virtual void write( const MidiByteArray & );
55         
56         /// write a sysex message
57         void write_sysex( const MidiByteArray & mba );
58         void write_sysex( MIDI::byte msg );
59
60         /// return the correct sysex header for this port
61         virtual const MidiByteArray & sysex_hdr() const = 0;
62
63         MIDI::Port & input_port() { return *_input_port; }
64         const MIDI::Port & input_port() const { return *_input_port; }
65         MIDI::Port & output_port() { return *_output_port; }
66         const MIDI::Port & output_port() const { return *_output_port; }
67         
68         // emitted just before the port goes into initialisation
69         // where it tries to establish that its device is connected
70         PBD::Signal0<void> init_event;
71         
72         // emitted when the port completes initialisation successfully
73         PBD::Signal0<void> active_event;
74
75         // emitted when the port goes inactive (ie a read or write failed)
76         PBD::Signal0<void> inactive_event;
77         
78         // the port number - master is 0(extenders are 1((,4
79         virtual int number() const { return _number; }
80         
81         // number of strips handled by this port. Usually 8.
82         virtual int strips() const = 0;
83
84         virtual bool active() const { return _active; }
85         virtual void active( bool yn ) { _active = yn; }
86
87         void add_in_use_timeout (Control &, Control *);
88         
89 protected:
90         /// Only for use by DummyPort
91         SurfacePort();
92
93         virtual void control_event (SurfacePort &, Control &, const ControlState &) {}
94         
95 private:
96         bool control_in_use_timeout (Control *, Control *);
97
98         MIDI::Port * _input_port;
99         MIDI::Port * _output_port;
100         int _number;
101         bool _active;
102
103         Glib::RecMutex _rwlock;
104 };      
105
106 std::ostream & operator << ( std::ostream & , const SurfacePort & port );
107
108 }
109
110 #endif