source: trunk/synergy/lib/platform/CMSWindowsEventQueueBuffer.cpp

Last change on this file was 2749, checked in by bird, 19 years ago

synergy v1.3.1 sources (zip).

File size: 3.5 KB
Line 
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
24class CEventQueueTimer { };
25
26
27//
28// CMSWindowsEventQueueBuffer
29//
30
31CMSWindowsEventQueueBuffer::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
47CMSWindowsEventQueueBuffer::~CMSWindowsEventQueueBuffer()
48{
49 // do nothing
50}
51
52void
53CMSWindowsEventQueueBuffer::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
79IEventQueueBuffer::Type
80CMSWindowsEventQueueBuffer::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
115bool
116CMSWindowsEventQueueBuffer::addEvent(UInt32 dataID)
117{
118 return (PostThreadMessage(m_thread, m_userEvent,
119 static_cast<WPARAM>(dataID), 0) != 0);
120}
121
122bool
123CMSWindowsEventQueueBuffer::isEmpty() const
124{
125 return (HIWORD(GetQueueStatus(QS_ALLINPUT)) == 0);
126}
127
128CEventQueueTimer*
129CMSWindowsEventQueueBuffer::newTimer(double, bool) const
130{
131 return new CEventQueueTimer;
132}
133
134void
135CMSWindowsEventQueueBuffer::deleteTimer(CEventQueueTimer* timer) const
136{
137 delete timer;
138}
Note: See TracBrowser for help on using the repository browser.