1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 00:17:36 +00:00

LibCore: Add CEventLoop and make LibGUI/GEventLoop inherit from it.

This is shaping up to be quite nice.
This commit is contained in:
Andreas Kling 2019-04-10 17:30:34 +02:00
parent 2f1f51b8ab
commit b2542414d7
6 changed files with 366 additions and 289 deletions

View file

@ -1,10 +1,6 @@
#pragma once
#include <AK/Badge.h>
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/Vector.h>
#include <AK/WeakPtr.h>
#include <LibCore/CEventLoop.h>
#include <WindowServer/WSAPITypes.h>
#include <LibGUI/GEvent.h>
@ -13,42 +9,43 @@ class CObject;
class GNotifier;
class GWindow;
class GEventLoop {
class GEventLoop final : public CEventLoop {
public:
GEventLoop();
~GEventLoop();
virtual ~GEventLoop() override;
int exec();
void post_event(CObject& receiver, OwnPtr<CEvent>&&);
static GEventLoop& main();
static GEventLoop& current();
bool running() const { return m_running; }
static int register_timer(CObject&, int milliseconds, bool should_reload);
static bool unregister_timer(int timer_id);
static void register_notifier(Badge<GNotifier>, GNotifier&);
static void unregister_notifier(Badge<GNotifier>, GNotifier&);
void quit(int);
static GEventLoop& current() { return static_cast<GEventLoop&>(CEventLoop::current()); }
static bool post_message_to_server(const WSAPI_ClientMessage&);
bool wait_for_specific_event(WSAPI_ServerMessage::Type, WSAPI_ServerMessage&);
WSAPI_ServerMessage sync_request(const WSAPI_ClientMessage& request, WSAPI_ServerMessage::Type response_type);
static pid_t server_pid() { return s_server_pid; }
void take_pending_events_from(GEventLoop& other)
virtual void take_pending_events_from(CEventLoop& other) override
{
m_queued_events.append(move(other.m_queued_events));
m_unprocessed_messages.append(move(other.m_unprocessed_messages));
CEventLoop::take_pending_events_from(other);
m_unprocessed_messages.append(move(static_cast<GEventLoop&>(other).m_unprocessed_messages));
}
private:
virtual void add_file_descriptors_for_select(fd_set& fds, int& max_fd_added) override
{
FD_SET(s_event_fd, &fds);
max_fd_added = s_event_fd;
}
virtual void process_file_descriptors_after_select(const fd_set& fds) override
{
if (FD_ISSET(s_event_fd, &fds))
drain_messages_from_server();
}
virtual void do_processing() override
{
process_unprocessed_messages();
}
void wait_for_event();
bool drain_messages_from_server();
void process_unprocessed_messages();
@ -61,37 +58,9 @@ private:
void handle_menu_event(const WSAPI_ServerMessage&);
void handle_window_entered_or_left_event(const WSAPI_ServerMessage&, GWindow&);
void handle_wm_event(const WSAPI_ServerMessage&, GWindow&);
void get_next_timer_expiration(timeval&);
void connect_to_server();
struct QueuedEvent {
WeakPtr<CObject> receiver;
OwnPtr<CEvent> event;
};
Vector<QueuedEvent> m_queued_events;
Vector<WSAPI_ServerMessage> m_unprocessed_messages;
bool m_running { false };
bool m_exit_requested { false };
int m_exit_code { 0 };
static pid_t s_server_pid;
static pid_t s_event_fd;
struct EventLoopTimer {
int timer_id { 0 };
int interval { 0 };
timeval fire_time;
bool should_reload { false };
WeakPtr<CObject> owner;
void reload();
bool has_expired() const;
};
static HashMap<int, OwnPtr<EventLoopTimer>>* s_timers;
static int s_next_timer_id;
static HashTable<GNotifier*>* s_notifiers;
};