libmidi++: split apart "base-y" aspects of MIDI::Port into MIDI::PortBase and make...
[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 #include "midi++/port_base.h"
38
39 namespace MIDI {
40
41 class Channel;
42 class PortRequest;
43
44 class Port : public PortBase {
45   public:
46         Port (std::string const &, PortBase::Flags, jack_client_t *);
47         Port (const XMLNode&, jack_client_t *);
48         ~Port ();
49
50         XMLNode& get_state () const;
51         void set_state (const XMLNode&);
52
53         void cycle_start (pframes_t nframes);
54         void cycle_end ();
55
56         void parse (framecnt_t timestamp);
57         int write (byte *msg, size_t msglen, timestamp_t timestamp);
58         int read (byte *buf, size_t bufsize);
59         void drain (int check_interval_usecs);
60         int selectable () const { return xthread.selectable(); }
61
62         pframes_t nframes_this_cycle() const { return _nframes_this_cycle; }
63
64         void reestablish (jack_client_t *);
65         void reconnect ();
66
67         static void set_process_thread (pthread_t);
68         static pthread_t get_process_thread () { return _process_thread; }
69         static bool is_process_thread();
70
71         static PBD::Signal0<void> MakeConnections;
72         static PBD::Signal0<void> JackHalted;
73
74 private:        
75         bool              _currently_in_cycle;
76         pframes_t         _nframes_this_cycle;
77         jack_client_t*    _jack_client;
78         jack_port_t*      _jack_port;
79         timestamp_t       _last_write_timestamp;
80         RingBuffer< Evoral::Event<double> > output_fifo;
81         Evoral::EventRingBuffer<timestamp_t> input_fifo;
82         Glib::Mutex output_fifo_lock;
83         CrossThreadChannel xthread;
84
85         int create_port ();
86
87         /** Channel used to signal to the MidiControlUI that input has arrived */
88         
89         std::string _connections;
90         PBD::ScopedConnection connect_connection;
91         PBD::ScopedConnection halt_connection;
92         void flush (void* jack_port_buffer);
93         void jack_halted ();
94         void make_connections ();
95         void init (std::string const &, Flags);
96
97         static pthread_t _process_thread;
98
99 };
100
101 } // namespace MIDI
102
103 #endif // __libmidi_port_h__