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

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