6b629aabd5b221ec3e71a4ffdf42f36b9d8eb867
[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         /* stop the thread */
56         quit ();
57         /* drop all ports as GIO::Sources */
58         clear_ports ();
59         /* we no longer exist */
60         _instance = 0;
61 }
62
63 void*
64 MidiControlUI::request_factory (uint32_t num_requests)
65 {
66         /* AbstractUI<T>::request_buffer_factory() is a template method only
67            instantiated in this source module. To provide something visible for
68            use when registering the factory, we have this static method that is
69            template-free.
70         */
71         return request_buffer_factory (num_requests);
72 }
73
74 void
75 MidiControlUI::do_request (MidiUIRequest* req)
76 {
77         if (req->type == Quit) {
78                 BaseUI::quit ();
79         } else if (req->type == CallSlot) {
80                 req->the_slot ();
81         }
82 }
83
84 bool
85 MidiControlUI::midi_input_handler (IOCondition ioc, boost::shared_ptr<AsyncMIDIPort> port)
86 {
87         DEBUG_TRACE (DEBUG::MidiIO, string_compose ("something happend on  %1\n", boost::shared_ptr<ARDOUR::Port> (port)->name()));
88         cout << "Something happened on " << boost::shared_ptr<ARDOUR::Port> (port)->name() << " ioc = " << hex << ioc << dec << endl;
89         if (ioc & ~IO_IN) {
90                 cout << "....no input ... do nothing" << endl;
91                 return false;
92         }
93
94         if (ioc & IO_IN) {
95
96                 port->clear ();
97                 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", boost::shared_ptr<ARDOUR::Port>(port)->name()));
98                 cout << "....input appears ready\n";
99                 framepos_t now = _session.engine().sample_time();
100                 port->parse (now);
101         }
102
103         return true;
104 }
105
106 void
107 MidiControlUI::clear_ports ()
108 {
109 }
110
111 void
112 MidiControlUI::reset_ports ()
113 {
114         vector<boost::shared_ptr<AsyncMIDIPort> > ports;
115         boost::shared_ptr<AsyncMIDIPort> p;
116
117         if ((p = boost::dynamic_pointer_cast<AsyncMIDIPort> (_session.midi_input_port()))) {
118                 ports.push_back (p);
119         }
120
121
122         if ((p = boost::dynamic_pointer_cast<AsyncMIDIPort> (_session.mmc_input_port()))) {
123                 ports.push_back (p);
124         }
125
126         if ((p = boost::dynamic_pointer_cast<AsyncMIDIPort> (_session.scene_input_port()))) {
127                 ports.push_back (p);
128         }
129
130         if (ports.empty()) {
131                 return;
132         }
133
134         for (vector<boost::shared_ptr<AsyncMIDIPort> >::const_iterator pi = ports.begin(); pi != ports.end(); ++pi) {
135                 (*pi)->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &MidiControlUI::midi_input_handler), *pi));
136                 (*pi)->xthread().attach (_main_loop->get_context());
137         }
138 }
139
140 void
141 MidiControlUI::thread_init ()
142 {
143         struct sched_param rtparam;
144
145         pthread_set_name (X_("midiUI"));
146
147         PBD::notify_event_loops_about_thread_creation (pthread_self(), X_("midiUI"), 2048);
148         SessionEvent::create_per_thread_pool (X_("midiUI"), 128);
149
150         memset (&rtparam, 0, sizeof (rtparam));
151         rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
152
153         if (pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam) != 0) {
154                 // do we care? not particularly.
155         }
156
157         reset_ports ();
158 }
159