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