major design changes: use glib event loop for MIDI thread/UI; rework design of BaseUI...
[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 "pbd/pthread_utils.h"
22
23 #include "midi++/manager.h"
24 #include "midi++/port.h"
25
26 #include "ardour/debug.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/midi_ui.h"
29 #include "ardour/session.h"
30 #include "ardour/session_event.h"
31 #include "ardour/types.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35 using namespace Glib;
36
37 #include "i18n.h"
38
39 BaseUI::RequestType MidiControlUI::PortChange = BaseUI::new_request_type();
40
41 #include "pbd/abstract_ui.cc"  /* instantiate the template */
42
43 MidiControlUI::MidiControlUI (Session& s)
44         : AbstractUI<MidiUIRequest> (_("midiui"))
45         , _session (s) 
46 {
47         MIDI::Manager::instance()->PortsChanged.connect (mem_fun (*this, &MidiControlUI::change_midi_ports));
48 }
49
50 MidiControlUI::~MidiControlUI ()
51 {
52         clear_ports ();
53 }
54
55 void
56 MidiControlUI::do_request (MidiUIRequest* req)
57 {
58         if (req->type == PortChange) {
59
60                 /* restart event loop with new ports */
61                 DEBUG_TRACE (DEBUG::MidiIO, "reset ports\n");
62                 reset_ports ();
63
64         } else if (req->type == CallSlot) {
65
66                 req->the_slot ();
67         }
68 }
69
70 void
71 MidiControlUI::change_midi_ports ()
72 {
73         MidiUIRequest* req = get_request (PortChange);
74         if (req == 0) {
75                 return;
76         }
77         send_request (req);
78 }
79
80 bool
81 MidiControlUI::midi_input_handler (IOCondition ioc, MIDI::Port* port)
82 {
83         if (ioc & ~IO_IN) {
84                 return false;
85         }
86
87         if (ioc & IO_IN) {
88
89                 if (port->must_drain_selectable()) {
90                         CrossThreadChannel::drain (port->selectable());
91                 }
92
93                 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", port->name()));
94                 nframes64_t now = _session.engine().frame_time();
95                 port->parse (now);
96         }
97
98         return true;
99 }
100
101 void
102 MidiControlUI::clear_ports ()
103 {
104         for (PortSources::iterator i = port_sources.begin(); i != port_sources.end(); ++i) {
105                 /* remove existing sources from the event loop */
106                 (*i)->destroy ();
107         }
108
109         port_sources.clear ();
110 }
111
112 void
113 MidiControlUI::reset_ports ()
114 {
115         clear_ports ();
116
117         MIDI::Manager::PortList plist = MIDI::Manager::instance()->get_midi_ports ();
118
119         for (MIDI::Manager::PortList::iterator i = plist.begin(); i != plist.end(); ++i) {
120                 int fd;
121                 if ((fd = (*i)->selectable ()) >= 0) {
122                         Glib::RefPtr<IOSource> psrc = IOSource::create (fd, IO_IN|IO_HUP|IO_ERR);
123                         psrc->connect (bind (mem_fun (*this, &MidiControlUI::midi_input_handler), (*i)));
124                         port_sources.push_back (psrc);
125                 } 
126         }
127
128         for (PortSources::iterator i = port_sources.begin(); i != port_sources.end(); ++i) {
129                 (*i)->attach (_main_loop->get_context());
130         }
131 }
132
133 void
134 MidiControlUI::thread_init ()
135 {       
136         struct sched_param rtparam;
137
138         PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("MIDI"), 2048);
139         SessionEvent::create_per_thread_pool (X_("MIDI I/O"), 128);
140
141         memset (&rtparam, 0, sizeof (rtparam));
142         rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
143
144         if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
145                 // do we care? not particularly.
146         }
147
148         reset_ports ();
149 }
150