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