2007-07-31 20:23:40 +04:00
|
|
|
/*
|
2009-03-15 13:21:56 +03:00
|
|
|
* Copyright 2007-2009, Haiku, Inc. All Rights Reserved.
|
2007-07-31 20:23:40 +04:00
|
|
|
* 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>
|
2015-01-12 13:06:03 +03:00
|
|
|
#include <util/StringHash.h>
|
2007-07-31 21:17:51 +04:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
#include <Referenceable.h>
|
|
|
|
|
2007-07-31 20:23:40 +04:00
|
|
|
#include <util/AutoLock.h>
|
2021-09-01 20:10:04 +03:00
|
|
|
#include <util/DoublyLinkedList.h>
|
2007-07-31 20:23:40 +04:00
|
|
|
#include <util/KMessage.h>
|
|
|
|
#include <util/OpenHashTable.h>
|
|
|
|
|
|
|
|
|
|
|
|
class NotificationService;
|
|
|
|
|
2007-08-01 18:48:44 +04:00
|
|
|
class NotificationListener {
|
2009-03-15 13:21:56 +03:00
|
|
|
public:
|
|
|
|
virtual ~NotificationListener();
|
2007-07-31 20:23:40 +04:00
|
|
|
|
2009-11-20 10:10:23 +03:00
|
|
|
virtual void EventOccurred(NotificationService& service,
|
2009-03-15 13:21:56 +03:00
|
|
|
const KMessage* event);
|
|
|
|
virtual void AllListenersNotified(
|
|
|
|
NotificationService& service);
|
2007-08-01 18:48:44 +04:00
|
|
|
|
2009-03-15 13:21:56 +03:00
|
|
|
virtual bool operator==(
|
|
|
|
const NotificationListener& other) const;
|
2007-08-01 18:48:44 +04:00
|
|
|
|
2009-03-15 13:21:56 +03:00
|
|
|
bool operator!=(
|
|
|
|
const NotificationListener& other) const
|
|
|
|
{ return !(*this == other); }
|
2007-07-31 20:23:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class UserMessagingMessageSender {
|
2009-03-15 13:21:56 +03:00
|
|
|
public:
|
|
|
|
UserMessagingMessageSender();
|
2007-07-31 20:23:40 +04:00
|
|
|
|
2009-03-15 13:21:56 +03:00
|
|
|
void SendMessage(const KMessage* message,
|
|
|
|
port_id port, int32 token);
|
|
|
|
void FlushMessage();
|
2007-07-31 20:23:40 +04:00
|
|
|
|
2009-03-15 13:21:56 +03:00
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
MAX_MESSAGING_TARGET_COUNT = 16,
|
|
|
|
};
|
2007-07-31 20:23:40 +04:00
|
|
|
|
2009-03-15 13:21:56 +03:00
|
|
|
const KMessage* fMessage;
|
|
|
|
messaging_target fTargets[MAX_MESSAGING_TARGET_COUNT];
|
|
|
|
int32 fTargetCount;
|
2007-07-31 20:23:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class UserMessagingListener : public NotificationListener {
|
2009-03-15 13:21:56 +03:00
|
|
|
public:
|
|
|
|
UserMessagingListener(
|
|
|
|
UserMessagingMessageSender& sender,
|
|
|
|
port_id port, int32 token);
|
|
|
|
virtual ~UserMessagingListener();
|
|
|
|
|
2009-11-20 10:10:23 +03:00
|
|
|
virtual void EventOccurred(NotificationService& service,
|
2009-03-15 13:21:56 +03:00
|
|
|
const KMessage* event);
|
|
|
|
virtual void AllListenersNotified(
|
|
|
|
NotificationService& service);
|
|
|
|
|
|
|
|
port_id Port() const { return fPort; }
|
|
|
|
int32 Token() const { return fToken; }
|
|
|
|
|
|
|
|
bool operator==(
|
|
|
|
const NotificationListener& _other) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
UserMessagingMessageSender& fSender;
|
|
|
|
port_id fPort;
|
|
|
|
int32 fToken;
|
2007-07-31 20:23:40 +04:00
|
|
|
};
|
|
|
|
|
2009-03-15 13:21:56 +03:00
|
|
|
inline bool
|
|
|
|
UserMessagingListener::operator==(const NotificationListener& _other) const
|
|
|
|
{
|
|
|
|
const UserMessagingListener* other
|
|
|
|
= dynamic_cast<const UserMessagingListener*>(&_other);
|
|
|
|
return other != NULL && other->Port() == Port()
|
|
|
|
&& other->Token() == Token();
|
|
|
|
}
|
|
|
|
|
2010-12-16 17:29:37 +03:00
|
|
|
class NotificationService : public BReferenceable {
|
2009-03-15 13:21:56 +03:00
|
|
|
public:
|
|
|
|
virtual ~NotificationService();
|
|
|
|
|
|
|
|
virtual status_t AddListener(const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener) = 0;
|
|
|
|
virtual status_t RemoveListener(const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener) = 0;
|
|
|
|
virtual status_t UpdateListener(const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener) = 0;
|
|
|
|
|
|
|
|
virtual const char* Name() = 0;
|
2009-07-27 04:39:12 +04:00
|
|
|
NotificationService*&
|
2009-03-15 13:21:56 +03:00
|
|
|
Link() { return fLink; }
|
|
|
|
|
|
|
|
private:
|
2009-07-27 04:39:12 +04:00
|
|
|
NotificationService* fLink;
|
2009-03-15 13:21:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct default_listener : public DoublyLinkedListLinkImpl<default_listener> {
|
|
|
|
~default_listener();
|
|
|
|
|
2009-04-12 01:45:25 +04:00
|
|
|
uint32 eventMask;
|
2009-03-15 13:21:56 +03:00
|
|
|
team_id team;
|
|
|
|
NotificationListener* listener;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef DoublyLinkedList<default_listener> DefaultListenerList;
|
2007-07-31 20:23:40 +04:00
|
|
|
|
|
|
|
|
2009-03-15 13:21:56 +03:00
|
|
|
class DefaultNotificationService : public NotificationService {
|
|
|
|
public:
|
|
|
|
DefaultNotificationService(const char* name);
|
|
|
|
virtual ~DefaultNotificationService();
|
|
|
|
|
2009-12-21 23:56:50 +03:00
|
|
|
inline bool Lock()
|
|
|
|
{ return recursive_lock_lock(&fLock)
|
|
|
|
== B_OK; }
|
|
|
|
inline void Unlock()
|
|
|
|
{ recursive_lock_unlock(&fLock); }
|
|
|
|
|
|
|
|
inline void Notify(const KMessage& event, uint32 eventMask);
|
|
|
|
void NotifyLocked(const KMessage& event,
|
|
|
|
uint32 eventMask);
|
|
|
|
|
|
|
|
inline bool HasListeners() const
|
|
|
|
{ return !fListeners.IsEmpty(); }
|
2009-03-15 13:21:56 +03:00
|
|
|
virtual status_t AddListener(const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener);
|
|
|
|
virtual status_t UpdateListener(const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener);
|
|
|
|
virtual status_t RemoveListener(const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener);
|
|
|
|
|
|
|
|
virtual const char* Name() { return fName; }
|
|
|
|
|
2013-04-20 07:00:00 +04:00
|
|
|
status_t Register();
|
|
|
|
void Unregister();
|
|
|
|
|
2009-03-15 13:21:56 +03:00
|
|
|
protected:
|
2009-04-12 01:45:25 +04:00
|
|
|
virtual status_t ToEventMask(const KMessage& eventSpecifier,
|
|
|
|
uint32& eventMask);
|
|
|
|
virtual void FirstAdded();
|
|
|
|
virtual void LastRemoved();
|
2009-03-15 13:21:56 +03:00
|
|
|
|
|
|
|
recursive_lock fLock;
|
|
|
|
DefaultListenerList fListeners;
|
|
|
|
const char* fName;
|
|
|
|
};
|
2007-07-31 20:23:40 +04:00
|
|
|
|
2009-03-15 13:21:56 +03:00
|
|
|
class DefaultUserNotificationService : public DefaultNotificationService,
|
|
|
|
NotificationListener {
|
|
|
|
public:
|
|
|
|
DefaultUserNotificationService(
|
|
|
|
const char* name);
|
|
|
|
virtual ~DefaultUserNotificationService();
|
|
|
|
|
|
|
|
virtual status_t AddListener(const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener);
|
|
|
|
virtual status_t UpdateListener(const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener);
|
|
|
|
virtual status_t RemoveListener(const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener);
|
|
|
|
|
|
|
|
status_t RemoveUserListeners(port_id port, uint32 token);
|
2009-04-12 01:45:25 +04:00
|
|
|
status_t UpdateUserListener(uint32 eventMask,
|
2009-03-15 13:21:56 +03:00
|
|
|
port_id port, uint32 token);
|
|
|
|
|
|
|
|
private:
|
2009-11-20 10:10:23 +03:00
|
|
|
virtual void EventOccurred(NotificationService& service,
|
2009-03-15 13:21:56 +03:00
|
|
|
const KMessage* event);
|
|
|
|
virtual void AllListenersNotified(
|
|
|
|
NotificationService& service);
|
2009-04-12 01:45:25 +04:00
|
|
|
status_t _AddListener(uint32 eventMask,
|
2009-03-15 13:21:56 +03:00
|
|
|
NotificationListener& listener);
|
|
|
|
|
|
|
|
UserMessagingMessageSender fSender;
|
2007-07-31 20:23:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class NotificationManager {
|
2009-03-15 13:21:56 +03:00
|
|
|
public:
|
|
|
|
static NotificationManager& Manager();
|
|
|
|
static status_t CreateManager();
|
|
|
|
|
|
|
|
status_t RegisterService(NotificationService& service);
|
|
|
|
void UnregisterService(
|
|
|
|
NotificationService& service);
|
|
|
|
|
|
|
|
status_t AddListener(const char* service,
|
|
|
|
uint32 eventMask,
|
|
|
|
NotificationListener& listener);
|
|
|
|
status_t AddListener(const char* service,
|
|
|
|
const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener);
|
|
|
|
|
|
|
|
status_t UpdateListener(const char* service,
|
|
|
|
uint32 eventMask,
|
|
|
|
NotificationListener& listener);
|
|
|
|
status_t UpdateListener(const char* service,
|
|
|
|
const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener);
|
|
|
|
|
|
|
|
status_t RemoveListener(const char* service,
|
|
|
|
const KMessage* eventSpecifier,
|
|
|
|
NotificationListener& listener);
|
|
|
|
|
|
|
|
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()); }
|
2009-07-27 04:39:12 +04:00
|
|
|
NotificationService*& GetLink(
|
2009-03-15 13:21:56 +03:00
|
|
|
NotificationService* service) const
|
2009-07-27 04:39:12 +04:00
|
|
|
{ return service->Link(); }
|
2009-03-15 13:21:56 +03:00
|
|
|
};
|
2009-07-27 04:39:12 +04:00
|
|
|
typedef BOpenHashTable<HashDefinition> ServiceHash;
|
2009-03-15 13:21:56 +03:00
|
|
|
|
|
|
|
static NotificationManager sManager;
|
|
|
|
|
|
|
|
mutex fLock;
|
|
|
|
ServiceHash fServiceHash;
|
2007-07-31 20:23:40 +04:00
|
|
|
};
|
|
|
|
|
2009-12-21 23:56:50 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
DefaultNotificationService::Notify(const KMessage& event, uint32 eventMask)
|
|
|
|
{
|
|
|
|
RecursiveLocker _(fLock);
|
|
|
|
NotifyLocked(event, eventMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-31 20:23:40 +04:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
#endif // __cplusplus
|
|
|
|
|
|
|
|
void notifications_init(void);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif // __cplusplus
|
|
|
|
|
|
|
|
#endif // _KERNEL_NOTIFICATIONS_H
|