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