00001 /* 00002 * synergy -- mouse and keyboard sharing utility 00003 * Copyright (C) 2004 Chris Schoeneman 00004 * 00005 * This package is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * found in the file COPYING that should have accompanied this file. 00008 * 00009 * This package is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 */ 00014 00015 #include "CSimpleEventQueueBuffer.h" 00016 #include "CStopwatch.h" 00017 #include "CArch.h" 00018 00019 class CEventQueueTimer { }; 00020 00021 // 00022 // CSimpleEventQueueBuffer 00023 // 00024 00025 CSimpleEventQueueBuffer::CSimpleEventQueueBuffer() 00026 { 00027 m_queueMutex = ARCH->newMutex(); 00028 m_queueReadyCond = ARCH->newCondVar(); 00029 m_queueReady = false; 00030 } 00031 00032 CSimpleEventQueueBuffer::~CSimpleEventQueueBuffer() 00033 { 00034 ARCH->closeCondVar(m_queueReadyCond); 00035 ARCH->closeMutex(m_queueMutex); 00036 } 00037 00038 void 00039 CSimpleEventQueueBuffer::waitForEvent(double timeout) 00040 { 00041 CArchMutexLock lock(m_queueMutex); 00042 CStopwatch timer(true); 00043 while (!m_queueReady) { 00044 double timeLeft = timeout; 00045 if (timeLeft >= 0.0) { 00046 timeLeft -= timer.getTime(); 00047 if (timeLeft < 0.0) { 00048 return; 00049 } 00050 } 00051 ARCH->waitCondVar(m_queueReadyCond, m_queueMutex, timeLeft); 00052 } 00053 } 00054 00055 IEventQueueBuffer::Type 00056 CSimpleEventQueueBuffer::getEvent(CEvent&, UInt32& dataID) 00057 { 00058 CArchMutexLock lock(m_queueMutex); 00059 if (!m_queueReady) { 00060 return kNone; 00061 } 00062 dataID = m_queue.back(); 00063 m_queue.pop_back(); 00064 m_queueReady = !m_queue.empty(); 00065 return kUser; 00066 } 00067 00068 bool 00069 CSimpleEventQueueBuffer::addEvent(UInt32 dataID) 00070 { 00071 CArchMutexLock lock(m_queueMutex); 00072 m_queue.push_front(dataID); 00073 if (!m_queueReady) { 00074 m_queueReady = true; 00075 ARCH->broadcastCondVar(m_queueReadyCond); 00076 } 00077 return true; 00078 } 00079 00080 bool 00081 CSimpleEventQueueBuffer::isEmpty() const 00082 { 00083 CArchMutexLock lock(m_queueMutex); 00084 return !m_queueReady; 00085 } 00086 00087 CEventQueueTimer* 00088 CSimpleEventQueueBuffer::newTimer(double, bool) const 00089 { 00090 return new CEventQueueTimer; 00091 } 00092 00093 void 00094 CSimpleEventQueueBuffer::deleteTimer(CEventQueueTimer* timer) const 00095 { 00096 delete timer; 00097 }