| 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 "CSimpleEventQueueBuffer.h" | 
|---|
| 16 | #include "CStopwatch.h" | 
|---|
| 17 | #include "CArch.h" | 
|---|
| 18 |  | 
|---|
| 19 | class CEventQueueTimer { }; | 
|---|
| 20 |  | 
|---|
| 21 | // | 
|---|
| 22 | // CSimpleEventQueueBuffer | 
|---|
| 23 | // | 
|---|
| 24 |  | 
|---|
| 25 | CSimpleEventQueueBuffer::CSimpleEventQueueBuffer() | 
|---|
| 26 | { | 
|---|
| 27 | m_queueMutex     = ARCH->newMutex(); | 
|---|
| 28 | m_queueReadyCond = ARCH->newCondVar(); | 
|---|
| 29 | m_queueReady     = false; | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | CSimpleEventQueueBuffer::~CSimpleEventQueueBuffer() | 
|---|
| 33 | { | 
|---|
| 34 | ARCH->closeCondVar(m_queueReadyCond); | 
|---|
| 35 | ARCH->closeMutex(m_queueMutex); | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | void | 
|---|
| 39 | CSimpleEventQueueBuffer::waitForEvent(double timeout) | 
|---|
| 40 | { | 
|---|
| 41 | CArchMutexLock lock(m_queueMutex); | 
|---|
| 42 | CStopwatch timer(true); | 
|---|
| 43 | while (!m_queueReady) { | 
|---|
| 44 | double timeLeft = timeout; | 
|---|
| 45 | if (timeLeft >= 0.0) { | 
|---|
| 46 | timeLeft -= timer.getTime(); | 
|---|
| 47 | if (timeLeft < 0.0) { | 
|---|
| 48 | return; | 
|---|
| 49 | } | 
|---|
| 50 | } | 
|---|
| 51 | ARCH->waitCondVar(m_queueReadyCond, m_queueMutex, timeLeft); | 
|---|
| 52 | } | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | IEventQueueBuffer::Type | 
|---|
| 56 | CSimpleEventQueueBuffer::getEvent(CEvent&, UInt32& dataID) | 
|---|
| 57 | { | 
|---|
| 58 | CArchMutexLock lock(m_queueMutex); | 
|---|
| 59 | if (!m_queueReady) { | 
|---|
| 60 | return kNone; | 
|---|
| 61 | } | 
|---|
| 62 | dataID = m_queue.back(); | 
|---|
| 63 | m_queue.pop_back(); | 
|---|
| 64 | m_queueReady = !m_queue.empty(); | 
|---|
| 65 | return kUser; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | bool | 
|---|
| 69 | CSimpleEventQueueBuffer::addEvent(UInt32 dataID) | 
|---|
| 70 | { | 
|---|
| 71 | CArchMutexLock lock(m_queueMutex); | 
|---|
| 72 | m_queue.push_front(dataID); | 
|---|
| 73 | if (!m_queueReady) { | 
|---|
| 74 | m_queueReady = true; | 
|---|
| 75 | ARCH->broadcastCondVar(m_queueReadyCond); | 
|---|
| 76 | } | 
|---|
| 77 | return true; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | bool | 
|---|
| 81 | CSimpleEventQueueBuffer::isEmpty() const | 
|---|
| 82 | { | 
|---|
| 83 | CArchMutexLock lock(m_queueMutex); | 
|---|
| 84 | return !m_queueReady; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | CEventQueueTimer* | 
|---|
| 88 | CSimpleEventQueueBuffer::newTimer(double, bool) const | 
|---|
| 89 | { | 
|---|
| 90 | return new CEventQueueTimer; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | void | 
|---|
| 94 | CSimpleEventQueueBuffer::deleteTimer(CEventQueueTimer* timer) const | 
|---|
| 95 | { | 
|---|
| 96 | delete timer; | 
|---|
| 97 | } | 
|---|