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