summaryrefslogtreecommitdiffstats
path: root/src/lib/base/EventQueue.h
blob: 97e7fba7890956c03ee4b59f089c79d7e6ff6b3e (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2012-2016 Symless Ltd.
 * Copyright (C) 2004 Chris Schoeneman
 * 
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file LICENSE that should have accompanied this file.
 * 
 * This package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "mt/CondVar.h"
#include "arch/IArchMultithread.h"
#include "base/IEventQueue.h"
#include "base/Event.h"
#include "base/PriorityQueue.h"
#include "base/Stopwatch.h"
#include "common/stdmap.h"
#include "common/stdset.h"
#include "base/NonBlockingStream.h"

#include <queue>

class Mutex;

//! Event queue
/*!
An event queue that implements the platform independent parts and
delegates the platform dependent parts to a subclass.
*/
class EventQueue : public IEventQueue {
public:
    EventQueue();
    virtual ~EventQueue();

    // IEventQueue overrides
    virtual void        loop();
    virtual void        adoptBuffer(IEventQueueBuffer*);
    virtual bool        getEvent(Event& event, double timeout = -1.0);
    virtual bool        dispatchEvent(const Event& event);
    virtual void        addEvent(const Event& event);
    virtual EventQueueTimer*
                        newTimer(double duration, void* target);
    virtual EventQueueTimer*
                        newOneShotTimer(double duration, void* target);
    virtual void        deleteTimer(EventQueueTimer*);
    virtual void        adoptHandler(Event::Type type,
                            void* target, IEventJob* handler);
    virtual void        removeHandler(Event::Type type, void* target);
    virtual void        removeHandlers(void* target);
    virtual Event::Type
                        registerTypeOnce(Event::Type& type, const char* name);
    virtual bool        isEmpty() const;
    virtual IEventJob*    getHandler(Event::Type type, void* target) const;
    virtual const char*    getTypeName(Event::Type type);
    virtual Event::Type
                        getRegisteredType(const String& name) const;
    void*                getSystemTarget();
    virtual void        waitForReady() const;

private:
    UInt32                saveEvent(const Event& event);
    Event                removeEvent(UInt32 eventID);
    bool                hasTimerExpired(Event& event);
    double                getNextTimerTimeout() const;
    void                addEventToBuffer(const Event& event);
    bool                parent_requests_shutdown() const;
    
private:
    class Timer {
    public:
        Timer(EventQueueTimer*, double timeout, double initialTime,
                            void* target, bool oneShot);
        ~Timer();

        void            reset();

        Timer&            operator-=(double);

                        operator double() const;

        bool            isOneShot() const;
        EventQueueTimer*
                        getTimer() const;
        void*            getTarget() const;
        void            fillEvent(TimerEvent&) const;

        bool            operator<(const Timer&) const;

    private:
        EventQueueTimer*    m_timer;
        double                m_timeout;
        void*                m_target;
        bool                m_oneShot;
        double                m_time;
    };

    typedef std::set<EventQueueTimer*> Timers;
    typedef PriorityQueue<Timer> TimerQueue;
    typedef std::map<UInt32, Event> EventTable;
    typedef std::vector<UInt32> EventIDList;
    typedef std::map<Event::Type, const char*> TypeMap;
    typedef std::map<String, Event::Type> NameMap;
    typedef std::map<Event::Type, IEventJob*> TypeHandlerTable;
    typedef std::map<void*, TypeHandlerTable> HandlerTable;

    int                    m_systemTarget;
    ArchMutex            m_mutex;

    // registered events
    Event::Type        m_nextType;
    TypeMap            m_typeMap;
    NameMap            m_nameMap;

    // buffer of events
    IEventQueueBuffer*    m_buffer;

    // saved events
    EventTable            m_events;
    EventIDList        m_oldEventIDs;

    // timers
    Stopwatch            m_time;
    Timers                m_timers;
    TimerQueue            m_timerQueue;
    TimerEvent            m_timerEvent;

    // event handlers
    HandlerTable        m_handlers;

public:
    //
    // Event type providers.
    //
    ClientEvents&                forClient();
    IStreamEvents&                forIStream();
    IpcClientEvents&            forIpcClient();
    IpcClientProxyEvents&        forIpcClientProxy();
    IpcServerEvents&            forIpcServer();
    IpcServerProxyEvents&        forIpcServerProxy();
    IDataSocketEvents&            forIDataSocket();
    IListenSocketEvents&        forIListenSocket();
    ISocketEvents&                forISocket();
    OSXScreenEvents&            forOSXScreen();
    ClientListenerEvents&        forClientListener();
    ClientProxyEvents&            forClientProxy();
    ClientProxyUnknownEvents&    forClientProxyUnknown();
    ServerEvents&                forServer();
    ServerAppEvents&            forServerApp();
    IKeyStateEvents&            forIKeyState();
    IPrimaryScreenEvents&        forIPrimaryScreen();
    IScreenEvents&                forIScreen();
    ClipboardEvents&            forClipboard();
    FileEvents&                    forFile();

private:
    ClientEvents*                m_typesForClient;
    IStreamEvents*                m_typesForIStream;
    IpcClientEvents*            m_typesForIpcClient;
    IpcClientProxyEvents*        m_typesForIpcClientProxy;
    IpcServerEvents*            m_typesForIpcServer;
    IpcServerProxyEvents*        m_typesForIpcServerProxy;
    IDataSocketEvents*            m_typesForIDataSocket;
    IListenSocketEvents*        m_typesForIListenSocket;
    ISocketEvents*                m_typesForISocket;
    OSXScreenEvents*            m_typesForOSXScreen;
    ClientListenerEvents*        m_typesForClientListener;
    ClientProxyEvents*            m_typesForClientProxy;
    ClientProxyUnknownEvents*    m_typesForClientProxyUnknown;
    ServerEvents*                m_typesForServer;
    ServerAppEvents*            m_typesForServerApp;
    IKeyStateEvents*            m_typesForIKeyState;
    IPrimaryScreenEvents*        m_typesForIPrimaryScreen;
    IScreenEvents*                m_typesForIScreen;
    ClipboardEvents*            m_typesForClipboard;
    FileEvents*                    m_typesForFile;
    Mutex*                        m_readyMutex;
    CondVar<bool>*                m_readyCondVar;
    std::queue<Event>            m_pending;
    NonBlockingStream            m_parentStream;
};

#define EVENT_TYPE_ACCESSOR(type_)                                            \
type_##Events&                                                                \
EventQueue::for##type_() {                                                \
    if (m_typesFor##type_ == NULL) {                                        \
        m_typesFor##type_ = new type_##Events();                            \
        m_typesFor##type_->setEvents(dynamic_cast<IEventQueue*>(this));        \
    }                                                                        \
    return *m_typesFor##type_;                                                \
}