2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <AK/Function.h>
|
2020-02-02 11:34:39 +00:00
|
|
|
#include <AK/String.h>
|
2019-04-10 15:01:54 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/WeakPtr.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,
|
2019-07-16 18:31:14 +00:00
|
|
|
NotifierRead,
|
|
|
|
NotifierWrite,
|
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
|
|
|
};
|
|
|
|
|
2020-08-26 20:05:52 +00:00
|
|
|
Event() { }
|
2020-02-02 11:34:39 +00:00
|
|
|
explicit Event(unsigned type)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_type(type)
|
|
|
|
{
|
|
|
|
}
|
2020-08-26 20:05:52 +00:00
|
|
|
virtual ~Event() { }
|
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;
|
2019-05-28 09:53:16 +00:00
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
public:
|
2020-02-02 11:34:39 +00:00
|
|
|
DeferredInvocationEvent(Function<void(Object&)> invokee)
|
|
|
|
: Event(Event::Type::DeferredInvoke)
|
2019-04-10 15:01:54 +00:00
|
|
|
, m_invokee(move(invokee))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-02-02 11:34:39 +00:00
|
|
|
Function<void(Object&)> 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:
|
2020-02-02 11:34:39 +00:00
|
|
|
explicit TimerEvent(int timer_id)
|
|
|
|
: Event(Event::Timer)
|
2019-05-28 09:53:16 +00:00
|
|
|
, m_timer_id(timer_id)
|
2019-04-10 15:01:54 +00:00
|
|
|
{
|
|
|
|
}
|
2020-08-26 20:05:52 +00:00
|
|
|
~TimerEvent() { }
|
2019-04-10 15:01:54 +00:00
|
|
|
|
|
|
|
int timer_id() const { return m_timer_id; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_timer_id;
|
|
|
|
};
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
class NotifierReadEvent final : public Event {
|
2019-07-16 18:31:14 +00:00
|
|
|
public:
|
2020-02-02 11:34:39 +00:00
|
|
|
explicit NotifierReadEvent(int fd)
|
|
|
|
: Event(Event::NotifierRead)
|
2019-07-16 18:31:14 +00:00
|
|
|
, m_fd(fd)
|
|
|
|
{
|
|
|
|
}
|
2020-08-26 20:05:52 +00:00
|
|
|
~NotifierReadEvent() { }
|
2019-07-16 18:31:14 +00:00
|
|
|
|
|
|
|
int fd() const { return m_fd; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_fd;
|
|
|
|
};
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
class NotifierWriteEvent final : public Event {
|
2019-07-16 18:31:14 +00:00
|
|
|
public:
|
2020-02-02 11:34:39 +00:00
|
|
|
explicit NotifierWriteEvent(int fd)
|
|
|
|
: Event(Event::NotifierWrite)
|
2019-07-16 18:31:14 +00:00
|
|
|
, m_fd(fd)
|
|
|
|
{
|
|
|
|
}
|
2020-08-26 20:05:52 +00:00
|
|
|
~NotifierWriteEvent() { }
|
2019-07-16 18:31:14 +00:00
|
|
|
|
|
|
|
int fd() const { return m_fd; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_fd;
|
|
|
|
};
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
class ChildEvent final : public Event {
|
2019-04-10 15:01:54 +00:00
|
|
|
public:
|
2020-02-02 11:34:39 +00:00
|
|
|
ChildEvent(Type, Object& child, Object* insertion_before_child = nullptr);
|
|
|
|
~ChildEvent();
|
2019-04-10 15:01:54 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Object* child() { return m_child.ptr(); }
|
|
|
|
const Object* child() const { return m_child.ptr(); }
|
2019-04-10 15:01:54 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Object* insertion_before_child() { return m_insertion_before_child.ptr(); }
|
|
|
|
const Object* insertion_before_child() const { return m_insertion_before_child.ptr(); }
|
2019-11-05 19:41:27 +00:00
|
|
|
|
2019-04-10 15:01:54 +00:00
|
|
|
private:
|
2020-02-02 11:34:39 +00:00
|
|
|
WeakPtr<Object> m_child;
|
|
|
|
WeakPtr<Object> 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)
|
|
|
|
{
|
|
|
|
}
|
2020-08-26 20:05:52 +00:00
|
|
|
~CustomEvent() { }
|
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
|
|
|
|
|
|
|
}
|