summaryrefslogtreecommitdiff
path: root/LibGUI/GEventLoop.h
blob: e68df0dc10e4bad2dc8320b5da13f2e5a2ff0e52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once

#include "GEvent.h"
#include <AK/Badge.h>
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/Vector.h>

class GObject;
class GNotifier;
class GWindow;
struct GUI_Event;

class GEventLoop {
public:
    GEventLoop();
    ~GEventLoop();

    int exec();

    void post_event(GObject* receiver, OwnPtr<GEvent>&&);

    static GEventLoop& main();

    static void initialize();

    bool running() const { return m_running; }

    int register_timer(GObject&, int milliseconds, bool should_reload);
    bool unregister_timer(int timer_id);

    void register_notifier(Badge<GNotifier>, GNotifier&);
    void unregister_notifier(Badge<GNotifier>, GNotifier&);

    void exit(int);

private:
    void wait_for_event();
    void handle_paint_event(const GUI_Event&, GWindow&);
    void handle_mouse_event(const GUI_Event&, GWindow&);
    void handle_key_event(const GUI_Event&, GWindow&);
    void handle_window_activation_event(const GUI_Event&, GWindow&);
    void handle_window_close_request_event(const GUI_Event&, GWindow&);
    void handle_menu_event(const GUI_Event&);

    void get_next_timer_expiration(timeval&);

    struct QueuedEvent {
        GObject* receiver { nullptr };
        OwnPtr<GEvent> event;
    };
    Vector<QueuedEvent> m_queued_events;

    int m_event_fd { -1 };
    bool m_running { false };
    bool m_exit_requested { false };
    int m_exit_code { 0 };

    int m_next_timer_id { 1 };

    struct EventLoopTimer {
        int timer_id { 0 };
        int interval { 0 };
        timeval fire_time;
        bool should_reload { false };
        GObject* owner { nullptr };

        void reload();
        bool has_expired() const;
    };

    HashMap<int, OwnPtr<EventLoopTimer>> m_timers;
    HashTable<GNotifier*> m_notifiers;
};