summaryrefslogtreecommitdiff
path: root/Servers/WindowServer/WSMessageLoop.h
blob: 752cd0d60cc6bdc96344cbcb4dc52479a245ab3f (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
#pragma once

#include "WSMessage.h"
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/Vector.h>
#include <AK/Function.h>
#include <AK/WeakPtr.h>

class WSMessageReceiver;
struct WSAPI_ClientMessage;
struct WSAPI_ServerMessage;

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

    int exec();

    void post_message(WSMessageReceiver& receiver, OwnPtr<WSMessage>&&);

    static WSMessageLoop& the();

    bool running() const { return m_running; }

    int start_timer(int ms, Function<void()>&&);
    int stop_timer(int timer_id);

    void on_receive_from_client(int client_id, const WSAPI_ClientMessage&);

    void notify_client_disconnected(int client_id);

private:
    void wait_for_message();
    void drain_mouse();
    void drain_keyboard();

    struct QueuedMessage {
        WeakPtr<WSMessageReceiver> receiver;
        OwnPtr<WSMessage> message;
    };
    Vector<QueuedMessage> m_queued_messages;

    bool m_running { false };

    int m_keyboard_fd { -1 };
    int m_mouse_fd { -1 };
    int m_server_fd { -1 };

    struct Timer {
        void reload();

        int timer_id { 0 };
        int interval { 0 };
        struct timeval next_fire_time { 0, 0 };
        Function<void()> callback;
    };

    int m_next_timer_id { 1 };
    HashMap<int, OwnPtr<Timer>> m_timers;
};