Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

EventManager.cxx 3.1KB

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