2019-01-20 06:49:48 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-02-10 16:28:39 +03:00
|
|
|
#include <AK/Badge.h>
|
2019-02-01 05:50:06 +03:00
|
|
|
#include <AK/HashMap.h>
|
2019-01-20 06:49:48 +03:00
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/Vector.h>
|
2019-02-26 02:51:49 +03:00
|
|
|
#include <AK/WeakPtr.h>
|
2019-02-15 11:14:21 +03:00
|
|
|
#include <WindowServer/WSAPITypes.h>
|
2019-03-02 12:04:49 +03:00
|
|
|
#include <LibGUI/GEvent.h>
|
2019-01-20 06:49:48 +03:00
|
|
|
|
2019-03-02 12:04:49 +03:00
|
|
|
class GAction;
|
2019-01-20 06:49:48 +03:00
|
|
|
class GObject;
|
2019-02-10 16:28:39 +03:00
|
|
|
class GNotifier;
|
2019-01-20 07:48:43 +03:00
|
|
|
class GWindow;
|
2019-01-20 06:49:48 +03:00
|
|
|
|
|
|
|
class GEventLoop {
|
|
|
|
public:
|
|
|
|
GEventLoop();
|
|
|
|
~GEventLoop();
|
|
|
|
|
|
|
|
int exec();
|
|
|
|
|
2019-02-26 02:51:49 +03:00
|
|
|
void post_event(GObject& receiver, OwnPtr<GEvent>&&);
|
2019-01-20 06:49:48 +03:00
|
|
|
|
|
|
|
static GEventLoop& main();
|
|
|
|
|
|
|
|
bool running() const { return m_running; }
|
|
|
|
|
2019-02-01 05:50:06 +03:00
|
|
|
int register_timer(GObject&, int milliseconds, bool should_reload);
|
|
|
|
bool unregister_timer(int timer_id);
|
|
|
|
|
2019-02-10 16:28:39 +03:00
|
|
|
void register_notifier(Badge<GNotifier>, GNotifier&);
|
|
|
|
void unregister_notifier(Badge<GNotifier>, GNotifier&);
|
|
|
|
|
2019-02-17 11:58:35 +03:00
|
|
|
void quit(int);
|
2019-02-05 12:31:37 +03:00
|
|
|
|
2019-02-15 11:17:18 +03:00
|
|
|
bool post_message_to_server(const WSAPI_ClientMessage&);
|
|
|
|
bool wait_for_specific_event(WSAPI_ServerMessage::Type, WSAPI_ServerMessage&);
|
2019-02-13 19:54:30 +03:00
|
|
|
|
2019-02-15 11:17:18 +03:00
|
|
|
WSAPI_ServerMessage sync_request(const WSAPI_ClientMessage& request, WSAPI_ServerMessage::Type response_type);
|
2019-02-13 19:54:30 +03:00
|
|
|
|
2019-03-17 06:23:54 +03:00
|
|
|
static pid_t server_pid() { return s_server_pid; }
|
2019-02-20 23:59:13 +03:00
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
private:
|
2019-01-20 07:48:43 +03:00
|
|
|
void wait_for_event();
|
2019-02-13 19:59:38 +03:00
|
|
|
bool drain_messages_from_server();
|
2019-02-20 23:59:13 +03:00
|
|
|
void process_unprocessed_messages();
|
2019-02-15 11:17:18 +03:00
|
|
|
void handle_paint_event(const WSAPI_ServerMessage&, GWindow&);
|
2019-02-20 17:34:55 +03:00
|
|
|
void handle_resize_event(const WSAPI_ServerMessage&, GWindow&);
|
2019-02-15 11:17:18 +03:00
|
|
|
void handle_mouse_event(const WSAPI_ServerMessage&, GWindow&);
|
|
|
|
void handle_key_event(const WSAPI_ServerMessage&, GWindow&);
|
|
|
|
void handle_window_activation_event(const WSAPI_ServerMessage&, GWindow&);
|
|
|
|
void handle_window_close_request_event(const WSAPI_ServerMessage&, GWindow&);
|
|
|
|
void handle_menu_event(const WSAPI_ServerMessage&);
|
2019-02-20 12:12:19 +03:00
|
|
|
void handle_window_entered_or_left_event(const WSAPI_ServerMessage&, GWindow&);
|
2019-02-01 05:50:06 +03:00
|
|
|
void get_next_timer_expiration(timeval&);
|
2019-03-09 19:34:09 +03:00
|
|
|
void connect_to_server();
|
2019-02-01 05:50:06 +03:00
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
struct QueuedEvent {
|
2019-02-26 02:51:49 +03:00
|
|
|
WeakPtr<GObject> receiver;
|
2019-01-20 06:49:48 +03:00
|
|
|
OwnPtr<GEvent> event;
|
|
|
|
};
|
2019-01-20 07:48:43 +03:00
|
|
|
Vector<QueuedEvent> m_queued_events;
|
2019-01-20 06:49:48 +03:00
|
|
|
|
2019-02-15 11:17:18 +03:00
|
|
|
Vector<WSAPI_ServerMessage> m_unprocessed_messages;
|
2019-02-13 19:54:30 +03:00
|
|
|
|
2019-01-20 06:49:48 +03:00
|
|
|
bool m_running { false };
|
2019-02-05 12:31:37 +03:00
|
|
|
bool m_exit_requested { false };
|
|
|
|
int m_exit_code { 0 };
|
2019-02-01 05:50:06 +03:00
|
|
|
int m_next_timer_id { 1 };
|
2019-03-09 19:34:09 +03:00
|
|
|
|
|
|
|
static pid_t s_server_pid;
|
|
|
|
static pid_t s_event_fd;
|
2019-02-01 05:50:06 +03:00
|
|
|
|
|
|
|
struct EventLoopTimer {
|
|
|
|
int timer_id { 0 };
|
|
|
|
int interval { 0 };
|
|
|
|
timeval fire_time;
|
|
|
|
bool should_reload { false };
|
2019-02-26 02:51:49 +03:00
|
|
|
WeakPtr<GObject> owner;
|
2019-02-01 05:50:06 +03:00
|
|
|
|
|
|
|
void reload();
|
|
|
|
bool has_expired() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
HashMap<int, OwnPtr<EventLoopTimer>> m_timers;
|
2019-02-10 16:28:39 +03:00
|
|
|
HashTable<GNotifier*> m_notifiers;
|
2019-01-20 06:49:48 +03:00
|
|
|
};
|