remove PBD::Connection (replace use with PBD::ScopedConnection); remove limitation...
[ardour.git] / libs / midi++2 / midi++ / jack.h
1 /*
2     Copyright (C) 2006 Paul Davis 
3         Written by Dave Robillard
4  
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9  
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14  
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  
19     $Id: jack.h 4 2005-05-13 20:47:18Z taybin $
20 */
21
22 #ifndef __jack_midiport_h__
23 #define __jack_midiport_h__
24
25 #include <vector>
26 #include <string>
27
28 #include <fcntl.h>
29 #include <unistd.h>
30
31 #include <glibmm/thread.h>
32
33 #include <jack/jack.h>
34 #include <jack/midiport.h>
35
36 #include "pbd/ringbuffer.h"
37 #include "pbd/signals.h"
38 #include "pbd/crossthread.h"
39 #include "evoral/EventRingBuffer.hpp"
40
41 #include "midi++/port.h"
42 #include "midi++/event.h"
43
44 namespace MIDI
45 {
46
47 class JACK_MidiPort : public Port
48 {
49 public:
50         JACK_MidiPort (const XMLNode& node, jack_client_t* jack_client);
51         virtual ~JACK_MidiPort ();
52
53         int write(byte *msg, size_t msglen, timestamp_t timestamp);
54         int read(byte *buf, size_t max);
55
56         int selectable() const { return xthread.selectable(); }
57         bool must_drain_selectable() const { return true; }
58         
59         void cycle_start(nframes_t nframes);
60         void cycle_end();
61
62         static std::string typestring;
63
64         XMLNode& get_state () const;
65         void set_state (const XMLNode&);
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         nframes_t nframes_this_cycle() const {  return _nframes_this_cycle; }
72
73         static PBD::Signal0<void> MakeConnections;
74         static PBD::Signal0<void> JackHalted;
75
76   protected:
77         std::string get_typestring () const {
78                 return typestring;
79         }
80
81 private:
82         int create_ports(const XMLNode&);
83
84         jack_client_t* _jack_client;
85         jack_port_t*   _jack_input_port;
86         jack_port_t*   _jack_output_port;
87         nframes_t      _last_read_index;
88         timestamp_t    _last_write_timestamp;
89         CrossThreadChannel xthread;
90         std::string    _inbound_connections;
91         std::string    _outbound_connections;
92         PBD::ScopedConnection connect_connection;
93         PBD::ScopedConnection halt_connection;
94         void flush (void* jack_port_buffer);
95         void jack_halted ();
96         void make_connections();
97
98         static pthread_t _process_thread;
99
100         RingBuffer< Evoral::Event<double> > output_fifo;
101         Evoral::EventRingBuffer<timestamp_t> input_fifo;
102
103         Glib::Mutex output_fifo_lock;
104 };
105
106
107 } /* namespace MIDI */
108
109 #endif // __jack_midiport_h__
110