00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "CMSWindowsEventQueueBuffer.h"
00016 #include "CThread.h"
00017 #include "IEventQueue.h"
00018 #include "CArchMiscWindows.h"
00019
00020
00021
00022
00023
00024 class CEventQueueTimer { };
00025
00026
00027
00028
00029
00030
00031 CMSWindowsEventQueueBuffer::CMSWindowsEventQueueBuffer()
00032 {
00033
00034 m_thread = GetCurrentThreadId();
00035
00036
00037 m_userEvent = RegisterWindowMessage("SYNERGY_USER_EVENT");
00038
00039
00040 m_daemonQuit = CArchMiscWindows::getDaemonQuitMessage();
00041
00042
00043 MSG dummy;
00044 PeekMessage(&dummy, NULL, WM_USER, WM_USER, PM_NOREMOVE);
00045 }
00046
00047 CMSWindowsEventQueueBuffer::~CMSWindowsEventQueueBuffer()
00048 {
00049
00050 }
00051
00052 void
00053 CMSWindowsEventQueueBuffer::waitForEvent(double timeout)
00054 {
00055
00056
00057
00058
00059 if (HIWORD(GetQueueStatus(QS_ALLINPUT)) != 0) {
00060 return;
00061 }
00062
00063
00064 DWORD t;
00065 if (timeout < 0.0) {
00066 t = INFINITE;
00067 }
00068 else {
00069 t = (DWORD)(1000.0 * timeout);
00070 }
00071
00072
00073
00074
00075 HANDLE dummy[1];
00076 MsgWaitForMultipleObjects(0, dummy, FALSE, t, QS_ALLINPUT);
00077 }
00078
00079 IEventQueueBuffer::Type
00080 CMSWindowsEventQueueBuffer::getEvent(CEvent& event, UInt32& dataID)
00081 {
00082
00083
00084
00085
00086 if (!PeekMessage(&m_event, NULL, 0, 0, PM_NOREMOVE) &&
00087 !PeekMessage(&m_event, (HWND)-1, 0, 0, PM_NOREMOVE)) {
00088 return kNone;
00089 }
00090
00091
00092 BOOL result = GetMessage(&m_event, NULL, 0, 0);
00093 if (result == -1) {
00094 return kNone;
00095 }
00096 else if (result == 0) {
00097 event = CEvent(CEvent::kQuit);
00098 return kSystem;
00099 }
00100 else if (m_daemonQuit != 0 && m_event.message == m_daemonQuit) {
00101 event = CEvent(CEvent::kQuit);
00102 return kSystem;
00103 }
00104 else if (m_event.message == m_userEvent) {
00105 dataID = static_cast<UInt32>(m_event.wParam);
00106 return kUser;
00107 }
00108 else {
00109 event = CEvent(CEvent::kSystem,
00110 IEventQueue::getSystemTarget(), &m_event);
00111 return kSystem;
00112 }
00113 }
00114
00115 bool
00116 CMSWindowsEventQueueBuffer::addEvent(UInt32 dataID)
00117 {
00118 return (PostThreadMessage(m_thread, m_userEvent,
00119 static_cast<WPARAM>(dataID), 0) != 0);
00120 }
00121
00122 bool
00123 CMSWindowsEventQueueBuffer::isEmpty() const
00124 {
00125 return (HIWORD(GetQueueStatus(QS_ALLINPUT)) == 0);
00126 }
00127
00128 CEventQueueTimer*
00129 CMSWindowsEventQueueBuffer::newTimer(double, bool) const
00130 {
00131 return new CEventQueueTimer;
00132 }
00133
00134 void
00135 CMSWindowsEventQueueBuffer::deleteTimer(CEventQueueTimer* timer) const
00136 {
00137 delete timer;
00138 }