fix MMC
[ardour.git] / libs / ardour / midi_ui.cc
1 /*
2   Copyright (C) 2009 Paul Davis
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19 #include <cstdlib>
20
21 #include <sigc++/signal.h>
22
23 #include "pbd/pthread_utils.h"
24
25 #include "ardour/async_midi_port.h"
26 #include "ardour/debug.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/midi_port.h"
29 #include "ardour/midiport_manager.h"
30 #include "ardour/midi_ui.h"
31 #include "ardour/session.h"
32 #include "ardour/session_event.h"
33 #include "ardour/types.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38 using namespace Glib;
39
40 #include "i18n.h"
41
42 MidiControlUI* MidiControlUI::_instance = 0;
43
44 #include "pbd/abstract_ui.cc"  /* instantiate the template */
45
46 MidiControlUI::MidiControlUI (Session& s)
47         : AbstractUI<MidiUIRequest> (X_("midiui"))
48         , _session (s)
49 {
50         _instance = this;
51 }
52
53 MidiControlUI::~MidiControlUI ()
54 {
55         clear_ports ();
56         _instance = 0;
57 }
58
59 void
60 MidiControlUI::do_request (MidiUIRequest* req)
61 {
62         if (req->type == Quit) {
63                 BaseUI::quit ();
64         }
65 }
66
67 bool
68 MidiControlUI::midi_input_handler (IOCondition ioc, AsyncMIDIPort* port)
69 {
70         DEBUG_TRACE (DEBUG::MidiIO, string_compose ("something happend on  %1\n", ((ARDOUR::Port*)port)->name()));
71
72         if (ioc & ~IO_IN) {
73                 return false;
74         }
75
76         if (ioc & IO_IN) {
77
78                 CrossThreadChannel::drain (port->selectable());
79
80                 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", ((ARDOUR::Port*)port)->name()));
81                 framepos_t now = _session.engine().sample_time();
82                 port->parse (now);
83         }
84
85         return true;
86 }
87
88 void
89 MidiControlUI::clear_ports ()
90 {
91         for (PortSources::iterator i = port_sources.begin(); i != port_sources.end(); ++i) {
92                 g_source_destroy (*i);
93                 g_source_unref (*i);
94         }
95
96         port_sources.clear ();
97 }
98
99 void
100 MidiControlUI::reset_ports ()
101 {
102         if (!port_sources.empty()) {
103                 return;
104         }
105         
106         vector<AsyncMIDIPort*> ports;
107         AsyncMIDIPort* p;
108         
109         if ((p = dynamic_cast<AsyncMIDIPort*> (_session.midi_input_port()))) {
110                 ports.push_back (p);
111         }
112         
113         
114         if ((p = dynamic_cast<AsyncMIDIPort*> (_session.mmc_input_port()))) {
115                 ports.push_back (p);
116         }
117         
118         if (ports.empty()) {
119                 return;
120         }
121         
122         int fd;
123         for (vector<AsyncMIDIPort*>::const_iterator pi = ports.begin(); pi != ports.end(); ++pi) {
124
125                 if ((fd = (*pi)->selectable ()) >= 0) {
126                         Glib::RefPtr<IOSource> psrc = IOSource::create (fd, IO_IN|IO_HUP|IO_ERR);
127                         
128                         psrc->connect (sigc::bind (sigc::mem_fun (this, &MidiControlUI::midi_input_handler), *pi));
129                         psrc->attach (_main_loop->get_context());
130                         
131                         // glibmm hack: for now, store only the GSource*
132                         
133                         port_sources.push_back (psrc->gobj());
134                         g_source_ref (psrc->gobj());
135                 }
136         }
137 }
138
139 void
140 MidiControlUI::thread_init ()
141 {
142         struct sched_param rtparam;
143
144         pthread_set_name (X_("midiUI"));
145
146         PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("MIDI"), 2048);
147         SessionEvent::create_per_thread_pool (X_("MIDI I/O"), 128);
148
149         memset (&rtparam, 0, sizeof (rtparam));
150         rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
151
152         if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
153                 // do we care? not particularly.
154         }
155
156         reset_ports ();
157 }
158