You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EventManager.cxx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <windows.h>
  22. #include <rfb_win32/EventManager.h>
  23. #include <rdr/Exception.h>
  24. #include <rfb/LogWriter.h>
  25. using namespace rfb;
  26. using namespace rfb::win32;
  27. static LogWriter vlog("EventManager");
  28. EventManager::EventManager() : eventCount(0) {
  29. }
  30. EventManager::~EventManager() {
  31. }
  32. bool EventManager::addEvent(HANDLE event, EventHandler* ecb) {
  33. if (eventCount >= MAXIMUM_WAIT_OBJECTS-1)
  34. return false;
  35. events[eventCount] = event;
  36. handlers[eventCount] = ecb;
  37. eventCount++;
  38. return true;
  39. }
  40. void EventManager::removeEvent(HANDLE event) {
  41. for (unsigned int i=0; i<eventCount; i++) {
  42. if (events[i] == event) {
  43. for (unsigned int j=i; j<eventCount-1; j++) {
  44. events[j] = events[j+1];
  45. handlers[j] = handlers[j+1];
  46. }
  47. eventCount--;
  48. return;
  49. }
  50. }
  51. throw rdr::Exception("Event not registered");
  52. }
  53. int EventManager::checkTimeouts() {
  54. return 0;
  55. }
  56. BOOL EventManager::getMessage(MSG* msg, HWND hwnd, UINT minMsg, UINT maxMsg) {
  57. while (true) {
  58. // - Process any pending timeouts
  59. DWORD timeout = checkTimeouts();
  60. if (timeout == 0)
  61. timeout = INFINITE;
  62. // - Events take precedence over messages
  63. DWORD result;
  64. if (eventCount) {
  65. // - Check whether any events are set
  66. result = WaitForMultipleObjects(eventCount, events, FALSE, 0);
  67. if (result == WAIT_TIMEOUT) {
  68. // - No events are set, so check for messages
  69. if (PeekMessage(msg, hwnd, minMsg, maxMsg, PM_REMOVE))
  70. return msg->message != WM_QUIT;
  71. // - Block waiting for an event to be set, or a message
  72. result = MsgWaitForMultipleObjects(eventCount, events, FALSE, timeout,
  73. QS_ALLINPUT);
  74. if (result == WAIT_OBJECT_0 + eventCount) {
  75. // - Return the message, if any
  76. if (PeekMessage(msg, hwnd, minMsg, maxMsg, PM_REMOVE))
  77. return msg->message != WM_QUIT;
  78. continue;
  79. }
  80. }
  81. } else
  82. return GetMessage(msg, hwnd, minMsg, maxMsg);
  83. if (result < (WAIT_OBJECT_0 + eventCount)) {
  84. // - An event was set - call the handler
  85. int index = result - WAIT_OBJECT_0;
  86. handlers[index]->processEvent(events[index]);
  87. } else if (result == WAIT_FAILED) {
  88. // - An error has occurred, so return the error status code
  89. return -1;
  90. }
  91. }
  92. }