band-aid fix for wrong-thread-MIDI-writes
[ardour.git] / libs / midi++2 / jack_midiport.cc
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
20 #include <fcntl.h>
21 #include <cerrno>
22 #include <cassert>
23
24 #include <pbd/error.h>
25
26 #include <midi++/types.h>
27 #include <midi++/jack.h>
28
29 using namespace std;
30 using namespace MIDI;
31 using namespace PBD;
32
33 JACK_MidiPort::JACK_MidiPort(const XMLNode& node, jack_client_t* jack_client)
34         : Port(node)
35         , _jack_client(jack_client)
36         , _jack_input_port(NULL)
37         , _jack_output_port(NULL)
38         , _last_read_index(0)
39 {
40         int err = create_ports (node);
41
42         if (!err) {
43                 _ok = true;
44         } 
45 }
46
47 JACK_MidiPort::~JACK_MidiPort()
48 {
49         // FIXME: remove port
50 }
51
52 void
53 JACK_MidiPort::cycle_start (nframes_t nframes)
54 {
55         Port::cycle_start(nframes);
56         assert(_nframes_this_cycle == nframes);
57         _last_read_index = 0;
58         jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes));
59 }
60
61 int
62 JACK_MidiPort::write(byte * msg, size_t msglen, timestamp_t timestamp)
63 {
64         if (!_currently_in_cycle) {
65                 error << "JACK MIDI write ignored - not in cycle ... FIX ME PAUL!" << endmsg;
66                 return msglen;
67         }
68         assert(timestamp < _nframes_this_cycle);
69         assert(_jack_output_port);
70
71         // FIXME: return value correct?
72         return jack_midi_event_write (
73                 jack_port_get_buffer(_jack_output_port, _nframes_this_cycle),
74                 timestamp, msg, msglen);
75 }
76
77 int
78 JACK_MidiPort::read(byte * buf, size_t max, timestamp_t timestamp)
79 {
80         assert(_currently_in_cycle);
81         assert(timestamp < _nframes_this_cycle);
82         assert(_jack_input_port);
83
84         jack_midi_event_t ev;
85
86         int err = jack_midi_event_get (&ev,
87                 jack_port_get_buffer(_jack_input_port, _nframes_this_cycle),
88                 _last_read_index++);
89         
90         if (!err) {
91                 memcpy(buf, ev.buffer, ev.size);
92                 return ev.size;
93         } else {
94                 return 0;
95         }
96 }
97
98 int
99 JACK_MidiPort::create_ports(const XMLNode& node)
100 {
101         Descriptor desc (node);
102
103         assert(!_jack_input_port);
104         assert(!_jack_output_port);
105         
106         jack_nframes_t nframes = jack_get_buffer_size(_jack_client);
107
108         bool ret = true;
109
110         if (desc.mode == O_RDWR || desc.mode == O_WRONLY) {
111                 _jack_output_port = jack_port_register(_jack_client,
112                                                        string(desc.tag).append("_out").c_str(),
113                                                        JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
114                 jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes));
115                 ret = ret && (_jack_output_port != NULL);
116         }
117         
118         if (desc.mode == O_RDWR || desc.mode == O_RDONLY) {
119                 _jack_input_port = jack_port_register(_jack_client,
120                                                       string(desc.tag).append("_in").c_str(),
121                                                       JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
122                 jack_midi_clear_buffer(jack_port_get_buffer(_jack_input_port, nframes));
123                 ret = ret && (_jack_input_port != NULL);
124         }
125
126         return ret ? 0 : -1;
127 }
128
129 XMLNode& 
130 JACK_MidiPort::get_state () const
131 {
132         XMLNode& root (Port::get_state ());
133         return root;
134 }
135
136 void
137 JACK_MidiPort::set_state (const XMLNode& node)
138 {
139 }