1 | /*
|
---|
2 | * synergy -- mouse and keyboard sharing utility
|
---|
3 | * Copyright (C) 2004 Chris Schoeneman
|
---|
4 | *
|
---|
5 | * This package is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU General Public License
|
---|
7 | * found in the file COPYING that should have accompanied this file.
|
---|
8 | *
|
---|
9 | * This package is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | */
|
---|
14 |
|
---|
15 | #include "CMSWindowsEventQueueBuffer.h"
|
---|
16 | #include "CThread.h"
|
---|
17 | #include "IEventQueue.h"
|
---|
18 | #include "CArchMiscWindows.h"
|
---|
19 |
|
---|
20 | //
|
---|
21 | // CEventQueueTimer
|
---|
22 | //
|
---|
23 |
|
---|
24 | class CEventQueueTimer { };
|
---|
25 |
|
---|
26 |
|
---|
27 | //
|
---|
28 | // CMSWindowsEventQueueBuffer
|
---|
29 | //
|
---|
30 |
|
---|
31 | CMSWindowsEventQueueBuffer::CMSWindowsEventQueueBuffer()
|
---|
32 | {
|
---|
33 | // remember thread. we'll be posting messages to it.
|
---|
34 | m_thread = GetCurrentThreadId();
|
---|
35 |
|
---|
36 | // create a message type for custom events
|
---|
37 | m_userEvent = RegisterWindowMessage("SYNERGY_USER_EVENT");
|
---|
38 |
|
---|
39 | // get message type for daemon quit
|
---|
40 | m_daemonQuit = CArchMiscWindows::getDaemonQuitMessage();
|
---|
41 |
|
---|
42 | // make sure this thread has a message queue
|
---|
43 | MSG dummy;
|
---|
44 | PeekMessage(&dummy, NULL, WM_USER, WM_USER, PM_NOREMOVE);
|
---|
45 | }
|
---|
46 |
|
---|
47 | CMSWindowsEventQueueBuffer::~CMSWindowsEventQueueBuffer()
|
---|
48 | {
|
---|
49 | // do nothing
|
---|
50 | }
|
---|
51 |
|
---|
52 | void
|
---|
53 | CMSWindowsEventQueueBuffer::waitForEvent(double timeout)
|
---|
54 | {
|
---|
55 | // check if messages are available first. if we don't do this then
|
---|
56 | // MsgWaitForMultipleObjects() will block even if the queue isn't
|
---|
57 | // empty if the messages in the queue were there before the last
|
---|
58 | // call to GetMessage()/PeekMessage().
|
---|
59 | if (HIWORD(GetQueueStatus(QS_ALLINPUT)) != 0) {
|
---|
60 | return;
|
---|
61 | }
|
---|
62 |
|
---|
63 | // convert timeout
|
---|
64 | DWORD t;
|
---|
65 | if (timeout < 0.0) {
|
---|
66 | t = INFINITE;
|
---|
67 | }
|
---|
68 | else {
|
---|
69 | t = (DWORD)(1000.0 * timeout);
|
---|
70 | }
|
---|
71 |
|
---|
72 | // wait for a message. we cannot be interrupted by thread
|
---|
73 | // cancellation but that's okay because we're run in the main
|
---|
74 | // thread and we never cancel that thread.
|
---|
75 | HANDLE dummy[1];
|
---|
76 | MsgWaitForMultipleObjects(0, dummy, FALSE, t, QS_ALLINPUT);
|
---|
77 | }
|
---|
78 |
|
---|
79 | IEventQueueBuffer::Type
|
---|
80 | CMSWindowsEventQueueBuffer::getEvent(CEvent& event, UInt32& dataID)
|
---|
81 | {
|
---|
82 | // peek at messages first. waiting for QS_ALLINPUT will return
|
---|
83 | // if a message has been sent to our window but GetMessage will
|
---|
84 | // dispatch that message behind our backs and block. PeekMessage
|
---|
85 | // will also dispatch behind our backs but won't block.
|
---|
86 | if (!PeekMessage(&m_event, NULL, 0, 0, PM_NOREMOVE) &&
|
---|
87 | !PeekMessage(&m_event, (HWND)-1, 0, 0, PM_NOREMOVE)) {
|
---|
88 | return kNone;
|
---|
89 | }
|
---|
90 |
|
---|
91 | // BOOL. yeah, right.
|
---|
92 | BOOL result = GetMessage(&m_event, NULL, 0, 0);
|
---|
93 | if (result == -1) {
|
---|
94 | return kNone;
|
---|
95 | }
|
---|
96 | else if (result == 0) {
|
---|
97 | event = CEvent(CEvent::kQuit);
|
---|
98 | return kSystem;
|
---|
99 | }
|
---|
100 | else if (m_daemonQuit != 0 && m_event.message == m_daemonQuit) {
|
---|
101 | event = CEvent(CEvent::kQuit);
|
---|
102 | return kSystem;
|
---|
103 | }
|
---|
104 | else if (m_event.message == m_userEvent) {
|
---|
105 | dataID = static_cast<UInt32>(m_event.wParam);
|
---|
106 | return kUser;
|
---|
107 | }
|
---|
108 | else {
|
---|
109 | event = CEvent(CEvent::kSystem,
|
---|
110 | IEventQueue::getSystemTarget(), &m_event);
|
---|
111 | return kSystem;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | bool
|
---|
116 | CMSWindowsEventQueueBuffer::addEvent(UInt32 dataID)
|
---|
117 | {
|
---|
118 | return (PostThreadMessage(m_thread, m_userEvent,
|
---|
119 | static_cast<WPARAM>(dataID), 0) != 0);
|
---|
120 | }
|
---|
121 |
|
---|
122 | bool
|
---|
123 | CMSWindowsEventQueueBuffer::isEmpty() const
|
---|
124 | {
|
---|
125 | return (HIWORD(GetQueueStatus(QS_ALLINPUT)) == 0);
|
---|
126 | }
|
---|
127 |
|
---|
128 | CEventQueueTimer*
|
---|
129 | CMSWindowsEventQueueBuffer::newTimer(double, bool) const
|
---|
130 | {
|
---|
131 | return new CEventQueueTimer;
|
---|
132 | }
|
---|
133 |
|
---|
134 | void
|
---|
135 | CMSWindowsEventQueueBuffer::deleteTimer(CEventQueueTimer* timer) const
|
---|
136 | {
|
---|
137 | delete timer;
|
---|
138 | }
|
---|