summaryrefslogtreecommitdiff
path: root/LibGUI/GEventLoop.h
blob: 694dfa62e9055d151023f4d6a8ba20572bc21994 (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
#pragma once

#include <LibCore/CEventLoop.h>
#include <WindowServer/WSAPITypes.h>
#include <LibGUI/GEvent.h>

class GAction;
class CObject;
class CNotifier;
class GWindow;

class GEventLoop final : public CEventLoop {
public:
    GEventLoop();
    virtual ~GEventLoop() override;

    static GEventLoop& current() { return static_cast<GEventLoop&>(CEventLoop::current()); }

    static bool post_message_to_server(const WSAPI_ClientMessage&, const ByteBuffer& extra_data = { });
    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; }

    virtual void take_pending_events_from(CEventLoop& other) override
    {
        CEventLoop::take_pending_events_from(other);
        m_unprocessed_bundles.append(move(static_cast<GEventLoop&>(other).m_unprocessed_bundles));
    }

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_bundles();
    }

    void wait_for_event();
    bool drain_messages_from_server();
    void process_unprocessed_bundles();
    void handle_paint_event(const WSAPI_ServerMessage&, GWindow&, const ByteBuffer& extra_data);
    void handle_resize_event(const WSAPI_ServerMessage&, GWindow&);
    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&);
    void handle_window_entered_or_left_event(const WSAPI_ServerMessage&, GWindow&);
    void handle_wm_event(const WSAPI_ServerMessage&, GWindow&);
    void connect_to_server();

    struct IncomingWSMessageBundle {
        WSAPI_ServerMessage message;
        ByteBuffer extra_data;
    };

    Vector<IncomingWSMessageBundle, 64> m_unprocessed_bundles;
    static pid_t s_server_pid;
    static pid_t s_event_fd;
};