aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/platform/MSWindowsEventQueueBuffer.cpp
blob: 311136765b7f3a7be0c116be427a043ad6865533 (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
/*
 * 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/>.
 */

#include "platform/MSWindowsEventQueueBuffer.h"

#include "arch/win32/ArchMiscWindows.h"
#include "mt/Thread.h"
#include "base/IEventQueue.h"
#include <VersionHelpers.h>

//
// EventQueueTimer
//

class EventQueueTimer { };


//
// MSWindowsEventQueueBuffer
//

MSWindowsEventQueueBuffer::MSWindowsEventQueueBuffer(IEventQueue* events) :
    m_events(events)
{
    // remember thread.  we'll be posting messages to it.
    m_thread     = GetCurrentThreadId();

    // create a message type for custom events
    m_userEvent  = RegisterWindowMessage("BARRIER_USER_EVENT");

    // get message type for daemon quit
    m_daemonQuit = ArchMiscWindows::getDaemonQuitMessage();

    // make sure this thread has a message queue
    MSG dummy;
    PeekMessage(&dummy, NULL, WM_USER, WM_USER, PM_NOREMOVE);

    m_os_supported_message_types = QS_ALLINPUT;
    if (!IsWindows8OrGreater())
    {
        // don't use QS_POINTER, QS_TOUCH
        // because they can cause GetQueueStatus() to always return 0 and we miss events
        // since those flags are confusing Windows 7. See QTBUG-29097 for related info
        m_os_supported_message_types &= ~(QS_TOUCH | QS_POINTER);
    }
}

MSWindowsEventQueueBuffer::~MSWindowsEventQueueBuffer()
{
    // do nothing
}

void
MSWindowsEventQueueBuffer::waitForEvent(double timeout)
{
    // check if messages are available first.  if we don't do this then
    // MsgWaitForMultipleObjects() will block even if the queue isn't
    // empty if the messages in the queue were there before the last
    // call to GetMessage()/PeekMessage().
    if (!isEmpty()) {
        return;
    }

    // convert timeout
    DWORD t;
    if (timeout < 0.0) {
        t = INFINITE;
    }
    else {
        t = (DWORD)(1000.0 * timeout);
    }

    // wait for a message.  we cannot be interrupted by thread
    // cancellation but that's okay because we're run in the main
    // thread and we never cancel that thread.
    HANDLE dummy[1];
    MsgWaitForMultipleObjects(0, dummy, FALSE, t, m_os_supported_message_types);
}

IEventQueueBuffer::Type
MSWindowsEventQueueBuffer::getEvent(Event& event, UInt32& dataID)
{
    // peek at messages first.  waiting for QS_ALLINPUT will return
    // if a message has been sent to our window but GetMessage will
    // dispatch that message behind our backs and block.  PeekMessage
    // will also dispatch behind our backs but won't block.
    if (!PeekMessage(&m_event, NULL, 0, 0, PM_NOREMOVE) &&
        !PeekMessage(&m_event, (HWND)-1, 0, 0, PM_NOREMOVE)) {
        return kNone;
    }

    // BOOL.  yeah, right.
    BOOL result = GetMessage(&m_event, NULL, 0, 0);
    if (result == -1) {
        return kNone;
    }
    else if (result == 0) {
        event = Event(Event::kQuit);
        return kSystem;
    }
    else if (m_daemonQuit != 0 && m_event.message == m_daemonQuit) {
        event = Event(Event::kQuit);
        return kSystem;
    }
    else if (m_event.message == m_userEvent) {
        dataID = static_cast<UInt32>(m_event.wParam);
        return kUser;
    }
    else {
        event = Event(Event::kSystem,
                            m_events->getSystemTarget(), &m_event);
        return kSystem;
    }
}

bool
MSWindowsEventQueueBuffer::addEvent(UInt32 dataID)
{
    return (PostThreadMessage(m_thread, m_userEvent,
                            static_cast<WPARAM>(dataID), 0) != 0);
}

bool
MSWindowsEventQueueBuffer::isEmpty() const
{
    return (HIWORD(GetQueueStatus(m_os_supported_message_types)) == 0);
}

EventQueueTimer*
MSWindowsEventQueueBuffer::newTimer(double, bool) const
{
    return new EventQueueTimer;
}

void
MSWindowsEventQueueBuffer::deleteTimer(EventQueueTimer* timer) const
{
    delete timer;
}