13
0
livetrax/libs/midi++2/manager.cc
Carl Hetherington 91850f0eb4 Remove non-JACK midi++ ports.
git-svn-id: svn://localhost/ardour2/branches/3.0@7377 d708f5d6-7413-0410-9779-e7cbd77b26cf
2010-07-06 00:16:36 +00:00

233 lines
4.0 KiB
C++

/*
Copyright (C) 1998-99 Paul Barton-Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id$
*/
#include <fcntl.h>
#include <glib.h>
#include "pbd/error.h"
#include "midi++/types.h"
#include "midi++/manager.h"
#include "midi++/channel.h"
#include "midi++/port.h"
using namespace std;
using namespace MIDI;
using namespace PBD;
/* XXX check for strdup leaks */
Manager *Manager::theManager = 0;
Manager::Manager ()
{
inputPort = 0;
outputPort = 0;
inputChannelNumber = 0;
outputChannelNumber = 0;
api_data = 0;
}
Manager::~Manager ()
{
for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
delete *p;
}
if (theManager == this) {
theManager = 0;
}
}
Port *
Manager::add_port (const XMLNode& node)
{
Port::Descriptor desc (node);
Port *port;
PortList::iterator p;
for (p = _ports.begin(); p != _ports.end(); ++p) {
if (desc.tag == (*p)->name()) {
break;
}
}
if (p != _ports.end()) {
return 0;
}
port = new Port (node, (jack_client_t *) api_data);
if (port == 0) {
return 0;
}
if (!port->ok()) {
delete port;
return 0;
}
_ports.push_back (port);
/* first port added becomes the default input
port.
*/
if (inputPort == 0) {
inputPort = port;
}
if (outputPort == 0) {
outputPort = port;
}
PortsChanged (); /* EMIT SIGNAL */
return port;
}
int
Manager::remove_port (Port* port)
{
if (inputPort == port) {
inputPort = 0;
}
if (outputPort == port) {
outputPort = 0;
}
_ports.remove (port);
delete port;
PortsChanged (); /* EMIT SIGNAL */
return 0;
}
int
Manager::set_input_port (string tag)
{
for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
if ((*p)->name() == tag) {
inputPort = (*p);
return 0;
}
}
return -1;
}
int
Manager::set_output_port (string tag)
{
PortList::iterator p;
for (p = _ports.begin(); p != _ports.end(); ++p) {
if ((*p)->name() == tag) {
inputPort = (*p);
break;
}
}
if (p == _ports.end()) {
return -1;
}
// XXX send a signal to say we're about to change output ports
if (outputPort) {
for (channel_t chan = 0; chan < 16; chan++) {
outputPort->channel (chan)->all_notes_off (0);
}
}
outputPort = (*p);
// XXX send a signal to say we've changed output ports
return 0;
}
Port *
Manager::port (string name)
{
for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
if (name == (*p)->name()) {
return (*p);
}
}
return 0;
}
int
Manager::foreach_port (int (*func)(const Port &, size_t, void *),
void *arg)
{
int n = 0;
for (PortList::const_iterator p = _ports.begin(); p != _ports.end(); ++p, ++n) {
int retval;
if ((retval = func (**p, n, arg)) != 0) {
return retval;
}
}
return 0;
}
void
Manager::cycle_start(nframes_t nframes)
{
for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
(*p)->cycle_start (nframes);
}
}
void
Manager::cycle_end()
{
for (PortList::iterator p = _ports.begin(); p != _ports.end(); ++p) {
(*p)->cycle_end ();
}
}
/** Re-register ports that disappear on JACK shutdown */
void
Manager::reestablish (void* a)
{
for (PortList::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
(*p)->reestablish (a);
}
}
/** Re-connect ports after a reestablish () */
void
Manager::reconnect ()
{
for (PortList::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
(*p)->reconnect ();
}
}