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
|
|
|
|
*/
|
|
|
|
#ifndef _KERNEL_NOTIFICATIONS_H
|
|
|
|
#define _KERNEL_NOTIFICATIONS_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
|
|
|
#include <lock.h>
|
|
|
|
#include <messaging.h>
|
2007-07-31 21:17:51 +04:00
|
|
|
#include <util/khash.h>
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
#include <Referenceable.h>
|
|
|
|
|
2007-07-31 20:23:40 +04:00
|
|
|
#include <util/AutoLock.h>
|
|
|
|
#include <util/KMessage.h>
|
|
|
|
#include <util/OpenHashTable.h>
|
|
|
|
|
|
|
|
|
|
|
|
class NotificationService;
|
|
|
|
|
2007-08-01 18:48:44 +04:00
|
|
|
class NotificationListener {
|
2007-07-31 20:23:40 +04:00
|
|
|
public:
|
|
|
|
virtual ~NotificationListener();
|
|
|
|
|
|
|
|
virtual void EventOccured(NotificationService& service,
|
|
|
|
const KMessage* event);
|
2007-08-01 18:48:44 +04:00
|
|
|
virtual void AllListenersNotified(NotificationService& service);
|
|
|
|
|
|
|
|
virtual bool operator==(const NotificationListener& other) const;
|
|
|
|
|
|
|
|
bool operator!=(const NotificationListener& other) const
|
|
|
|
{ return !(*this == other); }
|
2007-07-31 20:23:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class UserMessagingMessageSender {
|
|
|
|
public:
|
|
|
|
UserMessagingMessageSender();
|
|
|
|
|
|
|
|
void SendMessage(const KMessage* message, port_id port, int32 token);
|
|
|
|
void FlushMessage();
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
MAX_MESSAGING_TARGET_COUNT = 16,
|
|
|
|
};
|
|
|
|
|
|
|
|
const KMessage* fMessage;
|
|
|
|
messaging_target fTargets[MAX_MESSAGING_TARGET_COUNT];
|
|
|
|
int32 fTargetCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UserMessagingListener : public NotificationListener {
|
|
|
|
public:
|
|
|
|
UserMessagingListener(UserMessagingMessageSender& sender, port_id port,
|
|
|
|
int32 token);
|
|
|
|
virtual ~UserMessagingListener();
|
|
|
|
|
|
|
|
virtual void EventOccured(NotificationService& service,
|
|
|
|
const KMessage* event);
|
2007-08-01 18:48:44 +04:00
|
|
|
virtual void AllListenersNotified(NotificationService& service);
|
2007-07-31 20:23:40 +04:00
|
|
|
|
2007-08-01 18:48:44 +04:00
|
|
|
port_id Port() const { return fPort; }
|
|
|
|
int32 Token() const { return fToken; }
|
2007-07-31 20:23:40 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
UserMessagingMessageSender& fSender;
|
|
|
|
port_id fPort;
|
|
|
|
int32 fToken;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NotificationService : public Referenceable {
|
|
|
|
public:
|
2007-08-01 18:48:44 +04:00
|
|
|
virtual ~NotificationService();
|
2007-07-31 20:23:40 +04:00
|
|
|
|
|
|
|
virtual status_t AddListener(const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener) = 0;
|
|
|
|
virtual status_t RemoveListener(const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener) = 0;
|
2007-08-01 18:48:44 +04:00
|
|
|
virtual status_t UpdateListener(const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener) = 0;
|
2007-07-31 20:23:40 +04:00
|
|
|
|
|
|
|
virtual const char* Name() = 0;
|
|
|
|
HashTableLink<NotificationService>& Link() { return fLink; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
HashTableLink<NotificationService> fLink;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NotificationManager {
|
|
|
|
public:
|
|
|
|
static NotificationManager& Manager();
|
|
|
|
static status_t CreateManager();
|
|
|
|
|
|
|
|
status_t RegisterService(NotificationService& service);
|
|
|
|
void UnregisterService(NotificationService& service);
|
|
|
|
|
|
|
|
NotificationService* GetService(const char* name);
|
|
|
|
void PutService(NotificationService* service);
|
|
|
|
|
|
|
|
status_t AddListener(const char* service, uint32 eventMask,
|
|
|
|
NotificationListener& listener);
|
|
|
|
status_t AddListener(const char* service,
|
|
|
|
const KMessage* eventSpecifier, NotificationListener& listener);
|
|
|
|
|
2007-08-01 18:48:44 +04:00
|
|
|
status_t UpdateListener(const char* service,
|
|
|
|
uint32 eventMask, NotificationListener& listener);
|
|
|
|
status_t UpdateListener(const char* service,
|
2007-07-31 20:23:40 +04:00
|
|
|
const KMessage* eventSpecifier, NotificationListener& listener);
|
|
|
|
|
2007-08-01 18:48:44 +04:00
|
|
|
status_t RemoveListener(const char* service,
|
|
|
|
const KMessage* eventSpecifier, NotificationListener& listener);
|
2007-07-31 20:23:40 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
NotificationManager();
|
|
|
|
~NotificationManager();
|
|
|
|
|
|
|
|
status_t _Init();
|
|
|
|
NotificationService* _ServiceFor(const char* name);
|
|
|
|
|
|
|
|
struct HashDefinition {
|
|
|
|
typedef const char* KeyType;
|
|
|
|
typedef NotificationService ValueType;
|
|
|
|
|
|
|
|
size_t HashKey(const char* key) const
|
|
|
|
{ return hash_hash_string(key); }
|
|
|
|
size_t Hash(NotificationService *service) const
|
|
|
|
{ return hash_hash_string(service->Name()); }
|
|
|
|
bool Compare(const char* key, NotificationService* service) const
|
|
|
|
{ return !strcmp(key, service->Name()); }
|
|
|
|
HashTableLink<NotificationService>* GetLink(
|
|
|
|
NotificationService* service) const
|
|
|
|
{ return &service->Link(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
static NotificationManager sManager;
|
|
|
|
|
|
|
|
mutex fLock;
|
|
|
|
typedef OpenHashTable<HashDefinition> ServiceHash;
|
|
|
|
ServiceHash fServiceHash;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
#endif // __cplusplus
|
|
|
|
|
|
|
|
void notifications_init(void);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif // __cplusplus
|
|
|
|
|
|
|
|
#endif // _KERNEL_NOTIFICATIONS_H
|