Merge remote-tracking branch 'remotes/origin/cairocanvas' into windows
[ardour.git] / libs / ardour / ardour / async_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  __libardour_async_midiport_h__
20 #define  __libardour_async_midiport_h__
21
22 #include <string>
23 #include <iostream>
24
25 #include "pbd/xml++.h"
26 #include "pbd/crossthread.h"
27 #include "pbd/signals.h"
28 #include "pbd/ringbuffer.h"
29
30 #include "evoral/Event.hpp"
31 #include "evoral/EventRingBuffer.hpp"
32
33 #include "midi++/types.h"
34 #include "midi++/parser.h"
35 #include "midi++/port.h"
36
37 #include "ardour/midi_port.h"
38
39 namespace ARDOUR {
40
41         class AsyncMIDIPort : public ARDOUR::MidiPort, public MIDI::Port {
42
43   public:
44         AsyncMIDIPort (std::string const &, PortFlags);
45         ~AsyncMIDIPort ();
46
47         /* called from an RT context */
48
49         void cycle_start (pframes_t nframes);
50         void cycle_end (pframes_t nframes);
51     
52         /* called from non-RT context */
53     
54         void parse (framecnt_t timestamp);
55         int write (const MIDI::byte *msg, size_t msglen, MIDI::timestamp_t timestamp);
56         int read (MIDI::byte *buf, size_t bufsize);
57         void drain (int check_interval_usecs);
58         int selectable () const {
59 #ifdef PLATFORM_WINDOWS
60                 return false;
61 #else
62                 return xthread.selectable();
63 #endif
64         }
65
66         static void set_process_thread (pthread_t);
67         static pthread_t get_process_thread () { return _process_thread; }
68         static bool is_process_thread();
69
70   private:      
71         bool                    _currently_in_cycle;
72         MIDI::timestamp_t       _last_write_timestamp;
73         RingBuffer< Evoral::Event<double> > output_fifo;
74         Evoral::EventRingBuffer<MIDI::timestamp_t> input_fifo;
75         Glib::Threads::Mutex output_fifo_lock;
76 #ifndef PLATFORM_WINDOWS
77         CrossThreadChannel xthread;
78 #endif
79
80         int create_port ();
81
82         /** Channel used to signal to the MidiControlUI that input has arrived */
83         
84         std::string _connections;
85         PBD::ScopedConnection connect_connection;
86         PBD::ScopedConnection halt_connection;
87         void flush (void* jack_port_buffer);
88         void jack_halted ();
89         void make_connections ();
90         void init (std::string const &, Flags);
91
92         void flush_output_fifo (pframes_t);
93
94         static pthread_t _process_thread;
95 };
96
97 } // namespace ARDOUR
98
99 #endif /* __libardour_async_midiport_h__ */