2007-07-31 20:23:40 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2007, Haiku, Inc. All Rights Reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Axel Dörfler, axeld@pinc-software.de
|
|
|
|
* Ingo Weinhold, bonefish@cs.tu-berlin.de
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <Notifications.h>
|
|
|
|
|
|
|
|
|
|
|
|
NotificationManager NotificationManager::sManager;
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark - UserMessagingListener
|
|
|
|
|
|
|
|
|
|
|
|
NotificationListener::~NotificationListener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
NotificationListener::EventOccured(NotificationService& service,
|
|
|
|
const KMessage* event)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2007-08-01 18:48:44 +04:00
|
|
|
NotificationListener::AllListenersNotified(NotificationService& service)
|
2007-07-31 20:23:40 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-01 18:48:44 +04:00
|
|
|
bool
|
|
|
|
NotificationListener::operator==(const NotificationListener& other) const
|
|
|
|
{
|
|
|
|
return &other == this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-31 20:23:40 +04:00
|
|
|
// #pragma mark - UserMessagingMessageSender
|
|
|
|
|
|
|
|
|
|
|
|
UserMessagingMessageSender::UserMessagingMessageSender()
|
|
|
|
:
|
|
|
|
fMessage(NULL),
|
|
|
|
fTargetCount(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
UserMessagingMessageSender::SendMessage(const KMessage* message, port_id port,
|
|
|
|
int32 token)
|
|
|
|
{
|
|
|
|
if (message != fMessage && fMessage != NULL
|
|
|
|
|| fTargetCount == MAX_MESSAGING_TARGET_COUNT) {
|
|
|
|
FlushMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
fMessage = message;
|
|
|
|
fTargets[fTargetCount].port = port;
|
|
|
|
fTargets[fTargetCount].token = token;
|
|
|
|
fTargetCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
UserMessagingMessageSender::FlushMessage()
|
|
|
|
{
|
|
|
|
if (fMessage != NULL && fTargetCount > 0) {
|
|
|
|
send_message(fMessage->Buffer(), fMessage->ContentSize(),
|
|
|
|
fTargets, fTargetCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
fMessage = NULL;
|
|
|
|
fTargetCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark - UserMessagingListener
|
|
|
|
|
|
|
|
|
|
|
|
UserMessagingListener::UserMessagingListener(UserMessagingMessageSender& sender,
|
|
|
|
port_id port, int32 token)
|
|
|
|
:
|
|
|
|
fSender(sender),
|
|
|
|
fPort(port),
|
|
|
|
fToken(token)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UserMessagingListener::~UserMessagingListener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
UserMessagingListener::EventOccured(NotificationService& service,
|
|
|
|
const KMessage* event)
|
|
|
|
{
|
|
|
|
fSender.SendMessage(event, fPort, fToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2007-08-01 18:48:44 +04:00
|
|
|
UserMessagingListener::AllListenersNotified(NotificationService& service)
|
2007-07-31 20:23:40 +04:00
|
|
|
{
|
|
|
|
fSender.FlushMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-01 18:48:44 +04:00
|
|
|
// #pragma mark - NotificationService
|
2007-07-31 20:23:40 +04:00
|
|
|
|
|
|
|
|
|
|
|
NotificationService::~NotificationService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark - NotificationManager
|
|
|
|
|
|
|
|
|
|
|
|
/*static*/ NotificationManager&
|
|
|
|
NotificationManager::Manager()
|
|
|
|
{
|
|
|
|
return sManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*static*/ status_t
|
|
|
|
NotificationManager::CreateManager()
|
|
|
|
{
|
|
|
|
new(&sManager) NotificationManager;
|
|
|
|
return sManager._Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NotificationManager::NotificationManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NotificationManager::~NotificationManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
NotificationManager::_Init()
|
|
|
|
{
|
|
|
|
status_t status = mutex_init(&fLock, "notification manager");
|
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
return fServiceHash.InitCheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NotificationService*
|
|
|
|
NotificationManager::_ServiceFor(const char* name)
|
|
|
|
{
|
|
|
|
return fServiceHash.Lookup(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
NotificationManager::RegisterService(NotificationService& service)
|
|
|
|
{
|
|
|
|
MutexLocker _(fLock);
|
|
|
|
|
|
|
|
if (_ServiceFor(service.Name()))
|
|
|
|
return B_NAME_IN_USE;
|
|
|
|
|
|
|
|
status_t status = fServiceHash.Insert(&service);
|
|
|
|
if (status == B_OK)
|
|
|
|
service.AddReference();
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
NotificationManager::UnregisterService(NotificationService& service)
|
|
|
|
{
|
|
|
|
MutexLocker _(fLock);
|
|
|
|
fServiceHash.Remove(&service);
|
|
|
|
service.RemoveReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
NotificationManager::AddListener(const char* serviceName,
|
|
|
|
uint32 eventMask, NotificationListener& listener)
|
|
|
|
{
|
|
|
|
char buffer[96];
|
|
|
|
KMessage specifier;
|
|
|
|
specifier.SetTo(buffer, sizeof(buffer), 0);
|
|
|
|
specifier.AddInt32("event mask", eventMask);
|
|
|
|
|
|
|
|
return AddListener(serviceName, &specifier, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
NotificationManager::AddListener(const char* serviceName,
|
|
|
|
const KMessage* eventSpecifier, NotificationListener& listener)
|
|
|
|
{
|
|
|
|
MutexLocker locker(fLock);
|
|
|
|
NotificationService* service = _ServiceFor(serviceName);
|
|
|
|
if (service == NULL)
|
|
|
|
return B_NAME_NOT_FOUND;
|
|
|
|
|
|
|
|
Reference<NotificationService> reference(service);
|
|
|
|
locker.Unlock();
|
|
|
|
|
|
|
|
return service->AddListener(eventSpecifier, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2007-08-01 18:48:44 +04:00
|
|
|
NotificationManager::UpdateListener(const char* serviceName,
|
|
|
|
uint32 eventMask, NotificationListener& listener)
|
2007-07-31 20:23:40 +04:00
|
|
|
{
|
|
|
|
char buffer[96];
|
|
|
|
KMessage specifier;
|
|
|
|
specifier.SetTo(buffer, sizeof(buffer), 0);
|
|
|
|
specifier.AddInt32("event mask", eventMask);
|
|
|
|
|
2007-08-01 18:48:44 +04:00
|
|
|
return UpdateListener(serviceName, &specifier, listener);
|
2007-07-31 20:23:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2007-08-01 18:48:44 +04:00
|
|
|
NotificationManager::UpdateListener(const char* serviceName,
|
2007-07-31 20:23:40 +04:00
|
|
|
const KMessage* eventSpecifier, NotificationListener& listener)
|
|
|
|
{
|
|
|
|
MutexLocker locker(fLock);
|
|
|
|
NotificationService* service = _ServiceFor(serviceName);
|
|
|
|
if (service == NULL)
|
|
|
|
return B_NAME_NOT_FOUND;
|
|
|
|
|
|
|
|
Reference<NotificationService> reference(service);
|
|
|
|
locker.Unlock();
|
|
|
|
|
2007-08-01 18:48:44 +04:00
|
|
|
return service->UpdateListener(eventSpecifier, listener);
|
2007-07-31 20:23:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
2007-08-01 18:48:44 +04:00
|
|
|
NotificationManager::RemoveListener(const char* serviceName,
|
|
|
|
const KMessage* eventSpecifier, NotificationListener& listener)
|
2007-07-31 20:23:40 +04:00
|
|
|
{
|
|
|
|
MutexLocker locker(fLock);
|
|
|
|
NotificationService* service = _ServiceFor(serviceName);
|
|
|
|
if (service == NULL)
|
|
|
|
return B_NAME_NOT_FOUND;
|
|
|
|
|
|
|
|
Reference<NotificationService> reference(service);
|
|
|
|
locker.Unlock();
|
|
|
|
|
2007-08-01 18:48:44 +04:00
|
|
|
return service->RemoveListener(eventSpecifier, listener);
|
2007-07-31 20:23:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark -
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
notifications_init(void)
|
|
|
|
{
|
2007-07-31 21:17:51 +04:00
|
|
|
status_t status = NotificationManager::CreateManager();
|
2007-07-31 20:23:40 +04:00
|
|
|
if (status < B_OK) {
|
|
|
|
panic("Creating the notification manager failed: %s\n",
|
|
|
|
strerror(status));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|