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