6d3e3341bc16f8d1ae3c6cf3d1447fbf2eb5d73a
[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 <pbd/ringbuffer.h>
34 #include <jack/jack.h>
35 #include <jack/midiport.h>
36 #include <midi++/port.h>
37 #include <midi++/event.h>
38
39 namespace MIDI
40 {
41
42
43 class JACK_MidiPort : public Port
44 {
45 public:
46         JACK_MidiPort (const XMLNode& node, jack_client_t* jack_client);
47         virtual ~JACK_MidiPort ();
48
49         int write(byte *msg, size_t msglen, timestamp_t timestamp);
50         int read(byte *buf, size_t max);
51
52         /* No select(2)/poll(2)-based I/O */
53         virtual int selectable() const { return -1; }
54         
55         virtual void cycle_start(nframes_t nframes);
56
57         static std::string typestring;
58
59         virtual XMLNode& get_state () const;
60         virtual void set_state (const XMLNode&);
61
62         static void set_process_thread (pthread_t);
63
64   protected:
65         std::string get_typestring () const {
66                 return typestring;
67         }
68
69 private:
70         int create_ports(const XMLNode&);
71
72         jack_client_t* _jack_client;
73         jack_port_t*   _jack_input_port;
74         jack_port_t*   _jack_output_port;
75         nframes_t      _last_read_index;
76         timestamp_t    _last_write_timestamp;
77
78         void flush (void* jack_port_buffer);
79
80         static pthread_t _process_thread;
81         static bool is_process_thread();
82
83         RingBuffer<MIDI::Event> non_process_thread_fifo;
84         Glib::Mutex non_process_thread_fifo_lock;
85 };
86
87
88 } /* namespace MIDI */
89
90 #endif // __jack_midiport_h__
91