| 1 | /*
|
|---|
| 2 | * synergy -- mouse and keyboard sharing utility
|
|---|
| 3 | * Copyright (C) 2004 Chris Schoeneman
|
|---|
| 4 | * Copyright (C) 2006 Knut St. Osmundsen
|
|---|
| 5 | *
|
|---|
| 6 | * This package is free software; you can redistribute it and/or
|
|---|
| 7 | * modify it under the terms of the GNU General Public License
|
|---|
| 8 | * found in the file COPYING that should have accompanied this file.
|
|---|
| 9 | *
|
|---|
| 10 | * This package is distributed in the hope that it will be useful,
|
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | * GNU General Public License for more details.
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 | #include "CPMEventQueueBuffer.h"
|
|---|
| 17 | #include "CThread.h"
|
|---|
| 18 | #include "IEventQueue.h"
|
|---|
| 19 |
|
|---|
| 20 | //
|
|---|
| 21 | // CEventQueueTimer
|
|---|
| 22 | //
|
|---|
| 23 |
|
|---|
| 24 | class CEventQueueTimer { };
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 | //
|
|---|
| 28 | // CPMEventQueueBuffer
|
|---|
| 29 | //
|
|---|
| 30 |
|
|---|
| 31 | CPMEventQueueBuffer::CPMEventQueueBuffer()
|
|---|
| 32 | {
|
|---|
| 33 | // remember the queue (/ thread / hab). we'll be posting messages to it.
|
|---|
| 34 | m_hab = WinInitialize(0);
|
|---|
| 35 | m_hmq = WinCreateMsgQueue(m_hab, 0);
|
|---|
| 36 | assert(m_hmq != NULLHANDLE);
|
|---|
| 37 |
|
|---|
| 38 | // create a message type for custom events
|
|---|
| 39 | m_userEvent = WM_USER + 42 + 42 + 42;
|
|---|
| 40 |
|
|---|
| 41 | // get message type for daemon quit
|
|---|
| 42 | m_daemonQuit = WM_USER + 666;
|
|---|
| 43 |
|
|---|
| 44 | // create the event semaphore we'll be waiting on when waiting for messages.
|
|---|
| 45 | m_hev = NULLHANDLE;
|
|---|
| 46 | APIRET rc = DosCreateEventSem(NULL, &m_hev, 0, FALSE);
|
|---|
| 47 | assert(rc == NO_ERROR);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | CPMEventQueueBuffer::~CPMEventQueueBuffer()
|
|---|
| 51 | {
|
|---|
| 52 | // do nothing
|
|---|
| 53 | DosCloseEventSem(m_hev);
|
|---|
| 54 | WinDestroyMsgQueue(m_hmq);
|
|---|
| 55 | WinTerminate(m_hab);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | void
|
|---|
| 59 | CPMEventQueueBuffer::waitForEvent(double timeout)
|
|---|
| 60 | {
|
|---|
| 61 | // check if messages are available first.
|
|---|
| 62 | ULONG fStatus = WinQueryQueueStatus(HWND_DESKTOP);
|
|---|
| 63 | if (!fStatus) {
|
|---|
| 64 | // convert timeout and wait.
|
|---|
| 65 | ULONG ulPMTimeout = timeout < 0.0
|
|---|
| 66 | ? SEM_INDEFINITE_WAIT
|
|---|
| 67 | : (ULONG)(1000.0 * timeout);
|
|---|
| 68 | WinWaitEventSem(m_hev, ulPMTimeout);
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | IEventQueueBuffer::Type
|
|---|
| 73 | CPMEventQueueBuffer::getEvent(CEvent& event, UInt32& dataID)
|
|---|
| 74 | {
|
|---|
| 75 | // peek at messages first.
|
|---|
| 76 | QMSG qmsg;
|
|---|
| 77 | if (!WinPeekMsg(m_hab, &qmsg, NULLHANDLE, 0, 0, PM_NOREMOVE)) {
|
|---|
| 78 | return kNone;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | // get the message
|
|---|
| 82 | if ( !WinGetMsg(m_hab, &m_event, NULLHANDLE, 0, 0)
|
|---|
| 83 | || (m_event.msg == m_daemonQuit && m_daemonQuit != 0)) {
|
|---|
| 84 | event = CEvent(CEvent::kQuit);
|
|---|
| 85 | return kSystem;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | if (m_event.msg == m_userEvent) {
|
|---|
| 89 | dataID = (UInt32)(uintptr_t)m_event.mp1;
|
|---|
| 90 | return kUser;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | event = CEvent(CEvent::kSystem, IEventQueue::getSystemTarget(), &m_event);
|
|---|
| 94 | return kSystem;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | bool
|
|---|
| 98 | CPMEventQueueBuffer::addEvent(UInt32 dataID)
|
|---|
| 99 | {
|
|---|
| 100 | return WinPostQueueMsg(m_hmq, m_userEvent, (MPARAM)dataID, 0) != FALSE;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | bool
|
|---|
| 104 | CPMEventQueueBuffer::isEmpty() const
|
|---|
| 105 | {
|
|---|
| 106 | ULONG fStatus = WinQueryQueueStatus(HWND_DESKTOP);
|
|---|
| 107 | return fStatus != 0;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | CEventQueueTimer*
|
|---|
| 111 | CPMEventQueueBuffer::newTimer(double, bool) const
|
|---|
| 112 | {
|
|---|
| 113 | return new CEventQueueTimer;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | void
|
|---|
| 117 | CPMEventQueueBuffer::deleteTimer(CEventQueueTimer* timer) const
|
|---|
| 118 | {
|
|---|
| 119 | delete timer;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /*
|
|---|
| 123 | * Local Variables:
|
|---|
| 124 | * mode: c
|
|---|
| 125 | * c-file-style: "k&r"
|
|---|
| 126 | * c-basic-offset: 4
|
|---|
| 127 | * tab-width: 4
|
|---|
| 128 | * indent-tabs-mode: t
|
|---|
| 129 | * End:
|
|---|
| 130 | */
|
|---|
| 131 |
|
|---|