2020-01-18 09:38:21 +01: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 17:30:34 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-02-14 22:29:06 +01:00
|
|
|
#include <AK/Forward.h>
|
2019-04-10 17:30:34 +02:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
#include <AK/WeakPtr.h>
|
2020-02-14 22:29:06 +01:00
|
|
|
#include <LibCore/Forward.h>
|
|
|
|
|
#include <LibCore/Object.h>
|
2019-08-25 23:51:27 +03:00
|
|
|
#include <LibThread/Lock.h>
|
2019-05-28 13:48:06 +02:00
|
|
|
#include <sys/time.h>
|
2019-04-10 17:30:34 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
2019-04-10 17:30:34 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
class EventLoop {
|
2019-04-10 17:30:34 +02:00
|
|
|
public:
|
2020-02-02 12:34:39 +01:00
|
|
|
EventLoop();
|
|
|
|
|
~EventLoop();
|
2019-04-10 17:30:34 +02:00
|
|
|
|
|
|
|
|
int exec();
|
|
|
|
|
|
2019-06-07 17:13:23 +02:00
|
|
|
enum class WaitMode {
|
2019-05-18 13:39:21 +02:00
|
|
|
WaitForEvents,
|
|
|
|
|
PollForEvents,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// processe events, generally called by exec() in a loop.
|
|
|
|
|
// this should really only be used for integrating with other event loops
|
|
|
|
|
void pump(WaitMode = WaitMode::WaitForEvents);
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
void post_event(Object& receiver, NonnullOwnPtr<Event>&&);
|
2019-04-10 17:30:34 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
static EventLoop& main();
|
|
|
|
|
static EventLoop& current();
|
2019-04-10 17:30:34 +02:00
|
|
|
|
2019-05-26 17:53:03 +02:00
|
|
|
bool was_exit_requested() const { return m_exit_requested; }
|
2019-04-10 17:30:34 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
static int register_timer(Object&, int milliseconds, bool should_reload, TimerShouldFireWhenNotVisible);
|
2019-04-10 17:30:34 +02:00
|
|
|
static bool unregister_timer(int timer_id);
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
static void register_notifier(Badge<Notifier>, Notifier&);
|
|
|
|
|
static void unregister_notifier(Badge<Notifier>, Notifier&);
|
2019-04-10 17:30:34 +02:00
|
|
|
|
|
|
|
|
void quit(int);
|
2019-10-25 00:18:31 -05:00
|
|
|
void unquit();
|
2019-04-10 17:30:34 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
void take_pending_events_from(EventLoop& other)
|
2019-04-10 17:30:34 +02:00
|
|
|
{
|
|
|
|
|
m_queued_events.append(move(other.m_queued_events));
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 10:20:57 +02:00
|
|
|
static void wake();
|
|
|
|
|
|
2019-04-10 17:30:34 +02:00
|
|
|
private:
|
2019-05-18 13:39:21 +02:00
|
|
|
void wait_for_event(WaitMode);
|
2019-04-10 17:30:34 +02:00
|
|
|
void get_next_timer_expiration(timeval&);
|
|
|
|
|
|
|
|
|
|
struct QueuedEvent {
|
2020-02-14 22:29:06 +01:00
|
|
|
AK_MAKE_NONCOPYABLE(QueuedEvent);
|
|
|
|
|
public:
|
|
|
|
|
QueuedEvent(Object& receiver, NonnullOwnPtr<Event>);
|
|
|
|
|
QueuedEvent(QueuedEvent&&);
|
|
|
|
|
~QueuedEvent();
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
WeakPtr<Object> receiver;
|
|
|
|
|
NonnullOwnPtr<Event> event;
|
2019-04-10 17:30:34 +02:00
|
|
|
};
|
2019-05-18 13:39:21 +02:00
|
|
|
|
2019-04-20 14:02:19 +02:00
|
|
|
Vector<QueuedEvent, 64> m_queued_events;
|
2019-04-10 17:30:34 +02:00
|
|
|
|
|
|
|
|
bool m_exit_requested { false };
|
|
|
|
|
int m_exit_code { 0 };
|
|
|
|
|
|
2019-07-14 10:20:57 +02:00
|
|
|
static int s_wake_pipe_fds[2];
|
|
|
|
|
|
2019-08-25 23:51:27 +03:00
|
|
|
LibThread::Lock m_lock;
|
2019-04-29 15:57:49 +02:00
|
|
|
|
2019-04-10 17:30:34 +02:00
|
|
|
struct EventLoopTimer {
|
|
|
|
|
int timer_id { 0 };
|
|
|
|
|
int interval { 0 };
|
2019-08-01 10:49:31 +02:00
|
|
|
timeval fire_time { 0, 0 };
|
2019-04-10 17:30:34 +02:00
|
|
|
bool should_reload { false };
|
2019-12-29 15:58:07 +01:00
|
|
|
TimerShouldFireWhenNotVisible fire_when_not_visible { TimerShouldFireWhenNotVisible::No };
|
2020-02-02 12:34:39 +01:00
|
|
|
WeakPtr<Object> owner;
|
2019-04-10 17:30:34 +02:00
|
|
|
|
2019-04-18 01:37:23 +02:00
|
|
|
void reload(const timeval& now);
|
|
|
|
|
bool has_expired(const timeval& now) const;
|
2019-04-10 17:30:34 +02:00
|
|
|
};
|
|
|
|
|
|
2019-07-24 08:42:55 +02:00
|
|
|
static HashMap<int, NonnullOwnPtr<EventLoopTimer>>* s_timers;
|
2019-04-10 17:30:34 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
static HashTable<Notifier*>* s_notifiers;
|
2019-08-17 11:35:09 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
static RefPtr<LocalServer> s_rpc_server;
|
2019-04-10 17:30:34 +02:00
|
|
|
};
|
2020-02-02 12:34:39 +01:00
|
|
|
|
|
|
|
|
}
|