ardour
midi_ui.cc
Go to the documentation of this file.
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"
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 
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
65 {
66  if (req->type == Quit) {
67  BaseUI::quit ();
68  } else if (req->type == CallSlot) {
69  req->the_slot ();
70  }
71 }
72 
73 bool
75 {
76  DEBUG_TRACE (DEBUG::MidiIO, string_compose ("something happend on %1\n", ((ARDOUR::Port*)port)->name()));
77 
78  if (ioc & ~IO_IN) {
79  return false;
80  }
81 
82  if (ioc & IO_IN) {
83 
84  AsyncMIDIPort* asp = dynamic_cast<AsyncMIDIPort*> (port);
85  if (asp) {
86  asp->clear ();
87  }
88 
89  DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", ((ARDOUR::Port*)port)->name()));
91  port->parse (now);
92  }
93 
94  return true;
95 }
96 
97 void
99 {
100 }
101 
102 void
104 {
105  vector<AsyncMIDIPort*> ports;
106  AsyncMIDIPort* p;
107 
108  if ((p = dynamic_cast<AsyncMIDIPort*> (_session.midi_input_port()))) {
109  ports.push_back (p);
110  }
111 
112 
113  if ((p = dynamic_cast<AsyncMIDIPort*> (_session.mmc_input_port()))) {
114  ports.push_back (p);
115  }
116 
117  if ((p = dynamic_cast<AsyncMIDIPort*> (_session.scene_input_port()))) {
118  ports.push_back (p);
119  }
120 
121  if (ports.empty()) {
122  return;
123  }
124 
125  for (vector<AsyncMIDIPort*>::const_iterator pi = ports.begin(); pi != ports.end(); ++pi) {
126  (*pi)->xthread().set_receive_handler (sigc::bind (sigc::mem_fun (this, &MidiControlUI::midi_input_handler), *pi));
127  (*pi)->xthread().attach (_main_loop->get_context());
128  }
129 }
130 
131 void
133 {
134  struct sched_param rtparam;
135 
136  pthread_set_name (X_("midiUI"));
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 
static RequestType CallSlot
Definition: base_ui.h:62
std::string name() const
Definition: base_ui.h:57
void quit()
Definition: base_ui.cc:114
static void create_per_thread_pool(const std::string &n, uint32_t nitems)
LIBARDOUR_API uint64_t MidiIO
Definition: debug.cc:44
Definition: Beats.hpp:239
LIBPBD_API void notify_gui_about_thread_creation(std::string, pthread_t, std::string, int requests=256)
void do_request(MidiUIRequest *)
Definition: midi_ui.cc:64
framepos_t sample_time()
ARDOUR::Session & _session
Definition: midi_ui.h:62
#define X_(Text)
Definition: i18n.h:13
void parse(framecnt_t timestamp)
Definition: amp.h:29
#define DEBUG_TRACE(bits, str)
Definition: debug.h:55
int64_t framepos_t
Definition: types.h:66
LIBPBD_API void pthread_set_name(const char *name)
MIDI::Port * midi_input_port() const
MIDI::Port * scene_input_port() const
static RequestType Quit
Definition: base_ui.h:63
Definition: debug.h:30
static MidiControlUI * _instance
Definition: midi_ui.h:68
MIDI::Port * mmc_input_port() const
bool midi_input_handler(Glib::IOCondition, AsyncMIDIPort *)
Definition: midi_ui.cc:74
CrossThreadChannel & xthread()
void set_receive_handler(sigc::slot< bool, Glib::IOCondition > s)
Definition: crossthread.cc:53
AudioEngine & engine()
Definition: session.h:546
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208
Glib::RefPtr< Glib::MainLoop > _main_loop
Definition: base_ui.h:77