2 * Copyright 2006, Haiku.
4 * Copyright (c) 2002-2003 Matthijs Hollemans
5 * Distributed under the terms of the MIT License.
12 #include <MidiEndpoint.h>
13 #include <MidiRoster.h>
14 #include "MidiRosterLooper.h"
19 BMidiEndpoint::Name() const
21 const char* str
= NULL
;
23 // It seems reasonable to assume that the pointer returned by
24 // BString::String() can change when the string is modified,
25 // e.g. to allocate more space. That's why we lock here too.
37 BMidiEndpoint::SetName(const char* newName
)
39 if (newName
== NULL
) {
40 WARN("SetName() does not accept a NULL name");
44 WARN("SetName() is not allowed on remote endpoints");
50 if (fName
!= newName
) {
52 msg
.AddString("midi:name", newName
);
54 if (SendChangeRequest(&msg
) == B_OK
) {
65 BMidiEndpoint::ID() const
72 BMidiEndpoint::IsProducer() const
79 BMidiEndpoint::IsConsumer() const
86 BMidiEndpoint::IsRemote() const
93 BMidiEndpoint::IsLocal() const
100 BMidiEndpoint::IsPersistent() const
107 BMidiEndpoint::IsValid() const
113 return IsRegistered();
118 BMidiEndpoint::Release()
120 int32 old
= atomic_add(&fRefCount
, -1);
122 TRACE(("BMidiEndpoint::Release refCount is now %" B_PRId32
, old
- 1))
125 // If the reference count of a local endpoint drops to zero,
126 // we must delete it. The destructor of BMidiLocalXXX calls
127 // BMidiRoster::DeleteLocal(), which does all the hard work.
128 // If we are a proxy for a remote endpoint, we must only be
129 // deleted if that remote endpoint no longer exists.
131 if (IsLocal() || !fIsAlive
)
133 } else if (old
<= 0) {
134 debugger("too many calls to Release()");
142 BMidiEndpoint::Acquire()
147 atomic_add(&fRefCount
, 1);
149 TRACE(("BMidiEndpoint::Acquire refCount is now %" B_PRId32
, old
+ 1))
156 BMidiEndpoint::SetProperties(const BMessage
* properties_
)
158 if (properties_
== NULL
) {
159 WARN("SetProperties() does not accept a NULL message")
161 } else if (IsRemote()) {
162 WARN("SetProperties() is not allowed on remote endpoints");
164 } else if (!IsValid()) {
168 msg
.AddMessage("midi:properties", properties_
);
170 status_t err
= SendChangeRequest(&msg
);
173 *fProperties
= *properties_
;
184 BMidiEndpoint::GetProperties(BMessage
* _properties
) const
186 if (_properties
== NULL
) {
187 WARN("GetProperties() does not accept NULL properties")
192 *_properties
= *fProperties
;
201 BMidiEndpoint::Register()
204 WARN("You cannot Register() remote endpoints");
207 if (IsRegistered()) {
208 WARN("This endpoint is already registered");
214 return SendRegisterRequest(true);
219 BMidiEndpoint::Unregister()
222 WARN("You cannot Unregister() remote endpoints");
225 if (!IsRegistered()) {
226 WARN("This endpoint is already unregistered");
232 return SendRegisterRequest(false);
236 BMidiEndpoint::BMidiEndpoint(const char* name_
)
238 TRACE(("BMidiEndpoint::BMidiEndpoint"))
246 fIsRegistered
= false;
249 fProperties
= new BMessage();
253 BMidiEndpoint::~BMidiEndpoint()
255 TRACE(("BMidiEndpoint::~BMidiEndpoint (%p)", this))
259 "you should use Release() to dispose of endpoints; "
260 "do not \"delete\" them or allocate them on the stack!");
265 //------------------------------------------------------------------------------
267 void BMidiEndpoint::_Reserved1() { }
268 void BMidiEndpoint::_Reserved2() { }
269 void BMidiEndpoint::_Reserved3() { }
270 void BMidiEndpoint::_Reserved4() { }
271 void BMidiEndpoint::_Reserved5() { }
272 void BMidiEndpoint::_Reserved6() { }
273 void BMidiEndpoint::_Reserved7() { }
274 void BMidiEndpoint::_Reserved8() { }
276 //------------------------------------------------------------------------------
279 BMidiEndpoint::SendRegisterRequest(bool registered
)
282 msg
.AddBool("midi:registered", registered
);
284 status_t err
= SendChangeRequest(&msg
);
287 fIsRegistered
= registered
;
297 BMidiEndpoint::SendChangeRequest(BMessage
* msg
)
301 msg
->what
= MSG_CHANGE_ENDPOINT
;
302 msg
->AddInt32("midi:id", ID());
305 status_t err
= BMidiRoster::MidiRoster()->SendRequest(msg
, &reply
);
310 if (reply
.FindInt32("midi:result", &res
) == B_OK
)
318 BMidiEndpoint::IsRegistered() const
320 // No need to protect this with a lock, because reading
321 // and writing a bool is always an atomic operation.
323 return fIsRegistered
;
328 BMidiEndpoint::LockLooper() const
330 return BMidiRoster::MidiRoster()->fLooper
->Lock();
335 BMidiEndpoint::UnlockLooper() const
337 BMidiRoster::MidiRoster()->fLooper
->Unlock();