2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
|
2022-02-26 16:09:45 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
#pragma once
|
|
|
|
|
2024-02-02 01:21:15 +00:00
|
|
|
#include <AK/EnumBits.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/Function.h>
|
2019-04-10 15:01:54 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/WeakPtr.h>
|
2021-08-30 10:43:28 +00:00
|
|
|
#include <LibCore/DeferredInvocationContext.h>
|
2020-02-14 21:29:06 +00:00
|
|
|
#include <LibCore/Forward.h>
|
2019-04-10 15:01:54 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
2019-04-10 15:01:54 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
class Event {
|
2019-04-10 15:01:54 +00:00
|
|
|
public:
|
2019-06-07 15:13:23 +00:00
|
|
|
enum Type {
|
2019-04-10 15:01:54 +00:00
|
|
|
Invalid = 0,
|
|
|
|
Quit,
|
|
|
|
Timer,
|
2023-04-23 18:59:32 +00:00
|
|
|
NotifierActivation,
|
2019-04-10 15:01:54 +00:00
|
|
|
DeferredInvoke,
|
|
|
|
ChildAdded,
|
|
|
|
ChildRemoved,
|
2019-07-13 16:35:13 +00:00
|
|
|
Custom,
|
2019-04-10 15:01:54 +00:00
|
|
|
};
|
|
|
|
|
2022-02-26 16:09:45 +00:00
|
|
|
Event() = default;
|
2020-02-02 11:34:39 +00:00
|
|
|
explicit Event(unsigned type)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_type(type)
|
|
|
|
{
|
|
|
|
}
|
2022-02-26 16:09:45 +00:00
|
|
|
virtual ~Event() = default;
|
2019-04-10 15:01:54 +00:00
|
|
|
|
|
|
|
unsigned type() const { return m_type; }
|
|
|
|
|
2019-09-20 18:37:31 +00:00
|
|
|
bool is_accepted() const { return m_accepted; }
|
|
|
|
void accept() { m_accepted = true; }
|
|
|
|
void ignore() { m_accepted = false; }
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
private:
|
|
|
|
unsigned m_type { Type::Invalid };
|
2019-09-20 18:37:31 +00:00
|
|
|
bool m_accepted { true };
|
2019-04-10 15:01:54 +00:00
|
|
|
};
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
class DeferredInvocationEvent : public Event {
|
|
|
|
friend class EventLoop;
|
2023-04-23 17:45:12 +00:00
|
|
|
friend class ThreadEventQueue;
|
2019-05-28 09:53:16 +00:00
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
public:
|
2021-08-30 10:43:28 +00:00
|
|
|
DeferredInvocationEvent(NonnullRefPtr<DeferredInvocationContext> context, Function<void()> invokee)
|
2020-02-02 11:34:39 +00:00
|
|
|
: Event(Event::Type::DeferredInvoke)
|
2021-08-30 10:43:28 +00:00
|
|
|
, m_context(move(context))
|
2019-04-10 15:01:54 +00:00
|
|
|
, m_invokee(move(invokee))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-08-30 10:43:28 +00:00
|
|
|
NonnullRefPtr<DeferredInvocationContext> m_context;
|
|
|
|
Function<void()> m_invokee;
|
2019-04-10 15:01:54 +00:00
|
|
|
};
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
class TimerEvent final : public Event {
|
2019-04-10 15:01:54 +00:00
|
|
|
public:
|
2024-02-18 04:59:28 +00:00
|
|
|
explicit TimerEvent()
|
2020-02-02 11:34:39 +00:00
|
|
|
: Event(Event::Timer)
|
2019-04-10 15:01:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-02-18 04:59:28 +00:00
|
|
|
~TimerEvent() = default;
|
2019-04-10 15:01:54 +00:00
|
|
|
};
|
|
|
|
|
2024-02-02 01:21:15 +00:00
|
|
|
enum class NotificationType {
|
|
|
|
None = 0,
|
|
|
|
Read = 1,
|
|
|
|
Write = 2,
|
2024-02-02 02:51:36 +00:00
|
|
|
HangUp = 4,
|
|
|
|
Error = 8,
|
2024-02-02 01:21:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AK_ENUM_BITWISE_OPERATORS(NotificationType);
|
|
|
|
|
2023-04-23 18:59:32 +00:00
|
|
|
class NotifierActivationEvent final : public Event {
|
2019-07-16 18:31:14 +00:00
|
|
|
public:
|
2024-02-02 01:21:15 +00:00
|
|
|
explicit NotifierActivationEvent(int fd, NotificationType type)
|
2023-04-23 18:59:32 +00:00
|
|
|
: Event(Event::NotifierActivation)
|
2019-07-16 18:31:14 +00:00
|
|
|
, m_fd(fd)
|
2024-02-02 01:21:15 +00:00
|
|
|
, m_type(type)
|
2019-07-16 18:31:14 +00:00
|
|
|
{
|
|
|
|
}
|
2023-04-23 18:59:32 +00:00
|
|
|
~NotifierActivationEvent() = default;
|
2019-07-16 18:31:14 +00:00
|
|
|
|
|
|
|
int fd() const { return m_fd; }
|
2024-02-02 01:21:15 +00:00
|
|
|
NotificationType type() const { return m_type; }
|
2019-07-16 18:31:14 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int m_fd;
|
2024-02-02 01:21:15 +00:00
|
|
|
NotificationType m_type;
|
2019-07-16 18:31:14 +00:00
|
|
|
};
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
class ChildEvent final : public Event {
|
2019-04-10 15:01:54 +00:00
|
|
|
public:
|
2023-08-06 16:09:39 +00:00
|
|
|
ChildEvent(Type, EventReceiver& child, EventReceiver* insertion_before_child = nullptr);
|
2022-02-26 16:09:45 +00:00
|
|
|
~ChildEvent() = default;
|
2019-04-10 15:01:54 +00:00
|
|
|
|
2023-08-06 16:09:39 +00:00
|
|
|
EventReceiver* child();
|
|
|
|
EventReceiver const* child() const;
|
2019-04-10 15:01:54 +00:00
|
|
|
|
2023-08-06 16:09:39 +00:00
|
|
|
EventReceiver* insertion_before_child();
|
|
|
|
EventReceiver const* insertion_before_child() const;
|
2019-11-05 19:41:27 +00:00
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
private:
|
2023-08-06 16:09:39 +00:00
|
|
|
WeakPtr<EventReceiver> m_child;
|
|
|
|
WeakPtr<EventReceiver> m_insertion_before_child;
|
2019-04-10 15:01:54 +00:00
|
|
|
};
|
2019-07-13 16:35:13 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
class CustomEvent : public Event {
|
2019-07-13 16:35:13 +00:00
|
|
|
public:
|
2020-08-26 20:05:52 +00:00
|
|
|
CustomEvent(int custom_type)
|
2020-02-02 11:34:39 +00:00
|
|
|
: Event(Event::Type::Custom)
|
2019-07-13 16:35:13 +00:00
|
|
|
, m_custom_type(custom_type)
|
|
|
|
{
|
|
|
|
}
|
2022-02-26 16:09:45 +00:00
|
|
|
~CustomEvent() = default;
|
2019-07-13 16:35:13 +00:00
|
|
|
|
|
|
|
int custom_type() const { return m_custom_type; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_custom_type { 0 };
|
|
|
|
};
|
2020-02-02 11:34:39 +00:00
|
|
|
|
|
|
|
}
|