Setup fixed ports for MIDI control data; hence remove configuration of those ports...
[ardour.git] / libs / midi++2 / midi++ / port.h
1 /*
2     Copyright (C) 1998-2010 Paul Barton-Davis 
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2 of the License, or
6     (at your option) any later version.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
17 */
18
19 #ifndef  __libmidi_port_h__
20 #define  __libmidi_port_h__
21
22 #include <string>
23 #include <iostream>
24
25 #include <jack/types.h>
26
27 #include "pbd/xml++.h"
28 #include "pbd/crossthread.h"
29 #include "pbd/signals.h"
30 #include "pbd/ringbuffer.h"
31
32 #include "evoral/Event.hpp"
33 #include "evoral/EventRingBuffer.hpp"
34
35 #include "midi++/types.h"
36 #include "midi++/parser.h"
37
38 namespace MIDI {
39
40 class Channel;
41 class PortRequest;
42
43 class Port {
44   public:
45         Port (std::string const &, int, jack_client_t *);
46         Port (const XMLNode&, jack_client_t *);
47         ~Port ();
48
49         XMLNode& get_state () const;
50         void set_state (const XMLNode&);
51
52         // FIXME: make Manager a friend of port so these can be hidden?
53
54         /* Only for use by MidiManager.  Don't ever call this. */
55         void cycle_start(nframes_t nframes);
56         /* Only for use by MidiManager.  Don't ever call this. */
57         void cycle_end();
58
59         /** Write a message to port.
60          * @param msg Raw MIDI message to send
61          * @param msglen Size of @a msg
62          * @param timestamp Time stamp in frames of this message (relative to cycle start)
63          * @return number of bytes successfully written
64          */
65         int write (byte *msg, size_t msglen, timestamp_t timestamp);
66
67         /** Read raw bytes from a port.
68          * @param buf memory to store read data in
69          * @param bufsize size of @a buf
70          * @return number of bytes successfully read, negative if error
71          */
72         int read (byte *buf, size_t bufsize);
73
74         void parse (nframes_t timestamp);
75         
76         /** Write a message to port.
77          * @return true on success.
78          * FIXME: describe semantics here
79          */
80         int midimsg (byte *msg, size_t len, timestamp_t timestamp) {
81                 return !(write (msg, len, timestamp) == (int) len);
82         } 
83
84         bool clock (timestamp_t timestamp);
85
86         /* select(2)/poll(2)-based I/O */
87
88         /** Get the file descriptor for port.
89          * @return File descriptor, or -1 if not selectable. 
90          */
91         int selectable () const {
92                 return xthread.selectable();
93         }
94
95         Channel *channel (channel_t chn) { 
96                 return _channel[chn&0x7F];
97         }
98         
99         Parser *input()     { return input_parser; }
100         Parser *output()    { return output_parser; }
101         
102         const char *name () const   { return _tagname.c_str(); }
103         int    mode () const        { return _mode; }
104         bool   ok ()   const        { return _ok; }
105
106         struct Descriptor {
107             std::string tag;
108             int mode;
109
110             Descriptor (const XMLNode&);
111             XMLNode& get_state();
112         };
113
114         nframes_t nframes_this_cycle() const {  return _nframes_this_cycle; }
115
116         void reestablish (void *);
117         void reconnect ();
118
119         static void set_process_thread (pthread_t);
120         static pthread_t get_process_thread () { return _process_thread; }
121         static bool is_process_thread();
122         
123         static PBD::Signal0<void> MakeConnections;
124         static PBD::Signal0<void> JackHalted;
125
126 private:        
127         bool             _ok;
128         bool             _currently_in_cycle;
129         nframes_t        _nframes_this_cycle;
130         std::string      _tagname;
131         int              _mode;
132         size_t           _number;
133         Channel          *_channel[16];
134         Parser           *input_parser;
135         Parser           *output_parser;
136
137         static size_t nports;
138
139         void create_port_names ();
140         int create_ports ();
141
142         jack_client_t* _jack_client;
143         std::string    _jack_input_port_name; /// input port name, or empty if there isn't one
144         jack_port_t*   _jack_input_port;
145         std::string    _jack_output_port_name; /// output port name, or empty if there isn't one
146         jack_port_t*   _jack_output_port;
147         nframes_t      _last_read_index;
148         timestamp_t    _last_write_timestamp;
149
150         /** Channel used to signal to the MidiControlUI that input has arrived */
151         CrossThreadChannel xthread;
152         
153         std::string    _inbound_connections;
154         std::string    _outbound_connections;
155         PBD::ScopedConnection connect_connection;
156         PBD::ScopedConnection halt_connection;
157         void flush (void* jack_port_buffer);
158         void jack_halted ();
159         void make_connections();
160         void init (std::string const &, int);
161
162         static pthread_t _process_thread;
163
164         RingBuffer< Evoral::Event<double> > output_fifo;
165         Evoral::EventRingBuffer<timestamp_t> input_fifo;
166
167         Glib::Mutex output_fifo_lock;
168         
169 };
170
171 struct PortSet {
172     PortSet (std::string str) : owner (str) { }
173     
174     std::string owner;
175     std::list<XMLNode> ports;
176 };
177
178 std::ostream & operator << ( std::ostream & os, const Port & port );
179
180 } // namespace MIDI
181
182 #endif // __libmidi_port_h__