switch MIDI Clock slave code to use DEBUG_TRACE; don't make it require start/stop...
[ardour2.git] / libs / ardour / midi_ui.cc
blobfb85309ee1a7e9203d288ec97b9a2cb7919354c7
1 /*
2 Copyright (C) 2009 Paul Davis
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.
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.
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.
19 #include <cstdlib>
21 #include <sigc++/signal.h>
23 #include "pbd/pthread_utils.h"
25 #include "midi++/manager.h"
26 #include "midi++/port.h"
28 #include "ardour/debug.h"
29 #include "ardour/audioengine.h"
30 #include "ardour/midi_ui.h"
31 #include "ardour/session.h"
32 #include "ardour/session_event.h"
33 #include "ardour/types.h"
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace Glib;
39 #include "i18n.h"
41 BaseUI::RequestType MidiControlUI::PortChange = BaseUI::new_request_type();
42 MidiControlUI* MidiControlUI::_instance = 0;
44 #include "pbd/abstract_ui.cc" /* instantiate the template */
46 MidiControlUI::MidiControlUI (Session& s)
47 : AbstractUI<MidiUIRequest> (_("midiui"))
48 , _session (s)
50 MIDI::Manager::instance()->PortsChanged.connect_same_thread (rebind_connection, boost::bind (&MidiControlUI::change_midi_ports, this));
51 _instance = this;
54 MidiControlUI::~MidiControlUI ()
56 clear_ports ();
57 _instance = 0;
60 void
61 MidiControlUI::do_request (MidiUIRequest* req)
63 if (req->type == PortChange) {
65 /* restart event loop with new ports */
66 DEBUG_TRACE (DEBUG::MidiIO, "reset ports\n");
67 reset_ports ();
69 } else if (req->type == CallSlot) {
71 #ifndef NDEBUG
72 if (getenv ("DEBUG_THREADED_SIGNALS")) {
73 cerr << "MIDI UI calls a slot\n";
75 #endif
77 req->the_slot ();
79 } else if (req->type == Quit) {
81 BaseUI::quit ();
85 void
86 MidiControlUI::change_midi_ports ()
88 MidiUIRequest* req = get_request (PortChange);
89 if (req == 0) {
90 return;
92 send_request (req);
95 bool
96 MidiControlUI::midi_input_handler (IOCondition ioc, MIDI::Port* port)
98 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("something happend on %1\n", port->name()));
100 if (ioc & ~IO_IN) {
101 return false;
104 if (ioc & IO_IN) {
106 if (port->must_drain_selectable()) {
107 CrossThreadChannel::drain (port->selectable());
110 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", port->name()));
111 nframes64_t now = _session.engine().frame_time();
112 port->parse (now);
115 return true;
118 void
119 MidiControlUI::clear_ports ()
121 for (PortSources::iterator i = port_sources.begin(); i != port_sources.end(); ++i) {
122 g_source_destroy (*i);
123 g_source_unref (*i);
126 port_sources.clear ();
129 void
130 MidiControlUI::reset_ports ()
132 clear_ports ();
134 MIDI::Manager::PortList plist = MIDI::Manager::instance()->get_midi_ports ();
136 for (MIDI::Manager::PortList::iterator i = plist.begin(); i != plist.end(); ++i) {
137 int fd;
139 if ((fd = (*i)->selectable ()) >= 0) {
140 Glib::RefPtr<IOSource> psrc = IOSource::create (fd, IO_IN|IO_HUP|IO_ERR);
142 psrc->connect (sigc::bind (sigc::mem_fun (this, &MidiControlUI::midi_input_handler), *i));
143 psrc->attach (_main_loop->get_context());
145 // glibmm hack: for now, store only the GSource*
147 port_sources.push_back (psrc->gobj());
148 g_source_ref (psrc->gobj());
153 void
154 MidiControlUI::thread_init ()
156 struct sched_param rtparam;
158 char* c = new char[7];
159 strcpy (c, X_("midiUI"));
160 pthread_set_name (c);
162 cerr << "MIDI UI running\n";
164 PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("MIDI"), 2048);
165 SessionEvent::create_per_thread_pool (X_("MIDI I/O"), 128);
167 memset (&rtparam, 0, sizeof (rtparam));
168 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
170 if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
171 // do we care? not particularly.
174 reset_ports ();