2 * Copyright 2006, Haiku.
4 * Copyright (c) 2002-2003 Matthijs Hollemans
5 * Distributed under the terms of the MIT License.
12 #include <MidiConsumer.h>
13 #include <MidiProducer.h>
14 #include <MidiRoster.h>
15 #include "MidiRosterLooper.h"
20 BMidiProducer::Connect(BMidiConsumer
* cons
)
23 WARN("Connect() does not accept a NULL consumer")
26 if (!IsValid() || !cons
->IsValid()) {
29 return SendConnectRequest(cons
, true);
34 BMidiProducer::Disconnect(BMidiConsumer
* cons
)
37 WARN("Disconnect() does not accept a NULL consumer")
40 if (!IsValid() || !cons
->IsValid()) {
43 return SendConnectRequest(cons
, false);
48 BMidiProducer::IsConnected(BMidiConsumer
* cons
) const
50 bool isConnected
= false;
54 isConnected
= fConnections
->HasItem(cons
);
64 BMidiProducer::Connections() const
66 BList
* list
= new BList();
69 for (int32 t
= 0; t
< CountConsumers(); ++t
) {
70 BMidiConsumer
* cons
= ConsumerAt(t
);
82 BMidiProducer::BMidiProducer(const char* name
)
83 : BMidiEndpoint(name
),
84 fLocker("MidiProducerLock")
87 fConnections
= new BList();
91 BMidiProducer::~BMidiProducer()
97 void BMidiProducer::_Reserved1() { }
98 void BMidiProducer::_Reserved2() { }
99 void BMidiProducer::_Reserved3() { }
100 void BMidiProducer::_Reserved4() { }
101 void BMidiProducer::_Reserved5() { }
102 void BMidiProducer::_Reserved6() { }
103 void BMidiProducer::_Reserved7() { }
104 void BMidiProducer::_Reserved8() { }
108 BMidiProducer::SendConnectRequest(
109 BMidiConsumer
* cons
, bool mustConnect
)
116 msg
.what
= MSG_CONNECT_ENDPOINTS
;
118 msg
.what
= MSG_DISCONNECT_ENDPOINTS
;
121 msg
.AddInt32("midi:producer", ID());
122 msg
.AddInt32("midi:consumer", cons
->ID());
124 status_t err
= BMidiRoster::MidiRoster()->SendRequest(&msg
, &reply
);
129 if (reply
.FindInt32("midi:result", &res
) == B_OK
) {
132 ConnectionMade(cons
);
134 ConnectionBroken(cons
);
138 BMidiRoster::MidiRoster()->fLooper
->DumpEndpoints();
150 BMidiProducer::ConnectionMade(BMidiConsumer
* consumer
)
152 if (consumer
== NULL
)
155 if (LockProducer()) {
156 ASSERT(!fConnections
->HasItem(consumer
))
158 fConnections
->AddItem(consumer
);
163 ((BMidiLocalProducer
*) this)->Connected(consumer
);
169 BMidiProducer::ConnectionBroken(BMidiConsumer
* consumer
)
171 if (consumer
== NULL
)
174 bool wasConnected
= false;
176 if (LockProducer()) {
177 wasConnected
= fConnections
->RemoveItem(consumer
);
181 if (wasConnected
&& IsLocal()) {
182 ((BMidiLocalProducer
*) this)->Disconnected(consumer
);
190 BMidiProducer::CountConsumers() const
192 return fConnections
->CountItems();
197 BMidiProducer::ConsumerAt(int32 index
) const
199 ASSERT(index
>= 0 && index
< CountConsumers())
201 return (BMidiConsumer
*) fConnections
->ItemAt(index
);
206 BMidiProducer::LockProducer() const
208 return fLocker
.Lock();
213 BMidiProducer::UnlockProducer() const