2019-04-10 15:30:34 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Badge.h>
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <AK/WeakPtr.h>
|
2019-05-28 11:48:06 +00:00
|
|
|
#include <LibCore/CLock.h>
|
2019-04-10 15:30:34 +00:00
|
|
|
#include <sys/select.h>
|
2019-05-28 11:48:06 +00:00
|
|
|
#include <sys/time.h>
|
2019-04-10 15:30:34 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
class CEvent;
|
|
|
|
class CObject;
|
2019-04-10 15:35:43 +00:00
|
|
|
class CNotifier;
|
2019-04-10 15:30:34 +00:00
|
|
|
|
|
|
|
class CEventLoop {
|
|
|
|
public:
|
|
|
|
CEventLoop();
|
|
|
|
virtual ~CEventLoop();
|
|
|
|
|
|
|
|
int exec();
|
|
|
|
|
2019-06-07 15:13:23 +00:00
|
|
|
enum class WaitMode {
|
2019-05-18 11:39:21 +00: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);
|
|
|
|
|
2019-04-10 15:30:34 +00:00
|
|
|
void post_event(CObject& receiver, OwnPtr<CEvent>&&);
|
|
|
|
|
|
|
|
static CEventLoop& main();
|
|
|
|
static CEventLoop& current();
|
|
|
|
|
2019-05-26 15:53:03 +00:00
|
|
|
bool was_exit_requested() const { return m_exit_requested; }
|
2019-04-10 15:30:34 +00:00
|
|
|
|
|
|
|
static int register_timer(CObject&, int milliseconds, bool should_reload);
|
|
|
|
static bool unregister_timer(int timer_id);
|
|
|
|
|
2019-04-10 15:35:43 +00:00
|
|
|
static void register_notifier(Badge<CNotifier>, CNotifier&);
|
|
|
|
static void unregister_notifier(Badge<CNotifier>, CNotifier&);
|
2019-04-10 15:30:34 +00:00
|
|
|
|
|
|
|
void quit(int);
|
|
|
|
|
|
|
|
virtual void take_pending_events_from(CEventLoop& other)
|
|
|
|
{
|
|
|
|
m_queued_events.append(move(other.m_queued_events));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void add_file_descriptors_for_select(fd_set&, int& max_fd) { UNUSED_PARAM(max_fd); }
|
2019-05-28 09:53:16 +00:00
|
|
|
virtual void process_file_descriptors_after_select(const fd_set&) {}
|
|
|
|
virtual void do_processing() {}
|
2019-04-10 15:30:34 +00:00
|
|
|
|
|
|
|
private:
|
2019-05-18 11:39:21 +00:00
|
|
|
void wait_for_event(WaitMode);
|
2019-04-10 15:30:34 +00:00
|
|
|
void get_next_timer_expiration(timeval&);
|
|
|
|
|
|
|
|
struct QueuedEvent {
|
|
|
|
WeakPtr<CObject> receiver;
|
|
|
|
OwnPtr<CEvent> event;
|
|
|
|
};
|
2019-05-18 11:39:21 +00:00
|
|
|
|
2019-04-20 12:02:19 +00:00
|
|
|
Vector<QueuedEvent, 64> m_queued_events;
|
2019-04-10 15:30:34 +00:00
|
|
|
|
|
|
|
bool m_exit_requested { false };
|
|
|
|
int m_exit_code { 0 };
|
|
|
|
|
2019-04-29 13:57:49 +00:00
|
|
|
CLock m_lock;
|
|
|
|
|
2019-04-10 15:30:34 +00:00
|
|
|
struct EventLoopTimer {
|
|
|
|
int timer_id { 0 };
|
|
|
|
int interval { 0 };
|
|
|
|
timeval fire_time;
|
|
|
|
bool should_reload { false };
|
|
|
|
WeakPtr<CObject> owner;
|
|
|
|
|
2019-04-17 23:37:23 +00:00
|
|
|
void reload(const timeval& now);
|
|
|
|
bool has_expired(const timeval& now) const;
|
2019-04-10 15:30:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static HashMap<int, OwnPtr<EventLoopTimer>>* s_timers;
|
|
|
|
static int s_next_timer_id;
|
|
|
|
|
2019-04-10 15:35:43 +00:00
|
|
|
static HashTable<CNotifier*>* s_notifiers;
|
2019-04-10 15:30:34 +00:00
|
|
|
};
|