1 /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19 #include <rfb_win32/EventManager.h>
20 #include <rdr/Exception.h>
21 #include <rfb/LogWriter.h>
22
23 using namespace rfb;
24 using namespace rfb::win32;
25
26 static LogWriter vlog("EventManager");
27
28
EventManager()29 EventManager::EventManager() : eventCount(0) {
30 }
31
~EventManager()32 EventManager::~EventManager() {
33 }
34
35
addEvent(HANDLE event,EventHandler * ecb)36 bool EventManager::addEvent(HANDLE event, EventHandler* ecb) {
37 if (eventCount >= MAXIMUM_WAIT_OBJECTS-1)
38 return false;
39 events[eventCount] = event;
40 handlers[eventCount] = ecb;
41 eventCount++;
42 return true;
43 }
44
removeEvent(HANDLE event)45 void EventManager::removeEvent(HANDLE event) {
46 for (unsigned int i=0; i<eventCount; i++) {
47 if (events[i] == event) {
48 for (unsigned int j=i; j<eventCount-1; j++) {
49 events[j] = events[j+1];
50 handlers[j] = handlers[j+1];
51 }
52 eventCount--;
53 return;
54 }
55 }
56 throw rdr::Exception("Event not registered");
57 }
58
59
checkTimeouts()60 int EventManager::checkTimeouts() {
61 return 0;
62 }
63
getMessage(MSG * msg,HWND hwnd,UINT minMsg,UINT maxMsg)64 BOOL EventManager::getMessage(MSG* msg, HWND hwnd, UINT minMsg, UINT maxMsg) {
65 while (true) {
66 // - Process any pending timeouts
67 DWORD timeout = checkTimeouts();
68 if (timeout == 0)
69 timeout = INFINITE;
70
71 // - Events take precedence over messages
72 DWORD result;
73 if (eventCount) {
74 // - Check whether any events are set
75 result = WaitForMultipleObjects(eventCount, events, FALSE, 0);
76 if (result == WAIT_TIMEOUT) {
77 // - No events are set, so check for messages
78 if (PeekMessage(msg, hwnd, minMsg, maxMsg, PM_REMOVE))
79 return msg->message != WM_QUIT;
80
81 // - Block waiting for an event to be set, or a message
82 result = MsgWaitForMultipleObjects(eventCount, events, FALSE, timeout,
83 QS_ALLINPUT);
84 if (result == WAIT_OBJECT_0 + eventCount) {
85 // - Return the message, if any
86 if (PeekMessage(msg, hwnd, minMsg, maxMsg, PM_REMOVE))
87 return msg->message != WM_QUIT;
88 continue;
89 }
90 }
91 } else
92 return GetMessage(msg, hwnd, minMsg, maxMsg);
93
94 if ((result >= WAIT_OBJECT_0) && (result < (WAIT_OBJECT_0 + eventCount))) {
95 // - An event was set - call the handler
96 int index = result - WAIT_OBJECT_0;
97 handlers[index]->processEvent(events[index]);
98 } else if (result == WAIT_FAILED) {
99 // - An error has occurred, so return the error status code
100 return -1;
101 }
102 }
103 }
104